Class OpenMathLib

java.lang.Object
cloud.opencode.base.math.OpenMathLib

public final class OpenMathLib extends Object
OpenMathLib - Unified facade for the opencode-base-math module OpenMathLib - opencode-base-math 模块统一门面

Provides convenient static access to the most frequently used mathematical functions from all sub-packages. For advanced or less common operations, use the individual classes directly (e.g., Statistics, Matrix, Combinatorics).

提供对所有子包中最常用数学函数的便捷静态访问。对于高级或不常见的操作, 请直接使用各个类(如 StatisticsMatrixCombinatorics)。

Features | 主要功能:

  • Statistics: percentile, correlation, regression, streaming - 统计: 百分位数、相关、回归、流式
  • Linear Algebra: vector/matrix creation - 线性代数: 向量/矩阵创建
  • Analysis: root finding, differentiation, integration, interpolation - 分析: 求根、微分、积分、插值
  • Combinatorics: binomial, permutation - 组合数学: 二项式、排列
  • Distributions: normal, t, chi-squared, etc. - 概率分布: 正态、t、卡方等
  • Special functions: gamma, beta, erf - 特殊函数: gamma、beta、erf

Usage Examples | 使用示例:

// One-stop import
import cloud.opencode.base.math.OpenMathLib;

// Statistics
double median = OpenMathLib.percentile(data, 50);
double r = OpenMathLib.correlation(x, y);

// Root finding
double sqrt2 = OpenMathLib.findRoot(x -> x * x - 2, 0, 2);

// Integration
double area = OpenMathLib.integrate(Math::sin, 0, Math.PI);

// Combinatorics
long c = OpenMathLib.binomial(20, 10);

// Special functions
double g = OpenMathLib.gamma(5.0);

Security | 安全性:

  • Thread-safe: Yes (stateless) - 线程安全: 是(无状态)
  • Null-safe: Yes - 空值安全: 是
