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