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>
impl<const PWR: i32> NumericalDiff<PWR>
pub fn jacobian_variable_1<V1: VariableDtype, VOut: VariableDtype, F: Fn(V1) -> VOut>( f: F, v1: &V1, ) -> DiffResult<VOut, MatrixX>
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>
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>
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>
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>
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>
impl<const PWR: i32> Diff for NumericalDiff<PWR>
fn jacobian_1<V1: VariableDtype, F: Fn(V1) -> VectorX>( f: F, v1: &V1, ) -> DiffResult<VectorX, MatrixX>
fn jacobian_2<V1: VariableDtype, V2: VariableDtype, F: Fn(V1, V2) -> VectorX>( f: F, v1: &V1, v2: &V2, ) -> DiffResult<VectorX, MatrixX>
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>
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>
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>
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>
fn gradient_1<V1: VariableDtype, F: Fn(V1::Alias<Self::T>) -> Self::T>( f: F, v1: &V1, ) -> DiffResult<dtype, VectorX>
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>
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>
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>
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>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CheckedAs for T
impl<T> CheckedAs for T
Source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
Casts the value.
Source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
Source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
Casts the value.
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
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>
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)
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)
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
impl<T> DowncastSend for T
Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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 moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
Wrap the input message
T
in a tonic::Request
Source§impl<Src, Dst> LosslessTryInto<Dst> for Srcwhere
Dst: LosslessTryFrom<Src>,
impl<Src, Dst> LosslessTryInto<Dst> for Srcwhere
Dst: LosslessTryFrom<Src>,
Source§fn lossless_try_into(self) -> Option<Dst>
fn lossless_try_into(self) -> Option<Dst>
Performs the conversion.
Source§impl<Src, Dst> LossyInto<Dst> for Srcwhere
Dst: LossyFrom<Src>,
impl<Src, Dst> LossyInto<Dst> for Srcwhere
Dst: LossyFrom<Src>,
Source§fn lossy_into(self) -> Dst
fn lossy_into(self) -> Dst
Performs the conversion.
Source§impl<T> OverflowingAs for T
impl<T> OverflowingAs for T
Source§fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
Casts the value.
Source§impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
Source§fn overflowing_cast_from(src: Src) -> (Dst, bool)
fn overflowing_cast_from(src: Src) -> (Dst, bool)
Casts the value.
Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> SaturatingAs for T
impl<T> SaturatingAs for T
Source§fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
Casts the value.
Source§impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
Source§fn saturating_cast_from(src: Src) -> Dst
fn saturating_cast_from(src: Src) -> Dst
Casts the value.
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self
from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
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
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
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self
to the equivalent element of its superset.Source§impl<T> UnwrappedAs for T
impl<T> UnwrappedAs for T
Source§fn unwrapped_as<Dst>(self) -> Dstwhere
T: UnwrappedCast<Dst>,
fn unwrapped_as<Dst>(self) -> Dstwhere
T: UnwrappedCast<Dst>,
Casts the value.
Source§impl<Src, Dst> UnwrappedCastFrom<Src> for Dstwhere
Src: UnwrappedCast<Dst>,
impl<Src, Dst> UnwrappedCastFrom<Src> for Dstwhere
Src: UnwrappedCast<Dst>,
Source§fn unwrapped_cast_from(src: Src) -> Dst
fn unwrapped_cast_from(src: Src) -> Dst
Casts the value.
Source§impl<T> WithSubscriber for T
impl<T> WithSubscriber for T
Source§fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
Source§fn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Source§impl<T> WrappingAs for T
impl<T> WrappingAs for T
Source§fn wrapping_as<Dst>(self) -> Dstwhere
T: WrappingCast<Dst>,
fn wrapping_as<Dst>(self) -> Dstwhere
T: WrappingCast<Dst>,
Casts the value.
Source§impl<Src, Dst> WrappingCastFrom<Src> for Dstwhere
Src: WrappingCast<Dst>,
impl<Src, Dst> WrappingCastFrom<Src> for Dstwhere
Src: WrappingCast<Dst>,
Source§fn wrapping_cast_from(src: Src) -> Dst
fn wrapping_cast_from(src: Src) -> Dst
Casts the value.