@@ -91,10 +91,66 @@ namespace ROOT {
91
91
// if it inherits from ROOT::Math::IGradientFunctionMultiDim.
92
92
virtual bool HasGradient () const { return false ; }
93
93
94
- private:
94
+ virtual bool returnsInMinuit2ParameterSpace () const { return false ; }
95
+
96
+ // / Evaluate all the vector of function derivatives (gradient) at a point x.
97
+ // / Derived classes must re-implement it if more efficient than evaluating one at a time
98
+ virtual void Gradient (const T *x, T *grad) const
99
+ {
100
+ unsigned int ndim = NDim ();
101
+ for (unsigned int icoord = 0 ; icoord < ndim; ++icoord) {
102
+ grad[icoord] = Derivative (x, icoord);
103
+ }
104
+ }
105
+
106
+ // / In some cases, the gradient algorithm will use information from the previous step, these can be passed
107
+ // / in with this overload. The `previous_*` arrays can also be used to return second derivative and step size
108
+ // / so that these can be passed forward again as well at the call site, if necessary.
109
+ virtual void GradientWithPrevResult (const T *x, T *grad, T *previous_grad, T *previous_g2, T *previous_gstep) const
110
+ {
111
+ unsigned int ndim = NDim ();
112
+ for (unsigned int icoord = 0 ; icoord < ndim; ++icoord) {
113
+ grad[icoord] = Derivative (x, icoord, previous_grad, previous_g2, previous_gstep);
114
+ }
115
+ }
116
+
117
+ // / Optimized method to evaluate at the same time the function value and derivative at a point x.
118
+ // / Often both value and derivatives are needed and it is often more efficient to compute them at the same time.
119
+ // / Derived class should implement this method if performances play an important role and if it is faster to
120
+ // / evaluate value and derivative at the same time
121
+ virtual void FdF (const T *x, T &f, T *df) const
122
+ {
123
+ f = operator ()(x);
124
+ Gradient (x, df);
125
+ }
126
+
127
+ // / Return the partial derivative with respect to the passed coordinate.
128
+ T Derivative (const T *x, unsigned int icoord = 0 ) const { return DoDerivative (x, icoord); }
129
+
130
+ // / In some cases, the derivative algorithm will use information from the previous step, these can be passed
131
+ // / in with this overload. The `previous_*` arrays can also be used to return second derivative and step size
132
+ // / so that these can be passed forward again as well at the call site, if necessary.
133
+ T Derivative (const T *x, unsigned int icoord, T *previous_grad, T *previous_g2,
134
+ T *previous_gstep) const
135
+ {
136
+ return DoDerivativeWithPrevResult (x, icoord, previous_grad, previous_g2, previous_gstep);
137
+ }
95
138
139
+ private:
96
140
// / Implementation of the evaluation function. Must be implemented by derived classes.
97
141
virtual T DoEval (const T *x) const = 0;
142
+
143
+ // / Function to evaluate the derivative with respect each coordinate. To be implemented by the derived class.
144
+ virtual T DoDerivative (const T * /* x*/ , unsigned int /* icoord*/ ) const { return {}; }
145
+
146
+ // / In some cases, the derivative algorithm will use information from the previous step, these can be passed
147
+ // / in with this overload. The `previous_*` arrays can also be used to return second derivative and step size
148
+ // / so that these can be passed forward again as well at the call site, if necessary.
149
+ virtual T DoDerivativeWithPrevResult (const T *x, unsigned int icoord, T * /* previous_grad*/ ,
150
+ T * /* previous_g2*/ , T * /* previous_gstep*/ ) const
151
+ {
152
+ return DoDerivative (x, icoord);
153
+ }
98
154
};
99
155
100
156
@@ -135,10 +191,36 @@ namespace ROOT {
135
191
// if it inherits from ROOT::Math::IGradientFunctionOneDim.
136
192
virtual bool HasGradient () const { return false ; }
137
193
194
+ // / Return the derivative of the function at a point x
195
+ // / Use the private method DoDerivative
196
+ double Derivative (double x) const { return DoDerivative (x); }
197
+
198
+ // / Compatibility method with multi-dimensional interface for partial derivative.
199
+ double Derivative (const double *x) const { return DoDerivative (*x); }
200
+
201
+ // / Compatibility method with multi-dimensional interface for Gradient.
202
+ void Gradient (const double *x, double *g) const { g[0 ] = DoDerivative (*x); }
203
+
204
+ // / Optimized method to evaluate at the same time the function value and derivative at a point x.
205
+ // / Often both value and derivatives are needed and it is often more efficient to compute them at the same time.
206
+ // / Derived class should implement this method if performances play an important role and if it is faster to
207
+ // / evaluate value and derivative at the same time.
208
+ virtual void FdF (double x, double &f, double &df) const
209
+ {
210
+ f = operator ()(x);
211
+ df = Derivative (x);
212
+ }
213
+
214
+ // / Compatibility method with multi-dimensional interface for Gradient and function evaluation.
215
+ void FdF (const double *x, double &f, double *df) const { FdF (*x, f, *df); }
216
+
138
217
private:
139
218
140
219
// / implementation of the evaluation function. Must be implemented by derived classes
141
220
virtual double DoEval (double x) const = 0;
221
+
222
+ // / Function to evaluate the derivative with respect each coordinate. To be implemented by the derived class.
223
+ virtual double DoDerivative (double ) const { return 0 .; }
142
224
};
143
225
144
226
@@ -170,71 +252,8 @@ namespace ROOT {
170
252
class IGradientFunctionMultiDimTempl : virtual public IBaseFunctionMultiDimTempl<T> {
171
253
172
254
public:
173
- typedef IBaseFunctionMultiDimTempl<T> BaseFunc;
174
- typedef IGradientFunctionMultiDimTempl<T> BaseGrad;
175
-
176
-
177
- // / Evaluate all the vector of function derivatives (gradient) at a point x.
178
- // / Derived classes must re-implement it if more efficient than evaluating one at a time
179
- virtual void Gradient (const T *x, T *grad) const
180
- {
181
- unsigned int ndim = NDim ();
182
- for (unsigned int icoord = 0 ; icoord < ndim; ++icoord) {
183
- grad[icoord] = Derivative (x, icoord);
184
- }
185
- }
186
-
187
- // / In some cases, the gradient algorithm will use information from the previous step, these can be passed
188
- // / in with this overload. The `previous_*` arrays can also be used to return second derivative and step size
189
- // / so that these can be passed forward again as well at the call site, if necessary.
190
- virtual void GradientWithPrevResult (const T *x, T *grad, T *previous_grad, T *previous_g2, T *previous_gstep) const
191
- {
192
- unsigned int ndim = NDim ();
193
- for (unsigned int icoord = 0 ; icoord < ndim; ++icoord) {
194
- grad[icoord] = Derivative (x, icoord, previous_grad, previous_g2, previous_gstep);
195
- }
196
- }
197
-
198
- using BaseFunc::NDim;
199
-
200
- // / Optimized method to evaluate at the same time the function value and derivative at a point x.
201
- // / Often both value and derivatives are needed and it is often more efficient to compute them at the same time.
202
- // / Derived class should implement this method if performances play an important role and if it is faster to
203
- // / evaluate value and derivative at the same time
204
- virtual void FdF (const T *x, T &f, T *df) const
205
- {
206
- f = BaseFunc::operator ()(x);
207
- Gradient (x, df);
208
- }
209
-
210
- // / Return the partial derivative with respect to the passed coordinate.
211
- T Derivative (const T *x, unsigned int icoord = 0 ) const { return DoDerivative (x, icoord); }
212
-
213
- // / In some cases, the derivative algorithm will use information from the previous step, these can be passed
214
- // / in with this overload. The `previous_*` arrays can also be used to return second derivative and step size
215
- // / so that these can be passed forward again as well at the call site, if necessary.
216
- T Derivative (const T *x, unsigned int icoord, T *previous_grad, T *previous_g2,
217
- T *previous_gstep) const
218
- {
219
- return DoDerivativeWithPrevResult (x, icoord, previous_grad, previous_g2, previous_gstep);
220
- }
221
255
222
256
bool HasGradient () const override { return true ; }
223
-
224
- virtual bool returnsInMinuit2ParameterSpace () const { return false ; }
225
-
226
- private:
227
- // / Function to evaluate the derivative with respect each coordinate. To be implemented by the derived class.
228
- virtual T DoDerivative (const T *x, unsigned int icoord) const = 0;
229
-
230
- // / In some cases, the derivative algorithm will use information from the previous step, these can be passed
231
- // / in with this overload. The `previous_*` arrays can also be used to return second derivative and step size
232
- // / so that these can be passed forward again as well at the call site, if necessary.
233
- virtual T DoDerivativeWithPrevResult (const T *x, unsigned int icoord, T * /* previous_grad*/ ,
234
- T * /* previous_g2*/ , T * /* previous_gstep*/ ) const
235
- {
236
- return DoDerivative (x, icoord);
237
- }
238
257
};
239
258
240
259
@@ -257,38 +276,7 @@ namespace ROOT {
257
276
258
277
public:
259
278
260
- typedef IBaseFunctionOneDim BaseFunc;
261
- typedef IGradientFunctionOneDim BaseGrad;
262
-
263
- // / Return the derivative of the function at a point x
264
- // / Use the private method DoDerivative
265
- double Derivative (double x) const { return DoDerivative (x); }
266
-
267
- // / Compatibility method with multi-dimensional interface for partial derivative.
268
- double Derivative (const double *x) const { return DoDerivative (*x); }
269
-
270
- // / Compatibility method with multi-dimensional interface for Gradient.
271
- void Gradient (const double *x, double *g) const { g[0 ] = DoDerivative (*x); }
272
-
273
- // / Optimized method to evaluate at the same time the function value and derivative at a point x.
274
- // / Often both value and derivatives are needed and it is often more efficient to compute them at the same time.
275
- // / Derived class should implement this method if performances play an important role and if it is faster to
276
- // / evaluate value and derivative at the same time.
277
- virtual void FdF (double x, double &f, double &df) const
278
- {
279
- f = operator ()(x);
280
- df = Derivative (x);
281
- }
282
-
283
- // / Compatibility method with multi-dimensional interface for Gradient and function evaluation.
284
- void FdF (const double *x, double &f, double *df) const { FdF (*x, f, *df); }
285
-
286
279
bool HasGradient () const override { return true ; }
287
-
288
- private:
289
-
290
- // / Function to evaluate the derivative with respect each coordinate. To be implemented by the derived class.
291
- virtual double DoDerivative (double x) const = 0;
292
280
};
293
281
294
282
0 commit comments