block_diagonal_operator.py 2.87 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
import numpy as np

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


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

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

Martin Reinecke's avatar
Martin Reinecke committed
51
    def draw_sample(self, from_inverse=False, dtype=np.float64):
Martin Reinecke's avatar
Martin Reinecke committed
52 53 54 55 56 57
        from ..sugar import from_random
        val = tuple(
            op.draw_sample(from_inverse, dtype)
            if op is not None
            else from_random('normal', self._domain[key], dtype=dtype)
            for op, key in zip(self._ops, self._domain.keys()))
Martin Reinecke's avatar
Martin Reinecke committed
58
        return MultiField(self._domain, val)
Martin Reinecke's avatar
Martin Reinecke committed
59 60

    def _combine_chain(self, op):
Martin Reinecke's avatar
Martin Reinecke committed
61
        if self._domain != op._domain:
Martin Reinecke's avatar
Martin Reinecke committed
62
            raise ValueError("domain mismatch")
63 64
        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
65
        return BlockDiagonalOperator(self._domain, res)
Martin Reinecke's avatar
Martin Reinecke committed
66 67

    def _combine_sum(self, op, selfneg, opneg):
Martin Reinecke's avatar
fixes  
Martin Reinecke committed
68
        from ..operators.sum_operator import SumOperator
Martin Reinecke's avatar
Martin Reinecke committed
69
        if self._domain != op._domain:
Martin Reinecke's avatar
Martin Reinecke committed
70
            raise ValueError("domain mismatch")
71 72
        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
73
        return BlockDiagonalOperator(self._domain, res)