gmac/morph/
linear_algebra.rs1use hologram::linear_algebra::lu_linear_solver;
2
3use crate::error::Result;
4
5pub fn least_squares_solver(mat: &[Vec<f64>], rhs: &[Vec<f64>]) -> Result<Vec<Vec<f64>>> {
16 let mat_rows = mat.len();
17 let mat_cols = mat[0].len();
18 let rhs_dim = rhs[0].len();
19
20 let mut ata = vec![vec![0.0; mat_cols]; mat_cols];
22 for i in 0..mat_cols {
23 for j in 0..mat_cols {
24 for k in 0..mat_rows {
25 ata[i][j] += mat[k][i] * mat[k][j];
26 }
27 }
28 }
29
30 let mut atb = vec![vec![0.0; rhs_dim]; mat_cols];
32 for i in 0..mat_cols {
33 for d in 0..rhs_dim {
34 for k in 0..mat_rows {
35 atb[i][d] += mat[k][i] * rhs[k][d];
36 }
37 }
38 }
39
40 let x = lu_linear_solver(&ata, &atb)?;
41
42 Ok(x)
43}