import pypocketfft import pyfftw import numpy as np import pytest from numpy.testing import assert_allclose pmp = pytest.mark.parametrize shapes1D = ((10,), (127,)) shapes2D = ((128, 128), (128, 129), (1, 129), (129,1)) shapes3D = ((32,17,39),) shapes = shapes1D+shapes2D+shapes3D def test1D(): maxlen=8192 a=np.random.rand(maxlen)-0.5 + 1j*np.random.rand(maxlen)-0.5j b=a.astype(np.complex64) for i in range(1,maxlen+1): assert_allclose(pypocketfft.ifftn(pypocketfft.fftn(a[:i]),fct=1./i),a[:i], atol=2e-15, rtol=0) assert_allclose(pypocketfft.irfftn(pypocketfft.rfftn(a[:i].real),fct=1./i,lastsize=i),a[:i].real, atol=2e-15, rtol=0) assert_allclose(pypocketfft.ifftn(pypocketfft.fftn(b[:i]),fct=1./i),b[:i], atol=8e-7, rtol=0) assert_allclose(pypocketfft.irfftn(pypocketfft.rfftn(b[:i].real),fct=1./i,lastsize=i),b[:i].real, atol=8e-7, rtol=0) @pmp("shp", shapes) def test_fftn(shp): a=np.random.rand(*shp)-0.5 + 1j*np.random.rand(*shp)-0.5j vmax = np.max(np.abs(pyfftw.interfaces.numpy_fft.fftn(a))) assert_allclose(pyfftw.interfaces.numpy_fft.fftn(a), pypocketfft.fftn(a), atol=2e-15*vmax, rtol=0) a=a.astype(np.complex64) assert_allclose(pyfftw.interfaces.numpy_fft.fftn(a), pypocketfft.fftn(a), atol=6e-7*vmax, rtol=0) @pmp("shp", shapes2D) @pmp("axes", ((0,),(1,),(0,1),(1,0))) def test_fftn2D(shp, axes): a=np.random.rand(*shp)-0.5 + 1j*np.random.rand(*shp)-0.5j vmax = np.max(np.abs(pyfftw.interfaces.numpy_fft.fftn(a, axes=axes))) assert_allclose(pyfftw.interfaces.numpy_fft.fftn(a, axes=axes), pypocketfft.fftn(a, axes=axes), atol=2e-15*vmax, rtol=0) a=a.astype(np.complex64) assert_allclose(pyfftw.interfaces.numpy_fft.fftn(a, axes=axes), pypocketfft.fftn(a, axes=axes), atol=6e-7*vmax, rtol=0) @pmp("shp", shapes) def test_rfftn(shp): a=np.random.rand(*shp)-0.5 vmax = np.max(np.abs(pyfftw.interfaces.numpy_fft.fftn(a))) assert_allclose(pyfftw.interfaces.numpy_fft.rfftn(a), pypocketfft.rfftn(a), atol=2e-15*vmax, rtol=0) a=a.astype(np.float32) assert_allclose(pyfftw.interfaces.numpy_fft.rfftn(a), pypocketfft.rfftn(a), atol=6e-7*vmax, rtol=0) @pmp("shp", shapes2D) @pmp("axes", ((0,),(1,),(0,1),(1,0))) def test_rfftn2D(shp, axes): a=np.random.rand(*shp)-0.5 vmax = np.max(np.abs(pyfftw.interfaces.numpy_fft.rfftn(a, axes=axes))) assert_allclose(pyfftw.interfaces.numpy_fft.rfftn(a, axes=axes), pypocketfft.rfftn(a, axes=axes), atol=2e-15*vmax, rtol=0) a=a.astype(np.float32) assert_allclose(pyfftw.interfaces.numpy_fft.rfftn(a, axes=axes), pypocketfft.rfftn(a, axes=axes), atol=6e-7*vmax, rtol=0) @pmp("shp", shapes) def test_identity(shp): a=np.random.rand(*shp)-0.5 + 1j*np.random.rand(*shp)-0.5j vmax = np.max(np.abs(a)) assert_allclose(pypocketfft.ifftn(pypocketfft.fftn(a),fct=1./a.size), a, atol=2e-15*vmax, rtol=0) a=a.astype(np.complex64) assert_allclose(pypocketfft.ifftn(pypocketfft.fftn(a),fct=1./a.size), a, atol=6e-7*vmax, rtol=0) @pmp("shp", shapes) def test_identity_r(shp): a=np.random.rand(*shp)-0.5 b=a.astype(np.float32) vmax = np.max(np.abs(a)) for ax in range(a.ndim): n = a.shape[ax] assert_allclose(pypocketfft.irfftn(pypocketfft.rfftn(a,(ax,)),(ax,),lastsize=n,fct=1./n), a, atol=2e-15*vmax, rtol=0) assert_allclose(pypocketfft.irfftn(pypocketfft.rfftn(b,(ax,)),(ax,),lastsize=n,fct=1./n), b, atol=6e-7*vmax, rtol=0) @pmp("shp", shapes) def test_identity_r2(shp): a=np.random.rand(*shp)-0.5 + 1j*np.random.rand(*shp)-0.5j a=pypocketfft.rfftn(pypocketfft.irfftn(a)) fct = 1./pypocketfft.irfftn(a).size vmax = np.max(np.abs(a)) assert_allclose(pypocketfft.rfftn(pypocketfft.irfftn(a),fct=fct), a, atol=2e-15*vmax, rtol=0) @pmp("shp", shapes2D+shapes3D) def test_hartley2(shp): a=np.random.rand(*shp)-0.5 v1 = pypocketfft.hartley2(a) v2 = pypocketfft.fftn(a.astype(np.complex128)) vmax = np.max(np.abs(v1)) v2 = v2.real+v2.imag assert_allclose(v1, v2, atol=2e-15*vmax, rtol=0) @pmp("shp", shapes) def test_hartley_identity(shp): a=np.random.rand(*shp)-0.5 v1 = pypocketfft.hartley(pypocketfft.hartley(a))/a.size vmax = np.max(np.abs(a)) assert_allclose(a, v1, atol=2e-15*vmax, rtol=0) @pmp("shp", shapes) def test_hartley2_identity(shp): a=np.random.rand(*shp)-0.5 v1 = pypocketfft.hartley2(pypocketfft.hartley2(a))/a.size vmax = np.max(np.abs(a)) assert_allclose(a, v1, atol=2e-15*vmax, rtol=0) @pmp("shp", shapes2D) @pmp("axes", ((0,),(1,),(0,1),(1,0))) def test_hartley2_2D(shp, axes): a=np.random.rand(*shp)-0.5 fct = 1./np.prod(np.take(shp,axes)) vmax = np.max(np.abs(a)) assert_allclose(pypocketfft.hartley2(pypocketfft.hartley2(a, axes=axes), axes=axes, fct=fct),a , atol=2e-15*vmax, rtol=0)