Skip to main content

Dual

Struct Dual 

Source
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 (typically f32 or f64)

§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 = 15

Implementations§

Source§

impl<T: Float> Dual<T>

Source

pub fn new(value: T, deriv: T) -> Self

Create a new dual number with given value and derivative

§Arguments
  • value - The function value
  • deriv - 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);
Source

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.

Source

pub fn constant(value: T) -> Self

Create a dual number representing a constant (derivative = 0)

Use this for constants in expressions.

Source

pub fn value(&self) -> T

Get the value component

Source

pub fn deriv(&self) -> T

Get the derivative component

Source

pub fn pow(&self, n: T) -> Self

Compute power: x^n

Source

pub fn exp(&self) -> Self

Compute exponential: e^x

Source

pub fn ln(&self) -> Self

Compute natural logarithm: ln(x)

Source

pub fn sin(&self) -> Self

Compute sine: sin(x)

Source

pub fn cos(&self) -> Self

Compute cosine: cos(x)

Source

pub fn tan(&self) -> Self

Compute tangent: tan(x)

Source

pub fn sinh(&self) -> Self

Compute hyperbolic sine: sinh(x)

Source

pub fn cosh(&self) -> Self

Compute hyperbolic cosine: cosh(x)

Source

pub fn tanh(&self) -> Self

Compute hyperbolic tangent: tanh(x)

Source

pub fn sqrt(&self) -> Self

Compute square root: √x

Source

pub fn abs(&self) -> Self

Compute absolute value: |x|

Note: The derivative at x=0 is set to 0 by convention.

Source

pub fn sigmoid(&self) -> Self

Compute sigmoid function: 1 / (1 + e^(-x))

Source

pub fn relu(&self) -> Self

Compute ReLU: max(0, x)

Note: The derivative at x=0 is set to 0 by convention.

Trait Implementations§

Source§

impl<T: Float> Add for Dual<T>

Source§

type Output = Dual<T>

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl<T: Clone> Clone for Dual<T>

Source§

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

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<T: Copy> Copy for Dual<T>

Source§

impl<T: Debug> Debug for Dual<T>

Source§

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

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

impl<T: Float + Display> Display for Dual<T>

Source§

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

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

impl<T: Float> Div for Dual<T>

Source§

type Output = Dual<T>

The resulting type after applying the / operator.
Source§

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

Performs the / operation. Read more
Source§

impl<T: Float> Mul for Dual<T>

Source§

type Output = Dual<T>

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl<T: Float> Neg for Dual<T>

Source§

type Output = Dual<T>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl<T: PartialEq> PartialEq for Dual<T>

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · 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> StructuralPartialEq for Dual<T>

Source§

impl<T: Float> Sub for Dual<T>

Source§

type Output = Dual<T>

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more

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

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> 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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V