summaryrefslogtreecommitdiff
path: root/numpy/matrixlib
diff options
context:
space:
mode:
Diffstat (limited to 'numpy/matrixlib')
-rw-r--r--numpy/matrixlib/defmatrix.py40
-rw-r--r--numpy/matrixlib/tests/test_defmatrix.py47
2 files changed, 76 insertions, 11 deletions
diff --git a/numpy/matrixlib/defmatrix.py b/numpy/matrixlib/defmatrix.py
index 2d785e213..51903e258 100644
--- a/numpy/matrixlib/defmatrix.py
+++ b/numpy/matrixlib/defmatrix.py
@@ -927,24 +927,42 @@ class matrix(N.ndarray):
return self.__array__().ravel()
- def ravel(self):
+ def ravel(self, order='C'):
"""
- Return a flattened array.
+ Return a flattened matrix.
- Refer to `numpy.ravel` for full documentation.
+ Refer to `numpy.ravel` for more documentation.
- See Also
- --------
- numpy.ravel : equivalent function
+ Parameters
+ ----------
+ order : {'C', 'F', 'A', 'K'}, optional
+ The elements of `m` are read using this index order. 'C' means to
+ index the elements in C-like order, with the last axis index
+ changing fastest, back to the first axis index changing slowest.
+ 'F' means to index the elements in Fortran-like index order, with
+ the first index changing fastest, and the last index changing
+ slowest. Note that the 'C' and 'F' options take no account of the
+ memory layout of the underlying array, and only refer to the order
+ of axis indexing. 'A' means to read the elements in Fortran-like
+ index order if `m` is Fortran *contiguous* in memory, C-like order
+ otherwise. 'K' means to read the elements in the order they occur
+ in memory, except for reversing the data when strides are negative.
+ By default, 'C' index order is used.
- ndarray.flat : a flat iterator on the array.
+ Returns
+ -------
+ ret : matrix
+ A copy of the matrix, flattened to a `(1, N)` matrix where `N`
+ is the number of elements in the original matrix.
- Notes
- -----
- This returns a matrix.
+ See Also
+ --------
+ matrix.flatten : returns a similar output matrix but always a copy
+ matrix.flat : a flat iterator on the array.
+ numpy.ravel : related function which returns an ndarray
"""
- return core.multiarray.ndarray.ravel(self)
+ return core.multiarray.ndarray.ravel(self, order=order)
def getT(self):
diff --git a/numpy/matrixlib/tests/test_defmatrix.py b/numpy/matrixlib/tests/test_defmatrix.py
index a06a564aa..d2a89bd51 100644
--- a/numpy/matrixlib/tests/test_defmatrix.py
+++ b/numpy/matrixlib/tests/test_defmatrix.py
@@ -386,6 +386,7 @@ class TestNewScalarIndexing(TestCase):
assert_array_equal(x[:, [1, 0]], x[:, ::-1])
assert_array_equal(x[[2, 1, 0],:], x[::-1,:])
+
class TestPower(TestCase):
def test_returntype(self):
a = array([[0, 1], [0, 0]])
@@ -396,5 +397,51 @@ class TestPower(TestCase):
def test_list(self):
assert_array_equal(matrix_power([[0, 1], [0, 0]], 2), [[0, 0], [0, 0]])
+
+class TestShape(TestCase):
+ def setUp(self):
+ self.a = array([[1], [2]])
+ self.m = matrix([[1], [2]])
+
+ def test_shape(self):
+ assert_equal(self.a.shape, (2, 1))
+ assert_equal(self.m.shape, (2, 1))
+
+ def test_numpy_ravel(self):
+ assert_equal(np.ravel(self.a).shape, (2,))
+ assert_equal(np.ravel(self.m).shape, (2,))
+
+ def test_member_ravel(self):
+ assert_equal(self.a.ravel().shape, (2,))
+ assert_equal(self.m.ravel().shape, (1, 2))
+
+ def test_member_flatten(self):
+ assert_equal(self.a.flatten().shape, (2,))
+ assert_equal(self.m.flatten().shape, (1, 2))
+
+ def test_numpy_ravel_order(self):
+ for t in array, matrix:
+ x = t([[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]])
+ assert_equal(x.ravel(), [[1, 2, 3, 4, 5, 6]])
+ assert_equal(x.ravel(order='F'), [[1, 4, 2, 5, 3, 6]])
+ assert_equal(x.T.ravel(), [[1, 4, 2, 5, 3, 6]])
+ assert_equal(x.T.ravel(order='A'), [[1, 2, 3, 4, 5, 6]])
+
+ def test_array_memory_sharing(self):
+ assert_(np.may_share_memory(self.a, self.a.ravel()))
+ assert_(not np.may_share_memory(self.a, self.a.flatten()))
+
+ def test_matrix_memory_sharing(self):
+ assert_(np.may_share_memory(self.m, self.m.ravel()))
+ assert_(not np.may_share_memory(self.m, self.m.flatten()))
+
+
if __name__ == "__main__":
run_module_suite()