Dual

Struct Dual 

Source
pub struct Dual<T> {
    pub value: T,
    pub deriv: T,
}
Expand description

A dual number representing a value and its derivative.

Dual(value, deriv) represents value + deriv·ε where ε² = 0. Arithmetic operations follow the algebraic rules of dual numbers; derivatives propagate automatically.

§Type Parameter

  • T: The numeric type (typically f64 or f32)

§Examples

§Basic Usage

use autodiff::Dual;

let x = Dual::variable(5.0);
let y = x * x;  // y = x²

assert_eq!(y.value, 25.0);  // 5² = 25
assert_eq!(y.deriv, 10.0);  // d/dx(x²) at x=5 is 2*5 = 10

§Chain Rule

use autodiff::Dual;

// f(x) = (x + 1) * (x + 2)
let x = Dual::variable(3.0);
let f = (x + Dual::constant(1.0)) * (x + Dual::constant(2.0));

assert_eq!(f.value, 20.0);  // (3+1)*(3+2) = 4*5 = 20
assert_eq!(f.deriv, 9.0);   // f'(x) = 2x+3, f'(3) = 9

§Multiple Operations

use autodiff::Dual;

// f(x) = x³ - 2x + 1
let x = Dual::variable(2.0);
let x2 = x * x;
let x3 = x2 * x;
let f = x3 - Dual::constant(2.0) * x + Dual::constant(1.0);

assert_eq!(f.value, 5.0);   // 8 - 4 + 1 = 5
assert_eq!(f.deriv, 10.0);  // f'(x) = 3x² - 2, f'(2) = 12 - 2 = 10

Fields§

§value: T

The primal value

§deriv: T

The derivative (tangent)

Implementations§

Source§

impl<T> Dual<T>

Source

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

Create a new dual number with explicit value and derivative.

§Example
use autodiff::Dual;

let d = Dual::new(3.0, 1.0);
assert_eq!(d.value, 3.0);
assert_eq!(d.deriv, 1.0);
Source

pub fn constant(value: T) -> Self
where T: Zero,

Create a constant (derivative = 0).

Use this for literal values in your computation.

§Example
use autodiff::Dual;

let c = Dual::constant(5.0);
assert_eq!(c.value, 5.0);
assert_eq!(c.deriv, 0.0);
Source

pub fn variable(value: T) -> Self
where T: One,

Create a variable (derivative = 1).

Use this for the input variable you’re differentiating with respect to.

§Example
use autodiff::Dual;

let x = Dual::variable(3.0);
assert_eq!(x.value, 3.0);
assert_eq!(x.deriv, 1.0);  // dx/dx = 1
Source

pub fn recip(self) -> Self
where T: One + Div<Output = T> + Mul<Output = T> + Neg<Output = T> + Clone,

Reciprocal (multiplicative inverse).

For g = b + b′·ε with b ≠ 0, computes:

g⁻¹ = (1/b) + (-b′/b²)·ε

This encodes the derivative rule: (1/g)′ = -g′/g².

§Example
use autodiff::Dual;

// f(x) = 1/x at x=2
let x = Dual::variable(2.0);
let f = x.recip();

assert_eq!(f.value, 0.5);      // 1/2 = 0.5
assert_eq!(f.deriv, -0.25);    // d/dx(1/x) at x=2 is -1/4
Source

pub fn exp(self) -> Self
where T: Float,

Exponential function.

For f = a + a′·ε, computes e^f = e^a + (a′·e^a)·ε.

This encodes the derivative: d/dx(e^f) = f′·e^f.

§Example
use autodiff::Dual;

// f(x) = e^x at x=0
let x = Dual::variable(0.0);
let f = x.exp();

assert_eq!(f.value, 1.0);      // e^0 = 1
assert_eq!(f.deriv, 1.0);      // d/dx(e^x) at x=0 is e^0 = 1
Source

pub fn ln(self) -> Self
where T: Float,

Natural logarithm.

For f = a + a′·ε, computes ln(f) = ln(a) + (a′/a)·ε.

This encodes the derivative: d/dx(ln f) = f′/f.

§Example
use autodiff::Dual;

// f(x) = ln(x) at x=1
let x = Dual::variable(1.0);
let f = x.ln();

assert!((f.value - 0.0_f64).abs() < 1e-12);   // ln(1) = 0
assert!((f.deriv - 1.0_f64).abs() < 1e-12);   // d/dx(ln x) at x=1 is 1/1 = 1
Source

pub fn sin(self) -> Self
where T: Float,

Sine function.

For f = a + a′·ε, computes sin(f) = sin(a) + (a′·cos(a))·ε.

This encodes the derivative: d/dx(sin f) = f′·cos f.

§Example
use autodiff::Dual;

// f(x) = sin(x) at x=0
let x = Dual::variable(0.0);
let f = x.sin();

assert!((f.value - 0.0_f64).abs() < 1e-12);   // sin(0) = 0
assert!((f.deriv - 1.0_f64).abs() < 1e-12);   // d/dx(sin x) at x=0 is cos(0) = 1
Source

pub fn cos(self) -> Self
where T: Float,

Cosine function.

For f = a + a′·ε, computes cos(f) = cos(a) + (-a′·sin(a))·ε.

This encodes the derivative: d/dx(cos f) = -f′·sin f.

§Example
use autodiff::Dual;

// f(x) = cos(x) at x=0
let x = Dual::variable(0.0);
let f = x.cos();

assert!((f.value - 1.0_f64).abs() < 1e-12);   // cos(0) = 1
assert!((f.deriv - 0.0_f64).abs() < 1e-12);   // d/dx(cos x) at x=0 is -sin(0) = 0
Source

pub fn sqrt(self) -> Self
where T: Float,

Square root.

For f = a + a′·ε, computes √f = √a + (a′/(2√a))·ε.

This encodes the derivative: d/dx(√f) = f′/(2√f).

§Example
use autodiff::Dual;

// f(x) = √x at x=4
let x = Dual::variable(4.0);
let f = x.sqrt();

assert_eq!(f.value, 2.0);      // √4 = 2
assert_eq!(f.deriv, 0.25);     // d/dx(√x) at x=4 is 1/(2*2) = 0.25

Trait Implementations§

Source§

impl<T: Add<Output = T>> Add for Dual<T>

Addition: (a + a′·ε) + (b + b′·ε) = (a+b) + (a′+b′)·ε

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 · Source§

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

Performs copy-assignment from source. Read more
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> Div for Dual<T>
where T: One + Div<Output = T> + Mul<Output = T> + Add<Output = T> + Neg<Output = T> + Clone,

Division: f / g = f * (1/g).

The quotient rule emerges automatically from the product rule (in Mul) composed with the reciprocal rule (in recip).

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: Mul<Output = T> + Add<Output = T> + Clone> Mul for Dual<T>

Multiplication: (a + a′·ε) * (b + b′·ε) = ab + (a′b + ab′)·ε

This implements the product rule: d/dx(f·g) = f′·g + f·g′

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: Neg<Output = T>> Neg for Dual<T>

Negation: -(a + a′·ε) = -a + (-a′)·ε

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 · 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<Output = T>> Sub for Dual<T>

Subtraction: (a + a′·ε) - (b + b′·ε) = (a-b) + (a′-b′)·ε

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
Source§

impl<T: Copy> Copy for Dual<T>

Source§

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> 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<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> 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, 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.