diagonal_operator.py 7.26 KB
Newer Older
1
2
3
4
5
6
7
# -*- coding: utf-8 -*-

import numpy as np

from d2o import distributed_data_object,\
                STRATEGIES as DISTRIBUTION_STRATEGIES

8
from nifty.config import nifty_configuration as gc
9
10
11
12
13
14
15
16
from nifty.field import Field
from nifty.operators.endomorphic_operator import EndomorphicOperator


class DiagonalOperator(EndomorphicOperator):

    # ---Overwritten properties and methods---

17
    def __init__(self, domain=(), field_type=(), implemented=True,
18
19
                 diagonal=None, bare=False, copy=True,
                 distribution_strategy=None):
20
21
        self._domain = self._parse_domain(domain)
        self._field_type = self._parse_field_type(field_type)
22

23
24
        self._implemented = bool(implemented)

25
        if distribution_strategy is None:
26
            if isinstance(diagonal, distributed_data_object):
27
                distribution_strategy = diagonal.distribution_strategy
28
            elif isinstance(diagonal, Field):
29
                distribution_strategy = diagonal.distribution_strategy
30

31
        self._distribution_strategy = self._parse_distribution_strategy(
32
33
                               distribution_strategy=distribution_strategy,
                               val=diagonal)
34
35
36
37

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

    def _times(self, x, spaces, types):
38
39
        return self._times_helper(x, spaces, types,
                                  operation=lambda z: z.__mul__)
40
41

    def _adjoint_times(self, x, spaces, types):
42
43
        return self._times_helper(x, spaces, types,
                                  operation=lambda z: z.adjoint().__mul__)
44
45

    def _inverse_times(self, x, spaces, types):
46
47
        return self._times_helper(x, spaces, types,
                                  operation=lambda z: z.__rdiv__)
48
49

    def _adjoint_inverse_times(self, x, spaces, types):
50
51
        return self._times_helper(x, spaces, types,
                                  operation=lambda z: z.adjoint().__rdiv__)
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85

    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---

86
87
88
89
90
91
92
93
    @property
    def domain(self):
        return self._domain

    @property
    def field_type(self):
        return self._field_type

94
95
96
97
    @property
    def implemented(self):
        return self._implemented

98
99
100
101
102
103
104
105
106
107
108
    @property
    def symmetric(self):
        return self._symmetric

    @property
    def unitary(self):
        return self._unitary

    # ---Added properties and methods---

    @property
109
110
    def distribution_strategy(self):
        return self._distribution_strategy
111

112
113
    def _parse_distribution_strategy(self, distribution_strategy, val):
        if distribution_strategy is None:
114
            if isinstance(val, distributed_data_object):
115
                distribution_strategy = val.distribution_strategy
116
            elif isinstance(val, Field):
117
                distribution_strategy = val.distribution_strategy
118
            else:
119
                self.logger.info("Datamodel set to default!")
120
121
                distribution_strategy = gc['default_distribution_strategy']
        elif distribution_strategy not in DISTRIBUTION_STRATEGIES['all']:
122
123
            raise ValueError(
                    "Invalid distribution_strategy!")
124
        return distribution_strategy
125
126
127
128
129
130

    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,
                  field_type=self.field_type,
131
                  distribution_strategy=self.distribution_strategy,
132
133
                  copy=copy)

134
135
136
137
138
139
140
141
142
143
        # weight if the given values were `bare` and `implemented` is True
        # do inverse weightening if the other way around
        if bare and self.implemented:
            # If `copy` is True, we won't change external data by weightening
            # Otherwise, inplace weightening would change the external field
            f.weight(inplace=copy)
        elif not bare and not self.implemented:
            # If `copy` is True, we won't change external data by weightening
            # Otherwise, inplace weightening would change the external field
            f.weight(inplace=copy, power=-1)
144
145
146
147
148
149
150
151
152

        # check if the operator is symmetric:
        self._symmetric = (f.val.imag == 0).all()

        # check if the operator is unitary:
        self._unitary = (f.val * f.val.conjugate() == 1).all()

        # store the diagonal-field
        self._diagonal = f
153
154
155
156
157
158
159
160
161
162
163
164

    def _times_helper(self, x, spaces, types, operation):
        # if the domain and field_type match directly
        # -> multiply the fields directly
        if x.domain == self.domain and x.field_type == self.field_type:
            # 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:
165
166
167
            if self.domain != ():
                for axes in x.domain_axes:
                    active_axes += axes
168
169
170
171
172
        else:
            for space_index in spaces:
                active_axes += x.domain_axes[space_index]

        if types is None:
173
174
175
            if self.field_type != ():
                for axes in x.field_type_axes:
                    active_axes += axes
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
        else:
            for type_index in types:
                active_axes += x.field_type_axes[type_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
            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