CommonMath
目录
The Apache Commons Math project is a library of lightweight, self-contained mathematics and statistics components addressing the most common practical problems not immediately available in the Java programming language or commons-lang. 官方案例文档
- org.apache.commons.math4.stat - statistics, statistical tests
- org.apache.commons.math4.analysis - rootfinding, integration, interpolation, polynomials
- org.apache.commons.math4.random - random numbers, strings and data generation
- org.apache.commons.math4.special - special functions (Gamma, Beta)
- org.apache.commons.math4.linear - matrices, solving linear systems
- org.apache.commons.math4.util - common math/stat functions extending java.lang.Math
- org.apache.commons.math4.complex - complex numbers
- org.apache.commons.math4.distribution - probability distributions
- org.apache.commons.math4.fraction - rational numbers
- org.apache.commons.math4.transform - transform methods (Fast Fourier)
- org.apache.commons.math4.geometry - geometry (Euclidean spaces and Binary Space Partitioning)
- org.apache.commons.math4.optim - function maximization or minimization
- org.apache.commons.math4.ode - Ordinary Differential Equations integration
- org.apache.commons.math4.genetics - Genetic Algorithms
- org.apache.commons.math4.fitting - Curve Fitting
- org.apache.commons.math4.ml - Machine Learning
- Computing
means, variances
and othersummary statistics
for a list of numbers- Fitting a line to a set of data points using
linear regression
- Fitting a
curve
to a set of data points- Finding a
smooth curve
that passes through a collection of points (interpolation
)- Fitting a
parametric model
to a set of measurements using least-squares methods- Solving equations involving real-valued functions (i.e. root-finding)
- Solving systems of linear equations
- Solving Ordinary Differential Equations
- Minimizing multi-dimensional functions
Generating random numbers
with more restrictions (e.g distribution, range) than what is possible using the JDK- Generating random samples and/or datasets that are “like” the data in an input file
- Performing statistical significance tests
- Miscellaneous mathematical functions such as factorials, binomial coefficients and “special functions” (e.g. gamma, beta functions)
1. 多项式拟合
double[] x = new double[]{1, 2, 3, 4, 5};
double[] y = new double[]{19,33,53,79,111};
WeightedObservedPoints points = new WeightedObservedPoints();
for(int i = 0; i < x.length; i++) { //把数据点加入观察的序列
points.add(x[i], y[i]);
}
PolynomialCurveFitter fitter = PolynomialCurveFitter.create(2); //指定多项式阶数
double[] result = fitter.fit(points.toList()); // 曲线拟合,结果保存于数组[c,b,a] a*x*x+b*x+c=y
for(int i = 0; i < result.length; i++) {
System.out.println(result[i]);
}
/**
* 计算指定对象的运行时间开销。
*
* @param curveFitting 指定被测对象。
* @return 返回sub.run的时间开销,单位为s。
* @throws Exception
*/
public double calcTimeCost(CurveFitting curveFitting) throws Exception {
List<Object> params = curveFitting.getParams();
long startTime = System.nanoTime();
Object result = curveFitting.run(params);
long stopTime = System.nanoTime();
curveFitting.printResult(result);
System.out.println("start: " + startTime + " / stop: " + stopTime);
return 1.0e-9 * (stopTime - startTime);
}