lixiaojun
2023-04-12 fc6b7c9852f18e42fb9bccaf0cc22fbe5389d179
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
// <copyright file="NumericalDerivative.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
//
// Copyright (c) 2009-2015 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
 
using System;
using System.Linq;
 
 
namespace IStation.Numerics.Differentiation
{
    /// <summary>
    /// Type of finite different step size.
    /// </summary>
    public enum StepType
    {
        /// <summary>
        /// The absolute step size value will be used in numerical derivatives, regardless of order or function parameters.
        /// </summary>
        Absolute,
 
        /// <summary>
        /// A base step size value, h, will be scaled according to the function input parameter. A common example is hx = h*(1+abs(x)), however
        /// this may vary depending on implementation. This definition only guarantees that the only scaling will be relative to the
        /// function input parameter and not the order of the finite difference derivative.
        /// </summary>
        RelativeX,
 
        /// <summary>
        /// A base step size value, eps (typically machine precision), is scaled according to the finite difference coefficient order
        /// and function input parameter. The initial scaling according to finite different coefficient order can be thought of as producing a
        /// base step size, h, that is equivalent to <see cref="RelativeX"/> scaling. This step size is then scaled according to the function
        /// input parameter. Although implementation may vary, an example of second order accurate scaling may be (eps)^(1/3)*(1+abs(x)).
        /// </summary>
        Relative
    };
 
    /// <summary>
    /// Class to evaluate the numerical derivative of a function using finite difference approximations.
    /// Variable point and center methods can be initialized <seealso cref="FiniteDifferenceCoefficients"/>.
    /// This class can also be used to return function handles (delegates) for a fixed derivative order and variable.
    /// It is possible to evaluate the derivative and partial derivative of univariate and multivariate functions respectively.
    /// </summary>
    public class NumericalDerivative
    {
        readonly int _points;
        int _center;
        double _stepSize = Math.Pow(2, -10);
        double _epsilon = Precision.PositiveMachineEpsilon;
        double _baseStepSize = Math.Pow(2, -26);
        readonly FiniteDifferenceCoefficients _coefficients;
 
        /// <summary>
        /// Initializes a NumericalDerivative class with the default 3 point center difference method.
        /// </summary>
        public NumericalDerivative() : this(3, 1)
        {
        }
 
        /// <summary>
        /// Initialized a NumericalDerivative class.
        /// </summary>
        /// <param name="points">Number of points for finite difference derivatives.</param>
        /// <param name="center">Location of the center with respect to other points. Value ranges from zero to points-1.</param>
        public NumericalDerivative(int points, int center)
        {
            if (points < 2)
            {
                throw new ArgumentOutOfRangeException(nameof(points), "Points must be two or greater.");
            }
 
            _center = center;
            _points = points;
            Center = center;
            _coefficients = new FiniteDifferenceCoefficients(points);
        }
 
        /// <summary>
        /// Sets and gets the finite difference step size. This value is for each function evaluation if relative step size types are used.
        /// If the base step size used in scaling is desired, see <see cref="Epsilon"/>.
        /// </summary>
        /// <remarks>
        /// Setting then getting the StepSize may return a different value. This is not unusual since a user-defined step size is converted to a
        /// base-2 representable number to improve finite difference accuracy.
        /// </remarks>
        public double StepSize
        {
            get => _stepSize;
            set
            {
                //Base 2 yields more accurate results...
                var p = Math.Log(Math.Abs(value))/Math.Log(2);
                _stepSize = Math.Pow(2, Math.Round(p));
            }
        }
 
        /// <summary>
        /// Sets and gets the base finite difference step size. This assigned value to this parameter is only used if <see cref="StepType"/> is set to RelativeX.
        /// However, if the StepType is Relative, it will contain the base step size computed from <see cref="Epsilon"/> based on the finite difference order.
        /// </summary>
        public double BaseStepSize
        {
            get => _baseStepSize;
            set
            {
                //Base 2 yields more accurate results...
                var p = Math.Log(Math.Abs(value)) / Math.Log(2);
                _baseStepSize = Math.Pow(2, Math.Round(p));
            }
        }
 
        /// <summary>
        /// Sets and gets the base finite difference step size. This parameter is only used if <see cref="StepType"/> is set to Relative.
        /// By default this is set to machine epsilon, from which <see cref="BaseStepSize"/> is computed.
        /// </summary>
        public double Epsilon
        {
            get => _epsilon;
            set
            {
                //Base 2 yields more accurate results...
                var p = Math.Log(Math.Abs(value)) / Math.Log(2);
                _epsilon = Math.Pow(2, Math.Round(p));
            }
        }
 
