Tue, 24 Feb 2026 09:44:53 -0500
Add LowerExp to Float trait bounds
| 5 | 1 | /*! |
| 2 | Traits for mathematical functions. | |
| 3 | */ | |
| 0 | 4 | |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
105
diff
changeset
|
5 | use crate::error::DynResult; |
|
128
f75bf34adda0
Switch from Cow to MyCow for DifferentiableImpl to avoid Clone trait bound
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
6 | use crate::instance::MyCow; |
| 150 | 7 | pub use crate::instance::{BasicDecomposition, ClosedSpace, Decomposition, Instance, Space}; |
| 0 | 8 | use crate::loc::Loc; |
| 61 | 9 | use crate::norms::{Norm, NormExponent}; |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
10 | use crate::operator_arithmetic::{Constant, Weighted}; |
|
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
11 | use crate::types::{ClosedMul, Float, Num}; |
|
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
12 | use std::marker::PhantomData; |
| 105 | 13 | use std::ops::Mul; |
| 0 | 14 | |
|
75
e9f4550cfa18
Fix out-of-date references in doc comments
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
15 | /// A mapping from `Domain` to `Self::Codomain`. |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
16 | pub trait Mapping<Domain: Space> { |
| 150 | 17 | type Codomain: ClosedSpace; |
|
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
18 | |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
19 | /// Compute the value of `self` at `x`. |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
20 | fn apply<I: Instance<Domain>>(&self, x: I) -> Self::Codomain; |
| 61 | 21 | |
| 22 | #[inline] | |
| 23 | /// Form the composition `self ∘ other` | |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
24 | fn compose<X: Space, T: Mapping<X, Codomain = Domain>>(self, other: T) -> Composition<Self, T> |
| 61 | 25 | where |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
26 | Self: Sized, |
| 61 | 27 | { |
| 150 | 28 | Composition { outer: self, inner: other, intermediate_norm_exponent: () } |
| 61 | 29 | } |
| 30 | ||
| 31 | #[inline] | |
| 32 | /// Form the composition `self ∘ other`, assigning a norm to the inermediate space | |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
33 | fn compose_with_norm<F, X, T, E>(self, other: T, norm: E) -> Composition<Self, T, E> |
| 61 | 34 | where |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
35 | Self: Sized, |
|
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
36 | X: Space, |
|
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
37 | T: Mapping<X, Codomain = Domain>, |
|
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
38 | E: NormExponent, |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
122
diff
changeset
|
39 | Domain: Norm<E, F>, |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
40 | F: Num, |
| 61 | 41 | { |
| 150 | 42 | Composition { outer: self, inner: other, intermediate_norm_exponent: norm } |
| 61 | 43 | } |
|
68
c5f70e767511
Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
44 | |
|
c5f70e767511
Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
45 | /// Multiply `self` by the scalar `a`. |
|
c5f70e767511
Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
46 | #[inline] |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
47 | fn weigh<C>(self, a: C) -> Weighted<Self, C> |
|
68
c5f70e767511
Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
48 | where |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
49 | Self: Sized, |
|
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
50 | C: Constant, |
|
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
51 | Self::Codomain: ClosedMul<C::Type>, |
|
68
c5f70e767511
Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
52 | { |
| 150 | 53 | Weighted { weight: a, base_fn: self } |
|
68
c5f70e767511
Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
61
diff
changeset
|
54 | } |
|
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
55 | } |
|
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
56 | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
122
diff
changeset
|
57 | /// Automatically implemented shorthand for referring to [`Mapping`]s from [`Loc<N, F>`] to `F`. |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
122
diff
changeset
|
58 | pub trait RealMapping<const N: usize, F: Float = f64>: Mapping<Loc<N, F>, Codomain = F> {} |
|
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
59 | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
122
diff
changeset
|
60 | impl<F: Float, T, const N: usize> RealMapping<N, F> for T where T: Mapping<Loc<N, F>, Codomain = F> {} |
|
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
61 | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
122
diff
changeset
|
62 | /// A helper trait alias for referring to [`Mapping`]s from [`Loc<N, F>`] to [`Loc<M, F>`]. |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
122
diff
changeset
|
63 | pub trait RealVectorField<const N: usize, const M: usize, F: Float = f64>: |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
122
diff
changeset
|
64 | Mapping<Loc<N, F>, Codomain = Loc<M, F>> |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
65 | { |
|
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
66 | } |
|
35
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
67 | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
122
diff
changeset
|
68 | impl<F: Float, T, const N: usize, const M: usize> RealVectorField<N, M, F> for T where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
122
diff
changeset
|
69 | T: Mapping<Loc<N, F>, Codomain = Loc<M, F>> |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
70 | { |
|
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
71 | } |
|
35
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
72 | |
| 5 | 73 | /// A differentiable mapping from `Domain` to [`Mapping::Codomain`], with differentials |
| 74 | /// `Differential`. | |
|
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
75 | /// |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
76 | /// This is automatically implemented when [`DifferentiableImpl`] is. |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
77 | pub trait DifferentiableMapping<Domain: Space>: Mapping<Domain> { |
| 150 | 78 | type DerivativeDomain: ClosedSpace; |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
79 | type Differential<'b>: Mapping<Domain, Codomain = Self::DerivativeDomain> |
|
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
80 | where |
|
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
81 | Self: 'b; |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
82 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
83 | /// Calculate differential at `x` |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
84 | fn differential<I: Instance<Domain>>(&self, x: I) -> Self::DerivativeDomain; |
|
34
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
85 | |
| 194 | 86 | /// Calculate differential and value at `x` |
| 87 | fn apply_and_differential<I: Instance<Domain>>( | |
| 88 | &self, | |
| 89 | x: I, | |
| 90 | ) -> (Self::Codomain, Self::DerivativeDomain) { | |
| 91 | x.eval_ref(|xo| (self.apply(xo), self.differential(xo))) | |
| 92 | } | |
| 93 | ||
|
34
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
94 | /// Form the differential mapping of `self`. |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
95 | fn diff(self) -> Self::Differential<'static>; |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
96 | |
| 48 | 97 | /// Form the differential mapping of `self`. |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
98 | fn diff_ref(&self) -> Self::Differential<'_>; |
| 48 | 99 | } |
| 100 | ||
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
101 | /// Automatically implemented shorthand for referring to differentiable [`Mapping`]s from |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
122
diff
changeset
|
102 | /// [`Loc<N, F>`] to `F`. |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
122
diff
changeset
|
103 | pub trait DifferentiableRealMapping<const N: usize, F: Float>: |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
122
diff
changeset
|
104 | DifferentiableMapping<Loc<N, F>, Codomain = F, DerivativeDomain = Loc<N, F>> |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
105 | { |
|
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
106 | } |
| 48 | 107 | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
122
diff
changeset
|
108 | impl<F: Float, T, const N: usize> DifferentiableRealMapping<N, F> for T where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
122
diff
changeset
|
109 | T: DifferentiableMapping<Loc<N, F>, Codomain = F, DerivativeDomain = Loc<N, F>> |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
110 | { |
|
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
111 | } |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
112 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
113 | /// Helper trait for implementing [`DifferentiableMapping`] |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
114 | pub trait DifferentiableImpl<X: Space>: Sized { |
| 150 | 115 | type Derivative: ClosedSpace; |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
116 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
117 | /// Compute the differential of `self` at `x`, consuming the input. |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
118 | fn differential_impl<I: Instance<X>>(&self, x: I) -> Self::Derivative; |
| 194 | 119 | |
| 120 | fn apply_and_differential_impl<I: Instance<X>>( | |
| 121 | &self, | |
| 122 | x: I, | |
| 123 | ) -> (Self::Codomain, Self::Derivative) | |
| 124 | where | |
| 125 | Self: Mapping<X>, | |
| 126 | { | |
| 127 | x.eval_ref(|xo| (self.apply(xo), self.differential_impl(xo))) | |
| 128 | } | |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
129 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
130 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
131 | impl<T, Domain> DifferentiableMapping<Domain> for T |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
132 | where |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
133 | Domain: Space, |
|
128
f75bf34adda0
Switch from Cow to MyCow for DifferentiableImpl to avoid Clone trait bound
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
134 | T: Mapping<Domain> + DifferentiableImpl<Domain>, |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
135 | { |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
136 | type DerivativeDomain = T::Derivative; |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
137 | type Differential<'b> |
|
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
138 | = Differential<'b, Domain, Self> |
|
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
139 | where |
|
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
140 | Self: 'b; |
|
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
141 | |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
142 | #[inline] |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
143 | fn differential<I: Instance<Domain>>(&self, x: I) -> Self::DerivativeDomain { |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
144 | self.differential_impl(x) |
|
34
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
145 | } |
| 44 | 146 | |
| 194 | 147 | #[inline] |
| 148 | fn apply_and_differential<I: Instance<Domain>>( | |
| 149 | &self, | |
| 150 | x: I, | |
| 151 | ) -> (T::Codomain, Self::DerivativeDomain) { | |
| 152 | self.apply_and_differential_impl(x) | |
| 153 | } | |
| 154 | ||
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
155 | fn diff(self) -> Differential<'static, Domain, Self> { |
| 150 | 156 | Differential { g: MyCow::Owned(self), _space: PhantomData } |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
157 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
158 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
159 | fn diff_ref(&self) -> Differential<'_, Domain, Self> { |
| 150 | 160 | Differential { g: MyCow::Borrowed(self), _space: PhantomData } |
| 44 | 161 | } |
| 0 | 162 | } |
| 163 | ||
|
75
e9f4550cfa18
Fix out-of-date references in doc comments
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
164 | /// Container for the differential [`Mapping`] of a [`DifferentiableMapping`]. |
|
128
f75bf34adda0
Switch from Cow to MyCow for DifferentiableImpl to avoid Clone trait bound
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
165 | pub struct Differential<'a, X, G> { |
|
f75bf34adda0
Switch from Cow to MyCow for DifferentiableImpl to avoid Clone trait bound
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
166 | g: MyCow<'a, G>, |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
167 | _space: PhantomData<X>, |
|
34
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
168 | } |
|
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
169 | |
|
128
f75bf34adda0
Switch from Cow to MyCow for DifferentiableImpl to avoid Clone trait bound
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
170 | impl<'a, X, G> Differential<'a, X, G> { |
| 49 | 171 | pub fn base_fn(&self) -> &G { |
| 172 | &self.g | |
| 173 | } | |
| 174 | } | |
| 175 | ||
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
176 | impl<'a, X, G> Mapping<X> for Differential<'a, X, G> |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
177 | where |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
178 | X: Space, |
|
128
f75bf34adda0
Switch from Cow to MyCow for DifferentiableImpl to avoid Clone trait bound
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
179 | G: DifferentiableMapping<X>, |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
180 | { |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
181 | type Codomain = G::DerivativeDomain; |
|
34
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
182 | |
|
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
183 | #[inline] |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
184 | fn apply<I: Instance<X>>(&self, x: I) -> Self::Codomain { |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
185 | (*self.g).differential(x) |
|
34
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
186 | } |
|
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
187 | } |
|
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
188 | |
|
35
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
189 | /// Container for flattening [`Loc`]`<F, 1>` codomain of a [`Mapping`] to `F`. |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
190 | pub struct FlattenedCodomain<X, F, G> { |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
191 | g: G, |
|
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
192 | _phantoms: PhantomData<(X, F)>, |
|
35
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
193 | } |
|
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
194 | |
| 151 | 195 | impl<F, X, G> Mapping<X> for FlattenedCodomain<X, F, G> |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
196 | where |
| 151 | 197 | F: ClosedSpace, |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
198 | X: Space, |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
122
diff
changeset
|
199 | G: Mapping<X, Codomain = Loc<1, F>>, |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
200 | { |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
201 | type Codomain = F; |
|
35
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
202 | |
|
34
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
203 | #[inline] |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
204 | fn apply<I: Instance<X>>(&self, x: I) -> Self::Codomain { |
|
35
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
205 | self.g.apply(x).flatten1d() |
|
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
206 | } |
|
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
207 | } |
|
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
208 | |
|
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
209 | /// An auto-trait for constructing a [`FlattenCodomain`] structure for |
|
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
210 | /// flattening the codomain of a [`Mapping`] from [`Loc`]`<F, 1>` to `F`. |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
122
diff
changeset
|
211 | pub trait FlattenCodomain<X: Space, F>: Mapping<X, Codomain = Loc<1, F>> + Sized { |
|
35
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
212 | /// Flatten the codomain from [`Loc`]`<F, 1>` to `F`. |
|
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
213 | fn flatten_codomain(self) -> FlattenedCodomain<X, F, Self> { |
| 150 | 214 | FlattenedCodomain { g: self, _phantoms: PhantomData } |
|
34
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
215 | } |
|
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
216 | } |
|
35
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
217 | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
122
diff
changeset
|
218 | impl<X: Space, F, G: Sized + Mapping<X, Codomain = Loc<1, F>>> FlattenCodomain<X, F> for G {} |
|
35
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
219 | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
122
diff
changeset
|
220 | /// Container for dimensional slicing [`Loc`]`<N, F>` codomain of a [`Mapping`] to `F`. |
|
128
f75bf34adda0
Switch from Cow to MyCow for DifferentiableImpl to avoid Clone trait bound
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
221 | pub struct SlicedCodomain<'a, X, F, G, const N: usize> { |
|
f75bf34adda0
Switch from Cow to MyCow for DifferentiableImpl to avoid Clone trait bound
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
222 | g: MyCow<'a, G>, |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
223 | slice: usize, |
|
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
224 | _phantoms: PhantomData<(X, F)>, |
|
35
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
225 | } |
|
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
226 | |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
227 | impl<'a, X, F, G, const N: usize> Mapping<X> for SlicedCodomain<'a, X, F, G, N> |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
228 | where |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
229 | X: Space, |
| 151 | 230 | F: Copy + ClosedSpace, |
|
128
f75bf34adda0
Switch from Cow to MyCow for DifferentiableImpl to avoid Clone trait bound
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
231 | G: Mapping<X, Codomain = Loc<N, F>>, |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
232 | { |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
233 | type Codomain = F; |
|
35
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
234 | |
|
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
235 | #[inline] |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
236 | fn apply<I: Instance<X>>(&self, x: I) -> Self::Codomain { |
|
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
237 | let tmp: [F; N] = (*self.g).apply(x).into(); |
|
35
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
238 | // Safety: `slice_codomain` below checks the range. |
|
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
239 | unsafe { *tmp.get_unchecked(self.slice) } |
|
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
240 | } |
|
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
241 | } |
|
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
242 | |
|
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
243 | /// An auto-trait for constructing a [`FlattenCodomain`] structure for |
|
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
244 | /// flattening the codomain of a [`Mapping`] from [`Loc`]`<F, 1>` to `F`. |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
122
diff
changeset
|
245 | pub trait SliceCodomain<X: Space, const N: usize, F: Copy = f64>: |
|
128
f75bf34adda0
Switch from Cow to MyCow for DifferentiableImpl to avoid Clone trait bound
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
246 | Mapping<X, Codomain = Loc<N, F>> + Sized |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
247 | { |
|
35
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
248 | /// Flatten the codomain from [`Loc`]`<F, 1>` to `F`. |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
249 | fn slice_codomain(self, slice: usize) -> SlicedCodomain<'static, X, F, Self, N> { |
|
35
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
250 | assert!(slice < N); |
| 150 | 251 | SlicedCodomain { g: MyCow::Owned(self), slice, _phantoms: PhantomData } |
|
35
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
252 | } |
| 44 | 253 | |
| 254 | /// Flatten the codomain from [`Loc`]`<F, 1>` to `F`. | |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
255 | fn slice_codomain_ref(&self, slice: usize) -> SlicedCodomain<'_, X, F, Self, N> { |
| 44 | 256 | assert!(slice < N); |
| 150 | 257 | SlicedCodomain { g: MyCow::Borrowed(self), slice, _phantoms: PhantomData } |
| 44 | 258 | } |
|
35
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
259 | } |
|
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
260 | |
|
128
f75bf34adda0
Switch from Cow to MyCow for DifferentiableImpl to avoid Clone trait bound
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
261 | impl<X: Space, F: Copy, G: Sized + Mapping<X, Codomain = Loc<N, F>>, const N: usize> |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
122
diff
changeset
|
262 | SliceCodomain<X, N, F> for G |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
263 | { |
|
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
264 | } |
| 61 | 265 | |
| 266 | /// The composition S ∘ T. `E` is for storing a `NormExponent` for the intermediate space. | |
| 267 | pub struct Composition<S, T, E = ()> { | |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
268 | pub outer: S, |
|
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
269 | pub inner: T, |
|
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
270 | pub intermediate_norm_exponent: E, |
| 61 | 271 | } |
| 272 | ||
| 273 | impl<S, T, X, E> Mapping<X> for Composition<S, T, E> | |
| 274 | where | |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
275 | X: Space, |
|
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
276 | T: Mapping<X>, |
|
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
277 | S: Mapping<T::Codomain>, |
| 61 | 278 | { |
| 279 | type Codomain = S::Codomain; | |
| 280 | ||
| 281 | #[inline] | |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
282 | fn apply<I: Instance<X>>(&self, x: I) -> Self::Codomain { |
| 61 | 283 | self.outer.apply(self.inner.apply(x)) |
| 284 | } | |
| 285 | } | |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
286 | |
| 105 | 287 | /// Helper trait for implementing [`DifferentiableMapping`] |
| 288 | impl<S, T, X, E, Y> DifferentiableImpl<X> for Composition<S, T, E> | |
| 289 | where | |
| 290 | X: Space, | |
| 291 | T: DifferentiableImpl<X> + Mapping<X>, | |
| 292 | S: DifferentiableImpl<T::Codomain>, | |
| 293 | E: Copy, | |
| 294 | //Composition<S::Derivative, T::Derivative, E>: Space, | |
| 295 | S::Derivative: Mul<T::Derivative, Output = Y>, | |
| 150 | 296 | Y: ClosedSpace, |
| 105 | 297 | { |
| 298 | //type Derivative = Composition<S::Derivative, T::Derivative, E>; | |
| 299 | type Derivative = Y; | |
| 300 | ||
| 301 | /// Compute the differential of `self` at `x`, consuming the input. | |
| 302 | fn differential_impl<I: Instance<X>>(&self, x: I) -> Self::Derivative { | |
| 303 | // Composition { | |
| 304 | // outer: self | |
| 305 | // .outer | |
| 306 | // .differential_impl(self.inner.apply(x.ref_instance())), | |
| 307 | // inner: self.inner.differential_impl(x), | |
| 308 | // intermediate_norm_exponent: self.intermediate_norm_exponent, | |
| 309 | // } | |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
128
diff
changeset
|
310 | |
| 105 | 311 | self.outer |
| 171 | 312 | .differential_impl(x.eval_ref(|r| self.inner.apply(r))) |
| 105 | 313 | * self.inner.differential_impl(x) |
| 314 | } | |
| 315 | } | |
| 316 | ||
| 317 | mod dataterm; | |
| 318 | pub use dataterm::DataTerm; | |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
319 | |
|
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
320 | /// Trait for indicating that `Self` is Lipschitz with respect to the (semi)norm `D`. |
|
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
321 | pub trait Lipschitz<M> { |
|
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
322 | /// The type of floats |
|
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
323 | type FloatType: Float; |
|
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
324 | |
|
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
325 | /// Returns the Lipschitz factor of `self` with respect to the (semi)norm `D`. |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
105
diff
changeset
|
326 | fn lipschitz_factor(&self, seminorm: M) -> DynResult<Self::FloatType>; |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
327 | } |
| 102 | 328 | |
| 329 | /// Helper trait for implementing [`Lipschitz`] for mappings that implement [`DifferentiableImpl`]. | |
| 330 | pub trait LipschitzDifferentiableImpl<X: Space, M>: DifferentiableImpl<X> { | |
| 331 | type FloatType: Float; | |
| 332 | ||
| 333 | /// Compute the lipschitz factor of the derivative of `f`. | |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
105
diff
changeset
|
334 | fn diff_lipschitz_factor(&self, seminorm: M) -> DynResult<Self::FloatType>; |
| 102 | 335 | } |
| 336 | ||
| 337 | impl<'b, M, X, A> Lipschitz<M> for Differential<'b, X, A> | |
| 338 | where | |
| 339 | X: Space, | |
|
128
f75bf34adda0
Switch from Cow to MyCow for DifferentiableImpl to avoid Clone trait bound
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
340 | A: LipschitzDifferentiableImpl<X, M>, |
| 102 | 341 | { |
| 342 | type FloatType = A::FloatType; | |
| 343 | ||
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
105
diff
changeset
|
344 | fn lipschitz_factor(&self, seminorm: M) -> DynResult<Self::FloatType> { |
| 102 | 345 | (*self.g).diff_lipschitz_factor(seminorm) |
| 346 | } | |
| 347 | } |