diff options
Diffstat (limited to 'numpy/base/tests')
| -rw-r--r-- | numpy/base/tests/test_function_base.py | 338 | ||||
| -rw-r--r-- | numpy/base/tests/test_getlimits.py | 38 | ||||
| -rw-r--r-- | numpy/base/tests/test_index_tricks.py | 53 | ||||
| -rw-r--r-- | numpy/base/tests/test_ma.py | 637 | ||||
| -rw-r--r-- | numpy/base/tests/test_matrix.py | 117 | ||||
| -rw-r--r-- | numpy/base/tests/test_polynomial.py | 83 | ||||
| -rw-r--r-- | numpy/base/tests/test_records.py | 44 | ||||
| -rw-r--r-- | numpy/base/tests/test_shape_base.py | 364 | ||||
| -rw-r--r-- | numpy/base/tests/test_twodim_base.py | 134 | ||||
| -rw-r--r-- | numpy/base/tests/test_type_check.py | 238 | ||||
| -rw-r--r-- | numpy/base/tests/test_ufunclike.py | 63 | ||||
| -rw-r--r-- | numpy/base/tests/test_umath.py | 18 | ||||
| -rw-r--r-- | numpy/base/tests/testdata.fits | bin | 0 -> 8640 bytes |
13 files changed, 2127 insertions, 0 deletions
diff --git a/numpy/base/tests/test_function_base.py b/numpy/base/tests/test_function_base.py new file mode 100644 index 000000000..fafd75eef --- /dev/null +++ b/numpy/base/tests/test_function_base.py @@ -0,0 +1,338 @@ + +import sys + +from scipy.testing import * +set_package_path() +import scipy.base;reload(scipy.base) +from scipy.base import * +del sys.path[0] + +class test_any(ScipyTestCase): + def check_basic(self): + y1 = [0,0,1,0] + y2 = [0,0,0,0] + y3 = [1,0,1,0] + assert(any(y1)) + assert(any(y3)) + assert(not any(y2)) + + def check_nd(self): + y1 = [[0,0,0],[0,1,0],[1,1,0]] + assert(any(y1)) + assert_array_equal(sometrue(y1),[1,1,0]) + assert_array_equal(sometrue(y1,axis=1),[0,1,1]) + +class test_all(ScipyTestCase): + def check_basic(self): + y1 = [0,1,1,0] + y2 = [0,0,0,0] + y3 = [1,1,1,1] + assert(not all(y1)) + assert(all(y3)) + assert(not all(y2)) + assert(all(~array(y2))) + + def check_nd(self): + y1 = [[0,0,1],[0,1,1],[1,1,1]] + assert(not all(y1)) + assert_array_equal(alltrue(y1),[0,0,1]) + assert_array_equal(alltrue(y1,axis=1),[0,0,1]) + +class test_average(ScipyTestCase): + def check_basic(self): + y1 = array([1,2,3]) + assert(average(y1) == 2.) + y2 = array([1.,2.,3.]) + assert(average(y2) == 2.) + y3 = [0.,0.,0.] + assert(average(y3) == 0.) + + y4 = ones((4,4)) + y4[0,1] = 0 + y4[1,0] = 2 + assert_array_equal(y4.mean(0), average(y4, 0)) + assert_array_equal(y4.mean(1), average(y4, 1)) + + y5 = rand(5,5) + assert_array_equal(y5.mean(0), average(y5, 0)) + assert_array_equal(y5.mean(1), average(y5, 1)) + +class test_logspace(ScipyTestCase): + def check_basic(self): + y = logspace(0,6) + assert(len(y)==50) + y = logspace(0,6,num=100) + assert(y[-1] == 10**6) + y = logspace(0,6,endpoint=0) + assert(y[-1] < 10**6) + y = logspace(0,6,num=7) + assert_array_equal(y,[1,10,100,1e3,1e4,1e5,1e6]) + +class test_linspace(ScipyTestCase): + def check_basic(self): + y = linspace(0,10) + assert(len(y)==50) + y = linspace(2,10,num=100) + assert(y[-1] == 10) + y = linspace(2,10,endpoint=0) + assert(y[-1] < 10) + y,st = linspace(2,10,retstep=1) + assert_almost_equal(st,8/49.0) + assert_array_almost_equal(y,mgrid[2:10:50j],13) + + def check_corner(self): + y = list(linspace(0,1,1)) + assert y == [0.0], y + y = list(linspace(0,1,2.5)) + assert y == [0.0, 1.0] + +class test_amax(ScipyTestCase): + def check_basic(self): + a = [3,4,5,10,-3,-5,6.0] + assert_equal(amax(a),10.0) + b = [[3,6.0, 9.0], + [4,10.0,5.0], + [8,3.0,2.0]] + assert_equal(amax(b,axis=0),[8.0,10.0,9.0]) + assert_equal(amax(b,axis=1),[9.0,10.0,8.0]) + +class test_amin(ScipyTestCase): + def check_basic(self): + a = [3,4,5,10,-3,-5,6.0] + assert_equal(amin(a),-5.0) + b = [[3,6.0, 9.0], + [4,10.0,5.0], + [8,3.0,2.0]] + assert_equal(amin(b,axis=0),[3.0,3.0,2.0]) + assert_equal(amin(b,axis=1),[3.0,4.0,2.0]) + +class test_ptp(ScipyTestCase): + def check_basic(self): + a = [3,4,5,10,-3,-5,6.0] + assert_equal(ptp(a),15.0) + b = [[3,6.0, 9.0], + [4,10.0,5.0], + [8,3.0,2.0]] + assert_equal(ptp(b,axis=0),[5.0,7.0,7.0]) + assert_equal(ptp(b,axis=-1),[6.0,6.0,6.0]) + +class test_cumsum(ScipyTestCase): + def check_basic(self): + ba = [1,2,10,11,6,5,4] + ba2 = [[1,2,3,4],[5,6,7,9],[10,3,4,5]] + for ctype in [int8,uint8,int16,uint16,int32,uint32, + float32,float64,complex64,complex128]: + a = array(ba,ctype) + a2 = array(ba2,ctype) + assert_array_equal(cumsum(a), array([1,3,13,24,30,35,39],ctype)) + assert_array_equal(cumsum(a2,axis=0), array([[1,2,3,4],[6,8,10,13], + [16,11,14,18]],ctype)) + assert_array_equal(cumsum(a2,axis=1), + array([[1,3,6,10], + [5,11,18,27], + [10,13,17,22]],ctype)) + +class test_prod(ScipyTestCase): + def check_basic(self): + ba = [1,2,10,11,6,5,4] + ba2 = [[1,2,3,4],[5,6,7,9],[10,3,4,5]] + for ctype in [int16,uint16,int32,uint32, + float32,float64,complex64,complex128]: + a = array(ba,ctype) + a2 = array(ba2,ctype) + if ctype in ['1', 'b']: + self.failUnlessRaises(ArithmeticError, prod, a) + self.failUnlessRaises(ArithmeticError, prod, a2, 1) + self.failUnlessRaises(ArithmeticError, prod, a) + else: + assert_equal(prod(a),26400) + assert_array_equal(prod(a2,axis=0), + array([50,36,84,180],ctype)) + assert_array_equal(prod(a2,axis=-1),array([24, 1890, 600],ctype)) + +class test_cumprod(ScipyTestCase): + def check_basic(self): + ba = [1,2,10,11,6,5,4] + ba2 = [[1,2,3,4],[5,6,7,9],[10,3,4,5]] + for ctype in [int16,uint16,int32,uint32, + float32,float64,complex64,complex128]: + a = array(ba,ctype) + a2 = array(ba2,ctype) + if ctype in ['1', 'b']: + self.failUnlessRaises(ArithmeticError, cumprod, a) + self.failUnlessRaises(ArithmeticError, cumprod, a2, 1) + self.failUnlessRaises(ArithmeticError, cumprod, a) + else: + assert_array_equal(cumprod(a,axis=-1), + array([1, 2, 20, 220, + 1320, 6600, 26400],ctype)) + assert_array_equal(cumprod(a2,axis=0), + array([[ 1, 2, 3, 4], + [ 5, 12, 21, 36], + [50, 36, 84, 180]],ctype)) + assert_array_equal(cumprod(a2,axis=-1), + array([[ 1, 2, 6, 24], + [ 5, 30, 210, 1890], + [10, 30, 120, 600]],ctype)) + +class test_diff(ScipyTestCase): + def check_basic(self): + x = [1,4,6,7,12] + out = array([3,2,1,5]) + out2 = array([-1,-1,4]) + out3 = array([0,5]) + assert_array_equal(diff(x),out) + assert_array_equal(diff(x,n=2),out2) + assert_array_equal(diff(x,n=3),out3) + + def check_nd(self): + x = 20*rand(10,20,30) + out1 = x[:,:,1:] - x[:,:,:-1] + out2 = out1[:,:,1:] - out1[:,:,:-1] + out3 = x[1:,:,:] - x[:-1,:,:] + out4 = out3[1:,:,:] - out3[:-1,:,:] + assert_array_equal(diff(x),out1) + assert_array_equal(diff(x,n=2),out2) + assert_array_equal(diff(x,axis=0),out3) + assert_array_equal(diff(x,n=2,axis=0),out4) + +class test_angle(ScipyTestCase): + def check_basic(self): + x = [1+3j,sqrt(2)/2.0+1j*sqrt(2)/2,1,1j,-1,-1j,1-3j,-1+3j] + y = angle(x) + yo = [arctan(3.0/1.0),arctan(1.0),0,pi/2,pi,-pi/2.0, + -arctan(3.0/1.0),pi-arctan(3.0/1.0)] + z = angle(x,deg=1) + zo = array(yo)*180/pi + assert_array_almost_equal(y,yo,11) + assert_array_almost_equal(z,zo,11) + +class test_trim_zeros(ScipyTestCase): + """ only testing for integer splits. + """ + def check_basic(self): + a= array([0,0,1,2,3,4,0]) + res = trim_zeros(a) + assert_array_equal(res,array([1,2,3,4])) + def check_leading_skip(self): + a= array([0,0,1,0,2,3,4,0]) + res = trim_zeros(a) + assert_array_equal(res,array([1,0,2,3,4])) + def check_trailing_skip(self): + a= array([0,0,1,0,2,3,0,4,0]) + res = trim_zeros(a) + assert_array_equal(res,array([1,0,2,3,0,4])) + + +class test_extins(ScipyTestCase): + def check_basic(self): + a = array([1,3,2,1,2,3,3]) + b = extract(a>1,a) + assert_array_equal(b,[3,2,2,3,3]) + def check_insert(self): + a = array([1,4,3,2,5,8,7]) + insert(a,[0,1,0,1,0,1,0],[2,4,6]) + assert_array_equal(a,[1,2,3,4,5,6,7]) + def check_both(self): + a = rand(10) + mask = a > 0.5 + ac = a.copy() + c = extract(mask, a) + insert(a,mask,0) + insert(a,mask,c) + assert_array_equal(a,ac) + +class test_vectorize(ScipyTestCase): + def check_simple(self): + def addsubtract(a,b): + if a > b: + return a - b + else: + return a + b + f = vectorize(addsubtract) + r = f([0,3,6,9],[1,3,5,7]) + assert_array_equal(r,[1,6,1,2]) + def check_scalar(self): + def addsubtract(a,b): + if a > b: + return a - b + else: + return a + b + f = vectorize(addsubtract) + r = f([0,3,6,9],5) + assert_array_equal(r,[5,8,1,4]) + + + +class test_unwrap(ScipyTestCase): + def check_simple(self): + #check that unwrap removes jumps greather that 2*pi + assert_array_equal(unwrap([1,1+2*pi]),[1,1]) + #check that unwrap maintans continuity + assert(all(diff(unwrap(rand(10)*100))<pi)) + + +class test_filterwindows(ScipyTestCase): + def check_hanning(self): + #check symmetry + w=hanning(10) + assert_array_almost_equal(w,flipud(w),7) + #check known value + assert_almost_equal(sum(w),4.500,4) + + def check_hamming(self): + #check symmetry + w=hamming(10) + assert_array_almost_equal(w,flipud(w),7) + #check known value + assert_almost_equal(sum(w),4.9400,4) + + def check_bartlett(self): + #check symmetry + w=bartlett(10) + assert_array_almost_equal(w,flipud(w),7) + #check known value + assert_almost_equal(sum(w),4.4444,4) + + def check_blackman(self): + #check symmetry + w=blackman(10) + assert_array_almost_equal(w,flipud(w),7) + #check known value + assert_almost_equal(sum(w),3.7800,4) + + +class test_trapz(ScipyTestCase): + def check_simple(self): + r=trapz(exp(-1.0/2*(arange(-10,10,.1))**2)/sqrt(2*pi),dx=0.1) + #check integral of normal equals 1 + assert_almost_equal(sum(r),1,7) + +class test_sinc(ScipyTestCase): + def check_simple(self): + assert(sinc(0)==1) + w=sinc(linspace(-1,1,100)) + #check symmetry + assert_array_almost_equal(w,flipud(w),7) + +class test_histogram(ScipyTestCase): + def check_simple(self): + n=100 + v=rand(n) + (a,b)=histogram(v) + #check if the sum of the bins equals the number of samples + assert(sum(a)==n) + #check that the bin counts are evenly spaced when the data is from a linear function + (a,b)=histogram(linspace(0,10,100)) + assert(all(a==10)) + + + + + +def compare_results(res,desired): + for i in range(len(desired)): + assert_array_equal(res[i],desired[i]) + +if __name__ == "__main__": + ScipyTest('scipy.base.function_base').run() diff --git a/numpy/base/tests/test_getlimits.py b/numpy/base/tests/test_getlimits.py new file mode 100644 index 000000000..99a6f5160 --- /dev/null +++ b/numpy/base/tests/test_getlimits.py @@ -0,0 +1,38 @@ +""" Test functions for limits module. +""" + +from scipy.testing import * +set_package_path() +import scipy.base;reload(scipy.base) +from scipy.base.getlimits import finfo +from scipy import single,double,longdouble +restore_path() + +################################################## + +class test_python_float(ScipyTestCase): + def check_singleton(self): + ftype = finfo(float) + ftype2 = finfo(float) + assert_equal(id(ftype),id(ftype2)) + +class test_single(ScipyTestCase): + def check_singleton(self): + ftype = finfo(single) + ftype2 = finfo(single) + assert_equal(id(ftype),id(ftype2)) + +class test_double(ScipyTestCase): + def check_singleton(self): + ftype = finfo(double) + ftype2 = finfo(double) + assert_equal(id(ftype),id(ftype2)) + +class test_longdouble(ScipyTestCase): + def check_singleton(self,level=2): + ftype = finfo(longdouble) + ftype2 = finfo(longdouble) + assert_equal(id(ftype),id(ftype2)) + +if __name__ == "__main__": + ScipyTest().run() diff --git a/numpy/base/tests/test_index_tricks.py b/numpy/base/tests/test_index_tricks.py new file mode 100644 index 000000000..96e9dff84 --- /dev/null +++ b/numpy/base/tests/test_index_tricks.py @@ -0,0 +1,53 @@ + +from scipy.testing import * +set_package_path() +import scipy.base;reload(scipy.base) +from scipy.base import * +restore_path() + +class test_grid(ScipyTestCase): + def check_basic(self): + a = mgrid[-1:1:10j] + b = mgrid[-1:1:0.1] + assert(a.shape == (10,)) + assert(b.shape == (20,)) + assert(a[0] == -1) + assert_almost_equal(a[-1],1) + assert(b[0] == -1) + assert_almost_equal(b[1]-b[0],0.1,11) + assert_almost_equal(b[-1],b[0]+19*0.1,11) + assert_almost_equal(a[1]-a[0],2.0/9.0,11) + + def check_nd(self): + c = mgrid[-1:1:10j,-2:2:10j] + d = mgrid[-1:1:0.1,-2:2:0.2] + assert(c.shape == (2,10,10)) + assert(d.shape == (2,20,20)) + assert_array_equal(c[0][0,:],-ones(10,'d')) + assert_array_equal(c[1][:,0],-2*ones(10,'d')) + assert_array_almost_equal(c[0][-1,:],ones(10,'d'),11) + assert_array_almost_equal(c[1][:,-1],2*ones(10,'d'),11) + assert_array_almost_equal(d[0,1,:]-d[0,0,:], 0.1*ones(20,'d'),11) + assert_array_almost_equal(d[1,:,1]-d[1,:,0], 0.2*ones(20,'d'),11) + +class test_concatenator(ScipyTestCase): + def check_1d(self): + assert_array_equal(r_[1,2,3,4,5,6],array([1,2,3,4,5,6])) + b = ones(5) + c = r_[b,0,0,b] + assert_array_equal(c,[1,1,1,1,1,0,0,1,1,1,1,1]) + + def check_2d(self): + b = rand(5,5) + c = rand(5,5) + d = r_[b,c,'1'] # append columns + assert(d.shape == (5,10)) + assert_array_equal(d[:,:5],b) + assert_array_equal(d[:,5:],c) + d = r_[b,c] + assert(d.shape == (10,5)) + assert_array_equal(d[:5,:],b) + assert_array_equal(d[5:,:],c) + +if __name__ == "__main__": + ScipyTest().run() diff --git a/numpy/base/tests/test_ma.py b/numpy/base/tests/test_ma.py new file mode 100644 index 000000000..884a4a277 --- /dev/null +++ b/numpy/base/tests/test_ma.py @@ -0,0 +1,637 @@ +import scipy +import types, time +from scipy.base.ma import * +from scipy.testing import ScipyTestCase, ScipyTest +def eq(v,w): + result = allclose(v,w) + if not result: + print """Not eq: +%s +---- +%s"""% (str(v), str(w)) + return result + +class test_ma(ScipyTestCase): + def __init__(self, *args, **kwds): + ScipyTestCase.__init__(self, *args, **kwds) + self.setUp() + + def setUp (self): + x=scipy.array([1.,1.,1.,-2., pi/2.0, 4., 5., -10., 10., 1., 2., 3.]) + y=scipy.array([5.,0.,3., 2., -1., -4., 0., -10., 10., 1., 0., 3.]) + a10 = 10. + m1 = [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0] + m2 = [0, 0, 1, 0, 0, 1, 1, 0, 0, 0 ,0, 1] + xm = array(x, mask=m1) + ym = array(y, mask=m2) + z = scipy.array([-.5, 0., .5, .8]) + zm = array(z, mask=[0,1,0,0]) + xf = scipy.where(m1, 1.e+20, x) + s = x.shape + xm.set_fill_value(1.e+20) + self.d = (x, y, a10, m1, m2, xm, ym, z, zm, xf, s) + + def check_testBasic1d(self): + "Test of basic array creation and properties in 1 dimension." + (x, y, a10, m1, m2, xm, ym, z, zm, xf, s) = self.d + self.failIf(isMaskedArray(x)) + self.failUnless(isMaskedArray(xm)) + self.assertEqual(shape(xm), s) + self.assertEqual(xm.shape, s) + self.assertEqual(xm.dtype, x.dtype) + self.assertEqual(xm.dtypechar, x.dtypechar) + self.assertEqual( xm.size , reduce(lambda x,y:x*y, s)) + self.assertEqual(count(xm) , len(m1) - reduce(lambda x,y:x+y, m1)) + self.failUnless(eq(xm, xf)) + self.failUnless(eq(filled(xm, 1.e20), xf)) + self.failUnless(eq(x, xm)) + + def check_testBasic2d(self): + "Test of basic array creation and properties in 2 dimensions." + for s in [(4,3), (6,2)]: + (x, y, a10, m1, m2, xm, ym, z, zm, xf, s) = self.d + x.shape = s + y.shape = s + xm.shape = s + ym.shape = s + xf.shape = s + + self.failIf(isMaskedArray(x)) + self.failUnless(isMaskedArray(xm)) + self.assertEqual(shape(xm), s) + self.assertEqual(xm.shape, s) + self.assertEqual( xm.size , reduce(lambda x,y:x*y, s)) + self.assertEqual( count(xm) , len(m1) - reduce(lambda x,y:x+y, m1)) + self.failUnless(eq(xm, xf)) + self.failUnless(eq(filled(xm, 1.e20), xf)) + self.failUnless(eq(x, xm)) + self.setUp() + + def check_testArithmetic (self): + "Test of basic arithmetic." + (x, y, a10, m1, m2, xm, ym, z, zm, xf, s) = self.d + a2d = array([[1,2],[0,4]]) + a2dm = masked_array(a2d, [[0,0],[1,0]]) + self.failUnless(eq (a2d * a2d, a2d * a2dm)) + self.failUnless(eq (a2d + a2d, a2d + a2dm)) + self.failUnless(eq (a2d - a2d, a2d - a2dm)) + for s in [(12,), (4,3), (2,6)]: + x = x.reshape(s) + y = y.reshape(s) + xm = xm.reshape(s) + ym = ym.reshape(s) + xf = xf.reshape(s) + self.failUnless(eq(-x, -xm)) + self.failUnless(eq(x + y, xm + ym)) + self.failUnless(eq(x - y, xm - ym)) + self.failUnless(eq(x * y, xm * ym)) + self.failUnless(eq(x / y, xm / ym)) + self.failUnless(eq(a10 + y, a10 + ym)) + self.failUnless(eq(a10 - y, a10 - ym)) + self.failUnless(eq(a10 * y, a10 * ym)) + self.failUnless(eq(a10 / y, a10 / ym)) + self.failUnless(eq(x + a10, xm + a10)) + self.failUnless(eq(x - a10, xm - a10)) + self.failUnless(eq(x * a10, xm * a10)) + self.failUnless(eq(x / a10, xm / a10)) + self.failUnless(eq(x**2, xm**2)) + self.failUnless(eq(abs(x)**2.5, abs(xm) **2.5)) + self.failUnless(eq(x**y, xm**ym)) + self.failUnless(eq(scipy.add(x,y), add(xm, ym))) + self.failUnless(eq(scipy.subtract(x,y), subtract(xm, ym))) + self.failUnless(eq(scipy.multiply(x,y), multiply(xm, ym))) + self.failUnless(eq(scipy.divide(x,y), divide(xm, ym))) + + + def check_testUfuncs1 (self): + "Test various functions such as sin, cos." + (x, y, a10, m1, m2, xm, ym, z, zm, xf, s) = self.d + self.failUnless (eq(scipy.cos(x), cos(xm))) + self.failUnless (eq(scipy.cosh(x), cosh(xm))) + self.failUnless (eq(scipy.sin(x), sin(xm))) + self.failUnless (eq(scipy.sinh(x), sinh(xm))) + self.failUnless (eq(scipy.tan(x), tan(xm))) + self.failUnless (eq(scipy.tanh(x), tanh(xm))) + self.failUnless (eq(scipy.sqrt(abs(x)), sqrt(xm))) + self.failUnless (eq(scipy.log(abs(x)), log(xm))) + self.failUnless (eq(scipy.log10(abs(x)), log10(xm))) + self.failUnless (eq(scipy.exp(x), exp(xm))) + self.failUnless (eq(scipy.arcsin(z), arcsin(zm))) + self.failUnless (eq(scipy.arccos(z), arccos(zm))) + self.failUnless (eq(scipy.arctan(z), arctan(zm))) + self.failUnless (eq(scipy.arctan2(x, y), arctan2(xm, ym))) + self.failUnless (eq(scipy.absolute(x), absolute(xm))) + self.failUnless (eq(scipy.equal(x,y), equal(xm, ym))) + self.failUnless (eq(scipy.not_equal(x,y), not_equal(xm, ym))) + self.failUnless (eq(scipy.less(x,y), less(xm, ym))) + self.failUnless (eq(scipy.greater(x,y), greater(xm, ym))) + self.failUnless (eq(scipy.less_equal(x,y), less_equal(xm, ym))) + self.failUnless (eq(scipy.greater_equal(x,y), greater_equal(xm, ym))) + self.failUnless (eq(scipy.conjugate(x), conjugate(xm))) + self.failUnless (eq(scipy.concatenate((x,y)), concatenate((xm,ym)))) + self.failUnless (eq(scipy.concatenate((x,y)), concatenate((x,y)))) + self.failUnless (eq(scipy.concatenate((x,y)), concatenate((xm,y)))) + self.failUnless (eq(scipy.concatenate((x,y,x)), concatenate((x,ym,x)))) + + def check_xtestCount (self): + "Test count" + ott = array([0.,1.,2.,3.], mask=[1,0,0,0]) + self.failUnless( isinstance(count(ott), types.IntType)) + self.assertEqual(3, count(ott)) + self.assertEqual(1, count(1)) + self.failUnless (eq(0, array(1,mask=[1]))) + ott=ott.reshape((2,2)) + assert isMaskedArray(count(ott,0)) + assert isinstance(count(ott), types.IntType) + self.failUnless (eq(3, count(ott))) + assert getmask(count(ott,0)) is None + self.failUnless (eq([1,2],count(ott,0))) + + def check_testMinMax (self): + "Test minimum and maximum." + (x, y, a10, m1, m2, xm, ym, z, zm, xf, s) = self.d + xr = scipy.ravel(x) #max doesn't work if shaped + xmr = ravel(xm) + self.failUnless (eq(max(xr), maximum(xmr))) #true because of careful selection of data + self.failUnless (eq(min(xr), minimum(xmr))) #true because of careful selection of data + + def check_testAddSumProd (self): + "Test add, sum, product." + (x, y, a10, m1, m2, xm, ym, z, zm, xf, s) = self.d + self.failUnless (eq(scipy.add.reduce(x), add.reduce(x))) + self.failUnless (eq(scipy.add.accumulate(x), add.accumulate(x))) + self.failUnless (eq(4, sum(array(4)))) + self.failUnless (eq(4, sum(array(4), axis=0))) + self.failUnless (eq(scipy.sum(x), sum(x))) + self.failUnless (eq(scipy.sum(filled(xm,0)), sum(xm))) + self.failUnless (eq(scipy.sum(x,0), sum(x,0))) + self.failUnless (eq(scipy.product(x), product(x))) + self.failUnless (eq(scipy.product(x,0), product(x,0))) + self.failUnless (eq(scipy.product(filled(xm,1)), product(xm))) + if len(s) > 1: + self.failUnless (eq(scipy.concatenate((x,y),1), concatenate((xm,ym),1))) + self.failUnless (eq(scipy.add.reduce(x,1), add.reduce(x,1))) + self.failUnless (eq(scipy.sum(x,1), sum(x,1))) + self.failUnless (eq(scipy.product(x,1), product(x,1))) + + + def check_testCI(self): + "Test of conversions and indexing" + x1 = scipy.array([1,2,4,3]) + x2 = array(x1, mask = [1,0,0,0]) + x3 = array(x1, mask = [0,1,0,1]) + x4 = array(x1) + # test conversion to strings + junk, garbage = str(x2), repr(x2) + assert eq(scipy.sort(x1),sort(x2, fill_value=0)) + # tests of indexing + assert type(x2[1]) is type(x1[1]) + assert x1[1] == x2[1] + assert x2[0] is masked + assert eq(x1[2],x2[2]) + assert eq(x1[2:5],x2[2:5]) + assert eq(x1[:],x2[:]) + assert eq(x1[1:], x3[1:]) + x1[2]=9 + x2[2]=9 + assert eq(x1,x2) + x1[1:3] = 99 + x2[1:3] = 99 + assert eq(x1,x2) + x2[1] = masked + assert eq(x1,x2) + x2[1:3]=masked + assert eq(x1,x2) + x2[:] = x1 + x2[1] = masked + assert allequal(getmask(x2),array([0,1,0,0])) + x3[:] = masked_array([1,2,3,4],[0,1,1,0]) + assert allequal(getmask(x3), array([0,1,1,0])) + x4[:] = masked_array([1,2,3,4],[0,1,1,0]) + assert allequal(getmask(x4), array([0,1,1,0])) + assert allequal(x4, array([1,2,3,4])) + x1 = scipy.arange(5)*1.0 + x2 = masked_values(x1, 3.0) + assert eq(x1,x2) + assert allequal(array([0,0,0,1,0],MaskType), x2.mask) + assert eq(3.0, x2.fill_value()) + x1 = array([1,'hello',2,3],object) + x2 = scipy.array([1,'hello',2,3],object) + s1 = x1[1].item() + s2 = x2[1].item() + self.assertEqual(type(s2), str) + self.assertEqual(type(s1), str) + self.assertEqual(s1, s2) + assert x1[1:1].shape == (0,) + + def check_testCopySize(self): + "Tests of some subtle points of copying and sizing." + n = [0,0,1,0,0] + m = make_mask(n) + m2 = make_mask(m) + self.failUnless(m is m2) + m3 = make_mask(m, copy=1) + self.failUnless(m is not m3) + + x1 = scipy.arange(5) + y1 = array(x1, mask=m) + self.failUnless( y1.raw_data() is not x1) + self.failUnless( allequal(x1,y1.raw_data())) + self.failUnless( y1.mask is m) + + y1a = array(y1, copy=0) + self.failUnless( y1a.raw_data() is y1.raw_data()) + self.failUnless( y1a.mask is y1.mask) + + y2 = array(x1, mask=m, copy=0) + self.failUnless( y2.raw_data() is x1) + self.failUnless( y2.mask is m) + self.failUnless( y2[2] is masked) + y2[2]=9 + self.failUnless( y2[2] is not masked) + self.failUnless( y2.mask is not m) + self.failUnless( allequal(y2.mask, 0)) + + y3 = array(x1*1.0, mask=m) + self.failUnless(filled(y3).dtype is (x1*1.0).dtype) + + x4 = arange(4) + x4[2] = masked + y4 = resize(x4, (8,)) + self.failUnless( eq(concatenate([x4,x4]), y4)) + self.failUnless( eq(getmask(y4),[0,0,1,0,0,0,1,0])) + y5 = repeat(x4, (2,2,2,2)) + self.failUnless( eq(y5, [0,0,1,1,2,2,3,3])) + y6 = repeat(x4, 2) + self.failUnless( eq(y5, y6)) + + def check_testPut(self): + "Test of put" + d = arange(5) + n = [0,0,0,1,1] + m = make_mask(n) + x = array(d, mask = m) + self.failUnless( x[3] is masked) + self.failUnless( x[4] is masked) + x[[1,4]] = [10,40] + self.failUnless( x.mask is not m) + self.failUnless( x[3] is masked) + self.failUnless( x[4] is not masked) + self.failUnless( eq(x, [0,10,2,-1,40])) + + x = array(d, mask = m) + x.put([-1,100,200]) + self.failUnless( eq(x, [-1,100,200,0,0])) + self.failUnless( x[3] is masked) + self.failUnless( x[4] is masked) + + x = array(d, mask = m) + x.putmask([30,40]) + self.failUnless( eq(x, [0,1,2,30,40])) + self.failUnless( x.mask is None) + + x = array(d, mask = m) + y = x.compressed() + z = array(x, mask = m) + z.put(y) + assert eq (x, z) + + def check_testMaPut(self): + (x, y, a10, m1, m2, xm, ym, z, zm, xf, s) = self.d + m = [1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1] + i = scipy.nonzero(m) + putmask(xm, m, z) + assert take(xm, i) == z + put(ym, i, zm) + assert take(ym, i) == zm + + def check_testOddFeatures(self): + "Test of other odd features" + x = arange(20); x=x.reshape(4,5) + x.flat[5] = 12 + assert x[1,0] == 12 + z = x + 10j * x + assert eq(z.real, x) + assert eq(z.imag, 10*x) + assert eq((z*conjugate(z)).real, 101*x*x) + z.imag[...] = 0.0 + + x = arange(10) + x[3] = masked + assert str(x[3]) == str(masked) + c = x >= 8 + assert count(where(c,masked,masked)) == 0 + assert shape(where(c,masked,masked)) == c.shape + z = where(c , x, masked) + assert z.dtype is x.dtype + assert z[3] is masked + assert z[4] is masked + assert z[7] is masked + assert z[8] is not masked + assert z[9] is not masked + assert eq(x,z) + z = where(c , masked, x) + assert z.dtype is x.dtype + assert z[3] is masked + assert z[4] is not masked + assert z[7] is not masked + assert z[8] is masked + assert z[9] is masked + z = masked_where(c, x) + assert z.dtype is x.dtype + assert z[3] is masked + assert z[4] is not masked + assert z[7] is not masked + assert z[8] is masked + assert z[9] is masked + assert eq(x,z) + x = array([1.,2.,3.,4.,5.]) + c = array([1,1,1,0,0]) + x[2] = masked + z = where(c, x, -x) + assert eq(z, [1.,2.,0., -4., -5]) + c[0] = masked + z = where(c, x, -x) + assert eq(z, [1.,2.,0., -4., -5]) + assert z[0] is masked + assert z[1] is not masked + assert z[2] is masked + assert eq(masked_where(greater(x, 2), x), masked_greater(x,2)) + assert eq(masked_where(greater_equal(x, 2), x), masked_greater_equal(x,2)) + assert eq(masked_where(less(x, 2), x), masked_less(x,2)) + assert eq(masked_where(less_equal(x, 2), x), masked_less_equal(x,2)) + assert eq(masked_where(not_equal(x, 2), x), masked_not_equal(x,2)) + assert eq(masked_where(equal(x, 2), x), masked_equal(x,2)) + assert eq(masked_where(not_equal(x,2), x), masked_not_equal(x,2)) + assert eq(masked_inside(range(5), 1, 3), [0, 199, 199, 199, 4]) + assert eq(masked_outside(range(5), 1, 3),[199,1,2,3,199]) + assert eq(masked_inside(array(range(5), mask=[1,0,0,0,0]), 1, 3).mask, [1,1,1,1,0]) + assert eq(masked_outside(array(range(5), mask=[0,1,0,0,0]), 1, 3).mask, [1,1,0,0,1]) + assert eq(masked_equal(array(range(5), mask=[1,0,0,0,0]), 2).mask, [1,0,1,0,0]) + assert eq(masked_not_equal(array([2,2,1,2,1], mask=[1,0,0,0,0]), 2).mask, [1,0,1,0,1]) + assert eq(masked_where([1,1,0,0,0], [1,2,3,4,5]), [99,99,3,4,5]) + atest = ones((10,10,10), dtype=float32) + btest = zeros(atest.shape, MaskType) + ctest = masked_where(btest,atest) + assert eq(atest,ctest) + z = choose(c, (-x, x)) + assert eq(z, [1.,2.,0., -4., -5]) + assert z[0] is masked + assert z[1] is not masked + assert z[2] is masked + x = arange(6) + x[5] = masked + y = arange(6)*10 + y[2]= masked + c = array([1,1,1,0,0,0], mask=[1,0,0,0,0,0]) + cm = c.filled(1) + z = where(c,x,y) + zm = where(cm,x,y) + assert eq(z, zm) + assert getmask(zm) is None + assert eq(zm, [0,1,2,30,40,50]) + z = where(c, masked, 1) + assert eq(z, [99,99,99,1,1,1]) + z = where(c, 1, masked) + assert eq(z, [99, 1, 1, 99, 99, 99]) + + def check_testMinMax(self): + "Test of minumum, maximum." + assert eq(minimum([1,2,3],[4,0,9]), [1,0,3]) + assert eq(maximum([1,2,3],[4,0,9]), [4,2,9]) + x = arange(5) + y = arange(5) - 2 + x[3] = masked + y[0] = masked + assert eq(minimum(x,y), where(less(x,y), x, y)) + assert eq(maximum(x,y), where(greater(x,y), x, y)) + assert minimum(x) == 0 + assert maximum(x) == 4 + + def check_testTakeTransposeInnerOuter(self): + "Test of take, transpose, inner, outer products" + x = arange(24) + y = scipy.arange(24) + x[5:6] = masked + x=x.reshape(2,3,4) + y=y.reshape(2,3,4) + assert eq(scipy.transpose(y,(2,0,1)), transpose(x,(2,0,1))) + assert eq(scipy.take(y, (2,0,1), 1), take(x, (2,0,1), 1)) + assert eq(scipy.innerproduct(filled(x,0),filled(y,0)), + innerproduct(x, y)) + assert eq(scipy.outerproduct(filled(x,0),filled(y,0)), + outerproduct(x, y)) + y = array(['abc', 1, 'def', 2, 3], object) + y[2] = masked + t = take(y,[0,3,4]) + assert t[0].item() == 'abc' + assert t[1].item() == 2 + assert t[2].item() == 3 + + def check_testInplace(self): + """Test of inplace operations and rich comparisons""" + y = arange(10) + + x = arange(10) + xm = arange(10) + xm[2] = masked + x += 1 + assert eq(x, y+1) + xm += 1 + assert eq(x, y+1) + + x = arange(10) + xm = arange(10) + xm[2] = masked + x -= 1 + assert eq(x, y-1) + xm -= 1 + assert eq(xm, y-1) + + x = arange(10)*1.0 + xm = arange(10)*1.0 + xm[2] = masked + x *= 2.0 + assert eq(x, y*2) + xm *= 2.0 + assert eq(xm, y*2) + + x = arange(10)*2 + xm = arange(10) + xm[2] = masked + x /= 2 + assert eq(x, y) + xm /= 2 + assert eq(x, y) + + x = arange(10)*1.0 + xm = arange(10)*1.0 + xm[2] = masked + x /= 2.0 + assert eq(x, y/2.0) + xm /= arange(10) + assert eq(xm, ones((10,))) + + x = arange(10).astype(float32) + xm = arange(10) + xm[2] = masked + id1 = id(x.raw_data()) + x += 1. + assert id1 == id(x.raw_data()) + assert eq(x, y+1.) + + def check_testPickle(self): + "Test of pickling" + x = arange(12) + x[4:10:2] = masked + x=x.reshape(4,3) + f = open('test9.pik','wb') + import pickle + pickle.dump(x, f) + f.close() + f = open('test9.pik', 'rb') + y = pickle.load(f) + assert eq(x,y) + + def check_testMasked(self): + "Test of masked element" + xx=arange(6) + xx[1] = masked + self.failUnless(xx[1] is masked) + self.failUnlessRaises(Exception, lambda x,y: x+y, masked, masked) + self.failUnlessRaises(Exception, lambda x,y: x+y, masked, 2) + self.failUnlessRaises(Exception, lambda x,y: x+y, masked, xx) + self.failUnlessRaises(Exception, lambda x,y: x+y, xx, masked) + + def check_testAverage1(self): + "Test of average." + ott = array([0.,1.,2.,3.], mask=[1,0,0,0]) + self.failUnless(eq(2.0, average(ott))) + self.failUnless(eq(2.0, average(ott, weights=[1., 1., 2., 1.]))) + result, wts = average(ott, weights=[1.,1.,2.,1.], returned=1) + self.failUnless(eq(2.0, result)) + self.failUnless(wts == 4.0) + ott[:] = masked + self.failUnless(average(ott) is masked) + ott = array([0.,1.,2.,3.], mask=[1,0,0,0]) + ott=ott.reshape(2,2) + ott[:,1] = masked + self.failUnless(eq(average(ott), [2.0, 0.0])) + self.failUnless(average(ott,axis=1)[0] is masked) + self.failUnless(eq([2.,0.], average(ott))) + result, wts = average(ott, returned=1) + self.failUnless(eq(wts, [1., 0.])) + + def check_testAverage2(self): + "More tests of average." + w1 = [0,1,1,1,1,0] + w2 = [[0,1,1,1,1,0],[1,0,0,0,0,1]] + x=arange(6) + self.failUnless(allclose(average(x), 2.5)) + self.failUnless(allclose(average(x, weights=w1), 2.5)) + y=array([arange(6), 2.0*arange(6)]) + self.failUnless(allclose(average(y, None), scipy.add.reduce(scipy.arange(6))*3./12.)) + self.failUnless(allclose(average(y, axis=0), scipy.arange(6) * 3./2.)) + self.failUnless(allclose(average(y, axis=1), [average(x), average(x) * 2.0])) + self.failUnless(allclose(average(y, None, weights=w2), 20./6.)) + self.failUnless(allclose(average(y, axis=0, weights=w2), [0.,1.,2.,3.,4.,10.])) + self.failUnless(allclose(average(y, axis=1), [average(x), average(x) * 2.0])) + m1 = zeros(6) + m2 = [0,0,1,1,0,0] + m3 = [[0,0,1,1,0,0],[0,1,1,1,1,0]] + m4 = ones(6) + m5 = [0, 1, 1, 1, 1, 1] + self.failUnless(allclose(average(masked_array(x, m1)), 2.5)) + self.failUnless(allclose(average(masked_array(x, m2)), 2.5)) + self.failUnless(average(masked_array(x, m4)) is masked) + self.assertEqual(average(masked_array(x, m5)), 0.0) + self.assertEqual(count(average(masked_array(x, m4))), 0) + z = masked_array(y, m3) + self.failUnless(allclose(average(z, None), 20./6.)) + self.failUnless(allclose(average(z, axis=0), [0.,1.,99.,99.,4.0, 7.5])) + self.failUnless(allclose(average(z, axis=1), [2.5, 5.0])) + self.failUnless(allclose( average(z,weights=w2), [0.,1., 99., 99., 4.0, 10.0])) + + a = arange(6) + b = arange(6) * 3 + r1, w1 = average([[a,b],[b,a]], axis=1, returned=1) + self.assertEqual(shape(r1) , shape(w1)) + self.assertEqual(r1.shape , w1.shape) + r2, w2 = average(ones((2,2,3)), axis=0, weights=[3,1], returned=1) + self.assertEqual(shape(w2) , shape(r2)) + r2, w2 = average(ones((2,2,3)), returned=1) + self.assertEqual(shape(w2) , shape(r2)) + r2, w2 = average(ones((2,2,3)), weights=ones((2,2,3)), returned=1) + self.failUnless(shape(w2) == shape(r2)) + a2d = array([[1,2],[0,4]], float) + a2dm = masked_array(a2d, [[0,0],[1,0]]) + a2da = average(a2d) + self.failUnless(eq (a2da, [0.5, 3.0])) + a2dma = average(a2dm) + self.failUnless(eq( a2dma, [1.0, 3.0])) + a2dma = average(a2dm, axis=None) + self.failUnless(eq(a2dma, 7./3.)) + a2dma = average(a2dm, axis=1) + self.failUnless(eq(a2dma, [1.5, 4.0])) + + def check_testToPython(self): + self.assertEqual(1, int(array(1))) + self.assertEqual(1.0, float(array(1))) + self.assertEqual(1, int(array([[[1]]]))) + self.assertEqual(1.0, float(array([[1]]))) + self.failUnlessRaises(ValueError, float, array([1,1])) + self.failUnlessRaises(MAError, float, array([1],mask=[1])) + +def timingTest(): + for f in [testf, testinplace]: + for n in [1000,10000,50000]: + t = testta(n, f) + t1 = testtb(n, f) + t2 = testtc(n, f) + print f.test_name + print """\ +n = %7d +scipy time (ms) %6.1f +MA maskless ratio %6.1f +MA masked ratio %6.1f +""" % (n, t*1000.0, t1/t, t2/t) + +def testta(n, f): + x=scipy.arange(n) + 1.0 + tn0 = time.time() + z = f(x) + return time.time() - tn0 + +def testtb(n, f): + x=arange(n) + 1.0 + tn0 = time.time() + z = f(x) + return time.time() - tn0 + +def testtc(n, f): + x=arange(n) + 1.0 + x[0] = masked + tn0 = time.time() + z = f(x) + return time.time() - tn0 + +def testf(x): + for i in range(25): + y = x **2 + 2.0 * x - 1.0 + w = x **2 + 1.0 + z = (y / w) ** 2 + return z +testf.test_name = 'Simple arithmetic' + +def testinplace(x): + for i in range(25): + y = x**2 + y += 2.0*x + y -= 1.0 + y /= x + return y +testinplace.test_name = 'Inplace operations' + +if __name__ == "__main__": + ScipyTest('scipy.base.ma').run() + #timingTest() diff --git a/numpy/base/tests/test_matrix.py b/numpy/base/tests/test_matrix.py new file mode 100644 index 000000000..59b0a131e --- /dev/null +++ b/numpy/base/tests/test_matrix.py @@ -0,0 +1,117 @@ + +from scipy.testing import * +set_package_path() +import scipy.base;reload(scipy.base) +from scipy.base import * +restore_path() + +class test_ctor(ScipyTestCase): + def test_basic(self): + A = array([[1,2],[3,4]]) + mA = matrix(A) + assert all(mA.A == A) + + B = bmat("A,A;A,A") + C = bmat([[A,A], [A,A]]) + D = array([[1,2,1,2], + [3,4,3,4], + [1,2,1,2], + [3,4,3,4]]) + assert all(B.A == D) + assert all(C.A == D) + + vec = arange(5) + mvec = matrix(vec) + assert mvec.shape == (1,5) + +class test_properties(ScipyTestCase): + def test_basic(self): + import scipy.corelinalg as linalg + + A = array([[1., 2.], + [3., 4.]]) + mA = matrix(A) + assert allclose(linalg.inv(A), mA.I) + assert all(array(transpose(A) == mA.T)) + assert all(array(transpose(A) == mA.H)) + assert all(A == mA.A) + + B = A + 2j*A + mB = matrix(B) + assert allclose(linalg.inv(B), mB.I) + assert all(array(transpose(B) == mB.T)) + assert all(array(conjugate(transpose(B)) == mB.H)) + + def test_comparisons(self): + A = arange(100).reshape(10,10) + mA = matrix(A) + mB = matrix(A) + 0.1 + assert all(mB == A+0.1) + assert all(mB == matrix(A+0.1)) + assert not any(mB == matrix(A-0.1)) + assert all(mA < mB) + assert all(mA <= mB) + assert all(mA <= mA) + assert not any(mA < mA) + + assert not any(mB < mA) + assert all(mB >= mA) + assert all(mB >= mB) + assert not any(mB > mB) + + assert all(mA == mA) + assert not any(mA == mB) + assert all(mB != mA) + + assert not all(abs(mA) > 0) + assert all(abs(mB > 0)) + + def test_asmatrix(self): + A = arange(100).reshape(10,10) + mA = asmatrix(A) + mB = matrix(A) + A[0,0] = -10 + assert A[0,0] == mA[0,0] + assert A[0,0] != mB[0,0] + +class test_autocasting(ScipyTestCase): + def test_basic(self): + A = arange(100).reshape(10,10) + mA = matrix(A) + + mB = mA.copy() + O = ones((10,10), float64) * 0.1 + mB = mB + O + assert mB.dtype == float64 + assert all(mA != mB) + assert all(mB == mA+0.1) + + mC = mA.copy() + O = ones((10,10), complex128) + mC = mC * O + assert mC.dtype == complex128 + assert all(mA != mB) + +class test_algebra(ScipyTestCase): + def test_basic(self): + import scipy.corelinalg as linalg + + A = array([[1., 2.], + [3., 4.]]) + mA = matrix(A) + + B = identity(2) + for i in xrange(6): + assert allclose((mA ** i).A, B) + B = dot(B, A) + + Ainv = linalg.inv(A) + B = identity(2) + for i in xrange(6): + assert allclose((mA ** -i).A, B) + B = dot(B, Ainv) + + assert allclose((mA * mA).A, dot(A, A)) + assert allclose((mA + mA).A, (A + A)) + assert allclose((3*mA).A, (3*A)) + diff --git a/numpy/base/tests/test_polynomial.py b/numpy/base/tests/test_polynomial.py new file mode 100644 index 000000000..51d4b5707 --- /dev/null +++ b/numpy/base/tests/test_polynomial.py @@ -0,0 +1,83 @@ +""" +>>> import scipy.base as nx +>>> from scipy.base.polynomial import poly1d, polydiv + +>>> p = poly1d([1.,2,3]) +>>> p +poly1d([ 1., 2., 3.]) +>>> print p + 2 +1 x + 2 x + 3 +>>> q = poly1d([3.,2,1]) +>>> q +poly1d([ 3., 2., 1.]) +>>> print q + 2 +3 x + 2 x + 1 + +>>> p(0) +3.0 +>>> p(5) +38.0 +>>> q(0) +1.0 +>>> q(5) +86.0 + +>>> p * q +poly1d([ 3., 8., 14., 8., 3.]) +>>> p / q +(poly1d([ 0.33333333]), poly1d([ 1.33333333, 2.66666667])) +>>> p + q +poly1d([ 4., 4., 4.]) +>>> p - q +poly1d([-2., 0., 2.]) +>>> p ** 4 +poly1d([ 1., 8., 36., 104., 214., 312., 324., 216., 81.]) + +>>> p(q) +poly1d([ 9., 12., 16., 8., 6.]) +>>> q(p) +poly1d([ 3., 12., 32., 40., 34.]) + +>>> nx.asarray(p) +array([ 1., 2., 3.]) +>>> len(p) +2 + +>>> p[0], p[1], p[2], p[3] +(3.0, 2.0, 1.0, 0) + +>>> p.integ() +poly1d([ 0.33333333, 1. , 3. , 0. ]) +>>> p.integ(1) +poly1d([ 0.33333333, 1. , 3. , 0. ]) +>>> p.integ(5) +poly1d([ 0.00039683, 0.00277778, 0.025 , 0. , 0. , + 0. , 0. , 0. ]) +>>> p.deriv() +poly1d([ 2., 2.]) +>>> p.deriv(2) +poly1d([ 2.]) + +>>> q = poly1d([1.,2,3], variable='y') +>>> print q + 2 +1 y + 2 y + 3 +>>> q = poly1d([1.,2,3], variable='lambda') +>>> print q + 2 +1 lambda + 2 lambda + 3 + +>>> polydiv(poly1d([1,0,-1]), poly1d([1,1])) +(poly1d([ 1., -1.]), poly1d([ 0.])) +""" + +from scipy.testing import * + +import doctest +def test_suite(level=1): + return doctest.DocTestSuite() + +if __name__ == "__main__": + ScipyTest().run() diff --git a/numpy/base/tests/test_records.py b/numpy/base/tests/test_records.py new file mode 100644 index 000000000..8135a55a8 --- /dev/null +++ b/numpy/base/tests/test_records.py @@ -0,0 +1,44 @@ + +from scipy.testing import * +set_package_path() +import os as _os +import scipy.base;reload(scipy.base) +from scipy.base import * +from scipy.base import records as rec +restore_path() + +class test_fromrecords(ScipyTestCase): + def check_fromrecords(self): + r = rec.fromrecords([[456,'dbe',1.2],[2,'de',1.3]],names='col1,col2,col3') + assert_equal(r[0].item(),(456, 'dbe', 1.2)) + + def check_method_array(self): + r = rec.array('abcdefg'*100,formats='i2,a3,i4',shape=3,byteorder='big') + assert_equal(r[1].item(),(25444, 'efg', 1633837924)) + + def check_method_array2(self): + r=rec.array([(1,11,'a'),(2,22,'b'),(3,33,'c'),(4,44,'d'),(5,55,'ex'),(6,66,'f'),(7,77,'g')],formats='u1,f4,a1') + assert_equal(r[1].item(),(2, 22.0, 'b')) + + def check_recarray_slices(self): + r=rec.array([(1,11,'a'),(2,22,'b'),(3,33,'c'),(4,44,'d'),(5,55,'ex'),(6,66,'f'),(7,77,'g')],formats='u1,f4,a1') + assert_equal(r[1::2][1].item(),(4, 44.0, 'd')) + + def check_recarray_fromarrays(self): + x1 = array([1,2,3,4]) + x2 = array(['a','dd','xyz','12']) + x3 = array([1.1,2,3,4]) + r = rec.fromarrays([x1,x2,x3],names='a,b,c') + assert_equal(r[1].item(),(2,'dd',2.0)) + x1[1] = 34 + assert_equal(r.a,array([1,2,3,4])) + + def check_recarray_fromfile(self): + __path__ = _os.path.split(__file__) + filename = _os.path.join(__path__[0], "testdata.fits") + fd = open(filename) + fd.seek(2880*2) + r = rec.fromfile(fd, formats='f8,i4,a5', shape=3, byteorder='big') + +if __name__ == "__main__": + ScipyTest().run() diff --git a/numpy/base/tests/test_shape_base.py b/numpy/base/tests/test_shape_base.py new file mode 100644 index 000000000..005868e96 --- /dev/null +++ b/numpy/base/tests/test_shape_base.py @@ -0,0 +1,364 @@ + +from scipy.testing import * +set_package_path() +import scipy.base; +from scipy.base import * +restore_path() + +class test_apply_along_axis(ScipyTestCase): + def check_simple(self): + a = ones((20,10),'d') + assert_array_equal(apply_along_axis(len,0,a),len(a)*ones(shape(a)[1])) + def check_simple101(self,level=11): + # This test causes segmentation fault (Numeric 23.3,23.6,Python 2.3.4) + # when enabled and shape(a)[1]>100. See Issue 202. + a = ones((10,101),'d') + assert_array_equal(apply_along_axis(len,0,a),len(a)*ones(shape(a)[1])) + +class test_array_split(ScipyTestCase): + def check_integer_0_split(self): + a = arange(10) + try: + res = array_split(a,0) + assert(0) # it should have thrown a value error + except ValueError: + pass + def check_integer_split(self): + a = arange(10) + res = array_split(a,1) + desired = [arange(10)] + compare_results(res,desired) + + res = array_split(a,2) + desired = [arange(5),arange(5,10)] + compare_results(res,desired) + + res = array_split(a,3) + desired = [arange(4),arange(4,7),arange(7,10)] + compare_results(res,desired) + + res = array_split(a,4) + desired = [arange(3),arange(3,6),arange(6,8),arange(8,10)] + compare_results(res,desired) + + res = array_split(a,5) + desired = [arange(2),arange(2,4),arange(4,6),arange(6,8),arange(8,10)] + compare_results(res,desired) + + res = array_split(a,6) + desired = [arange(2),arange(2,4),arange(4,6),arange(6,8),arange(8,9), + arange(9,10)] + compare_results(res,desired) + + res = array_split(a,7) + desired = [arange(2),arange(2,4),arange(4,6),arange(6,7),arange(7,8), + arange(8,9), arange(9,10)] + compare_results(res,desired) + + res = array_split(a,8) + desired = [arange(2),arange(2,4),arange(4,5),arange(5,6),arange(6,7), + arange(7,8), arange(8,9), arange(9,10)] + compare_results(res,desired) + + res = array_split(a,9) + desired = [arange(2),arange(2,3),arange(3,4),arange(4,5),arange(5,6), + arange(6,7), arange(7,8), arange(8,9), arange(9,10)] + compare_results(res,desired) + + res = array_split(a,10) + desired = [arange(1),arange(1,2),arange(2,3),arange(3,4), + arange(4,5),arange(5,6), arange(6,7), arange(7,8), + arange(8,9), arange(9,10)] + compare_results(res,desired) + + res = array_split(a,11) + desired = [arange(1),arange(1,2),arange(2,3),arange(3,4), + arange(4,5),arange(5,6), arange(6,7), arange(7,8), + arange(8,9), arange(9,10),array([])] + compare_results(res,desired) + def check_integer_split_2D_rows(self): + a = array([arange(10),arange(10)]) + res = array_split(a,3,axis=0) + desired = [array([arange(10)]),array([arange(10)]),array([])] + compare_results(res,desired) + def check_integer_split_2D_cols(self): + a = array([arange(10),arange(10)]) + res = array_split(a,3,axis=-1) + desired = [array([arange(4),arange(4)]), + array([arange(4,7),arange(4,7)]), + array([arange(7,10),arange(7,10)])] + compare_results(res,desired) + def check_integer_split_2D_default(self): + """ This will fail if we change default axis + """ + a = array([arange(10),arange(10)]) + res = array_split(a,3) + desired = [array([arange(10)]),array([arange(10)]),array([])] + compare_results(res,desired) + #perhaps should check higher dimensions + + def check_index_split_simple(self): + a = arange(10) + indices = [1,5,7] + res = array_split(a,indices,axis=-1) + desired = [arange(0,1),arange(1,5),arange(5,7),arange(7,10)] + compare_results(res,desired) + + def check_index_split_low_bound(self): + a = arange(10) + indices = [0,5,7] + res = array_split(a,indices,axis=-1) + desired = [array([]),arange(0,5),arange(5,7),arange(7,10)] + compare_results(res,desired) + def check_index_split_high_bound(self): + a = arange(10) + indices = [0,5,7,10,12] + res = array_split(a,indices,axis=-1) + desired = [array([]),arange(0,5),arange(5,7),arange(7,10), + array([]),array([])] + compare_results(res,desired) + +class test_split(ScipyTestCase): + """* This function is essentially the same as array_split, + except that it test if splitting will result in an + equal split. Only test for this case. + *""" + def check_equal_split(self): + a = arange(10) + res = split(a,2) + desired = [arange(5),arange(5,10)] + compare_results(res,desired) + + def check_unequal_split(self): + a = arange(10) + try: + res = split(a,3) + assert(0) # should raise an error + except ValueError: + pass + +class test_atleast_1d(ScipyTestCase): + def check_0D_array(self): + a = array(1); b = array(2); + res=map(atleast_1d,[a,b]) + desired = [array([1]),array([2])] + assert_array_equal(res,desired) + def check_1D_array(self): + a = array([1,2]); b = array([2,3]); + res=map(atleast_1d,[a,b]) + desired = [array([1,2]),array([2,3])] + assert_array_equal(res,desired) + def check_2D_array(self): + a = array([[1,2],[1,2]]); b = array([[2,3],[2,3]]); + res=map(atleast_1d,[a,b]) + desired = [a,b] + assert_array_equal(res,desired) + def check_3D_array(self): + a = array([[1,2],[1,2]]); b = array([[2,3],[2,3]]); + a = array([a,a]);b = array([b,b]); + res=map(atleast_1d,[a,b]) + desired = [a,b] + assert_array_equal(res,desired) + def check_r1array(self): + """ Test to make sure equivalent Travis O's r1array function + """ + assert(atleast_1d(3).shape == (1,)) + assert(atleast_1d(3j).shape == (1,)) + assert(atleast_1d(3L).shape == (1,)) + assert(atleast_1d(3.0).shape == (1,)) + assert(atleast_1d([[2,3],[4,5]]).shape == (2,2)) + +class test_atleast_2d(ScipyTestCase): + def check_0D_array(self): + a = array(1); b = array(2); + res=map(atleast_2d,[a,b]) + desired = [array([[1]]),array([[2]])] + assert_array_equal(res,desired) + def check_1D_array(self): + a = array([1,2]); b = array([2,3]); + res=map(atleast_2d,[a,b]) + desired = [array([[1,2]]),array([[2,3]])] + assert_array_equal(res,desired) + def check_2D_array(self): + a = array([[1,2],[1,2]]); b = array([[2,3],[2,3]]); + res=map(atleast_2d,[a,b]) + desired = [a,b] + assert_array_equal(res,desired) + def check_3D_array(self): + a = array([[1,2],[1,2]]); b = array([[2,3],[2,3]]); + a = array([a,a]);b = array([b,b]); + res=map(atleast_2d,[a,b]) + desired = [a,b] + assert_array_equal(res,desired) + def check_r2array(self): + """ Test to make sure equivalent Travis O's r2array function + """ + assert(atleast_2d(3).shape == (1,1)) + assert(atleast_2d([3j,1]).shape == (1,2)) + assert(atleast_2d([[[3,1],[4,5]],[[3,5],[1,2]]]).shape == (2,2,2)) + +class test_atleast_3d(ScipyTestCase): + def check_0D_array(self): + a = array(1); b = array(2); + res=map(atleast_3d,[a,b]) + desired = [array([[[1]]]),array([[[2]]])] + assert_array_equal(res,desired) + def check_1D_array(self): + a = array([1,2]); b = array([2,3]); + res=map(atleast_3d,[a,b]) + desired = [array([[[1],[2]]]),array([[[2],[3]]])] + assert_array_equal(res,desired) + def check_2D_array(self): + a = array([[1,2],[1,2]]); b = array([[2,3],[2,3]]); + res=map(atleast_3d,[a,b]) + desired = [a[:,:,NewAxis],b[:,:,NewAxis]] + assert_array_equal(res,desired) + def check_3D_array(self): + a = array([[1,2],[1,2]]); b = array([[2,3],[2,3]]); + a = array([a,a]);b = array([b,b]); + res=map(atleast_3d,[a,b]) + desired = [a,b] + assert_array_equal(res,desired) + +class test_hstack(ScipyTestCase): + def check_0D_array(self): + a = array(1); b = array(2); + res=hstack([a,b]) + desired = array([1,2]) + assert_array_equal(res,desired) + def check_1D_array(self): + a = array([1]); b = array([2]); + res=hstack([a,b]) + desired = array([1,2]) + assert_array_equal(res,desired) + def check_2D_array(self): + a = array([[1],[2]]); b = array([[1],[2]]); + res=hstack([a,b]) + desired = array([[1,1],[2,2]]) + assert_array_equal(res,desired) + +class test_vstack(ScipyTestCase): + def check_0D_array(self): + a = array(1); b = array(2); + res=vstack([a,b]) + desired = array([[1],[2]]) + assert_array_equal(res,desired) + def check_1D_array(self): + a = array([1]); b = array([2]); + res=vstack([a,b]) + desired = array([[1],[2]]) + assert_array_equal(res,desired) + def check_2D_array(self): + a = array([[1],[2]]); b = array([[1],[2]]); + res=vstack([a,b]) + desired = array([[1],[2],[1],[2]]) + assert_array_equal(res,desired) + def check_2D_array2(self): + a = array([1,2]); b = array([1,2]); + res=vstack([a,b]) + desired = array([[1,2],[1,2]]) + assert_array_equal(res,desired) + +class test_dstack(ScipyTestCase): + def check_0D_array(self): + a = array(1); b = array(2); + res=dstack([a,b]) + desired = array([[[1,2]]]) + assert_array_equal(res,desired) + def check_1D_array(self): + a = array([1]); b = array([2]); + res=dstack([a,b]) + desired = array([[[1,2]]]) + assert_array_equal(res,desired) + def check_2D_array(self): + a = array([[1],[2]]); b = array([[1],[2]]); + res=dstack([a,b]) + desired = array([[[1,1]],[[2,2,]]]) + assert_array_equal(res,desired) + def check_2D_array2(self): + a = array([1,2]); b = array([1,2]); + res=dstack([a,b]) + desired = array([[[1,1],[2,2]]]) + assert_array_equal(res,desired) + +""" array_split has more comprehensive test of splitting. + only do simple test on hsplit, vsplit, and dsplit +""" +class test_hsplit(ScipyTestCase): + """ only testing for integer splits. + """ + def check_0D_array(self): + a= array(1) + try: + hsplit(a,2) + assert(0) + except ValueError: + pass + def check_1D_array(self): + a= array([1,2,3,4]) + res = hsplit(a,2) + desired = [array([1,2]),array([3,4])] + compare_results(res,desired) + def check_2D_array(self): + a= array([[1,2,3,4], + [1,2,3,4]]) + res = hsplit(a,2) + desired = [array([[1,2],[1,2]]),array([[3,4],[3,4]])] + compare_results(res,desired) + +class test_vsplit(ScipyTestCase): + """ only testing for integer splits. + """ + def check_1D_array(self): + a= array([1,2,3,4]) + try: + vsplit(a,2) + assert(0) + except ValueError: + pass + def check_2D_array(self): + a= array([[1,2,3,4], + [1,2,3,4]]) + res = vsplit(a,2) + desired = [array([[1,2,3,4]]),array([[1,2,3,4]])] + compare_results(res,desired) + +class test_dsplit(ScipyTestCase): + """ only testing for integer splits. + """ + def check_2D_array(self): + a= array([[1,2,3,4], + [1,2,3,4]]) + try: + dsplit(a,2) + assert(0) + except ValueError: + pass + def check_3D_array(self): + a= array([[[1,2,3,4], + [1,2,3,4]], + [[1,2,3,4], + [1,2,3,4]]]) + res = dsplit(a,2) + desired = [array([[[1,2],[1,2]],[[1,2],[1,2]]]), + array([[[3,4],[3,4]],[[3,4],[3,4]]])] + compare_results(res,desired) + +class test_squeeze(ScipyTestCase): + def check_basic(self): + a = rand(20,10,10,1,1) + b = rand(20,1,10,1,20) + c = rand(1,1,20,10) + assert_array_equal(squeeze(a),reshape(a,(20,10,10))) + assert_array_equal(squeeze(b),reshape(b,(20,10,20))) + assert_array_equal(squeeze(c),reshape(c,(20,10))) + +# Utility + +def compare_results(res,desired): + for i in range(len(desired)): + assert_array_equal(res[i],desired[i]) + + +if __name__ == "__main__": + ScipyTest().run() diff --git a/numpy/base/tests/test_twodim_base.py b/numpy/base/tests/test_twodim_base.py new file mode 100644 index 000000000..b061d4a5d --- /dev/null +++ b/numpy/base/tests/test_twodim_base.py @@ -0,0 +1,134 @@ +""" Test functions for matrix module + +""" + +from scipy.testing import * +set_package_path() +import scipy.base;reload(scipy.base) +from scipy.base import * +restore_path() + +################################################## + + +def get_mat(n): + data = arange(n) + data = add.outer(data,data) + return data + +class test_eye(ScipyTestCase): + def check_basic(self): + assert_equal(eye(4),array([[1,0,0,0], + [0,1,0,0], + [0,0,1,0], + [0,0,0,1]])) + assert_equal(eye(4,dtype='f'),array([[1,0,0,0], + [0,1,0,0], + [0,0,1,0], + [0,0,0,1]],'f')) + def check_diag(self): + assert_equal(eye(4,k=1),array([[0,1,0,0], + [0,0,1,0], + [0,0,0,1], + [0,0,0,0]])) + assert_equal(eye(4,k=-1),array([[0,0,0,0], + [1,0,0,0], + [0,1,0,0], + [0,0,1,0]])) + def check_2d(self): + assert_equal(eye(4,3),array([[1,0,0], + [0,1,0], + [0,0,1], + [0,0,0]])) + assert_equal(eye(3,4),array([[1,0,0,0], + [0,1,0,0], + [0,0,1,0]])) + def check_diag2d(self): + assert_equal(eye(3,4,k=2),array([[0,0,1,0], + [0,0,0,1], + [0,0,0,0]])) + assert_equal(eye(4,3,k=-2),array([[0,0,0], + [0,0,0], + [1,0,0], + [0,1,0]])) + +class test_diag(ScipyTestCase): + def check_vector(self): + vals = (100*arange(5)).astype('l') + b = zeros((5,5)) + for k in range(5): + b[k,k] = vals[k] + assert_equal(diag(vals),b) + b = zeros((7,7)) + c = b.copy() + for k in range(5): + b[k,k+2] = vals[k] + c[k+2,k] = vals[k] + assert_equal(diag(vals,k=2), b) + assert_equal(diag(vals,k=-2), c) + + def check_matrix(self): + vals = (100*get_mat(5)+1).astype('l') + b = zeros((5,)) + for k in range(5): + b[k] = vals[k,k] + assert_equal(diag(vals),b) + b = b*0 + for k in range(3): + b[k] = vals[k,k+2] + assert_equal(diag(vals,2),b[:3]) + for k in range(3): + b[k] = vals[k+2,k] + assert_equal(diag(vals,-2),b[:3]) + +class test_fliplr(ScipyTestCase): + def check_basic(self): + self.failUnlessRaises(ValueError, fliplr, ones(4)) + a = get_mat(4) + b = a[:,::-1] + assert_equal(fliplr(a),b) + a = [[0,1,2], + [3,4,5]] + b = [[2,1,0], + [5,4,3]] + assert_equal(fliplr(a),b) + +class test_flipud(ScipyTestCase): + def check_basic(self): + a = get_mat(4) + b = a[::-1,:] + assert_equal(flipud(a),b) + a = [[0,1,2], + [3,4,5]] + b = [[3,4,5], + [0,1,2]] + assert_equal(flipud(a),b) + +class test_rot90(ScipyTestCase): + def check_basic(self): + self.failUnlessRaises(ValueError, rot90, ones(4)) + + a = [[0,1,2], + [3,4,5]] + b1 = [[2,5], + [1,4], + [0,3]] + b2 = [[5,4,3], + [2,1,0]] + b3 = [[3,0], + [4,1], + [5,2]] + b4 = [[0,1,2], + [3,4,5]] + + for k in range(-3,13,4): + assert_equal(rot90(a,k=k),b1) + for k in range(-2,13,4): + assert_equal(rot90(a,k=k),b2) + for k in range(-1,13,4): + assert_equal(rot90(a,k=k),b3) + for k in range(0,13,4): + assert_equal(rot90(a,k=k),b4) + +if __name__ == "__main__": + ScipyTest().run() diff --git a/numpy/base/tests/test_type_check.py b/numpy/base/tests/test_type_check.py new file mode 100644 index 000000000..aac24bd6e --- /dev/null +++ b/numpy/base/tests/test_type_check.py @@ -0,0 +1,238 @@ + +import sys + +from scipy.testing import * +set_package_path() +import scipy.base;reload(scipy.base);reload(scipy.base.type_check) +from scipy.base import * +restore_path() + +def assert_all(x): + assert(all(x)), x + +class test_mintypecode(ScipyTestCase): + + def check_default_1(self): + for itype in '1bcsuwil': + assert_equal(mintypecode(itype),'d') + assert_equal(mintypecode('f'),'f') + assert_equal(mintypecode('d'),'d') + assert_equal(mintypecode('F'),'F') + assert_equal(mintypecode('D'),'D') + + def check_default_2(self): + for itype in '1bcsuwil': + assert_equal(mintypecode(itype+'f'),'f') + assert_equal(mintypecode(itype+'d'),'d') + assert_equal(mintypecode(itype+'F'),'F') + assert_equal(mintypecode(itype+'D'),'D') + assert_equal(mintypecode('ff'),'f') + assert_equal(mintypecode('fd'),'d') + assert_equal(mintypecode('fF'),'F') + assert_equal(mintypecode('fD'),'D') + assert_equal(mintypecode('df'),'d') + assert_equal(mintypecode('dd'),'d') + #assert_equal(mintypecode('dF',savespace=1),'F') + assert_equal(mintypecode('dF'),'D') + assert_equal(mintypecode('dD'),'D') + assert_equal(mintypecode('Ff'),'F') + #assert_equal(mintypecode('Fd',savespace=1),'F') + assert_equal(mintypecode('Fd'),'D') + assert_equal(mintypecode('FF'),'F') + assert_equal(mintypecode('FD'),'D') + assert_equal(mintypecode('Df'),'D') + assert_equal(mintypecode('Dd'),'D') + assert_equal(mintypecode('DF'),'D') + assert_equal(mintypecode('DD'),'D') + + def check_default_3(self): + assert_equal(mintypecode('fdF'),'D') + #assert_equal(mintypecode('fdF',savespace=1),'F') + assert_equal(mintypecode('fdD'),'D') + assert_equal(mintypecode('fFD'),'D') + assert_equal(mintypecode('dFD'),'D') + + assert_equal(mintypecode('ifd'),'d') + assert_equal(mintypecode('ifF'),'F') + assert_equal(mintypecode('ifD'),'D') + assert_equal(mintypecode('idF'),'D') + #assert_equal(mintypecode('idF',savespace=1),'F') + assert_equal(mintypecode('idD'),'D') + +class test_isscalar(ScipyTestCase): + def check_basic(self): + assert(isscalar(3)) + assert(not isscalar([3])) + assert(not isscalar((3,))) + assert(isscalar(3j)) + assert(isscalar(10L)) + assert(isscalar(4.0)) + +class test_real(ScipyTestCase): + def check_real(self): + y = rand(10,) + assert_array_equal(y,real(y)) + + def check_cmplx(self): + y = rand(10,)+1j*rand(10,) + assert_array_equal(y.real,real(y)) + +class test_imag(ScipyTestCase): + def check_real(self): + y = rand(10,) + assert_array_equal(0,imag(y)) + + def check_cmplx(self): + y = rand(10,)+1j*rand(10,) + assert_array_equal(y.imag,imag(y)) + +class test_iscomplex(ScipyTestCase): + def check_fail(self): + z = array([-1,0,1]) + res = iscomplex(z) + assert(not sometrue(res)) + def check_pass(self): + z = array([-1j,1,0]) + res = iscomplex(z) + assert_array_equal(res,[1,0,0]) + +class test_isreal(ScipyTestCase): + def check_pass(self): + z = array([-1,0,1j]) + res = isreal(z) + assert_array_equal(res,[1,1,0]) + def check_fail(self): + z = array([-1j,1,0]) + res = isreal(z) + assert_array_equal(res,[0,1,1]) + +class test_iscomplexobj(ScipyTestCase): + def check_basic(self): + z = array([-1,0,1]) + assert(not iscomplexobj(z)) + z = array([-1j,0,-1]) + assert(iscomplexobj(z)) + +class test_isrealobj(ScipyTestCase): + def check_basic(self): + z = array([-1,0,1]) + assert(isrealobj(z)) + z = array([-1j,0,-1]) + assert(not isrealobj(z)) + +class test_isnan(ScipyTestCase): + def check_goodvalues(self): + z = array((-1.,0.,1.)) + res = isnan(z) == 0 + assert_all(alltrue(res)) + def check_posinf(self): + assert_all(isnan(array((1.,))/0.) == 0) + def check_neginf(self): + assert_all(isnan(array((-1.,))/0.) == 0) + def check_ind(self): + assert_all(isnan(array((0.,))/0.) == 1) + #def check_qnan(self): log(-1) return pi*j now + # assert_all(isnan(log(-1.)) == 1) + def check_integer(self): + assert_all(isnan(1) == 0) + def check_complex(self): + assert_all(isnan(1+1j) == 0) + def check_complex1(self): + assert_all(isnan(array(0+0j)/0.) == 1) + +class test_isfinite(ScipyTestCase): + def check_goodvalues(self): + z = array((-1.,0.,1.)) + res = isfinite(z) == 1 + assert_all(alltrue(res)) + def check_posinf(self): + assert_all(isfinite(array((1.,))/0.) == 0) + def check_neginf(self): + assert_all(isfinite(array((-1.,))/0.) == 0) + def check_ind(self): + assert_all(isfinite(array((0.,))/0.) == 0) + #def check_qnan(self): + # assert_all(isfinite(log(-1.)) == 0) + def check_integer(self): + assert_all(isfinite(1) == 1) + def check_complex(self): + assert_all(isfinite(1+1j) == 1) + def check_complex1(self): + assert_all(isfinite(array(1+1j)/0.) == 0) + +class test_isinf(ScipyTestCase): + def check_goodvalues(self): + z = array((-1.,0.,1.)) + res = isinf(z) == 0 + assert_all(alltrue(res)) + def check_posinf(self): + assert_all(isinf(array((1.,))/0.) == 1) + def check_posinf_scalar(self): + assert_all(isinf(array(1.,)/0.) == 1) + def check_neginf(self): + assert_all(isinf(array((-1.,))/0.) == 1) + def check_neginf_scalar(self): + assert_all(isinf(array(-1.)/0.) == 1) + def check_ind(self): + assert_all(isinf(array((0.,))/0.) == 0) + #def check_qnan(self): + # assert_all(isinf(log(-1.)) == 0) + # assert_all(isnan(log(-1.)) == 1) + +class test_isposinf(ScipyTestCase): + def check_generic(self): + vals = isposinf(array((-1.,0,1))/0.) + assert(vals[0] == 0) + assert(vals[1] == 0) + assert(vals[2] == 1) + +class test_isneginf(ScipyTestCase): + def check_generic(self): + vals = isneginf(array((-1.,0,1))/0.) + assert(vals[0] == 1) + assert(vals[1] == 0) + assert(vals[2] == 0) + +class test_nan_to_num(ScipyTestCase): + def check_generic(self): + vals = nan_to_num(array((-1.,0,1))/0.) + assert_all(vals[0] < -1e10) and assert_all(isfinite(vals[0])) + assert(vals[1] == 0) + assert_all(vals[2] > 1e10) and assert_all(isfinite(vals[2])) + def check_integer(self): + vals = nan_to_num(1) + assert_all(vals == 1) + def check_complex_good(self): + vals = nan_to_num(1+1j) + assert_all(vals == 1+1j) + def check_complex_bad(self): + v = 1+1j + v += array(0+1.j)/0. + vals = nan_to_num(v) + # !! This is actually (unexpectedly) zero + assert_all(isfinite(vals)) + def check_complex_bad2(self): + v = 1+1j + v += array(-1+1.j)/0. + vals = nan_to_num(v) + assert_all(isfinite(vals)) + #assert_all(vals.imag > 1e10) and assert_all(isfinite(vals)) + # !! This is actually (unexpectedly) positive + # !! inf. Comment out for now, and see if it + # !! changes + #assert_all(vals.real < -1e10) and assert_all(isfinite(vals)) + + +class test_real_if_close(ScipyTestCase): + def check_basic(self): + a = rand(10) + b = real_if_close(a+1e-15j) + assert_all(isrealobj(b)) + assert_array_equal(a,b) + b = real_if_close(a+1e-7j) + assert_all(iscomplexobj(b)) + b = real_if_close(a+1e-7j,tol=1e-6) + assert_all(isrealobj(b)) + +if __name__ == "__main__": + ScipyTest().run() diff --git a/numpy/base/tests/test_ufunclike.py b/numpy/base/tests/test_ufunclike.py new file mode 100644 index 000000000..ca06140c7 --- /dev/null +++ b/numpy/base/tests/test_ufunclike.py @@ -0,0 +1,63 @@ +""" +>>> import scipy.base as nx +>>> import scipy.base.ufunclike as U + +Test fix: +>>> a = nx.array([[1.0, 1.1, 1.5, 1.8], [-1.0, -1.1, -1.5, -1.8]]) +>>> U.fix(a) +array([[ 1., 1., 1., 1.], + [ 0., -1., -1., -1.]]) +>>> y = nx.zeros(a.shape, float) +>>> U.fix(a, y) +array([[ 1., 1., 1., 1.], + [ 0., -1., -1., -1.]]) +>>> y +array([[ 1., 1., 1., 1.], + [ 0., -1., -1., -1.]]) + +Test isposinf, isneginf, sign +>>> a = nx.array([nx.Inf, -nx.Inf, nx.NaN, 0.0, 3.0, -3.0]) +>>> U.isposinf(a) +array([True, False, False, False, False, False], dtype=bool) +>>> U.isneginf(a) +array([False, True, False, False, False, False], dtype=bool) +>>> U.sign(a) +array([ 1, -1, 0, 0, 1, -1]) + +Same thing with an output array: +>>> y = nx.zeros(a.shape, bool) +>>> U.isposinf(a, y) +array([True, False, False, False, False, False], dtype=bool) +>>> y +array([True, False, False, False, False, False], dtype=bool) +>>> U.isneginf(a, y) +array([False, True, False, False, False, False], dtype=bool) +>>> y +array([False, True, False, False, False, False], dtype=bool) +>>> U.sign(a, y) +array([True, True, False, False, True, True], dtype=bool) +>>> y +array([True, True, False, False, True, True], dtype=bool) + +Now log2: +>>> a = nx.array([4.5, 2.3, 6.5]) +>>> U.log2(a) +array([ 2.169925 , 1.20163386, 2.70043972]) +>>> 2**_ +array([ 4.5, 2.3, 6.5]) +>>> y = nx.zeros(a.shape, float) +>>> U.log2(a, y) +array([ 2.169925 , 1.20163386, 2.70043972]) +>>> y +array([ 2.169925 , 1.20163386, 2.70043972]) + +""" + +from scipy.testing import * + +import doctest +def test_suite(level=1): + return doctest.DocTestSuite() + +if __name__ == "__main__": + ScipyTest().run() diff --git a/numpy/base/tests/test_umath.py b/numpy/base/tests/test_umath.py new file mode 100644 index 000000000..9cd99f7e1 --- /dev/null +++ b/numpy/base/tests/test_umath.py @@ -0,0 +1,18 @@ + +from scipy.testing import * +set_package_path() +from scipy.base.umath import minimum, maximum +restore_path() + + +class test_maximum(ScipyTestCase): + def check_reduce_complex(self): + assert_equal(maximum.reduce([1,2j]),1) + assert_equal(maximum.reduce([1+3j,2j]),1+3j) + +class test_minimum(ScipyTestCase): + def check_reduce_complex(self): + assert_equal(minimum.reduce([1,2j]),2j) + +if __name__ == "__main__": + ScipyTest().run() diff --git a/numpy/base/tests/testdata.fits b/numpy/base/tests/testdata.fits Binary files differnew file mode 100644 index 000000000..ca48ee851 --- /dev/null +++ b/numpy/base/tests/testdata.fits |
