integration API

integration 包在有限区间上对 (Double) -> Double 型函数求积分,并返回 Estimate[Double]。原有的积分器位于 legacy/integration。

gauss_kronrod_21 : (function: (Double) -> Double, lower: Double, upper: Double) -> (Double, Double, Int)

描述: 在 [lower, upper] 上应用一次 21 点 Gauss–Kronrod 求积公式(内嵌 10 点 Gauss 公式)。

返回值:

元组 (value, error, evaluations):Kronrod 值、Kronrod 值与 Gauss 值之差的绝对值(经验误差估计),以及 21。

adaptive_gauss_kronrod : (function: (Double) -> Double, lower: Double, upper: Double, absolute_tolerance?: Double, relative_tolerance?: Double, max_intervals?: Int) -> Estimate[Double]

描述: 反复二分误差估计最大的子区间并对其应用 gauss_kronrod_21,直到总误差估计不超过 max(absolute_tolerance, relative_tolerance * |value|)。

参数:

  • absolute_tolerance、relative_tolerance — 默认值均为 1.0e-10,且不得为负。
  • max_intervals — 子区间的最大数目。默认为 256;必须为正。

返回值:

若满足容差(包括在最后一次允许的细分时才满足),status 为 Converged,否则为 MaxIterations。当 lower == upper、容差为负或 max_intervals 不为正时,返回 InvalidInput,且不进行任何求值。上下限颠倒时得到积分的相反数。iterations 是二分次数。

示例:

let result = @integration.adaptive_gauss_kronrod(fn(x) { x * x }, 0.0, 1.0)
// result.value is close to 1/3

tanh_sinh : (function: (Double) -> Double, lower: Double, upper: Double, step?: Double, tolerance?: Double, max_terms?: Int) -> Estimate[Double]

描述: 使用 Takahasi 和 Mori 的 tanh–sinh(双指数)公式求积分。该公式的节点向端点作双指数聚集,因此能处理可积的端点奇异性,例如 0 处的 1/sqrt(x)。

参数:

  • step — 变换变量中的间距。默认为 0.125;必须为正。
  • tolerance — 当最新一项的绝对值乘以 step 不超过该值时停止。默认为 1.0e-12;不得为负。
  • max_terms — 中点每一侧的最大项数。默认为 64;必须为正。

返回值:

当满足容差,或节点在浮点数意义下到达端点时,status 为 Converged;用完 max_terms 时为 MaxIterations。estimated_error 是最后一项的绝对值,是启发式指标,而不是误差界。靠近端点的节点按 midpoint ± half_length * tanh(…) 计算,因此它们与端点的距离受双精度限制。

示例:

let result = @integration.tanh_sinh(fn(x) { 1.0 / x.sqrt() }, 0.0, 1.0)
// result.value is close to 2