operator.py 1007 Bytes
Newer Older
Martin Reinecke's avatar
Martin Reinecke committed
1
2
3
4
5
from __future__ import absolute_import, division, print_function

from .compat import *
from .utilities import NiftyMetaBase

Martin Reinecke's avatar
more    
Martin Reinecke committed
6

Martin Reinecke's avatar
Martin Reinecke committed
7
8
9
10
11
class Operator(NiftyMetaBase()):
    """Transforms values living on one domain into values living on another
    domain, and can also provide the Jacobian.
    """

Martin Reinecke's avatar
Martin Reinecke committed
12
13
14
15
16
17
18
    def chain(self, x):
        if not callable(x):
            raise TypeError("callable needed")
        ops1 = self._ops if isinstance(self, OpChain) else (self,)
        ops2 = x._ops if isinstance(x, OpChain) else (x,)
        return OpChain(ops1+ops2)

Martin Reinecke's avatar
Martin Reinecke committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
    def __call__(self, x):
        """Returns transformed x

        Parameters
        ----------
        x : Linearization
            input

        Returns
        -------
        Linearization
            output
        """
        raise NotImplementedError
Martin Reinecke's avatar
Martin Reinecke committed
33
34
35
36
37
38
39
40
41
42


class OpChain(Operator):
    def __init__(self, ops):
        self._ops = tuple(ops)

    def __call__(self, x):
        for op in reversed(self._ops):
            x = op(x)
        return x