Example #1
0
 def testOperators (self):
     "Test the operators +, -, *, /, %, ^, &, |"
     x = Numeric.array([1.,2.,3.,4.,5.,6.])
     y = Numeric.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 = Numeric.array([1.,2.,3.,4.,5.,6.])
     xc += y
     assert_eq(xc, x + y)
     xc = Numeric.array([1.,2.,3.,4.,5.,6.])
     xc -= y
     assert_eq(xc, x - y)
     xc = Numeric.array([1.,2.,3.,4.,5.,6.])
     xc *= y
     assert_eq(xc, x * y)
     yc = Numeric.array(y)
     yc /= x
     assert_eq( yc, y / x)
     assert_eq(x + y, Numeric.add(x, y))
     assert_eq(x - y, Numeric.subtract(x, y))
     assert_eq(x * y, Numeric.multiply(x, y))
     assert_eq(y / x, Numeric.divide (y, x))
     self.failUnlessRaises(ZeroDivisionError, Numeric.divide, 
                           Numeric.array(1), Numeric.array(0))
     assert_eq(x**2, Numeric.power(x,2))
     x = Numeric.array([1,2])
     y = Numeric.zeros((2,))
     assert_eq(x%x, y)
     assert_eq(Numeric.remainder(x,x), y)
     assert_eq(x <<1, [2,4])
     assert_eq(Numeric.left_shift(x,1), [2,4])
     assert_eq(x >>1, [0,1])
     assert_eq(Numeric.right_shift(x,1), [0,1])
     assert_eq(x & 2, [0,2])
     assert_eq(Numeric.bitwise_and (x, 2), [0,2])
     assert_eq(x | 1, [1,3])
     assert_eq(Numeric.bitwise_or (x, 1), [1,3])
     assert_eq(x ^ 2, [3,0])
     assert_eq(Numeric.bitwise_xor(x,2), [3,0])
     x = divmod(Numeric.array([2,1]), Numeric.array([1,2]))
     assert_eq(x[0], [2,0])
     assert_eq(x[1], [0,1])
     assert (4L*Numeric.arange(3)).typecode() == Numeric.PyObject
     x = Numeric.array([1,2,3,4,5,6],'u')
     y = Numeric.array([1,2,0,2,2,3],'u')
     assert_eq(x + y, [2, 4, 3, 6, 7, 9])
     assert_eq(x - y, [0, 0, 3, 2, 3, 3])
     assert_eq(x * y, [1, 4, 0, 8, 10, 18])
     assert_eq(y / x, [1, 1, 0, 0, 0, 0])
     assert_eq(y // x, [1, 1, 0, 0, 0, 0])
     assert_eq(x**2, [1, 4, 9, 16, 25, 36])