Struct peroxide::structure::ad::ADFn[][src]

pub struct ADFn<F> { /* fields omitted */ }
Expand description

Generic AD functions

Description

To lift AD functions

Implementation

  • All Fn(AD) -> AD functions can be lift to Fn(f64) -> f64 via StableFn<f64>
  • grad(&self) -> Self gives gradient of original function
  • But still can use Fn(AD) -> AD via StableFn<AD>

Usage

extern crate peroxide;
use peroxide::fuga::*;

fn main() {
    let ad0 = 2f64;
    let ad1 = AD1(2f64, 1f64);
    let ad2 = AD2(2f64, 1f64, 0f64);

    let f_ad = ADFn::new(f);

    let ans0 = ad0.powi(2);
    let ans1 = ad1.powi(2).dx();
    let ans2 = ad2.powi(2).ddx();

    assert_eq!(ans0, f_ad.call_stable(ad0));

    let df = f_ad.grad();
    assert_eq!(ans1, df.call_stable(ad0));

    let ddf = df.grad();
    assert_eq!(ans2, ddf.call_stable(ad0));

    let ad1_output = f_ad.call_stable(ad1);
    assert_eq!(ad1_output, AD1(4f64, 4f64));

    let ad2_output = f_ad.call_stable(ad2);
    assert_eq!(ad2_output, AD2(4f64, 4f64, 2f64));
}

fn f(x: AD) -> AD {
    x.powi(2)
}

Implementations

impl<F: Clone> ADFn<F>[src]

pub fn new(f: F) -> Self[src]

pub fn grad(&self) -> Self[src]

Gradient

Trait Implementations

impl<'a, F: Fn(&Vec<AD>) -> Vec<AD>> StableFn<&'a Vec<AD, Global>> for ADFn<F>[src]

type Output = Vec<AD>

fn call_stable(&self, target: &'a Vec<AD>) -> Self::Output[src]

impl<'a, F: Fn(&Vec<AD>) -> Vec<AD>> StableFn<&'a Vec<f64, Global>> for ADFn<F>[src]

type Output = Vec<f64>

fn call_stable(&self, target: &'a Vec<f64>) -> Self::Output[src]

impl<F: Fn(AD) -> AD> StableFn<AD> for ADFn<F>[src]

type Output = AD

fn call_stable(&self, target: AD) -> Self::Output[src]

impl<F: Fn(Vec<AD>) -> Vec<AD>> StableFn<Vec<AD, Global>> for ADFn<F>[src]

type Output = Vec<AD>

fn call_stable(&self, target: Vec<AD>) -> Self::Output[src]

impl<F: Fn(Vec<AD>) -> Vec<AD>> StableFn<Vec<f64, Global>> for ADFn<F>[src]

type Output = Vec<f64>

fn call_stable(&self, target: Vec<f64>) -> Self::Output[src]

impl<F: Fn(AD) -> AD> StableFn<f64> for ADFn<F>[src]

type Output = f64

fn call_stable(&self, target: f64) -> Self::Output[src]

Auto Trait Implementations

impl<F> RefUnwindSafe for ADFn<F> where
    F: RefUnwindSafe

impl<F> Send for ADFn<F> where
    F: Send

impl<F> Sync for ADFn<F> where
    F: Sync

impl<F> Unpin for ADFn<F>

impl<F> UnwindSafe for ADFn<F> where
    F: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>[src]

Performs the conversion.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>, 

pub fn vzip(self) -> V