projection_operator.py 5.82 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 builtins import range
Theo Steininger's avatar
Theo Steininger committed
20
21
import numpy as np

22
23
from ...field import Field
from ..endomorphic_operator import EndomorphicOperator
Theo Steininger's avatar
Theo Steininger committed
24
25
26


class ProjectionOperator(EndomorphicOperator):
Theo Steininger's avatar
Theo Steininger committed
27
    """ NIFTY class for projection operators.
Theo Steininger's avatar
Theo Steininger committed
28

Theo Steininger's avatar
Theo Steininger committed
29
30
    The NIFTY ProjectionOperator class is a class derived from the
    EndomorphicOperator.
31
32
33

    Parameters
    ----------
Theo Steininger's avatar
Theo Steininger committed
34
    projection_field : Field
35
        Field on which the operator projects
36
37
38
    default_spaces : tuple of ints *optional*
        Defines on which space(s) of a given field the Operator acts by
        default (default: None)
39
40
41

    Attributes
    ----------
42
43
44
45
46
47
48
49
50
    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.
51
52
53
54
55

    Raises
    ------
    TypeError
        Raised if
Theo Steininger's avatar
Theo Steininger committed
56
            * if projection_field is not a Field
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77

    Notes
    -----


    Examples
    --------
    >>> x_space = RGSpace(5)
    >>> f1 = Field(x_space, val=3.)
    >>> f2 = Field(x_space, val=5.)
    >>> P = ProjectionOperator(f1)
    >>> res = P.times(f2)
    >>> res.val
    <distributed_data_object>
    array([ 225.,  225.,  225.,  225.,  225.])

    See Also
    --------
    EndomorphicOperator

    """
Theo Steininger's avatar
Theo Steininger committed
78

Theo Steininger's avatar
Theo Steininger committed
79
80
    # ---Overwritten properties and methods---

81
82
83
    def __init__(self, projection_field, default_spaces=None):
        super(ProjectionOperator, self).__init__(default_spaces)

Theo Steininger's avatar
Theo Steininger committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
        if not isinstance(projection_field, Field):
            raise TypeError("The projection_field must be a NIFTy-Field"
                            "instance.")
        self._projection_field = projection_field
        self._unitary = None

    def _times(self, x, spaces):
        # if the domain matches directly
        # -> multiply the fields directly
        if x.domain == self.domain:
            # here the actual multiplication takes place
            dotted = (self._projection_field * x).sum()
            return self._projection_field * dotted

        # 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:
Martin Reinecke's avatar
Martin Reinecke committed
102
            active_axes = list(range(len(x.shape)))
Theo Steininger's avatar
Theo Steininger committed
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
        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._projection_field.distribution_strategy:
            local_projection_vector = \
                self._projection_field.val.get_local_data(copy=False)
        else:
            # create an array that is sub-slice compatible
            self.logger.warn("The input field is not sub-slice compatible to "
                             "the distribution strategy of the operator. "
                             "Performing an probably expensive "
                             "redistribution.")
            redistr_projection_val = self._projection_field.val.copy(
                distribution_strategy=axes_local_distribution_strategy)
            local_projection_vector = \
                redistr_projection_val.get_local_data(copy=False)

        local_x = x.val.get_local_data(copy=False)

        l = len(local_projection_vector.shape)
Martin Reinecke's avatar
Martin Reinecke committed
127
        sublist_projector = list(range(l))
Theo Steininger's avatar
Theo Steininger committed
128
129
        sublist_x = np.arange(len(local_x.shape)) + l

Martin Reinecke's avatar
Martin Reinecke committed
130
        for i in range(l):
Theo Steininger's avatar
Theo Steininger committed
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
            a = active_axes[i]
            sublist_x[a] = i

        dotted = np.einsum(local_projection_vector, sublist_projector,
                           local_x, sublist_x)

        # get those elements from sublist_x that haven't got contracted
        sublist_dotted = sublist_x[sublist_x >= l]

        remultiplied = np.einsum(local_projection_vector, sublist_projector,
                                 dotted, sublist_dotted,
                                 sublist_x)
        result_field = x.copy_empty(dtype=remultiplied.dtype)
        result_field.val.set_local_data(remultiplied, copy=False)
        return result_field

    def _inverse_times(self, x, spaces):
        raise NotImplementedError("The ProjectionOperator is a singular "
                                  "operator and therefore has no inverse.")

    # ---Mandatory properties and methods---

    @property
    def domain(self):
        return self._projection_field.domain

    @property
    def unitary(self):
        if self._unitary is None:
            self._unitary = (self._projection_field.val == 1).all()
        return self._unitary

    @property
Martin Reinecke's avatar
Martin Reinecke committed
164
    def self_adjoint(self):
Theo Steininger's avatar
Theo Steininger committed
165
        return True
166
167
168
169
170
171

    # ---Added properties and methods---

    @property
    def projection_field(self):
        return self._projection_field