summaryrefslogtreecommitdiff
path: root/numpy/linalg/linalg.py
diff options
context:
space:
mode:
authorJeremy Chen <convexset@gmail.com>2018-08-01 14:49:40 +0800
committerJeremy Chen <convexset@gmail.com>2018-08-03 00:18:34 +0800
commit45d8c5d1562007492c459f290e16cbbf99c72e1c (patch)
treede13615d09cdc1d76ba76161e7db160b636ba37d /numpy/linalg/linalg.py
parent6105281cf245c5713660245a0c87ae00e85aec23 (diff)
downloadnumpy-45d8c5d1562007492c459f290e16cbbf99c72e1c.tar.gz
ENH: support for empty matrices in linalg.lstsq
Diffstat (limited to 'numpy/linalg/linalg.py')
-rw-r--r--numpy/linalg/linalg.py10
1 files changed, 9 insertions, 1 deletions
diff --git a/numpy/linalg/linalg.py b/numpy/linalg/linalg.py
index c3b76ada7..3bf26d4fc 100644
--- a/numpy/linalg/linalg.py
+++ b/numpy/linalg/linalg.py
@@ -2115,7 +2115,6 @@ def lstsq(a, b, rcond="warn"):
if is_1d:
b = b[:, newaxis]
_assertRank2(a, b)
- _assertNoEmpty2d(a, b) # TODO: relax this constraint
m, n = a.shape[-2:]
m2, n_rhs = b.shape[-2:]
if m != m2:
@@ -2146,7 +2145,16 @@ def lstsq(a, b, rcond="warn"):
signature = 'DDd->Ddid' if isComplexType(t) else 'ddd->ddid'
extobj = get_linalg_error_extobj(_raise_linalgerror_lstsq)
+ if n_rhs == 0:
+ # lapack can't handle n_rhs = 0 - so allocate the array one larger in that axis
+ b = zeros(b.shape[:-2] + (m, n_rhs + 1), dtype=b.dtype)
x, resids, rank, s = gufunc(a, b, rcond, signature=signature, extobj=extobj)
+ if m == 0:
+ x[...] = 0
+ if n_rhs == 0:
+ # remove the item we added
+ x = x[..., :n_rhs]
+ resids = resids[..., :n_rhs]
# remove the axis we added
if is_1d: