-
Notifications
You must be signed in to change notification settings - Fork 79
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
Add pinv function (Moore-Penrose Pseudo-inverse) #299
base: master
Are you sure you want to change the base?
Conversation
I haven't had a chance to review the code, but I thought I'd respond to this portion of your comment:
Probably the simplest approach is to take advantage of the properties of the rank of matrix products and the fact that matrices generated with use ndarray::prelude::*;
use ndarray::linalg::general_mat_mul;
use ndarray_linalg::{Scalar, generate::random};
/// Returns an array with the specified shape and rank.
///
/// # Panics
///
/// Panics if the rank is impossible to achieve for the given shape, i.e. if
/// it's less than the minimum of the number of rows and number of columns.
fn random_with_rank<A, Sh>(shape: Sh, rank: usize) -> Array2<A>
where
A: Scalar,
Sh: ShapeBuilder<Dim = Ix2>,
{
let mut out = Array2::zeros(shape);
assert!(rank <= usize::min(out.nrows(), out.ncols()));
for _ in 0..10 {
let left: Array2<A> = random([out.nrows(), rank]);
let right: Array2<A> = random([rank, out.ncols()]);
general_mat_mul(A::one(), &left, &right, A::zero(), &mut out);
if let Ok(out_rank) = out.rank() {
if out_rank == rank {
return out;
}
}
}
unreachable!("Failed to generate random matrix of desired rank within 10 tries. This is very unlikely.");
} This implementation uses |
Codecov Report
@@ Coverage Diff @@
## master #299 +/- ##
==========================================
+ Coverage 89.01% 89.19% +0.17%
==========================================
Files 71 75 +4
Lines 3578 3656 +78
==========================================
+ Hits 3185 3261 +76
- Misses 393 395 +2
Continue to review full report at Codecov.
|
@emiddleton, I had the same thought, but you beat me to it! I wrote fully-tested implementation here, but had not yet covered the in-place use case. The main difference between our implementations is that my algorithm attempts QR decomposition first and then falls back to singular value decomposition. My reasoning is that 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 What are your thoughts? |
This is my work in progress adding Moore-Penrose Pseudo-inverse of a Matrices #292. I have added all the tests suggested in @jturner314 #292 (comment) but it still needs more documentation and the tests could do with some cleanup. I wrote some functions for creating various ranked matrices but I think there might be a better way to do this. I added a rank function to help create random matrices of various ranks.