Skip to main content

Dual

Struct Dual 

Source
pub struct Dual {
    pub value: f64,
    pub tangent: f64,
}
Expand description

A forward-mode dual number: a value (primal) paired with a tangent (directional derivative).

Running a computation written against Scalar on Dual propagates the derivative through every operation via the chain rule. Seed an input’s tangent to 1.0 with Dual::seed, then extract the (value, derivative) pair.

Both PartialEq and PartialOrd compare the value (primal) field only, so equality and ordering agree on the “primal decides control flow” semantics (see the module-level docs). Two Duals with equal value but different tangents therefore compare equal and unordered-as-Equal; this keeps a == b ⟺ a.partial_cmp(&b) == Some(Equal) — the std contract that a derived (both-field) PartialEq would violate against the value-only PartialOrd.

Fields§

§value: f64

The primal value of the computation.

§tangent: f64

The tangent (directional derivative) accumulated by the chain rule.

Implementations§

Source§

impl Dual

Source

pub fn seed(value: f64) -> Self

Seed an independent variable: value = x, tangent = 1.0.

Use this on the input you are differentiating with respect to.

Source

pub fn constant(value: f64) -> Self

A constant: value = x, tangent = 0.0 (no gradient flows through it).

Source

pub fn extract(self) -> (f64, f64)

Extract the (value, derivative) pair after a computation.

Trait Implementations§

Source§

impl Add for Dual

Source§

type Output = Dual

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self

Performs the + operation. Read more
Source§

impl AddAssign for Dual

Source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
Source§

impl Clone for Dual

Source§

fn clone(&self) -> Dual

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Dual

Source§

impl Debug for Dual

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Div for Dual

Source§

type Output = Dual

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Self) -> Self

Performs the / operation. Read more
Source§

impl Mul for Dual

Source§

type Output = Dual

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Self) -> Self

Performs the * operation. Read more
Source§

impl MulAssign for Dual

Source§

fn mul_assign(&mut self, rhs: Self)

Performs the *= operation. Read more
Source§

impl Neg for Dual

Source§

type Output = Dual

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self

Performs the unary - operation. Read more
Source§

impl PartialEq for Dual

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PartialOrd for Dual

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Scalar for Dual

Source§

fn zero() -> Self

The additive identity (0).
Source§

fn one() -> Self

The multiplicative identity (1).
Source§

fn from_f64(v: f64) -> Self

Construct a constant from an f64 (tangent 0 for Dual).
Source§

fn infinity() -> Self

Positive infinity sentinel (used e.g. for DP recurrence initialization).
Source§

fn sqrt(self) -> Self

Square root. Requires a non-negative value; the derivative diverges at 0.
Source§

fn exp(self) -> Self

Natural exponential.
Source§

fn ln(self) -> Self

Natural logarithm. Requires a strictly positive value.
Source§

fn sin(self) -> Self

Sine.
Source§

fn cos(self) -> Self

Cosine.
Source§

fn powf(self, p: f64) -> Self

Raise to a concrete f64 power. The tangent uses p * v^(p-1); at value == 0.0 with p < 1.0 this may be NaN/Inf.
Source§

fn abs(self) -> Self

Absolute value. The tangent uses the subdifferential convention d/dx |v| = signum(v), with the honest at-zero selection signum(0) = 0: at exactly value == 0.0 the tangent is 0.0 (the midpoint of the subdifferential [-1, 1]). The value is v.abs() (bit-for-bit f64 parity).
Source§

fn signum(self) -> Self

Sign. Piecewise-constant, so the tangent is 0.0 everywhere. The value is f64::signum(v) (+1.0 at +0.0, -1.0 at -0.0) to preserve f64 parity.
Source§

impl Sub for Dual

Source§

type Output = Dual

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Self) -> Self

Performs the - operation. Read more
Source§

impl SubAssign for Dual

Source§

fn sub_assign(&mut self, rhs: Self)

Performs the -= operation. Read more

Auto Trait Implementations§

§

impl Freeze for Dual

§

impl RefUnwindSafe for Dual

§

impl Send for Dual

§

impl Sync for Dual

§

impl Unpin for Dual

§

impl UnsafeUnpin for Dual

§

impl UnwindSafe for Dual

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T, Right> ClosedAdd<Right> for T
where T: Add<Right, Output = T> + AddAssign<Right>,

Source§

impl<T, Right> ClosedAddAssign<Right> for T
where T: ClosedAdd<Right> + AddAssign<Right>,

Source§

impl<T, Right> ClosedMul<Right> for T
where T: Mul<Right, Output = T> + MulAssign<Right>,

Source§

impl<T, Right> ClosedMulAssign<Right> for T
where T: ClosedMul<Right> + MulAssign<Right>,

Source§

impl<T> ClosedNeg for T
where T: Neg<Output = T>,

Source§

impl<T, Right> ClosedSub<Right> for T
where T: Sub<Right, Output = T> + SubAssign<Right>,

Source§

impl<T, Right> ClosedSubAssign<Right> for T
where T: ClosedSub<Right> + SubAssign<Right>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> Scalar for T
where T: 'static + Clone + PartialEq + Debug,

Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V