mod colmajor;
mod reduce;
use crate::domain::real::RealScalar;
use leto::{Array2, ArrayView2, LetoError, Result};
#[derive(Debug, Clone)]
pub struct BidiagonalDecomposition<T> {
u: Array2<T>,
b: Array2<T>,
v: Array2<T>,
}
impl<T: RealScalar> BidiagonalDecomposition<T> {
#[must_use]
pub fn u(&self) -> &Array2<T> {
&self.u
}
#[must_use]
pub fn b(&self) -> &Array2<T> {
&self.b
}
#[must_use]
pub fn v(&self) -> &Array2<T> {
&self.v
}
}
pub fn bidiagonalize<T: RealScalar>(
matrix: &ArrayView2<'_, T>,
) -> Result<BidiagonalDecomposition<T>> {
let [m, n] = matrix.shape();
if m < n {
return Err(LetoError::ShapeMismatch {
lhs: vec![m, n],
rhs: vec![n, n],
});
}
let (u, b, v) = reduce::reduce_to_bidiagonal(matrix, m, n)?;
Ok(BidiagonalDecomposition {
u: Array2::from_shape_vec([m, m], u).expect("U shape matches storage"),
b: Array2::from_shape_vec([m, n], b).expect("B shape matches storage"),
v: Array2::from_shape_vec([n, n], v).expect("V shape matches storage"),
})
}
pub(crate) fn bidiagonal_diag_colmajor<T: RealScalar>(
matrix: &ArrayView2<'_, T>,
) -> Result<(Vec<T>, Vec<T>)> {
let [m, n] = matrix.shape();
if m < n {
return Err(LetoError::ShapeMismatch {
lhs: vec![m, n],
rhs: vec![n, n],
});
}
debug_assert!(
if let Some(slice) = matrix.as_slice() {
slice.iter().all(|x| x.is_finite())
} else {
matrix.iter().all(|x| x.is_finite())
},
"bidiagonal_diag_colmajor precondition: caller (validate_input) guarantees finite entries"
);
Ok(colmajor::reduce_values(matrix))
}