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