invertible_operator_mixin.py 4.55 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
20
21
from ...minimization import ConjugateGradient
from ...field import Field
22
from ...energies import QuadraticEnergy
23
24
25


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

Theo Steininger's avatar
Theo Steininger committed
28
29
30
31
32
33
    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)
34
35
36

    Parameters
    ----------
Theo Steininger's avatar
Theo Steininger committed
37
38
    inverter : Inverter
        An instance of an Inverter class.
39
40
41
        (default: ConjugateGradient)

    preconditioner : LinearOperator
Martin Reinecke's avatar
Martin Reinecke committed
42
        Preconditioner that is used by ConjugateGradient if no minimizer was
43
        given.
44
45
46
47
48
49
50
51
52
53
54
55

    Attributes
    ----------

    Raises
    ------

    Notes
    -----

    Examples
    --------
Theo Steininger's avatar
Theo Steininger committed
56
    The PropagatorOperator inherits from InvertibleOperatorMixin.
57
58
59

    See Also
    --------
Theo Steininger's avatar
Theo Steininger committed
60
    PropagatorOperator
61
62
63

    """

64
65
    def __init__(self, inverter=None, preconditioner=None,
                 forward_x0=None, backward_x0=None, *args, **kwargs):
66
67
68
69
70
        self.__preconditioner = preconditioner
        if inverter is not None:
            self.__inverter = inverter
        else:
            self.__inverter = ConjugateGradient(
71
                                        preconditioner=self.__preconditioner)
72
73
74

        self.__forward_x0 = forward_x0
        self.__backward_x0 = backward_x0
75
        super(InvertibleOperatorMixin, self).__init__(*args, **kwargs)
76

77
78
79
80
    def _times(self, x, spaces):
        if self.__forward_x0 is not None:
            x0 = self.__forward_x0
        else:
81
82
            x0 = Field(self.target, val=0., dtype=x.dtype,
                       distribution_strategy=x.distribution_strategy)
83

Martin Reinecke's avatar
Martin Reinecke committed
84
85
        (result, convergence) = self.__inverter(QuadraticEnergy(
                                                A=self.inverse_times,
86
                                                b=x,
Martin Reinecke's avatar
Martin Reinecke committed
87
88
                                                position=x0))
        return result.position
89

90
91
92
93
    def _adjoint_times(self, x, spaces):
        if self.__backward_x0 is not None:
            x0 = self.__backward_x0
        else:
94
95
            x0 = Field(self.domain, val=0., dtype=x.dtype,
                       distribution_strategy=x.distribution_strategy)
96

Martin Reinecke's avatar
Martin Reinecke committed
97
98
        (result, convergence) = self.__inverter(QuadraticEnergy(
                                                A=self.adjoint_inverse_times,
99
                                                b=x,
Martin Reinecke's avatar
Martin Reinecke committed
100
101
                                                position=x0))
        return result.position
102

103
104
105
106
    def _inverse_times(self, x, spaces):
        if self.__backward_x0 is not None:
            x0 = self.__backward_x0
        else:
107
108
            x0 = Field(self.domain, val=0., dtype=x.dtype,
                       distribution_strategy=x.distribution_strategy)
109

Martin Reinecke's avatar
Martin Reinecke committed
110
111
        (result, convergence) = self.__inverter(QuadraticEnergy(
                                                A=self.times,
112
                                                b=x,
Martin Reinecke's avatar
Martin Reinecke committed
113
114
                                                position=x0))
        return result.position
115

116
117
118
119
    def _adjoint_inverse_times(self, x, spaces):
        if self.__forward_x0 is not None:
            x0 = self.__forward_x0
        else:
120
121
            x0 = Field(self.target, val=0., dtype=x.dtype,
                       distribution_strategy=x.distribution_strategy)
122

Martin Reinecke's avatar
Martin Reinecke committed
123
124
        (result, convergence) = self.__inverter(QuadraticEnergy(
                                                A=self.adjoint_times,
125
                                                b=x,
Martin Reinecke's avatar
Martin Reinecke committed
126
127
                                                position=x0))
        return result.position