propagator_operator.py 3 KB
Newer Older
1
# -*- coding: utf-8 -*-
2
import numpy as np
3
from nifty.minimization import ConjugateGradient
4
from nifty.nifty_utilities import get_default_codomain
5
from nifty.field import Field
6
7
from nifty.operators import EndomorphicOperator,\
                            FFTOperator
8
9


10
class PropagatorOperator(EndomorphicOperator):
11
12
13

    # ---Overwritten properties and methods---

14
15
    def __init__(self, S=None, M=None, R=None, N=None, inverter=None,
                 preconditioner=None):
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
        """
            Sets the standard operator properties and `codomain`, `_A1`, `_A2`,
            and `RN` if required.

            Parameters
            ----------
            S : operator
                Covariance of the signal prior.
            M : operator
                Likelihood contribution.
            R : operator
                Response operator translating signal to (noiseless) data.
            N : operator
                Covariance of the noise prior or the likelihood, respectively.

        """
32
33
34
35
36
37
38
39
40
41
42
        # infer domain, and target
        if M is not None:
            self._domain = M.domain
            self._likelihood_times = M.times

        elif N is None:
            raise ValueError("Either M or N must be given!")

        elif R is not None:
            self._domain = R.domain
            self._likelihood_times = \
43
                lambda z: R.adjoint_times(N.inverse_times(R.times(z)))
44
        else:
45
46
            self._domain = N.domain
            self._likelihood_times = lambda z: N.inverse_times(z)
47

48
49
50
51
        fft_S = FFTOperator(S.domain, target=self._domain)
        self._S_times = lambda z: fft_S(S(fft_S.inverse_times(z)))
        self._S_inverse_times = lambda z: fft_S(S.inverse_times(
                                          fft_S.inverse_times(z)))
52
53
54
55
56

        if preconditioner is None:
            preconditioner = self._S_times

        self.preconditioner = preconditioner
57
58
59
60

        if inverter is not None:
            self.inverter = inverter
        else:
61
62
            self.inverter = ConjugateGradient(
                                preconditioner=self.preconditioner)
63

64
65
        self.x0 = None

66
67
68
69
    # ---Mandatory properties and methods---

    @property
    def domain(self):
70
        return self._domain
71
72
73

    @property
    def field_type(self):
74
        return ()
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89

    @property
    def implemented(self):
        return True

    @property
    def symmetric(self):
        return True

    @property
    def unitary(self):
        return False

    # ---Added properties and methods---

90
    def _times(self, x, spaces, types):
91
92
93
94
95
96
97
98
        if self.x0 is None:
            x0 = Field(self.domain, val=0., dtype=np.complex128)
        else:
            x0 = self.x0
        (result, convergence) = self.inverter(A=self.inverse_times,
                                              b=x,
                                              x0=x0)
        self.x0 = result
99
100
        return result

101
    def _inverse_times(self, x, spaces, types):
102
103
        result = self._S_inverse_times(x)
        result += self._likelihood_times(x)
104
        return result