quadratic_energy.py 771 Bytes
Newer Older
Martin Reinecke's avatar
Martin Reinecke committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from nifty.energies.energy import Energy
from nifty.energies.memoization import memo


class QuadraticEnergy(Energy):
    """The Energy for a quadratic form.
    """

    def __init__(self, position, A, b):
        super(QuadraticEnergy, self).__init__(position=position)
        self._A = A
        self._b = b

    def at(self, position):
        return self.__class__(position=position, A=self._A, b=self._b)

    @property
    @memo
    def value(self):
        return 0.5*self.position.vdot(self._Ax) - self._b.vdot(self.position)

    @property
    @memo
    def gradient(self):
        return self._Ax - self._b

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

    @property
    @memo
    def _Ax(self):
        return self.curvature(self.position)