我想在 java 中实现 matlab 的根函数(多项式的根)

I want to implement the roots function of matlab (root of polynomial) in java

我正在尝试理解根函数。我正在寻找实现类似函数 matlab r = roots(p).

的 java 代码

例如如果p = [1 -6 -72 -27],matlabreturnsr = 12.1229 -5.7345 -0.3884

我承认我不知道它在实际函数根中的含义,但我需要在我的 java 应用程序的算法中使用它。

我尝试将此代码与 Efficent-java-matrix-library 一起使用:

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;
}
}

但此代码 returns [ -2.5747724050560374, -0.17438281737671643, 0.08248855576608725 ] 用于我提出的示例。

我问你: roots函数matlab和java中的roots函数是同一个函数吗? 你有什么想法在 matlab 中实现一个类似于 roots 的 java 函数吗?

功能应该是一样的,不同的是你传给方法的系数的顺序变了。尝试:

final double[] coeff = new double[] { -27, -72, -6, 1 };

或使用 apache 数学:

final LaguerreSolver solver = new LaguerreSolver();
final Complex[] result = solver.solveAllComplex(coeff, 0);