energy.py 3.9 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
from ..nifty_meta import NiftyMeta
20
from .memoization import memo
21

Martin Reinecke's avatar
Martin Reinecke committed
22
from future.utils import with_metaclass
23

24

Martin Reinecke's avatar
Martin Reinecke committed
25
class Energy(with_metaclass(NiftyMeta, type('NewBase', (object,), {}))):
26
    """ Provides the functional used by minimization schemes.
27

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

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

    Attributes
    ----------
38
39
40
41
42
43
    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
Martin Reinecke's avatar
Martin Reinecke committed
44
        The gradient at given `position`.
45
46
47
    curvature : LinearOperator, callable
        A positive semi-definite operator or function describing the curvature
        of the potential at the given `position`.
48
49
50

    Notes
    -----
51
52
53
54
55
56
    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.
57

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

Martin Reinecke's avatar
Martin Reinecke committed
61
    See Also
62
63
    --------
    memo
64
65

    """
66

67
    def __init__(self, position):
68
69
        super(Energy, self).__init__()
        self._position = position.copy()
70
71

    def at(self, position):
72
        """ Initializes and returns a new Energy object at the new position.
73
74
75
76
77
78
79
80
81
82
83
84

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

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

        """
85

86
87
        return self.__class__(position)

88
89
    @property
    def position(self):
90
91
92
93
94
95
        """
        The Field location in parameter space where value, gradient and
        curvature are evaluated.

        """

96
97
        return self._position

98
99
    @property
    def value(self):
100
101
102
103
104
        """
        The value of the energy functional at given `position`.

        """

105
106
107
108
        raise NotImplementedError

    @property
    def gradient(self):
109
        """
Martin Reinecke's avatar
Martin Reinecke committed
110
        The gradient at given `position`.
111
112
113

        """

114
115
        raise NotImplementedError

Martin Reinecke's avatar
Martin Reinecke committed
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
    @property
    @memo
    def gradient_norm(self):
        """
        The length of the gradient at given `position`.

        """

        return self.gradient.norm()

    @property
    @memo
    def gradient_infnorm(self):
        """
        The infinity norm of the gradient at given `position`.

        """

        return abs(self.gradient).max()

136
137
    @property
    def curvature(self):
138
139
140
141
142
143
        """
        A positive semi-definite operator or function describing the curvature
        of the potential at the given `position`.

        """

144
        raise NotImplementedError