示例#1
0
 def testArange(self):
     "Test arange"
     x = MA.arange(5)
     assert eq(x, [0,1,2,3,4])
     x = MA.arange(4, 6)
     assert eq(x, [4,5])
     x = MA.arange(0, 1.5, .5)
     assert eq (x, [0.0, 0.5, 1.0])
     assert len(MA.arange(5,3,2)) == 0
     x = MA.arange(3L)
     assert x[1] == 1L
     assert x.typecode() == MA.PyObject
示例#2
0
 def testDotOuter (self):
     "test the dot product and outer product"
     assert MA.dot(self.a, self.a) == 55
     assert eq (MA.add.outer(self.a[:3], self.a[:3]),
                [[0,1,2],[1,2,3],[2,3,4]])
     assert eq (MA.outerproduct(self.a[:3], self.a[:3]),
                [[0,0,0],[0,1,2],[0,2,4]])
     a = MA.arange(4)
     b = MA.arange(3)
     c = MA.zeros((4,3))
     c[0] = a[0]*b
     c[1] = a[1]*b
     c[2] = a[2]*b
     c[3] = a[3]*b
     assert eq(c, MA.outerproduct(a,b))
示例#3
0
    def testIndexing (self):
        'Test indexing operations.'
        x = MA.array([0,1,2,3,4,5])
        for i in range(len(x)):
            assert i == x[i]
        x[2] = 20
        assert eq(x, x[...])
        w = MA.array([None])
        assert w.typecode() == MA.PyObject
        assert w[0] is None
        assert isinstance(x[2], types.IntType)
        assert x[2] == 20
        x = MA.array([0,1,2,3,4,5,6])
        assert eq (x[4:1:-1], [4,3,2])
        assert eq(x[4:1:-2], [4,2])
        assert eq(x[::-1], [6, 5,4,3,2,1,0])
        assert eq(x[2:-1], [2,3,4,5])
        m = MA.array([[1,2,3],[11,12,13]])
        assert m[0,2] == 3
        assert isinstance(m[0,2], types.IntType)
        assert eq(m[...,1], [2,12])
        assert eq(MA.arange(6)[..., MA.NewAxis], [[0],[1],[2],[3],[4],[5]])
        x = MA.array([1,2,3])
        y = MA.array(x)
        x[0] == 66
        assert y[0] != 66
        b=MA.array([[1,2,3,4],[5,6,7,8]]*2)
#        assert b[1:1].shape == (0,4)
#        assert b[1:1, :].shape == (0,4)
#        assert b[10:].shape == (0,4)
        assert eq(b[2:10], [[1,2,3,4],[5,6,7,8]])
        assert eq(b[2:10, ...], [[1,2,3,4],[5,6,7,8]])
示例#4
0
 def testComplex (self):
     "Test complex numbers"
     y = MA.arange(5) * 1.0
     x = y + y[::-1] * 1.0j
     assert x.typecode() == MA.Complex
     assert eq(x.real, y)
     assert eq(x.imag, y[::-1])
示例#5
0
 def testReshape (self):
     "test reshape"
     w = MA.reshape(self.m, (-1,2))
     assert eq(w, [[1,2],[3,11],[12,13]])
     w = MA.reshape (self.a, (2,3))
     assert eq(w, [[0, 1, 2], [3, 4, 5]])
     self.failUnlessRaises(ValueError, MA.reshape, MA.arange(5), (3,2))
     self.failUnlessRaises(ValueError, MA.reshape, self.a, (4,-1))
     self.failUnlessRaises(ValueError, MA.reshape, self.a, (5,6))
