diff options
-rw-r--r-- | numpy/add_newdocs.py | 32 | ||||
-rw-r--r-- | numpy/core/tests/test_multiarray.py | 70 | ||||
-rw-r--r-- | numpy/fft/tests/test_fftpack.py | 16 |
3 files changed, 103 insertions, 15 deletions
diff --git a/numpy/add_newdocs.py b/numpy/add_newdocs.py index 2acef6b69..ce4b43107 100644 --- a/numpy/add_newdocs.py +++ b/numpy/add_newdocs.py @@ -379,25 +379,35 @@ add_newdoc('numpy.core.multiarray','lexsort', sort order, and so on). The keys argument must be a sequence of things that can be converted to arrays of the same shape. - Parameters: + *Parameters*: - a : array type + keys : (k,N) array or tuple of (N,) sequences Array containing values that the returned indices should sort. axis : integer - Axis to be indirectly sorted. None indicates that the flattened - array should be used. Default is -1. + Axis to be indirectly sorted. Default is -1 (i.e. last axis). - Returns: + *Returns*: - indices : integer array - Array of indices that sort the keys along the specified axis. The - array has the same shape as the keys. + indices : (N,) integer array + Array of indices that sort the keys along the specified axis. - SeeAlso: + *See Also*: + + `argsort` : indirect sort + `sort` : inplace sort + + *Examples* - argsort : indirect sort - sort : inplace sort + >>> a = [1,5,1,4,3,6,7] + >>> b = [9,4,0,4,0,4,3] + >>> ind = lexsort((b,a)) + >>> print ind + [2 0 4 3 1 5 6] + >>> print take(a,ind) + [1 1 3 4 5 6 7] + >>> print take(b,ind) + [0 9 0 4 4 4 3] """) diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index 475787a09..81c14d99a 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -3,6 +3,8 @@ from numpy.core import * from numpy import random import numpy as N +import tempfile + class TestFlags(NumpyTestCase): def setUp(self): self.a = arange(10) @@ -420,7 +422,7 @@ class TestClip(NumpyTestCase): y = rec['x'].clip(-0.3,0.5) self._check_range(y,-0.3,0.5) -class test_putmask(ParametricTestCase): +class TestPutmask(ParametricTestCase): def tst_basic(self,x,T,mask,val): N.putmask(x,mask,val) assert N.all(x[mask] == T(val)) @@ -466,10 +468,74 @@ class test_putmask(ParametricTestCase): ## N.putmask(z,[True,True,True],3) pass -# Import tests from unicode +class TestLexsort(NumpyTestCase): + def test_basic(self): + a = [1,2,1,3,1,5] + b = [0,4,5,6,2,3] + idx = N.lexsort((b,a)) + expected_idx = N.array([0,4,2,1,3,5]) + assert_array_equal(idx,expected_idx) + + x = N.vstack((b,a)) + idx = N.lexsort(x) + assert_array_equal(idx,expected_idx) + + assert_array_equal(x[1][idx],N.sort(x[1])) + +class TestFromToFile(NumpyTestCase): + def setUp(self): + shape = (4,7) + rand = N.random.random + + self.x = rand(shape) + rand(shape).astype(N.complex)*1j + self.dtype = N.complex + + def test_file(self): + f = tempfile.TemporaryFile() + self.x.tofile(f) + f.seek(0) + y = N.fromfile(f,dtype=self.dtype) + assert_array_equal(y,self.x.flat) + + def test_filename(self): + filename = tempfile.mktemp() + f = open(filename,'wb') + self.x.tofile(f) + f.close() + y = N.fromfile(filename,dtype=self.dtype) + assert_array_equal(y,self.x.flat) + +class TestFromBuffer(ParametricTestCase): + def tst_basic(self,buffer,expected,kwargs): + assert_array_equal(N.frombuffer(buffer,**kwargs),expected) + + def testip_basic(self): + tests = [] + for byteorder in ['<','>']: + for dtype in [float,int,N.complex]: + dt = N.dtype(dtype).newbyteorder(byteorder) + x = (N.random.random((4,7))*5).astype(dt) + buf = x.tostring() + tests.append((self.tst_basic,buf,x.flat,{'dtype':dt})) + return tests + +class TestResize(NumpyTestCase): + def test_basic(self): + x = N.eye(3) + x.resize((5,5)) + assert_array_equal(x.flat[:9],N.eye(3).flat) + assert_array_equal(x[9:].flat,0) + + def test_check_reference(self): + x = N.eye(3) + y = x + self.failUnlessRaises(ValueError,x.resize,(5,1)) + +# Import tests without matching module names set_local_path() from test_unicode import * from test_regression import * +from test_ufunc import * restore_path() if __name__ == "__main__": diff --git a/numpy/fft/tests/test_fftpack.py b/numpy/fft/tests/test_fftpack.py index 74e2d06f5..53d4a8c66 100644 --- a/numpy/fft/tests/test_fftpack.py +++ b/numpy/fft/tests/test_fftpack.py @@ -1,12 +1,24 @@ import sys from numpy.testing import * set_package_path() -from numpy.fft import * +import numpy as N restore_path() +def fft1(x): + L = len(x) + phase = -2j*N.pi*(N.arange(L)/float(L)) + phase = N.arange(L).reshape(-1,1) * phase + return N.sum(x*N.exp(phase),axis=1) + class TestFFTShift(NumpyTestCase): def check_fft_n(self): - self.failUnlessRaises(ValueError,fft,[1,2,3],0) + self.failUnlessRaises(ValueError,N.fft.fft,[1,2,3],0) + +class TestFFT1D(NumpyTestCase): + def check_basic(self): + rand = N.random.random + x = rand(30) + 1j*rand(30) + assert_array_almost_equal(fft1(x), N.fft.fft(x)) if __name__ == "__main__": NumpyTest().run() |