Struct NumericalDiff

Source
pub struct NumericalDiff<const PWR: i32 = 6>;
Expand description

Forward mode differentiator

It operates on functions with regular dtype inputs and outputs, no dual numbers required. The generic parameter PWR is used to specify the power of the step size, it PWR=6 uses 1e-6 as a step size.

This struct is used to compute the Jacobian of a function using forward mode differentiation via dual-numbers. It can operate on functions with up to 6 inputs and with vector-valued outputs.

use factrs::{
    linalg::{vectorx, DiffResult, NumericalDiff, VectorX},
    traits::*,
    variables::SO2,
};

// We can also be generic over Numeric as in [ForwardProp] as well if desired
fn f(x: SO2, y: SO2) -> VectorX {
    x.ominus(&y)
}

let x = SO2::from_theta(2.0);
let y = SO2::from_theta(1.0);

// 2 as the generic since we have 2 dimensions going in
let DiffResult { value, diff } = NumericalDiff::<6>::jacobian_2(f, &x, &y);
assert_eq!(value, vectorx![1.0]);

Implementations§

Source§

impl<const PWR: i32> NumericalDiff<PWR>

Source

pub fn jacobian_variable_1<V1: VariableDtype, VOut: VariableDtype, F: Fn(V1) -> VOut>( f: F, v1: &V1, ) -> DiffResult<VOut, MatrixX>

Source

pub fn jacobian_variable_2<V1: VariableDtype, V2: VariableDtype, VOut: VariableDtype, F: Fn(V1, V2) -> VOut>( f: F, v1: &V1, v2: &V2, ) -> DiffResult<VOut, MatrixX>

Source

pub fn jacobian_variable_3<V1: VariableDtype, V2: VariableDtype, V3: VariableDtype, VOut: VariableDtype, F: Fn(V1, V2, V3) -> VOut>( f: F, v1: &V1, v2: &V2, v3: &V3, ) -> DiffResult<VOut, MatrixX>

Source

pub fn jacobian_variable_4<V1: VariableDtype, V2: VariableDtype, V3: VariableDtype, V4: VariableDtype, VOut: VariableDtype, F: Fn(V1, V2, V3, V4) -> VOut>( f: F, v1: &V1, v2: &V2, v3: &V3, v4: &V4, ) -> DiffResult<VOut, MatrixX>

Source

pub fn jacobian_variable_5<V1: VariableDtype, V2: VariableDtype, V3: VariableDtype, V4: VariableDtype, V5: VariableDtype, VOut: VariableDtype, F: Fn(V1, V2, V3, V4, V5) -> VOut>( f: F, v1: &V1, v2: &V2, v3: &V3, v4: &V4, v5: &V5, ) -> DiffResult<VOut, MatrixX>

Source

pub fn jacobian_variable_6<V1: VariableDtype, V2: VariableDtype, V3: VariableDtype, V4: VariableDtype, V5: VariableDtype, V6: VariableDtype, VOut: VariableDtype, F: Fn(V1, V2, V3, V4, V5, V6) -> VOut>( f: F, v1: &V1, v2: &V2, v3: &V3, v4: &V4, v5: &V5, v6: &V6, ) -> DiffResult<VOut, MatrixX>

Trait Implementations§

Source§

impl<const PWR: i32> Diff for NumericalDiff<PWR>

Source§

type T = f64

The dtype of the variables
Source§

fn jacobian_1<V1: VariableDtype, F: Fn(V1) -> VectorX>( f: F, v1: &V1, ) -> DiffResult<VectorX, MatrixX>

Source§

fn jacobian_2<V1: VariableDtype, V2: VariableDtype, F: Fn(V1, V2) -> VectorX>( f: F, v1: &V1, v2: &V2, ) -> DiffResult<VectorX, MatrixX>

Source§

fn jacobian_3<V1: VariableDtype, V2: VariableDtype, V3: VariableDtype, F: Fn(V1, V2, V3) -> VectorX>( f: F, v1: &V1, v2: &V2, v3: &V3, ) -> DiffResult<VectorX, MatrixX>

Source§

fn jacobian_4<V1: VariableDtype, V2: VariableDtype, V3: VariableDtype, V4: VariableDtype, F: Fn(V1, V2, V3, V4) -> VectorX>( f: F, v1: &V1, v2: &V2, v3: &V3, v4: &V4, ) -> DiffResult<VectorX, MatrixX>

Source§

fn jacobian_5<V1: VariableDtype, V2: VariableDtype, V3: VariableDtype, V4: VariableDtype, V5: VariableDtype, F: Fn(V1, V2, V3, V4, V5) -> VectorX>( f: F, v1: &V1, v2: &V2, v3: &V3, v4: &V4, v5: &V5, ) -> DiffResult<VectorX, MatrixX>

Source§

fn jacobian_6<V1: VariableDtype, V2: VariableDtype, V3: VariableDtype, V4: VariableDtype, V5: VariableDtype, V6: VariableDtype, F: Fn(V1, V2, V3, V4, V5, V6) -> VectorX>( f: F, v1: &V1, v2: &V2, v3: &V3, v4: &V4, v5: &V5, v6: &V6, ) -> DiffResult<VectorX, MatrixX>

Source§

