Martin Reinecke committed Jan 17, 2018 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 . # `````` Martin Reinecke committed Feb 17, 2018 14 ``````# Copyright(C) 2013-2018 Max-Planck-Society `````` Martin Reinecke committed Jan 17, 2018 15 16 17 18 ``````# # NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik # and financially supported by the Studienstiftung des deutschen Volkes. `````` Martin Reinecke committed Jul 09, 2018 19 20 ``````from __future__ import absolute_import, division, print_function from ..compat import * `````` Martin Reinecke committed Aug 23, 2017 21 ``````from .energy import Energy `````` Martin Reinecke committed Aug 19, 2017 22 23 24 25 `````` class QuadraticEnergy(Energy): """The Energy for a quadratic form. `````` Martin Reinecke committed Jul 03, 2018 26 `````` The most important aspect of this energy is that its metric must be `````` Martin Reinecke committed Aug 19, 2017 27 `````` position-independent. `````` Martin Reinecke committed Aug 19, 2017 28 29 `````` """ `````` Martin Reinecke committed Sep 29, 2017 30 `````` def __init__(self, position, A, b, _grad=None): `````` Martin Reinecke committed Aug 19, 2017 31 32 33 `````` super(QuadraticEnergy, self).__init__(position=position) self._A = A self._b = b `````` Martin Reinecke committed Sep 07, 2017 34 `````` if _grad is not None: `````` Martin Reinecke committed Dec 03, 2017 35 `````` self._grad = _grad `````` Martin Reinecke committed Mar 22, 2018 36 `````` Ax = _grad if b is None else _grad + b `````` Martin Reinecke committed Aug 20, 2017 37 `````` else: `````` Martin Reinecke committed Dec 03, 2017 38 `````` Ax = self._A(self.position) `````` Martin Reinecke committed Mar 22, 2018 39 40 41 42 `````` self._grad = Ax if b is None else Ax - b self._value = 0.5*self.position.vdot(Ax) if b is not None: self._value -= b.vdot(self.position) `````` Martin Reinecke committed Aug 19, 2017 43 44 `````` def at(self, position): `````` Martin Reinecke committed Sep 29, 2017 45 `````` return QuadraticEnergy(position=position, A=self._A, b=self._b) `````` Martin Reinecke committed Aug 19, 2017 46 `````` `````` Martin Reinecke committed Aug 20, 2017 47 `````` def at_with_grad(self, position, grad): `````` Martin Reinecke committed Feb 18, 2018 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 `````` """ 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 committed Sep 29, 2017 66 67 `````` return QuadraticEnergy(position=position, A=self._A, b=self._b, _grad=grad) `````` Martin Reinecke committed Aug 20, 2017 68 `````` `````` Martin Reinecke committed Aug 19, 2017 69 70 `````` @property def value(self): `````` Martin Reinecke committed Dec 03, 2017 71 `````` return self._value `````` Martin Reinecke committed Aug 19, 2017 72 73 74 `````` @property def gradient(self): `````` Martin Reinecke committed Dec 03, 2017 75 `````` return self._grad `````` Martin Reinecke committed Aug 19, 2017 76 77 `````` @property `````` Martin Reinecke committed Jul 03, 2018 78 `````` def metric(self): `````` Martin Reinecke committed Aug 19, 2017 79 `` return self._A``