# HG changeset patch # User Tuomo Valkonen # Date 1696424534 18000 # Node ID 3dbc04100b0910c6b24863761c24360d08abcd12 # Parent 75d65fa74eba001701173f3713cc3f31c9bcbb37 Add Differential struct and DifferentiableMapping.diff diff -r 75d65fa74eba -r 3dbc04100b09 src/mapping.rs --- a/src/mapping.rs Mon Jul 31 13:35:06 2023 +0300 +++ b/src/mapping.rs Wed Oct 04 08:02:14 2023 -0500 @@ -43,22 +43,21 @@ } -/// A helper trait alias for referring to [`Mapping`]s from [`Loc`] to `F` a [`Float`]. -pub trait RealMapping : Mapping, Codomain = F> {} +pub trait RealMapping +: Mapping, Codomain = F> {} impl RealMapping for T where T : Mapping, Codomain = F> {} /// Trait for calculation the differential of `Self` as a mathematical function on `X`. -pub trait Differentiable { +pub trait Differentiable : Sized { type Output; /// Compute the differential of `self` at `x`. fn differential(&self, x : X) -> Self::Output; } - /// A differentiable mapping from `Domain` to [`Mapping::Codomain`], with differentials /// `Differential`. /// @@ -66,8 +65,13 @@ pub trait DifferentiableMapping : Mapping + Differentiable - + for<'a> Differentiable<&'a Domain, Output=Self::Differential>{ + + for<'a> Differentiable<&'a Domain, Output=Self::Differential> { type Differential; + + /// Form the differential mapping of `self`. + fn diff(self) -> Differential { + Differential{ g : self, _space : PhantomData } + } } @@ -115,3 +119,33 @@ self.components.iter().map(|c| c.differential(x)).sum() } } + +/// Container for the differential [`Mapping`] of a [`Differentiable`] mapping. +pub struct Differential> { + g : G, + _space : PhantomData +} + +impl> Apply for Differential { + type Output = G::Differential; + + #[inline] + fn apply(&self, x : X) -> G::Differential { + self.g.differential(x) + } +} + +impl<'a, X, G : DifferentiableMapping> Apply<&'a X> for Differential { + type Output = G::Differential; + + #[inline] + fn apply(&self, x : &'a X) -> G::Differential { + self.g.differential(x) + } +} + + #[inline] + fn apply(&self, x : X) -> Self::Output { + self.g.differential(x) + } +}