invertible_operator_mixin.py 1.92 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# -*- coding: utf-8 -*-

from nifty.minimization import ConjugateGradient

from nifty.field import Field


class InvertibleOperatorMixin(object):
    def __init__(self, inverter=None, preconditioner=None):
        self.__preconditioner = preconditioner
        if inverter is not None:
            self.__inverter = inverter
        else:
            self.__inverter = ConjugateGradient(
                                            preconditioner=self.preconditioner)

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

        (result, convergence) = self.inverter(A=self.inverse_times,
                                              b=x,
                                              x0=x0)
        return result

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

        (result, convergence) = self.inverter(A=self.adjoint_inverse_times,
                                              b=x,
                                              x0=x0)
        return result

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

        (result, convergence) = self.inverter(A=self.times,
                                              b=x,
                                              x0=x0)
        return result

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

        (result, convergence) = self.inverter(A=self.adjoint_times,
                                              b=x,
                                              x0=x0)
        return result

    def _inverse_adjoint_times(self, x, spaces, types):
        raise NotImplementedError(
            "no generic instance method 'inverse_adjoint_times'.")