quadratic_energy.py 1.86 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-2017 Max-Planck-Society
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik
# and financially supported by the Studienstiftung des deutschen Volkes.

Martin Reinecke's avatar
Martin Reinecke committed
19
from .energy import Energy
Martin Reinecke's avatar
Martin Reinecke committed
20 21 22 23


class QuadraticEnergy(Energy):
    """The Energy for a quadratic form.
Martin Reinecke's avatar
Martin Reinecke committed
24 25
    The most important aspect of this energy is that its curvature must be
    position-independent.
Martin Reinecke's avatar
Martin Reinecke committed
26 27
    """

28
    def __init__(self, position, A, b, _grad=None):
Martin Reinecke's avatar
Martin Reinecke committed
29 30 31
        super(QuadraticEnergy, self).__init__(position=position)
        self._A = A
        self._b = b
32
        if _grad is not None:
Martin Reinecke's avatar
Martin Reinecke committed
33 34
            self._grad = _grad
            Ax = _grad + self._b
Martin Reinecke's avatar
tweaks  
Martin Reinecke committed
35
        else:
Martin Reinecke's avatar
Martin Reinecke committed
36 37 38
            Ax = self._A(self.position)
            self._grad = Ax - self._b
        self._value = 0.5*self.position.vdot(Ax) - b.vdot(self.position)
Martin Reinecke's avatar
Martin Reinecke committed
39 40

    def at(self, position):
41
        return QuadraticEnergy(position=position, A=self._A, b=self._b)
Martin Reinecke's avatar
Martin Reinecke committed
42

Martin Reinecke's avatar
tweaks  
Martin Reinecke committed
43
    def at_with_grad(self, position, grad):
44 45
        return QuadraticEnergy(position=position, A=self._A, b=self._b,
                               _grad=grad)
Martin Reinecke's avatar
tweaks  
Martin Reinecke committed
46

Martin Reinecke's avatar
Martin Reinecke committed
47 48
    @property
    def value(self):
Martin Reinecke's avatar
Martin Reinecke committed
49
        return self._value
Martin Reinecke's avatar
Martin Reinecke committed
50 51 52

    @property
    def gradient(self):
Martin Reinecke's avatar
Martin Reinecke committed
53
        return self._grad
Martin Reinecke's avatar
Martin Reinecke committed
54 55 56 57

    @property
    def curvature(self):
        return self._A