`linalg.inv` fails for arrays of shape (0, 0)
See original GitHub issueMy 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:
- Created 2 years ago
- Comments:8 (8 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
You can just return
a1.copy()
after the squareness check.Thanks @ilayn, clicking though… found it —It’s de Boor, not Higham, it turns out: https://github.com/scipy/scipy/pull/4672#issuecomment-124718692