Wed, 03 Sep 2025 14:27:21 -0500
nalgebra stuff
| 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 | |
|
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
86 | /// Form the differential mapping of `self`. |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
87 | fn diff(self) -> Self::Differential<'static>; |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
88 | |
| 48 | 89 | /// Form the differential mapping of `self`. |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
90 | fn diff_ref(&self) -> Self::Differential<'_>; |
| 48 | 91 | } |
| 92 | ||
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
93 | /// 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
|
94 | /// [`Loc<N, F>`] to `F`. |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
122
diff
changeset
|
95 | 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
|
96 | 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
|
97 | { |
|
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
98 | } |
| 48 | 99 | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
122
diff
changeset
|
100 | 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
|
101 | 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
|
102 | { |
|
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
103 | } |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
104 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
105 | /// Helper trait for implementing [`DifferentiableMapping`] |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
106 | pub trait DifferentiableImpl<X: Space>: Sized { |
| 150 | 107 | type Derivative: ClosedSpace; |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
108 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
109 | /// 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
|
110 | fn differential_impl<I: Instance<X>>(&self, x: I) -> Self::Derivative; |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
111 | } |
|
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 | impl<T, Domain> DifferentiableMapping<Domain> for T |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
114 | where |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
115 | 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
|
116 | T: Mapping<Domain> + DifferentiableImpl<Domain>, |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
117 | { |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
118 | type DerivativeDomain = T::Derivative; |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
119 | type Differential<'b> |
|
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
120 | = Differential<'b, Domain, Self> |
|
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
121 | where |
|
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
122 | Self: 'b; |
|
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
123 | |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
124 | #[inline] |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
125 | 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
|
126 | self.differential_impl(x) |
|
34
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
127 | } |
| 44 | 128 | |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
129 | fn diff(self) -> Differential<'static, Domain, Self> { |
| 150 | 130 | Differential { g: MyCow::Owned(self), _space: PhantomData } |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
131 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
132 | |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
133 | fn diff_ref(&self) -> Differential<'_, Domain, Self> { |
| 150 | 134 | Differential { g: MyCow::Borrowed(self), _space: PhantomData } |
| 44 | 135 | } |
| 0 | 136 | } |
| 137 | ||
|
75
e9f4550cfa18
Fix out-of-date references in doc comments
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
138 | /// 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
|
139 | 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
|
140 | g: MyCow<'a, G>, |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
141 | _space: PhantomData<X>, |
|
34
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
142 | } |
|
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
143 | |
|
128
f75bf34adda0
Switch from Cow to MyCow for DifferentiableImpl to avoid Clone trait bound
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
144 | impl<'a, X, G> Differential<'a, X, G> { |
| 49 | 145 | pub fn base_fn(&self) -> &G { |
| 146 | &self.g | |
| 147 | } | |
| 148 | } | |
| 149 | ||
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
150 | 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
|
151 | where |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
152 | 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
|
153 | G: DifferentiableMapping<X>, |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
154 | { |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
155 | type Codomain = G::DerivativeDomain; |
|
34
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
156 | |
|
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
157 | #[inline] |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
158 | 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
|
159 | (*self.g).differential(x) |
|
34
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
160 | } |
|
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
161 | } |
|
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
162 | |
|
35
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
163 | /// 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
|
164 | 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
|
165 | g: G, |
|
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
166 | _phantoms: PhantomData<(X, F)>, |
|
35
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
167 | } |
|
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
168 | |
| 151 | 169 | 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
|
170 | where |
| 151 | 171 | F: ClosedSpace, |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
172 | X: Space, |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
122
diff
changeset
|
173 | G: Mapping<X, Codomain = Loc<1, F>>, |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
174 | { |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
175 | type Codomain = F; |
|
35
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
176 | |
|
34
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
177 | #[inline] |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
178 | 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
|
179 | self.g.apply(x).flatten1d() |
|
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
180 | } |
|
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
181 | } |
|
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
182 | |
|
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
183 | /// 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
|
184 | /// 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
|
185 | 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
|
186 | /// 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
|
187 | fn flatten_codomain(self) -> FlattenedCodomain<X, F, Self> { |
| 150 | 188 | FlattenedCodomain { g: self, _phantoms: PhantomData } |
|
34
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
189 | } |
|
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
190 | } |
|
35
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
191 | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
122
diff
changeset
|
192 | 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
|
193 | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
122
diff
changeset
|
194 | /// 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
|
195 | 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
|
196 | g: MyCow<'a, G>, |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
197 | slice: usize, |
|
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
198 | _phantoms: PhantomData<(X, F)>, |
|
35
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
199 | } |
|
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
200 | |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
201 | 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
|
202 | where |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
203 | X: Space, |
| 151 | 204 | 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
|
205 | G: Mapping<X, Codomain = Loc<N, F>>, |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
206 | { |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
207 | type Codomain = F; |
|
35
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 | #[inline] |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
210 | 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
|
211 | 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
|
212 | // Safety: `slice_codomain` below checks the range. |
|
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
213 | unsafe { *tmp.get_unchecked(self.slice) } |
|
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
214 | } |
|
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
215 | } |
|
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
216 | |
|
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
217 | /// 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
|
218 | /// 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
|
219 | 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
|
220 | Mapping<X, Codomain = Loc<N, F>> + Sized |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
53
diff
changeset
|
221 | { |
|
35
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
222 | /// 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
|
223 | 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
|
224 | assert!(slice < N); |
| 150 | 225 | 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
|
226 | } |
| 44 | 227 | |
| 228 | /// 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
|
229 | fn slice_codomain_ref(&self, slice: usize) -> SlicedCodomain<'_, X, F, Self, N> { |
| 44 | 230 | assert!(slice < N); |
| 150 | 231 | SlicedCodomain { g: MyCow::Borrowed(self), slice, _phantoms: PhantomData } |
| 44 | 232 | } |
|
35
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
233 | } |
|
3b82a9d16307
Add Mapping codomain slicing and RealVectorField
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
234 | |
|
128
f75bf34adda0
Switch from Cow to MyCow for DifferentiableImpl to avoid Clone trait bound
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
235 | 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
|
236 | 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
|
237 | { |
|
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
238 | } |
| 61 | 239 | |
| 240 | /// The composition S ∘ T. `E` is for storing a `NormExponent` for the intermediate space. | |
| 241 | 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
|
242 | pub outer: S, |
|
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
243 | pub inner: T, |
|
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
244 | pub intermediate_norm_exponent: E, |
| 61 | 245 | } |
| 246 | ||
| 247 | impl<S, T, X, E> Mapping<X> for Composition<S, T, E> | |
| 248 | where | |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
249 | X: Space, |
|
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
250 | T: Mapping<X>, |
|
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
251 | S: Mapping<T::Codomain>, |
| 61 | 252 | { |
| 253 | type Codomain = S::Codomain; | |
| 254 | ||
| 255 | #[inline] | |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
256 | fn apply<I: Instance<X>>(&self, x: I) -> Self::Codomain { |
| 61 | 257 | self.outer.apply(self.inner.apply(x)) |
| 258 | } | |
| 259 | } | |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
260 | |
| 105 | 261 | /// Helper trait for implementing [`DifferentiableMapping`] |
| 262 | impl<S, T, X, E, Y> DifferentiableImpl<X> for Composition<S, T, E> | |
| 263 | where | |
| 264 | X: Space, | |
| 265 | T: DifferentiableImpl<X> + Mapping<X>, | |
| 266 | S: DifferentiableImpl<T::Codomain>, | |
| 267 | E: Copy, | |
| 268 | //Composition<S::Derivative, T::Derivative, E>: Space, | |
| 269 | S::Derivative: Mul<T::Derivative, Output = Y>, | |
| 150 | 270 | Y: ClosedSpace, |
| 105 | 271 | { |
| 272 | //type Derivative = Composition<S::Derivative, T::Derivative, E>; | |
| 273 | type Derivative = Y; | |
| 274 | ||
| 275 | /// Compute the differential of `self` at `x`, consuming the input. | |
| 276 | fn differential_impl<I: Instance<X>>(&self, x: I) -> Self::Derivative { | |
| 277 | // Composition { | |
| 278 | // outer: self | |
| 279 | // .outer | |
| 280 | // .differential_impl(self.inner.apply(x.ref_instance())), | |
| 281 | // inner: self.inner.differential_impl(x), | |
| 282 | // intermediate_norm_exponent: self.intermediate_norm_exponent, | |
| 283 | // } | |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
128
diff
changeset
|
284 | |
| 105 | 285 | self.outer |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
128
diff
changeset
|
286 | .differential_impl(x.eval_ref_decompose(|r| self.inner.apply(r))) |
| 105 | 287 | * self.inner.differential_impl(x) |
| 288 | } | |
| 289 | } | |
| 290 | ||
| 291 | mod dataterm; | |
| 292 | pub use dataterm::DataTerm; | |
|
99
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
293 | |
|
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
294 | /// 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
|
295 | pub trait Lipschitz<M> { |
|
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
296 | /// The type of floats |
|
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
297 | type FloatType: Float; |
|
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
298 | |
|
9e5b9fc81c52
Quadratic Mappings; Lipschitz trait (moved from pointsource_algs).
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
299 | /// 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
|
300 | 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
|
301 | } |
| 102 | 302 | |
| 303 | /// Helper trait for implementing [`Lipschitz`] for mappings that implement [`DifferentiableImpl`]. | |
| 304 | pub trait LipschitzDifferentiableImpl<X: Space, M>: DifferentiableImpl<X> { | |
| 305 | type FloatType: Float; | |
| 306 | ||
| 307 | /// 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
|
308 | fn diff_lipschitz_factor(&self, seminorm: M) -> DynResult<Self::FloatType>; |
| 102 | 309 | } |
| 310 | ||
| 311 | impl<'b, M, X, A> Lipschitz<M> for Differential<'b, X, A> | |
| 312 | where | |
| 313 | 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
|
314 | A: LipschitzDifferentiableImpl<X, M>, |
| 102 | 315 | { |
| 316 | type FloatType = A::FloatType; | |
| 317 | ||
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
105
diff
changeset
|
318 | fn lipschitz_factor(&self, seminorm: M) -> DynResult<Self::FloatType> { |
| 102 | 319 | (*self.g).diff_lipschitz_factor(seminorm) |
| 320 | } | |
| 321 | } |