示例#6
0
 def testPickle (self):
     "Test pickling of MA arrays."
     import pickle
     x = MA.arange(10)
     fpik = open('foo.pik', 'wb')
     pickle.dump(x, fpik, 0)
     fpik.close()
     fpik = open('foo.pik', 'rb')
     y = pickle.load(open('foo.pik', 'rb'))
     fpik.close()
     assert eq(y,[0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
     assert MA.alltrue(MA.equal(x,y))
     assert MA.sometrue(MA.equal(x,3))
     assert y.shape == (10,)
示例#7
0
    def testOperators (self):
        "Test the operators +, -, *, /, %, ^, &, |"
        x = MA.array([1.,2.,3.,4.,5.,6.])
        y = MA.array([-1.,2.,0.,2.,-1, 3.])
        assert eq(x + y, [0., 4., 3., 6., 4., 9.])
        assert eq(x - y, [2., 0., 3., 2., 6., 3.])
        assert eq(x * y, [-1., 4., 0., 8., -5., 18.])
        assert eq(y / x, [-1, 1., 0., .5, -.2, .5])
        assert eq(x**2, [1., 4., 9., 16., 25., 36.])
        xc = MA.array([1.,2.,3.,4.,5.,6.])
        xc += y
        assert eq(xc, x + y)
        xc = MA.array([1.,2.,3.,4.,5.,6.])
        xc -= y
        assert eq(xc, x - y)
        yc = MA.array(y, copy=1)
        yc /= x
        assert eq ( yc, y / x)
        xc = MA.array([1.,2.,3.,4.,5.,6.])
        y1 = [-1.,2.,0.,2.,-1, 3.]
        xc *= y1
        assert eq(xc, x * y1)

        assert eq (x + y, MA.add(x, y))
        assert eq (x - y, MA.subtract(x, y))
        assert eq (x * y, MA.multiply(x, y))
        assert eq (y / x, MA.divide (y, x))
        d = x / y
        assert d[2] is MA.masked 
        assert (MA.array(1) / MA.array(0)) is MA.masked
        assert eq (x**2, MA.power(x,2))
        x = MA.array([1,2])
        y = MA.zeros((2,))
        assert eq (x%x, y)
        assert eq (MA.remainder(x,x), y)
        assert eq (x <<1, [2,4])
        assert eq (MA.left_shift(x,1), [2,4])
        assert eq (x >>1, [0,1])
        assert eq (MA.right_shift(x,1), [0,1])
        assert eq (x & 2, [0,2])
        assert eq (MA.bitwise_and (x, 2), [0,2])
        assert eq (x | 1, [1,3])
        assert eq (MA.bitwise_or (x, 1), [1,3])
        assert eq (x ^ 2, [3,0])
        assert eq (MA.bitwise_xor(x,2), [3,0])
#        x = divmod(MA.array([2,1]), MA.array([1,2]))
#        assert eq (x[0], [2,0])
#        assert eq (x[1], [0,1])
        assert (4L*MA.arange(3)).typecode() == MA.PyObject
示例#8
0
 def testReductions (self):
     "Tests of reduce attribute."
     a = MA.arange(6)
     m = MA.array([[1,2,3],[11,12,13]])
     assert MA.add.reduce(a) == 15
     assert MA.multiply.reduce(m.shape) == len(m.flat)
     assert eq(MA.add.reduce (m, 0), [12,14,16])
     assert eq(MA.add.reduce (m, -1), [6,36])
     assert eq(MA.multiply.reduce (m, 0), [11,24,39])
     assert eq(MA.multiply.reduce (m, -1), [6,11*12*13])
     assert MA.add.reduce([1]) == 1
     assert MA.add.reduce([]) == 0
     assert MA.multiply.reduce([]) == 1
     assert MA.minimum.reduce(a) == 0
     assert MA.maximum.reduce(a) == 5
示例#9
0
 def setUp (self):
     self.a = .01 + MA.arange(6) / 8.0 
     self.m = MA.array([[1,2,3],[11,12,13]]) / 16.0
示例#10
0
 def testPut (self):
     "test put and putmask"
     x=MA.arange(5)
     MA.put (x, [1,4],[10,40])
     assert eq(x, [0,10,2,3,40])
     
     x=MA.arange(5) * 1.0
     MA.put (x, [1,4],[10.,40.])
     assert eq(x, [0.,10.,2.,3.,40.])
     
     x=MA.arange(5) 
     MA.put (x, [1,4],[10])
     assert eq(x, [0,10,2,3,10])
     
     x=MA.arange(5) 
     MA.put (x, [1,4],10)
     assert eq(x, [0,10,2,3,10])
     
     x=MA.arange(5) 
     MA.put (x, [0,1,2,3], [10,20])
     assert eq(x, [10,20,10,20,4])
     
     x=MA.arange(5) 
     MA.put (x, [[0,1],[2,3]], [10,20])
     assert eq(x, [10,20,10,20,4])
     
     x = MA.arange(5).astype(MA.Float32)
     MA.put (x, [1,4],[10.,40.])
     assert eq(x, [0,10,2,3,40])
     
     x=MA.arange(6)*1.0
     x.shape=(2,3)
     MA.put(x, [1,4],[10,40])
     assert eq(x, [[0,10,2],[3,40,5]])
     
     x=MA.arange(5)
     MA.putmask (x, [1,0,1,0,1], [-1,10,20,30,40]) 
     assert eq(x, [-1,1,20,3,40])
     
     x=MA.arange(10)
     MA.putmask(x, MA.ones(10), 5)
     assert eq(x, 5*MA.ones(10))
     
     x=MA.arange(10)*1.0
     x=x.astype(MA.Float32)
     MA.putmask(x, [0,0,1,0,0,0,0,0,0,1], 3.0)
     assert eq(x, [0.,1.,3.,3.,4.,5.,6.,7.,8.,3.])
     
     x=MA.zeros((10,),MA.PyObject)
     MA.putmask(x, [0,0,1,0,0,0,1,0,0,1], 0.0)
     assert x[2] == 0.
     assert x[1] == 0
     
     x=MA.zeros((5,2),MA.PyObject)
     m=MA.zeros((5,2), MA.Int)
     m[3,1] = 1
     m[2,0] = 1
     MA.putmask(x, m, 0.0)
     assert x[3,1] == 0.0
     assert x[2,0] == 0
示例#11
0
 def setUp (self):
     self.a = MA.arange(6)
     self.m = MA.array([[1,2,3],[11,12,13]])