1
from numpy.testing import *
2
from numpy.core import *
5
class TestCtor(TestCase):
7
A = array([[1,2],[3,4]])
12
C = bmat([[A,A], [A,A]])
20
E = array([[5,6],[7,8]])
21
AEresult = matrix([[1,2,5,6],[3,4,7,8]])
22
assert all(bmat([A,E]) == AEresult)
26
assert mvec.shape == (1,5)
28
def test_bmat_nondefault_str(self):
29
A = array([[1,2],[3,4]])
30
B = array([[5,6],[7,8]])
31
Aresult = array([[1,2,1,2],
35
Bresult = array([[5,6,5,6],
39
mixresult = array([[1,2,5,6],
43
assert all(bmat("A,A;A,A") == Aresult)
44
assert all(bmat("A,A;A,A",ldict={'A':B}) == Aresult)
45
assert_raises(TypeError, bmat, "A,A;A,A",gdict={'A':B})
46
assert all(bmat("A,A;A,A",ldict={'A':A},gdict={'A':B}) == Aresult)
47
b2 = bmat("A,B;C,D",ldict={'A':A,'B':B},gdict={'C':B,'D':A})
48
assert all(b2 == mixresult)
51
class TestProperties(TestCase):
53
"""Test whether matrix.sum(axis=1) preserves orientation.
54
Fails in NumPy <= 0.9.6.2127.
56
M = matrix([[1,2,0,0],
60
sum0 = matrix([8,12,4,6])
61
sum1 = matrix([3,7,6,14]).T
63
assert_array_equal(sum0, M.sum(axis=0))
64
assert_array_equal(sum1, M.sum(axis=1))
65
assert sumall == M.sum()
69
x = matrix([[1,2,3],[4,5,6]])
70
assert x.prod() == 720
71
assert all(x.prod(0) == matrix([[4,10,18]]))
72
assert all(x.prod(1) == matrix([[6],[120]]))
78
x = matrix([[1,2,3],[4,5,6]])
80
assert all(x.max(0) == matrix([[4,5,6]]))
81
assert all(x.max(1) == matrix([[3],[6]]))
84
x = matrix([[1,2,3],[4,5,6]])
86
assert all(x.min(0) == matrix([[1,2,3]]))
87
assert all(x.min(1) == matrix([[1],[4]]))
90
x = np.arange(4).reshape((2,2))
92
assert all(x.ptp(0) == array([2, 2]))
93
assert all(x.ptp(1) == array([1, 1]))
96
x = np.arange(9).reshape((3,3))
97
mx = x.view(np.matrix)
98
assert_equal(x.var(ddof=0), mx.var(ddof=0))
99
assert_equal(x.var(ddof=1), mx.var(ddof=1))
101
def test_basic(self):
102
import numpy.linalg as linalg
107
assert allclose(linalg.inv(A), mA.I)
108
assert all(array(transpose(A) == mA.T))
109
assert all(array(transpose(A) == mA.H))
110
assert all(A == mA.A)
114
assert allclose(linalg.inv(B), mB.I)
115
assert all(array(transpose(B) == mB.T))
116
assert all(array(conjugate(transpose(B)) == mB.H))
119
x = matrix(arange(6).reshape(2,3))
120
xpinv = matrix([[-0.77777778, 0.27777778],
121
[-0.11111111, 0.11111111],
122
[ 0.55555556, -0.05555556]])
123
assert_almost_equal(x.I, xpinv)
125
def test_comparisons(self):
126
A = arange(100).reshape(10,10)
129
assert all(mB == A+0.1)
130
assert all(mB == matrix(A+0.1))
131
assert not any(mB == matrix(A-0.1))
135
assert not any(mA < mA)
137
assert not any(mB < mA)
140
assert not any(mB > mB)
143
assert not any(mA == mB)
146
assert not all(abs(mA) > 0)
147
assert all(abs(mB > 0))
149
def test_asmatrix(self):
150
A = arange(100).reshape(10,10)
153
assert A[0,0] == mA[0,0]
155
def test_noaxis(self):
156
A = matrix([[1,0],[0,1]])
157
assert A.sum() == matrix(2)
158
assert A.mean() == matrix(0.5)
161
A = matrix([[1,0],[0,1]])
162
assert repr(A) == "matrix([[1, 0],\n [0, 1]])"
164
class TestCasting(TestCase):
165
def test_basic(self):
166
A = arange(100).reshape(10,10)
170
O = ones((10,10), float64) * 0.1
172
assert mB.dtype.type == float64
174
assert all(mB == mA+0.1)
177
O = ones((10,10), complex128)
179
assert mC.dtype.type == complex128
183
class TestAlgebra(TestCase):
184
def test_basic(self):
185
import numpy.linalg as linalg
193
assert allclose((mA ** i).A, B)
199
assert allclose((mA ** -i).A, B)
202
assert allclose((mA * mA).A, dot(A, A))
203
assert allclose((mA + mA).A, (A + A))
204
assert allclose((3*mA).A, (3*A))
208
assert allclose(mA2.A, 3*A)
211
"""Test raising a matrix to an integer power works as expected."""
212
m = matrix("1. 2.; 3. 4.")
219
assert_array_almost_equal(m2, m**2)
220
assert_array_almost_equal(m4, np.dot(m2, m2))
221
assert_array_almost_equal(np.dot(mi, m), np.eye(2))
223
def test_notimplemented(self):
224
'''Check that 'not implemented' operations produce a failure.'''
225
A = matrix([[1., 2.],
234
self.fail("matrix.__rpow__ doesn't raise a TypeError")
236
# __mul__ with something not a list, ndarray, tuple, or scalar
242
self.fail("matrix.__mul__ with non-numeric object doesn't raise"
245
class TestMatrixReturn(TestCase):
246
def test_instance_methods(self):
247
a = matrix([1.0], dtype='f8')
249
'astype' : ('intc',),
257
'argmin', 'choose', 'dump', 'dumps', 'fill', 'getfield',
258
'getA', 'getA1', 'item', 'nonzero', 'put', 'putmask', 'resize',
259
'searchsorted', 'setflags', 'setfield', 'sort', 'take',
260
'tofile', 'tolist', 'tostring', 'all', 'any', 'sum',
261
'argmax', 'argmin', 'min', 'max', 'mean', 'var', 'ptp',
262
'prod', 'std', 'ctypes', 'itemset'
264
for attrib in dir(a):
265
if attrib.startswith('_') or attrib in excluded_methods:
267
f = eval('a.%s' % attrib)
269
# reset contents of a
272
if attrib in methodargs:
273
args = methodargs[attrib]
277
assert type(b) is matrix, "%s" % attrib
278
assert type(a.real) is matrix
279
assert type(a.imag) is matrix
280
c,d = matrix([0.0]).nonzero()
281
assert type(c) is matrix
282
assert type(d) is matrix
285
class TestIndexing(TestCase):
286
def test_basic(self):
287
x = asmatrix(zeros((3,2),float))
288
y = zeros((3,1),float)
289
y[:,0] = [0.8,0.2,0.3]
291
assert_equal(x, [[0,1],[0,0],[0,0]])
294
class TestNewScalarIndexing(TestCase):
296
self.a = matrix([[1, 2],[3,4]])
298
def test_dimesions(self):
301
assert_equal(x.ndim, 2)
303
def test_array_from_matrix_list(self):
306
assert_equal(x.shape, [2,2,2])
308
def test_array_to_list(self):
310
assert_equal(a.tolist(),[[1, 2], [3, 4]])
312
def test_fancy_indexing(self):
315
assert isinstance(x, matrix)
316
assert_equal(x, matrix([[3, 4, 3]]))
318
assert isinstance(x, matrix)
319
assert_equal(x, matrix([[3, 4], [1, 2]]))
320
x = a[[[1],[0]],[[1,0],[0,1]]]
321
assert isinstance(x, matrix)
322
assert_equal(x, matrix([[4, 3], [1, 2]]))
324
def test_matrix_element(self):
325
x = matrix([[1,2,3],[4,5,6]])
326
assert_equal(x[0][0],matrix([[1,2,3]]))
327
assert_equal(x[0][0].shape,(1,3))
328
assert_equal(x[0].shape,(1,3))
329
assert_equal(x[:,0].shape,(2,1))
332
assert_equal(x[0,0],0)
334
assert_equal(x[:,0].shape,x.shape)
336
def test_scalar_indexing(self):
337
x = asmatrix(zeros((3,2),float))
338
assert_equal(x[0,0],x[0][0])
340
def test_row_column_indexing(self):
341
x = asmatrix(np.eye(2))
342
assert_array_equal(x[0,:],[[1,0]])
343
assert_array_equal(x[1,:],[[0,1]])
344
assert_array_equal(x[:,0],[[1],[0]])
345
assert_array_equal(x[:,1],[[0],[1]])
347
def test_boolean_indexing(self):
351
assert_array_equal(x[:,array([True,False])],x[:,0])
352
assert_array_equal(x[array([True,False,False]),:],x[0,:])
354
def test_list_indexing(self):
358
assert_array_equal(x[:,[1,0]],x[:,::-1])
359
assert_array_equal(x[[2,1,0],:],x[::-1,:])
362
if __name__ == "__main__":