DualNumber

Struct DualNumber 

Source
pub struct DualNumber<T>
where T: Float,
{ pub real: T, pub dual: T, }
Expand description

A dual number for automatic differentiation

Dual numbers enable exact derivative computation using the algebraic property ε² = 0. For a function f(x), evaluating f(x + ε) automatically computes both f(x) and f’(x).

§Examples

use amari_dual::DualNumber;

// Create a variable x = 3.0 (with derivative dx/dx = 1.0)
let x = DualNumber::variable(3.0);

// Compute f(x) = x² + 2x + 1
let result = x * x + DualNumber::constant(2.0) * x + DualNumber::constant(1.0);

// result.real = f(3) = 9 + 6 + 1 = 16
// result.dual = f'(3) = 2(3) + 2 = 8
assert_eq!(result.real, 16.0);
assert_eq!(result.dual, 8.0);

Fields§

§real: T

The real part (function value)

§dual: T

The dual part (derivative)

Implementations§

Source§

impl<T> DualNumber<T>
where T: Float,

Source

pub fn new(real: T, dual: T) -> DualNumber<T>

Create a new dual number

Source

pub fn constant(value: T) -> DualNumber<T>

Create a constant (derivative = 0)

Constants have zero derivative since d/dx(c) = 0.

Source

pub fn variable(value: T) -> DualNumber<T>

Create a variable (derivative = 1)

Variables have unit derivative since d/dx(x) = 1.

Source

pub fn value(&self) -> T

Get the value (real part)

Source

pub fn derivative(&self) -> T

Get the derivative (dual part)

Source

pub fn exp(self) -> DualNumber<T>

Exponential function: exp(a + b·ε) = exp(a) + b·exp(a)·ε

Uses the chain rule: d/dx(e^f) = f’·e^f

Source

pub fn ln(self) -> DualNumber<T>

Natural logarithm: ln(a + b·ε) = ln(a) + (b/a)·ε

Uses the chain rule: d/dx(ln f) = f’/f

Source

pub fn sin(self) -> DualNumber<T>

Sine function: sin(a + b·ε) = sin(a) + b·cos(a)·ε

Uses the chain rule: d/dx(sin f) = f’·cos(f)

Source

pub fn cos(self) -> DualNumber<T>

Cosine function: cos(a + b·ε) = cos(a) - b·sin(a)·ε

Uses the chain rule: d/dx(cos f) = -f’·sin(f)

Source

pub fn tan(self) -> DualNumber<T>

Tangent function: tan(a + b·ε) = tan(a) + b·sec²(a)·ε

Uses the chain rule: d/dx(tan f) = f’·sec²(f) = f’/cos²(f)

Source

pub fn sqrt(self) -> DualNumber<T>

Square root: sqrt(a + b·ε) = sqrt(a) + (b/(2·sqrt(a)))·ε

Uses the chain rule: d/dx(√f) = f’/(2√f)

Source

pub fn powf(self, n: T) -> DualNumber<T>

Power function: (a + b·ε)^n = a^n + n·b·a^(n-1)·ε

Uses the power rule: d/dx(f^n) = n·f’·f^(n-1)

Source

pub fn powi(self, n: i32) -> DualNumber<T>

Integer power function: (a + b·ε)^n = a^n + n·b·a^(n-1)·ε

Uses the power rule: d/dx(f^n) = n·f’·f^(n-1)

Source

pub fn abs(self) -> DualNumber<T>

Absolute value: |a + b·ε| = |a| + b·sign(a)·ε

Derivative is sign(a), undefined at a=0

Source

pub fn sinh(self) -> DualNumber<T>

Hyperbolic sine: sinh(a + b·ε) = sinh(a) + b·cosh(a)·ε

Source

pub fn cosh(self) -> DualNumber<T>

Hyperbolic cosine: cosh(a + b·ε) = cosh(a) + b·sinh(a)·ε

Source

pub fn tanh(self) -> DualNumber<T>

Hyperbolic tangent: tanh(a + b·ε) = tanh(a) + b·sech²(a)·ε

Source

pub fn max(self, other: DualNumber<T>) -> DualNumber<T>

Maximum of two dual numbers (non-differentiable at equality)

Source

pub fn min(self, other: DualNumber<T>) -> DualNumber<T>

Minimum of two dual numbers (non-differentiable at equality)

