Skip to content

Commit e2516ac

Browse files
Added tests for higher dimensional transforms
1 parent 92d36b9 commit e2516ac

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed

mkl_fft/tests/test_fftnd.py

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
#!/usr/bin/env python
2+
# Copyright (c) 2017, Intel Corporation
3+
#
4+
# Redistribution and use in source and binary forms, with or without
5+
# modification, are permitted provided that the following conditions are met:
6+
#
7+
# * Redistributions of source code must retain the above copyright notice,
8+
# this list of conditions and the following disclaimer.
9+
# * Redistributions in binary form must reproduce the above copyright
10+
# notice, this list of conditions and the following disclaimer in the
11+
# documentation and/or other materials provided with the distribution.
12+
# * Neither the name of Intel Corporation nor the names of its contributors
13+
# may be used to endorse or promote products derived from this software
14+
# without specific prior written permission.
15+
#
16+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22+
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23+
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24+
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
27+
from __future__ import division, absolute_import, print_function
28+
29+
import numpy as np
30+
from numpy.testing import (
31+
TestCase, run_module_suite, assert_, assert_raises, assert_equal,
32+
assert_warns, assert_allclose)
33+
from numpy import random as rnd
34+
import sys
35+
import warnings
36+
37+
import mkl_fft
38+
import numpy.fft.fftpack as np_fft
39+
40+
reps_64 = (2**11)*np.finfo(np.float64).eps
41+
reps_32 = (2**11)*np.finfo(np.float32).eps
42+
atol_64 = (2**8)*np.finfo(np.float64).eps
43+
atol_32 = (2**8)*np.finfo(np.float32).eps
44+
45+
def _get_rtol_atol(x):
46+
dt = x.dtype
47+
if dt == np.double or dt == np.complex128:
48+
return reps_64, atol_64
49+
elif dt == np.single or dt == np.complex64:
50+
return reps_32, atol_32
51+
else:
52+
assert (dt == np.double or dt == np.complex128 or dt == np.single or dt == np.complex64), "Unexpected dtype {}".format(dt)
53+
return reps_64, atol_64
54+
55+
56+
class Test_mklfft_matrix(TestCase):
57+
def setUp(self):
58+
rnd.seed(123456)
59+
self.md = rnd.randn(256, 256)
60+
self.mf = self.md.astype(np.float32)
61+
self.mz = rnd.randn(256, 256*2).view(np.complex128)
62+
self.mc = self.mz.astype(np.complex64)
63+
64+
def test_matrix1(self):
65+
"""fftn equals repeated fft"""
66+
for ar in [self.md, self.mz, self.mf, self.mc]:
67+
r_tol, a_tol = _get_rtol_atol(ar)
68+
d = ar.copy()
69+
t1 = mkl_fft.fftn(d)
70+
t2 = mkl_fft.fft(mkl_fft.fft(d, axis=0), axis=1)
71+
t3 = mkl_fft.fft(mkl_fft.fft(d, axis=1), axis=0)
72+
assert_allclose(t1, t2, rtol=r_tol, atol=a_tol, err_msg = "failed test for dtype {}, max abs diff: {}".format(d.dtype, np.max(np.abs(t1-t2))))
73+
assert_allclose(t1, t3, rtol=r_tol, atol=a_tol, err_msg = "failed test for dtype {}, max abs diff: {}".format(d.dtype, np.max(np.abs(t1-t3))))
74+
75+
def test_matrix2(self):
76+
"""ifftn(fftn(x)) is x"""
77+
for ar in [self.md, self.mz, self.mf, self.mc]:
78+
d = ar.copy()
79+
r_tol, a_tol = _get_rtol_atol(d)
80+
t = mkl_fft.ifftn(mkl_fft.fftn(d))
81+
assert_allclose(d, t, rtol=r_tol, atol=a_tol, err_msg = "failed test for dtype {}, max abs diff: {}".format(d.dtype, np.max(np.abs(d-t))))
82+
83+
def test_matrix3(self):
84+
"""fftn(ifftn(x)) is x"""
85+
for ar in [self.md, self.mz, self.mf, self.mc]:
86+
d = ar.copy()
87+
r_tol, a_tol = _get_rtol_atol(d)
88+
t = mkl_fft.fftn(mkl_fft.ifftn(d))
89+
assert_allclose(d, t, rtol=r_tol, atol=a_tol, err_msg = "failed test for dtype {}, max abs diff: {}".format(d.dtype, np.max(np.abs(d-t))))
90+
91+
92+
def test_matrix4(self):
93+
"""fftn of strided array is same as fftn of a contiguous copy"""
94+
for ar in [self.md, self.mz, self.mf, self.mc]:
95+
r_tol, a_tol = _get_rtol_atol(ar)
96+
d_strided = ar[::2,::2]
97+
d_contig = d_strided.copy()
98+
t_strided = mkl_fft.fftn(d_strided)
99+
t_contig = mkl_fft.fftn(d_contig)
100+
assert_allclose(t_strided, t_contig, rtol=r_tol, atol=a_tol)
101+
102+
103+
class Test_Regressions(TestCase):
104+
105+
def setUp(self):
106+
rnd.seed(123456)
107+
self.ad = rnd.randn(32, 17, 23)
108+
self.af = self.ad.astype(np.float32)
109+
self.az = rnd.randn(32, 17, 23*2).view(np.complex128)
110+
self.ac = self.az.astype(np.complex64)
111+
112+
def test_cf_contig(self):
113+
"""fft of F-contiguous array is the same as of C-contiguous with same data"""
114+
for ar in [self.ad, self.af, self.az, self.ac]:
115+
r_tol, a_tol = _get_rtol_atol(ar)
116+
d_ccont = ar.copy()
117+
d_fcont = np.asfortranarray(d_ccont)
118+
f1 = mkl_fft.fft(d_ccont)
119+
f2 = mkl_fft.fft(d_fcont)
120+
assert_allclose(f1, f2, rtol=r_tol, atol=a_tol)

0 commit comments

Comments
 (0)