laplace_operator.py 6.26 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 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/>.
#
# Copyright(C) 2013-2017 Max-Planck-Society
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik
# and financially supported by the Studienstiftung des deutschen Volkes.
18
19

import numpy as np
20
21
22
23
from nifty.field import Field
from nifty.spaces.power_space import PowerSpace
from nifty.operators.endomorphic_operator import EndomorphicOperator

24
import nifty.nifty_utilities as utilities
Jakob Knollmueller's avatar
Jakob Knollmueller committed
25
26


27
class LaplaceOperator(EndomorphicOperator):
Jakob Knollmueller's avatar
Jakob Knollmueller committed
28
29
30
31
32
33
34
35
36
37
38
39
40
41
    """A irregular LaplaceOperator with free boundary and excluding monopole.

    This LaplaceOperator implements the second derivative of a Field in PowerSpace
    on logarithmic or linear scale with vanishing curvature at the boundary, starting
    at the second entry of the Field. The second derivative of the Field on the irregular grid
    is calculated using finite differences.

    Parameters
    ----------
    logarithmic : boolean,
        Whether smoothness is calculated on a logarithmic scale or linear scale
        default : True
    """

42
    def __init__(self, domain, default_spaces=None, logarithmic=True):
43
        super(LaplaceOperator, self).__init__(default_spaces)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
44
        self._domain = self._parse_domain(domain)
45
46
47
48
49
50
51
        if len(self.domain) != 0:
            raise ValueError("The domain must contain exactly one PowerSpace.")

        if not isinstance(self.domain[0], PowerSpace):
            raise TypeError("The domain must contain exactly one PowerSpace.")

        if logarithmic:
52
53
54
            self.positions = self.domain[0].kindex.copy()
            self.positions[1:] = np.log(self.positions[1:])
            self.positions[0] = -1.
55
        else:
56
57
58
59
            self.positions = self.domain[0].kindex.copy()
            self.positions[0] = -1

        self.fwd_dist = self.positions[1:] - self.positions[:-1]
Jakob Knollmueller's avatar
Jakob Knollmueller committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80

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

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

    @property
    def unitary(self):
        return False

    @property
    def symmetric(self):
        return False

    @property
    def self_adjoint(self):
        return False

81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
    def _times(self, x, spaces):
        spaces = utilities.cast_axis_to_tuple(spaces, len(x.domain))
        if spaces is None:
            # this case means that x lives on only one space, which is
            # identical to the space in the domain of `self`. Otherwise the
            # input check of LinearOperator would have failed.
            axes = x.domain_axes[0]
        else:
            axes = x.domain_axes[spaces[0]]
        axis = axes[0]
        prefix = (slice(None),) * axis
        fwd_dist = self.fwd_dist.reshape((1,)*axis + self.fwd_dist.shape)
        positions = self.positions.reshape((1,)*axis + self.positions.shape)
        ret = x.val.copy_empty()
        x = x.val
96
97
98
99
100
101
102
103
        ret[prefix + (slice(1, -1),)] = \
            (-((x[prefix + (slice(1, -1),)] - x[prefix + (slice(0, -2),)]) /
               fwd_dist[prefix + (slice(0, -1),)]) +
             ((x[prefix + (slice(2, None),)] - x[prefix + (slice(1, -1),)]) /
              fwd_dist[prefix + (slice(1, None),)]))
        ret[prefix + (slice(1, -1),)] /= \
            (positions[prefix + (slice(2, None),)] -
             positions[prefix + (slice(None, -2),)])
104
        ret *= 2.
105
106
107
108
109
        ret[prefix + (slice(0, 2),)] = 0
        ret[prefix + (slice(-1, -1),)] = 0
        ret[prefix + (slice(2, None),)] *= \
            np.sqrt(fwd_dist)[prefix + (slice(1, None),)]
        return Field(self.domain, val=ret).weight(power=-0.5, spaces=spaces)
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124

    def _adjoint_times(self, x, spaces):
        spaces = utilities.cast_axis_to_tuple(spaces, len(x.domain))
        if spaces is None:
            # this case means that x lives on only one space, which is
            # identical to the space in the domain of `self`. Otherwise the
            # input check of LinearOperator would have failed.
            axes = x.domain_axes[0]
        else:
            axes = x.domain_axes[spaces[0]]
        axis = axes[0]
        prefix = (slice(None),) * axis
        fwd_dist = self.fwd_dist.reshape((1,)*axis + self.fwd_dist.shape)
        positions = self.positions.reshape((1,)*axis + self.positions.shape)
        y = x.copy().weight(power=0.5).val
125
126
127
128
        y[prefix + (slice(2, None),)] *= \
            np.sqrt(fwd_dist)[prefix + (slice(1, None),)]
        y[prefix + (slice(0, 2),)] = 0
        y[prefix + (slice(-1, -1),)] = 0
129
        ret = y.copy_empty()
130
131
132
        y[prefix + (slice(1, -1),)] /= \
            (positions[prefix + (slice(2, None),)] -
             positions[prefix + (slice(None, -2),)])
133
        y *= 2
134
135
136
137
138
139
140
141
        ret[prefix + (slice(1, -1),)] = \
            (-y[prefix + (slice(1, -1),)]/fwd_dist[prefix + (slice(0, -1),)] -
             y[prefix + (slice(1, -1),)]/fwd_dist[prefix + (slice(1, None),)])
        ret[prefix + (slice(0, -2),)] += \
            y[prefix + (slice(1, -1),)] / fwd_dist[prefix + (slice(0, -1),)]
        ret[prefix + (slice(2, None),)] += \
            y[prefix + (slice(1, -1),)] / fwd_dist[prefix + (slice(1, None),)]
        return Field(self.domain, val=ret).weight(-1, spaces=spaces)
142
143
144

    def _irregular_laplace(self, x):
        ret = np.zeros_like(x)
145
146
        ret[1:-1] = (-(x[1:-1] - x[0:-2]) / self.fwd_dist[:-1] +
                      (x[2:] - x[1:-1]) / self.fwd_dist[1:])
147
148
149
150
151
152
153
154
155
156
157
158
159
        ret[1:-1] /= self.positions[2:] - self.positions[:-2]
        ret *= 2.
        return ret

    def _irregular_adj_laplace(self, x):
        ret = np.zeros_like(x)
        y = x.copy()
        y[1:-1] /= self.positions[2:] - self.positions[:-2]
        y *= 2
        ret[1:-1] = -y[1:-1] / self.fwd_dist[:-1] - y[1:-1] / self.fwd_dist[1:]
        ret[0:-2] += y[1:-1] / self.fwd_dist[:-1]
        ret[2:] += y[1:-1] / self.fwd_dist[1:]
        return ret