1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
//! Linear solves: A x = b via LU with partial pivoting on a dense copy.
use crate::{
error::Error,
traits::{Real, State},
};
use super::base::{Matrix, MatrixStorage};
impl<T: Real> Matrix<T> {
/// Solve A x = b, Returns Err if the matrix is singular or dimensions are incompatible
pub fn lin_solve<Y>(&self, b: Y) -> Result<Y, Error<T, Y>>
where
Y: State<T>,
{
let n = self.n;
if b.len() != n {
return Err(Error::BadInput {
msg: "Incompatible vector length".into(),
});
}
// 1) Densify A into a Vec<T> of size n*n (row-major)
let mut a = vec![T::zero(); n * n];
match &self.storage {
MatrixStorage::Identity => {
for i in 0..n {
a[i * n + i] = T::one();
}
}
MatrixStorage::Full => {
a.copy_from_slice(&self.data[0..n * n]);
}
MatrixStorage::Banded { ml, mu, .. } => {
let rows = *ml + *mu + 1;
for j in 0..self.m {
for r in 0..rows {
let k = r as isize - *mu as isize; // i - j
let i_signed = j as isize + k;
if i_signed >= 0 && (i_signed as usize) < self.n {
let i = i_signed as usize;
a[i * self.m + j] += self.data[r * self.m + j];
}
}
}
}
}
// 2) Copy b into a dense vector x and perform solve
let mut x = b;
// 3) LU factorization with partial pivoting and singularity checking
let mut piv: Vec<usize> = (0..n).collect();
let eps = T::from_f64(1e-14).unwrap(); // Singularity threshold
let mut swapper;
for k in 0..n {
// Find pivot row
let mut pivot_row = k;
let mut pivot_val = a[k * n + k].abs();
for i in (k + 1)..n {
let val = a[i * n + k].abs();
if val > pivot_val {
pivot_val = val;
pivot_row = i;
}
}
// Check for singularity
if pivot_val <= eps {
// Note the t, y are not known here and should be updated by caller before returning to user
return Err(Error::LinearAlgebra {
msg: "Singular matrix encountered".into(),
});
}
if pivot_row != k {
// swap rows in A
for j in 0..n {
a.swap(k * n + j, pivot_row * n + j);
}
// swap entries in x
swapper = x.get(k);
x.set(k, x.get(pivot_row));
x.set(pivot_row, swapper);
piv.swap(k, pivot_row);
}
// Eliminate below the pivot
let akk = a[k * n + k];
for i in (k + 1)..n {
let factor = a[i * n + k] / akk;
a[i * n + k] = factor; // store L(i,k)
for j in (k + 1)..n {
a[i * n + j] = a[i * n + j] - factor * a[k * n + j];
}
}
}
// Forward solve Ly = Pb (x currently holds permuted b)
for i in 0..n {
let mut sum = x.get(i);
for k in 0..i {
sum -= a[i * n + k] * x.get(k);
}
x.set(i, sum); // since L has ones on diagonal
}
// Backward solve Ux = y
for i in (0..n).rev() {
let mut sum = x.get(i);
for k in (i + 1)..n {
sum -= a[i * n + k] * x.get(k);
}
x.set(i, sum / a[i * n + i]);
}
// Build output State from x
let mut out = Y::zeros();
for i in 0..n {
out.set(i, x.get(i));
}
Ok(out)
}
/// In-place solve: overwrites `b` with `x`.
pub fn lin_solve_mut(&self, b: &mut [T]) {
let n = self.n;
assert_eq!(
b.len(),
n,
"dimension mismatch in solve: A is {}x{}, b has length {}",
n,
n,
b.len()
);
// Densify A into row-major Vec<T>
let mut a = vec![T::zero(); n * n];
match &self.storage {
MatrixStorage::Identity => {
for i in 0..n {
a[i * n + i] = T::one();
}
}
MatrixStorage::Full => {
a.copy_from_slice(&self.data[0..n * n]);
}
MatrixStorage::Banded { ml, mu, .. } => {
let rows = *ml + *mu + 1;
for j in 0..self.m {
for r in 0..rows {
let k = r as isize - *mu as isize;
let i_signed = j as isize + k;
if i_signed >= 0 && (i_signed as usize) < self.n {
let i = i_signed as usize;
a[i * self.m + j] += self.data[r * self.m + j];
}
}
}
}
}
// LU with partial pivoting, applying permutations to b
for k in 0..n {
// pivot
let mut pivot_row = k;
let mut pivot_val = a[k * n + k].abs();
for i in (k + 1)..n {
let val = a[i * n + k].abs();
if val > pivot_val {
pivot_val = val;
pivot_row = i;
}
}
if pivot_val == T::zero() {
panic!("singular matrix in solve");
}
if pivot_row != k {
for j in 0..n {
a.swap(k * n + j, pivot_row * n + j);
}
b.swap(k, pivot_row);
}
// Eliminate below the pivot
let akk = a[k * n + k];
for i in (k + 1)..n {
let factor = a[i * n + k] / akk;
a[i * n + k] = factor;
for j in (k + 1)..n {
a[i * n + j] = a[i * n + j] - factor * a[k * n + j];
}
}
}
// Forward solve Ly = Pb (b is permuted)
for i in 0..n {
let mut sum = b[i];
for k in 0..i {
sum -= a[i * n + k] * b[k];
}
b[i] = sum;
}
// Backward solve Ux = y
for i in (0..n).rev() {
let mut sum = b[i];
for k in (i + 1)..n {
sum -= a[i * n + k] * b[k];
}
b[i] = sum / a[i * n + i];
}
}
}
#[cfg(test)]
mod tests {
use crate::linalg::matrix::Matrix;
use nalgebra::Vector2;
#[test]
fn solve_full_2x2() {
// A = [[3, 2],[1, 4]], b = [5, 6] -> x = [0.8, 1.3]
let mut a: Matrix<f64> = Matrix::full(2, 2);
a[(0, 0)] = 3.0;
a[(0, 1)] = 2.0;
a[(1, 0)] = 1.0;
a[(1, 1)] = 4.0;
let b = Vector2::new(5.0, 6.0);
let x = a.lin_solve(b).unwrap();
// Solve manually: [[3,2],[1,4]] x = [5,6] => x = [ (20-12)/10, (15-5)/10 ] = [0.8, 1.3]
assert!((x.x - 0.8).abs() < 1e-12);
assert!((x.y - 1.3).abs() < 1e-12);
}
}