pub struct Dual<const N: usize> {
pub v: f64,
pub d: [f64; N],
}Expand description
A forward-mode dual number: a primal value paired with an N-slot gradient.
Each arithmetic and elementary operation propagates the derivative slots
alongside the value, so evaluating an expression on Dual<N> computes both
the result and its partial derivatives with respect to N seeded
directions in a single pass. N is the number of independent directions
(inputs) tracked; Dual<0> carries no gradient and reduces to plain f64
arithmetic on the value.
§Examples
Differentiate f(x) = x * x + 3 * x at x = 2, where f'(x) = 2x + 3 = 7:
use sim_lib_numbers_ad::Dual;
let x = Dual::<1>::var(2.0, 0);
let y = x * x + Dual::<1>::cst(3.0) * x;
assert_eq!(y.v, 10.0);
assert_eq!(y.d, [7.0]);Fields§
§v: f64The primal value of the number.
d: [f64; N]The gradient: one partial derivative per tracked direction.
Implementations§
Trait Implementations§
impl<const N: usize> Copy for Dual<N>
Source§impl<const N: usize> Scalarish for Dual<N>
impl<const N: usize> Scalarish for Dual<N>
impl<const N: usize> StructuralPartialEq for Dual<N>
Auto Trait Implementations§
impl<const N: usize> Freeze for Dual<N>
impl<const N: usize> RefUnwindSafe for Dual<N>
impl<const N: usize> Send for Dual<N>
impl<const N: usize> Sync for Dual<N>
impl<const N: usize> Unpin for Dual<N>
impl<const N: usize> UnsafeUnpin for Dual<N>
impl<const N: usize> UnwindSafe for Dual<N>
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