|
| 1 | +// <copyright file="Series.cs" company="Math.NET"> |
| 2 | +// Math.NET Numerics, part of the Math.NET Project |
| 3 | +// https://numerics.mathdotnet.com |
| 4 | +// https://github.com/mathnet/mathnet-numerics |
| 5 | +// |
| 6 | +// Copyright (c) 2009-2021 Math.NET |
| 7 | +// |
| 8 | +// Permission is hereby granted, free of charge, to any person |
| 9 | +// obtaining a copy of this software and associated documentation |
| 10 | +// files (the "Software"), to deal in the Software without |
| 11 | +// restriction, including without limitation the rights to use, |
| 12 | +// copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | +// copies of the Software, and to permit persons to whom the |
| 14 | +// Software is furnished to do so, subject to the following |
| 15 | +// conditions: |
| 16 | +// |
| 17 | +// The above copyright notice and this permission notice shall be |
| 18 | +// included in all copies or substantial portions of the Software. |
| 19 | +// |
| 20 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 21 | +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 22 | +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 23 | +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 24 | +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 25 | +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 26 | +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 27 | +// OTHER DEALINGS IN THE SOFTWARE. |
| 28 | +// </copyright> |
| 29 | + |
| 30 | +using System; |
| 31 | +using System.Collections.Generic; |
| 32 | + |
| 33 | +namespace MathNet.Numerics |
| 34 | +{ |
| 35 | + public static class Series |
| 36 | + { |
| 37 | + /// <summary> |
| 38 | + /// Numerically stable series summation (stops automatically). |
| 39 | + /// </summary> |
| 40 | + /// <param name="nextSummand">provides the summands sequentially</param> |
| 41 | + /// <returns>Sum</returns> |
| 42 | + public static double Evaluate(Func<double> nextSummand) |
| 43 | + { |
| 44 | + double compensation = 0.0; |
| 45 | + double current; |
| 46 | + const double factor = 1 << 16; |
| 47 | + |
| 48 | + double sum = nextSummand(); |
| 49 | + |
| 50 | + do |
| 51 | + { |
| 52 | + // Kahan Summation |
| 53 | + // NOTE (ruegg): do NOT optimize. Now, how to tell that the compiler? |
| 54 | + current = nextSummand(); |
| 55 | + double y = current - compensation; |
| 56 | + double t = sum + y; |
| 57 | + compensation = t - sum; |
| 58 | + compensation -= y; |
| 59 | + sum = t; |
| 60 | + } |
| 61 | + while (Math.Abs(sum) < Math.Abs(factor*current)); |
| 62 | + |
| 63 | + return sum; |
| 64 | + } |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// Numerically stable series summation (stops automatically). |
| 68 | + /// </summary> |
| 69 | + /// <param name="infiniteSummands">provides the summands sequentially</param> |
| 70 | + /// <returns>Sum</returns> |
| 71 | + public static double Evaluate(IEnumerable<double> infiniteSummands) |
| 72 | + { |
| 73 | + double compensation = 0.0; |
| 74 | + double current, sum; |
| 75 | + const double factor = 1 << 16; |
| 76 | + |
| 77 | + using (var enumerator = infiniteSummands.GetEnumerator()) |
| 78 | + { |
| 79 | + if (!enumerator.MoveNext()) |
| 80 | + { |
| 81 | + return 0.0; |
| 82 | + } |
| 83 | + |
| 84 | + sum = enumerator.Current; |
| 85 | + if (!enumerator.MoveNext()) |
| 86 | + { |
| 87 | + return sum; |
| 88 | + } |
| 89 | + |
| 90 | + do |
| 91 | + { |
| 92 | + // Kahan Summation |
| 93 | + // NOTE (ruegg): do NOT optimize. Now, how to tell that the compiler? |
| 94 | + current = enumerator.Current; |
| 95 | + double y = current - compensation; |
| 96 | + double t = sum + y; |
| 97 | + compensation = t - sum; |
| 98 | + compensation -= y; |
| 99 | + sum = t; |
| 100 | + } while (Math.Abs(sum) < Math.Abs(factor * current) && enumerator.MoveNext()); |
| 101 | + } |
| 102 | + |
| 103 | + return sum; |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments