diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2015-01-03 17:55:43 -0700 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2015-01-04 08:17:46 -0700 |
commit | ea927d961dfc32b6963aed3b3a10bc51c12543df (patch) | |
tree | 2381b2f1a9613b43a1614a38218f220a5ecd7c92 | |
parent | ad2d26442a4cf39ca378040f56ee928e673ad42a (diff) | |
download | numpy-ea927d961dfc32b6963aed3b3a10bc51c12543df.tar.gz |
BUG: Make ravel function return 1-D arrays for matrix argument.
This is a backward compatibility hack to avoid breaking scipy.sparse
after fixing ravel to respect subtypes. Subtypes are still respected
except in the case of matrices and subclasses of matrices.
-rw-r--r-- | numpy/core/fromnumeric.py | 11 | ||||
-rw-r--r-- | numpy/matrixlib/tests/test_defmatrix.py | 10 |
2 files changed, 13 insertions, 8 deletions
diff --git a/numpy/core/fromnumeric.py b/numpy/core/fromnumeric.py index da61fc187..2a527a4a4 100644 --- a/numpy/core/fromnumeric.py +++ b/numpy/core/fromnumeric.py @@ -1376,8 +1376,10 @@ def ravel(a, order='C'): Returns ------- y : array_like - Array of the same type as `a`, and of shape ``(a.size,)`` - or ``(1, a.size)`` for matrices. + If `a` is a matrix, y is a 1-D ndarray, otherwise y is an array of + the same subtype as `a`. The shape of the returned array is + ``(a.size,)``. Matrices are special cased for backward + compatibility. See Also -------- @@ -1435,7 +1437,10 @@ def ravel(a, order='C'): array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]) """ - return asanyarray(a).ravel(order) + if isinstance(a, np.matrix): + return asarray(a).ravel(order) + else: + return asanyarray(a).ravel(order) def nonzero(a): diff --git a/numpy/matrixlib/tests/test_defmatrix.py b/numpy/matrixlib/tests/test_defmatrix.py index 93843c55c..f3a8e72ca 100644 --- a/numpy/matrixlib/tests/test_defmatrix.py +++ b/numpy/matrixlib/tests/test_defmatrix.py @@ -409,7 +409,7 @@ class TestShape(TestCase): def test_numpy_ravel(self): assert_equal(np.ravel(self.a).shape, (2,)) - assert_equal(np.ravel(self.m).shape, (1, 2)) + assert_equal(np.ravel(self.m).shape, (2,)) def test_member_ravel(self): assert_equal(self.a.ravel().shape, (2,)) @@ -426,10 +426,10 @@ class TestShape(TestCase): assert_equal(np.ravel(x.T), [1, 4, 2, 5, 3, 6]) assert_equal(np.ravel(x.T, order='A'), [1, 2, 3, 4, 5, 6]) x = matrix([[1, 2, 3], [4, 5, 6]]) - assert_equal(np.ravel(x), [[1, 2, 3, 4, 5, 6]]) - assert_equal(np.ravel(x, order='F'), [[1, 4, 2, 5, 3, 6]]) - assert_equal(np.ravel(x.T), [[1, 4, 2, 5, 3, 6]]) - assert_equal(np.ravel(x.T, order='A'), [[1, 2, 3, 4, 5, 6]]) + assert_equal(np.ravel(x), [1, 2, 3, 4, 5, 6]) + assert_equal(np.ravel(x, order='F'), [1, 4, 2, 5, 3, 6]) + assert_equal(np.ravel(x.T), [1, 4, 2, 5, 3, 6]) + assert_equal(np.ravel(x.T, order='A'), [1, 2, 3, 4, 5, 6]) def test_matrix_ravel_order(self): x = matrix([[1, 2, 3], [4, 5, 6]]) |