Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pseudoinverse #304

Closed
benkay86 opened this issue Jul 29, 2021 · 1 comment
Closed

pseudoinverse #304

benkay86 opened this issue Jul 29, 2021 · 1 comment

Comments

@benkay86
Copy link
Contributor

Should ndarray-linalg have a dedicated algorithm for computing the Moore-Penrose pseudoinverse? Python's numpy has pinv(). LAPACK does not have a built-in pinv(), although it has been much discussed. Writing a turnkey pseudoinverse method is challenging because the optimal implementation depends on the use case. Personally, I think having some reasonably-optimal pinv() in ndarray-linalg would make the crate more user-friendly.

Currently it is possible to compute the pseudoinverse with ndarray-linalg by solving into an identity matrix, which calls LAPACK's dgesvd().

use ndarray::{Array, array};
use ndarray_linalg::{LeastSquaresResult, LeastSquaresSvdInto};
let x = array![[1., 7.], [4., 2.], [3., 8.]]; // skinny matrix
let LeastSquaresResult { solution: x_pinv, .. } = x.least_squares_into(Array::<f64, _>::eye(n))?;
println!("The pseudoinverse is:\n{:?}", x_pinv);

I propose a more optimal algorithm implemented here. The most common use case for computing the Moore-Penrose pseudoinverse is performing least-squares regression, where we would like to know the pseudoinverse of the design matrix x. Most of the time x will have full column rank, in which case we can compute the pseudoinverse using faster QR decomposition. If the diagonal elements of R are ill-conditioned we can fall back to singular value decomposition (using the newer dgesdd() instead of dgesvd()). We discover the matrix rank for free. We can have specialized methods for cases where the caller knows something about the matrix rank in advance. While there is surely room for further optimization, this algorithm already benchmarks 2-4 times faster than the code above.

Are the maintainers of ndarray-linalg interested in a pull request adding extension traits for pinv()?

@benkay86
Copy link
Contributor Author

Apologies, Github's search function failed me. Closing in favor of #299.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant