Skip to main content

full_piv_lu_matrix_owned

Function full_piv_lu_matrix_owned 

Source
pub fn full_piv_lu_matrix_owned<T>(
    a: Matrix<T>,
) -> Result<FullPivLuMatrixResult<T>, BackendLinalgError>
Expand description

Compute complete-pivoting LU for an owned column-major Matrix.

This is the owned-buffer counterpart of full_piv_lu_matrix. It consumes the matrix so its column-major buffer can be transferred to tenferro without cloning the input before factorization. The returned factors satisfy P * A * Qᵀ = L * U when interpreted as column-major matrices.

§Errors

Returns BackendLinalgError when the input is not square (tenferro reports an incompatible shape), the configured backend rejects the scalar dtype, the complete-pivoting factorization fails, or a factor produced by the backend cannot be converted back to a matrix.

§Examples

use tensor4all_tensorbackend::{from_vec2d, full_piv_lu_matrix_owned, Matrix};

fn matmul(a: &Matrix<f64>, b: &Matrix<f64>) -> Matrix<f64> {
    let mut out = Matrix::zeros(a.nrows(), b.ncols());
    for col in 0..b.ncols() {
        for k in 0..a.ncols() {
            for row in 0..a.nrows() {
                out[[row, col]] += a[[row, k]] * b[[k, col]];
            }
        }
    }
    out
}

fn transpose(a: &Matrix<f64>) -> Matrix<f64> {
    let mut out = Matrix::zeros(a.ncols(), a.nrows());
    for col in 0..a.ncols() {
        for row in 0..a.nrows() {
            out[[col, row]] = a[[row, col]];
        }
    }
    out
}

let matrix = from_vec2d(vec![vec![2.0_f64, 1.0], vec![1.0, 2.0]]);
let factors = full_piv_lu_matrix_owned(matrix.clone()).unwrap();
let lhs = matmul(&factors.p, &matmul(&matrix, &transpose(&factors.q)));
let rhs = matmul(&factors.l, &factors.u);
for row in 0..2 {
    for col in 0..2 {
        assert!((lhs[[row, col]] - rhs[[row, col]]).abs() < 1.0e-12);
    }
}