8000 svd requires positive-size matrices · Issue #5419 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

svd requires positive-size matrices #5419

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
argriffing opened this issue Jan 3, 2015 · 13 comments
Closed

svd requires positive-size matrices #5419

argriffing opened this issue Jan 3, 2015 · 13 comments

Comments

@argriffing
Copy link
Contributor

If I'm using svd to help compute the norm of a matrix by looking at its singular values, then I want a size-0 matrix to have a length-0 sequence of singular values whose aggregate function (e.g. max, sum) will be 0. But numpy explicitly forbids empty inputs with _assertNoEmpty2d(a).

@njsmith
Copy link
Member
njsmith commented Jan 3, 2015

Seems reasonable, though if full_matrices=True then one has to decide what
to do with the n x n 'u' matrix if the input is n x 0. I think just
returning np.eye(n) would satisfy all the SVD invariants?

(BTW, max([]) is an error.)

On Sat, Jan 3, 2015 at 5:37 PM, argriffing notifications@github.com wrote:

If I'm using svd to help compute the norm of a matrix by looking at its
singular values, then I want a size-0 matrix to have a length-0 sequence of
singular values whose aggregate function (e.g. max, sum) will be 0. But
numpy explicitly forbids empty inputs with _assertNoEmpty2d(a).


Reply to this email directly or view it on GitHub
#5419.

Nathaniel J. Smith
Postdoctoral researcher - Informatics - University of Edinburgh
http://vorpus.org

@argriffing
Copy link
Contributor Author

a length-0 sequence of singular values whose aggregate function (e.g. max, sum) will be 0.

(BTW, max([]) is an error.)

Yep, just noticed that.

@seberg
Copy link
Member
seberg commented Jan 4, 2015

We explicitly forbid it mostly because 1. our gufunc stuff doesn't handle it (which is easy to fix, I got the PR ready mostly) and 2. at least not all lapacks can handle it. If the math is clear about the definition I think we can just special case it.

@maniteja123
Copy link
Contributor

Just wanted to clarify regarding np.max and np.min functions,

>>>np.max(np.array([]))
ValueError: zero-size array to reduction operation maximum which has no identity

Is this expected behavior ? because this is a cause for some of the exceptions mentioned in #5420 .

@seberg
Copy link
Member
seberg commented Jan 4, 2015

Yes, it is expected. There is no reasonable definition of a maximum from an empty sequence. You could argue it is -inf, but -inf is a rather special beast in any case.

@maniteja123
Copy link
Contributor

@seberg , Thanks. Yeah, there isn't a proper definition, but raising an exception means the other codes which use these utility functions, like np.linalg.norm or np.clip need to take care of the edge cases, or is it okay to raise exceptions. Whereas np.clip is designed to handle these cases, as mentioned in gh-5420, np.linalg.norm fails to handle all of them.

@seberg
Copy link
Member
seberg commented Jan 4, 2015

Sure. We are forcing our users to think about what is correct and explicitly handle it when we cannot say it for sure, and we have to do it ourselves :). The fact is, very old numpy versions or even numeric didn't have a very clear handling of these cases I think, so in some places the special cases may still be missing or even be incorrect/weird, though I think numpy is mostly clean by now when it comes to empty arrays.

@maniteja123
Copy link
Contributor

@seberg , thanks for the response :) Yeah, numpy is clean in most of the places when it comes to empty arrays. Just wanted to know if the exception in np.linalg.norm was intentional or unreported, because neither the documentation or the references mentioned about the vector or matrix norms don't talk about either negative order or empty arrays/matrices. I understand that old numpy code might need for special cases to be taken care of.

@argriffing
Copy link
Contributor Author

Sorry for having parenthetically mentioned max in my first comment in this issue. It seems to have quite shifted the topic of discussion...

@maniteja123
Copy link
Contributor

@argriffing Sorry, It's me who has diverted the discussion. I was just querying about the behavior of different functions on zero sized arrays, which I found when going through gh-5420

@seberg
Copy link
Member
seberg commented Jan 5, 2015

So just to note. If this interests anyone, gh-3861 was the PR I mentioned before, and I think what is mostly needed for it is to clean up all those empty array cases in linalg (since linalg is the biggest user of gufuncs).

@eric-wieser
Copy link
Member

Related: #8654

@eric-wieser
Copy link
Member

Fixed in #11424

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

No branches or pull requests

5 participants
0