diagonal_operator.py 4.99 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
20
from __future__ import division
from builtins import range
21
22
import numpy as np

Martin Reinecke's avatar
Martin Reinecke committed
23
from ...field import Field
Martin Reinecke's avatar
Martin Reinecke committed
24
from ...domain_tuple import DomainTuple
Martin Reinecke's avatar
Martin Reinecke committed
25
from ..endomorphic_operator import EndomorphicOperator
26
27
28


class DiagonalOperator(EndomorphicOperator):
Theo Steininger's avatar
Theo Steininger committed
29
30
31
32
33
    """ NIFTY class for diagonal operators.

    The NIFTY DiagonalOperator class is a subclass derived from the
    EndomorphicOperator. It multiplies an input field pixel-wise with its
    diagonal.
34

35
36
37

    Parameters
    ----------
Martin Reinecke's avatar
Martin Reinecke committed
38
    diagonal : Field
39
40
41
        The diagonal entries of the operator.
    copy : boolean
        Internal copy of the diagonal (default: True)
42
43
44
    default_spaces : tuple of ints *optional*
        Defines on which space(s) of a given field the Operator acts by
        default (default: None)
45
46
47

    Attributes
    ----------
48
49
50
51
52
53
54
55
56
    domain : tuple of DomainObjects, i.e. Spaces and FieldTypes
        The domain on which the Operator's input Field lives.
    target : tuple of DomainObjects, i.e. Spaces and FieldTypes
        The domain in which the outcome of the operator lives. As the Operator
        is endomorphic this is the same as its domain.
    unitary : boolean
        Indicates whether the Operator is unitary or not.
    self_adjoint : boolean
        Indicates whether the operator is self_adjoint or not.
57
58
59
60
61
62
63
64
65
66

    Raises
    ------

    See Also
    --------
    EndomorphicOperator

    """

67
68
    # ---Overwritten properties and methods---

Martin Reinecke's avatar
Martin Reinecke committed
69
    def __init__(self, diagonal, copy=True, default_spaces=None):
70
71
        super(DiagonalOperator, self).__init__(default_spaces)

Martin Reinecke's avatar
Martin Reinecke committed
72
73
74
        if not isinstance(diagonal, Field):
            raise TypeError("Field object required")
        self._diagonal = diagonal if not copy else diagonal.copy()
75
76
        self._self_adjoint = None
        self._unitary = None
77

78
79
    def _times(self, x, spaces):
        return self._times_helper(x, spaces, operation=lambda z: z.__mul__)
80

81
82
    def _adjoint_times(self, x, spaces):
        return self._times_helper(x, spaces,
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
83
                                  operation=lambda z: z.conjugate().__mul__)
84

85
    def _inverse_times(self, x, spaces):
Martin Reinecke's avatar
Martin Reinecke committed
86
87
        return self._times_helper(x, spaces,
                                  operation=lambda z: z.__rtruediv__)
88

89
90
    def _adjoint_inverse_times(self, x, spaces):
        return self._times_helper(x, spaces,
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
91
                                  operation=lambda z:
92
                                      z.conjugate().__rtruediv__)
93

Martin Reinecke's avatar
Martin Reinecke committed
94
    def diagonal(self, copy=True):
95
96
97
98
99
100
101
102
103
104
105
106
107
        """ Returns the diagonal of the Operator.

        Parameters
        ----------
        copy : boolean
            Whether the returned Field should be copied or not.

        Returns
        -------
        out : Field
            The diagonal of the Operator.

        """
Martin Reinecke's avatar
Martin Reinecke committed
108
        return self._diagonal.copy() if copy else self._diagonal
109

110
111
    # ---Mandatory properties and methods---

112
113
    @property
    def domain(self):
Martin Reinecke's avatar
Martin Reinecke committed
114
        return self._diagonal.domain
115

116
    @property
Martin Reinecke's avatar
Martin Reinecke committed
117
118
    def self_adjoint(self):
        if self._self_adjoint is None:
Martin Reinecke's avatar
Martin Reinecke committed
119
120
121
122
            if not issubclass(self._diagonal.dtype.type, np.complexfloating):
                self._self_adjoint = True
            else:
                self._self_adjoint = (self._diagonal.val.imag == 0).all()
Martin Reinecke's avatar
Martin Reinecke committed
123
        return self._self_adjoint
124
125
126

    @property
    def unitary(self):
127
        if self._unitary is None:
Martin Reinecke's avatar
Martin Reinecke committed
128
            self._unitary = (abs(self._diagonal.val) == 1.).all()
129
130
131
132
        return self._unitary

    # ---Added properties and methods---

133
134
    def _times_helper(self, x, spaces, operation):
        # if the domain matches directly
135
        # -> multiply the fields directly
136
        if x.domain == self.domain:
137
            # here the actual multiplication takes place
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
138
            return operation(self._diagonal)(x)
139
140

        if spaces is None:
Martin Reinecke's avatar
Martin Reinecke committed
141
            active_axes = range(len(x.shape))
142
        else:
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
143
            active_axes = []
144
            for space_index in spaces:
Martin Reinecke's avatar
Martin Reinecke committed
145
                active_axes += x.domain.axes[space_index]
146

Martin Reinecke's avatar
Martin Reinecke committed
147
        reshaper = [x.shape[i] if i in active_axes else 1
Martin Reinecke's avatar
Martin Reinecke committed
148
                    for i in range(len(x.shape))]
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
149
        reshaped_local_diagonal = np.reshape(self._diagonal.val, reshaper)
150
151

        # here the actual multiplication takes place
152
        return Field(x.domain, val=operation(reshaped_local_diagonal)(x.val))