我需要找到 Legendre polynomial 的(近似数值)解.我尝试了几个 Java 库,但没有一个有我要找的东西(最接近的是 commons-math,它甚至有用于在 Laguerre solver 中查找解决方案的代码,但它没有公开方法)。是否有现有的解决方案,或者我是否需要实现自己的解决方案?
最佳答案
您可以使用 EJML (高效的 Java 矩阵库)。
请在下面找到相同的示例。
public class PolynomialRootFinder {
/**
* <p>
* Given a set of polynomial coefficients, compute the roots of the polynomial. Depending on
* the polynomial being considered the roots may contain complex number. When complex numbers are
* present they will come in pairs of complex conjugates.
* </p>
*
* @param coefficients Coefficients of the polynomial.
* @return The roots of the polynomial
*/
public static Complex64F[] findRoots(double... coefficients) {
int N = coefficients.length-1;
// Construct the companion matrix
DenseMatrix64F c = new DenseMatrix64F(N,N);
double a = coefficients[N];
for( int i = 0; i < N; i++ ) {
c.set(i,N-1,-coefficients[i]/a);
}
for( int i = 1; i < N; i++ ) {
c.set(i,i-1,1);
}
// Use generalized eigenvalue decomposition to find the roots
EigenDecomposition<DenseMatrix64F> evd = DecompositionFactory.eigGeneral(N, false);
evd.decompose(c);
Complex64F[] roots = new Complex64F[N];
for( int i = 0; i < N; i++ ) {
roots[i] = evd.getEigenvalue(i);
}
return roots;
}
}
关于java - 在Java中寻找多项式的根,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13805644/