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