        /// <summary>
        /// Sets and gets the location of the center point for the finite difference derivative.
        /// </summary>
        public int Center
        {
            get => _center;
            set
            {
                if (value >= _points || value < 0)
                    throw new ArgumentOutOfRangeException(nameof(value), "Center must lie between 0 and points -1");
                _center = value;
            }
        }
 
        /// <summary>
        /// Number of times a function is evaluated for numerical derivatives.
        /// </summary>
        public int Evaluations { get; private set; }
 
        /// <summary>
        /// Type of step size for computing finite differences. If set to absolute, dx = h.
        /// If set to relative, dx = (1+abs(x))*h^(2/(order+1)). This provides accurate results when
        /// h is approximately equal to the square-root of machine accuracy, epsilon.
        /// </summary>
        public StepType StepType { get; set; } = StepType.Relative;
 
        /// <summary>
        /// Evaluates the derivative of equidistant points using the finite difference method.
        /// </summary>
        /// <param name="points">Vector of points StepSize apart.</param>
        /// <param name="order">Derivative order.</param>
        /// <param name="stepSize">Finite difference step size.</param>
        /// <returns>Derivative of points of the specified order.</returns>
        public double EvaluateDerivative(double[] points, int order, double stepSize)
        {
            if (points == null)
                throw new ArgumentNullException(nameof(points));
 
            if (order >= _points || order < 0)
                throw new ArgumentOutOfRangeException(nameof(order), "Order must be between zero and points-1.");
 
            var c = _coefficients.GetCoefficients(Center, order);
            var result = c.Select((t, i) => t*points[i]).Sum();
            result /= Math.Pow(stepSize, order);
            return result;
        }
 
        /// <summary>
        /// Evaluates the derivative of a scalar univariate function.
        /// </summary>
        /// <remarks>
        /// Supplying the optional argument currentValue will reduce the number of function evaluations
        /// required to calculate the finite difference derivative.
        /// </remarks>
        /// <param name="f">Function handle.</param>
        /// <param name="x">Point at which to compute the derivative.</param>
        /// <param name="order">Derivative order.</param>
        /// <param name="currentValue">Current function value at center.</param>
        /// <returns>Function derivative at x of the specified order.</returns>
        public double EvaluateDerivative(Func<double, double> f, double x, int order, double? currentValue = null)
        {
            var c = _coefficients.GetCoefficients(Center, order);
            var h = CalculateStepSize(_points, x, order);
 
            var points = new double[_points];
            for (int i = 0; i < _points; i++)
            {
                if (i == Center && currentValue.HasValue)
                    points[i] = currentValue.Value;
                else if(c[i] != 0) // Only evaluate function if it will actually be used.
                {
                    points[i] = f(x + (i - Center) * h);
                    Evaluations++;
                }
            }
 
            return EvaluateDerivative(points, order, h);
        }
 
        /// <summary>
        /// Creates a function handle for the derivative of a scalar univariate function.
        /// </summary>
        /// <param name="f">Input function handle.</param>
        /// <param name="order">Derivative order.</param>
        /// <returns>Function handle that evaluates the derivative of input function at a fixed order.</returns>
        public Func<double, double> CreateDerivativeFunctionHandle(Func<double, double> f, int order)
        {
            return x => EvaluateDerivative(f, x, order);
        }
 
        /// <summary>
        /// Evaluates the partial derivative of a multivariate function.
        /// </summary>
        /// <param name="f">Multivariate function handle.</param>
        /// <param name="x">Vector at which to evaluate the derivative.</param>
        /// <param name="parameterIndex">Index of independent variable for partial derivative.</param>
        /// <param name="order">Derivative order.</param>
        /// <param name="currentValue">Current function value at center.</param>
        /// <returns>Function partial derivative at x of the specified order.</returns>
        public double EvaluatePartialDerivative(Func<double[], double> f, double[] x, int parameterIndex, int order, double? currentValue = null)
        {
            var xi = x[parameterIndex];
            var c = _coefficients.GetCoefficients(Center, order);
            var h = CalculateStepSize(_points, x[parameterIndex], order);
 
            var points = new double[_points];
            for (int i = 0; i < _points; i++)
            {
                if (i == Center && currentValue.HasValue)
                    points[i] = currentValue.Value;
                else if(c[i] != 0) // Only evaluate function if it will actually be used.
                {
                    x[parameterIndex] = xi + (i - Center) * h;
                    points[i] = f(x);
                    Evaluations++;
                }
            }
 
            //restore original value
            x[parameterIndex] = xi;
            return EvaluateDerivative(points, order, h);
        }
 
