def test_object_scalar_multiply(): # Tickets #2469 and #4482 # 2018-04-29: moved here from core.tests.test_ufunc arr = npm.matrix([1, 2], dtype=object) desired = npm.matrix([[3, 6]], dtype=object) assert_equal(np.multiply(arr, 3), desired) assert_equal(np.multiply(3, arr), desired)
def test_inner_scalar_and_matrix_of_objects(): # Ticket #4482 # 2018-04-29: moved here from core.tests.test_multiarray arr = npm.matrix([1, 2], dtype=object) desired = npm.matrix([[3, 6]], dtype=object) assert_equal(np.inner(arr, 3), desired) assert_equal(np.inner(3, arr), desired)
def test_sort_matrix_none(): # 2018-04-29: moved here from core.tests.test_multiarray a = npm.matrix([[2, 1, 0]]) actual = np.sort(a, axis=None) expected = npm.matrix([[0, 1, 2]]) assert_equal(actual, expected) assert_(type(expected) is npm.matrix)
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 = npm.matrix([[1, 2], [3, 4]], dtype=dt) desired = npm.matrix([[3, 6], [9, 12]], dtype=dt) assert_equal(np.inner(arr, sca), desired) assert_equal(np.inner(sca, arr), desired)
def test_matrix_properties(self): # Ticket #125 a = npm.matrix([1.0], dtype=float) assert_(type(a.real) is npm.matrix) assert_(type(a.imag) is npm.matrix) c, d = npm.matrix([0.0]).nonzero() assert_(type(c) is np.ndarray) assert_(type(d) is np.ndarray)
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_diagonal(): b1 = npm.matrix([[1, 2], [3, 4]]) diag_b1 = npm.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)
def test_trapz_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 = x * x r = np.trapz(y, x) mx = npm.matrix(x) my = npm.matrix(y) mr = np.trapz(my, mx) assert_almost_equal(mr, r)
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_average_matrix(): # 2018-04-29: moved here from core.tests.test_function_base. y = npm.matrix(np.random.rand(5, 5)) assert_array_equal(y.mean(0), np.average(y, 0)) a = npm.matrix([[1, 2], [3, 4]]) w = npm.matrix([[1, 2], [3, 4]]) r = np.average(a, axis=0, weights=w) assert_equal(type(r), npm.matrix) assert_equal(r, [[2.5, 10.0 / 3]])
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 = npm.matrix([[1., 2.]]) m2 = npm.matrix([[1., np.nan]]) m3 = npm.matrix([[1., -np.inf]]) m4 = npm.matrix([[np.nan, np.inf]]) m5 = npm.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)
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_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_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 = npm.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, npm.matrix)) assert_(res.shape == (1, 3)) res = f(mat, axis=1) assert_(isinstance(res, npm.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, npm.matrix)) assert_(res.shape == (3, 3)) res = f(mat, axis=1) assert_(isinstance(res, npm.matrix)) assert_(res.shape == (3, 3)) res = f(mat) assert_(isinstance(res, npm.matrix)) assert_(res.shape == (1, 3 * 3))
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 = npm.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 npm.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 npm.matrix) assert_equal(i.operands[2].shape, (1, 2, 2))
def test_pickling_subbaseclass(self): # Test pickling w/ a subclass of ndarray a = masked_array(npm.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, npm.matrix))
def test_compressed(self): a = masked_array(npm.matrix([1, 2, 3, 4]), mask=[0, 0, 0, 0]) b = a.compressed() assert_equal(b, a) assert_(isinstance(b, npm.matrix)) a[0, 0] = masked b = a.compressed() assert_equal(b, [[2, 3, 4]])
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 = npm.matrix([[0, 1], [2, 3]]) expected = npm.matrix([[0, 2], [4, 6]]) result = np.apply_along_axis(double, 0, m) assert_(isinstance(result, npm.matrix)) assert_array_equal(result, expected) result = np.apply_along_axis(double, 1, m) assert_(isinstance(result, npm.matrix)) assert_array_equal(result, expected)
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 = npm.matrix([dom1, dom1]) res = np.polynomial.polyutils.mapdomain(x, dom1, dom2) assert_(isinstance(res, npm.matrix))
def test_count_mean_with_matrix(self): m = masked_array(npm.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_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 like_function(): # 2018-04-29: moved here from core.tests.test_numeric a = npm.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 npm.matrix) c = like_function(a, subok=False) assert_(type(c) is not npm.matrix)
def test_matrix_indexing(self): # Tests conversions and indexing x1 = npm.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 = npm.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_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])
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(npm.matrix) expanded = np.expand_dims(a, axis=1) assert_equal(expanded.ndim, 3) assert_(not isinstance(expanded, npm.matrix))
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()
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_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]), npm.matrix([1, 2])) msg = str(exc_info.value) msg_reference = textwrap.dedent("""\ Arrays are not equal (shapes (2,), (1, 2) mismatch) x: array([1, 2]) y: matrix([[1, 2]])""") assert_equal(msg, msg_reference)