java - 我想用java实现matlab的roots函数(多项式的根)

标签 java matlab math polynomial-math

我正在尝试理解 root 函数。我正在寻找实现类似函数 matlab r = root(p) 的 java 代码。

例如,如果p = [1 -6 -72 -27],matlab返回r = 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;
}
}

但对于我建议的示例,此代码返回 [ -2.5747724050560374, -0.17438281737671643, 0.08248855576608725 ]

我问你: matlab中的roots函数和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);

关于java - 我想用java实现matlab的roots函数(多项式的根),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28366954/

相关文章:

matlab - 从单元格值绘制直方图

matlab - matlab中数组行的组合和乘法

javascript - 生成具有对数分布和自定义斜率的随机数

Java 不兼容类型场景

java - 设置启用垃圾收集的java堆大小

java - 使用 JWT 的 Spring Data Rest

matlab - 在 MATLAB 中将图像文件转换为 AVI 视频

algorithm - 长度为k的所有子数组的元素乘积之和

r - geom_text 中的数学符号错误

java - 在用户下载程序之前在服务器上编译 Java 程序