        /// <summary>
        /// Evaluates the partial derivatives of a multivariate function array.
        /// </summary>
        /// <remarks>
        /// This function assumes the input vector x is of the correct length for f.
        /// </remarks>
        /// <param name="f">Multivariate vector function array handle.</param>
        /// <param name="x">Vector at which to evaluate the derivatives.</param>
        /// <param name="parameterIndex">Index of independent variable for partial derivative.</param>
        /// <param name="order">Derivative order.</param>
        /// <param name="currentValue">Current function value at center.</param>
        /// <returns>Vector of functions partial derivatives at x of the specified order.</returns>
        public double[] EvaluatePartialDerivative(Func<double[], double>[] f, double[] x, int parameterIndex, int order, double?[] currentValue = null)
        {
            var df = new double[f.Length];
            for (int i = 0; i < f.Length; i++)
            {
                if(currentValue != null && currentValue[i].HasValue)
                    df[i] = EvaluatePartialDerivative(f[i], x, parameterIndex, order, currentValue[i]!.Value);
                else
                    df[i] = EvaluatePartialDerivative(f[i], x, parameterIndex, order);
            }
 
            return df;
        }
 
        /// <summary>
        /// Creates a function handle for the partial derivative of a multivariate function.
        /// </summary>
        /// <param name="f">Input function handle.</param>
        /// <param name="parameterIndex">Index of the independent variable for partial derivative.</param>
        /// <param name="order">Derivative order.</param>
        /// <returns>Function handle that evaluates partial derivative of input function at a fixed order.</returns>
        public Func<double[], double> CreatePartialDerivativeFunctionHandle(Func<double[], double> f, int parameterIndex,
                                                                            int order)
        {
            return x => EvaluatePartialDerivative(f, x, parameterIndex, order);
        }
 
        /// <summary>
        /// Creates a function handle for the partial derivative of a vector multivariate function.
        /// </summary>
        /// <param name="f">Input function handle.</param>
        /// <param name="parameterIndex">Index of the independent variable for partial derivative.</param>
        /// <param name="order">Derivative order.</param>
        /// <returns>Function handle that evaluates partial derivative of input function at fixed order.</returns>
        public Func<double[], double[]> CreatePartialDerivativeFunctionHandle(Func<double[], double>[] f,
                                                                            int parameterIndex,
                                                                            int order)
        {
            return x => EvaluatePartialDerivative(f, x, parameterIndex, order);
        }
 
        /// <summary>
        /// Evaluates the mixed partial derivative of variable order for multivariate functions.
        /// </summary>
        /// <remarks>
        /// This function recursively uses <see cref="EvaluatePartialDerivative(Func&lt;double[], double&gt;, double[], int, int, double?)"/> to evaluate mixed partial derivative.
        /// Therefore, it is more efficient to call <see cref="EvaluatePartialDerivative(Func&lt;double[], double&gt;, double[], int, int, double?)"/> for higher order derivatives of
        /// a single independent variable.
        /// </remarks>
        /// <param name="f">Multivariate function handle.</param>
        /// <param name="x">Points at which to evaluate the derivative.</param>
        /// <param name="parameterIndex">Vector of indices for the independent variables at descending derivative orders.</param>
        /// <param name="order">Highest order of differentiation.</param>
        /// <param name="currentValue">Current function value at center.</param>
        /// <returns>Function mixed partial derivative at x of the specified order.</returns>
        public double EvaluateMixedPartialDerivative(Func<double[], double> f, double[] x, int[] parameterIndex,
                                                     int order, double? currentValue = null)
        {
            if (parameterIndex.Length != order)
                throw new ArgumentOutOfRangeException(nameof(parameterIndex),
                                                      "The number of parameters must match derivative order.");
 
            if (order == 1)
                return EvaluatePartialDerivative(f, x, parameterIndex[0], order, currentValue);
 
            int reducedOrder = order - 1;
            var reducedParameterIndex = new int[reducedOrder];
            Array.Copy(parameterIndex, 0, reducedParameterIndex, 0, reducedOrder);
 
            var points = new double[_points];
            var currentParameterIndex = parameterIndex[order - 1];
            var h = CalculateStepSize(_points, x[currentParameterIndex], order);
 
            var xi = x[currentParameterIndex];
            for (int i = 0; i < _points; i++)
            {
                x[currentParameterIndex] = xi + (i - Center)*h;
                points[i] = EvaluateMixedPartialDerivative(f, x, reducedParameterIndex, reducedOrder);
            }
 
            // restore original value
            x[currentParameterIndex] = xi;
 
            // This will always be to the first order
            return EvaluateDerivative(points, 1, h);
        }
 
