diagonal_operator.py 5.42 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
20
import numpy as np
Martin Reinecke's avatar
Martin Reinecke committed
21
22
23
24
from ..field import Field
from ..domain_tuple import DomainTuple
from .endomorphic_operator import EndomorphicOperator
from ..nifty_utilities import cast_iseq_to_tuple
25

26

27
class DiagonalOperator(EndomorphicOperator):
Theo Steininger's avatar
Theo Steininger committed
28
29
30
31
32
    """ 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.
33

34
35
    Parameters
    ----------
Martin Reinecke's avatar
Martin Reinecke committed
36
    diagonal : Field
37
38
        The diagonal entries of the operator
        (already containing volume factors).
39
40
41
42
43
44
    domain : tuple of DomainObjects, i.e. Spaces and FieldTypes
        The domain on which the Operator's input Field lives.
        If None, use the domain of "diagonal".
    spaces : tuple of int
        The elements of "domain" on which the operator acts.
        If None, it acts on all elements.
45
46
47

    Attributes
    ----------
48
49
50
51
52
53
54
55
    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
56
        Indicates whether the operator is self-adjoint or not.
57

Martin Reinecke's avatar
Martin Reinecke committed
58
    NOTE: the fields given to __init__ and returned from .diagonal() are
59
60
    considered to be non-bare, i.e. during operator application, no additional
    volume factors are applied!
Martin Reinecke's avatar
Martin Reinecke committed
61

62
63
64
65
66
    See Also
    --------
    EndomorphicOperator
    """

67
    def __init__(self, diagonal, domain=None, spaces=None):
68
        super(DiagonalOperator, self).__init__()
69

Martin Reinecke's avatar
Martin Reinecke committed
70
71
        if not isinstance(diagonal, Field):
            raise TypeError("Field object required")
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
        if domain is None:
            self._domain = diagonal.domain
        else:
            self._domain = DomainTuple.make(domain)
        if spaces is None:
            self._spaces = None
            if diagonal.domain != self._domain:
                raise ValueError("domain mismatch")
        else:
            self._spaces = cast_iseq_to_tuple(spaces)
            nspc = len(self._spaces)
            if nspc != len(diagonal.domain.domains):
                raise ValueError("spaces and domain must have the same length")
            if nspc > len(self._domain.domains):
                raise ValueError("too many spaces")
            if nspc > len(set(self._spaces)):
                raise ValueError("non-unique space indices")
            # if nspc==len(self.diagonal.domain.domains, we could do some optimization
90
            for i, j in enumerate(self._spaces):
91
92
93
                if diagonal.domain[i] != self._domain[j]:
                    raise ValueError("domain mismatch")

94
        self._diagonal = diagonal.copy()
95
96
        self._self_adjoint = None
        self._unitary = None
97

98
99
    def _times(self, x):
        return self._times_helper(x, lambda z: z.__mul__)
100

101
102
    def _adjoint_times(self, x):
        return self._times_helper(x, lambda z: z.conjugate().__mul__)
103

104
105
    def _inverse_times(self, x):
        return self._times_helper(x, lambda z: z.__rtruediv__)
106

107
108
    def _adjoint_inverse_times(self, x):
        return self._times_helper(x, lambda z: z.conjugate().__rtruediv__)
109

110
    def diagonal(self):
111
112
113
114
115
116
117
        """ Returns the diagonal of the Operator.

        Returns
        -------
        out : Field
            The diagonal of the Operator.
        """
118
        return self._diagonal.copy()
119

120
121
    @property
    def domain(self):
122
        return self._domain
123

124
    @property
Martin Reinecke's avatar
Martin Reinecke committed
125
126
    def self_adjoint(self):
        if self._self_adjoint is None:
Martin Reinecke's avatar
Martin Reinecke committed
127
128
129
130
            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
131
        return self._self_adjoint
132
133
134

    @property
    def unitary(self):
135
        if self._unitary is None:
Martin Reinecke's avatar
Martin Reinecke committed
136
            self._unitary = (abs(self._diagonal.val) == 1.).all()
137
138
        return self._unitary

139
140
    def _times_helper(self, x, operation):
        if self._spaces is None:
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
141
            return operation(self._diagonal)(x)
142

143
144
145
        active_axes = []
        for space_index in self._spaces:
            active_axes += x.domain.axes[space_index]
146

Martin Reinecke's avatar
Martin Reinecke committed
147
148
        reshaper = [shp if i in active_axes else 1
                    for i, shp in enumerate(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))