pub struct Dual<T> { /* private fields */ }Expand description
Dual number for forward-mode automatic differentiation
A dual number represents both a value and its derivative: f(x) + f'(x)ε
where ε² = 0. This enables exact derivative computation through operator
overloading and the chain rule.
§Type Parameters
T- The numeric type (typicallyf32orf64)
§Examples
ⓘ
use numrs2::autodiff::Dual;
// f(x) = x³ + 2x² - 5x + 1
fn f(x: Dual<f64>) -> Dual<f64> {
x.pow(3.0) + x.pow(2.0) * 2.0 - x * 5.0 + Dual::constant(1.0)
}
// Compute f(2) and f'(2)
let x = Dual::new(2.0, 1.0);
let result = f(x);
assert_eq!(result.value(), 3.0); // f(2) = 8 + 8 - 10 + 1 = 7... wait let me recalculate
assert_eq!(result.deriv(), 15.0); // f'(2) = 3(4) + 2(2)(2) - 5 = 12 + 8 - 5 = 15Implementations§
Source§impl<T: Float> Dual<T>
impl<T: Float> Dual<T>
Sourcepub fn new(value: T, deriv: T) -> Self
pub fn new(value: T, deriv: T) -> Self
Create a new dual number with given value and derivative
§Arguments
value- The function valuederiv- The derivative value
§Examples
ⓘ
use numrs2::autodiff::Dual;
// Variable with derivative = 1 (for computing ∂f/∂x)
let x = Dual::new(3.0, 1.0);
// Constant with derivative = 0
let c = Dual::new(5.0, 0.0);Sourcepub fn variable(value: T) -> Self
pub fn variable(value: T) -> Self
Create a dual number representing a variable (derivative = 1)
Use this for the input variable with respect to which you want to differentiate.
Sourcepub fn constant(value: T) -> Self
pub fn constant(value: T) -> Self
Create a dual number representing a constant (derivative = 0)
Use this for constants in expressions.
Trait Implementations§
impl<T: Copy> Copy for Dual<T>
impl<T> StructuralPartialEq for Dual<T>
Auto Trait Implementations§
impl<T> Freeze for Dual<T>where
T: Freeze,
impl<T> RefUnwindSafe for Dual<T>where
T: RefUnwindSafe,
impl<T> Send for Dual<T>where
T: Send,
impl<T> Sync for Dual<T>where
T: Sync,
impl<T> Unpin for Dual<T>where
T: Unpin,
impl<T> UnsafeUnpin for Dual<T>where
T: UnsafeUnpin,
impl<T> UnwindSafe for Dual<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
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> ClosedNeg for Twhere
T: Neg<Output = T>,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
impl<T> Scalar for T
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>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
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
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
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.