Class NumericalIntegration

java.lang.Object
cloud.opencode.base.math.integration.NumericalIntegration

public final class NumericalIntegration extends Object
Numerical integration utility class providing common quadrature algorithms. 数值积分工具类,提供常见求积算法。

All methods are stateless and thread-safe. Supported algorithms:

所有方法无状态且线程安全。支持的算法:

  • Composite trapezoidal rule — 复合梯形法
  • Composite Simpson's 1/3 rule — 复合辛普森 1/3 法
  • Simpson's 3/8 rule — 辛普森 3/8 法
  • Romberg integration — 龙贝格积分
  • Gauss-Legendre quadrature (2–5 points) — 高斯-勒让德求积(2–5 点)
Since:
JDK 25, opencode-base-math V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • trapezoid

      public static double trapezoid(DoubleUnaryOperator f, double a, double b, int n)
      Composite trapezoidal rule. 复合梯形法。

      Approximates the definite integral of f over [a, b] using n equal subintervals. If a > b, the result is negated (orientation convention).

      使用 n 个等距子区间近似 f 在 [a, b] 上的定积分。 若 a > b,结果取反(方向约定)。

      Parameters:
      f - the integrand — 被积函数
      a - the lower bound — 积分下限
      b - the upper bound — 积分上限
      n - the number of subintervals (≥ 1) — 子区间数(≥ 1)
      Returns:
      the approximate integral value — 近似积分值
      Throws:
      MathException - if inputs are invalid — 输入无效时抛出
    • simpson

      public static double simpson(DoubleUnaryOperator f, double a, double b, int n)
      Composite Simpson's 1/3 rule. 复合辛普森 1/3 法。

      Approximates the definite integral using n subintervals. n must be even. Exact for polynomials up to degree 3.

      使用 n 个子区间近似定积分。n 必须为偶数。对 3 阶及以下多项式精确。

      Parameters:
      f - the integrand — 被积函数
      a - the lower bound — 积分下限
      b - the upper bound — 积分上限
      n - the number of subintervals (must be even, ≥ 2) — 子区间数(必须为偶数,≥ 2)
      Returns:
      the approximate integral value — 近似积分值
      Throws:
      MathException - if inputs are invalid — 输入无效时抛出
    • simpsonThreeEighths

      public static double simpsonThreeEighths(DoubleUnaryOperator f, double a, double b, int n)
      Simpson's 3/8 rule (composite). 辛普森 3/8 法(复合)。

      Approximates the definite integral using n subintervals. n must be divisible by 3.

      使用 n 个子区间近似定积分。n 必须能被 3 整除。

      Parameters:
      f - the integrand — 被积函数
      a - the lower bound — 积分下限
      b - the upper bound — 积分上限
      n - the number of subintervals (must be divisible by 3, ≥ 3) — 子区间数(必须能被 3 整除,≥ 3)
      Returns:
      the approximate integral value — 近似积分值
      Throws:
      MathException - if inputs are invalid — 输入无效时抛出
    • romberg

      public static double romberg(DoubleUnaryOperator f, double a, double b, int maxIterations, double tolerance)
      Romberg integration using Richardson extrapolation. 基于理查森外推的龙贝格积分。

      Iteratively refines the trapezoidal rule estimate using Richardson extrapolation until the desired tolerance is achieved or maxIterations is reached.

      通过理查森外推迭代改进梯形法估计,直到达到所需精度或最大迭代次数。

      Parameters:
      f - the integrand — 被积函数
      a - the lower bound — 积分下限
      b - the upper bound — 积分上限
      maxIterations - maximum number of Romberg iterations (≥ 1) — 最大迭代次数(≥ 1)
      tolerance - convergence tolerance (> 0) — 收敛容差(> 0)
      Returns:
      the approximate integral value — 近似积分值
      Throws:
      MathException - if inputs are invalid — 输入无效时抛出
    • gaussLegendre

      public static double gaussLegendre(DoubleUnaryOperator f, double a, double b, int points)
      Gauss-Legendre quadrature. 高斯-勒让德求积。

      Uses pre-computed nodes and weights for 2, 3, 4, or 5 point Gauss-Legendre quadrature on the interval [a, b]. A 2n-point rule is exact for polynomials up to degree 2n-1.

      使用预计算的 2、3、4 或 5 点高斯-勒让德求积节点和权重在 [a, b] 上积分。 n 点规则对 2n-1 阶及以下多项式精确。

      Parameters:
      f - the integrand — 被积函数
      a - the lower bound — 积分下限
      b - the upper bound — 积分上限
      points - number of quadrature points (2, 3, 4, or 5) — 求积点数(2、3、4 或 5)
      Returns:
      the approximate integral value — 近似积分值
      Throws:
      MathException - if inputs are invalid — 输入无效时抛出