field_zero_padder.py 2.22 KB
Newer Older
Martin Reinecke's avatar
Martin Reinecke committed
1
2
3
4
5
6
7
from __future__ import absolute_import, division, print_function

import numpy as np

from .. import dobj
from ..compat import *
from ..domain_tuple import DomainTuple
8
9
from ..domains.rg_space import RGSpace
from ..field import Field
Martin Reinecke's avatar
Martin Reinecke committed
10
from .linear_operator import LinearOperator
11
from .. import utilities
Martin Reinecke's avatar
Martin Reinecke committed
12
13
14


class FieldZeroPadder(LinearOperator):
15
    def __init__(self, domain, factor, space=0):
Martin Reinecke's avatar
Martin Reinecke committed
16
        super(FieldZeroPadder, self).__init__()
17
        self._domain = DomainTuple.make(domain)
18
        self._space = utilities.infer_space(self._domain, space)
19
20
        dom = self._domain[self._space]
        if not isinstance(dom, RGSpace):
Martin Reinecke's avatar
Martin Reinecke committed
21
            raise TypeError("RGSpace required")
22
        if not len(dom.shape) == 1:
Martin Reinecke's avatar
Martin Reinecke committed
23
            raise TypeError("RGSpace must be one-dimensional")
24
        if dom.harmonic:
Martin Reinecke's avatar
Martin Reinecke committed
25
26
            raise TypeError("RGSpace must not be harmonic")

27
28
29
30
        tgt = RGSpace((int(factor*dom.shape[0]),), dom.distances)
        self._target = list(self._domain)
        self._target[self._space] = tgt
        self._target = DomainTuple.make(self._target)
Martin Reinecke's avatar
Martin Reinecke committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

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

    @property
    def target(self):
        return self._target

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

    def apply(self, x, mode):
        self._check_input(x, mode)
        x = x.val
        dax = dobj.distaxis(x)
        shp_in = x.shape
        shp_out = self._tgt(mode).shape
50
        ax = self._target.axes[self._space][0]
Martin Reinecke's avatar
Martin Reinecke committed
51
52
53
54
        if dax == ax:
            x = dobj.redistribute(x, nodist=(ax,))
        curax = dobj.distaxis(x)

55
        if mode == self.ADJOINT_TIMES:
56
            newarr = np.empty(dobj.local_shape(shp_out, curax), dtype=x.dtype)
Martin Reinecke's avatar
Martin Reinecke committed
57
58
59
            newarr[()] = dobj.local_data(x)[(slice(None),)*ax +
                                            (slice(0, shp_out[ax]),)]
        else:
60
            newarr = np.zeros(dobj.local_shape(shp_out, curax), dtype=x.dtype)
Martin Reinecke's avatar
Martin Reinecke committed
61
62
63
64
65
66
            newarr[(slice(None),)*ax +
                   (slice(0, shp_in[ax]),)] = dobj.local_data(x)
        newarr = dobj.from_local_data(shp_out, newarr, distaxis=curax)
        if dax == ax:
            newarr = dobj.redistribute(newarr, dist=ax)
        return Field(self._tgt(mode), val=newarr)