Browse Source

Expose previously internal Series.Evaluate

dependabot/nuget/NUnit3TestAdapter-4.2.0
Christoph Ruegg 5 years ago
parent
commit
c02b01b23d
  1. 2
      build.fsx
  2. 106
      src/Numerics/Series.cs
  3. 31
      src/Numerics/SpecialFunctions/Evaluate.cs
  4. 2
      src/Numerics/SpecialFunctions/Stability.cs

2
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"

106
src/Numerics/Series.cs

@ -0,0 +1,106 @@
// <copyright file="Series.cs" company="Math.NET">
// 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.
// </copyright>
using System;
using System.Collections.Generic;
namespace MathNet.Numerics
{
public static class Series
{
/// <summary>
/// Numerically stable series summation (stops automatically).
/// </summary>
/// <param name="nextSummand">provides the summands sequentially</param>
/// <returns>Sum</returns>
public static double Evaluate(Func<double> 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;
}
/// <summary>
/// Numerically stable series summation (stops automatically).
/// </summary>
/// <param name="infiniteSummands">provides the summands sequentially</param>
/// <returns>Sum</returns>
public static double Evaluate(IEnumerable<double> 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;
}
}
}

31
src/Numerics/SpecialFunctions/Evaluate.cs

@ -52,37 +52,8 @@ namespace MathNet.Numerics
/// <summary>
/// Evaluation functions, useful for function approximation.
/// </summary>
public static class Evaluate
internal static class Evaluate
{
/// <summary>
/// Numerically stable series summation
/// </summary>
/// <param name="nextSummand">provides the summands sequentially</param>
/// <returns>Sum</returns>
internal static double Series(Func<double> 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;
}
/// <summary> Evaluates the series of Chebyshev polynomials Ti at argument x/2.
/// The series is given by
/// <pre>

2
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++;

Loading…
Cancel
Save