Class NumericalMethods
- Namespace
- Utils.Mathematics
- Assembly
- Utils.Mathematics.dll
Numerical integration and interpolation utilities.
public static class NumericalMethods
- Inheritance
-
NumericalMethods
- Inherited Members
Methods
Integrate<T>(Func<T, T>, T, T, int)
Numerically integrates f over [a, b]
using the composite Simpson's 1/3 rule.
public static T Integrate<T>(Func<T, T> f, T a, T b, int steps = 1000) where T : struct, IFloatingPoint<T>
Parameters
fFunc<T, T>Integrand function. Must be defined and finite on [a, b].
aTLower bound of integration.
bTUpper bound of integration.
stepsintNumber of sub-intervals. Must be positive and even; an odd value is rounded up by one. Higher values increase accuracy at the cost of more function evaluations.
Returns
- T
The approximate value of ∫f(x)dx from a to b. If
freturns a non-finite value anywhere in the interval, that non-finite value propagates into the result (visible to the caller as NaN or an infinity) rather than being silently discarded.
Type Parameters
TFloating-point scalar type.
Exceptions
- ArgumentNullException
Thrown when
fis null.- ArgumentOutOfRangeException
Thrown when
stepsis not positive, whenstepsis the odd value MaxValue (which cannot be rounded up to an even count without overflowing), or whena/bis not finite.
Lagrange<T>(IEnumerable<(T x, T y)>, T)
Evaluates the Lagrange interpolating polynomial at at,
given a set of data points.
public static T Lagrange<T>(IEnumerable<(T x, T y)> points, T at) where T : struct, IFloatingPoint<T>
Parameters
pointsIEnumerable<(T x, T y)>Sequence of (x, y) data points. All x values must be distinct and every coordinate finite.
atTThe point at which to evaluate the interpolating polynomial. Must be finite.
Returns
- T
The interpolated value at
at.
Type Parameters
TFloating-point scalar type.
Remarks
The result is exact at each data point and provides a polynomial approximation elsewhere. For large point sets, consider alternative methods as Lagrange interpolation can exhibit Runge's phenomenon near the boundaries.
Exceptions
- ArgumentException
Thrown when no points are provided, a coordinate is not finite, or x values are not distinct.
- ArgumentOutOfRangeException
Thrown when
atis not finite.