invertible_operator_mixin.py 3.5 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
19
20
21
22
23
24

from nifty.minimization import ConjugateGradient

from nifty.field import Field


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
42
43
44
45
46
47
48
49
50
51
52
53
        (default: ConjugateGradient)

    preconditioner : LinearOperator
        Preconditions the minimizaion problem

    Attributes
    ----------

    Raises
    ------

    Notes
    -----

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

    See Also
    --------
Theo Steininger's avatar
Theo Steininger committed
58
    PropagatorOperator
59
60
61

    """

62
    def __init__(self, inverter=None, preconditioner=None, *args, **kwargs):
63
64
65
66
67
        self.__preconditioner = preconditioner
        if inverter is not None:
            self.__inverter = inverter
        else:
            self.__inverter = ConjugateGradient(
68
                                        preconditioner=self.__preconditioner)
69
        super(InvertibleOperatorMixin, self).__init__(*args, **kwargs)
70

71
    def _times(self, x, spaces, x0=None):
72
73
74
        if x0 is None:
            x0 = Field(self.target, val=0., dtype=x.dtype)

75
        (result, convergence) = self.__inverter(A=self.inverses_times,
76
77
                                                b=x,
                                                x0=x0)
78
79
        return result

80
    def _adjoint_times(self, x, spaces, x0=None):
81
82
83
        if x0 is None:
            x0 = Field(self.domain, val=0., dtype=x.dtype)

84
85
86
        (result, convergence) = self.__inverter(A=self.adjoint_inverse_times,
                                                b=x,
                                                x0=x0)
87
88
        return result

89
    def _inverse_times(self, x, spaces, x0=None):
90
91
92
        if x0 is None:
            x0 = Field(self.domain, val=0., dtype=x.dtype)

93
94
95
        (result, convergence) = self.__inverter(A=self.times,
                                                b=x,
                                                x0=x0)
96
97
        return result

98
    def _adjoint_inverse_times(self, x, spaces, x0=None):
99
100
101
        if x0 is None:
            x0 = Field(self.target, val=0., dtype=x.dtype)

102
103
104
        (result, convergence) = self.__inverter(A=self.adjoint_times,
                                                b=x,
                                                x0=x0)
105
        return result