energy.py 3.62 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
# 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/>.
Theo Steininger's avatar
Theo Steininger committed
13 14 15 16 17
#
# 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.
18

19 20
from nifty.nifty_meta import NiftyMeta

21
from keepers import Loggable
22

23 24

class Energy(Loggable, object):
25
    """ Provides the functional used by minimization schemes.
26

27 28
   The Energy object is an implementation of a scalar function including its
   gradient and curvature at some position.
29 30 31

    Parameters
    ----------
32 33
    position : Field
        The input parameter of the scalar function.
34 35 36

    Attributes
    ----------
37 38 39 40 41 42 43 44 45 46
    position : Field
        The Field location in parameter space where value, gradient and
        curvature are evaluated.
    value : np.float
        The value of the energy functional at given `position`.
    gradient : Field
        The gradient at given `position` in parameter direction.
    curvature : LinearOperator, callable
        A positive semi-definite operator or function describing the curvature
        of the potential at the given `position`.
47 48 49

    Notes
    -----
50 51 52 53 54 55
    An instance of the Energy class is defined at a certain location. If one
    is interested in the value, gradient or curvature of the abstract energy
    functional one has to 'jump' to the new position using the `at` method.
    This method returns a new energy instance residing at the new position. By
    this approach, intermediate results from computing e.g. the gradient can
    safely be reused for e.g. the value or the curvature.
56

57 58 59 60 61 62
    Memorizing the evaluations of some quantities (using the memo decorator)
    minimizes the computational effort for multiple calls.

    See also
    --------
    memo
63 64

    """
65

Theo Steininger's avatar
Theo Steininger committed
66
    __metaclass__ = NiftyMeta
67

68 69 70 71 72 73
    def __init__(self, position):
        self._cache = {}
        try:
            position = position.copy()
        except AttributeError:
            pass
Theo Steininger's avatar
Theo Steininger committed
74
        self._position = position
75 76

    def at(self, position):
77
        """ Initializes and returns a new Energy object at the new position.
78 79 80 81 82 83 84 85 86 87 88 89

        Parameters
        ----------
        position : Field
            Parameter for the new Energy object.

        Returns
        -------
        out : Energy
            Energy object at new position.

        """
90

91 92
        return self.__class__(position)

93 94
    @property
    def position(self):
95 96 97 98 99 100
        """
        The Field location in parameter space where value, gradient and
        curvature are evaluated.

        """

101 102
        return self._position

103 104
    @property
    def value(self):
105 106 107 108 109
        """
        The value of the energy functional at given `position`.

        """

110 111 112 113
        raise NotImplementedError

    @property
    def gradient(self):
114 115 116 117 118
        """
        The gradient at given `position` in parameter direction.

        """

119 120 121 122
        raise NotImplementedError

    @property
    def curvature(self):
123 124 125 126 127 128
        """
        A positive semi-definite operator or function describing the curvature
        of the potential at the given `position`.

        """

129
        raise NotImplementedError