model.py 2.53 KB
Newer Older
Philipp Arras's avatar
Philipp Arras committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 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) 2013-2018 Max-Planck-Society
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik
# and financially supported by the Studienstiftung des deutschen Volkes.

Philipp Arras's avatar
Philipp Arras committed
19
from ..operators.selection_operator import SelectionOperator
20
from ..operators.diagonal_operator import DiagonalOperator
Philipp Arras's avatar
Philipp Arras committed
21
from ..utilities import NiftyMetaBase
22
from ..field import Field
23
24


Philipp Arras's avatar
Philipp Arras committed
25
class Model(NiftyMetaBase()):
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
    def __init__(self, position):
        self._position = position

    def at(self, position):
        raise NotImplementedError

    @property
    def position(self):
        return self._position

    @property
    def value(self):
        return self._value

    @property
    def gradient(self):
        return self._gradient

    def __getitem__(self, key):
        sel = SelectionOperator(self.value.domain, key)
Philipp Arras's avatar
Fixups    
Philipp Arras committed
46
        return sel(self)
47
48

    def __add__(self, other):
Philipp Arras's avatar
Philipp Arras committed
49
50
        if not isinstance(other, Model):
            raise TypeError
Philipp Arras's avatar
Philipp Arras committed
51
        from .binary_helpers import Add
52
53
54
        return Add.make(self, other)

    def __sub__(self, other):
Philipp Arras's avatar
Philipp Arras committed
55
56
        if not isinstance(other, Model):
            raise TypeError
Philipp Arras's avatar
Philipp Arras committed
57
        from .binary_helpers import Add
58
59
60
61
        return Add.make(self, (-1) * other)

    def __mul__(self, other):
        if isinstance(other, (float, int)):
Philipp Arras's avatar
Philipp Arras committed
62
            from .binary_helpers import ScalarMul
Philipp Arras's avatar
Philipp Arras committed
63
            return ScalarMul(other, self)
Philipp Arras's avatar
Philipp Arras committed
64
        if isinstance(other, Model):
Philipp Arras's avatar
Philipp Arras committed
65
            from .binary_helpers import Mul
66
            return Mul.make(self, other)
67
68
69
70
        if isinstance(other, Field):
            if other.domain == self.value.domain:
                from .binary_helpers import Mul
                return DiagonalOperator(other)(self)
71
72
73
74
75
        raise NotImplementedError

    def __rmul__(self, other):
        if isinstance(other, (float, int)):
            return self.__mul__(other)
76
77
        if isinstance(other, Field):
            return self.__mul__(other)
78
        raise NotImplementedError