Var

Struct Var 

Source
pub struct Var<T> { /* private fields */ }
Expand description

A differentiable variable for reverse-mode automatic differentiation.

§Examples

Using reverse_diff for reusable functions:

use autodiff::{reverse_diff, Var};

// Define f(x) = (x+1)(x-1) = x² - 1
let f = |x: Var<f64>| (x.clone() + 1.0) * (x - 1.0);

let (val, deriv) = reverse_diff(f, 3.0);
assert_eq!(val, 8.0);    // f(3) = 8
assert_eq!(deriv, 6.0);  // f'(3) = 2x = 6
use autodiff::{reverse_diff, Var};

// Define f(x) = x² + x
let f = |x: Var<f64>| x.clone() * x.clone() + x;

let (val, deriv) = reverse_diff(f, 3.0);
assert_eq!(val, 12.0);   // f(3) = 12
assert_eq!(deriv, 7.0);  // f'(3) = 2x + 1 = 7

Implementations§

Source§

impl<T: Copy> Var<T>

Source

pub fn value(&self) -> T

Returns the value of this variable.

Source§

impl<T: Float> Var<T>

Source

pub fn backward(&self) -> Gradients<T>

Computes gradients by backpropagation from this variable.

Returns a Gradients object that can be queried for the gradient with respect to any variable.

§Examples
use autodiff::Tape;

let tape = Tape::new();
let x = tape.var(3.0);
let y = x.clone() * x.clone();  // y = x²

let grads = y.backward();
assert_eq!(grads.get(&x), 6.0);  // dy/dx = 2x = 6
Source

pub fn recip(self) -> Self

Computes the reciprocal 1/self.

Source

pub fn exp(self) -> Self

Computes e^self.

Source

pub fn sin(self) -> Self

Computes sin(self).

Source

pub fn cos(self) -> Self

Computes cos(self).

Source

pub fn ln(self) -> Self

Computes ln(self).

Source

pub fn sqrt(self) -> Self

Computes sqrt(self).

Trait Implementations§

Source§

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

Source§

type Output = Var<T>

The resulting type after applying the + operator.
Source§

fn add(self, c: T) -> Self::Output

Performs the + operation. Read more
Source§

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

Source§

type Output = Var<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 Var<T>

Source§

fn clone(&self) -> Var<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: Float> Div<T> for Var<T>

Source§

type Output = Var<T>

The resulting type after applying the / operator.
Source§

fn div(self, c: T) -> Self::Output

Performs the / operation. Read more
Source§

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

Source§

type Output = Var<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<T> for Var<T>

Source§

type Output = Var<T>

The resulting type after applying the * operator.
Source§

fn mul(self, c: T) -> Self::Output

Performs the * operation. Read more
Source§

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

Source§

type Output = Var<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 Var<T>

Source§

type Output = Var<T>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

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

Source§

type Output = Var<T>

The resulting type after applying the - operator.
Source§

fn sub(self, c: T) -> Self::Output

Performs the - operation. Read more
Source§

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

Source§

type Output = Var<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 Var<T>

§

impl<T> !RefUnwindSafe for Var<T>

§

impl<T> !Send for Var<T>

§

impl<T> !Sync for Var<T>

§

impl<T> Unpin for Var<T>

§

impl<T> !UnwindSafe for Var<T>

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.