1. Overview
1.概述
We’re frequently in need of using mathematical tools, and sometimes java.lang.Math is simply not enough. Fortunately, Apache Commons has the goal of filling in the leaks of the standard library, with Apache Commons Math.
我们经常需要使用数学工具,而有时java.lang.Math根本不够用。幸运的是,Apache Commons的目标是填补标准库的漏洞,有Apache Commons Math。
Apache Commons Math is the biggest open-source library of mathematical functions and utilities for Java. Given that this article is just an introduction, we will just give an overview of the library and present the most compelling use cases.
Apache Commons Math是最大的Java数学函数和工具的开源库。鉴于本文只是一个介绍,我们将只对该库进行概述,并介绍最引人注目的使用案例。
2. Starting with Apache Commons Math
2.从Apache Commons Math开始
2.1. The Usages of Apache Commons Math
2.1.阿帕奇共享数学的使用
Apache Commons Math consists of mathematical functions (erf for instance), structures representing mathematical concepts (like complex numbers, polynomials, vectors, etc.), and algorithms that we can apply to these structures (root finding, optimization, curve fitting, computation of intersections of geometrical figures, etc.).
Apache Commons Math由数学函数(例如erf)、代表数学概念的结构(如复数、多项式、向量等)以及我们可以应用于这些结构的算法(寻根、优化、曲线拟合、计算几何图形的交叉点等)组成。
2.2. Maven Configuration
2.2.Maven配置</b
If you’re using Maven, simply add this dependency:
如果您使用的是Maven,只需添加这个依赖性。
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
<version>3.6.1</version>
</dependency>
2.3. Package Overview
2.3.包装概述
Apache Commons Math is divided into several packages:
Apache Commons Math被分为几个包。
- org.apache.commons.math3.stat – statistics and statistical tests
- org.apache.commons.math3.distribution – probability distributions
- org.apache.commons.math3.random – random numbers, strings and data generation
- org.apache.commons.math3.analysis – root finding, integration, interpolation, polynomials, etc.
- org.apache.commons.math3.linear – matrices, solving linear systems
- org.apache.commons.math3.geometry – geometry (Euclidean spaces and binary space partitioning)
- org.apache.commons.math3.transform – transform methods (fast Fourier)
- org.apache.commons.math3.ode – ordinary differential equations integration
- org.apache.commons.math3.fitting – curve fitting
- org.apache.commons.math3.optim – function maximization or minimization
- org.apache.commons.math3.genetics – genetic algorithms
- org.apache.commons.math3.ml – machine learning (clustering and neural networks)
- org.apache.commons.math3.util – common math/stat functions extending java.lang.Math
- org.apache.commons.math3.special – special functions (Gamma, Beta)
- org.apache.commons.math3.complex – complex numbers
- org.apache.commons.math3.fraction – rational numbers
3. Statistics, Probabilities, and Randomness
3.统计学、概率和随机性
3.1. Statistics
3.1.统计数据
The package org.apache.commons.math3.stat provides several tools for statistical computations. For example, to compute mean, standard deviation, and many more, we can use DescriptiveStatistics:
包org.apache.commons.math3.stat提供了几个统计计算的工具。例如,为了计算平均值、标准差等,我们可以使用DescriptiveStatistics。
double[] values = new double[] {65, 51 , 16, 11 , 6519, 191 ,0 , 98, 19854, 1, 32};
DescriptiveStatistics descriptiveStatistics = new DescriptiveStatistics();
for (double v : values) {
descriptiveStatistics.addValue(v);
}
double mean = descriptiveStatistics.getMean();
double median = descriptiveStatistics.getPercentile(50);
double standardDeviation = descriptiveStatistics.getStandardDeviation();
In this package, we can find tools for computing the covariance, correlation, or to perform statistical tests (using TestUtils).
在这个软件包中,我们可以找到计算协方差、相关的工具,或者进行统计测试(使用TestUtils)。
3.2. Probabilities and Distributions
3.2.概率和分布</b
In core Java, Math.random() can be used for generating random values, but these values are uniformly distributed between 0 and 1.
在核心Java中,Math.random()可用于生成随机值,但这些值是在0和1之间均匀分布。
Sometimes, we want to produce a random value using a more complex distribution. For this, we can use the framework provided by org.apache.commons.math3.distribution.
有时,我们想用更复杂的分布来产生一个随机值。为此,我们可以使用org.apache.commons.math3.distribution提供的框架。
Here is how to generate random values according to the normal distribution with the mean of 10 and the standard deviation of 3:
下面是如何根据正态分布产生随机值,其平均值为10,标准差为3。
NormalDistribution normalDistribution = new NormalDistribution(10, 3);
double randomValue = normalDistribution.sample();
Or we can obtain the probability P(X = x) of getting a value for discrete distributions, or the cumulative probability P(X <= x) for continuous distributions.
或者,对于离散分布,我们可以得到得到一个值的概率P(X=x),对于连续分布,可以得到累积概率P(X<=x)。
4. Analysis
4.分析
Analysis related functions and algorithms can be found in org.apache.commons.math3.analysis.
与分析有关的函数和算法可以在org.apache.commons.math3.analysis中找到。
4.1. Root Finding
4.1.寻根
A root is a value where a function has the value of 0. Commons-Math includes implementation of several root-finding algorithms.
根是一个函数值为0的数值。Commons-Math包括几个寻根算法的实现。
Here, we try to find the root of v -> (v * v) – 2 :
这里,我们试图找到v -> (v * v) – 2的根。
UnivariateFunction function = v -> Math.pow(v, 2) - 2;
UnivariateSolver solver = new BracketingNthOrderBrentSolver(1.0e-12, 1.0e-8, 5);
double c = solver.solve(100, function, -10.0, 10.0, 0);
First, we start by defining the function, then we define the solver, and we set the desired accuracy. Finally, we call the solve() API.
首先,我们从定义函数开始,然后定义求解器,并设置所需的精度。最后,我们调用solve() API。
The root-finding operation will be performed using several iterations, so it’s a matter of finding a compromise between execution time and accuracy.
寻根操作将采用多次迭代的方式进行,所以要在执行时间和准确性之间找到一个折中点。
4.2. Calculating Integrals
4.2.计算积分
The integration works almost like root finding:
这种整合的工作方式几乎与找根一样。
UnivariateFunction function = v -> v;
UnivariateIntegrator integrator = new SimpsonIntegrator(1.0e-12, 1.0e-8, 1, 32);
double i = integrator.integrate(100, function, 0, 10);
We start by defining a function, we choose an integrator among the available integration solutions existing, we set the desired accuracy, and finally, we integrate.
我们首先定义一个函数,在现有的可用的积分方案中选择一个积分器,设置所需的精度,最后进行积分。
5. Linear Algebra
5.线性代数</b
If we have a linear system of equations under the form AX = B where A is a matrix of real numbers, and B a vector of real numbers – Commons Math provides structures to represent both the matrix and the vector, and also provide solvers to find the value of X:
如果我们有一个AX=B形式的线性方程组,其中A是一个实数矩阵,B是一个实数向量–Commons Math提供了表示矩阵和向量的结构,也提供了寻找X值的求解器。
RealMatrix a = new Array2DRowRealMatrix(
new double[][] { { 2, 3, -2 }, { -1, 7, 6 }, { 4, -3, -5 } },
false);
RealVector b = new ArrayRealVector(n
ew double[] { 1, -2, 1 },
false);
DecompositionSolver solver = new LUDecomposition(a).getSolver();
RealVector solution = solver.solve(b);
The case is pretty straightforward: we define a matrix a from an array of array of doubles, and a vector b from an array of a vector.
这种情况非常简单:我们从一个双数数组中定义一个矩阵a,从一个向量数组中定义一个向量b。
Then, we create an LUDecomposition which provides a solver for equations under the form AX = B. As its name states it, LUDecomposition relies on the LU decomposition, and thus works only with square matrices.
然后,我们创建一个LUDecomposition,为AX=B形式的方程提供一个解算器。正如它的名字所说,LUDecomposition依赖于LU分解,因此只对方形矩阵工作。
For other matrices, different solvers exist, usually solving the equation using the least square method.
对于其他矩阵,存在不同的求解器,通常使用最小平方法求解方程。
6. Geometry
6.几何学
The package org.apache.commons.math3.geometry provides several classes for representing geometrical objects and several tools to manipulate them. It is important to note that this package is divided into different sub-packages, regarding of the kind of geometry we want to use:
包org.apache.commons.math3.geometry提供了几个用于表示几何对象的类和一些操作它们的工具。值得注意的是,这个包根据我们想要使用的几何图形的种类分为不同的子包。
It is important to note that this package is divided into different sub-packages, regarding of the kind of geometry we want to use:
值得注意的是,这个包被分为不同的子包,这与我们想使用的几何图形的种类有关。
- org.apache.commons.math3.geometry.euclidean.oned – 1D Euclidean geometry
- org.apache.commons.math3.geometry.euclidean.twod – 2D Euclidean geometry
- org.apache.commons.math3.geometry.euclidean.threed – 3D Euclidean geometry
- org.apache.commons.math3.geometry.spherical.oned – 1D spherical geometry
- org.apache.commons.math3.geometry.spherical.twod – 2D spherical geometry
The most useful classes are probably Vector2D, Vector3D, Line, and Segment. They are used for representing 2D vectors (or points), 3D vectors, lines, and segments respectively.
最有用的类可能是Vector2D, Vector3D, Line, 和Segment。它们分别用于表示二维向量(或点)、三维向量、线和段。
When using classes mentioned above, it is possible to perform some computation. For instance, the following code performs the calculation of the intersection of two 2D lines:
在使用上述的类时,可以进行一些计算。例如,下面的代码执行了两个二维线的交点的计算。
Line l1 = new Line(new Vector2D(0, 0), new Vector2D(1, 1), 0);
Line l2 = new Line(new Vector2D(0, 1), new Vector2D(1, 1.5), 0);
Vector2D intersection = l1.intersection(l2);
It is also feasible to use these structures to get the distance of a point to a line, or the closest point of a line to another line (in 3D).
使用这些结构来获得一个点到一条线的距离,或一条线到另一条线的最近点(在三维中)也是可行的。
7. Optimization, Genetic Algorithms, and Machine Learning
7.优化、遗传算法和机器学习
Commons-Math also provides some tools and algorithms for more complex tasks related to optimization and machine learning.
Commons-Math还为与优化和机器学习有关的更复杂的任务提供一些工具和算法。
7.1. Optimization
7.1.优化
Optimization usually consists of minimizing or maximizing cost functions. Algorithms for optimization can be found in org.apache.commons.math3.optim and org.apache.commons.math3.optimimization. It includes linear and nonlinear optimization algorithms.
优化通常包括最小化或最大化成本函数。优化的算法可以在org.apache.commons.math3.optimization和org.apache.commons.math3.optimization找到。它包括线性和非线性优化算法。
We can note that there are duplicate classes in the optim and optimization packages: the optimization package is mostly deprecated and will be removed in the Commons Math 4.
我们可以注意到,在optim和optimization包中存在重复的类:optimization包大部分已经废弃,将在Commons Math 4中被删除。
7.2. Genetic Algorithms
7.2.遗传算法
Genetic algorithms are a kind of meta-heuristics: they are a solution to finding an acceptable solution to a problem when deterministic algorithms are too slow. An overview of genetic algorithms can be found here.
遗传算法是一种元启发式算法:当确定性算法太慢时,它们是寻找可接受的问题解决方案的一种方法。遗传算法的概述可以在这里找到。
The package org.apache.commons.math3.genetics provides a framework to perform computations using genetic algorithms. It contains structure that can be used to represent a population and a chromosome, and standard algorithms to perform mutation, crossover, and selection operations.
包org.apache.commons.math3.genetics提供了一个使用遗传算法进行计算的框架。它包含可用于表示种群和染色体的结构,以及执行突变、交叉和选择操作的标准算法。
The following classes give a good start point:
下面的课程提供了一个很好的开始点。
- GeneticAlgorithm – the genetic algorithm framework
- Population – the interface representing a population
- Chromosome – the interface representing a chromosome
7.3. Machine Learning
7.3.机器学习
Machine learning in Commons-Math is divided into two parts: clustering and neural networks.
Commons-Math的机器学习分为两部分:聚类和神经网络。
The clustering part consists of putting a label on vectors according to their similarity regarding a distance metric. The clustering algorithms provided are based on the K-means algorithm.
聚类部分包括根据向量在距离度量方面的相似性给它们贴上标签。所提供的聚类算法是基于K-means算法的。
The neural network part gives classes to represent networks (Network) and neurons (Neuron). One may note that the provided functions are limited compared to the most common neural network frameworks, but it can still be useful for small applications with low requirements.
神经网络部分提供了表示网络(Network)和神经元(Neuron)的类。人们可能会注意到,与最常见的神经网络框架相比,所提供的功能是有限的,但它对于要求不高的小型应用仍然是有用的。
8. Utilities
8.公用事业
8.1. FastMath
8.1. FastMath
FastMath is a static class located in org.apache.commons.math3.util and working exactly like java.lang.Math.
FastMath是位于org.apache.commons.math3.util中的一个静态类,其工作方式与java.lang.Math完全一样。
Its purpose is to provide, at least the same functions that we can found in java.lang.Math, but with faster implementations. So, when a program is heavily relying on mathematical computations, it is a good idea to replace calls to Math.sin() (for instance) to calls to FastMath.sin() to improve the performance of the application. On the other hand please note that FastMath is less accurate than java.lang.Math.
它的目的是提供至少与我们在java.lang.Math中可以找到的相同的函数,但要有更快的实现。因此,当程序严重依赖数学计算时,将对Math.sin()的调用(例如)改为对FastMath.sin()的调用,以提高应用程序的性能,是一个好主意。另一方面,请注意,FastMath不如java.lang.Math.准确。
8.2. Common and Special Functions
8.2.常用和特殊功能
Commons-Math provides standard mathematical functions that are not implemented in java.lang.Math (like factorial). Most of these functions can be found in the packages org.apache.commons.math3.special and org.apache.commons.math3.util.
Commons-Math提供了标准的数学函数,这些函数没有在java.lang.Math中实现(如阶乘)。大多数这些函数可以在org.apache.commons.math3.special和org.apache.commons.math3.util包里找到。
For instance, if we want to compute the factorial of 10 we can simply do:
例如,如果我们想计算10的阶乘,我们可以简单地做。
long factorial = CombinatorialUtils.factorial(10);
Functions related to arithmetic (gcd, lcm, etc.) can be found in ArithmeticUtils, and functions related to combinatorial can be found in CombinatorialUtils. Some other special functions, like erf, can be accessed in org.apache.commons.math3.special.
与算术有关的函数(gcd,lcm,等等)可以在ArithmeticUtils中找到,而与组合有关的函数可以在CombinatorialUtils中找到。其他一些特殊函数,如erf,可以在org.apache.commons.math3.special中访问。
8.3. Fraction and Complex Numbers
8.3.分数和复数
It is also possible to handle more complex types using commons-math: fraction and complex numbers. These structures allow us to perform specific computation on this kind of numbers.
使用commons-math还可以处理更复杂的类型:分数和复数。这些结构允许我们对这类数字进行特定的计算。
Then, we can compute the sum of two fractions and display the result as a string representation of a fraction (i.e. under the form “a / b”):
然后,我们可以计算两个分数的总和,并将结果显示为分数的字符串表示(即在 “a/b “形式下)。
Fraction lhs = new Fraction(1, 3);
Fraction rhs = new Fraction(2, 5);
Fraction sum = lhs.add(rhs);
String str = new FractionFormat().format(sum);
Or, we can quickly compute power of complex numbers:
或者,我们可以快速计算复数的功率。
Complex first = new Complex(1.0, 3.0);
Complex second = new Complex(2.0, 5.0);
Complex power = first.pow(second);
9. Conclusion
9.结论
In this tutorial, we presented a few of the interesting things you can do using Apache Commons Math.
在这个教程中,我们介绍了一些你可以用Apache Commons Math做的有趣的事情。
Unfortunately, this article can’t cover the whole field of analysis or linear algebra, and thus, only provides examples for the most common situations.
不幸的是,本文无法涵盖整个分析或线性代数领域,因此,只提供最常见情况的例子。
However, for more information, we can read the well-written documentation, which provides a lot of details for all aspects of the library.
然而,为了获得更多的信息,我们可以阅读写得很好的文档,其中提供了很多关于库的各个方面的细节。
And, as always, the code samples can be found here on GitHub.
而且,像往常一样,代码样本可以在GitHub上找到这里。