00001
00002
00003
00004
00005
00006
00007
00008 import ndarray_mod
00009 import unittest
00010 import numpy
00011
00012 class TestNdarray(unittest.TestCase):
00013
00014 def testNdzeros(self):
00015 for dtp in (numpy.int16, numpy.int32, numpy.float32, numpy.complex128):
00016 v = numpy.zeros(60, dtype=dtp)
00017 dt = numpy.dtype(dtp)
00018 for shape in ((60,),(6,10),(4,3,5),(2,2,3,5)):
00019 a1 = ndarray_mod.zeros(shape,dt)
00020 a2 = v.reshape(a1.shape)
00021 self.assertEqual(shape,a1.shape)
00022 self.assert_((a1 == a2).all())
00023
00024 def testNdzeros_matrix(self):
00025 for dtp in (numpy.int16, numpy.int32, numpy.float32, numpy.complex128):
00026 dt = numpy.dtype(dtp)
00027 shape = (6, 10)
00028 a1 = ndarray_mod.zeros_matrix(shape, dt)
00029 a2 = numpy.matrix(numpy.zeros(shape, dtype=dtp))
00030 self.assertEqual(shape,a1.shape)
00031 self.assert_((a1 == a2).all())
00032 self.assertEqual(type(a1), type(a2))
00033
00034 def testNdarray(self):
00035 a = range(0,60)
00036 for dtp in (numpy.int16, numpy.int32, numpy.float32, numpy.complex128):
00037 v = numpy.array(a, dtype=dtp)
00038 dt = numpy.dtype(dtp)
00039 a1 = ndarray_mod.array(a)
00040 a2 = ndarray_mod.array(a,dt)
00041 self.assert_((a1 == v).all())
00042 self.assert_((a2 == v).all())
00043 for shape in ((60,),(6,10),(4,3,5),(2,2,3,5)):
00044 a1 = a1.reshape(shape)
00045 self.assertEqual(shape,a1.shape)
00046 a2 = a2.reshape(shape)
00047 self.assertEqual(shape,a2.shape)
00048
00049 def testNdempty(self):
00050 for dtp in (numpy.int16, numpy.int32, numpy.float32, numpy.complex128):
00051 dt = numpy.dtype(dtp)
00052 for shape in ((60,),(6,10),(4,3,5),(2,2,3,5)):
00053 a1 = ndarray_mod.empty(shape,dt)
00054 a2 = ndarray_mod.c_empty(shape,dt)
00055 self.assertEqual(shape,a1.shape)
00056 self.assertEqual(shape,a2.shape)
00057
00058 def testTranspose(self):
00059 for dtp in (numpy.int16, numpy.int32, numpy.float32, numpy.complex128):
00060 dt = numpy.dtype(dtp)
00061 for shape in ((6,10),(4,3,5),(2,2,3,5)):
00062 a1 = numpy.empty(shape,dt)
00063 a2 = a1.transpose()
00064 a1 = ndarray_mod.transpose(a1)
00065 self.assertEqual(a1.shape,a2.shape)
00066
00067 def testSqueeze(self):
00068 a1 = numpy.array([[[3,4,5]]])
00069 a2 = a1.squeeze()
00070 a1 = ndarray_mod.squeeze(a1)
00071 self.assertEqual(a1.shape,a2.shape)
00072
00073 def testReshape(self):
00074 a1 = numpy.empty((2,2))
00075 a2 = ndarray_mod.reshape(a1,(1,4))
00076 self.assertEqual(a2.shape,(1,4))
00077
00078 if __name__=="__main__":
00079 unittest.main()