Mon, 24 Oct 2022 09:41:43 +0300
Allow step closure of AlgIterators to indicate succesfull termination or failure.
0 | 1 | /// Linear solvers for small problems. |
2 | ||
3 | use crate::types::Float; | |
4 | use std::mem::MaybeUninit; | |
5 | ||
6 | /// Gaussian elimination for $AX=B$, where $A$ and $B$ are both stored in `ab`, | |
7 | /// $A \in \mathbb{R}^{M \times M}$ and $X, B \in \mathbb{R}^{M \times K}$. | |
8 | pub fn linsolve0<F : Float, const M : usize, const N : usize, const K : usize>( | |
9 | mut ab : [[F; N]; M] | |
10 | ) -> [[F; K]; M] { | |
11 | assert_eq!(M + K , N); | |
12 | ||
13 | let mut k = 0; | |
14 | ||
15 | // Convert to row-echelon form | |
16 | for h in 0..(M-1) { | |
17 | // Find pivotable column (has some non-zero entries in rows ≥ h) | |
18 | 'find_pivot: while k < N { | |
19 | let (mut î, mut v) = (h, ab[h][k].abs()); | |
20 | // Find row ≥ h of maximum absolute value in this column | |
21 | for i in (h+1)..M { | |
22 | let ṽ = ab[i][k].abs(); | |
23 | if ṽ > v { | |
24 | î = i; | |
25 | v = ṽ; | |
26 | } | |
27 | } | |
28 | if v > F::ZERO { | |
29 | ab.swap(h, î); | |
30 | for i in (h+1)..M { | |
31 | let f = ab[i][k] / ab[h][k]; | |
32 | ab[i][k] = F::ZERO; | |
33 | for j in (k+1)..N { | |
34 | ab[i][j] -= ab[h][j]*f; | |
35 | } | |
36 | } | |
37 | k += 1; | |
38 | break 'find_pivot; | |
39 | } | |
40 | k += 1 | |
41 | } | |
42 | } | |
43 | ||
44 | // Solve UAX=UB for X where UA with U presenting the transformations above an | |
45 | // upper triangular matrix. | |
46 | // This use of MaybeUninit assumes F : Copy. Otherwise undefined behaviour may occur. | |
47 | let mut x : [[MaybeUninit<F>; K]; M] = core::array::from_fn(|_| MaybeUninit::uninit_array::<K>() ); | |
48 | //unsafe { std::mem::MaybeUninit::uninit().assume_init() }; | |
49 | for i in (0..M).rev() { | |
50 | for 𝓁 in 0..K { | |
51 | let mut tmp = ab[i][M+𝓁]; | |
52 | for j in (i+1)..M { | |
53 | tmp -= ab[i][j] * unsafe { *(x[j][𝓁].assume_init_ref()) }; | |
54 | } | |
55 | tmp /= ab[i][i]; | |
56 | x[i][𝓁].write(tmp); | |
57 | } | |
58 | } | |
59 | //unsafe { MaybeUninit::array_assume_init(x) }; | |
60 | let xinit = unsafe { | |
61 | //core::intrinsics::assert_inhabited::<[[F; K]; M]>(); | |
62 | (&x as *const _ as *const [[F; K]; M]).read() | |
63 | }; | |
64 | ||
65 | std::mem::forget(x); | |
66 | xinit | |
67 | } | |
68 | ||
69 | /// Gaussian elimination for $Ax=b$, where $A$ and $b$ are both stored in `ab`, | |
70 | /// $A \in \mathbb{R}^{M \times M}$ and $x, b \in \mathbb{R}^M$. | |
71 | #[inline] | |
72 | pub fn linsolve<F : Float, const M : usize, const N : usize>(ab : [[F; N]; M]) -> [F; M] { | |
73 | let x : [[F; 1]; M] = linsolve0(ab); | |
74 | unsafe { *((&x as *const [F; 1]) as *const [F; M] ) } | |
75 | } | |
76 | ||
77 | ||
78 | #[cfg(test)] | |
79 | mod tests { | |
80 | use super::*; | |
81 | ||
82 | #[test] | |
83 | fn linsolve_test() { | |
84 | let ab1 = [[1.0, 2.0, 3.0], [2.0, 1.0, 6.0]]; | |
85 | assert_eq!(linsolve(ab1), [3.0, 0.0]); | |
86 | let ab2 = [[1.0, 2.0, 0.0, 1.0], [4.0, 0.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0]]; | |
87 | assert_eq!(linsolve(ab2), [0.0, 0.5, 0.0]); | |
88 | } | |
89 | } | |
90 |