Martin Reinecke committed Aug 23, 2017 1 ``````from .energy import Energy `````` Martin Reinecke committed Aug 19, 2017 2 3 4 5 `````` class QuadraticEnergy(Energy): """The Energy for a quadratic form. `````` Martin Reinecke committed Aug 19, 2017 6 7 `````` The most important aspect of this energy is that its curvature must be position-independent. `````` Martin Reinecke committed Aug 19, 2017 8 9 `````` """ `````` Martin Reinecke committed Sep 29, 2017 10 `````` def __init__(self, position, A, b, _grad=None): `````` Martin Reinecke committed Aug 19, 2017 11 12 13 `````` super(QuadraticEnergy, self).__init__(position=position) self._A = A self._b = b `````` Martin Reinecke committed Sep 07, 2017 14 `````` if _grad is not None: `````` Martin Reinecke committed Dec 03, 2017 15 16 `````` self._grad = _grad Ax = _grad + self._b `````` Martin Reinecke committed Aug 20, 2017 17 `````` else: `````` Martin Reinecke committed Dec 03, 2017 18 19 20 `````` Ax = self._A(self.position) self._grad = Ax - self._b self._value = 0.5*self.position.vdot(Ax) - b.vdot(self.position) `````` Martin Reinecke committed Aug 19, 2017 21 22 `````` def at(self, position): `````` Martin Reinecke committed Sep 29, 2017 23 `````` return QuadraticEnergy(position=position, A=self._A, b=self._b) `````` Martin Reinecke committed Aug 19, 2017 24 `````` `````` Martin Reinecke committed Aug 20, 2017 25 `````` def at_with_grad(self, position, grad): `````` Martin Reinecke committed Sep 29, 2017 26 27 `````` return QuadraticEnergy(position=position, A=self._A, b=self._b, _grad=grad) `````` Martin Reinecke committed Aug 20, 2017 28 `````` `````` Martin Reinecke committed Aug 19, 2017 29 30 `````` @property def value(self): `````` Martin Reinecke committed Dec 03, 2017 31 `````` return self._value `````` Martin Reinecke committed Aug 19, 2017 32 33 34 `````` @property def gradient(self): `````` Martin Reinecke committed Dec 03, 2017 35 `````` return self._grad `````` Martin Reinecke committed Aug 19, 2017 36 37 38 39 `````` @property def curvature(self): return self._A``````