rust-ndarray / ndarray-linalg

Linear algebra package for rust-ndarray using LAPACK binding
Other
376 stars 74 forks source link

Better in-place LU factorization #316

Open SoAsEr opened 2 years ago

SoAsEr commented 2 years ago

Currently the method factorize_into still allocates a vector for the pivots. This seems normal, but I would like an api where there is no allocation at all. Here is one possibility

impl LuFactorized {
    //updates factorization. Will resize (and therefore allocate) if neccessary.
    fn factorize(&mut self, array: &ArrayBase)
}

The other (in my opinion even more useful) api is something like this:

fn factorize(array: ArrayBase, pivot_buffer: Option<Pivot>)  -> LuFactorized

impl LuFactorized {
    fn release(self) -> (ArrayBase, Pivot)
} 

The second api is useful for the following use case

{
    let mut repeatedly_used_view = get_mut_view();
    let mut pivot_buffer : Option<Pivot> = None;
    loop {
        fill_with_info(&mut repeatedly_used_view)
        let lu_factorization=factorize(repeatedly_used_view, pivot_buffer)

        // use lu_factorization to solve stuff
        //...

        if converged {
            break;
        }

        let pair = lu_factorization.release();
        repeatedly_used_view = pair.0;
        pivot_buffer  = Some(pair.1);
    }
}
SoAsEr commented 2 years ago

related: https://github.com/rust-ndarray/ndarray-linalg/issues/300

Basically a factorize_inplace that also takes in a Pivot