From c02b01b23d7eb5283e8f9afea0c4644da1b740a7 Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Sun, 27 Jun 2021 15:42:07 +0200 Subject: [PATCH] Expose previously internal Series.Evaluate --- build.fsx | 2 +- src/Numerics/Series.cs | 106 +++++++++++++++++++++ src/Numerics/SpecialFunctions/Evaluate.cs | 31 +----- src/Numerics/SpecialFunctions/Stability.cs | 2 +- 4 files changed, 109 insertions(+), 32 deletions(-) create mode 100644 src/Numerics/Series.cs diff --git a/build.fsx b/build.fsx index c4c0259d..6d78b463 100644 --- a/build.fsx +++ b/build.fsx @@ -385,7 +385,7 @@ Target "DocsWatch" (fun _ -> Target "CleanApi" (fun _ -> CleanDirs ["out/api"]) Target "Api" (fun _ -> - !! "src/Numerics/bin/Release/net40/MathNet.Numerics.dll" + !! "src/Numerics/bin/Release/net461/MathNet.Numerics.dll" |> Docu (fun p -> { p with ToolPath = "tools/docu/docu.exe" diff --git a/src/Numerics/Series.cs b/src/Numerics/Series.cs new file mode 100644 index 00000000..77e2c3ee --- /dev/null +++ b/src/Numerics/Series.cs @@ -0,0 +1,106 @@ +// +// Math.NET Numerics, part of the Math.NET Project +// https://numerics.mathdotnet.com +// https://github.com/mathnet/mathnet-numerics +// +// Copyright (c) 2009-2021 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. +// + +using System; +using System.Collections.Generic; + +namespace MathNet.Numerics +{ + public static class Series + { + /// + /// Numerically stable series summation (stops automatically). + /// + /// provides the summands sequentially + /// Sum + public static double Evaluate(Func nextSummand) + { + double compensation = 0.0; + double current; + const double factor = 1 << 16; + + double sum = nextSummand(); + + do + { + // Kahan Summation + // NOTE (ruegg): do NOT optimize. Now, how to tell that the compiler? + current = nextSummand(); + double y = current - compensation; + double t = sum + y; + compensation = t - sum; + compensation -= y; + sum = t; + } + while (Math.Abs(sum) < Math.Abs(factor*current)); + + return sum; + } + + /// + /// Numerically stable series summation (stops automatically). + /// + /// provides the summands sequentially + /// Sum + public static double Evaluate(IEnumerable infiniteSummands) + { + double compensation = 0.0; + double current, sum; + const double factor = 1 << 16; + + using (var enumerator = infiniteSummands.GetEnumerator()) + { + if (!enumerator.MoveNext()) + { + return 0.0; + } + + sum = enumerator.Current; + if (!enumerator.MoveNext()) + { + return sum; + } + + do + { + // Kahan Summation + // NOTE (ruegg): do NOT optimize. Now, how to tell that the compiler? + current = enumerator.Current; + double y = current - compensation; + double t = sum + y; + compensation = t - sum; + compensation -= y; + sum = t; + } while (Math.Abs(sum) < Math.Abs(factor * current) && enumerator.MoveNext()); + } + + return sum; + } + } +} diff --git a/src/Numerics/SpecialFunctions/Evaluate.cs b/src/Numerics/SpecialFunctions/Evaluate.cs index 4df41602..7ea5d5ff 100644 --- a/src/Numerics/SpecialFunctions/Evaluate.cs +++ b/src/Numerics/SpecialFunctions/Evaluate.cs @@ -52,37 +52,8 @@ namespace MathNet.Numerics /// /// Evaluation functions, useful for function approximation. /// - public static class Evaluate + internal static class Evaluate { - /// - /// Numerically stable series summation - /// - /// provides the summands sequentially - /// Sum - internal static double Series(Func nextSummand) - { - double compensation = 0.0; - double current; - const double factor = 1 << 16; - - double sum = nextSummand(); - - do - { - // Kahan Summation - // NOTE (ruegg): do NOT optimize. Now, how to tell that the compiler? - current = nextSummand(); - double y = current - compensation; - double t = sum + y; - compensation = t - sum; - compensation -= y; - sum = t; - } - while (Math.Abs(sum) < Math.Abs(factor*current)); - - return sum; - } - /// Evaluates the series of Chebyshev polynomials Ti at argument x/2. /// The series is given by ///
diff --git a/src/Numerics/SpecialFunctions/Stability.cs b/src/Numerics/SpecialFunctions/Stability.cs
index 3758e0f1..82f99971 100644
--- a/src/Numerics/SpecialFunctions/Stability.cs
+++ b/src/Numerics/SpecialFunctions/Stability.cs
@@ -56,7 +56,7 @@ namespace MathNet.Numerics
             // Series Expansion to x^k / k!
             int k = 0;
             double term = 1.0;
-            return Evaluate.Series(
+            return Series.Evaluate(
                 () =>
                 {
                     k++;