MultiAD

Enum MultiAD 

Source
pub enum MultiAD {
Show 13 variants Inp, Add, Sub, Mul, Div, Pow, Sin, Cos, Tan, Exp, Ln, Sqrt, Abs,
}
Expand description

Multi-variable automatic differentiation operations.

Represents operations in a computational graph for functions with multiple inputs. Each operation takes references to previous results via indices.

§Examples

use petite_ad::{MultiAD, multi_ops};

// Build graph: f(x, y) = sin(x) * (x + y)
let exprs = multi_ops![
    (inp, 0),    // x at index 0
    (inp, 1),    // y at index 1
    (add, 0, 1), // x + y at index 2
    (sin, 0),    // sin(x) at index 3
    (mul, 2, 3), // sin(x) * (x + y) at index 4
];

let (value, grad_fn) = MultiAD::compute_grad(&exprs, &[0.6, 1.4]).unwrap();
let gradients = grad_fn(1.0);
println!("f(0.6, 1.4) = {}", value);
println!("∇f = {:?}", gradients);

Variants§

§

Inp

Input placeholder - references an input variable

§

Add

Addition: a + b

§

Sub

Subtraction: a - b

§

Mul

Multiplication: a * b

§

Div

Division: a / b

§Notes

  • Delegates to f64::div(), which returns inf for division by zero
  • Returns NaN for 0.0 / 0.0
§

Pow

Power: a^b (a raised to the power of b)

§Notes

  • Delegates to f64::powf()
  • For x^n where n is an integer, consider using repeated multiplication
§

Sin

Sine function: sin(x)

§Notes

  • Delegates to f64::sin(), which operates in radians
  • Returns values in the range [-1.0, 1.0]
§

Cos

Cosine function: cos(x)

§Notes

  • Delegates to f64::cos(), which operates in radians
  • Returns values in the range [-1.0, 1.0]
§

Tan

Tangent function: tan(x)

§Notes

  • Delegates to f64::tan(), which operates in radians
  • Returns very large values near π/2 + kπ (asymptotes)
§

Exp

Exponential function: exp(x)

§Notes

  • Delegates to f64::exp()
  • Returns inf for very large inputs (> ~709 for f64)
  • Returns 0.0 for very large negative inputs (< ~-745 for f64)
§

Ln

Natural logarithm: ln(x)

§Notes

  • Delegates to f64::ln()
  • Returns NaN for negative inputs
  • Returns -inf for ln(0.0)
§

Sqrt

Square root: sqrt(x)

§Notes

  • Delegates to f64::sqrt()
  • Returns NaN for negative inputs
§

Abs

Absolute value: abs(x)

§Notes

  • Delegates to f64::abs()
  • Subgradient at x=0 is 0 (consistent with common practice)

Implementations§

Source§

impl MultiAD

Source

pub fn compute(exprs: &[(MultiAD, Vec<usize>)], inputs: &[f64]) -> Result<f64>

Compute forward pass only (no gradient computation).

Evaluates the computational graph to produce the final output value.

§Arguments
  • exprs - Slice of (operation, indices) pairs defining the computation graph
  • inputs - Input values for the function
§Errors

Returns Err(AutodiffError) if an operation receives incorrect arity.

§Examples
use petite_ad::{MultiAD, multi_ops};

let exprs = multi_ops![(inp, 0), (inp, 1), (add, 0, 1)];
let result = MultiAD::compute(&exprs, &[2.0, 3.0]).unwrap();
assert!((result - 5.0).abs() < 1e-10);
Source

pub fn compute_grad_generic<W>( exprs: &[(MultiAD, Vec<usize>)], inputs: &[f64], ) -> Result<(f64, W)>
where W: From<Box<DynGradFn>> + Deref<Target = DynGradFn> + 'static,

Compute forward pass and return gradient function.

Returns a tuple of (value, gradient_function). The gradient function takes a cotangent (typically 1.0) and returns a vector of gradients with respect to each input.

The result is Box-wrapped by default. If you need Arc for sharing across threads, convert using Arc::from(box_fn).

§Arguments
  • exprs - Computational graph as (operation, indices) pairs
  • inputs - Input values to evaluate at
§Returns

Tuple of (output_value, gradient_function)

§Errors

Returns Err(AutodiffError) if an operation receives incorrect arity.

§Examples
use petite_ad::{MultiAD, multi_ops};
use std::sync::Arc;

let exprs = multi_ops![
    (inp, 0), (inp, 1),
    (add, 0, 1), (sin, 0), (mul, 2, 3)
];
let (value, grad_fn) = MultiAD::compute_grad(&exprs, &[0.6, 1.4]).unwrap();
let gradients = grad_fn(1.0);

// Convert to Arc if needed for sharing
let arc_grad_fn: Arc<dyn Fn(f64) -> Vec<f64>> = Arc::from(grad_fn);
Source

pub fn compute_grad( exprs: &[(MultiAD, Vec<usize>)], inputs: &[f64], ) -> Result<BackwardResultBox>

Trait Implementations§

Source§

impl Clone for MultiAD

Source§

fn clone(&self) -> MultiAD

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 Debug for MultiAD

Source§

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

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

impl Hash for MultiAD

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for MultiAD

Source§

fn eq(&self, other: &MultiAD) -> 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 Copy for MultiAD

Source§

impl Eq for MultiAD

Source§

impl StructuralPartialEq for MultiAD

Auto Trait Implementations§

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.