done
This commit is contained in:
		| @ -0,0 +1,455 @@ | ||||
| import collections.abc | ||||
|  | ||||
| import numpy as np | ||||
| from numpy import asmatrix, bmat, matrix | ||||
| from numpy.linalg import matrix_power | ||||
| from numpy.testing import ( | ||||
|     assert_, | ||||
|     assert_almost_equal, | ||||
|     assert_array_almost_equal, | ||||
|     assert_array_equal, | ||||
|     assert_equal, | ||||
|     assert_raises, | ||||
| ) | ||||
|  | ||||
|  | ||||
| class TestCtor: | ||||
|     def test_basic(self): | ||||
|         A = np.array([[1, 2], [3, 4]]) | ||||
|         mA = matrix(A) | ||||
|         assert_(np.all(mA.A == A)) | ||||
|  | ||||
|         B = bmat("A,A;A,A") | ||||
|         C = bmat([[A, A], [A, A]]) | ||||
|         D = np.array([[1, 2, 1, 2], | ||||
|                       [3, 4, 3, 4], | ||||
|                       [1, 2, 1, 2], | ||||
|                       [3, 4, 3, 4]]) | ||||
|         assert_(np.all(B.A == D)) | ||||
|         assert_(np.all(C.A == D)) | ||||
|  | ||||
|         E = np.array([[5, 6], [7, 8]]) | ||||
|         AEresult = matrix([[1, 2, 5, 6], [3, 4, 7, 8]]) | ||||
|         assert_(np.all(bmat([A, E]) == AEresult)) | ||||
|  | ||||
|         vec = np.arange(5) | ||||
|         mvec = matrix(vec) | ||||
|         assert_(mvec.shape == (1, 5)) | ||||
|  | ||||
|     def test_exceptions(self): | ||||
|         # Check for ValueError when called with invalid string data. | ||||
|         assert_raises(ValueError, matrix, "invalid") | ||||
|  | ||||
|     def test_bmat_nondefault_str(self): | ||||
|         A = np.array([[1, 2], [3, 4]]) | ||||
|         B = np.array([[5, 6], [7, 8]]) | ||||
|         Aresult = np.array([[1, 2, 1, 2], | ||||
|                             [3, 4, 3, 4], | ||||
|                             [1, 2, 1, 2], | ||||
|                             [3, 4, 3, 4]]) | ||||
|         mixresult = np.array([[1, 2, 5, 6], | ||||
|                               [3, 4, 7, 8], | ||||
|                               [5, 6, 1, 2], | ||||
|                               [7, 8, 3, 4]]) | ||||
|         assert_(np.all(bmat("A,A;A,A") == Aresult)) | ||||
|         assert_(np.all(bmat("A,A;A,A", ldict={'A': B}) == Aresult)) | ||||
|         assert_raises(TypeError, bmat, "A,A;A,A", gdict={'A': B}) | ||||
|         assert_( | ||||
|             np.all(bmat("A,A;A,A", ldict={'A': A}, gdict={'A': B}) == Aresult)) | ||||
|         b2 = bmat("A,B;C,D", ldict={'A': A, 'B': B}, gdict={'C': B, 'D': A}) | ||||
|         assert_(np.all(b2 == mixresult)) | ||||
|  | ||||
|  | ||||
| class TestProperties: | ||||
|     def test_sum(self): | ||||
|         """Test whether matrix.sum(axis=1) preserves orientation. | ||||
|         Fails in NumPy <= 0.9.6.2127. | ||||
|         """ | ||||
|         M = matrix([[1, 2, 0, 0], | ||||
|                    [3, 4, 0, 0], | ||||
|                    [1, 2, 1, 2], | ||||
|                    [3, 4, 3, 4]]) | ||||
|         sum0 = matrix([8, 12, 4, 6]) | ||||
|         sum1 = matrix([3, 7, 6, 14]).T | ||||
|         sumall = 30 | ||||
|         assert_array_equal(sum0, M.sum(axis=0)) | ||||
|         assert_array_equal(sum1, M.sum(axis=1)) | ||||
|         assert_equal(sumall, M.sum()) | ||||
|  | ||||
|         assert_array_equal(sum0, np.sum(M, axis=0)) | ||||
|         assert_array_equal(sum1, np.sum(M, axis=1)) | ||||
|         assert_equal(sumall, np.sum(M)) | ||||
|  | ||||
|     def test_prod(self): | ||||
|         x = matrix([[1, 2, 3], [4, 5, 6]]) | ||||
|         assert_equal(x.prod(), 720) | ||||
|         assert_equal(x.prod(0), matrix([[4, 10, 18]])) | ||||
|         assert_equal(x.prod(1), matrix([[6], [120]])) | ||||
|  | ||||
|         assert_equal(np.prod(x), 720) | ||||
|         assert_equal(np.prod(x, axis=0), matrix([[4, 10, 18]])) | ||||
|         assert_equal(np.prod(x, axis=1), matrix([[6], [120]])) | ||||
|  | ||||
|         y = matrix([0, 1, 3]) | ||||
|         assert_(y.prod() == 0) | ||||
|  | ||||
|     def test_max(self): | ||||
|         x = matrix([[1, 2, 3], [4, 5, 6]]) | ||||
|         assert_equal(x.max(), 6) | ||||
|         assert_equal(x.max(0), matrix([[4, 5, 6]])) | ||||
|         assert_equal(x.max(1), matrix([[3], [6]])) | ||||
|  | ||||
|         assert_equal(np.max(x), 6) | ||||
|         assert_equal(np.max(x, axis=0), matrix([[4, 5, 6]])) | ||||
|         assert_equal(np.max(x, axis=1), matrix([[3], [6]])) | ||||
|  | ||||
|     def test_min(self): | ||||
|         x = matrix([[1, 2, 3], [4, 5, 6]]) | ||||
|         assert_equal(x.min(), 1) | ||||
|         assert_equal(x.min(0), matrix([[1, 2, 3]])) | ||||
|         assert_equal(x.min(1), matrix([[1], [4]])) | ||||
|  | ||||
|         assert_equal(np.min(x), 1) | ||||
|         assert_equal(np.min(x, axis=0), matrix([[1, 2, 3]])) | ||||
|         assert_equal(np.min(x, axis=1), matrix([[1], [4]])) | ||||
|  | ||||
|     def test_ptp(self): | ||||
|         x = np.arange(4).reshape((2, 2)) | ||||
|         mx = x.view(np.matrix) | ||||
|         assert_(mx.ptp() == 3) | ||||
|         assert_(np.all(mx.ptp(0) == np.array([2, 2]))) | ||||
|         assert_(np.all(mx.ptp(1) == np.array([1, 1]))) | ||||
|  | ||||
|     def test_var(self): | ||||
|         x = np.arange(9).reshape((3, 3)) | ||||
|         mx = x.view(np.matrix) | ||||
|         assert_equal(x.var(ddof=0), mx.var(ddof=0)) | ||||
|         assert_equal(x.var(ddof=1), mx.var(ddof=1)) | ||||
|  | ||||
|     def test_basic(self): | ||||
|         import numpy.linalg as linalg | ||||
|  | ||||
|         A = np.array([[1., 2.], | ||||
|                       [3., 4.]]) | ||||
|         mA = matrix(A) | ||||
|         assert_(np.allclose(linalg.inv(A), mA.I)) | ||||
|         assert_(np.all(np.array(np.transpose(A) == mA.T))) | ||||
|         assert_(np.all(np.array(np.transpose(A) == mA.H))) | ||||
|         assert_(np.all(A == mA.A)) | ||||
|  | ||||
|         B = A + 2j * A | ||||
|         mB = matrix(B) | ||||
|         assert_(np.allclose(linalg.inv(B), mB.I)) | ||||
|         assert_(np.all(np.array(np.transpose(B) == mB.T))) | ||||
|         assert_(np.all(np.array(np.transpose(B).conj() == mB.H))) | ||||
|  | ||||
|     def test_pinv(self): | ||||
|         x = matrix(np.arange(6).reshape(2, 3)) | ||||
|         xpinv = matrix([[-0.77777778,  0.27777778], | ||||
|                         [-0.11111111,  0.11111111], | ||||
|                         [ 0.55555556, -0.05555556]]) | ||||
|         assert_almost_equal(x.I, xpinv) | ||||
|  | ||||
|     def test_comparisons(self): | ||||
|         A = np.arange(100).reshape(10, 10) | ||||
|         mA = matrix(A) | ||||
|         mB = matrix(A) + 0.1 | ||||
|         assert_(np.all(mB == A + 0.1)) | ||||
|         assert_(np.all(mB == matrix(A + 0.1))) | ||||
|         assert_(not np.any(mB == matrix(A - 0.1))) | ||||
|         assert_(np.all(mA < mB)) | ||||
|         assert_(np.all(mA <= mB)) | ||||
|         assert_(np.all(mA <= mA)) | ||||
|         assert_(not np.any(mA < mA)) | ||||
|  | ||||
|         assert_(not np.any(mB < mA)) | ||||
|         assert_(np.all(mB >= mA)) | ||||
|         assert_(np.all(mB >= mB)) | ||||
|         assert_(not np.any(mB > mB)) | ||||
|  | ||||
|         assert_(np.all(mA == mA)) | ||||
|         assert_(not np.any(mA == mB)) | ||||
|         assert_(np.all(mB != mA)) | ||||
|  | ||||
|         assert_(not np.all(abs(mA) > 0)) | ||||
|         assert_(np.all(abs(mB > 0))) | ||||
|  | ||||
|     def test_asmatrix(self): | ||||
|         A = np.arange(100).reshape(10, 10) | ||||
|         mA = asmatrix(A) | ||||
|         A[0, 0] = -10 | ||||
|         assert_(A[0, 0] == mA[0, 0]) | ||||
|  | ||||
|     def test_noaxis(self): | ||||
|         A = matrix([[1, 0], [0, 1]]) | ||||
|         assert_(A.sum() == matrix(2)) | ||||
|         assert_(A.mean() == matrix(0.5)) | ||||
|  | ||||
|     def test_repr(self): | ||||
|         A = matrix([[1, 0], [0, 1]]) | ||||
|         assert_(repr(A) == "matrix([[1, 0],\n        [0, 1]])") | ||||
|  | ||||
|     def test_make_bool_matrix_from_str(self): | ||||
|         A = matrix('True; True; False') | ||||
|         B = matrix([[True], [True], [False]]) | ||||
|         assert_array_equal(A, B) | ||||
|  | ||||
| class TestCasting: | ||||
|     def test_basic(self): | ||||
|         A = np.arange(100).reshape(10, 10) | ||||
|         mA = matrix(A) | ||||
|  | ||||
|         mB = mA.copy() | ||||
|         O = np.ones((10, 10), np.float64) * 0.1 | ||||
|         mB = mB + O | ||||
|         assert_(mB.dtype.type == np.float64) | ||||
|         assert_(np.all(mA != mB)) | ||||
|         assert_(np.all(mB == mA + 0.1)) | ||||
|  | ||||
|         mC = mA.copy() | ||||
|         O = np.ones((10, 10), np.complex128) | ||||
|         mC = mC * O | ||||
|         assert_(mC.dtype.type == np.complex128) | ||||
|         assert_(np.all(mA != mB)) | ||||
|  | ||||
|  | ||||
| class TestAlgebra: | ||||
|     def test_basic(self): | ||||
|         import numpy.linalg as linalg | ||||
|  | ||||
|         A = np.array([[1., 2.], [3., 4.]]) | ||||
|         mA = matrix(A) | ||||
|  | ||||
|         B = np.identity(2) | ||||
|         for i in range(6): | ||||
|             assert_(np.allclose((mA ** i).A, B)) | ||||
|             B = np.dot(B, A) | ||||
|  | ||||
|         Ainv = linalg.inv(A) | ||||
|         B = np.identity(2) | ||||
|         for i in range(6): | ||||
|             assert_(np.allclose((mA ** -i).A, B)) | ||||
|             B = np.dot(B, Ainv) | ||||
|  | ||||
|         assert_(np.allclose((mA * mA).A, np.dot(A, A))) | ||||
|         assert_(np.allclose((mA + mA).A, (A + A))) | ||||
|         assert_(np.allclose((3 * mA).A, (3 * A))) | ||||
|  | ||||
|         mA2 = matrix(A) | ||||
|         mA2 *= 3 | ||||
|         assert_(np.allclose(mA2.A, 3 * A)) | ||||
|  | ||||
|     def test_pow(self): | ||||
|         """Test raising a matrix to an integer power works as expected.""" | ||||
|         m = matrix("1. 2.; 3. 4.") | ||||
|         m2 = m.copy() | ||||
|         m2 **= 2 | ||||
|         mi = m.copy() | ||||
|         mi **= -1 | ||||
|         m4 = m2.copy() | ||||
|         m4 **= 2 | ||||
|         assert_array_almost_equal(m2, m**2) | ||||
|         assert_array_almost_equal(m4, np.dot(m2, m2)) | ||||
|         assert_array_almost_equal(np.dot(mi, m), np.eye(2)) | ||||
|  | ||||
|     def test_scalar_type_pow(self): | ||||
|         m = matrix([[1, 2], [3, 4]]) | ||||
|         for scalar_t in [np.int8, np.uint8]: | ||||
|             two = scalar_t(2) | ||||
|             assert_array_almost_equal(m ** 2, m ** two) | ||||
|  | ||||
|     def test_notimplemented(self): | ||||
|         '''Check that 'not implemented' operations produce a failure.''' | ||||
|         A = matrix([[1., 2.], | ||||
|                     [3., 4.]]) | ||||
|  | ||||
|         # __rpow__ | ||||
|         with assert_raises(TypeError): | ||||
|             1.0**A | ||||
|  | ||||
|         # __mul__ with something not a list, ndarray, tuple, or scalar | ||||
|         with assert_raises(TypeError): | ||||
|             A * object() | ||||
|  | ||||
|  | ||||
| class TestMatrixReturn: | ||||
|     def test_instance_methods(self): | ||||
|         a = matrix([1.0], dtype='f8') | ||||
|         methodargs = { | ||||
|             'astype': ('intc',), | ||||
|             'clip': (0.0, 1.0), | ||||
|             'compress': ([1],), | ||||
|             'repeat': (1,), | ||||
|             'reshape': (1,), | ||||
|             'swapaxes': (0, 0), | ||||
|             'dot': np.array([1.0]), | ||||
|             } | ||||
|         excluded_methods = [ | ||||
|             'argmin', 'choose', 'dump', 'dumps', 'fill', 'getfield', | ||||
|             'getA', 'getA1', 'item', 'nonzero', 'put', 'putmask', 'resize', | ||||
|             'searchsorted', 'setflags', 'setfield', 'sort', | ||||
|             'partition', 'argpartition', 'newbyteorder', 'to_device', | ||||
|             'take', 'tofile', 'tolist', 'tobytes', 'all', 'any', | ||||
|             'sum', 'argmax', 'argmin', 'min', 'max', 'mean', 'var', 'ptp', | ||||
|             'prod', 'std', 'ctypes', 'itemset', 'bitwise_count', | ||||
|             ] | ||||
|         for attrib in dir(a): | ||||
|             if attrib.startswith('_') or attrib in excluded_methods: | ||||
|                 continue | ||||
|             f = getattr(a, attrib) | ||||
|             if isinstance(f, collections.abc.Callable): | ||||
|                 # reset contents of a | ||||
|                 a.astype('f8') | ||||
|                 a.fill(1.0) | ||||
|                 args = methodargs.get(attrib, ()) | ||||
|                 b = f(*args) | ||||
|                 assert_(type(b) is matrix, f"{attrib}") | ||||
|         assert_(type(a.real) is matrix) | ||||
|         assert_(type(a.imag) is matrix) | ||||
|         c, d = matrix([0.0]).nonzero() | ||||
|         assert_(type(c) is np.ndarray) | ||||
|         assert_(type(d) is np.ndarray) | ||||
|  | ||||
|  | ||||
| class TestIndexing: | ||||
|     def test_basic(self): | ||||
|         x = asmatrix(np.zeros((3, 2), float)) | ||||
|         y = np.zeros((3, 1), float) | ||||
|         y[:, 0] = [0.8, 0.2, 0.3] | ||||
|         x[:, 1] = y > 0.5 | ||||
|         assert_equal(x, [[0, 1], [0, 0], [0, 0]]) | ||||
|  | ||||
|  | ||||
| class TestNewScalarIndexing: | ||||
|     a = matrix([[1, 2], [3, 4]]) | ||||
|  | ||||
|     def test_dimesions(self): | ||||
|         a = self.a | ||||
|         x = a[0] | ||||
|         assert_equal(x.ndim, 2) | ||||
|  | ||||
|     def test_array_from_matrix_list(self): | ||||
|         a = self.a | ||||
|         x = np.array([a, a]) | ||||
|         assert_equal(x.shape, [2, 2, 2]) | ||||
|  | ||||
|     def test_array_to_list(self): | ||||
|         a = self.a | ||||
|         assert_equal(a.tolist(), [[1, 2], [3, 4]]) | ||||
|  | ||||
|     def test_fancy_indexing(self): | ||||
|         a = self.a | ||||
|         x = a[1, [0, 1, 0]] | ||||
|         assert_(isinstance(x, matrix)) | ||||
|         assert_equal(x, matrix([[3,  4,  3]])) | ||||
|         x = a[[1, 0]] | ||||
|         assert_(isinstance(x, matrix)) | ||||
|         assert_equal(x, matrix([[3, 4], [1, 2]])) | ||||
|         x = a[[[1], [0]], [[1, 0], [0, 1]]] | ||||
|         assert_(isinstance(x, matrix)) | ||||
|         assert_equal(x, matrix([[4, 3], [1, 2]])) | ||||
|  | ||||
|     def test_matrix_element(self): | ||||
|         x = matrix([[1, 2, 3], [4, 5, 6]]) | ||||
|         assert_equal(x[0][0], matrix([[1, 2, 3]])) | ||||
|         assert_equal(x[0][0].shape, (1, 3)) | ||||
|         assert_equal(x[0].shape, (1, 3)) | ||||
|         assert_equal(x[:, 0].shape, (2, 1)) | ||||
|  | ||||
|         x = matrix(0) | ||||
|         assert_equal(x[0, 0], 0) | ||||
|         assert_equal(x[0], 0) | ||||
|         assert_equal(x[:, 0].shape, x.shape) | ||||
|  | ||||
|     def test_scalar_indexing(self): | ||||
|         x = asmatrix(np.zeros((3, 2), float)) | ||||
|         assert_equal(x[0, 0], x[0][0]) | ||||
|  | ||||
|     def test_row_column_indexing(self): | ||||
|         x = asmatrix(np.eye(2)) | ||||
|         assert_array_equal(x[0, :], [[1, 0]]) | ||||
|         assert_array_equal(x[1, :], [[0, 1]]) | ||||
|         assert_array_equal(x[:, 0], [[1], [0]]) | ||||
|         assert_array_equal(x[:, 1], [[0], [1]]) | ||||
|  | ||||
|     def test_boolean_indexing(self): | ||||
|         A = np.arange(6) | ||||
|         A.shape = (3, 2) | ||||
|         x = asmatrix(A) | ||||
|         assert_array_equal(x[:, np.array([True, False])], x[:, 0]) | ||||
|         assert_array_equal(x[np.array([True, False, False]), :], x[0, :]) | ||||
|  | ||||
|     def test_list_indexing(self): | ||||
|         A = np.arange(6) | ||||
|         A.shape = (3, 2) | ||||
|         x = asmatrix(A) | ||||
|         assert_array_equal(x[:, [1, 0]], x[:, ::-1]) | ||||
|         assert_array_equal(x[[2, 1, 0], :], x[::-1, :]) | ||||
|  | ||||
|  | ||||
| class TestPower: | ||||
|     def test_returntype(self): | ||||
|         a = np.array([[0, 1], [0, 0]]) | ||||
|         assert_(type(matrix_power(a, 2)) is np.ndarray) | ||||
|         a = asmatrix(a) | ||||
|         assert_(type(matrix_power(a, 2)) is matrix) | ||||
|  | ||||
|     def test_list(self): | ||||
|         assert_array_equal(matrix_power([[0, 1], [0, 0]], 2), [[0, 0], [0, 0]]) | ||||
|  | ||||
|  | ||||
| class TestShape: | ||||
|  | ||||
|     a = np.array([[1], [2]]) | ||||
|     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): | ||||
|         x = np.array([[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]) | ||||
|         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]) | ||||
|  | ||||
|     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())) | ||||
|  | ||||
|     def test_expand_dims_matrix(self): | ||||
|         # matrices are always 2d - so expand_dims only makes sense when the | ||||
|         # type is changed away from matrix. | ||||
|         a = np.arange(10).reshape((2, 5)).view(np.matrix) | ||||
|         expanded = np.expand_dims(a, axis=1) | ||||
|         assert_equal(expanded.ndim, 3) | ||||
|         assert_(not isinstance(expanded, np.matrix)) | ||||
| @ -0,0 +1,360 @@ | ||||
| """Tests of interaction of matrix with other parts of numpy. | ||||
|  | ||||
| Note that tests with MaskedArray and linalg are done in separate files. | ||||
| """ | ||||
| import textwrap | ||||
| import warnings | ||||
|  | ||||
| import pytest | ||||
|  | ||||
| import numpy as np | ||||
| from numpy.testing import ( | ||||
|     assert_, | ||||
|     assert_almost_equal, | ||||
|     assert_array_almost_equal, | ||||
|     assert_array_equal, | ||||
|     assert_equal, | ||||
|     assert_raises, | ||||
|     assert_raises_regex, | ||||
| ) | ||||
|  | ||||
|  | ||||
| def test_fancy_indexing(): | ||||
|     # The matrix class messes with the shape. While this is always | ||||
|     # weird (getitem is not used, it does not have setitem nor knows | ||||
|     # about fancy indexing), this tests gh-3110 | ||||
|     # 2018-04-29: moved here from core.tests.test_index. | ||||
|     m = np.matrix([[1, 2], [3, 4]]) | ||||
|  | ||||
|     assert_(isinstance(m[[0, 1, 0], :], np.matrix)) | ||||
|  | ||||
|     # gh-3110. Note the transpose currently because matrices do *not* | ||||
|     # support dimension fixing for fancy indexing correctly. | ||||
|     x = np.asmatrix(np.arange(50).reshape(5, 10)) | ||||
|     assert_equal(x[:2, np.array(-1)], x[:2, -1].T) | ||||
|  | ||||
|  | ||||
| def test_polynomial_mapdomain(): | ||||
|     # test that polynomial preserved matrix subtype. | ||||
|     # 2018-04-29: moved here from polynomial.tests.polyutils. | ||||
|     dom1 = [0, 4] | ||||
|     dom2 = [1, 3] | ||||
|     x = np.matrix([dom1, dom1]) | ||||
|     res = np.polynomial.polyutils.mapdomain(x, dom1, dom2) | ||||
|     assert_(isinstance(res, np.matrix)) | ||||
|  | ||||
|  | ||||
| def test_sort_matrix_none(): | ||||
|     # 2018-04-29: moved here from core.tests.test_multiarray | ||||
|     a = np.matrix([[2, 1, 0]]) | ||||
|     actual = np.sort(a, axis=None) | ||||
|     expected = np.matrix([[0, 1, 2]]) | ||||
|     assert_equal(actual, expected) | ||||
|     assert_(type(expected) is np.matrix) | ||||
|  | ||||
|  | ||||
| def test_partition_matrix_none(): | ||||
|     # gh-4301 | ||||
|     # 2018-04-29: moved here from core.tests.test_multiarray | ||||
|     a = np.matrix([[2, 1, 0]]) | ||||
|     actual = np.partition(a, 1, axis=None) | ||||
|     expected = np.matrix([[0, 1, 2]]) | ||||
|     assert_equal(actual, expected) | ||||
|     assert_(type(expected) is np.matrix) | ||||
|  | ||||
|  | ||||
| def test_dot_scalar_and_matrix_of_objects(): | ||||
|     # Ticket #2469 | ||||
|     # 2018-04-29: moved here from core.tests.test_multiarray | ||||
|     arr = np.matrix([1, 2], dtype=object) | ||||
|     desired = np.matrix([[3, 6]], dtype=object) | ||||
|     assert_equal(np.dot(arr, 3), desired) | ||||
|     assert_equal(np.dot(3, arr), desired) | ||||
|  | ||||
|  | ||||
| def test_inner_scalar_and_matrix(): | ||||
|     # 2018-04-29: moved here from core.tests.test_multiarray | ||||
|     for dt in np.typecodes['AllInteger'] + np.typecodes['AllFloat'] + '?': | ||||
|         sca = np.array(3, dtype=dt)[()] | ||||
|         arr = np.matrix([[1, 2], [3, 4]], dtype=dt) | ||||
|         desired = np.matrix([[3, 6], [9, 12]], dtype=dt) | ||||
|         assert_equal(np.inner(arr, sca), desired) | ||||
|         assert_equal(np.inner(sca, arr), desired) | ||||
|  | ||||
|  | ||||
| def test_inner_scalar_and_matrix_of_objects(): | ||||
|     # Ticket #4482 | ||||
|     # 2018-04-29: moved here from core.tests.test_multiarray | ||||
|     arr = np.matrix([1, 2], dtype=object) | ||||
|     desired = np.matrix([[3, 6]], dtype=object) | ||||
|     assert_equal(np.inner(arr, 3), desired) | ||||
|     assert_equal(np.inner(3, arr), desired) | ||||
|  | ||||
|  | ||||
| def test_iter_allocate_output_subtype(): | ||||
|     # Make sure that the subtype with priority wins | ||||
|     # 2018-04-29: moved here from core.tests.test_nditer, given the | ||||
|     # matrix specific shape test. | ||||
|  | ||||
|     # matrix vs ndarray | ||||
|     a = np.matrix([[1, 2], [3, 4]]) | ||||
|     b = np.arange(4).reshape(2, 2).T | ||||
|     i = np.nditer([a, b, None], [], | ||||
|                   [['readonly'], ['readonly'], ['writeonly', 'allocate']]) | ||||
|     assert_(type(i.operands[2]) is np.matrix) | ||||
|     assert_(type(i.operands[2]) is not np.ndarray) | ||||
|     assert_equal(i.operands[2].shape, (2, 2)) | ||||
|  | ||||
|     # matrix always wants things to be 2D | ||||
|     b = np.arange(4).reshape(1, 2, 2) | ||||
|     assert_raises(RuntimeError, np.nditer, [a, b, None], [], | ||||
|                   [['readonly'], ['readonly'], ['writeonly', 'allocate']]) | ||||
|     # but if subtypes are disabled, the result can still work | ||||
|     i = np.nditer([a, b, None], [], | ||||
|                   [['readonly'], ['readonly'], | ||||
|                    ['writeonly', 'allocate', 'no_subtype']]) | ||||
|     assert_(type(i.operands[2]) is np.ndarray) | ||||
|     assert_(type(i.operands[2]) is not np.matrix) | ||||
|     assert_equal(i.operands[2].shape, (1, 2, 2)) | ||||
|  | ||||
|  | ||||
| def like_function(): | ||||
|     # 2018-04-29: moved here from core.tests.test_numeric | ||||
|     a = np.matrix([[1, 2], [3, 4]]) | ||||
|     for like_function in np.zeros_like, np.ones_like, np.empty_like: | ||||
|         b = like_function(a) | ||||
|         assert_(type(b) is np.matrix) | ||||
|  | ||||
|         c = like_function(a, subok=False) | ||||
|         assert_(type(c) is not np.matrix) | ||||
|  | ||||
|  | ||||
| def test_array_astype(): | ||||
|     # 2018-04-29: copied here from core.tests.test_api | ||||
|     # subok=True passes through a matrix | ||||
|     a = np.matrix([[0, 1, 2], [3, 4, 5]], dtype='f4') | ||||
|     b = a.astype('f4', subok=True, copy=False) | ||||
|     assert_(a is b) | ||||
|  | ||||
|     # subok=True is default, and creates a subtype on a cast | ||||
|     b = a.astype('i4', copy=False) | ||||
|     assert_equal(a, b) | ||||
|     assert_equal(type(b), np.matrix) | ||||
|  | ||||
|     # subok=False never returns a matrix | ||||
|     b = a.astype('f4', subok=False, copy=False) | ||||
|     assert_equal(a, b) | ||||
|     assert_(not (a is b)) | ||||
|     assert_(type(b) is not np.matrix) | ||||
|  | ||||
|  | ||||
| def test_stack(): | ||||
|     # 2018-04-29: copied here from core.tests.test_shape_base | ||||
|     # check np.matrix cannot be stacked | ||||
|     m = np.matrix([[1, 2], [3, 4]]) | ||||
|     assert_raises_regex(ValueError, 'shape too large to be a matrix', | ||||
|                         np.stack, [m, m]) | ||||
|  | ||||
|  | ||||
| def test_object_scalar_multiply(): | ||||
|     # Tickets #2469 and #4482 | ||||
|     # 2018-04-29: moved here from core.tests.test_ufunc | ||||
|     arr = np.matrix([1, 2], dtype=object) | ||||
|     desired = np.matrix([[3, 6]], dtype=object) | ||||
|     assert_equal(np.multiply(arr, 3), desired) | ||||
|     assert_equal(np.multiply(3, arr), desired) | ||||
|  | ||||
|  | ||||
| def test_nanfunctions_matrices(): | ||||
|     # Check that it works and that type and | ||||
|     # shape are preserved | ||||
|     # 2018-04-29: moved here from core.tests.test_nanfunctions | ||||
|     mat = np.matrix(np.eye(3)) | ||||
|     for f in [np.nanmin, np.nanmax]: | ||||
|         res = f(mat, axis=0) | ||||
|         assert_(isinstance(res, np.matrix)) | ||||
|         assert_(res.shape == (1, 3)) | ||||
|         res = f(mat, axis=1) | ||||
|         assert_(isinstance(res, np.matrix)) | ||||
|         assert_(res.shape == (3, 1)) | ||||
|         res = f(mat) | ||||
|         assert_(np.isscalar(res)) | ||||
|     # check that rows of nan are dealt with for subclasses (#4628) | ||||
|     mat[1] = np.nan | ||||
|     for f in [np.nanmin, np.nanmax]: | ||||
|         with warnings.catch_warnings(record=True) as w: | ||||
|             warnings.simplefilter('always') | ||||
|             res = f(mat, axis=0) | ||||
|             assert_(isinstance(res, np.matrix)) | ||||
|             assert_(not np.any(np.isnan(res))) | ||||
|             assert_(len(w) == 0) | ||||
|  | ||||
|         with warnings.catch_warnings(record=True) as w: | ||||
|             warnings.simplefilter('always') | ||||
|             res = f(mat, axis=1) | ||||
|             assert_(isinstance(res, np.matrix)) | ||||
|             assert_(np.isnan(res[1, 0]) and not np.isnan(res[0, 0]) | ||||
|                     and not np.isnan(res[2, 0])) | ||||
|             assert_(len(w) == 1, 'no warning raised') | ||||
|             assert_(issubclass(w[0].category, RuntimeWarning)) | ||||
|  | ||||
|         with warnings.catch_warnings(record=True) as w: | ||||
|             warnings.simplefilter('always') | ||||
|             res = f(mat) | ||||
|             assert_(np.isscalar(res)) | ||||
|             assert_(res != np.nan) | ||||
|             assert_(len(w) == 0) | ||||
|  | ||||
|  | ||||
| def test_nanfunctions_matrices_general(): | ||||
|     # Check that it works and that type and | ||||
|     # shape are preserved | ||||
|     # 2018-04-29: moved here from core.tests.test_nanfunctions | ||||
|     mat = np.matrix(np.eye(3)) | ||||
|     for f in (np.nanargmin, np.nanargmax, np.nansum, np.nanprod, | ||||
|               np.nanmean, np.nanvar, np.nanstd): | ||||
|         res = f(mat, axis=0) | ||||
|         assert_(isinstance(res, np.matrix)) | ||||
|         assert_(res.shape == (1, 3)) | ||||
|         res = f(mat, axis=1) | ||||
|         assert_(isinstance(res, np.matrix)) | ||||
|         assert_(res.shape == (3, 1)) | ||||
|         res = f(mat) | ||||
|         assert_(np.isscalar(res)) | ||||
|  | ||||
|     for f in np.nancumsum, np.nancumprod: | ||||
|         res = f(mat, axis=0) | ||||
|         assert_(isinstance(res, np.matrix)) | ||||
|         assert_(res.shape == (3, 3)) | ||||
|         res = f(mat, axis=1) | ||||
|         assert_(isinstance(res, np.matrix)) | ||||
|         assert_(res.shape == (3, 3)) | ||||
|         res = f(mat) | ||||
|         assert_(isinstance(res, np.matrix)) | ||||
|         assert_(res.shape == (1, 3 * 3)) | ||||
|  | ||||
|  | ||||
| def test_average_matrix(): | ||||
|     # 2018-04-29: moved here from core.tests.test_function_base. | ||||
|     y = np.matrix(np.random.rand(5, 5)) | ||||
|     assert_array_equal(y.mean(0), np.average(y, 0)) | ||||
|  | ||||
|     a = np.matrix([[1, 2], [3, 4]]) | ||||
|     w = np.matrix([[1, 2], [3, 4]]) | ||||
|  | ||||
|     r = np.average(a, axis=0, weights=w) | ||||
|     assert_equal(type(r), np.matrix) | ||||
|     assert_equal(r, [[2.5, 10.0 / 3]]) | ||||
|  | ||||
|  | ||||
| def test_dot_matrix(): | ||||
|     # Test to make sure matrices give the same answer as ndarrays | ||||
|     # 2018-04-29: moved here from core.tests.test_function_base. | ||||
|     x = np.linspace(0, 5) | ||||
|     y = np.linspace(-5, 0) | ||||
|     mx = np.matrix(x) | ||||
|     my = np.matrix(y) | ||||
|     r = np.dot(x, y) | ||||
|     mr = np.dot(mx, my.T) | ||||
|     assert_almost_equal(mr, r) | ||||
|  | ||||
|  | ||||
| def test_ediff1d_matrix(): | ||||
|     # 2018-04-29: moved here from core.tests.test_arraysetops. | ||||
|     assert isinstance(np.ediff1d(np.matrix(1)), np.matrix) | ||||
|     assert isinstance(np.ediff1d(np.matrix(1), to_begin=1), np.matrix) | ||||
|  | ||||
|  | ||||
| def test_apply_along_axis_matrix(): | ||||
|     # this test is particularly malicious because matrix | ||||
|     # refuses to become 1d | ||||
|     # 2018-04-29: moved here from core.tests.test_shape_base. | ||||
|     def double(row): | ||||
|         return row * 2 | ||||
|  | ||||
|     m = np.matrix([[0, 1], [2, 3]]) | ||||
|     expected = np.matrix([[0, 2], [4, 6]]) | ||||
|  | ||||
|     result = np.apply_along_axis(double, 0, m) | ||||
|     assert_(isinstance(result, np.matrix)) | ||||
|     assert_array_equal(result, expected) | ||||
|  | ||||
|     result = np.apply_along_axis(double, 1, m) | ||||
|     assert_(isinstance(result, np.matrix)) | ||||
|     assert_array_equal(result, expected) | ||||
|  | ||||
|  | ||||
| def test_kron_matrix(): | ||||
|     # 2018-04-29: moved here from core.tests.test_shape_base. | ||||
|     a = np.ones([2, 2]) | ||||
|     m = np.asmatrix(a) | ||||
|     assert_equal(type(np.kron(a, a)), np.ndarray) | ||||
|     assert_equal(type(np.kron(m, m)), np.matrix) | ||||
|     assert_equal(type(np.kron(a, m)), np.matrix) | ||||
|     assert_equal(type(np.kron(m, a)), np.matrix) | ||||
|  | ||||
|  | ||||
| class TestConcatenatorMatrix: | ||||
|     # 2018-04-29: moved here from core.tests.test_index_tricks. | ||||
|     def test_matrix(self): | ||||
|         a = [1, 2] | ||||
|         b = [3, 4] | ||||
|  | ||||
|         ab_r = np.r_['r', a, b] | ||||
|         ab_c = np.r_['c', a, b] | ||||
|  | ||||
|         assert_equal(type(ab_r), np.matrix) | ||||
|         assert_equal(type(ab_c), np.matrix) | ||||
|  | ||||
|         assert_equal(np.array(ab_r), [[1, 2, 3, 4]]) | ||||
|         assert_equal(np.array(ab_c), [[1], [2], [3], [4]]) | ||||
|  | ||||
|         assert_raises(ValueError, lambda: np.r_['rc', a, b]) | ||||
|  | ||||
|     def test_matrix_scalar(self): | ||||
|         r = np.r_['r', [1, 2], 3] | ||||
|         assert_equal(type(r), np.matrix) | ||||
|         assert_equal(np.array(r), [[1, 2, 3]]) | ||||
|  | ||||
|     def test_matrix_builder(self): | ||||
|         a = np.array([1]) | ||||
|         b = np.array([2]) | ||||
|         c = np.array([3]) | ||||
|         d = np.array([4]) | ||||
|         actual = np.r_['a, b; c, d'] | ||||
|         expected = np.bmat([[a, b], [c, d]]) | ||||
|  | ||||
|         assert_equal(actual, expected) | ||||
|         assert_equal(type(actual), type(expected)) | ||||
|  | ||||
|  | ||||
| def test_array_equal_error_message_matrix(): | ||||
|     # 2018-04-29: moved here from testing.tests.test_utils. | ||||
|     with pytest.raises(AssertionError) as exc_info: | ||||
|         assert_equal(np.array([1, 2]), np.matrix([1, 2])) | ||||
|     msg = str(exc_info.value) | ||||
|     msg_reference = textwrap.dedent("""\ | ||||
|  | ||||
|     Arrays are not equal | ||||
|  | ||||
|     (shapes (2,), (1, 2) mismatch) | ||||
|      ACTUAL: array([1, 2]) | ||||
|      DESIRED: matrix([[1, 2]])""") | ||||
|     assert_equal(msg, msg_reference) | ||||
|  | ||||
|  | ||||
| def test_array_almost_equal_matrix(): | ||||
|     # Matrix slicing keeps things 2-D, while array does not necessarily. | ||||
|     # See gh-8452. | ||||
|     # 2018-04-29: moved here from testing.tests.test_utils. | ||||
|     m1 = np.matrix([[1., 2.]]) | ||||
|     m2 = np.matrix([[1., np.nan]]) | ||||
|     m3 = np.matrix([[1., -np.inf]]) | ||||
|     m4 = np.matrix([[np.nan, np.inf]]) | ||||
|     m5 = np.matrix([[1., 2.], [np.nan, np.inf]]) | ||||
|     for assert_func in assert_array_almost_equal, assert_almost_equal: | ||||
|         for m in m1, m2, m3, m4, m5: | ||||
|             assert_func(m, m) | ||||
|             a = np.array(m) | ||||
|             assert_func(a, m) | ||||
|             assert_func(m, a) | ||||
| @ -0,0 +1,240 @@ | ||||
| import pickle | ||||
|  | ||||
| import numpy as np | ||||
| from numpy.ma.core import ( | ||||
|     MaskedArray, | ||||
|     MaskType, | ||||
|     add, | ||||
|     allequal, | ||||
|     divide, | ||||
|     getmask, | ||||
|     hypot, | ||||
|     log, | ||||
|     masked, | ||||
|     masked_array, | ||||
|     masked_values, | ||||
|     nomask, | ||||
| ) | ||||
| from numpy.ma.extras import mr_ | ||||
| from numpy.ma.testutils import assert_, assert_array_equal, assert_equal, assert_raises | ||||
|  | ||||
|  | ||||
| class MMatrix(MaskedArray, np.matrix,): | ||||
|  | ||||
|     def __new__(cls, data, mask=nomask): | ||||
|         mat = np.matrix(data) | ||||
|         _data = MaskedArray.__new__(cls, data=mat, mask=mask) | ||||
|         return _data | ||||
|  | ||||
|     def __array_finalize__(self, obj): | ||||
|         np.matrix.__array_finalize__(self, obj) | ||||
|         MaskedArray.__array_finalize__(self, obj) | ||||
|  | ||||
|     @property | ||||
|     def _series(self): | ||||
|         _view = self.view(MaskedArray) | ||||
|         _view._sharedmask = False | ||||
|         return _view | ||||
|  | ||||
|  | ||||
| class TestMaskedMatrix: | ||||
|     def test_matrix_indexing(self): | ||||
|         # Tests conversions and indexing | ||||
|         x1 = np.matrix([[1, 2, 3], [4, 3, 2]]) | ||||
|         x2 = masked_array(x1, mask=[[1, 0, 0], [0, 1, 0]]) | ||||
|         x3 = masked_array(x1, mask=[[0, 1, 0], [1, 0, 0]]) | ||||
|         x4 = masked_array(x1) | ||||
|         # test conversion to strings | ||||
|         str(x2)  # raises? | ||||
|         repr(x2)  # raises? | ||||
|         # tests of indexing | ||||
|         assert_(type(x2[1, 0]) is type(x1[1, 0])) | ||||
|         assert_(x1[1, 0] == x2[1, 0]) | ||||
|         assert_(x2[1, 1] is masked) | ||||
|         assert_equal(x1[0, 2], x2[0, 2]) | ||||
|         assert_equal(x1[0, 1:], x2[0, 1:]) | ||||
|         assert_equal(x1[:, 2], x2[:, 2]) | ||||
|         assert_equal(x1[:], x2[:]) | ||||
|         assert_equal(x1[1:], x3[1:]) | ||||
|         x1[0, 2] = 9 | ||||
|         x2[0, 2] = 9 | ||||
|         assert_equal(x1, x2) | ||||
|         x1[0, 1:] = 99 | ||||
|         x2[0, 1:] = 99 | ||||
|         assert_equal(x1, x2) | ||||
|         x2[0, 1] = masked | ||||
|         assert_equal(x1, x2) | ||||
|         x2[0, 1:] = masked | ||||
|         assert_equal(x1, x2) | ||||
|         x2[0, :] = x1[0, :] | ||||
|         x2[0, 1] = masked | ||||
|         assert_(allequal(getmask(x2), np.array([[0, 1, 0], [0, 1, 0]]))) | ||||
|         x3[1, :] = masked_array([1, 2, 3], [1, 1, 0]) | ||||
|         assert_(allequal(getmask(x3)[1], masked_array([1, 1, 0]))) | ||||
|         assert_(allequal(getmask(x3[1]), masked_array([1, 1, 0]))) | ||||
|         x4[1, :] = masked_array([1, 2, 3], [1, 1, 0]) | ||||
|         assert_(allequal(getmask(x4[1]), masked_array([1, 1, 0]))) | ||||
|         assert_(allequal(x4[1], masked_array([1, 2, 3]))) | ||||
|         x1 = np.matrix(np.arange(5) * 1.0) | ||||
|         x2 = masked_values(x1, 3.0) | ||||
|         assert_equal(x1, x2) | ||||
|         assert_(allequal(masked_array([0, 0, 0, 1, 0], dtype=MaskType), | ||||
|                          x2.mask)) | ||||
|         assert_equal(3.0, x2.fill_value) | ||||
|  | ||||
|     def test_pickling_subbaseclass(self): | ||||
|         # Test pickling w/ a subclass of ndarray | ||||
|         a = masked_array(np.matrix(list(range(10))), mask=[1, 0, 1, 0, 0] * 2) | ||||
|         for proto in range(2, pickle.HIGHEST_PROTOCOL + 1): | ||||
|             a_pickled = pickle.loads(pickle.dumps(a, protocol=proto)) | ||||
|             assert_equal(a_pickled._mask, a._mask) | ||||
|             assert_equal(a_pickled, a) | ||||
|             assert_(isinstance(a_pickled._data, np.matrix)) | ||||
|  | ||||
|     def test_count_mean_with_matrix(self): | ||||
|         m = masked_array(np.matrix([[1, 2], [3, 4]]), mask=np.zeros((2, 2))) | ||||
|  | ||||
|         assert_equal(m.count(axis=0).shape, (1, 2)) | ||||
|         assert_equal(m.count(axis=1).shape, (2, 1)) | ||||
|  | ||||
|         # Make sure broadcasting inside mean and var work | ||||
|         assert_equal(m.mean(axis=0), [[2., 3.]]) | ||||
|         assert_equal(m.mean(axis=1), [[1.5], [3.5]]) | ||||
|  | ||||
|     def test_flat(self): | ||||
|         # Test that flat can return items even for matrices [#4585, #4615] | ||||
|         # test simple access | ||||
|         test = masked_array(np.matrix([[1, 2, 3]]), mask=[0, 0, 1]) | ||||
|         assert_equal(test.flat[1], 2) | ||||
|         assert_equal(test.flat[2], masked) | ||||
|         assert_(np.all(test.flat[0:2] == test[0, 0:2])) | ||||
|         # Test flat on masked_matrices | ||||
|         test = masked_array(np.matrix([[1, 2, 3]]), mask=[0, 0, 1]) | ||||
|         test.flat = masked_array([3, 2, 1], mask=[1, 0, 0]) | ||||
|         control = masked_array(np.matrix([[3, 2, 1]]), mask=[1, 0, 0]) | ||||
|         assert_equal(test, control) | ||||
|         # Test setting | ||||
|         test = masked_array(np.matrix([[1, 2, 3]]), mask=[0, 0, 1]) | ||||
|         testflat = test.flat | ||||
|         testflat[:] = testflat[[2, 1, 0]] | ||||
|         assert_equal(test, control) | ||||
|         testflat[0] = 9 | ||||
|         # test that matrices keep the correct shape (#4615) | ||||
|         a = masked_array(np.matrix(np.eye(2)), mask=0) | ||||
|         b = a.flat | ||||
|         b01 = b[:2] | ||||
|         assert_equal(b01.data, np.array([[1., 0.]])) | ||||
|         assert_equal(b01.mask, np.array([[False, False]])) | ||||
|  | ||||
|     def test_allany_onmatrices(self): | ||||
|         x = np.array([[0.13, 0.26, 0.90], | ||||
|                       [0.28, 0.33, 0.63], | ||||
|                       [0.31, 0.87, 0.70]]) | ||||
|         X = np.matrix(x) | ||||
|         m = np.array([[True, False, False], | ||||
|                       [False, False, False], | ||||
|                       [True, True, False]], dtype=np.bool) | ||||
|         mX = masked_array(X, mask=m) | ||||
|         mXbig = (mX > 0.5) | ||||
|         mXsmall = (mX < 0.5) | ||||
|  | ||||
|         assert_(not mXbig.all()) | ||||
|         assert_(mXbig.any()) | ||||
|         assert_equal(mXbig.all(0), np.matrix([False, False, True])) | ||||
|         assert_equal(mXbig.all(1), np.matrix([False, False, True]).T) | ||||
|         assert_equal(mXbig.any(0), np.matrix([False, False, True])) | ||||
|         assert_equal(mXbig.any(1), np.matrix([True, True, True]).T) | ||||
|  | ||||
|         assert_(not mXsmall.all()) | ||||
|         assert_(mXsmall.any()) | ||||
|         assert_equal(mXsmall.all(0), np.matrix([True, True, False])) | ||||
|         assert_equal(mXsmall.all(1), np.matrix([False, False, False]).T) | ||||
|         assert_equal(mXsmall.any(0), np.matrix([True, True, False])) | ||||
|         assert_equal(mXsmall.any(1), np.matrix([True, True, False]).T) | ||||
|  | ||||
|     def test_compressed(self): | ||||
|         a = masked_array(np.matrix([1, 2, 3, 4]), mask=[0, 0, 0, 0]) | ||||
|         b = a.compressed() | ||||
|         assert_equal(b, a) | ||||
|         assert_(isinstance(b, np.matrix)) | ||||
|         a[0, 0] = masked | ||||
|         b = a.compressed() | ||||
|         assert_equal(b, [[2, 3, 4]]) | ||||
|  | ||||
|     def test_ravel(self): | ||||
|         a = masked_array(np.matrix([1, 2, 3, 4, 5]), mask=[[0, 1, 0, 0, 0]]) | ||||
|         aravel = a.ravel() | ||||
|         assert_equal(aravel.shape, (1, 5)) | ||||
|         assert_equal(aravel._mask.shape, a.shape) | ||||
|  | ||||
|     def test_view(self): | ||||
|         # Test view w/ flexible dtype | ||||
|         iterator = list(zip(np.arange(10), np.random.rand(10))) | ||||
|         data = np.array(iterator) | ||||
|         a = masked_array(iterator, dtype=[('a', float), ('b', float)]) | ||||
|         a.mask[0] = (1, 0) | ||||
|         test = a.view((float, 2), np.matrix) | ||||
|         assert_equal(test, data) | ||||
|         assert_(isinstance(test, np.matrix)) | ||||
|         assert_(not isinstance(test, MaskedArray)) | ||||
|  | ||||
|  | ||||
| class TestSubclassing: | ||||
|     # Test suite for masked subclasses of ndarray. | ||||
|  | ||||
|     def setup_method(self): | ||||
|         x = np.arange(5, dtype='float') | ||||
|         mx = MMatrix(x, mask=[0, 1, 0, 0, 0]) | ||||
|         self.data = (x, mx) | ||||
|  | ||||
|     def test_maskedarray_subclassing(self): | ||||
|         # Tests subclassing MaskedArray | ||||
|         (x, mx) = self.data | ||||
|         assert_(isinstance(mx._data, np.matrix)) | ||||
|  | ||||
|     def test_masked_unary_operations(self): | ||||
|         # Tests masked_unary_operation | ||||
|         (x, mx) = self.data | ||||
|         with np.errstate(divide='ignore'): | ||||
|             assert_(isinstance(log(mx), MMatrix)) | ||||
|             assert_equal(log(x), np.log(x)) | ||||
|  | ||||
|     def test_masked_binary_operations(self): | ||||
|         # Tests masked_binary_operation | ||||
|         (x, mx) = self.data | ||||
|         # Result should be a MMatrix | ||||
|         assert_(isinstance(add(mx, mx), MMatrix)) | ||||
|         assert_(isinstance(add(mx, x), MMatrix)) | ||||
|         # Result should work | ||||
|         assert_equal(add(mx, x), mx + x) | ||||
|         assert_(isinstance(add(mx, mx)._data, np.matrix)) | ||||
|         with assert_raises(TypeError): | ||||
|             add.outer(mx, mx) | ||||
|         assert_(isinstance(hypot(mx, mx), MMatrix)) | ||||
|         assert_(isinstance(hypot(mx, x), MMatrix)) | ||||
|  | ||||
|     def test_masked_binary_operations2(self): | ||||
|         # Tests domained_masked_binary_operation | ||||
|         (x, mx) = self.data | ||||
|         xmx = masked_array(mx.data.__array__(), mask=mx.mask) | ||||
|         assert_(isinstance(divide(mx, mx), MMatrix)) | ||||
|         assert_(isinstance(divide(mx, x), MMatrix)) | ||||
|         assert_equal(divide(mx, mx), divide(xmx, xmx)) | ||||
|  | ||||
| class TestConcatenator: | ||||
|     # Tests for mr_, the equivalent of r_ for masked arrays. | ||||
|  | ||||
|     def test_matrix_builder(self): | ||||
|         assert_raises(np.ma.MAError, lambda: mr_['1, 2; 3, 4']) | ||||
|  | ||||
|     def test_matrix(self): | ||||
|         # Test consistency with unmasked version.  If we ever deprecate | ||||
|         # matrix, this test should either still pass, or both actual and | ||||
|         # expected should fail to be build. | ||||
|         actual = mr_['r', 1, 2, 3] | ||||
|         expected = np.ma.array(np.r_['r', 1, 2, 3]) | ||||
|         assert_array_equal(actual, expected) | ||||
|  | ||||
|         # outer type is masked array, inner type is matrix | ||||
|         assert_equal(type(actual), type(expected)) | ||||
|         assert_equal(type(actual.data), type(expected.data)) | ||||
| @ -0,0 +1,105 @@ | ||||
| """ Test functions for linalg module using the matrix class.""" | ||||
| import numpy as np | ||||
| from numpy.linalg.tests.test_linalg import ( | ||||
|     CondCases, | ||||
|     DetCases, | ||||
|     EigCases, | ||||
|     EigvalsCases, | ||||
|     InvCases, | ||||
|     LinalgCase, | ||||
|     LinalgTestCase, | ||||
|     LstsqCases, | ||||
|     PinvCases, | ||||
|     SolveCases, | ||||
|     SVDCases, | ||||
|     _TestNorm2D, | ||||
|     _TestNormDoubleBase, | ||||
|     _TestNormInt64Base, | ||||
|     _TestNormSingleBase, | ||||
|     apply_tag, | ||||
| ) | ||||
| from numpy.linalg.tests.test_linalg import TestQR as _TestQR | ||||
|  | ||||
| CASES = [] | ||||
|  | ||||
| # square test cases | ||||
| CASES += apply_tag('square', [ | ||||
|     LinalgCase("0x0_matrix", | ||||
|                np.empty((0, 0), dtype=np.double).view(np.matrix), | ||||
|                np.empty((0, 1), dtype=np.double).view(np.matrix), | ||||
|                tags={'size-0'}), | ||||
|     LinalgCase("matrix_b_only", | ||||
|                np.array([[1., 2.], [3., 4.]]), | ||||
|                np.matrix([2., 1.]).T), | ||||
|     LinalgCase("matrix_a_and_b", | ||||
|                np.matrix([[1., 2.], [3., 4.]]), | ||||
|                np.matrix([2., 1.]).T), | ||||
| ]) | ||||
|  | ||||
| # hermitian test-cases | ||||
| CASES += apply_tag('hermitian', [ | ||||
|     LinalgCase("hmatrix_a_and_b", | ||||
|                np.matrix([[1., 2.], [2., 1.]]), | ||||
|                None), | ||||
| ]) | ||||
| # No need to make generalized or strided cases for matrices. | ||||
|  | ||||
|  | ||||
| class MatrixTestCase(LinalgTestCase): | ||||
|     TEST_CASES = CASES | ||||
|  | ||||
|  | ||||
| class TestSolveMatrix(SolveCases, MatrixTestCase): | ||||
|     pass | ||||
|  | ||||
|  | ||||
| class TestInvMatrix(InvCases, MatrixTestCase): | ||||
|     pass | ||||
|  | ||||
|  | ||||
| class TestEigvalsMatrix(EigvalsCases, MatrixTestCase): | ||||
|     pass | ||||
|  | ||||
|  | ||||
| class TestEigMatrix(EigCases, MatrixTestCase): | ||||
|     pass | ||||
|  | ||||
|  | ||||
| class TestSVDMatrix(SVDCases, MatrixTestCase): | ||||
|     pass | ||||
|  | ||||
|  | ||||
| class TestCondMatrix(CondCases, MatrixTestCase): | ||||
|     pass | ||||
|  | ||||
|  | ||||
| class TestPinvMatrix(PinvCases, MatrixTestCase): | ||||
|     pass | ||||
|  | ||||
|  | ||||
| class TestDetMatrix(DetCases, MatrixTestCase): | ||||
|     pass | ||||
|  | ||||
|  | ||||
| class TestLstsqMatrix(LstsqCases, MatrixTestCase): | ||||
|     pass | ||||
|  | ||||
|  | ||||
| class _TestNorm2DMatrix(_TestNorm2D): | ||||
|     array = np.matrix | ||||
|  | ||||
|  | ||||
| class TestNormDoubleMatrix(_TestNorm2DMatrix, _TestNormDoubleBase): | ||||
|     pass | ||||
|  | ||||
|  | ||||
| class TestNormSingleMatrix(_TestNorm2DMatrix, _TestNormSingleBase): | ||||
|     pass | ||||
|  | ||||
|  | ||||
| class TestNormInt64Matrix(_TestNorm2DMatrix, _TestNormInt64Base): | ||||
|     pass | ||||
|  | ||||
|  | ||||
| class TestQRMatrix(_TestQR): | ||||
|     array = np.matrix | ||||
| @ -0,0 +1,17 @@ | ||||
| import numpy as np | ||||
| from numpy.testing import assert_, assert_array_equal, assert_equal | ||||
|  | ||||
|  | ||||
| class TestView: | ||||
|     def test_type(self): | ||||
|         x = np.array([1, 2, 3]) | ||||
|         assert_(isinstance(x.view(np.matrix), np.matrix)) | ||||
|  | ||||
|     def test_keywords(self): | ||||
|         x = np.array([(1, 2)], dtype=[('a', np.int8), ('b', np.int8)]) | ||||
|         # We must be specific about the endianness here: | ||||
|         y = x.view(dtype='<i2', type=np.matrix) | ||||
|         assert_array_equal(y, [[513]]) | ||||
|  | ||||
|         assert_(isinstance(y, np.matrix)) | ||||
|         assert_equal(y.dtype, np.dtype('<i2')) | ||||
| @ -0,0 +1,18 @@ | ||||
| import numpy as np | ||||
| from numpy.testing import assert_equal | ||||
|  | ||||
|  | ||||
| class TestDot: | ||||
|     def test_matscalar(self): | ||||
|         b1 = np.matrix(np.ones((3, 3), dtype=complex)) | ||||
|         assert_equal(b1 * 1.0, b1) | ||||
|  | ||||
|  | ||||
| def test_diagonal(): | ||||
|     b1 = np.matrix([[1, 2], [3, 4]]) | ||||
|     diag_b1 = np.matrix([[1, 4]]) | ||||
|     array_b1 = np.array([1, 4]) | ||||
|  | ||||
|     assert_equal(b1.diagonal(), diag_b1) | ||||
|     assert_equal(np.diagonal(b1), array_b1) | ||||
|     assert_equal(np.diag(b1), array_b1) | ||||
| @ -0,0 +1,31 @@ | ||||
| import numpy as np | ||||
| from numpy.testing import assert_, assert_equal, assert_raises | ||||
|  | ||||
|  | ||||
| class TestRegression: | ||||
|     def test_kron_matrix(self): | ||||
|         # Ticket #71 | ||||
|         x = np.matrix('[1 0; 1 0]') | ||||
|         assert_equal(type(np.kron(x, x)), type(x)) | ||||
|  | ||||
|     def test_matrix_properties(self): | ||||
|         # Ticket #125 | ||||
|         a = np.matrix([1.0], dtype=float) | ||||
|         assert_(type(a.real) is np.matrix) | ||||
|         assert_(type(a.imag) is np.matrix) | ||||
|         c, d = np.matrix([0.0]).nonzero() | ||||
|         assert_(type(c) is np.ndarray) | ||||
|         assert_(type(d) is np.ndarray) | ||||
|  | ||||
|     def test_matrix_multiply_by_1d_vector(self): | ||||
|         # Ticket #473 | ||||
|         def mul(): | ||||
|             np.asmatrix(np.eye(2)) * np.ones(2) | ||||
|  | ||||
|         assert_raises(ValueError, mul) | ||||
|  | ||||
|     def test_matrix_std_argmax(self): | ||||
|         # Ticket #83 | ||||
|         x = np.asmatrix(np.random.uniform(0, 1, (3, 3))) | ||||
|         assert_equal(x.std().shape, ()) | ||||
|         assert_equal(x.argmax().shape, ()) | ||||
		Reference in New Issue
	
	Block a user