#![allow(unused_variables)]
#![allow(unused_assignments)]
#![allow(unused_mut)]
use crate::error::{SparseError, SparseResult};
use crate::sparray::SparseArray;
use scirs2_core::ndarray::{Array1, ArrayView1};
use scirs2_core::numeric::{Float, SparseElement};
use std::fmt::Debug;
#[derive(Debug, Clone)]
pub struct LSQROptions {
pub max_iter: usize,
pub atol: f64,
pub btol: f64,
pub conlim: f64,
pub calc_var: bool,
pub store_residual_history: bool,
}
impl Default for LSQROptions {
fn default() -> Self {
Self {
max_iter: 1000,
atol: 1e-8,
btol: 1e-8,
conlim: 1e8,
calc_var: false,
store_residual_history: true,
}
}
}
#[derive(Debug, Clone)]
pub struct LSQRResult<T> {
pub x: Array1<T>,
pub iterations: usize,
pub residualnorm: T,
pub solution_norm: T,
pub condition_number: T,
pub converged: bool,
pub standard_errors: Option<Array1<T>>,
pub residual_history: Option<Vec<T>>,
pub convergence_reason: String,
}
#[allow(dead_code)]
pub fn lsqr<T, S>(
matrix: &S,
b: &ArrayView1<T>,
x0: Option<&ArrayView1<T>>,
options: LSQROptions,
) -> SparseResult<LSQRResult<T>>
where
T: Float + SparseElement + Debug + Copy + 'static,
S: SparseArray<T>,
{
let (m, n) = matrix.shape();
if b.len() != m {
return Err(SparseError::DimensionMismatch {
expected: m,
found: b.len(),
});
}
let mut x = match x0 {
Some(x0_val) => {
if x0_val.len() != n {
return Err(SparseError::DimensionMismatch {
expected: n,
found: x0_val.len(),
});
}
x0_val.to_owned()
}
None => Array1::zeros(n),
};
let ax = matrix_vector_multiply(matrix, &x.view())?;
let mut u = b - &ax;
let beta = l2_norm(&u.view());
if beta > T::sparse_zero() {
for i in 0..m {
u[i] = u[i] / beta;
}
}
let mut v = matrix_transpose_vector_multiply(matrix, &u.view())?;
let mut alpha = l2_norm(&v.view());
if alpha > T::sparse_zero() {
for i in 0..n {
v[i] = v[i] / alpha;
}
}
let mut w = v.clone();
let mut x_norm = T::sparse_zero();
let mut dd_norm = T::sparse_zero();
let mut res2 = beta;
let mut rho_bar = alpha;
let mut phi_bar = beta;
let atol = T::from(options.atol).expect("Operation failed");
let btol = T::from(options.btol).expect("Operation failed");
let conlim = T::from(options.conlim).expect("Operation failed");
let mut residual_history = if options.store_residual_history {
Some(vec![beta])
} else {
None
};
let mut converged = false;
let mut convergence_reason = String::new();
let mut iter = 0;
for k in 0..options.max_iter {
iter = k + 1;
let av = matrix_vector_multiply(matrix, &v.view())?;
for i in 0..m {
u[i] = av[i] - alpha * u[i];
}
let beta_new = l2_norm(&u.view());
if beta_new > T::sparse_zero() {
for i in 0..m {
u[i] = u[i] / beta_new;
}
}
let atu = matrix_transpose_vector_multiply(matrix, &u.view())?;
for i in 0..n {
v[i] = atu[i] - beta_new * v[i];
}
let alpha_new = l2_norm(&v.view());
if alpha_new > T::sparse_zero() {
for i in 0..n {
v[i] = v[i] / alpha_new;
}
}
let rho = (rho_bar * rho_bar + beta_new * beta_new).sqrt();
let c = rho_bar / rho;
let s = beta_new / rho;
let theta = s * alpha_new;
let rho_bar_new = -c * alpha_new;
let phi = c * phi_bar;
let phi_bar_new = s * phi_bar;
for i in 0..n {
x[i] = x[i] + (phi / rho) * w[i];
w[i] = v[i] - (theta / rho) * w[i];
}
x_norm = (x_norm * x_norm + (phi / rho) * (phi / rho)).sqrt();
dd_norm = dd_norm + (T::sparse_one() / rho) * (T::sparse_one() / rho);
res2 = phi_bar_new.abs();
if let Some(ref mut history) = residual_history {
history.push(res2);
}
let r1_norm = res2;
let r2_norm = if x_norm > T::sparse_zero() {
alpha_new.abs() * x_norm
} else {
alpha_new.abs()
};
let test1 = r1_norm / (atol + btol * beta);
let test2 = if x_norm > T::sparse_zero() {
alpha_new.abs() / (atol + btol * x_norm)
} else {
alpha_new.abs() / atol
};
let test3 = T::sparse_one() / conlim;
if test1 <= T::sparse_one() {
converged = true;
convergence_reason = "Residual tolerance satisfied".to_string();
break;
}
if test2 <= T::sparse_one() {
converged = true;
convergence_reason = "Solution tolerance satisfied".to_string();
break;
}
let condition_estimate = if dd_norm > T::sparse_zero() {
x_norm / dd_norm.sqrt()
} else {
T::sparse_one()
};
if condition_estimate > conlim {
converged = true;
convergence_reason = "Condition number limit reached".to_string();
break;
}
alpha = alpha_new;
rho_bar = rho_bar_new;
phi_bar = phi_bar_new;
}
if !converged {
convergence_reason = "Maximum iterations reached".to_string();
}
let ax_final = matrix_vector_multiply(matrix, &x.view())?;
let final_residual = b - &ax_final;
let final_residualnorm = l2_norm(&final_residual.view());
let final_solution_norm = l2_norm(&x.view());
let condition_number = if dd_norm > T::sparse_zero() {
x_norm / dd_norm.sqrt()
} else {
T::sparse_one()
};
let standard_errors = if options.calc_var {
Some(compute_standard_errors(matrix, final_residualnorm, n)?)
} else {
None
};
Ok(LSQRResult {
x,
iterations: iter,
residualnorm: final_residualnorm,
solution_norm: final_solution_norm,
condition_number,
converged,
standard_errors,
residual_history,
convergence_reason,
})
}
#[allow(dead_code)]
fn matrix_vector_multiply<T, S>(matrix: &S, x: &ArrayView1<T>) -> SparseResult<Array1<T>>
where
T: Float + SparseElement + Debug + Copy + 'static,
S: SparseArray<T>,
{
let (rows, cols) = matrix.shape();
if x.len() != cols {
return Err(SparseError::DimensionMismatch {
expected: cols,
found: x.len(),
});
}
let mut result = Array1::zeros(rows);
let (row_indices, col_indices, values) = matrix.find();
for (k, (&i, &j)) in row_indices.iter().zip(col_indices.iter()).enumerate() {
result[i] = result[i] + values[k] * x[j];
}
Ok(result)
}
#[allow(dead_code)]
fn matrix_transpose_vector_multiply<T, S>(matrix: &S, x: &ArrayView1<T>) -> SparseResult<Array1<T>>
where
T: Float + SparseElement + Debug + Copy + 'static,
S: SparseArray<T>,
{
let (rows, cols) = matrix.shape();
if x.len() != rows {
return Err(SparseError::DimensionMismatch {
expected: rows,
found: x.len(),
});
}
let mut result = Array1::zeros(cols);
let (row_indices, col_indices, values) = matrix.find();
for (k, (&i, &j)) in row_indices.iter().zip(col_indices.iter()).enumerate() {
result[j] = result[j] + values[k] * x[i];
}
Ok(result)
}
#[allow(dead_code)]
fn l2_norm<T>(x: &ArrayView1<T>) -> T
where
T: Float + SparseElement + Debug + Copy,
{
(x.iter()
.map(|&val| val * val)
.fold(T::sparse_zero(), |a, b| a + b))
.sqrt()
}
#[allow(dead_code)]
fn compute_standard_errors<T, S>(matrix: &S, residualnorm: T, n: usize) -> SparseResult<Array1<T>>
where
T: Float + SparseElement + Debug + Copy + 'static,
S: SparseArray<T>,
{
let (m, _) = matrix.shape();
let variance = if m > n {
residualnorm * residualnorm / T::from(m - n).expect("Operation failed")
} else {
residualnorm * residualnorm
};
let std_err = variance.sqrt();
Ok(Array1::from_elem(n, std_err))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::csr_array::CsrArray;
use approx::assert_relative_eq;
#[test]
fn test_lsqr_square_system() {
let rows = vec![0, 0, 1, 1, 2, 2];
let cols = vec![0, 1, 0, 1, 1, 2];
let data = vec![2.0, -1.0, -1.0, 2.0, -1.0, 2.0];
let matrix =
CsrArray::from_triplets(&rows, &cols, &data, (3, 3), false).expect("Operation failed");
let b = Array1::from_vec(vec![1.0, 0.0, 1.0]);
let result =
lsqr(&matrix, &b.view(), None, LSQROptions::default()).expect("Operation failed");
assert!(result.converged);
let ax = matrix_vector_multiply(&matrix, &result.x.view()).expect("Operation failed");
let residual = &b - &ax;
let residualnorm = l2_norm(&residual.view());
assert!(residualnorm < 1e-6);
}
#[test]
fn test_lsqr_overdetermined_system() {
let rows = vec![0, 0, 1, 1, 2, 2];
let cols = vec![0, 1, 0, 1, 0, 1];
let data = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0];
let matrix =
CsrArray::from_triplets(&rows, &cols, &data, (3, 2), false).expect("Operation failed");
let b = Array1::from_vec(vec![1.0, 2.0, 3.0]);
let result =
lsqr(&matrix, &b.view(), None, LSQROptions::default()).expect("Operation failed");
assert!(result.converged);
assert_eq!(result.x.len(), 2);
assert!(result.residualnorm < 2.0); }
#[test]
fn test_lsqr_diagonal_system() {
let rows = vec![0, 1, 2];
let cols = vec![0, 1, 2];
let data = vec![2.0, 3.0, 4.0];
let matrix =
CsrArray::from_triplets(&rows, &cols, &data, (3, 3), false).expect("Operation failed");
let b = Array1::from_vec(vec![4.0, 9.0, 16.0]);
let result =
lsqr(&matrix, &b.view(), None, LSQROptions::default()).expect("Operation failed");
assert!(result.converged);
assert_relative_eq!(result.x[0], 2.0, epsilon = 1e-6);
assert_relative_eq!(result.x[1], 3.0, epsilon = 1e-6);
assert_relative_eq!(result.x[2], 4.0, epsilon = 1e-6);
}
#[test]
fn test_lsqr_with_initial_guess() {
let rows = vec![0, 1, 2];
let cols = vec![0, 1, 2];
let data = vec![1.0, 1.0, 1.0];
let matrix =
CsrArray::from_triplets(&rows, &cols, &data, (3, 3), false).expect("Operation failed");
let b = Array1::from_vec(vec![5.0, 6.0, 7.0]);
let x0 = Array1::from_vec(vec![4.0, 5.0, 6.0]);
let result = lsqr(&matrix, &b.view(), Some(&x0.view()), LSQROptions::default())
.expect("Operation failed");
assert!(result.converged);
assert!(result.iterations <= 5); }
#[test]
fn test_lsqr_standard_errors() {
let rows = vec![0, 1, 2];
let cols = vec![0, 1, 2];
let data = vec![1.0, 1.0, 1.0];
let matrix =
CsrArray::from_triplets(&rows, &cols, &data, (3, 3), false).expect("Operation failed");
let b = Array1::from_vec(vec![1.0, 1.0, 1.0]);
let options = LSQROptions {
calc_var: true,
..Default::default()
};
let result = lsqr(&matrix, &b.view(), None, options).expect("Operation failed");
assert!(result.converged);
assert!(result.standard_errors.is_some());
let std_errs = result.standard_errors.expect("Operation failed");
assert_eq!(std_errs.len(), 3);
}
}