There is a maintenance of MPCDF Gitlab on Thursday, April 22st 2020, 9:00 am CEST - Expect some service interruptions during this time

block_diagonal_operator.py 2.86 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

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

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

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

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

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

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