rgrgtransformation.py 3.08 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
# 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/>.
Theo Steininger's avatar
Theo Steininger committed
13
14
15
16
17
#
# Copyright(C) 2013-2017 Max-Planck-Society
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik
# and financially supported by the Studienstiftung des deutschen Volkes.
18

Martin Reinecke's avatar
Martin Reinecke committed
19
from __future__ import division
Jait Dixit's avatar
Jait Dixit committed
20
import numpy as np
Martin Reinecke's avatar
Martin Reinecke committed
21
from .transformation import Transformation
Martin Reinecke's avatar
Martin Reinecke committed
22
from .rg_transforms import SerialFFT
Martin Reinecke's avatar
more    
Martin Reinecke committed
23
from .... import RGSpace
Jait Dixit's avatar
Jait Dixit committed
24
25

class RGRGTransformation(Transformation):
26
27
28

    # ---Overwritten properties and methods---

Martin Reinecke's avatar
Martin Reinecke committed
29
30
31
    def __init__(self, domain, codomain=None):
        super(RGRGTransformation, self).__init__(domain, codomain)
        self._transform = SerialFFT(self.domain, self.codomain)
Jait Dixit's avatar
Jait Dixit committed
32

33
34
35
36
37
38
    # ---Mandatory properties and methods---

    @property
    def unitary(self):
        return True

Jait Dixit's avatar
Jait Dixit committed
39

Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
40
    def transform(self, val, axes=None):
Jait Dixit's avatar
Jait Dixit committed
41
42
43
44
45
46
47
48
49
50
51
52
53
        """
        RG -> RG transform method.

        Parameters
        ----------
        val : np.ndarray or distributed_data_object
            The value array which is to be transformed

        axes : None or tuple
            The axes along which the transformation should take place

        """
        if self._transform.codomain.harmonic:
54
55
56
57
58
            # correct for forward fft.
            # naively one would set power to 0.5 here in order to
            # apply effectively a factor of 1/sqrt(N) to the field.
            # BUT: the pixel volumes of the domain and codomain are different.
            # Hence, in order to produce the same scalar product, power===1.
Jait Dixit's avatar
Jait Dixit committed
59
            val = self._transform.domain.weight(val, power=1, axes=axes)
Jait Dixit's avatar
Jait Dixit committed
60
61

        # Perform the transformation
Martin Reinecke's avatar
more    
Martin Reinecke committed
62
        if issubclass(val.dtype.type, np.complexfloating):
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
63
64
            Tval_real = self._transform.transform(val.real, axes)
            Tval_imag = self._transform.transform(val.imag, axes)
Martin Reinecke's avatar
more    
Martin Reinecke committed
65
66
            if self.codomain.harmonic:
                Tval_real.real += Tval_real.imag
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
67
                Tval_real.imag = Tval_imag.real + Tval_imag.imag
Martin Reinecke's avatar
more    
Martin Reinecke committed
68
69
            else:
                Tval_real.real -= Tval_real.imag
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
70
                Tval_real.imag = Tval_imag.real - Tval_imag.imag
Martin Reinecke's avatar
more    
Martin Reinecke committed
71
72

            Tval = Tval_real
73
        else:
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
74
            Tval = self._transform.transform(val, axes)
Martin Reinecke's avatar
more    
Martin Reinecke committed
75
76
            if self.codomain.harmonic:
                Tval.real += Tval.imag
77
            else:
Martin Reinecke's avatar
more    
Martin Reinecke committed
78
79
                Tval.real -= Tval.imag
            Tval = Tval.real
Jait Dixit's avatar
Jait Dixit committed
80
81

        if not self._transform.codomain.harmonic:
82
83
            # correct for inverse fft.
            # See discussion above.
Jait Dixit's avatar
Jait Dixit committed
84
            Tval = self._transform.codomain.weight(Tval, power=-1, axes=axes)
Jait Dixit's avatar
Jait Dixit committed
85
86

        return Tval