Source

pub fn sigmoid(self) -> DualNumber<T>

Sigmoid (logistic) function: σ(x) = 1/(1 + e^(-x))

Uses the chain rule: d/dx(σ(f)) = σ(f)·(1 - σ(f))·f’

Source

pub fn apply_with_derivative<F, G>(self, f: F, df: G) -> DualNumber<T>
where F: Fn(T) -> T, G: Fn(T) -> T,

Apply a function with its derivative

This is useful for applying functions where you know both f(x) and f’(x). The chain rule is applied automatically.

§Arguments
  • f - The function to apply
  • df - The derivative of the function

Trait Implementations§

Source§

impl<T> Add for DualNumber<T>
where T: Float,

Source§

type Output = DualNumber<T>

The resulting type after applying the + operator.
Source§

fn add(self, other: DualNumber<T>) -> DualNumber<T>

Performs the + operation. Read more
Source§

impl<T> Clone for DualNumber<T>
where T: Clone + Float,

Source§

fn clone(&self) -> DualNumber<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for DualNumber<T>
where T: Debug + Float,

Source§

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

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

impl<T> Display for DualNumber<T>
where T: Float + Display,

Source§

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

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

impl<T> Div for DualNumber<T>
where T: Float,

Source§

type Output = DualNumber<T>

The resulting type after applying the / operator.
Source§

fn div(self, other: DualNumber<T>) -> DualNumber<T>

Performs the / operation. Read more
Source§

impl<T, const P: usize, const Q: usize, const R: usize> Mul<DualNumber<T>> for DualMultivector<T, P, Q, R>
where T: Float,

Source§

type Output = DualMultivector<T, P, Q, R>

The resulting type after applying the * operator.
Source§

fn mul(self, scalar: DualNumber<T>) -> DualMultivector<T, P, Q, R>

Performs the * operation. Read more
Source§

impl<T> Mul for DualNumber<T>
where T: Float,

Source§

type Output = DualNumber<T>

The resulting type after applying the * operator.
Source§

fn mul(self, other: DualNumber<T>) -> DualNumber<T>

Performs the * operation. Read more
Source§

impl<T> Neg for DualNumber<T>
where T: Float,

Source§

type Output = DualNumber<T>

The resulting type after applying the - operator.
Source§

fn neg(self) -> DualNumber<T>

Performs the unary - operation. Read more
Source§

impl<T> One for DualNumber<T>
where T: Float,

Source§

fn one() -> DualNumber<T>

Returns the multiplicative identity element of Self, 1. Read more
Source§

fn set_one(&mut self)

Sets self to the multiplicative identity element of Self, 1.
Source§

fn is_one(&self) -> bool
where Self: PartialEq,

Returns true if self is equal to the multiplicative identity. Read more
Source§

impl<T> PartialEq for DualNumber<T>
where T: PartialEq + Float,

Source§

fn eq(&self, other: &DualNumber<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T> Sub for DualNumber<T>
where T: Float,

Source§

type Output = DualNumber<T>

The resulting type after applying the - operator.
Source§

fn sub(self, other: DualNumber<T>) -> DualNumber<T>

Performs the - operation. Read more
Source§

impl<T> Zero for DualNumber<T>
where T: Float,

Source§

fn zero() -> DualNumber<T>

Returns the additive identity element of Self, 0. Read more
Source§

fn is_zero(&self) -> bool

Returns true if self is equal to the additive identity.
Source§

fn set_zero(&mut self)

Sets self to the additive identity element of Self, 0.
Source§

impl<T> Copy for DualNumber<T>
where T: Copy + Float,

Source§

impl<T> StructuralPartialEq for DualNumber<T>
where T: Float,

Auto Trait Implementations§

§

impl<T> Freeze for DualNumber<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for DualNumber<T>
where T: RefUnwindSafe,

§

impl<T> Send for DualNumber<T>
where T: Send,

§

impl<T> Sync for DualNumber<T>
where T: Sync,

§

impl<T> Unpin for DualNumber<T>
where T: Unpin,

§

impl<T> UnwindSafe for DualNumber<T>
where T: UnwindSafe,

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<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> 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> Same for T

Source§

type Output = T

Should always be Self
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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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<T> ClosedNeg for T
where T: Neg<Output = T>,

Source§

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