diff options
Diffstat (limited to 'numpy/matrixlib')
-rw-r--r-- | numpy/matrixlib/defmatrix.py | 136 | ||||
-rw-r--r-- | numpy/matrixlib/tests/test_defmatrix.py | 47 | ||||
-rw-r--r-- | numpy/matrixlib/tests/test_multiarray.py | 3 | ||||
-rw-r--r-- | numpy/matrixlib/tests/test_numeric.py | 17 | ||||
-rw-r--r-- | numpy/matrixlib/tests/test_regression.py | 3 |
5 files changed, 204 insertions, 2 deletions
diff --git a/numpy/matrixlib/defmatrix.py b/numpy/matrixlib/defmatrix.py index 0fd5db66a..d0b39ad9f 100644 --- a/numpy/matrixlib/defmatrix.py +++ b/numpy/matrixlib/defmatrix.py @@ -454,6 +454,96 @@ class matrix(N.ndarray): """ return N.ndarray.sum(self, axis, dtype, out, keepdims=True)._collapse(axis) + + # To update docstring from array to matrix... + def squeeze(self, axis=None): + """ + Return a possibly reshaped matrix. + + Refer to `numpy.squeeze` for more documentation. + + Parameters + ---------- + axis : None or int or tuple of ints, optional + Selects a subset of the single-dimensional entries in the shape. + If an axis is selected with shape entry greater than one, + an error is raised. + + Returns + ------- + squeezed : matrix + The matrix, but as a (1, N) matrix if it had shape (N, 1). + + See Also + -------- + numpy.squeeze : related function + + Notes + ----- + If `m` has a single column then that column is returned + as the single row of a matrix. Otherwise `m` is returned. + The returned matrix is always either `m` itself or a view into `m`. + Supplying an axis keyword argument will not affect the returned matrix + but it may cause an error to be raised. + + Examples + -------- + >>> c = np.matrix([[1], [2]]) + >>> c + matrix([[1], + [2]]) + >>> c.squeeze() + matrix([[1, 2]]) + >>> r = c.T + >>> r + matrix([[1, 2]]) + >>> r.squeeze() + matrix([[1, 2]]) + >>> m = np.matrix([[1, 2], [3, 4]]) + >>> m.squeeze() + matrix([[1, 2], + [3, 4]]) + + """ + return N.ndarray.squeeze(self, axis=axis) + + + # To update docstring from array to matrix... + def flatten(self, order='C'): + """ + Return a flattened copy of the matrix. + + All `N` elements of the matrix are placed into a single row. + + Parameters + ---------- + order : {'C', 'F', 'A'}, optional + Whether to flatten in C (row-major), Fortran (column-major) order, + or preserve the C/Fortran ordering from `m`. + The default is 'C'. + + Returns + ------- + y : matrix + A copy of the matrix, flattened to a `(1, N)` matrix where `N` + is the number of elements in the original matrix. + + See Also + -------- + ravel : Return a flattened array. + flat : A 1-D flat iterator over the matrix. + + Examples + -------- + >>> m = np.matrix([[1,2], [3,4]]) + >>> m.flatten() + matrix([[1, 2, 3, 4]]) + >>> m.flatten('F') + matrix([[1, 3, 2, 4]]) + + """ + return N.ndarray.flatten(self, order=order) + def mean(self, axis=None, dtype=None, out=None): """ Returns the average of the matrix elements along the given axis. @@ -925,6 +1015,46 @@ class matrix(N.ndarray): """ return self.__array__().ravel() + + def ravel(self, order='C'): + """ + Return a flattened matrix. + + Refer to `numpy.ravel` for more documentation. + + 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. + + Returns + ------- + ret : matrix + Return the matrix flattened to shape `(1, N)` where `N` + is the number of elements in the original matrix. + A copy is made only if necessary. + + 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 N.ndarray.ravel(self, order=order) + + def getT(self): """ Returns the transpose of the matrix. @@ -1031,6 +1161,12 @@ def bmat(obj, ldict=None, gdict=None): obj : str or array_like Input data. Names of variables in the current scope may be referenced, even if `obj` is a string. + ldict : dict, optional + A dictionary that replaces local operands in current frame. + Ignored if `obj` is not a string or `gdict` is `None`. + gdict : dict, optional + A dictionary that replaces global operands in current frame. + Ignored if `obj` is not a string. Returns ------- 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() diff --git a/numpy/matrixlib/tests/test_multiarray.py b/numpy/matrixlib/tests/test_multiarray.py index fc5b1df17..64d63f125 100644 --- a/numpy/matrixlib/tests/test_multiarray.py +++ b/numpy/matrixlib/tests/test_multiarray.py @@ -16,3 +16,6 @@ class TestView(TestCase): assert_(isinstance(y, np.matrix)) assert_equal(y.dtype, np.dtype('<i2')) + +if __name__ == "__main__": + run_module_suite() diff --git a/numpy/matrixlib/tests/test_numeric.py b/numpy/matrixlib/tests/test_numeric.py index fa88f5288..91dc92d2e 100644 --- a/numpy/matrixlib/tests/test_numeric.py +++ b/numpy/matrixlib/tests/test_numeric.py @@ -1,10 +1,23 @@ from __future__ import division, absolute_import, print_function -from numpy.testing import assert_equal, TestCase +from numpy.testing import assert_equal, TestCase, run_module_suite from numpy.core import ones -from numpy import matrix +from numpy import matrix, diagonal, diag class TestDot(TestCase): def test_matscalar(self): b1 = matrix(ones((3, 3), dtype=complex)) assert_equal(b1*1.0, b1) + + +def test_diagonal(): + b1 = matrix([[1,2],[3,4]]) + diag_b1 = matrix([[1, 4]]) + + assert_equal(b1.diagonal(), diag_b1) + assert_equal(diagonal(b1), diag_b1) + assert_equal(diag(b1), diag_b1) + + +if __name__ == "__main__": + run_module_suite() diff --git a/numpy/matrixlib/tests/test_regression.py b/numpy/matrixlib/tests/test_regression.py index 4bbd44dcf..119b21d8a 100644 --- a/numpy/matrixlib/tests/test_regression.py +++ b/numpy/matrixlib/tests/test_regression.py @@ -32,3 +32,6 @@ class TestRegression(TestCase): x = np.asmatrix(np.random.uniform(0, 1, (3, 3))) self.assertEqual(x.std().shape, ()) self.assertEqual(x.argmax().shape, ()) + +if __name__ == "__main__": + run_module_suite() |