diagonal_operator.py 7.18 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# NIFTy
# Copyright (C) 2017  Theo Steininger
#
# Author: Theo Steininger
#
# 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/>.
18
19
20
21
22
23

import numpy as np

from d2o import distributed_data_object,\
                STRATEGIES as DISTRIBUTION_STRATEGIES

24
from nifty.config import nifty_configuration as gc
25
26
27
28
29
30
31
32
from nifty.field import Field
from nifty.operators.endomorphic_operator import EndomorphicOperator


class DiagonalOperator(EndomorphicOperator):

    # ---Overwritten properties and methods---

Martin Reinecke's avatar
Martin Reinecke committed
33
    def __init__(self, domain=(),
34
35
                 diagonal=None, bare=False, copy=True,
                 distribution_strategy=None):
36
        self._domain = self._parse_domain(domain)
37

38
        if distribution_strategy is None:
39
            if isinstance(diagonal, distributed_data_object):
40
                distribution_strategy = diagonal.distribution_strategy
41
            elif isinstance(diagonal, Field):
42
                distribution_strategy = diagonal.distribution_strategy
43

44
        self._distribution_strategy = self._parse_distribution_strategy(
45
46
                               distribution_strategy=distribution_strategy,
                               val=diagonal)
47
48
49

        self.set_diagonal(diagonal=diagonal, bare=bare, copy=copy)

50
51
    def _times(self, x, spaces):
        return self._times_helper(x, spaces, operation=lambda z: z.__mul__)
52

53
54
    def _adjoint_times(self, x, spaces):
        return self._times_helper(x, spaces,
55
                                  operation=lambda z: z.adjoint().__mul__)
56

57
58
    def _inverse_times(self, x, spaces):
        return self._times_helper(x, spaces, operation=lambda z: z.__rdiv__)
59

60
61
    def _adjoint_inverse_times(self, x, spaces):
        return self._times_helper(x, spaces,
62
                                  operation=lambda z: z.adjoint().__rdiv__)
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96

    def diagonal(self, bare=False, copy=True):
        if bare:
            diagonal = self._diagonal.weight(power=-1)
        elif copy:
            diagonal = self._diagonal.copy()
        else:
            diagonal = self._diagonal
        return diagonal

    def inverse_diagonal(self, bare=False):
        return 1/self.diagonal(bare=bare, copy=False)

    def trace(self, bare=False):
        return self.diagonal(bare=bare, copy=False).sum()

    def inverse_trace(self, bare=False):
        return self.inverse_diagonal(bare=bare, copy=False).sum()

    def trace_log(self):
        log_diagonal = self.diagonal(copy=False).apply_scalar_function(np.log)
        return log_diagonal.sum()

    def determinant(self):
        return self.diagonal(copy=False).val.prod()

    def inverse_determinant(self):
        return 1/self.determinant()

    def log_determinant(self):
        return np.log(self.determinant())

    # ---Mandatory properties and methods---

97
98
99
100
    @property
    def domain(self):
        return self._domain

101
    @property
Martin Reinecke's avatar
Martin Reinecke committed
102
103
104
105
    def self_adjoint(self):
        if self._self_adjoint is None:
            self._self_adjoint = (self._diagonal.val.imag == 0).all()
        return self._self_adjoint
106
107
108

    @property
    def unitary(self):
109
110
111
        if self._unitary is None:
            self._unitary = (self._diagonal.val *
                             self._diagonal.val.conjugate() == 1).all()
112
113
114
115
116
        return self._unitary

    # ---Added properties and methods---

    @property
117
118
    def distribution_strategy(self):
        return self._distribution_strategy
119

120
121
    def _parse_distribution_strategy(self, distribution_strategy, val):
        if distribution_strategy is None:
122
            if isinstance(val, distributed_data_object):
123
                distribution_strategy = val.distribution_strategy
124
            elif isinstance(val, Field):
125
                distribution_strategy = val.distribution_strategy
126
            else:
127
                self.logger.info("Datamodel set to default!")
128
129
                distribution_strategy = gc['default_distribution_strategy']
        elif distribution_strategy not in DISTRIBUTION_STRATEGIES['all']:
130
131
            raise ValueError(
                    "Invalid distribution_strategy!")
132
        return distribution_strategy
133
134
135
136
137

    def set_diagonal(self, diagonal, bare=False, copy=True):
        # use the casting functionality from Field to process `diagonal`
        f = Field(domain=self.domain,
                  val=diagonal,
138
                  distribution_strategy=self.distribution_strategy,
139
140
                  copy=copy)

Martin Reinecke's avatar
Martin Reinecke committed
141
        # weight if the given values were `bare` is True
142
        # do inverse weightening if the other way around
Martin Reinecke's avatar
Martin Reinecke committed
143
        if bare:
144
145
146
            # If `copy` is True, we won't change external data by weightening
            # Otherwise, inplace weightening would change the external field
            f.weight(inplace=copy)
147

Martin Reinecke's avatar
Martin Reinecke committed
148
149
        # Reset the self_adjoint property:
        self._self_adjoint = None
150

151
152
        # Reset the unitarity property
        self._unitary = None
153
154
155

        # store the diagonal-field
        self._diagonal = f
156

157
158
    def _times_helper(self, x, spaces, operation):
        # if the domain matches directly
159
        # -> multiply the fields directly
160
        if x.domain == self.domain:
161
162
163
164
165
166
167
            # here the actual multiplication takes place
            return operation(self.diagonal(copy=False))(x)

        # if the distribution_strategy of self is sub-slice compatible to
        # the one of x, reshape the local data of self and apply it directly
        active_axes = []
        if spaces is None:
168
            active_axes = range(len(x.shape))
169
170
171
172
173
174
175
176
177
178
        else:
            for space_index in spaces:
                active_axes += x.domain_axes[space_index]

        axes_local_distribution_strategy = \
            x.val.get_axes_local_distribution_strategy(active_axes)
        if axes_local_distribution_strategy == self.distribution_strategy:
            local_diagonal = self._diagonal.val.get_local_data(copy=False)
        else:
            # create an array that is sub-slice compatible
179
180
            self.logger.warn("The input field is not sub-slice compatible to "
                             "the distribution strategy of the operator.")
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
            redistr_diagonal_val = self._diagonal.val.copy(
                distribution_strategy=axes_local_distribution_strategy)
            local_diagonal = redistr_diagonal_val.get_local_data(copy=False)

        reshaper = [x.shape[i] if i in active_axes else 1
                    for i in xrange(len(x.shape))]
        reshaped_local_diagonal = np.reshape(local_diagonal, reshaper)

        # here the actual multiplication takes place
        local_result = operation(reshaped_local_diagonal)(
                           x.val.get_local_data(copy=False))

        result_field = x.copy_empty(dtype=local_result.dtype)
        result_field.val.set_local_data(local_result, copy=False)
        return result_field