question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`linalg.inv` fails for arrays of shape (0, 0)

See original GitHub issue

My issue is about the behavior of linalg.inv for arrays of shape (0, 0). I would like to move from numpy.linalg to scipy.linalg, but with scipy inverting this kind of matrices is not possible. Normally my functions runs with (M, M) matrices with M > 0 but I would like to handle the edge case as well where M = 0. I can work around this issue by checking the shape beforehand, but it seams to me like a bug with scipy’s linalg.inv that it can’t handle this case.

Reproducing code example:

import numpy
import scipy
z = numpy.zeros((0, 0))
numpy.linalg.inv(z) # --> array([], shape=(0, 0), dtype=float64)
scipy.linalg.inv(z) # --> ValueError

Error message:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~/test.py in <module>
      3 z = numpy.zeros((0, 0))
      4 numpy.linalg.inv(z) # --> array([], shape=(0, 0), dtype=float64)
----> 5 scipy.linalg.inv(z) # --> ValueError

~/anaconda3/lib/python3.8/site-packages/scipy/linalg/basic.py in inv(a, overwrite_a, check_finite)
    963         raise LinAlgError("singular matrix")
    964     if info < 0:
--> 965         raise ValueError('illegal value in %d-th argument of internal '
    966                          'getrf|getri' % -info)
    967     return inv_a

ValueError: illegal value in 4-th argument of internal getrf|getri

Scipy/Numpy/Python version information:

1.6.2 1.20.2 sys.version_info(major=3, minor=8, micro=5, releaselevel='final', serial=0)

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
ilayncommented, Jul 14, 2021

You can just return a1.copy() after the squareness check.

0reactions
ev-brcommented, Jul 14, 2021

Thanks @ilayn, clicking though… found it —It’s de Boor, not Higham, it turns out: https://github.com/scipy/scipy/pull/4672#issuecomment-124718692

Read more comments on GitHub >

github_iconTop Results From Across the Web

numpy.linalg.inv — NumPy v1.24 Manual
Given a square matrix a, return the matrix ainv satisfying dot(a, ainv) = dot(ainv, a) = eye(a.shape[0]) . Parameters: a(…, M, M) array_like....
Read more >
np.linalg.inv() giving unexpected results - Stack Overflow
linalg.det(p1) ? I suspect p1 is close to singular, the actual array more so than the recreated one. – hpaulj.
Read more >
linalg.py - Google Git
If None (default), no reordering is done. Returns. -------. x : array ...
Read more >
Python: module numpy.linalg.linalg - PyOpenGL
L = np.linalg.cholesky(A) >>> L array([[ 1.+0.j, 0.+0.j], [ 0.+2.j, 1.+0.j]]) ... of the columns may be dependent, although roundoff error may
Read more >
sarkarpratyayan/linear-algebra-numpy - Jovian
As the determinant of the array arr2 is 0 the computation of inverse of the array arr2 raised error and failed. Note -->...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found