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