Tue, 24 Feb 2026 09:44:53 -0500
Add LowerExp to Float trait bounds
| 67 | 1 | /*! |
| 2 | Simple disrete gradient operators | |
| 3 | */ | |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
4 | use crate::error::DynResult; |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
5 | use crate::instance::Instance; |
|
186
afe04e6b4a5b
StaticEuclidean Principal reference fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
133
diff
changeset
|
6 | use crate::linops::{Adjointable, BoundedLinear, Linear, Mapping, SimplyAdjointable, GEMV}; |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
7 | use crate::norms::{Norm, L2}; |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
8 | use crate::types::Float; |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
9 | use nalgebra::{DVector, Dyn, Matrix, Storage, StorageMut, U1}; |
| 67 | 10 | use numeric_literals::replace_float_literals; |
| 11 | ||
| 12 | #[derive(Copy, Clone, Debug)] | |
| 13 | /// Forward differences with Neumann boundary conditions | |
| 14 | pub struct ForwardNeumann; | |
| 15 | ||
| 16 | #[derive(Copy, Clone, Debug)] | |
| 17 | /// Forward differences with Dirichlet boundary conditions | |
| 18 | pub struct ForwardDirichlet; | |
| 19 | ||
| 20 | #[derive(Copy, Clone, Debug)] | |
| 21 | /// Backward differences with Dirichlet boundary conditions | |
| 22 | pub struct BackwardDirichlet; | |
| 23 | ||
| 24 | #[derive(Copy, Clone, Debug)] | |
| 25 | /// Backward differences with Neumann boundary conditions | |
| 26 | pub struct BackwardNeumann; | |
| 27 | ||
| 28 | /// Finite differences gradient | |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
29 | pub struct Grad<F: Float + nalgebra::RealField, B: Discretisation<F>, const N: usize> { |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
30 | dims: [usize; N], |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
31 | h: F, // may be negative to implement adjoints! |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
32 | discretisation: B, |
| 67 | 33 | } |
| 34 | ||
| 35 | /// Finite differences divergence | |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
36 | pub struct Div<F: Float + nalgebra::RealField, B: Discretisation<F>, const N: usize> { |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
37 | dims: [usize; N], |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
38 | h: F, // may be negative to implement adjoints! |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
39 | discretisation: B, |
| 67 | 40 | } |
| 41 | ||
| 42 | /// Internal: classification of a point in a 1D discretisation | |
| 43 | pub enum DiscretisationOrInterior { | |
| 44 | /// center, forward | |
| 45 | LeftBoundary(usize, usize), | |
| 46 | /// center, backward | |
| 47 | RightBoundary(usize, usize), | |
| 48 | /// center, (backward, forward) | |
| 49 | Interior(usize, (usize, usize)), | |
| 50 | } | |
| 51 | ||
| 52 | use DiscretisationOrInterior::*; | |
| 53 | ||
| 54 | /// Trait for different discretisations | |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
55 | pub trait Discretisation<F: Float + nalgebra::RealField>: Copy { |
| 67 | 56 | /// Opposite discretisation, appropriate for adjoints with negated cell width. |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
57 | type Opposite: Discretisation<F>; |
| 67 | 58 | |
| 59 | /// Add to appropiate index of `v` (as determined by `b`) the appropriate difference | |
| 60 | /// of `x` with cell width `h`. | |
| 61 | fn add_diff_mut<SMut, S>( | |
| 62 | &self, | |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
63 | v: &mut Matrix<F, Dyn, U1, SMut>, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
64 | x: &Matrix<F, Dyn, U1, S>, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
65 | α: F, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
66 | b: DiscretisationOrInterior, |
| 67 | 67 | ) where |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
68 | SMut: StorageMut<F, Dyn, U1>, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
69 | S: Storage<F, Dyn, U1>; |
| 67 | 70 | |
| 71 | /// Give the opposite discretisation, appropriate for adjoints with negated `h`. | |
| 72 | fn opposite(&self) -> Self::Opposite; | |
| 73 | ||
| 74 | /// Bound for the corresponding operator norm. | |
| 75 | #[replace_float_literals(F::cast_from(literal))] | |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
76 | fn opnorm_bound(&self, h: F) -> DynResult<F> { |
| 67 | 77 | // See: Chambolle, “An Algorithm for Total Variation Minimization and Applications”. |
| 78 | // Ok for forward and backward differences. | |
| 79 | // | |
| 80 | // Fuck nalgebra for polluting everything with its own shit. | |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
81 | Ok(num_traits::Float::sqrt(8.0) / h) |
| 67 | 82 | } |
| 83 | } | |
| 84 | ||
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
85 | impl<F: Float + nalgebra::RealField> Discretisation<F> for ForwardNeumann { |
| 67 | 86 | type Opposite = BackwardDirichlet; |
| 87 | ||
| 88 | #[inline] | |
| 89 | fn add_diff_mut<SMut, S>( | |
| 90 | &self, | |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
91 | v: &mut Matrix<F, Dyn, U1, SMut>, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
92 | x: &Matrix<F, Dyn, U1, S>, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
93 | α: F, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
94 | b: DiscretisationOrInterior, |
| 67 | 95 | ) where |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
96 | SMut: StorageMut<F, Dyn, U1>, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
97 | S: Storage<F, Dyn, U1>, |
| 67 | 98 | { |
| 99 | match b { | |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
100 | Interior(c, (_, f)) | LeftBoundary(c, f) => v[c] += (x[f] - x[c]) * α, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
101 | RightBoundary(_c, _b) => {} |
| 67 | 102 | } |
| 103 | } | |
| 104 | ||
| 105 | #[inline] | |
| 106 | fn opposite(&self) -> Self::Opposite { | |
| 107 | BackwardDirichlet | |
| 108 | } | |
| 109 | } | |
| 110 | ||
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
111 | impl<F: Float + nalgebra::RealField> Discretisation<F> for BackwardNeumann { |
| 67 | 112 | type Opposite = ForwardDirichlet; |
| 113 | ||
| 114 | #[inline] | |
| 115 | fn add_diff_mut<SMut, S>( | |
| 116 | &self, | |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
117 | v: &mut Matrix<F, Dyn, U1, SMut>, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
118 | x: &Matrix<F, Dyn, U1, S>, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
119 | α: F, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
120 | b: DiscretisationOrInterior, |
| 67 | 121 | ) where |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
122 | SMut: StorageMut<F, Dyn, U1>, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
123 | S: Storage<F, Dyn, U1>, |
| 67 | 124 | { |
| 125 | match b { | |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
126 | Interior(c, (b, _)) | RightBoundary(c, b) => v[c] += (x[c] - x[b]) * α, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
127 | LeftBoundary(_c, _f) => {} |
| 67 | 128 | } |
| 129 | } | |
| 130 | ||
| 131 | #[inline] | |
| 132 | fn opposite(&self) -> Self::Opposite { | |
| 133 | ForwardDirichlet | |
| 134 | } | |
| 135 | } | |
| 136 | ||
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
137 | impl<F: Float + nalgebra::RealField> Discretisation<F> for BackwardDirichlet { |
| 67 | 138 | type Opposite = ForwardNeumann; |
| 139 | ||
| 140 | #[inline] | |
| 141 | fn add_diff_mut<SMut, S>( | |
| 142 | &self, | |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
143 | v: &mut Matrix<F, Dyn, U1, SMut>, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
144 | x: &Matrix<F, Dyn, U1, S>, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
145 | α: F, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
146 | b: DiscretisationOrInterior, |
| 67 | 147 | ) where |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
148 | SMut: StorageMut<F, Dyn, U1>, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
149 | S: Storage<F, Dyn, U1>, |
| 67 | 150 | { |
| 151 | match b { | |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
152 | Interior(c, (b, _f)) => v[c] += (x[c] - x[b]) * α, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
153 | LeftBoundary(c, _f) => v[c] += x[c] * α, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
154 | RightBoundary(c, b) => v[c] -= x[b] * α, |
| 67 | 155 | } |
| 156 | } | |
| 157 | ||
| 158 | #[inline] | |
| 159 | fn opposite(&self) -> Self::Opposite { | |
| 160 | ForwardNeumann | |
| 161 | } | |
| 162 | } | |
| 163 | ||
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
164 | impl<F: Float + nalgebra::RealField> Discretisation<F> for ForwardDirichlet { |
| 67 | 165 | type Opposite = BackwardNeumann; |
| 166 | ||
| 167 | #[inline] | |
| 168 | fn add_diff_mut<SMut, S>( | |
| 169 | &self, | |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
170 | v: &mut Matrix<F, Dyn, U1, SMut>, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
171 | x: &Matrix<F, Dyn, U1, S>, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
172 | α: F, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
173 | b: DiscretisationOrInterior, |
| 67 | 174 | ) where |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
175 | SMut: StorageMut<F, Dyn, U1>, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
176 | S: Storage<F, Dyn, U1>, |
| 67 | 177 | { |
| 178 | match b { | |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
179 | Interior(c, (_b, f)) => v[c] += (x[f] - x[c]) * α, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
180 | LeftBoundary(c, f) => v[c] += x[f] * α, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
181 | RightBoundary(c, _b) => v[c] -= x[c] * α, |
| 67 | 182 | } |
| 183 | } | |
| 184 | ||
| 185 | #[inline] | |
| 186 | fn opposite(&self) -> Self::Opposite { | |
| 187 | BackwardNeumann | |
| 188 | } | |
| 189 | } | |
| 190 | ||
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
191 | struct Iter<'a, const N: usize> { |
| 67 | 192 | /// Dimensions |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
193 | dims: &'a [usize; N], |
| 67 | 194 | /// Dimension along which to calculate differences |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
195 | d: usize, |
| 67 | 196 | /// Stride along coordinate d |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
197 | d_stride: usize, |
| 67 | 198 | /// Cartesian indices |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
199 | i: [usize; N], |
| 67 | 200 | /// Linear index |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
201 | k: usize, |
| 67 | 202 | /// Maximal linear index |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
203 | len: usize, |
| 67 | 204 | } |
| 205 | ||
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
206 | impl<'a, const N: usize> Iter<'a, N> { |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
207 | fn new(dims: &'a [usize; N], d: usize) -> Self { |
| 67 | 208 | let d_stride = dims[0..d].iter().product::<usize>(); |
| 209 | let len = dims.iter().product::<usize>(); | |
|
186
afe04e6b4a5b
StaticEuclidean Principal reference fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
133
diff
changeset
|
210 | Iter { dims, d, d_stride, i: [0; N], k: 0, len } |
| 67 | 211 | } |
| 212 | } | |
| 213 | ||
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
214 | impl<'a, const N: usize> Iterator for Iter<'a, N> { |
| 67 | 215 | type Item = DiscretisationOrInterior; |
| 216 | fn next(&mut self) -> Option<Self::Item> { | |
| 217 | let res = if self.k >= self.len { | |
| 218 | None | |
| 219 | } else { | |
| 220 | let cartesian_idx = self.i[self.d]; | |
| 221 | let cartesian_max = self.dims[self.d]; | |
| 222 | let k = self.k; | |
| 223 | ||
| 224 | if cartesian_idx == 0 { | |
| 225 | Some(LeftBoundary(k, k + self.d_stride)) | |
| 226 | } else if cartesian_idx + 1 >= cartesian_max { | |
| 227 | Some(RightBoundary(k, k - self.d_stride)) | |
| 228 | } else { | |
| 229 | Some(Interior(k, (k - self.d_stride, k + self.d_stride))) | |
| 230 | } | |
| 231 | }; | |
| 232 | self.k += 1; | |
| 233 | for j in 0..N { | |
| 234 | if self.i[j] + 1 < self.dims[j] { | |
| 235 | self.i[j] += 1; | |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
236 | break; |
| 67 | 237 | } |
| 238 | self.i[j] = 0 | |
| 239 | } | |
| 240 | res | |
| 241 | } | |
| 242 | } | |
| 243 | ||
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
244 | impl<F, B, const N: usize> Mapping<DVector<F>> for Grad<F, B, N> |
| 67 | 245 | where |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
246 | B: Discretisation<F>, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
247 | F: Float + nalgebra::RealField, |
| 67 | 248 | { |
| 249 | type Codomain = DVector<F>; | |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
250 | fn apply<I: Instance<DVector<F>>>(&self, i: I) -> DVector<F> { |
| 67 | 251 | let mut y = DVector::zeros(N * self.len()); |
| 252 | self.apply_add(&mut y, i); | |
| 253 | y | |
| 254 | } | |
| 255 | } | |
| 256 | ||
| 257 | #[replace_float_literals(F::cast_from(literal))] | |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
258 | impl<F, B, const N: usize> GEMV<F, DVector<F>> for Grad<F, B, N> |
| 67 | 259 | where |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
260 | B: Discretisation<F>, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
261 | F: Float + nalgebra::RealField, |
| 67 | 262 | { |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
263 | fn gemv<I: Instance<DVector<F>>>(&self, y: &mut DVector<F>, α: F, i: I, β: F) { |
| 67 | 264 | if β == 0.0 { |
| 265 | y.as_mut_slice().iter_mut().for_each(|x| *x = 0.0); | |
| 266 | } else if β != 1.0 { | |
| 267 | //*y *= β; | |
| 268 | y.as_mut_slice().iter_mut().for_each(|x| *x *= β); | |
| 269 | } | |
| 270 | let h = self.h; | |
| 271 | let m = self.len(); | |
| 272 | i.eval(|x| { | |
| 273 | assert_eq!(x.len(), m); | |
| 274 | for d in 0..N { | |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
275 | let mut v = y.generic_view_mut((d * m, 0), (Dyn(m), U1)); |
| 67 | 276 | for b in Iter::new(&self.dims, d) { |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
277 | self.discretisation.add_diff_mut(&mut v, x, α / h, b) |
| 67 | 278 | } |
| 279 | } | |
| 280 | }) | |
| 281 | } | |
| 282 | } | |
| 283 | ||
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
284 | impl<F, B, const N: usize> Mapping<DVector<F>> for Div<F, B, N> |
| 67 | 285 | where |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
286 | B: Discretisation<F>, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
287 | F: Float + nalgebra::RealField, |
| 67 | 288 | { |
| 289 | type Codomain = DVector<F>; | |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
290 | fn apply<I: Instance<DVector<F>>>(&self, i: I) -> DVector<F> { |
| 67 | 291 | let mut y = DVector::zeros(self.len()); |
| 292 | self.apply_add(&mut y, i); | |
| 293 | y | |
| 294 | } | |
| 295 | } | |
| 296 | ||
| 297 | #[replace_float_literals(F::cast_from(literal))] | |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
298 | impl<F, B, const N: usize> GEMV<F, DVector<F>> for Div<F, B, N> |
| 67 | 299 | where |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
300 | B: Discretisation<F>, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
301 | F: Float + nalgebra::RealField, |
| 67 | 302 | { |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
303 | fn gemv<I: Instance<DVector<F>>>(&self, y: &mut DVector<F>, α: F, i: I, β: F) { |
| 67 | 304 | if β == 0.0 { |
| 305 | y.as_mut_slice().iter_mut().for_each(|x| *x = 0.0); | |
| 306 | } else if β != 1.0 { | |
| 307 | //*y *= β; | |
| 308 | y.as_mut_slice().iter_mut().for_each(|x| *x *= β); | |
| 309 | } | |
| 310 | let h = self.h; | |
| 311 | let m = self.len(); | |
|
133
2b13f8a0c8ba
Replace Instance ref_instance and decompose by eval_* for flexibility
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
312 | i.eval_decompose(|x| { |
| 67 | 313 | assert_eq!(x.len(), N * m); |
| 314 | for d in 0..N { | |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
315 | let v = x.generic_view((d * m, 0), (Dyn(m), U1)); |
| 67 | 316 | for b in Iter::new(&self.dims, d) { |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
317 | self.discretisation.add_diff_mut(y, &v, α / h, b) |
| 67 | 318 | } |
| 319 | } | |
| 320 | }) | |
| 321 | } | |
| 322 | } | |
| 323 | ||
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
324 | impl<F, B, const N: usize> Grad<F, B, N> |
| 67 | 325 | where |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
326 | B: Discretisation<F>, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
327 | F: Float + nalgebra::RealField, |
| 67 | 328 | { |
| 329 | /// Creates a new discrete gradient operator for the vector `u`, verifying dimensions. | |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
330 | pub fn new_for(u: &DVector<F>, h: F, dims: [usize; N], discretisation: B) -> Option<Self> { |
| 67 | 331 | if u.len() == dims.iter().product::<usize>() { |
|
186
afe04e6b4a5b
StaticEuclidean Principal reference fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
133
diff
changeset
|
332 | Some(Grad { dims, h, discretisation }) |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
333 | } else { |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
334 | None |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
335 | } |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
336 | } |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
337 | |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
338 | fn len(&self) -> usize { |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
339 | self.dims.iter().product::<usize>() |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
340 | } |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
341 | } |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
342 | |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
343 | impl<F, B, const N: usize> Div<F, B, N> |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
344 | where |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
345 | B: Discretisation<F>, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
346 | F: Float + nalgebra::RealField, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
347 | { |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
348 | /// Creates a new discrete gradient operator for the vector `u`, verifying dimensions. |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
349 | pub fn new_for(u: &DVector<F>, h: F, dims: [usize; N], discretisation: B) -> Option<Self> { |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
350 | if u.len() == dims.iter().product::<usize>() * N { |
|
186
afe04e6b4a5b
StaticEuclidean Principal reference fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
133
diff
changeset
|
351 | Some(Div { dims, h, discretisation }) |
| 67 | 352 | } else { |
| 353 | None | |
| 354 | } | |
| 355 | } | |
| 356 | ||
| 357 | fn len(&self) -> usize { | |
| 358 | self.dims.iter().product::<usize>() | |
| 359 | } | |
| 360 | } | |
| 361 | ||
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
362 | impl<F, B, const N: usize> Linear<DVector<F>> for Grad<F, B, N> |
| 67 | 363 | where |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
364 | B: Discretisation<F>, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
365 | F: Float + nalgebra::RealField, |
| 67 | 366 | { |
| 367 | } | |
| 368 | ||
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
369 | impl<F, B, const N: usize> Linear<DVector<F>> for Div<F, B, N> |
| 67 | 370 | where |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
371 | B: Discretisation<F>, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
372 | F: Float + nalgebra::RealField, |
| 67 | 373 | { |
| 374 | } | |
| 375 | ||
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
376 | impl<F, B, const N: usize> BoundedLinear<DVector<F>, L2, L2, F> for Grad<F, B, N> |
| 67 | 377 | where |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
378 | B: Discretisation<F>, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
379 | F: Float + nalgebra::RealField, |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
110
diff
changeset
|
380 | DVector<F>: Norm<L2, F>, |
| 67 | 381 | { |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
382 | fn opnorm_bound(&self, _: L2, _: L2) -> DynResult<F> { |
| 67 | 383 | // Fuck nalgebra. |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
384 | self.discretisation |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
385 | .opnorm_bound(num_traits::Float::abs(self.h)) |
| 67 | 386 | } |
| 387 | } | |
| 388 | ||
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
389 | impl<F, B, const N: usize> BoundedLinear<DVector<F>, L2, L2, F> for Div<F, B, N> |
| 67 | 390 | where |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
391 | B: Discretisation<F>, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
392 | F: Float + nalgebra::RealField, |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
110
diff
changeset
|
393 | DVector<F>: Norm<L2, F>, |
| 67 | 394 | { |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
395 | fn opnorm_bound(&self, _: L2, _: L2) -> DynResult<F> { |
| 67 | 396 | // Fuck nalgebra. |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
397 | self.discretisation |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
398 | .opnorm_bound(num_traits::Float::abs(self.h)) |
| 67 | 399 | } |
| 400 | } | |
| 401 | ||
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
402 | impl<F, B, const N: usize> Adjointable<DVector<F>, DVector<F>> for Grad<F, B, N> |
| 67 | 403 | where |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
404 | B: Discretisation<F>, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
405 | F: Float + nalgebra::RealField, |
| 67 | 406 | { |
| 407 | type AdjointCodomain = DVector<F>; | |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
408 | type Adjoint<'a> |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
409 | = Div<F, B::Opposite, N> |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
410 | where |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
411 | Self: 'a; |
| 67 | 412 | |
| 413 | fn adjoint(&self) -> Self::Adjoint<'_> { | |
|
186
afe04e6b4a5b
StaticEuclidean Principal reference fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
133
diff
changeset
|
414 | Div { dims: self.dims, h: -self.h, discretisation: self.discretisation.opposite() } |
|
afe04e6b4a5b
StaticEuclidean Principal reference fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
133
diff
changeset
|
415 | } |
|
afe04e6b4a5b
StaticEuclidean Principal reference fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
133
diff
changeset
|
416 | } |
|
afe04e6b4a5b
StaticEuclidean Principal reference fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
133
diff
changeset
|
417 | |
|
afe04e6b4a5b
StaticEuclidean Principal reference fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
133
diff
changeset
|
418 | impl<F, B, const N: usize> SimplyAdjointable<DVector<F>, DVector<F>> for Grad<F, B, N> |
|
afe04e6b4a5b
StaticEuclidean Principal reference fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
133
diff
changeset
|
419 | where |
|
afe04e6b4a5b
StaticEuclidean Principal reference fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
133
diff
changeset
|
420 | B: Discretisation<F>, |
|
afe04e6b4a5b
StaticEuclidean Principal reference fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
133
diff
changeset
|
421 | F: Float + nalgebra::RealField, |
|
afe04e6b4a5b
StaticEuclidean Principal reference fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
133
diff
changeset
|
422 | { |
|
afe04e6b4a5b
StaticEuclidean Principal reference fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
133
diff
changeset
|
423 | type AdjointCodomain = DVector<F>; |
|
afe04e6b4a5b
StaticEuclidean Principal reference fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
133
diff
changeset
|
424 | type SimpleAdjoint = Div<F, B::Opposite, N>; |
|
afe04e6b4a5b
StaticEuclidean Principal reference fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
133
diff
changeset
|
425 | |
|
afe04e6b4a5b
StaticEuclidean Principal reference fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
133
diff
changeset
|
426 | fn adjoint(&self) -> Self::SimpleAdjoint { |
|
afe04e6b4a5b
StaticEuclidean Principal reference fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
133
diff
changeset
|
427 | Div { dims: self.dims, h: -self.h, discretisation: self.discretisation.opposite() } |
| 67 | 428 | } |
| 429 | } | |
| 430 | ||
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
431 | impl<F, B, const N: usize> Adjointable<DVector<F>, DVector<F>> for Div<F, B, N> |
| 67 | 432 | where |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
433 | B: Discretisation<F>, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
434 | F: Float + nalgebra::RealField, |
| 67 | 435 | { |
| 436 | type AdjointCodomain = DVector<F>; | |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
437 | type Adjoint<'a> |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
438 | = Grad<F, B::Opposite, N> |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
439 | where |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
440 | Self: 'a; |
| 67 | 441 | |
| 442 | fn adjoint(&self) -> Self::Adjoint<'_> { | |
|
186
afe04e6b4a5b
StaticEuclidean Principal reference fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
133
diff
changeset
|
443 | Grad { dims: self.dims, h: -self.h, discretisation: self.discretisation.opposite() } |
|
afe04e6b4a5b
StaticEuclidean Principal reference fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
133
diff
changeset
|
444 | } |
|
afe04e6b4a5b
StaticEuclidean Principal reference fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
133
diff
changeset
|
445 | } |
|
afe04e6b4a5b
StaticEuclidean Principal reference fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
133
diff
changeset
|
446 | |
|
afe04e6b4a5b
StaticEuclidean Principal reference fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
133
diff
changeset
|
447 | impl<F, B, const N: usize> SimplyAdjointable<DVector<F>, DVector<F>> for Div<F, B, N> |
|
afe04e6b4a5b
StaticEuclidean Principal reference fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
133
diff
changeset
|
448 | where |
|
afe04e6b4a5b
StaticEuclidean Principal reference fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
133
diff
changeset
|
449 | B: Discretisation<F>, |
|
afe04e6b4a5b
StaticEuclidean Principal reference fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
133
diff
changeset
|
450 | F: Float + nalgebra::RealField, |
|
afe04e6b4a5b
StaticEuclidean Principal reference fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
133
diff
changeset
|
451 | { |
|
afe04e6b4a5b
StaticEuclidean Principal reference fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
133
diff
changeset
|
452 | type AdjointCodomain = DVector<F>; |
|
afe04e6b4a5b
StaticEuclidean Principal reference fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
133
diff
changeset
|
453 | type SimpleAdjoint = Grad<F, B::Opposite, N>; |
|
afe04e6b4a5b
StaticEuclidean Principal reference fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
133
diff
changeset
|
454 | |
|
afe04e6b4a5b
StaticEuclidean Principal reference fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
133
diff
changeset
|
455 | fn adjoint(&self) -> Self::SimpleAdjoint { |
|
afe04e6b4a5b
StaticEuclidean Principal reference fix
Tuomo Valkonen <tuomov@iki.fi>
parents:
133
diff
changeset
|
456 | Grad { dims: self.dims, h: -self.h, discretisation: self.discretisation.opposite() } |
| 67 | 457 | } |
| 458 | } | |
| 459 | ||
| 460 | #[cfg(test)] | |
| 461 | mod tests { | |
| 462 | use super::*; | |
| 463 | ||
| 464 | #[test] | |
| 465 | fn grad_adjoint() { | |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
466 | let im = DVector::from((0..9).map(|t| t as f64).collect::<Vec<_>>()); |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
467 | let v = DVector::from((0..18).map(|t| t as f64).collect::<Vec<_>>()); |
| 67 | 468 | |
| 469 | let grad = Grad::new_for(&im, 1.0, [3, 3], ForwardNeumann).unwrap(); | |
| 470 | let a = grad.apply(&im).dot(&v); | |
| 471 | let b = grad.adjoint().apply(&v).dot(&im); | |
| 472 | assert_eq!(a, b); | |
| 473 | ||
| 474 | let grad = Grad::new_for(&im, 1.0, [3, 3], ForwardDirichlet).unwrap(); | |
| 475 | let a = grad.apply(&im).dot(&v); | |
| 476 | let b = grad.adjoint().apply(&v).dot(&im); | |
| 477 | assert_eq!(a, b); | |
| 478 | } | |
| 479 | } |