|
| 1 | +using MultiPrecision; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Collections.ObjectModel; |
| 5 | +using System.Linq; |
| 6 | + |
| 7 | +namespace ErrorFunctionApproximation { |
| 8 | + public static class ErfcExpScaled<N> where N : struct, IConstant { |
| 9 | + private static readonly Dictionary<int, ReadOnlyCollection<(MultiPrecision<Double<N>> c, int n, int m)>> table = new() { |
| 10 | + { 0, new ReadOnlyCollection<(MultiPrecision<Double<N>> c, int n, int m)>(new (MultiPrecision<Double<N>> c, int n, int m)[]{ (1, 0, 1) })}, |
| 11 | + }; |
| 12 | + |
| 13 | + public static MultiPrecision<N> Diff(int d, MultiPrecision<N> x) { |
| 14 | + MultiPrecision<Double<N>> x_ex = x.Convert<Double<N>>(); |
| 15 | + MultiPrecision<Double<N>> y = MultiPrecision<Double<N>>.Exp(-x_ex * x_ex) / MultiPrecision<Double<N>>.Erfc(x_ex); |
| 16 | + |
| 17 | + MultiPrecision<Double<N>> s = 0, s0, s1; |
| 18 | + |
| 19 | + ReadOnlyCollection<(MultiPrecision<Double<N>> c, int n, int m)> coef = Coef(d); |
| 20 | + |
| 21 | + int i = 0; |
| 22 | + for (i = 0; i < coef.Count - 1; i += 2) { |
| 23 | + (MultiPrecision<Double<N>> c0, int n0, int m0) = coef[i]; |
| 24 | + (MultiPrecision<Double<N>> c1, int n1, int m1) = coef[i + 1]; |
| 25 | + |
| 26 | + (int n, int m) = (Math.Min(n0, n1), Math.Min(m0, m1)); |
| 27 | + |
| 28 | + if (n0 > n) { |
| 29 | + s0 = c0 * MultiPrecision<Double<N>>.Pow(x_ex, n0 - n) * MultiPrecision<Double<N>>.Pow(y, m0 - m); |
| 30 | + } |
| 31 | + else { |
| 32 | + s0 = c0 * MultiPrecision<Double<N>>.Pow(y, m0 - m); |
| 33 | + } |
| 34 | + |
| 35 | + if (n1 > n) { |
| 36 | + s1 = c1 * MultiPrecision<Double<N>>.Pow(x_ex, n1 - n) * MultiPrecision<Double<N>>.Pow(y, m1 - m); |
| 37 | + } |
| 38 | + else { |
| 39 | + s1 = c1 * MultiPrecision<Double<N>>.Pow(y, m1 - m); |
| 40 | + } |
| 41 | + |
| 42 | + s += MultiPrecision<Double<N>>.Pow(x_ex, n) * MultiPrecision<Double<N>>.Pow(y, m) * (s0 + s1); |
| 43 | + } |
| 44 | + |
| 45 | + for (; i < coef.Count; i++) { |
| 46 | + (MultiPrecision<Double<N>> c, int n, int m) = coef[i]; |
| 47 | + |
| 48 | + if (n > 0) { |
| 49 | + s += c * MultiPrecision<Double<N>>.Pow(x_ex, n) * MultiPrecision<Double<N>>.Pow(y, m); |
| 50 | + } |
| 51 | + else { |
| 52 | + s += c * MultiPrecision<Double<N>>.Pow(y, m); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + return s.Convert<N>(); |
| 57 | + } |
| 58 | + |
| 59 | + private static ReadOnlyCollection<(MultiPrecision<Double<N>> c, int n, int m)> Coef(int d) { |
| 60 | + if (d < 0) { |
| 61 | + throw new ArgumentOutOfRangeException(nameof(d)); |
| 62 | + } |
| 63 | + |
| 64 | + if (table.ContainsKey(d)) { |
| 65 | + return table[d]; |
| 66 | + } |
| 67 | + |
| 68 | + MultiPrecision<Double<N>> inv_sqrtpi = 1 / MultiPrecision<Double<N>>.Sqrt(MultiPrecision<Double<N>>.PI); |
| 69 | + |
| 70 | + Dictionary<(int n, int m), MultiPrecision<Double<N>>> t = new(); |
| 71 | + |
| 72 | + foreach ((MultiPrecision<Double<N>> c, int n, int m) in Coef(d - 1)) { |
| 73 | + if (!t.ContainsKey((n, m + 1))) { |
| 74 | + t.Add((n, m + 1), 0); |
| 75 | + } |
| 76 | + t[(n, m + 1)] += 2 * m * inv_sqrtpi * c; |
| 77 | + |
| 78 | + if (!t.ContainsKey((n + 1, m))) { |
| 79 | + t.Add((n + 1, m), 0); |
| 80 | + } |
| 81 | + t[(n + 1, m)] -= 2 * m * c; |
| 82 | + |
| 83 | + if (n > 0) { |
| 84 | + if (!t.ContainsKey((n - 1, m))) { |
| 85 | + t.Add((n - 1, m), 0); |
| 86 | + } |
| 87 | + t[(n - 1, m)] += n * c; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + ReadOnlyCollection<(MultiPrecision<Double<N>> c, int n, int m)> coef = new( |
| 92 | + t. |
| 93 | + OrderByDescending((item) => item.Key.m).ThenByDescending((item) => item.Key.n). |
| 94 | + Select((item) => (item.Value, item.Key.n, item.Key.m)).ToArray() |
| 95 | + ); |
| 96 | + |
| 97 | + table.Add(d, coef); |
| 98 | + |
| 99 | + return coef; |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments