Class RootFinder

java.lang.Object
cloud.opencode.base.math.analysis.RootFinder

public final class RootFinder extends Object
Numerical root-finding algorithms for single-variable real functions. 单变量实函数的数值求根算法

Provides static methods for bisection, Brent's method, Newton-Raphson, and the secant method. All methods are stateless and thread-safe.

提供二分法、Brent 法、牛顿-拉弗森法和割线法的静态方法。 所有方法无状态且线程安全。

Since:
JDK 25, opencode-base-math V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • bisection

      public static double bisection(DoubleUnaryOperator f, double a, double b, double tolerance)
      Finds a root of f in the interval [a, b] using the bisection method. 使用二分法在区间 [a, b] 内查找函数 f 的根

      Requires f(a) and f(b) to have opposite signs (or one is zero).

      要求 f(a) 与 f(b) 异号(或其中一个为零)。

      Parameters:
      f - the function / 函数
      a - left endpoint of the interval / 区间左端点
      b - right endpoint of the interval / 区间右端点
      tolerance - convergence tolerance on |b - a| / 收敛容差,基于 |b - a|
      Returns:
      an approximate root / 近似根
      Throws:
      MathException - if inputs are invalid or the method does not converge
    • brent

      public static double brent(DoubleUnaryOperator f, double a, double b, double tolerance)
      Finds a root of f in the interval [a, b] using Brent's method. 使用 Brent 法在区间 [a, b] 内查找函数 f 的根

      Combines bisection, secant, and inverse quadratic interpolation for robust and fast convergence. This is the recommended general-purpose root finder.

      结合二分法、割线法和逆二次插值以实现稳健且快速的收敛。 这是推荐的通用求根方法。

      Parameters:
      f - the function / 函数
      a - left endpoint of the interval / 区间左端点
      b - right endpoint of the interval / 区间右端点
      tolerance - convergence tolerance / 收敛容差
      Returns:
      an approximate root / 近似根
      Throws:
      MathException - if inputs are invalid or the method does not converge
    • newton

      public static double newton(DoubleUnaryOperator f, DoubleUnaryOperator df, double x0, double tolerance)
      Finds a root of f using the Newton-Raphson method. 使用牛顿-拉弗森法查找函数 f 的根

      Requires both the function and its derivative. Convergence is quadratic near simple roots but may diverge for poor initial guesses.

      需要提供函数及其导数。在简单根附近为二次收敛, 但初始猜测不佳时可能发散。

      Parameters:
      f - the function / 函数
      df - the derivative of f / f 的导数
      x0 - initial guess / 初始猜测值
      tolerance - convergence tolerance on |f(x)| / 收敛容差,基于 |f(x)|
      Returns:
      an approximate root / 近似根
      Throws:
      MathException - if the derivative is too close to zero or the method does not converge
    • secant

      public static double secant(DoubleUnaryOperator f, double x0, double x1, double tolerance)
      Finds a root of f using the secant method. 使用割线法查找函数 f 的根

      Similar to Newton's method but approximates the derivative using two function evaluations. Does not require an explicit derivative.

      类似牛顿法,但使用两个函数值近似导数。无需提供显式导数。

      Parameters:
      f - the function / 函数
      x0 - first initial point / 第一个初始点
      x1 - second initial point / 第二个初始点
      tolerance - convergence tolerance on |f(x)| / 收敛容差,基于 |f(x)|
      Returns:
      an approximate root / 近似根
      Throws:
      MathException - if the method does not converge