java - 在Java中寻找多项式的根

标签 java math polynomial-math

我需要找到 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/

相关文章:

java - 为什么使用 getter 函数代替 "?"时会出错

python - python中有数学nCr函数吗?

c# - 基本渲染 3D 透视投影到带摄像头的 2D 屏幕(不带 opengl)

math - OpenGL 数学 - 将屏幕空间投影到世界空间坐标

javascript - JS 中的牛顿法不准确

java - 如何使用 android 使用 FutureTask 或 BackgroundTask 实现 .get 功能?

java - 如何在 Spring XML 文件中使用 DOCTYPE

java - 对事务中需要的 DDL 语句进行单元测试

math - 在javascript中查找Antilog并在javascript中求解n次多项式方程

python - 用python求解联立多元多项式方程