        /// <summary>
        /// Evaluates the mixed partial derivative of variable order for multivariate function arrays.
        /// </summary>
        /// <remarks>
        /// This function recursively uses <see cref="EvaluatePartialDerivative(Func&lt;double[], double&gt;[], double[], int, int, double?[])"/> to evaluate mixed partial derivative.
        /// Therefore, it is more efficient to call <see cref="EvaluatePartialDerivative(Func&lt;double[], double&gt;[], double[], int, int, double?[])"/> for higher order derivatives of
        /// a single independent variable.
        /// </remarks>
        /// <param name="f">Multivariate function array handle.</param>
        /// <param name="x">Vector at which to evaluate the derivative.</param>
        /// <param name="parameterIndex">Vector of indices for the independent variables at descending derivative orders.</param>
        /// <param name="order">Highest order of differentiation.</param>
        /// <param name="currentValue">Current function value at center.</param>
        /// <returns>Function mixed partial derivatives at x of the specified order.</returns>
        public double[] EvaluateMixedPartialDerivative(Func<double[], double>[] f, double[] x, int[] parameterIndex,
                                                       int order, double?[]? currentValue = null)
        {
            var df = new double[f.Length];
            for (int i = 0; i < f.Length; i++)
            {
                if(currentValue != null && currentValue[i].HasValue)
                    df[i] = EvaluateMixedPartialDerivative(f[i], x, parameterIndex, order, currentValue[i]!.Value);
                else
                    df[i] = EvaluateMixedPartialDerivative(f[i], x, parameterIndex, order);
            }
 
            return df;
        }
 
        /// <summary>
        /// Creates a function handle for the mixed partial derivative of a multivariate function.
        /// </summary>
        /// <param name="f">Input function handle.</param>
        /// <param name="parameterIndex">Vector of indices for the independent variables at descending derivative orders.</param>
        /// <param name="order">Highest derivative order.</param>
        /// <returns>Function handle that evaluates the fixed mixed partial derivative of input function at fixed order.</returns>
        public Func<double[], double> CreateMixedPartialDerivativeFunctionHandle(Func<double[], double> f,
                                                                                 int[] parameterIndex, int order)
        {
            return x => EvaluateMixedPartialDerivative(f, x, parameterIndex, order);
        }
 
        /// <summary>
        /// Creates a function handle for the mixed partial derivative of a multivariate vector function.
        /// </summary>
        /// <param name="f">Input vector function handle.</param>
        /// <param name="parameterIndex">Vector of indices for the independent variables at descending derivative orders.</param>
        /// <param name="order">Highest derivative order.</param>
        /// <returns>Function handle that evaluates the fixed mixed partial derivative of input function at fixed order.</returns>
        public Func<double[], double[]> CreateMixedPartialDerivativeFunctionHandle(Func<double[], double>[] f,
                                                                                 int[] parameterIndex, int order)
        {
            return x => EvaluateMixedPartialDerivative(f, x, parameterIndex, order);
        }
 
        /// <summary>
        /// Resets the evaluation counter.
        /// </summary>
        public void ResetEvaluations()
        {
            Evaluations = 0;
        }
 
        private double[] CalculateStepSize(int points, double[] x, double order)
        {
            var h = new double[x.Length];
            for (int i = 1; i < h.Length; i++)
                h[i] = CalculateStepSize(points, x[i], order);
 
            return h;
        }
 
        private double CalculateStepSize(int points, double x, double order)
        {
            // Step size relative to function input parameter
            if (StepType == StepType.RelativeX)
            {
                StepSize = BaseStepSize*(1 + Math.Abs(x));
            }
            // Step size relative to function input parameter and order
            else if (StepType == StepType.Relative)
            {
                var accuracy = points - order;
                BaseStepSize = Math.Pow(Epsilon,(1/(accuracy + order)));
                StepSize = BaseStepSize*(1 + Math.Abs(x));
            }
            // Do nothing for absolute step size.
 
            return StepSize;
        }
    }
}