ADFn

Struct ADFn 

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

Generic AD function wrapper.

Lifts a function F: Fn(Jet<2>) -> Jet<2> to operate at multiple levels:

  • call_stable(f64)f64: evaluate function value, first derivative, or second derivative
  • call_stable(Jet<2>)Jet<2>: pass through

For vector functions, also lifts F: Fn(Vec<Jet<1>>) -> Vec<Jet<1>>.

§Examples

use peroxide::fuga::*;

let f_ad = ADFn::new(|x: Jet<2>| x.powi(2));

// Value: f(3) = 9
assert_eq!(f_ad.call_stable(3.0), 9.0);

// Gradient: f'(3) = 6  (2*3)
let df = f_ad.grad();
assert_eq!(df.call_stable(3.0), 6.0);

// Hessian: f''(3) = 2
let ddf = df.grad();
assert_eq!(ddf.call_stable(3.0), 2.0);

Implementations§

Source§

impl<F: Clone> ADFn<F>

Source

pub fn new(f: F) -> Self

Create a new ADFn wrapping function f at gradient level 0 (function evaluation).

Source

pub fn grad(&self) -> Self

Produce the gradient version of this function (increments grad_level by 1). Panics if grad_level >= 2.

Trait Implementations§

Source§

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

Vector version: F works with &Vec<Jet<1>>, target is &Vec<Jet<1>>.

Source§

type Output = Vec<Jet<1>>

Source§

fn call_stable(&self, target: &'a Vec<Jet<1>>) -> Vec<Jet<1>>

Source§

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

Vector version: F works with &Vec<Jet<1>>, target is &Vec<f64>.

Source§

type Output = Vec<f64>

Source§

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

Source§

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

Scalar version: F works with Jet<2>, target is Jet<2>.

Source§

type Output = Jet<2>

Source§

fn call_stable(&self, target: Jet<2>) -> Jet<2>

Source§

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

Vector version: F works with Vec<Jet<1>>, target is Vec<Jet<1>>.

Source§

type Output = Vec<Jet<1>>

Source§

fn call_stable(&self, target: Vec<Jet<1>>) -> Vec<Jet<1>>

Source§

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

Vector version: F works with Vec<Jet<1>>, target is Vec<f64>.

Source§

type Output = Vec<f64>

Source§

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

Source§

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

Scalar version: F works with Jet<2>, target is f64.

Source§

type Output = f64

Source§

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

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> Allocation for T
where T: RefUnwindSafe + Send + Sync,

§

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