block_diagonal_operator.py 2.69 KB
Newer Older
Martin Reinecke's avatar
Martin Reinecke committed
1
2
3
4
5
6
7
8
9
10
11
12
13
# 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/>.
#
14
# Copyright(C) 2013-2019 Max-Planck-Society
Martin Reinecke's avatar
Martin Reinecke committed
15
#
16
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik.
17

Martin Reinecke's avatar
Martin Reinecke committed
18
19
from ..multi_domain import MultiDomain
from ..multi_field import MultiField
Philipp Arras's avatar
Philipp Arras committed
20
from .endomorphic_operator import EndomorphicOperator
Martin Reinecke's avatar
Martin Reinecke committed
21
22
23


class BlockDiagonalOperator(EndomorphicOperator):
24
25
26
    """
    Parameters
    ----------
27
    domain : MultiDomain
28
        Domain and target of the operator.
29
    operators : dict
Martin Reinecke's avatar
Martin Reinecke committed
30
        Dictionary with subdomain names as keys and :class:`LinearOperator` s
31
        as items.
32
    """
Martin Reinecke's avatar
Martin Reinecke committed
33
    def __init__(self, domain, operators):
Martin Reinecke's avatar
Martin Reinecke committed
34
35
        if not isinstance(domain, MultiDomain):
            raise TypeError("MultiDomain expected")
Martin Reinecke's avatar
Martin Reinecke committed
36
        self._domain = domain
37
        self._ops = tuple(operators[key] for key in domain.keys())
Martin Reinecke's avatar
Martin Reinecke committed
38
        self._capability = self._all_ops
Martin Reinecke's avatar
Martin Reinecke committed
39
40
        for op in self._ops:
            if op is not None:
Martin Reinecke's avatar
Martin Reinecke committed
41
                self._capability &= op.capability
Martin Reinecke's avatar
Martin Reinecke committed
42
43
44

    def apply(self, x, mode):
        self._check_input(x, mode)
Martin Reinecke's avatar
Martin Reinecke committed
45
46
        val = tuple(op.apply(v, mode=mode) if op is not None else None
                    for op, v in zip(self._ops, x.values()))
Martin Reinecke's avatar
Martin Reinecke committed
47
        return MultiField(self._domain, val)
Martin Reinecke's avatar
Martin Reinecke committed
48

Martin Reinecke's avatar
Martin Reinecke committed
49
50
51
52
53
#    def draw_sample(self, from_inverse=False, dtype=np.float64):
#        dtype = MultiField.build_dtype(dtype, self._domain)
#        val = tuple(op.draw_sample(from_inverse, dtype)
#                    for op in self._op)
#        return MultiField(self._domain, val)
Martin Reinecke's avatar
Martin Reinecke committed
54
55

    def _combine_chain(self, op):
Martin Reinecke's avatar
Martin Reinecke committed
56
        if self._domain != op._domain:
Martin Reinecke's avatar
Martin Reinecke committed
57
            raise ValueError("domain mismatch")
58
59
        res = {key: v1(v2)
               for key, v1, v2 in zip(self._domain.keys(), self._ops, op._ops)}
Martin Reinecke's avatar
fixes  
Martin Reinecke committed
60
        return BlockDiagonalOperator(self._domain, res)
Martin Reinecke's avatar
Martin Reinecke committed
61
62

    def _combine_sum(self, op, selfneg, opneg):
Martin Reinecke's avatar
fixes  
Martin Reinecke committed
63
        from ..operators.sum_operator import SumOperator
Martin Reinecke's avatar
Martin Reinecke committed
64
        if self._domain != op._domain:
Martin Reinecke's avatar
Martin Reinecke committed
65
            raise ValueError("domain mismatch")
66
67
        res = {key: SumOperator.make([v1, v2], [selfneg, opneg])
               for key, v1, v2 in zip(self._domain.keys(), self._ops, op._ops)}
Martin Reinecke's avatar
fixes  
Martin Reinecke committed
68
        return BlockDiagonalOperator(self._domain, res)