10000 ENH: Allow use of svd on empty arrays by eric-wieser · Pull Request #11424 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

ENH: Allow use of svd on empty arrays #11424

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

Merged
merged 1 commit into from
Jun 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion numpy/linalg/linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -1527,7 +1527,6 @@ def svd(a, full_matrices=True, compute_uv=True):

"""
a, wrap = _makearray(a)
_assertNoEmpty2d(a)
_assertRankAtLeast2(a)
t, result_t = _commonType(a)

Expand Down Expand Up @@ -1644,6 +1643,7 @@ def cond(x, p=None):

"""
x = asarray(x) # in case we have a matrix
_assertNoEmpty2d(x)
if p is None or p == 2 or p == -2:
s = svd(x, compute_uv=False)
with errstate(all='ignore'):
Expand Down
26 changes: 13 additions & 13 deletions numpy/linalg/tests/test_linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,10 +644,6 @@ class ArraySubclass(np.ndarray):
class SVDCases(LinalgSquareTestCase, LinalgGeneralizedSquareTestCase):

def do(self, a, b, tags):
if 'size-0' in tags:
assert_raises(LinAlgError, linalg.svd, a, 0)
return

u, s, vt = linalg.svd(a, 0)
assert_allclose(a, dot_generalized(np.asarray(u) * np.asarray(s)[..., None, :],
np.asarray(vt)),
Expand All @@ -670,15 +666,19 @@ def check(dtype):
for dtype in [single, double, csingle, cdouble]:
check(dtype)

def test_0_size(self):
# These raise errors currently
# (which does not mean that it may not make sense)
a = np.zeros((0, 0), dtype=np.complex64)
assert_raises(linalg.LinAlgError, linalg.svd, a)
a = np.zeros((0, 1), dtype=np.complex64)
assert_raises(linalg.LinAlgError, linalg.svd, a)
a = np.zeros((1, 0), dtype=np.complex64)
assert_raises(linalg.LinAlgError, linalg.svd, a)
def test_empty_identity(self):
""" Empty input should put an identity matrix in u or vh """
x = np.empty((4, 0))
u, s, vh = linalg.svd(x, compute_uv=True)
assert_equal(u.shape, (4, 4))
assert_equal(vh.shape, (0, 0))
assert_equal(u, np.eye(4))

x = np.empty((0, 4))
u, s, vh = linalg.svd(x, compute_uv=True)
assert_equal(u.shape, (0, 0))
assert_equal(vh.shape, (4, 4))
assert_equal(vh, np.eye(4))


class CondCases(LinalgSquareTestCase, LinalgGeneralizedSquareTestCase):
Expand Down
14 changes: 11 additions & 3 deletions numpy/linalg/umath_linalg.c.src
Original file line number Diff line number Diff line change
Expand Up @@ -2735,19 +2735,18 @@ static NPY_INLINE void
(fortran_int)dimensions[0],
(fortran_int)dimensions[1])) {
LINEARIZE_DATA_t a_in, u_out, s_out, v_out;
fortran_int min_m_n = params.M < params.N ? params.M : params.N;

init_linearize_data(&a_in, params.N, params.M, steps[1], steps[0]);
if ('N' == params.JOBZ) {
/* only the singular values are wanted */
fortran_int min_m_n = params.M < params.N? params.M : params.N;
init_linearize_data(&s_out, 1, min_m_n, 0, steps[2]);
} else {
fortran_int u_columns, v_rows;
fortran_int min_m_n = params.M < params.N? params.M : params.N;
if ('S' == params.JOBZ) {
u_columns = min_m_n;
v_rows = min_m_n;
} else {
} else { /* JOBZ == 'A' */
u_columns = params.M;
v_rows = params.N;
}
Expand All @@ -2771,6 +2770,15 @@ static NPY_INLINE void
if ('N' == params.JOBZ) {
delinearize_@REALTYPE@_matrix(args[1], params.S, &s_out);
} else {
if ('A' == params.JOBZ && min_m_n == 0) {
/* Lapack has betrayed us and left these uninitialized,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: comment should start on the next line.

* so produce an identity matrix for whichever of u
* and v is not empty.
*/
identity_@TYPE@_matrix(params.U, params.M);
identity_@TYPE@_matrix(params.VT, params.N);
}

delinearize_@TYPE@_matrix(args[1], params.U, &u_out);
delinearize_@REALTYPE@_matrix(args[2], params.S, &s_out);
delinearize_@TYPE@_matrix(args[3], params.VT, &v_out);
Expand Down
0