invertible_operator_mixin.py 3.73 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 object
Martin Reinecke's avatar
Martin Reinecke committed
20
21
from ..energies import QuadraticEnergy
from ..field import Field
22
23
24


class InvertibleOperatorMixin(object):
Theo Steininger's avatar
Theo Steininger committed
25
    """ Mixin class to invert implicit defined operators.
26

Theo Steininger's avatar
Theo Steininger committed
27
28
29
30
31
32
    To invert the application of a given implicitly defined operator on a
    field, this class gives the necessary functionality. Inheriting
    functionality from this class provides the derived class with the inverse
    to the given implicitly definied application of the operator on a field.
    (e.g. .inverse_times vs. .times and
    .adjoint_times vs. .adjoint_inverse_times)
33
34
35

    Parameters
    ----------
Theo Steininger's avatar
Theo Steininger committed
36
37
    inverter : Inverter
        An instance of an Inverter class.
38
39
40

    """

41
    def __init__(self, inverter, preconditioner=None,
42
                 forward_x0=None, backward_x0=None, *args, **kwargs):
Martin Reinecke's avatar
Martin Reinecke committed
43
        self.__inverter = inverter
44
        self._preconditioner = preconditioner
45
46
        self.__forward_x0 = forward_x0
        self.__backward_x0 = backward_x0
47
        super(InvertibleOperatorMixin, self).__init__(*args, **kwargs)
48

49
    def _times(self, x):
50
51
52
        if self.__forward_x0 is not None:
            x0 = self.__forward_x0
        else:
53
            x0 = Field.zeros(self.target, dtype=x.dtype)
54

Martin Reinecke's avatar
Martin Reinecke committed
55
        (result, convergence) = self.__inverter(QuadraticEnergy(
56
57
58
                                           A=self.inverse_times,
                                           b=x, position=x0),
                                           preconditioner=self._preconditioner)
Martin Reinecke's avatar
Martin Reinecke committed
59
        return result.position
60

61
    def _adjoint_times(self, x):
62
63
64
        if self.__backward_x0 is not None:
            x0 = self.__backward_x0
        else:
65
            x0 = Field.zeros(self.domain, dtype=x.dtype)
66

Martin Reinecke's avatar
Martin Reinecke committed
67
        (result, convergence) = self.__inverter(QuadraticEnergy(
68
69
70
                                           A=self.adjoint_inverse_times,
                                           b=x, position=x0),
                                           preconditioner=self._preconditioner)
Martin Reinecke's avatar
Martin Reinecke committed
71
        return result.position
72

73
    def _inverse_times(self, x):
74
75
76
        if self.__backward_x0 is not None:
            x0 = self.__backward_x0
        else:
77
            x0 = Field.zeros(self.domain, dtype=x.dtype)
78

Martin Reinecke's avatar
Martin Reinecke committed
79
        (result, convergence) = self.__inverter(QuadraticEnergy(
80
81
82
                                           A=self.times,
                                           b=x, position=x0),
                                           preconditioner=self._preconditioner)
Martin Reinecke's avatar
Martin Reinecke committed
83
        return result.position
84

85
    def _adjoint_inverse_times(self, x):
86
87
88
        if self.__forward_x0 is not None:
            x0 = self.__forward_x0
        else:
89
            x0 = Field.zeros(self.target, dtype=x.dtype)
90

Martin Reinecke's avatar
Martin Reinecke committed
91
        (result, convergence) = self.__inverter(QuadraticEnergy(
92
93
94
                                           A=self.adjoint_times,
                                           b=x, position=x0),
                                           preconditioner=self._preconditioner)
Martin Reinecke's avatar
Martin Reinecke committed
95
        return result.position