Add Differential struct and DifferentiableMapping.diff dev

Wed, 04 Oct 2023 08:02:14 -0500

author
Tuomo Valkonen <tuomov@iki.fi>
date
Wed, 04 Oct 2023 08:02:14 -0500
branch
dev
changeset 34
3dbc04100b09
parent 33
75d65fa74eba
child 35
3b82a9d16307

Add Differential struct and DifferentiableMapping.diff

src/mapping.rs file | annotate | diff | comparison | revisions
--- 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<F, N>`] to `F` a [`Float`].
-pub trait RealMapping<F : Float, const N : usize> : Mapping<Loc<F, N>, Codomain = F> {}
+pub trait RealMapping<F : Float, const N : usize>
+: Mapping<Loc<F, N>, Codomain = F> {}
 
 impl<F : Float, T, const N : usize> RealMapping<F, N> for T
 where T : Mapping<Loc<F, N>, Codomain = F> {}
 
 
 /// Trait for calculation the differential of `Self` as a mathematical function on `X`.
-pub trait Differentiable<X> {
+pub trait Differentiable<X> : 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<Domain>
 : Mapping<Domain>
   + Differentiable<Domain, Output=Self::Differential>
-  + 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<Domain, Self> {
+        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<X, G : DifferentiableMapping<X>> {
+    g : G,
+    _space : PhantomData<X>
+}
+
+impl<X, G : DifferentiableMapping<X>> Apply<X> for Differential<X, G> {
+    type Output = G::Differential;
+
+    #[inline]
+    fn apply(&self, x : X) -> G::Differential {
+        self.g.differential(x)
+    }
+}
+
+impl<'a, X, G : DifferentiableMapping<X>> Apply<&'a X> for Differential<X, G> {
+    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)
+    }
+}

mercurial