diagonal_operator.py 5.43 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
from ..field import Field
from ..domain_tuple import DomainTuple
from .endomorphic_operator import EndomorphicOperator
Martin Reinecke's avatar
Martin Reinecke committed
24
from ..utilities import cast_iseq_to_tuple
Martin Reinecke's avatar
Martin Reinecke committed
25
from .. import dobj
Martin Reinecke's avatar
Martin Reinecke committed
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
    Parameters
    ----------
Martin Reinecke's avatar
Martin Reinecke committed
37
    diagonal : Field
38
39
        The diagonal entries of the operator
        (already containing volume factors).
Martin Reinecke's avatar
Martin Reinecke committed
40
    domain : tuple of DomainObjects
41
42
43
44
45
        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.
46
47
48

    Attributes
    ----------
Martin Reinecke's avatar
Martin Reinecke committed
49
    domain : DomainTuple
50
        The domain on which the Operator's input Field lives.
Martin Reinecke's avatar
Martin Reinecke committed
51
    target : DomainTuple
52
53
54
55
56
        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
57
        Indicates whether the operator is self-adjoint or not.
58

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

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

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

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

Martin Reinecke's avatar
Martin Reinecke committed
98
99
        self._diagonal = diagonal.copy()

Martin Reinecke's avatar
tweak  
Martin Reinecke committed
100
101
102
103
104
        if self._spaces is not None:
            active_axes = []
            for space_index in self._spaces:
                active_axes += self._domain.axes[space_index]

Martin Reinecke's avatar
Martin Reinecke committed
105
106
107
108
109
            if self._spaces[0] == 0:
                self._ldiag = dobj.local_data(self._diagonal.val)
            else:
                self._ldiag = dobj.to_global_data(self._diagonal.val)
            locshape = dobj.local_shape(self._domain.shape, 0)
Martin Reinecke's avatar
tweak  
Martin Reinecke committed
110
            self._reshaper = [shp if i in active_axes else 1
Martin Reinecke's avatar
Martin Reinecke committed
111
112
113
114
115
                              for i, shp in enumerate(locshape)]
            self._ldiag = self._ldiag.reshape(self._reshaper)

        else:
            self._ldiag = dobj.local_data(self._diagonal.val)
116

117
118
        self._self_adjoint = None
        self._unitary = None
119

120
    def _times(self, x):
Martin Reinecke's avatar
Martin Reinecke committed
121
        return Field(x.domain, val=x.val*self._ldiag)
122

123
    def _adjoint_times(self, x):
Martin Reinecke's avatar
Martin Reinecke committed
124
        return Field(x.domain, val=x.val*self._ldiag.conj())
125

126
    def _inverse_times(self, x):
Martin Reinecke's avatar
Martin Reinecke committed
127
        return Field(x.domain, val=x.val/self._ldiag)
128

129
    def _adjoint_inverse_times(self, x):
Martin Reinecke's avatar
Martin Reinecke committed
130
        return Field(x.domain, val=x.val/self._ldiag.conj())
131

132
    def diagonal(self):
Martin Reinecke's avatar
Martin Reinecke committed
133
        """ Returns the diagonal of the Operator."""
134
        return self._diagonal.copy()
135

136
137
    @property
    def domain(self):
138
        return self._domain
139

140
    @property
Martin Reinecke's avatar
Martin Reinecke committed
141
142
    def self_adjoint(self):
        if self._self_adjoint is None:
143
            if not np.issubdtype(self._diagonal.dtype, np.complexfloating):
Martin Reinecke's avatar
Martin Reinecke committed
144
145
146
                self._self_adjoint = True
            else:
                self._self_adjoint = (self._diagonal.val.imag == 0).all()
Martin Reinecke's avatar
Martin Reinecke committed
147
        return self._self_adjoint
148
149
150

    @property
    def unitary(self):
151
        if self._unitary is None:
Martin Reinecke's avatar
Martin Reinecke committed
152
            self._unitary = (abs(self._diagonal.val) == 1.).all()
153
        return self._unitary