Struct peroxide::structure::ad::ADFn

source ·
pub struct ADFn<F> { /* private fields */ }
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§

source§

impl<F: Clone> ADFn<F>

source

pub fn new(f: F) -> Self

source

pub fn grad(&self) -> Self

Gradient

Trait Implementations§

source§

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

source§

type Output = Vec<AD>

source§

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

source§

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

source§

type Output = Vec<f64>

source§

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

source§

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

source§

type Output = AD

source§

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

source§

impl<F: Fn(Vec<AD>) -> Vec<AD>> StableFn<Vec<AD>> for ADFn<F>

source§

type Output = Vec<AD>

source§

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

source§

impl<F: Fn(Vec<AD>) -> Vec<AD>> StableFn<Vec<f64>> for ADFn<F>

source§

type Output = Vec<f64>

source§

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

source§

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

source§

type Output = f64

source§

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

Auto Trait Implementations§

§

impl<F> Freeze for ADFn<F>

§

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§

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

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. 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.
§

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

§

fn vzip(self) -> V

§

impl<T> Ungil for T
where T: Send,