operand.py 3.6 KB
Newer Older
Martin Reinecke's avatar
Martin Reinecke committed
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
107
108
109
110
111
112
113
114
115
116
117
118
# 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/>.
#
# Copyright(C) 2020 Max-Planck-Society
# Author: Martin Reinecke
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik.

from .utilities import NiftyMeta
from . import pointwise


class Operand(metaclass=NiftyMeta):
    """Transforms values defined on one domain into values defined on another
    domain, and can also provide the Jacobian.
    """

    @property
    def target(self):
        """The domain on which the Operand's value is defined.

        Returns
        -------
        target : DomainTuple or MultiDomain
        """
        return self._target

    @property
    def fld(self):
        """Returns an object identical to `self`, except that all information
        about the Jacobian and metric is stripped away.
        Linearizations return the contained Field or MultiField object,
        Fields and MultiFields just return themselves.

        Returns
        -------
        Field or MultiField : the field object
        """
        raise NotImplementedError

    @property
    def val(self):
        """The numerical value associated with this object
        Depending on the `target` this is either  a `numpy.ndarray` or a
        dictionary of `numpy.ndarray`s matching the object's `target`.

        Returns
        -------
        numpy.ndarray or dictionary of np.ndarrays : the numerical value
        """
        raise NotImplementedError

    def val_rw(self):
        """Like `val`, but returns a writeable copy of the data.

        Returns
        -------
        numpy.ndarray or dictionary of np.ndarrays :
            a writeable copy of the numerical value
        """
        return None

    @property
    def jac(self):
        """The Jacobian associated with this object
        This can be `None` (in which case the object is a constant), or it can
        be a `LinearOperator` with a `target` matching the object's.

        Returns
        -------
        None or LinearOperator : the Jacobian
        """
        return None

    @property
    def want_metric(self):
        """Whether a metric should be computed for the full expression.
        This is `False` whenever `jac` is `None`. In other cases it signals
        that operators processing this object should compute the metric.

        Returns
        -------
        bool : whether the metric should be computed
        """
        return False

    @property
    def metric(self):
        """The metric associated with the object.
        This is `None`, except when all the following conditions hold:
        - `want_metric` is `True`
        - `target` is the scalar domain
        - the operator chain generating this object contained an operator which
          could compute the metric

        Returns
        -------
        None or LinearOperator : the metric
        """
        raise NotImplementedError


for f in pointwise.ptw_dict.keys():
    def func(f):
        def func2(self, *args, **kwargs):
            return self.ptw(f, *args, **kwargs)
        return func2
    setattr(Operand, f, func(f))