noise_energy.py 2.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 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-2018 Max-Planck-Society
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik
# and financially supported by the Studienstiftung des deutschen Volkes.

Philipp Arras's avatar
Philipp Arras committed
19
20
from .. import Field, exp
from ..minimization.energy import Energy
Philipp Arras's avatar
Philipp Arras committed
21
from ..operators.diagonal_operator import DiagonalOperator
22

23
24

class NoiseEnergy(Energy):
Philipp Arras's avatar
Philipp Arras committed
25
    def __init__(self, position, alpha, q, res_sample_list):
Martin Reinecke's avatar
Martin Reinecke committed
26
        super(NoiseEnergy, self).__init__(position=position)
27

Philipp Arras's avatar
Philipp Arras committed
28
        self.N = DiagonalOperator(diagonal=exp(self.position))
29
30
        self.alpha = alpha
        self.q = q
Philipp Arras's avatar
Philipp Arras committed
31
32
33
        alpha_field = Field(self.position.domain, val=alpha)
        q_field = Field(self.position.domain, val=q)
        self.res_sample_list = res_sample_list
Philipp Arras's avatar
Philipp Arras committed
34
35
36

        self._gradient = None

Philipp Arras's avatar
Philipp Arras committed
37
38
39
        for s in self.res_sample_list:
            lh = .5 * s.vdot(self.N.inverse_times(s))
            grad = -.5 * self.N.inverse_times(s.conjugate()*s)
Philipp Arras's avatar
Philipp Arras committed
40
41
42
43
44
45
46
            if self._gradient is None:
                self._value = lh
                self._gradient = grad.copy()
            else:
                self._value += lh
                self._gradient += grad

Philipp Arras's avatar
Philipp Arras committed
47
        self._value /= len(self.res_sample_list)
48
        self._value += .5 * self.position.sum()
Philipp Arras's avatar
Philipp Arras committed
49
50
        self._value += (alpha_field - 1.).vdot(self.position) + \
            q_field.vdot(exp(-self.position))
Philipp Arras's avatar
Philipp Arras committed
51

Philipp Arras's avatar
Philipp Arras committed
52
53
        self._gradient /= len(self.res_sample_list)
        self._gradient += (alpha_field-0.5) - q_field*(exp(-self.position))
54
55

    def at(self, position):
Philipp Arras's avatar
Philipp Arras committed
56
        return self.__class__(position, self.alpha, self.q, self.res_sample_list)
57
58

    @property
Martin Reinecke's avatar
Martin Reinecke committed
59
60
    def value(self):
        return self._value
61
62

    @property
Martin Reinecke's avatar
Martin Reinecke committed
63
64
    def gradient(self):
        return self._gradient