diff --git a/pypocketfft.cc b/pypocketfft.cc index ee2ce0fb06adf3efa63a5c0d08e5af5e3530e92b..2bdb3afb4ddb4b3997e92844f9693ff8c114653a 100644 --- a/pypocketfft.cc +++ b/pypocketfft.cc @@ -597,11 +597,18 @@ axes : list of integers inorm : int Normalization type 0 : no normalization - 1 : divide by sqrt(N) + 1 : make transform orthogonal and divide by sqrt(N) 2 : divide by N where N is the product of n_i for every transformed axis i. n_i is 2*(-1 for type 1 and 2* for types 2, 3, 4. + Making the transform orthogonal involves the following additional steps + for every 1D sub-transform: + Type 1 : multiply first and last input value by sqrt(2) + divide first and last output value by sqrt(2) + Type 2 : divide first output value by sqrt(2) + Type 3 : multiply first input value by sqrt(2) + Type 4 : nothing out : numpy.ndarray (same shape and data type as `a`) May be identical to `a`, but if it isn't, it must not overlap with `a`. If None, a new array is allocated to store the output. @@ -629,11 +636,17 @@ axes : list of integers inorm : int Normalization type 0 : no normalization - 1 : divide by sqrt(N) + 1 : make transform orthogonal and divide by sqrt(N) 2 : divide by N where N is the product of n_i for every transformed axis i. n_i is 2*(+1 for type 1 and 2* for types 2, 3, 4. + Making the transform orthogonal involves the following additional steps + for every 1D sub-transform: + Type 1 : nothing + Type 2 : divide first output value by sqrt(2) + Type 3 : multiply first input value by sqrt(2) + Type 4 : nothing out : numpy.ndarray (same shape and data type as `a`) May be identical to `a`, but if it isn't, it must not overlap with `a`. If None, a new array is allocated to store the output. diff --git a/test.py b/test.py index 9a309c0f60f6ce52232e472671071962fca7386e..7ee4505c0f86bb52b597bdacce1fc5333be1737d 100644 --- a/test.py +++ b/test.py @@ -55,36 +55,27 @@ def irfft_scipy(a, axis, inorm=0, out=None, nthreads=1): forward=False, inorm=inorm, out=out, nthreads=nthreads) +tol = {np.float32: 6e-7, np.float64: 1.5e-15, np.longfloat: 1e-18} +ctype = {np.float32: np.complex64, np.float64: np.complex128, np.longfloat: np.longcomplex} @pmp("len", len1D) @pmp("inorm", [0, 1, 2]) -def test1D(len, inorm): +@pmp("dtype", [np.float32, np.float64, np.longfloat]) +def test1D(len, inorm, dtype): a = np.random.rand(len)-0.5 + 1j*np.random.rand(len)-0.5j - b = a.astype(np.complex64) - c = a.astype(np.complex256) - _assert_close(a, ifftn(fftn(c, inorm=inorm), inorm=2-inorm), 1e-18) - assert_(_l2error(a, ifftn(fftn(a, inorm=inorm), inorm=2-inorm)) < 1.5e-15) + a = a.astype(ctype[dtype]) + eps = tol[dtype] + assert_(_l2error(a, ifftn(fftn(a, inorm=inorm), inorm=2-inorm)) < eps) assert_(_l2error(a.real, ifftn(fftn(a.real, inorm=inorm), inorm=2-inorm)) - < 1.5e-15) + < eps) assert_(_l2error(a.real, fftn(ifftn(a.real, inorm=inorm), inorm=2-inorm)) - < 1.5e-15) + < eps) assert_(_l2error(a.real, irfftn(rfftn(a.real, inorm=inorm), - inorm=2-inorm, lastsize=len)) < 1.5e-15) + inorm=2-inorm, lastsize=len)) < eps) tmp = a.copy() assert_(ifftn(fftn(tmp, out=tmp, inorm=inorm), out=tmp, inorm=2-inorm) is tmp) - assert_(_l2error(tmp, a) < 1.5e-15) - assert_(_l2error(b, ifftn(fftn(b, inorm=inorm), inorm=2-inorm)) < 6e-7) - assert_(_l2error(b.real, ifftn(fftn(b.real, inorm=inorm), inorm=2-inorm)) - < 6e-7) - assert_(_l2error(b.real, fftn(ifftn(b.real, inorm=inorm), inorm=2-inorm)) - < 6e-7) - assert_(_l2error(b.real, irfftn(rfftn(b.real, inorm=inorm), lastsize=len, - inorm=2-inorm)) < 6e-7) - tmp = b.copy() - assert_(ifftn(fftn(tmp, out=tmp, inorm=inorm), out=tmp, inorm=2-inorm) - is tmp) - assert_(_l2error(tmp, b) < 6e-7) + assert_(_l2error(tmp, a) < eps) @pmp("shp", shapes) @@ -206,54 +197,17 @@ def test_genuine_hartley_2D(shp, axes): @pmp("len", len1D) -@pmp("inorm", [0, 1, 2]) -@pmp("type", [1, 2, 3]) -def testdcst1D(len, inorm, type): - a = np.random.rand(len)-0.5 - b = a.astype(np.float32) - c = a.astype(np.float128) - itp = (0, 1, 3, 2, 4) - itype = itp[type] - if type != 1 or len > 1: - _assert_close(a, pypocketfft.dct(pypocketfft.dct(c, inorm=inorm, type=type), inorm=2-inorm, type=itype), 2e-18) - _assert_close(a, pypocketfft.dct(pypocketfft.dct(a, inorm=inorm, type=type), inorm=2-inorm, type=itype), 1.5e-15) - _assert_close(b, pypocketfft.dct(pypocketfft.dct(b, inorm=inorm, type=type), inorm=2-inorm, type=itype), 6e-7) - _assert_close(a, pypocketfft.dst(pypocketfft.dst(c, inorm=inorm, type=type), inorm=2-inorm, type=itype), 2e-18) - _assert_close(a, pypocketfft.dst(pypocketfft.dst(a, inorm=inorm, type=type), inorm=2-inorm, type=itype), 1.5e-15) - _assert_close(b, pypocketfft.dst(pypocketfft.dst(b, inorm=inorm, type=type), inorm=2-inorm, type=itype), 6e-7) - -@pmp("len", len1D) -@pmp("type", [1, 2, 3]) -def testdcst1Dortho(len, type): - a = np.random.rand(len)-0.5 - b = a.astype(np.float32) - c = a.astype(np.float128) - itp = (0, 1, 3, 2, 4) - itype = itp[type] - if type != 1 or len > 1: - _assert_close(a, pypocketfft.dct(pypocketfft.dct(c, inorm=1, type=type), inorm=1, type=itype), 2e-18) - _assert_close(a, pypocketfft.dct(pypocketfft.dct(a, inorm=1, type=type), inorm=1, type=itype), 1.5e-15) - _assert_close(b, pypocketfft.dct(pypocketfft.dct(b, inorm=1, type=type), inorm=1, type=itype), 6e-7) - if type != 1: - _assert_close(a, pypocketfft.dst(pypocketfft.dst(c, inorm=1, type=type), inorm=1, type=itype), 2e-18) - _assert_close(a, pypocketfft.dst(pypocketfft.dst(a, inorm=1, type=type), inorm=1, type=itype), 1.5e-15) - _assert_close(b, pypocketfft.dst(pypocketfft.dst(b, inorm=1, type=type), inorm=1, type=itype), 6e-7) - - -# TEMPORARY: separate test for DCT/DST IV, since they are less accurate -@pmp("len", len1D) -@pmp("inorm", [0, 1, 2]) -@pmp("type", [4]) -def testdcst1D4(len, inorm, type): - a = np.random.rand(len)-0.5 - b = a.astype(np.float32) - c = a.astype(np.float128) +@pmp("inorm", [0, 1]) # inorm==2 not needed, tested via inverse +@pmp("type", [1, 2, 3, 4]) +@pmp("dtype", [np.float32, np.float64, np.longfloat]) +def testdcst1D(len, inorm, type, dtype): + a = (np.random.rand(len)-0.5).astype(dtype) + eps = tol[dtype] itp = (0, 1, 3, 2, 4) + if type==4 and len%2 == 1: # relaxed accuracies for odd-length type 4 transforms + special_tol = {np.float32: 4e-5, np.float64: 6e-14, np.longfloat: 4e-17} + eps = special_tol[dtype] itype = itp[type] - if type != 1 or len > 1: - _assert_close(a, pypocketfft.dct(pypocketfft.dct(c, inorm=inorm, type=type), inorm=2-inorm, type=itype), 2e-16) - _assert_close(a, pypocketfft.dct(pypocketfft.dct(a, inorm=inorm, type=type), inorm=2-inorm, type=itype), 1.5e-13) - _assert_close(b, pypocketfft.dct(pypocketfft.dct(b, inorm=inorm, type=type), inorm=2-inorm, type=itype), 6e-5) - _assert_close(a, pypocketfft.dst(pypocketfft.dst(c, inorm=inorm, type=type), inorm=2-inorm, type=itype), 2e-16) - _assert_close(a, pypocketfft.dst(pypocketfft.dst(a, inorm=inorm, type=type), inorm=2-inorm, type=itype), 1.5e-13) - _assert_close(b, pypocketfft.dst(pypocketfft.dst(b, inorm=inorm, type=type), inorm=2-inorm, type=itype), 6e-5) + if type != 1 or len > 1: # there are no length-1 type 1 DCTs + _assert_close(a, pypocketfft.dct(pypocketfft.dct(a, inorm=inorm, type=type), inorm=2-inorm, type=itype), eps) + _assert_close(a, pypocketfft.dst(pypocketfft.dst(a, inorm=inorm, type=type), inorm=2-inorm, type=itype), eps)