diff options
author | Travis Oliphant <oliphant@enthought.com> | 2006-01-04 17:37:00 +0000 |
---|---|---|
committer | Travis Oliphant <oliphant@enthought.com> | 2006-01-04 17:37:00 +0000 |
commit | 3496a3cda8ea70253a76ed17c0af261f2d645fe2 (patch) | |
tree | 7efd3ed700501e2a10646632edc18eb5679538e4 /numpy/lib/tests | |
parent | 8057b2d910a5a6726a666a2c18ac495dbb9e6000 (diff) | |
download | numpy-3496a3cda8ea70253a76ed17c0af261f2d645fe2.tar.gz |
Moving things..
Diffstat (limited to 'numpy/lib/tests')
-rw-r--r-- | numpy/lib/tests/test_function_base.py | 338 | ||||
-rw-r--r-- | numpy/lib/tests/test_getlimits.py | 38 | ||||
-rw-r--r-- | numpy/lib/tests/test_index_tricks.py | 53 | ||||
-rw-r--r-- | numpy/lib/tests/test_polynomial.py | 83 | ||||
-rw-r--r-- | numpy/lib/tests/test_twodim_base.py | 134 |
5 files changed, 646 insertions, 0 deletions
diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py new file mode 100644 index 000000000..fafd75eef --- /dev/null +++ b/numpy/lib/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/lib/tests/test_getlimits.py b/numpy/lib/tests/test_getlimits.py new file mode 100644 index 000000000..99a6f5160 --- /dev/null +++ b/numpy/lib/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/lib/tests/test_index_tricks.py b/numpy/lib/tests/test_index_tricks.py new file mode 100644 index 000000000..96e9dff84 --- /dev/null +++ b/numpy/lib/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/lib/tests/test_polynomial.py b/numpy/lib/tests/test_polynomial.py new file mode 100644 index 000000000..51d4b5707 --- /dev/null +++ b/numpy/lib/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/lib/tests/test_twodim_base.py b/numpy/lib/tests/test_twodim_base.py new file mode 100644 index 000000000..b061d4a5d --- /dev/null +++ b/numpy/lib/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() |