fn gradient_1<V1: VariableDtype, F: Fn(V1::Alias<Self::T>) -> Self::T>( f: F, v1: &V1, ) -> DiffResult<dtype, VectorX>

Source§

fn gradient_2<V1: VariableDtype, V2: VariableDtype, F: Fn(V1::Alias<Self::T>, V2::Alias<Self::T>) -> Self::T>( f: F, v1: &V1, v2: &V2, ) -> DiffResult<dtype, VectorX>

Source§

fn gradient_3<V1: VariableDtype, V2: VariableDtype, V3: VariableDtype, F: Fn(V1::Alias<Self::T>, V2::Alias<Self::T>, V3::Alias<Self::T>) -> Self::T>( f: F, v1: &V1, v2: &V2, v3: &V3, ) -> DiffResult<dtype, VectorX>

Source§

fn gradient_4<V1: VariableDtype, V2: VariableDtype, V3: VariableDtype, V4: VariableDtype, F: Fn(V1::Alias<Self::T>, V2::Alias<Self::T>, V3::Alias<Self::T>, V4::Alias<Self::T>) -> Self::T>( f: F, v1: &V1, v2: &V2, v3: &V3, v4: &V4, ) -> DiffResult<dtype, VectorX>

Source§

fn gradient_5<V1: VariableDtype, V2: VariableDtype, V3: VariableDtype, V4: VariableDtype, V5: VariableDtype, F: Fn(V1::Alias<Self::T>, V2::Alias<Self::T>, V3::Alias<Self::T>, V4::Alias<Self::T>, V5::Alias<Self::T>) -> Self::T>( f: F, v1: &V1, v2: &V2, v3: &V3, v4: &V4, v5: &V5, ) -> DiffResult<dtype, VectorX>

Source§

fn gradient_6<V1: VariableDtype, V2: VariableDtype, V3: VariableDtype, V4: VariableDtype, V5: VariableDtype, V6: VariableDtype, F: Fn(V1::Alias<Self::T>, V2::Alias<Self::T>, V3::Alias<Self::T>, V4::Alias<Self::T>, V5::Alias<Self::T>, V6::Alias<Self::T>) -> Self::T>( f: F, v1: &V1, v2: &V2, v3: &V3, v4: &V4, v5: &V5, v6: &V6, ) -> DiffResult<dtype, VectorX>

Auto Trait Implementations§

§

impl<const PWR: i32> Freeze for NumericalDiff<PWR>

§

impl<const PWR: i32> RefUnwindSafe for NumericalDiff<PWR>

§

impl<const PWR: i32> Send for NumericalDiff<PWR>

§

impl<const PWR: i32> Sync for NumericalDiff<PWR>

§

impl<const PWR: i32> Unpin for NumericalDiff<PWR>

§

impl<const PWR: i32> UnwindSafe for NumericalDiff<PWR>

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> Az for T

Source§

fn az<Dst>(self) -> Dst
where T: Cast<Dst>,

Casts the value.
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<Src, Dst> CastFrom<Src> for Dst
where Src: Cast<Dst>,

Source§

fn cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> CheckedAs for T

Source§

fn checked_as<Dst>(self) -> Option<Dst>
where T: CheckedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> CheckedCastFrom<Src> for Dst
where Src: CheckedCast<Dst>,

Source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Converts Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Converts Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Converts &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Converts &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSend for T
where T: Any + Send,

Source§

fn into_any_send(self: Box<T>) -> Box<dyn Any + Send>

Converts Box<Trait> (where Trait: DowncastSend) to Box<dyn Any + Send>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_sync(self: Box<T>) -> Box<dyn Any + Send + Sync>

Converts Box<Trait> (where Trait: DowncastSync) to Box<dyn Any + Send + Sync>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Converts Arc<Trait> (where Trait: DowncastSync) to Arc<Any>, which can then be downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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
Source§

impl<T> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<Src, Dst> LosslessTryInto<Dst> for Src
where Dst: LosslessTryFrom<Src>,

Source§

fn lossless_try_into(self) -> Option<Dst>

Performs the conversion.
Source§

impl<Src, Dst> LossyInto<Dst> for Src
where Dst: LossyFrom<Src>,

Source§

fn lossy_into(self) -> Dst

Performs the conversion.
Source§

impl<T> OverflowingAs for T

Source§

fn overflowing_as<Dst>(self) -> (Dst, bool)
where T: OverflowingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dst
where Src: OverflowingCast<Dst>,

Source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

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

Initializes a with the given initializer. Read more
Source§

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

Dereferences the given pointer. Read more
Source§

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

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> SaturatingAs for T

Source§

fn saturating_as<Dst>(self) -> Dst
where T: SaturatingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dst
where Src: SaturatingCast<Dst>,

Source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
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.
Source§

impl<T> UnwrappedAs for T

Source§

fn unwrapped_as<Dst>(self) -> Dst
where T: UnwrappedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dst
where Src: UnwrappedCast<Dst>,

Source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
Source§

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

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> WrappingAs for T

Source§

fn wrapping_as<Dst>(self) -> Dst
where T: WrappingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> WrappingCastFrom<Src> for Dst
where Src: WrappingCast<Dst>,

Source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> Allocation for T
where T: RefUnwindSafe + Send + Sync,