Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions crates/chainrules/src/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub fn sub_frule<S: ScalarAd>(x: S, y: S, dx: S, dy: S) -> (S, S) {
/// assert_eq!(dy, -2.0_f64);
/// ```
pub fn sub_rrule<S: ScalarAd>(cotangent: S) -> (S, S) {
(cotangent, S::from_i32(-1) * cotangent)
(cotangent, -cotangent)
}

/// Primal `mul`.
Expand Down Expand Up @@ -176,7 +176,7 @@ pub fn div_frule<S: ScalarAd>(x: S, y: S, dx: S, dy: S) -> (S, S) {
let primal = x / y;
let inv_y = S::from_i32(1) / y;
let dfdx = inv_y.conj();
let dfdy = (S::from_i32(-1) * x * inv_y * inv_y).conj();
let dfdy = (-(x * inv_y * inv_y)).conj();
let tangent = dx * dfdx + dy * dfdy;
(primal, tangent)
}
Expand All @@ -200,6 +200,6 @@ pub fn div_frule<S: ScalarAd>(x: S, y: S, dx: S, dy: S) -> (S, S) {
pub fn div_rrule<S: ScalarAd>(x: S, y: S, cotangent: S) -> (S, S) {
let inv_y = S::from_i32(1) / y;
let dfdx = inv_y.conj();
let dfdy = (S::from_i32(-1) * x * inv_y * inv_y).conj();
let dfdy = (-(x * inv_y * inv_y)).conj();
(cotangent * dfdx, cotangent * dfdy)
}
5 changes: 5 additions & 0 deletions crates/chainrules/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
//! assert_eq!(dx, 12.0);
//! ```

pub use chainrules_core::{
AdResult, AutodiffError, Differentiable, ForwardRule, NodeId, PullbackEntry,
PullbackWithTangentsEntry, ReverseRule, SavePolicy,
};

mod binary;
mod power;
mod real_ops;
Expand Down
10 changes: 8 additions & 2 deletions crates/chainrules/src/scalar_ad.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use core::ops::{Add, Div, Mul, Sub};
use core::ops::{Add, Div, Mul, Neg, Sub};

use num_complex::{Complex32, Complex64};
use num_traits::Float;
Expand All @@ -16,7 +16,13 @@ use num_traits::Float;
/// takes_scalar(1.0_f64);
/// ```
pub trait ScalarAd:
Copy + PartialEq + Add<Output = Self> + Sub<Output = Self> + Mul<Output = Self> + Div<Output = Self>
Copy
+ PartialEq
+ Neg<Output = Self>
+ Add<Output = Self>
+ Sub<Output = Self>
+ Mul<Output = Self>
+ Div<Output = Self>
{
/// Real exponent type for `powf`.
type Real: Copy + Float;
Expand Down
4 changes: 0 additions & 4 deletions crates/chainrules/src/unary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ fn one<S: ScalarAd>() -> S {
S::from_i32(1)
}

fn neg_one<S: ScalarAd>() -> S {
S::from_i32(-1)
}

pub use basic::{conj, conj_frule, conj_rrule, sqrt, sqrt_frule, sqrt_rrule};
pub use exp_log::{
exp, exp_frule, exp_rrule, expm1, expm1_frule, expm1_rrule, log, log1p, log1p_frule,
Expand Down
10 changes: 5 additions & 5 deletions crates/chainrules/src/unary/trig.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::unary::{neg_one, one};
use crate::unary::one;
use crate::ScalarAd;

/// Primal `sin`.
Expand All @@ -25,12 +25,12 @@ pub fn cos<S: ScalarAd>(x: S) -> S {
/// Forward rule for `cos`.
pub fn cos_frule<S: ScalarAd>(x: S, dx: S) -> (S, S) {
let y = x.cos();
(y, dx * (neg_one::<S>() * x.sin()).conj())
(y, dx * (-x.sin()).conj())
}

/// Reverse rule for `cos`.
pub fn cos_rrule<S: ScalarAd>(x: S, cotangent: S) -> S {
cotangent * (neg_one::<S>() * x.sin()).conj()
cotangent * (-x.sin()).conj()
}

fn inverse_sqrt_one_minus_square<S: ScalarAd>(x: S) -> S {
Expand Down Expand Up @@ -62,13 +62,13 @@ pub fn acos<S: ScalarAd>(x: S) -> S {
/// Forward rule for `acos`.
pub fn acos_frule<S: ScalarAd>(x: S, dx: S) -> (S, S) {
let y = x.acos();
let scale = neg_one::<S>() * inverse_sqrt_one_minus_square(x);
let scale = -inverse_sqrt_one_minus_square(x);
(y, dx * scale.conj())
}

/// Reverse rule for `acos`.
pub fn acos_rrule<S: ScalarAd>(x: S, cotangent: S) -> S {
cotangent * (neg_one::<S>() * inverse_sqrt_one_minus_square(x)).conj()
cotangent * (-inverse_sqrt_one_minus_square(x)).conj()
}

/// Primal `atan`.
Expand Down
Loading