Skip to content

ENH: Add a pinvh function to linalg. #12665

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

Closed
wants to merge 2 commits into from

Conversation

jordan-melendez
Copy link

This is a faster version of linalg.pinv for Hermitian matrices. It is like the Scipy version of pinvh, but supports stacked matrices like Numpy's pinv.

@eric-wieser
Copy link
Member

eric-wieser commented Jan 4, 2019

I think you should perhaps instead add a hermitian=False argument to pinv, like we do here (more discussion in #9436):

def matrix_rank(M, tol=None, hermitian=False):

@rgommers
Copy link
Member

rgommers commented Jan 5, 2019

Is there a reason to put this in NumPy rather than extend the SciPy version? In general we'd like to keep the two in sync for features we have already in both, and put new features in SciPy unless there's a really good reason to put things in NumPy.

Note that incompatible changes will also break (or not have) Numba support for those new features. Numba uses scipy.linalg.cython_blas/lapack when it sees numpy.linalg functions, so anything added that's not in SciPy will not be supported.

@charris charris changed the title Pinvh ENH: Add a pinvh function to linalg. Jan 5, 2019
@jordan-melendez
Copy link
Author

@eric-wieser that could work! I was just trying to mirror the format of Scipy here.

@rgommers My reasoning was that Scipy uses its own internal functions, like eigh, which only operate on (M,M) shaped arrays. From what I can tell, "2d matrices only" seems to be a common theme of Scipy's linalg library, whereas Numpy provides a lot of the same functionality, but allows matrices to be stacked. I assume Scipy has a good reason for this restriction, and I don't entirely know what it is or how I would modify it if I wanted to.

In regards to the Numba issue, what would it take to add that support? I'm not super familiar with the way it interacts with Numpy/Scipy and using BLAS/LAPACK directly.

Thanks!

@eric-wieser
Copy link
Member

eric-wieser commented Jan 8, 2019

@rgommers: I think the fact we allowed a hermitian overload for matrix_rank (#8557) suggests it would be reasonable to add one for the rest of linalg, at least in places where no new lapack functions are needed.

s, u = eigh(a, UPLO='L')

# discard small singular values
cutoff = rcond[..., newaxis] * amax(s, axis=-1, keepdims=True)
Copy link
Member

@eric-wieser eric-wieser Jan 8, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is incorrect - eigh can return negative values of s, hence the abs in the other PR

@rgommers
Copy link
Member

rgommers commented Jan 9, 2019

I think the fact we allowed a hermitian overload for matrix_rank (#8557) suggests it would be reasonable to add one for the rest of linalg, at least in places where no new lapack functions are needed.

That's a good argument. Fair enough.

My reasoning was that Scipy uses its own internal functions, like eigh, which only operate on (M,M) shaped arrays. From what I can tell, "2d matrices only" seems to be a common theme of Scipy's linalg library, whereas Numpy provides a lot of the same functionality, but allows matrices to be stacked. I assume Scipy has a good reason for this restriction, and I don't entirely know what it is or how I would modify it if I wanted to.

I don't think there's a lot of thought put into that, it just evolved. In general, we do want APIs to match between NumPy and SciPy, and NumPy to provide the essentials and SciPy to be a superset of what NumPy has.

In regards to the Numba issue, what would it take to add that support? I'm not super familiar with the way it interacts with Numpy/Scipy and using BLAS/LAPACK directly.

I could read the Numba code base to find the answer, but better to ask the Numba devs I think. @seibert any thoughts/comments on how Numba deals with an evolving numpy.linalg API here? Would it be useful to at least send Numba PRs that document that the new feature is not (yet) supported at the time we merge the new feature here?

@jordan-melendez
Copy link
Author

@rgommers and @eric-wieser is this PR still worth pursuing given #12693?

@eric-wieser
Copy link
Member

Not now that #12693 is merged. Thanks for providing the catalyst for that PR!

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

Successfully merging this pull request may close these issues.

4 participants