Tue, 02 Sep 2025 00:05:29 -0500
bazquk
| 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; |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
6 | use crate::linops::{Adjointable, BoundedLinear, Linear, Mapping, GEMV}; |
|
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>(); | |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
210 | Iter { |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
211 | dims, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
212 | d, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
213 | d_stride, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
214 | i: [0; N], |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
215 | k: 0, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
216 | len, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
217 | } |
| 67 | 218 | } |
| 219 | } | |
| 220 | ||
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
221 | impl<'a, const N: usize> Iterator for Iter<'a, N> { |
| 67 | 222 | type Item = DiscretisationOrInterior; |
| 223 | fn next(&mut self) -> Option<Self::Item> { | |
| 224 | let res = if self.k >= self.len { | |
| 225 | None | |
| 226 | } else { | |
| 227 | let cartesian_idx = self.i[self.d]; | |
| 228 | let cartesian_max = self.dims[self.d]; | |
| 229 | let k = self.k; | |
| 230 | ||
| 231 | if cartesian_idx == 0 { | |
| 232 | Some(LeftBoundary(k, k + self.d_stride)) | |
| 233 | } else if cartesian_idx + 1 >= cartesian_max { | |
| 234 | Some(RightBoundary(k, k - self.d_stride)) | |
| 235 | } else { | |
| 236 | Some(Interior(k, (k - self.d_stride, k + self.d_stride))) | |
| 237 | } | |
| 238 | }; | |
| 239 | self.k += 1; | |
| 240 | for j in 0..N { | |
| 241 | if self.i[j] + 1 < self.dims[j] { | |
| 242 | self.i[j] += 1; | |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
243 | break; |
| 67 | 244 | } |
| 245 | self.i[j] = 0 | |
| 246 | } | |
| 247 | res | |
| 248 | } | |
| 249 | } | |
| 250 | ||
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
251 | impl<F, B, const N: usize> Mapping<DVector<F>> for Grad<F, B, N> |
| 67 | 252 | where |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
253 | B: Discretisation<F>, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
254 | F: Float + nalgebra::RealField, |
| 67 | 255 | { |
| 256 | type Codomain = DVector<F>; | |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
257 | fn apply<I: Instance<DVector<F>>>(&self, i: I) -> DVector<F> { |
| 67 | 258 | let mut y = DVector::zeros(N * self.len()); |
| 259 | self.apply_add(&mut y, i); | |
| 260 | y | |
| 261 | } | |
| 262 | } | |
| 263 | ||
| 264 | #[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
|
265 | impl<F, B, const N: usize> GEMV<F, DVector<F>> for Grad<F, B, N> |
| 67 | 266 | where |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
267 | B: Discretisation<F>, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
268 | F: Float + nalgebra::RealField, |
| 67 | 269 | { |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
270 | fn gemv<I: Instance<DVector<F>>>(&self, y: &mut DVector<F>, α: F, i: I, β: F) { |
| 67 | 271 | if β == 0.0 { |
| 272 | y.as_mut_slice().iter_mut().for_each(|x| *x = 0.0); | |
| 273 | } else if β != 1.0 { | |
| 274 | //*y *= β; | |
| 275 | y.as_mut_slice().iter_mut().for_each(|x| *x *= β); | |
| 276 | } | |
| 277 | let h = self.h; | |
| 278 | let m = self.len(); | |
| 279 | i.eval(|x| { | |
| 280 | assert_eq!(x.len(), m); | |
| 281 | 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
|
282 | let mut v = y.generic_view_mut((d * m, 0), (Dyn(m), U1)); |
| 67 | 283 | 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
|
284 | self.discretisation.add_diff_mut(&mut v, x, α / h, b) |
| 67 | 285 | } |
| 286 | } | |
| 287 | }) | |
| 288 | } | |
| 289 | } | |
| 290 | ||
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
291 | impl<F, B, const N: usize> Mapping<DVector<F>> for Div<F, B, N> |
| 67 | 292 | where |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
293 | B: Discretisation<F>, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
294 | F: Float + nalgebra::RealField, |
| 67 | 295 | { |
| 296 | type Codomain = DVector<F>; | |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
297 | fn apply<I: Instance<DVector<F>>>(&self, i: I) -> DVector<F> { |
| 67 | 298 | let mut y = DVector::zeros(self.len()); |
| 299 | self.apply_add(&mut y, i); | |
| 300 | y | |
| 301 | } | |
| 302 | } | |
| 303 | ||
| 304 | #[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
|
305 | impl<F, B, const N: usize> GEMV<F, DVector<F>> for Div<F, B, N> |
| 67 | 306 | where |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
307 | B: Discretisation<F>, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
308 | F: Float + nalgebra::RealField, |
| 67 | 309 | { |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
310 | fn gemv<I: Instance<DVector<F>>>(&self, y: &mut DVector<F>, α: F, i: I, β: F) { |
| 67 | 311 | if β == 0.0 { |
| 312 | y.as_mut_slice().iter_mut().for_each(|x| *x = 0.0); | |
| 313 | } else if β != 1.0 { | |
| 314 | //*y *= β; | |
| 315 | y.as_mut_slice().iter_mut().for_each(|x| *x *= β); | |
| 316 | } | |
| 317 | let h = self.h; | |
| 318 | 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
|
319 | i.eval_decompose(|x| { |
| 67 | 320 | assert_eq!(x.len(), N * m); |
| 321 | 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
|
322 | let v = x.generic_view((d * m, 0), (Dyn(m), U1)); |
| 67 | 323 | 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
|
324 | self.discretisation.add_diff_mut(y, &v, α / h, b) |
| 67 | 325 | } |
| 326 | } | |
| 327 | }) | |
| 328 | } | |
| 329 | } | |
| 330 | ||
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
331 | impl<F, B, const N: usize> Grad<F, B, N> |
| 67 | 332 | where |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
333 | B: Discretisation<F>, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
334 | F: Float + nalgebra::RealField, |
| 67 | 335 | { |
| 336 | /// 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
|
337 | pub fn new_for(u: &DVector<F>, h: F, dims: [usize; N], discretisation: B) -> Option<Self> { |
| 67 | 338 | if u.len() == dims.iter().product::<usize>() { |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
339 | Some(Grad { |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
340 | dims, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
341 | h, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
342 | discretisation, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
343 | }) |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
344 | } else { |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
345 | None |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
346 | } |
|
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 | |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
349 | fn len(&self) -> usize { |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
350 | self.dims.iter().product::<usize>() |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
351 | } |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
352 | } |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
353 | |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
354 | 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
|
355 | where |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
356 | B: Discretisation<F>, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
357 | F: Float + nalgebra::RealField, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
358 | { |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
359 | /// 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
|
360 | 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
|
361 | if u.len() == dims.iter().product::<usize>() * N { |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
362 | Some(Div { |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
363 | dims, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
364 | h, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
365 | discretisation, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
366 | }) |
| 67 | 367 | } else { |
| 368 | None | |
| 369 | } | |
| 370 | } | |
| 371 | ||
| 372 | fn len(&self) -> usize { | |
| 373 | self.dims.iter().product::<usize>() | |
| 374 | } | |
| 375 | } | |
| 376 | ||
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
377 | impl<F, B, const N: usize> Linear<DVector<F>> for Grad<F, B, N> |
| 67 | 378 | where |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
379 | B: Discretisation<F>, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
380 | F: Float + nalgebra::RealField, |
| 67 | 381 | { |
| 382 | } | |
| 383 | ||
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
384 | impl<F, B, const N: usize> Linear<DVector<F>> for Div<F, B, N> |
| 67 | 385 | where |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
386 | B: Discretisation<F>, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
387 | F: Float + nalgebra::RealField, |
| 67 | 388 | { |
| 389 | } | |
| 390 | ||
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
391 | impl<F, B, const N: usize> BoundedLinear<DVector<F>, L2, L2, F> for Grad<F, B, N> |
| 67 | 392 | where |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
393 | B: Discretisation<F>, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
394 | F: Float + nalgebra::RealField, |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
110
diff
changeset
|
395 | DVector<F>: Norm<L2, F>, |
| 67 | 396 | { |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
397 | fn opnorm_bound(&self, _: L2, _: L2) -> DynResult<F> { |
| 67 | 398 | // Fuck nalgebra. |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
399 | self.discretisation |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
400 | .opnorm_bound(num_traits::Float::abs(self.h)) |
| 67 | 401 | } |
| 402 | } | |
| 403 | ||
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
404 | impl<F, B, const N: usize> BoundedLinear<DVector<F>, L2, L2, F> for Div<F, B, N> |
| 67 | 405 | where |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
406 | B: Discretisation<F>, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
407 | F: Float + nalgebra::RealField, |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
110
diff
changeset
|
408 | DVector<F>: Norm<L2, F>, |
| 67 | 409 | { |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
410 | fn opnorm_bound(&self, _: L2, _: L2) -> DynResult<F> { |
| 67 | 411 | // Fuck nalgebra. |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
412 | self.discretisation |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
413 | .opnorm_bound(num_traits::Float::abs(self.h)) |
| 67 | 414 | } |
| 415 | } | |
| 416 | ||
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
417 | impl<F, B, const N: usize> Adjointable<DVector<F>, DVector<F>> for Grad<F, B, N> |
| 67 | 418 | where |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
419 | B: Discretisation<F>, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
420 | F: Float + nalgebra::RealField, |
| 67 | 421 | { |
| 422 | type AdjointCodomain = DVector<F>; | |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
423 | type Adjoint<'a> |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
424 | = Div<F, B::Opposite, N> |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
425 | where |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
426 | Self: 'a; |
| 67 | 427 | |
| 428 | /// Form the adjoint operator of `self`. | |
| 429 | fn adjoint(&self) -> Self::Adjoint<'_> { | |
| 430 | Div { | |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
431 | dims: self.dims, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
432 | h: -self.h, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
433 | discretisation: self.discretisation.opposite(), |
| 67 | 434 | } |
| 435 | } | |
| 436 | } | |
| 437 | ||
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
438 | impl<F, B, const N: usize> Adjointable<DVector<F>, DVector<F>> for Div<F, B, N> |
| 67 | 439 | where |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
440 | B: Discretisation<F>, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
441 | F: Float + nalgebra::RealField, |
| 67 | 442 | { |
| 443 | type AdjointCodomain = DVector<F>; | |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
444 | type Adjoint<'a> |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
445 | = Grad<F, B::Opposite, N> |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
446 | where |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
447 | Self: 'a; |
| 67 | 448 | |
| 449 | /// Form the adjoint operator of `self`. | |
| 450 | fn adjoint(&self) -> Self::Adjoint<'_> { | |
| 451 | Grad { | |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
452 | dims: self.dims, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
453 | h: -self.h, |
|
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
454 | discretisation: self.discretisation.opposite(), |
| 67 | 455 | } |
| 456 | } | |
| 457 | } | |
| 458 | ||
| 459 | #[cfg(test)] | |
| 460 | mod tests { | |
| 461 | use super::*; | |
| 462 | ||
| 463 | #[test] | |
| 464 | fn grad_adjoint() { | |
|
110
a1278320be26
Use DynResult for Lipschitz factors and operator norm bounds
Tuomo Valkonen <tuomov@iki.fi>
parents:
74
diff
changeset
|
465 | 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
|
466 | let v = DVector::from((0..18).map(|t| t as f64).collect::<Vec<_>>()); |
| 67 | 467 | |
| 468 | let grad = Grad::new_for(&im, 1.0, [3, 3], ForwardNeumann).unwrap(); | |
| 469 | let a = grad.apply(&im).dot(&v); | |
| 470 | let b = grad.adjoint().apply(&v).dot(&im); | |
| 471 | assert_eq!(a, b); | |
| 472 | ||
| 473 | let grad = Grad::new_for(&im, 1.0, [3, 3], ForwardDirichlet).unwrap(); | |
| 474 | let a = grad.apply(&im).dot(&v); | |
| 475 | let b = grad.adjoint().apply(&v).dot(&im); | |
| 476 | assert_eq!(a, b); | |
| 477 | } | |
| 478 | } |