Since:
JDK 25, opencode-base-math V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • percentile

      public static double percentile(double[] data, double p)
      Computes the p-th percentile of the data. 计算数据的第 p 百分位数
      Parameters:
      data - the data array / 数据数组
      p - the percentile in [0, 100] / 百分位数 [0, 100]
      Returns:
      the percentile value / 百分位数值
    • correlation

      public static double correlation(double[] x, double[] y)
      Computes the Pearson correlation coefficient. 计算 Pearson 相关系数
      Parameters:
      x - first array / 第一个数组
      y - second array / 第二个数组
      Returns:
      correlation in [-1, 1] / 相关系数 [-1, 1]
    • covariance

      public static double covariance(double[] x, double[] y)
      Computes the sample covariance. 计算样本协方差
      Parameters:
      x - first array / 第一个数组
      y - second array / 第二个数组
      Returns:
      the covariance / 协方差
    • spearmanCorrelation

      public static double spearmanCorrelation(double[] x, double[] y)
      Computes Spearman's rank correlation coefficient. 计算 Spearman 等级相关系数
      Parameters:
      x - first array / 第一个数组
      y - second array / 第二个数组
      Returns:
      Spearman's rho in [-1, 1] / Spearman rho [-1, 1]
    • linearRegression

      public static Regression.LinearModel linearRegression(double[] x, double[] y)
      Performs simple linear regression. 执行简单线性回归
      Parameters:
      x - independent variable / 自变量
      y - dependent variable / 因变量
      Returns:
      the linear model / 线性模型
    • streamingStats

      public static StreamingStatistics streamingStats()
      Creates a new streaming statistics accumulator. 创建新的流式统计累加器
      Returns:
      a new empty accumulator / 新的空累加器
    • tTestOneSample

      public static TestResult tTestOneSample(double[] data, double mu0)
      Performs a one-sample t-test. 执行单样本 t 检验
      Parameters:
      data - the sample data / 样本数据
      mu0 - the hypothesized mean / 假设均值
      Returns:
      the test result / 检验结果
    • tTestTwoSample

      public static TestResult tTestTwoSample(double[] x, double[] y)
      Performs Welch's two-sample t-test. 执行 Welch 双样本 t 检验
      Parameters:
      x - first sample / 第一个样本
      y - second sample / 第二个样本
      Returns:
      the test result / 检验结果
    • vector

      public static Vector vector(double... components)
      Creates a vector from components. 从分量创建向量
      Parameters:
      components - the components / 分量
      Returns:
      a new vector / 新向量
    • matrix

      public static Matrix matrix(double[][] data)
      Creates a matrix from a 2D array. 从二维数组创建矩阵
      Parameters:
      data - the matrix data / 矩阵数据
      Returns:
      a new matrix / 新矩阵
    • identityMatrix

      public static Matrix identityMatrix(int n)
      Creates an identity matrix of size n. 创建 n 阶单位矩阵
      Parameters:
      n - the size / 阶数
      Returns:
      the identity matrix / 单位矩阵
    • findRoot

      public static double findRoot(DoubleUnaryOperator f, double a, double b)
      Finds a root of f in [a, b] using Brent's method (recommended). 使用 Brent 法在 [a, b] 内求 f 的根(推荐)
      Parameters:
      f - the function / 函数
      a - left endpoint / 左端点
      b - right endpoint / 右端点
      Returns:
      the root / 根
    • findRoot

      public static double findRoot(DoubleUnaryOperator f, double a, double b, double tolerance)
      Finds a root of f in [a, b] with specified tolerance. 使用指定容差在 [a, b] 内求 f 的根
      Parameters:
      f - the function / 函数
      a - left endpoint / 左端点
      b - right endpoint / 右端点
      tolerance - convergence tolerance / 收敛容差
      Returns:
      the root / 根
    • derivative

      public static double derivative(DoubleUnaryOperator f, double x)
      Computes the numerical derivative of f at x. 计算 f 在 x 处的数值导数
      Parameters:
      f - the function / 函数
      x - the point / 点
      Returns:
      the approximate derivative / 近似导数
    • integrate

      public static double integrate(DoubleUnaryOperator f, double a, double b)
      Numerically integrates f from a to b using Simpson's rule with n=1000. 使用 Simpson 法则对 f 从 a 到 b 数值积分(n=1000)
      Parameters:
      f - the integrand / 被积函数
      a - lower bound / 下限
      b - upper bound / 上限
      Returns:
      the approximate integral / 近似积分值
    • interpolate

      public static double interpolate(double[] x, double[] y, double xi)
      Interpolates using piecewise linear interpolation. 分段线性插值
      Parameters:
      x - the x data points (sorted ascending) / x 数据点(升序)
      y - the y data points / y 数据点
      xi - the query point / 查询点
      Returns:
      the interpolated value / 插值结果
    • binomial

      public static long binomial(int n, int k)
      Computes the binomial coefficient C(n, k). 计算二项式系数 C(n, k)
      Parameters:
      n - total items / 总数
      k - items to choose / 选取数
      Returns:
      C(n, k) / 二项式系数
    • permutation

      public static long permutation(int n, int k)
      Computes the permutation P(n, k). 计算排列数 P(n, k)
      Parameters:
      n - total items / 总数
      k - items to arrange / 排列数
      Returns:
      P(n, k) / 排列数
    • standardNormal

      public static NormalDistribution standardNormal()
      Returns the standard normal distribution N(0, 1). 返回标准正态分布 N(0, 1)
      Returns:
      the standard normal / 标准正态分布
    • gamma

      public static double gamma(double x)
      Computes the gamma function. 计算 Gamma 函数
      Parameters:
      x - the input / 输入值
      Returns:
      Gamma(x) / Gamma 函数值
    • erf

      public static double erf(double x)
      Computes the error function. 计算误差函数
      Parameters:
      x - the input / 输入值
      Returns:
      erf(x) / 误差函数值
    • beta

      public static double beta(double a, double b)
      Computes the beta function. 计算 Beta 函数
      Parameters:
      a - first parameter / 第一个参数
      b - second parameter / 第二个参数
      Returns:
      Beta(a, b) / Beta 函数值