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: TThe real part (function value)
dual: TThe dual part (derivative)
Implementations§
Source§impl<T> DualNumber<T>where
T: Float,
impl<T> DualNumber<T>where
T: Float,
Sourcepub fn new(real: T, dual: T) -> DualNumber<T>
pub fn new(real: T, dual: T) -> DualNumber<T>
Create a new dual number
Sourcepub fn constant(value: T) -> DualNumber<T>
pub fn constant(value: T) -> DualNumber<T>
Create a constant (derivative = 0)
Constants have zero derivative since d/dx(c) = 0.
Sourcepub fn variable(value: T) -> DualNumber<T>
pub fn variable(value: T) -> DualNumber<T>
Create a variable (derivative = 1)
Variables have unit derivative since d/dx(x) = 1.
Sourcepub fn derivative(&self) -> T
pub fn derivative(&self) -> T
Get the derivative (dual part)
Sourcepub fn exp(self) -> DualNumber<T>
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
Sourcepub fn ln(self) -> DualNumber<T>
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
Sourcepub fn sin(self) -> DualNumber<T>
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)
Sourcepub fn cos(self) -> DualNumber<T>
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)
Sourcepub fn tan(self) -> DualNumber<T>
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)
Sourcepub fn sqrt(self) -> DualNumber<T>
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)
Sourcepub fn powf(self, n: T) -> DualNumber<T>
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)
Sourcepub fn powi(self, n: i32) -> DualNumber<T>
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)
Sourcepub fn abs(self) -> DualNumber<T>
pub fn abs(self) -> DualNumber<T>
Absolute value: |a + b·ε| = |a| + b·sign(a)·ε
Derivative is sign(a), undefined at a=0
Sourcepub fn sinh(self) -> DualNumber<T>
pub fn sinh(self) -> DualNumber<T>
Hyperbolic sine: sinh(a + b·ε) = sinh(a) + b·cosh(a)·ε
Sourcepub fn cosh(self) -> DualNumber<T>
pub fn cosh(self) -> DualNumber<T>
Hyperbolic cosine: cosh(a + b·ε) = cosh(a) + b·sinh(a)·ε
Sourcepub fn tanh(self) -> DualNumber<T>
pub fn tanh(self) -> DualNumber<T>
Hyperbolic tangent: tanh(a + b·ε) = tanh(a) + b·sech²(a)·ε
Sourcepub fn max(self, other: DualNumber<T>) -> DualNumber<T>
pub fn max(self, other: DualNumber<T>) -> DualNumber<T>
Maximum of two dual numbers (non-differentiable at equality)
Sourcepub fn min(self, other: DualNumber<T>) -> DualNumber<T>
pub fn min(self, other: DualNumber<T>) -> DualNumber<T>
Minimum of two dual numbers (non-differentiable at equality)
Sourcepub fn sigmoid(self) -> DualNumber<T>
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’
Sourcepub fn apply_with_derivative<F, G>(self, f: F, df: G) -> DualNumber<T>
pub fn apply_with_derivative<F, G>(self, f: F, df: G) -> DualNumber<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 applydf- The derivative of the function
Trait Implementations§
Source§impl<T> Add for DualNumber<T>where
T: Float,
impl<T> Add for DualNumber<T>where
T: Float,
Source§type Output = DualNumber<T>
type Output = DualNumber<T>
+ operator.Source§fn add(self, other: DualNumber<T>) -> DualNumber<T>
fn add(self, other: DualNumber<T>) -> DualNumber<T>
+ operation. Read moreSource§impl<T> Clone for DualNumber<T>
impl<T> Clone for DualNumber<T>
Source§fn clone(&self) -> DualNumber<T>
fn clone(&self) -> DualNumber<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T> Debug for DualNumber<T>
impl<T> Debug for DualNumber<T>
Source§impl<T> Display for DualNumber<T>
impl<T> Display for DualNumber<T>
Source§impl<T> Div for DualNumber<T>where
T: Float,
impl<T> Div for DualNumber<T>where
T: Float,
Source§type Output = DualNumber<T>
type Output = DualNumber<T>
/ operator.Source§fn div(self, other: DualNumber<T>) -> DualNumber<T>
fn div(self, other: DualNumber<T>) -> DualNumber<T>
/ operation. Read moreSource§impl<T, const P: usize, const Q: usize, const R: usize> Mul<DualNumber<T>> for DualMultivector<T, P, Q, R>where
T: Float,
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>
type Output = DualMultivector<T, P, Q, R>
* operator.Source§fn mul(self, scalar: DualNumber<T>) -> DualMultivector<T, P, Q, R>
fn mul(self, scalar: DualNumber<T>) -> DualMultivector<T, P, Q, R>
* operation. Read moreSource§impl<T> Mul for DualNumber<T>where
T: Float,
impl<T> Mul for DualNumber<T>where
T: Float,
Source§type Output = DualNumber<T>
type Output = DualNumber<T>
* operator.Source§fn mul(self, other: DualNumber<T>) -> DualNumber<T>
fn mul(self, other: DualNumber<T>) -> DualNumber<T>
* operation. Read moreSource§impl<T> Neg for DualNumber<T>where
T: Float,
impl<T> Neg for DualNumber<T>where
T: Float,
Source§type Output = DualNumber<T>
type Output = DualNumber<T>
- operator.Source§fn neg(self) -> DualNumber<T>
fn neg(self) -> DualNumber<T>
- operation. Read moreSource§impl<T> One for DualNumber<T>where
T: Float,
impl<T> One for DualNumber<T>where
T: Float,
Source§fn one() -> DualNumber<T>
fn one() -> DualNumber<T>
Source§impl<T> PartialEq for DualNumber<T>
impl<T> PartialEq for DualNumber<T>
Source§impl<T> Sub for DualNumber<T>where
T: Float,
impl<T> Sub for DualNumber<T>where
T: Float,
Source§type Output = DualNumber<T>
type Output = DualNumber<T>
- operator.Source§fn sub(self, other: DualNumber<T>) -> DualNumber<T>
fn sub(self, other: DualNumber<T>) -> DualNumber<T>
- operation. Read moreSource§impl<T> Zero for DualNumber<T>where
T: Float,
impl<T> Zero for DualNumber<T>where
T: Float,
impl<T> Copy for DualNumber<T>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.