symmetrizing_operator.py 1.1 KB
Newer Older
1
2
3
from ..domain_tuple import DomainTuple
from ..field import Field
from .endomorphic_operator import EndomorphicOperator
Martin Reinecke's avatar
Martin Reinecke committed
4
from .. import dobj
5
6
7
8
9
10
11
12
13
14
15
16
17


class SymmetrizingOperator(EndomorphicOperator):
    def __init__(self, domain):
        self._domain = DomainTuple.make(domain)
        self._ndim = len(self.domain.shape)

    @property
    def domain(self):
        return self._domain

    def apply(self, x, mode):
        self._check_input(x, mode)
Martin Reinecke's avatar
Martin Reinecke committed
18
19
20
        tmp = x.copy().val
        ax = dobj.distaxis(tmp)
        globshape = tmp.shape
21
22
        for i in range(self._ndim):
            lead = (slice(None),)*i
Martin Reinecke's avatar
Martin Reinecke committed
23
24
25
26
            if i == ax:
                tmp = dobj.redistribute(tmp, nodist=(ax,))
            curax = dobj.distaxis(tmp)
            tmp = dobj.local_data(tmp)
27
            tmp[lead + (slice(1, None),)] -= tmp[lead + (slice(None, 0, -1),)]
Martin Reinecke's avatar
Martin Reinecke committed
28
29
30
31
            tmp = dobj.from_local_data(globshape, tmp, distaxis=curax)
            if i == ax:
                tmp = dobj.redistribute(tmp, dist=ax)
            return Field(self.target, val=tmp)
32
33
34
35

    @property
    def capability(self):
        return self.TIMES | self.ADJOINT_TIMES