| 1 /*! |
1 /*! |
| 2 Linear equation solvers for small problems stored in Rust arrays. |
2 Linear equation solvers for small problems stored in Rust arrays. |
| 3 */ |
3 */ |
| 4 |
4 |
| 5 use crate::types::Float; |
5 use crate::types::Float; |
| 6 #[cfg(feature = "nightly")] |
6 #[cfg(nightly)] |
| 7 use std::mem::MaybeUninit; |
7 use std::mem::MaybeUninit; |
| 8 |
8 |
| 9 /// Gaussian elimination for $AX=B$, where $A$ and $B$ are both stored in `ab`, |
9 /// Gaussian elimination for $AX=B$, where $A$ and $B$ are both stored in `ab`, |
| 10 /// $A \in \mathbb{R}^{M \times M}$ and $X, B \in \mathbb{R}^{M \times K}$. |
10 /// $A \in \mathbb{R}^{M \times M}$ and $X, B \in \mathbb{R}^{M \times K}$. |
| 11 pub fn linsolve0<F: Float, const M: usize, const N: usize, const K: usize>( |
11 pub fn linsolve0<F: Float, const M: usize, const N: usize, const K: usize>( |
| 48 // upper triangular matrix. |
48 // upper triangular matrix. |
| 49 // |
49 // |
| 50 // If the "nightly" feature is enabled, we will use an uninitialised array for a |
50 // If the "nightly" feature is enabled, we will use an uninitialised array for a |
| 51 // little bit of efficiency. |
51 // little bit of efficiency. |
| 52 // This use of MaybeUninit assumes F : Copy. Otherwise undefined behaviour may occur. |
52 // This use of MaybeUninit assumes F : Copy. Otherwise undefined behaviour may occur. |
| 53 #[cfg(feature = "nightly")] |
53 #[cfg(nightly)] |
| 54 { |
54 { |
| 55 let mut x: [[MaybeUninit<F>; K]; M] = [[const { MaybeUninit::uninit() }; K]; M]; |
55 let mut x: [[MaybeUninit<F>; K]; M] = [[const { MaybeUninit::uninit() }; K]; M]; |
| 56 //unsafe { std::mem::MaybeUninit::uninit().assume_init() }; |
56 //unsafe { std::mem::MaybeUninit::uninit().assume_init() }; |
| 57 for i in (0..M).rev() { |
57 for i in (0..M).rev() { |
| 58 for 𝓁 in 0..K { |
58 for 𝓁 in 0..K { |
| 67 unsafe { |
67 unsafe { |
| 68 //core::intrinsics::assert_inhabited::<[[F; K]; M]>(); |
68 //core::intrinsics::assert_inhabited::<[[F; K]; M]>(); |
| 69 (&x as *const _ as *const [[F; K]; M]).read() |
69 (&x as *const _ as *const [[F; K]; M]).read() |
| 70 } |
70 } |
| 71 } |
71 } |
| 72 #[cfg(not(feature = "nightly"))] |
72 #[cfg(not(nightly))] |
| 73 { |
73 { |
| 74 let mut x: [[F; K]; M] = [[F::ZERO; K]; M]; |
74 let mut x: [[F; K]; M] = [[F::ZERO; K]; M]; |
| 75 for i in (0..M).rev() { |
75 for i in (0..M).rev() { |
| 76 for 𝓁 in 0..K { |
76 for 𝓁 in 0..K { |
| 77 let mut tmp = ab[i][M + 𝓁]; |
77 let mut tmp = ab[i][M + 𝓁]; |