propagator_operator.py 3.67 KB
Newer Older
1
# -*- coding: utf-8 -*-
2

3
from nifty.minimization import ConjugateGradient
4
from nifty.field import Field
5
from nifty.operators import EndomorphicOperator,\
6
7
                            FFTOperator,\
                            InvertibleOperatorMixin
8
9


10
class PropagatorOperator(InvertibleOperatorMixin, 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
        self._S = S
        self._fft_S = FFTOperator(self._domain, target=self._S.domain)
50
51
52
53

        if preconditioner is None:
            preconditioner = self._S_times

54
55
        super(PropagatorOperator, self).__init__(inverter=inverter,
                                                 preconditioner=preconditioner)
56
57
58
59
60

    # ---Mandatory properties and methods---

    @property
    def domain(self):
61
        return self._domain
62
63
64

    @property
    def field_type(self):
65
        return ()
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80

    @property
    def implemented(self):
        return True

    @property
    def symmetric(self):
        return True

    @property
    def unitary(self):
        return False

    # ---Added properties and methods---

81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
    def _S_times(self, x, spaces=None, types=None):
            transformed_x = self._fft_S(x,
                                        spaces=spaces,
                                        types=types)
            y = self._S(transformed_x, spaces=spaces, types=types)
            transformed_y = self._fft_S.inverse_times(y,
                                                      spaces=spaces,
                                                      types=types)
            result = x.copy_empty()
            result.set_val(transformed_y, copy=False)
            return result

    def _S_inverse_times(self, x, spaces=None, types=None):
            transformed_x = self._fft_S(x,
                                        spaces=spaces,
                                        types=types)
            y = self._S.inverse_times(transformed_x,
                                      spaces=spaces,
                                      types=types)
            transformed_y = self._fft_S.inverse_times(y,
                                                      spaces=spaces,
                                                      types=types)
            result = x.copy_empty()
            result.set_val(transformed_y, copy=False)
            return result

107
    def _inverse_times(self, x, spaces, types):
108
109
110
111
        pre_result = self._S_inverse_times(x, spaces, types)
        pre_result += self._likelihood_times(x)
        result = x.copy_empty()
        result.set_val(pre_result, copy=False)
112
        return result