ningshuxia
2022-12-12 4f1314cb69a47c22e52f1efdc4b4be6c1d55143d
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
using System;
 
namespace IStation.Numerics
{
    public static class DifferIntegrate
    {
        /// <summary>
        /// Evaluates the Riemann-Liouville fractional derivative that uses the double exponential integration.
        /// </summary>
        /// <remarks>
        /// <para>order = 1.0 : normal derivative</para>
        /// <para>order = 0.5 : semi-derivative</para>
        /// <para>order = -0.5 : semi-integral</para>
        /// <para>order = -1.0 : normal integral</para>
        /// </remarks>
        /// <param name="f">The analytic smooth function to differintegrate.</param>
        /// <param name="x">The evaluation point.</param>
        /// <param name="order">The order of fractional derivative.</param>
        /// <param name="x0">The reference point of integration.</param>
        /// <param name="targetAbsoluteError">The expected relative accuracy of the Double-Exponential integration.</param>
        /// <returns>Approximation of the differintegral of order n at x.</returns>
        public static double DoubleExponential(Func<double, double> f, double x, double order, double x0 = 0, double targetAbsoluteError = 1E-10)
        {
            // The Riemann–Liouville fractional derivative of f(x) of order n is defined as
            //    \,_{x_0}{\mathbb{D}}^n_xf(x) = \frac{1}{\Gamma(m-n)} \frac{d^m}{dx^m} \int_{x_0}^{x} (x-t)^{m-n-1} f(t) dt
            //    where m is the smallest interger greater than n.
            // see https://en.wikipedia.org/wiki/Differintegral
 
            if (Math.Abs(order) < double.Epsilon)
            {
                return f(x);
            }
            else if (order > 0 && Math.Abs(order - (int)order) < double.Epsilon)
            {
                return Differentiate.Derivative(f, x, (int)order);
            }
            else
            {
                int m = (int)Math.Ceiling(order) + 1;
                if (m < 1) m = 1;
                double r = m - order - 1;
                Func<double, double> g = (v) => Integrate.DoubleExponential((t) => Math.Pow(v - t, r) * f(t), x0, v, targetAbsoluteError: targetAbsoluteError);
                double numerator = Differentiate.Derivative(g, x, m);
                double denominator = SpecialFunctions.Gamma(m - order);
                return numerator / denominator;
            }
        }
 
        /// <summary>
        /// Evaluates the Riemann-Liouville fractional derivative that uses the Gauss-Legendre integration.
        /// </summary>
        /// <remarks>
        /// <para>order = 1.0 : normal derivative</para>
        /// <para>order = 0.5 : semi-derivative</para>
        /// <para>order = -0.5 : semi-integral</para>
        /// <para>order = -1.0 : normal integral</para>
        /// </remarks>
        /// <param name="f">The analytic smooth function to differintegrate.</param>
        /// <param name="x">The evaluation point.</param>
        /// <param name="order">The order of fractional derivative.</param>
        /// <param name="x0">The reference point of integration.</param>
        /// <param name="gaussLegendrePoints">The number of Gauss-Legendre points.</param>
        /// <returns>Approximation of the differintegral of order n at x.</returns>
        public static double GaussLegendre(Func<double, double> f, double x, double order, double x0 = 0, int gaussLegendrePoints = 128)
        {
            // The Riemann–Liouville fractional derivative of f(x) of order n is defined as
            //    \,_{x_0}{\mathbb{D}}^n_xf(x) = \frac{1}{\Gamma(m-n)} \frac{d^m}{dx^m} \int_{x_0}^{x} (x-t)^{m-n-1} f(t) dt
            //    where m is the smallest interger greater than n.
            // see https://en.wikipedia.org/wiki/Differintegral
 
            if (Math.Abs(order) < double.Epsilon)
            {
                return f(x);
            }
            else if (order > 0 && Math.Abs(order - (int)order) < double.Epsilon) 
            {
                return Differentiate.Derivative(f, x, (int)order);
            }
            else
            {
                int m = (int)Math.Ceiling(order) + 1;
                if (m < 1) m = 1;
                double r = m - order - 1;
                Func<double, double> g = (v) => Integrate.GaussLegendre((t) => Math.Pow(v - t, r) * f(t), x0, v, order: gaussLegendrePoints);
                double numerator = Differentiate.Derivative(g, x, m);
                double denominator = SpecialFunctions.Gamma(m - order);
                return numerator / denominator;
            }
        }
 
        /// <summary>
        /// Evaluates the Riemann-Liouville fractional derivative that uses the Gauss-Kronrod integration.
        /// </summary>
        /// <remarks>
        /// <para>order = 1.0 : normal derivative</para>
        /// <para>order = 0.5 : semi-derivative</para>
        /// <para>order = -0.5 : semi-integral</para>
        /// <para>order = -1.0 : normal integral</para>
        /// </remarks>
        /// <param name="f">The analytic smooth function to differintegrate.</param>
        /// <param name="x">The evaluation point.</param>
        /// <param name="order">The order of fractional derivative.</param>
        /// <param name="x0">The reference point of integration.</param>
        /// <param name="targetRelativeError">The expected relative accuracy of the Gauss-Kronrod integration.</param>
        /// <param name="gaussKronrodPoints">The number of Gauss-Kronrod points. Pre-computed for 15, 21, 31, 41, 51 and 61 points.</param>
        /// <returns>Approximation of the differintegral of order n at x.</returns>
        public static double GaussKronrod(Func<double, double> f, double x, double order, double x0 = 0, double targetRelativeError = 1E-10, int gaussKronrodPoints = 15)
        {
            // The Riemann–Liouville fractional derivative of f(x) of order n is defined as
            //    \,_{x_0}{\mathbb{D}}^n_xf(x) = \frac{1}{\Gamma(m-n)} \frac{d^m}{dx^m} \int_{x_0}^{x} (x-t)^{m-n-1} f(t) dt
            //    where m is the smallest interger greater than n.
            // see https://en.wikipedia.org/wiki/Differintegral
 
            if (Math.Abs(order) < double.Epsilon)
            {
                return f(x);
            }
            else if (order > 0 && Math.Abs(order - (int)order) < double.Epsilon) 
            {
                return Differentiate.Derivative(f, x, (int)order);
            }
            else
            {
                int m = (int)Math.Ceiling(order) + 1;
                if (m < 1) m = 1;
                double r = m - order - 1;
                Func<double, double> g = (v) => Integrate.GaussKronrod((t) => Math.Pow(v - t, r) * f(t), x0, v, targetRelativeError: targetRelativeError, order: gaussKronrodPoints);
                double numerator = Differentiate.Derivative(g, x, m);
                double denominator = SpecialFunctions.Gamma(m - order);
                return numerator / denominator;
            }
        }
    }
}