fft.py 1.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Copyright(C) 2013-2019 Max-Planck-Society
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik.

18

Martin Reinecke's avatar
Martin Reinecke committed
19
_nthreads = 1
20

Martin Reinecke's avatar
Martin Reinecke committed
21

Martin Reinecke's avatar
Martin Reinecke committed
22
23
def nthreads():
    return _nthreads
Martin Reinecke's avatar
Martin Reinecke committed
24
25


Martin Reinecke's avatar
Martin Reinecke committed
26
27
def set_nthreads(nthr):
    global _nthreads
Martin Reinecke's avatar
Martin Reinecke committed
28
    _nthreads = int(nthr)
Martin Reinecke's avatar
Martin Reinecke committed
29
30


Martin Reinecke's avatar
Martin Reinecke committed
31
try:
32
    import ducc0.fft as my_fft
Martin Reinecke's avatar
Martin Reinecke committed
33
34


Martin Reinecke's avatar
Martin Reinecke committed
35
    def fftn(a, axes=None):
Martin Reinecke's avatar
switch  
Martin Reinecke committed
36
        return my_fft.c2c(a, axes=axes, nthreads=max(_nthreads, 0))
37
38


Martin Reinecke's avatar
Martin Reinecke committed
39
    def ifftn(a, axes=None):
Martin Reinecke's avatar
switch  
Martin Reinecke committed
40
41
        return my_fft.c2c(a, axes=axes, inorm=2, forward=False,
                          nthreads=max(_nthreads, 0))
Martin Reinecke's avatar
Martin Reinecke committed
42
43
44


    def hartley(a, axes=None):
Martin Reinecke's avatar
switch  
Martin Reinecke committed
45
        return my_fft.genuine_hartley(a, axes=axes, nthreads=max(_nthreads, 0))
Martin Reinecke's avatar
Martin Reinecke committed
46

Martin Reinecke's avatar
tweaks  
Martin Reinecke committed
47
except ImportError:
Martin Reinecke's avatar
Martin Reinecke committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
    import scipy.fft


    def fftn(a, axes=None):
        return scipy.fft.fftn(a, axes=axes, workers=_nthreads)


    def ifftn(a, axes=None):
        return scipy.fft.ifftn(a, axes=axes, workers=_nthreads)


    def hartley(a, axes=None):
        tmp = scipy.fft.fftn(a, axes=axes, workers=_nthreads)
        return tmp.real+tmp.imag