gaussian_energy.py 2.37 KB
Newer Older
Philipp Arras's avatar
Philipp Arras committed
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.

19
from __future__ import absolute_import, division, print_function
Philipp Arras's avatar
Philipp Arras committed
20

21
from ..compat import *
Martin Reinecke's avatar
Martin Reinecke committed
22
from ..operators.operator import Operator
Martin Reinecke's avatar
Martin Reinecke committed
23
from ..operators.sandwich_operator import SandwichOperator
Martin Reinecke's avatar
Martin Reinecke committed
24
from ..domain_tuple import DomainTuple
Martin Reinecke's avatar
Martin Reinecke committed
25
from ..linearization import Linearization
Philipp Arras's avatar
Philipp Arras committed
26 27


Martin Reinecke's avatar
Martin Reinecke committed
28
class GaussianEnergy(Operator):
29
    def __init__(self, mean=None, covariance=None, domain=None):
Martin Reinecke's avatar
Martin Reinecke committed
30
        super(GaussianEnergy, self).__init__()
31 32 33 34 35 36 37 38 39
        self._domain = None
        if mean is not None:
            self._checkEquivalence(mean.domain)
        if covariance is not None:
            self._checkEquivalence(covariance.domain)
        if domain is not None:
            self._checkEquivalence(domain)
        if self._domain is None:
            raise ValueError("no domain given")
40
        self._mean = mean
Martin Reinecke's avatar
Martin Reinecke committed
41
        self._icov = None if covariance is None else covariance.inverse
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

    def _checkEquivalence(self, newdom):
        if self._domain is None:
            self._domain = newdom
        else:
            if self._domain is not newdom:
                raise ValueError("domain mismatch")

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

    @property
    def target(self):
        return DomainTuple.scalar_domain()
Martin Reinecke's avatar
Martin Reinecke committed
57 58 59 60 61

    def __call__(self, x):
        residual = x if self._mean is None else x-self._mean
        icovres = residual if self._icov is None else self._icov(residual)
        res = .5*(residual*icovres).sum()
Martin Reinecke's avatar
Martin Reinecke committed
62 63
        if not isinstance(x, Linearization):
            return res
Martin Reinecke's avatar
Martin Reinecke committed
64 65
        metric = SandwichOperator.make(x.jac, self._icov)
        return res.add_metric(metric)