ndarray-linalg icon indicating copy to clipboard operation
ndarray-linalg copied to clipboard

Usage of ndarray-linalg

Open Yatekii opened this issue 6 years ago • 7 comments

Hey folks!

I am trying to use ndarray-linalg to calculate an orthonormal basis and an inverse. Sadly it is 100% non-obvious to how to achieve this.

Is there maybe a guide how to use this lib somewhere? Because most of it's functionality seems to be implemented in non-convential ways (compare to numpy, matlab, etc.), even tho that might be me missunderstanding.

Best, Yatekii

Yatekii avatar Dec 15 '19 19:12 Yatekii

Hey Yatekii!

Could you share the NumPy equivalent of what you are trying to implement? So that we can provide a translation 😄

LukeMathWalker avatar Dec 15 '19 19:12 LukeMathWalker

Oh, silly me, ofc:

sp.linalg.orth(...);
sp.linalg.inv(...);

Basically it's those two for now.

Is there an active chat or the like where you folks hang out? I tried discord and gitter but it seems dead. And unfortunately I missed your workshop at RustFest :(

Yatekii avatar Dec 15 '19 21:12 Yatekii

Communication about ndarray, ndarray-linalg and ndarray-stats happens on GitHub issues and PRs more than anything else, so you are in the right place. I am in that Gitter channel as well, but for questions like this one I think it makes more sense to tackle them in an issue so that the answer is available to everybody for the future 😄

LukeMathWalker avatar Dec 16 '19 07:12 LukeMathWalker

The inverse computation looks like this:

use ndarray::array;
use ndarray_linalg::Inverse;

fn inverse() {
    let a = array![
        [1., 3.],
        [4., 2.]
    ];
    let inverse = a.inv().expect("Failed to invert matrix");
    println!("Inverse matrix: {:?}", inverse);
}

I am afraid we don't offer a Rust equivalent of orth right now - can you confirm @termoshtt?

LukeMathWalker avatar Dec 16 '19 07:12 LukeMathWalker

Communication about ndarray, ndarray-linalg and ndarray-stats happens on GitHub issues and PRs more than anything else, so you are in the right place. I am in that Gitter channel as well, but for questions like this one I think it makes more sense to tackle them in an issue so that the answer is available to everybody for the future smile

Oh, I see. I just like chats way more, as you don't loose context all the time as is with GH issues. But I understand the publicity argument :)

Yatekii avatar Dec 16 '19 15:12 Yatekii

The inverse computation looks like this:

use ndarray::array;
use ndarray_linalg::Inverse;

fn inverse() {
    let a = array![
        [1., 3.],
        [4., 2.]
    ];
    let inverse = a.inv().expect("Failed to invert matrix");
    println!("Inverse matrix: {:?}", inverse);
}

I am afraid we don't offer a Rust equivalent of orth right now - can you confirm @termoshtt?

Ok, so this sounds easy. Thank you! I now also see why I couldn't figure this: It's not implemented under Implementors but under Implementations on Foreign Types :/ shame on you rustdoc, why would that be treated differently :D I must have been tired looking at this :/

This https://docs.rs/ndarray-linalg/0.12.0/ndarray_linalg/krylov/trait.Orthogonalizer.html seems to be somewhat of what orth() does. I wish I was more versed with linalg. Because I feel like what I really wanna use is a complete solver instead of bare calls to inv() and orth() as in the numpy code I am translating (this is the code with the specific location fyi: https://github.com/ggventurini/python-deltasigma/blob/master/deltasigma/_simulateDSM_scipy_blas.pyx#L143), but I just don't know enought to read the forumlas with sparse comments like I would read some analysis equations or the likes.

Yatekii avatar Dec 16 '19 15:12 Yatekii

I would have to play around with it a little, but you could probably use Orthogonalizer to do something equivalent to what orth is doing: you just need to append all the columns in A and then call the get_q method.

LukeMathWalker avatar Dec 17 '19 08:12 LukeMathWalker