8000 linalg.det throws LinAlgError for 0-by-0 matrices · Issue #8212 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

linalg.det throws LinAlgError for 0-by-0 matrices #8212

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
mwallerb opened this issue Oct 26, 2016 · 12 comments
Closed

linalg.det throws LinAlgError for 0-by-0 matrices #8212

mwallerb opened this issue Oct 26, 2016 · 12 comments

Comments

@mwallerb
Copy link

Since LAPACK functions cannot handle 0-by-0 matrices, numpy must explicitly handle those cases. However, while in newer versions of numpy, the inverse is computed correctly, the determinant raises LinAlgError:

>>> A = numpy.empty((0, 0))
>>> numpy.linalg.inv(A)
array([], shape=(0, 0), dtype=float64)
>>> numpy.linalg.det(A)
LinAlgError: Arrays cannot be empty

Since the determinant of a zero-by-zero matrix is well-defined, a LinAlgError is IMHO wrong, since it indicates a numerics problem rather than the purely technical fact that the backend cannot handle this.

Thus, expected result:

>>> numpy.linalg.det(A)
1.0

Numpy version: 1.11.0 (also present in git master)
Python version: 2.7.12

@mhassell
Copy link
mhassell commented Nov 5, 2016

Hi! I'll take a look and see what I can do to fix this. But just a heads up, this is my first time fixing a bug in numpy, so I might not be super quick.

@mhassell
Copy link
mhassell commented Nov 5, 2016

Because of the ability to compute determinants of a stack of arrays this seems to get a little...messy.

>>> a = np.array([ [[1, 2], [3, 4]], [[1, 2], [2, 1]], [[1, 3], [3, 1]] ])
>>> a.shape
(3, 2, 2)
>>> np.linalg.det(a)
array([-2., -3., -8.])

Now, my thought is to do the following:

Loop over a and flag any arrays in the stack that are empty.

Keep track of the indices where there are empty arrays for later.
Delete the empty arrays, pass the nonempty arrays to _umath_linalg.det, read back the determinants, and then fill in the missing locations with ones where there were empty matrices.< 8000 /p>

Does this seem like it will address the issue?

@pv
Copy link
Member
pv commented Nov 5, 2016 via email

@mhassell
Copy link
mhassell commented Nov 5, 2016

Hm, so if one member of a stacked array is empty, they are then all empty? This doesn't seem to be explicitly forbidden by numpy:

>>> a = np.array( [ [[1, 2], [3,4]], [[]], [[]] ])
>>> a.shape
(3,)

@eric-wieser
Copy link
Member

@mhassell: That's a dtype=object, ndim=1 array though, which np.linalg.det (rightly) rejects

@mwallerb
Copy link
Author

@mhassell I think you may draw some inspiration from the code of linalg.inv, since it handles 0-by-0 just fine.

@mhassell
Copy link

Sure, I'll check it out and try to fix this. Thanks for the pointer.

@Don86
Copy link
Don86 commented Dec 12, 2016

Hi, I'd like to know if this is still being worked on? I'd like to try my hand at it otherwise.

@mhassell
Copy link

Hi Don86, I've gotten a bit swamped with other stuff, so please do have a go at it.

@eric-wieser
Copy link
Member

I've got this one, but I think there are a tonne more cases in linalg that ought to work for 0d

@ghost
Copy link
ghost commented Feb 20, 2017

I would like to work on this one, @eric-wieser can you point out some of the cases that you were talking about in the previous comment?

@eric-wieser
Copy link
Member
eric-wieser commented Feb 20, 2017

@ashwinpathak20: This is already well in process (#8368), but has opened a can of worms:

Update: mostly fixed!

eric-wieser added a commit to eric-wieser/numpy that referenced this issue Mar 4, 2017
Fixes numpy#8212

det: return ones of the right shape
slogdet: return sign=ones, log=zeros of the right shape
pinv, eigvals(h?), eig(h?): return empty array(s?) of the right shape

svd & qr: not implemented, due to complex return value rules
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

6 participants
0