quadratic_energy.py 2.41 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
# 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/>.
#
14
# Copyright(C) 2013-2019 Max-Planck-Society
15
#
16
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik.
17

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


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

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

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

Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
44
    def at_with_grad(self, position, grad):
Martin Reinecke's avatar
Martin Reinecke committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
        """ Specialized version of `at`, taking also a gradient.

        This custom method is meant for use within :class:ConjugateGradient`
        minimizers, which already have the gradient available. It saves time
        by not recomputing it.

        Parameters
        ----------
        position : Field
            Location in parameter space for the new Energy object.
        grad : Field
            Energy gradient at the new position.

        Returns
        -------
        Energy
            Energy object at new position.
        """
Martin Reinecke's avatar
Martin Reinecke committed
63
        return QuadraticEnergy(position, self._A, self._b, grad)
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
64

Martin Reinecke's avatar
Martin Reinecke committed
65
66
    @property
    def value(self):
Martin Reinecke's avatar
Martin Reinecke committed
67
        return self._value
Martin Reinecke's avatar
Martin Reinecke committed
68
69
70

    @property
    def gradient(self):
Martin Reinecke's avatar
Martin Reinecke committed
71
        return self._grad
Martin Reinecke's avatar
Martin Reinecke committed
72

Martin Reinecke's avatar
Martin Reinecke committed
73
74
75
76
    @property
    def metric(self):
        return self._A

Martin Reinecke's avatar
Martin Reinecke committed
77
78
    def apply_metric(self, x):
        return self._A(x)