C# Примеры использования класса Extreme.Numerics
Подскажите пожалуйста, где можно посмотреть примеры использования Extreme.Numerics https://www.nuget.org/packages/Extreme.Numerics/
Мне нужно использовать его для расчета определенного интеграла от функции одной переменной и хотелось бы увидеть какие нибудь примеры, чтобы разобраться
Ответы (1 шт):
Автор решения: motpfofs
→ Ссылка
Вот пример Basic Integration QuickStart Sample (C#) доступный по этому адресу
using System;
using System.Collections.Generic;
using System.Text;
using System;
namespace Extreme.Numerics.QuickStart.CSharp
{
// The numerical integration classes reside in the
// Extreme.Mathematics.Calculus namespace.
using Extreme.Mathematics.Calculus;
// Function delegates reside in the Extreme.Mathematics
// namespace.
using Extreme.Mathematics;
using Extreme.Mathematics.Algorithms;
/// <summary>
/// Illustrates the basic use of the numerical integration
/// classes in the Extreme.Mathematics.Calculus namespace of the Extreme
/// Optimization Mathematics Library for .NET.
/// </summary>
class BasicIntegration
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
// Numerical integration algorithms fall into two
// main categories: adaptive and non-adaptive.
// This QuickStart Sample illustrates the use of
// the non-adaptive numerical integrators.
//
// All numerical integration classes derive from
// NumericalIntegrator. This abstract base class
// defines properties and methods that are shared
// by all numerical integration classes.
//
// The integrand
//
// The function we are integrating must be
// provided as a Func<double, double>. For more
// information about this delegate, see the
// FunctionDelegates QuickStart sample.
Func<double, double> f = Math.Sin;
// Variable to hold the result:
double result;
//
// SimpsonIntegrator
//
// The simplest numerical integration algorithm
// is Simpson's rule.
SimpsonIntegrator simpson = new SimpsonIntegrator();
// You can set the relative or absolute tolerance
// to which to evaluate the integral.
simpson.RelativeTolerance = 1e-5;
// You can select the type of tolerance using the
// ConvergenceCriterion property:
simpson.ConvergenceCriterion =
ConvergenceCriterion.WithinRelativeTolerance;
// The Integrate method performs the actual
// integration:
result = simpson.Integrate(f, 0, 2);
Console.WriteLine("sin(x) on [0,2]");
Console.WriteLine("Simpson integrator:");
// The result is also available in the Result
// property:
Console.WriteLine(" Value: {0}", simpson.Result);
// To see whether the algorithm ended normally,
// inspect the Status property:
Console.WriteLine(" Status: {0}", simpson.Status);
// You can find out the estimated error of the result
// through the EstimatedError property:
Console.WriteLine(" Estimated error: {0}", simpson.EstimatedError);
// The number of iterations to achieve the result
// is available through the IterationsNeeded property.
Console.WriteLine(" Iterations: {0}", simpson.IterationsNeeded);
// The number of function evaluations is available
// through the EvaluationsNeeded property.
Console.WriteLine(" Function evaluations: {0}", simpson.EvaluationsNeeded);
//
// Gauss-Kronrod Integration
//
// Gauss-Kronrod integrators also use a fixed point
// scheme, but with certain optimizations in the
// choice of points where the integrand is evaluated.
// The NonAdaptiveGaussKronrodIntegrator uses a
// succession of 10, 21, 43, and 87 point rules
// to approximate the integral.
NonAdaptiveGaussKronrodIntegrator nagk =
new NonAdaptiveGaussKronrodIntegrator();
nagk.Integrate(Math.Sin, 0, 2);
Console.WriteLine("Non-adaptive Gauss-Kronrod rule:");
Console.WriteLine(" Value: {0}", nagk.Result);
Console.WriteLine(" Status: {0}", nagk.Status);
Console.WriteLine(" Estimated error: {0}", nagk.EstimatedError);
Console.WriteLine(" Iterations: {0}", nagk.IterationsNeeded);
Console.WriteLine(" Function evaluations: {0}", nagk.EvaluationsNeeded);
//
// Romberg Integration
//
// Romberg integration combines Simpson's Rule
// with a scheme to accelerate convergence.
// This algorithm is useful for smooth integrands.
RombergIntegrator romberg = new RombergIntegrator();
result = romberg.Integrate(Math.Sin, 0, 2);
Console.WriteLine("Romberg integration:");
Console.WriteLine(" Value: {0}", romberg.Result);
Console.WriteLine(" Status: {0}", romberg.Status);
Console.WriteLine(" Estimated error: {0}", romberg.EstimatedError);
Console.WriteLine(" Iterations: {0}", romberg.IterationsNeeded);
Console.WriteLine(" Function evaluations: {0}", romberg.EvaluationsNeeded);
// However, it breaks down if the integration
// algorithm contains singularities or
// discontinuities.
//
// The AdaptiveIntegrator can handle this type
// of integrand, and many other difficult cases.
// See the AdvancedIntegration QuickStart sample
// for details.
result = romberg.Integrate(x => x <= 0.0 ? 0.0 : Math.Pow(x, -0.9) * Math.Log(1 / x),
0.0, 1.0);
Console.WriteLine("Romberg on hard integrand:");
Console.WriteLine(" Value: {0}", romberg.Result);
Console.WriteLine(" Actual value: 100");
Console.WriteLine(" Status: {0}", romberg.Status);
Console.WriteLine(" Estimated error: {0}", romberg.EstimatedError);
Console.WriteLine(" Iterations: {0}", romberg.IterationsNeeded);
Console.WriteLine(" Function evaluations: {0}", romberg.EvaluationsNeeded);
Console.Write("Press Enter key to exit...");
Console.ReadLine();
}
/// <summary>
/// Function that will cause difficulties to the
/// simplistic integration algorithms.
/// </summary>
private static double HardIntegrand(double x)
{
// This is put in because some integration rules
// evaluate the function at x=0.
if (x <= 0)
return 0;
return Math.Pow(x, -0.9) * Math.Log(1 / x);
}
}
}
А это пример Advanced Integration QuickStart Sample (C#):
using System;
namespace Extreme.Numerics.QuickStart.CSharp
{
// The numerical integration classes reside in the
// Extreme.Mathematics.Calculus namespace.
using Extreme.Mathematics.Calculus;
// Function delegates reside in the Extreme.Mathematics
// namespace.
using Extreme.Mathematics;
/// <summary>
/// Illustrates the more advanced use of the
/// AdaptiveGaussKronrodIntegrator numerical integrator class
/// classes in the Extreme.Mathematics.Calculus namespace of the Extreme
/// Optimization Numerical Libraries for .NET.
/// </summary>
class AdvancedIntegration
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
// Numerical integration algorithms fall into two
// main categories: adaptive and non-adaptive.
// This QuickStart Sample illustrates the use of
// the adaptive numerical integrator implemented by
// the AdaptiveIntegrator class. This class is the
// most advanced of the numerical integration
// classes.
//
// All numerical integration classes derive from
// NumericalIntegrator. This abstract base class
// defines properties and methods that are shared
// by all numerical integration classes.
//
// The integrand
//
// The function we are integrating must be
// provided as a Func<double, double>. For more
// information about this delegate, see the
// FunctionDelegates QuickStart sample.
//
// Variable to hold the result:
double result;
// Construct an instance of the integrator class:
AdaptiveIntegrator integrator = new AdaptiveIntegrator();
//
// Adaptive integrator basics
//
// All the properties and methods defined by the
// NumericalIntegrator base class are available.
// See the BasicIntegration QuickStart Sample
// for details. The AdaptiveIntegrator class defines
// the following additional properties:
//
// The IntegrationRule property gets or sets the
// integration rule that is to be used for
// integrating subintervals. It can be any
// object derived from IntegrationRule.
//
// For convenience, a series of Gauss-Kronrod
// integration rules of order 15, 21, 31, 41, 51,
// and 61 have been provided.
integrator.IntegrationRule = IntegrationRule.CreateGaussKronrod15PointRule();
// The UseAcceleration property specifies whether
// precautions should be taken for singularities
// in the integration interval.
integrator.UseExtrapolation = false;
// Finally, the Singularities property allows you
// to specify singularities or discontinuities
// inside the integration interval. See the
// sample below for details.
//
// Integration over infinite intervals
//
integrator.AbsoluteTolerance = 1e-8;
integrator.ConvergenceCriterion = ConvergenceCriterion.WithinAbsoluteTolerance;
// The Integrate method performs the actual
// integration. To integrate over an infinite
// interval, simply use either or both of
// double.PositiveInfinity and
// double.NegativeInfinity as bounds:
result = integrator.Integrate(x => Math.Exp(-x - x * x),
double.NegativeInfinity, double.PositiveInfinity);
Console.WriteLine("Exp(-x^2-x) on [-inf,inf]");
Console.WriteLine(" Value: {0}", integrator.Result);
Console.WriteLine(" Exact value: {0}", Math.Exp(0.25) * Constants.SqrtPi);
// To see whether the algorithm ended normally,
// inspect the Status property:
Console.WriteLine(" Status: {0}", integrator.Status);
Console.WriteLine(" Estimated error: {0}", integrator.EstimatedError);
Console.WriteLine(" Iterations: {0}", integrator.IterationsNeeded);
Console.WriteLine(" Function evaluations: {0}", integrator.EvaluationsNeeded);
// If you just want the result, you can also call the Integrate
// extension method directly on the integrand:
Func<double, double> integrand = x => Math.Exp(-x - x * x);
result = integrand.Integrate(double.NegativeInfinity, double.PositiveInfinity);
Console.WriteLine(" Value: {0}", result);
//
// Functions with singularities at the end points
// of the integration interval.
//
// Thanks to the adaptive nature of the algorithm,
// special measures can be taken to accelerate
// convergence near singularities. To enable this
// acceleration, set the Singularities property
// to true.
integrator.UseExtrapolation = true;
// We'll use the function that gives the Romberg
// integrator in the BasicIntegration QuickStart
// sample trouble.
result = integrator.Integrate(x => Math.Pow(x, -0.9) * Math.Log(1 / x), 0.0, 1.0);
Console.WriteLine("Singularities on boundary:");
Console.WriteLine(" Value: {0}", integrator.Result);
Console.WriteLine(" Exact value: 100");
Console.WriteLine(" Status: {0}",
integrator.Status);
Console.WriteLine(" Estimated error: {0}",
integrator.EstimatedError);
// Where Romberg integration failed after 1,000,000
// function evaluations, we find the correct answer
// to within tolerance using only 135 function
// evaluations!
Console.WriteLine(" Iterations: {0}",
integrator.IterationsNeeded);
Console.WriteLine(" Function evaluations: {0}",
integrator.EvaluationsNeeded);
//
// Functions with singularities or discontinuities
// inside the interval.
//
integrator.UseExtrapolation = true;
// We will pass an array containing the interior
// singularities to the integrator through the
// Singularities property:
integrator.SetSingularities(1, Math.Sqrt(2));
integrator.Integrate(x => x * x * x * Math.Log(Math.Abs((x * x - 1) * (x * x - 2))),
0.0, 3.0);
Console.WriteLine("Singularities inside the interval:");
Console.WriteLine(" Value: {0}", integrator.Result);
Console.WriteLine(" Exact value: 52.740748383471444998");
Console.WriteLine(" Status: {0}",
integrator.Status);
Console.WriteLine(" Estimated error: {0}",
integrator.EstimatedError);
Console.WriteLine(" Iterations: {0}",
integrator.IterationsNeeded);
Console.WriteLine(" Function evaluations: {0}",
integrator.EvaluationsNeeded);
Console.Write("Press Enter key to exit...");
Console.ReadLine();
}
}
}
доступный по этому адресу
Обе страницы достал загуглив название сайта и слово Integration выбрав в результатах поиска "Сохранённая копия".