summaryrefslogtreecommitdiff
path: root/numpy/linalg
diff options
context:
space:
mode:
authorMarten van Kerkwijk <mhvk@astro.utoronto.ca>2017-11-06 10:43:37 -0500
committerMarten van Kerkwijk <mhvk@astro.utoronto.ca>2017-11-06 10:43:37 -0500
commita3c489f1b252b5d4790780261d5d38a0186022b7 (patch)
tree39d4b91df6b2fab73acfd3fc58bab7c4a246c4e5 /numpy/linalg
parentb97f9b0cd6b08e2174fbde6329d16099856e5947 (diff)
downloadnumpy-a3c489f1b252b5d4790780261d5d38a0186022b7.tar.gz
BUG: Ensure lstsq can handle RHS with all sizes.
This fixes a bug in the creation of workspace arrays for a call to `lapack_lite.zgelsd`, which led to segmentation faults when a RHS was passed in that had larger size than the size of the matrix.
Diffstat (limited to 'numpy/linalg')
-rw-r--r--numpy/linalg/linalg.py8
-rw-r--r--numpy/linalg/tests/test_regression.py12
2 files changed, 13 insertions, 7 deletions
diff --git a/numpy/linalg/linalg.py b/numpy/linalg/linalg.py
index 8f2e45fa5..26254903d 100644
--- a/numpy/linalg/linalg.py
+++ b/numpy/linalg/linalg.py
@@ -2021,14 +2021,8 @@ def lstsq(a, b, rcond="warn"):
work = zeros((lwork,), t)
results = lapack_routine(m, n, n_rhs, a, m, bstar, ldb, s, rcond,
0, work, -1, rwork, iwork, 0)
- lwork = int(abs(work[0]))
- rwork = zeros((lwork,), real_t)
- a_real = zeros((m, n), real_t)
- bstar_real = zeros((ldb, n_rhs,), real_t)
- results = lapack_lite.dgelsd(m, n, n_rhs, a_real, m,
- bstar_real, ldb, s, rcond,
- 0, rwork, -1, iwork, 0)
lrwork = int(rwork[0])
+ lwork = int(work[0].real)
work = zeros((lwork,), t)
rwork = zeros((lrwork,), real_t)
results = lapack_routine(m, n, n_rhs, a, m, bstar, ldb, s, rcond,
diff --git a/numpy/linalg/tests/test_regression.py b/numpy/linalg/tests/test_regression.py
index 07d72620b..c11689b3b 100644
--- a/numpy/linalg/tests/test_regression.py
+++ b/numpy/linalg/tests/test_regression.py
@@ -137,6 +137,18 @@ class TestRegression(object):
assert_raises(TypeError, linalg.norm, testmatrix, ord=-2)
assert_raises(ValueError, linalg.norm, testmatrix, ord=3)
+ def test_lstsq_complex_larger_rhs(self):
+ # gh-9891
+ size = 20
+ n_rhs = 70
+ G = np.random.randn(size, size) + 1j * np.random.randn(size, size)
+ u = np.random.randn(size, n_rhs) + 1j * np.random.randn(size, n_rhs)
+ b = G.dot(u)
+ # This should work without segmentation fault.
+ u_lstsq, res, rank, sv = linalg.lstsq(G, b, rcond=None)
+ # check results just in case
+ assert_array_almost_equal(u_lstsq, u)
+
if __name__ == '__main__':
run_module_suite()