【问题标题】:I want to implement the roots function of matlab (root of polynomial) in java我想在java中实现matlab的根函数(多项式的根)
【发布时间】:2015-02-06 13:33:41
【问题描述】:

我正在尝试理解 root 函数。我正在寻找实现类似函数 matlab r = roots(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 ]

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

【问题讨论】:

  • I have no idea what it means in practical function roots 尝试编写您不理解的代码是个坏主意
  • 如果您不知道多项式的根是什么...很难理解特征值分解...也许数学课 + 维基百科会对您有所帮助。
  • 是的,我现在正在研究它
  • 注意:math.stackexchange.com 可能更合适。

标签: java matlab math polynomial-math


【解决方案1】:

功能应该是一样的,不同的是你传递de方法的coefs的顺序改变了。试试:

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

或使用 apache 数学:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-01
    • 2015-01-29
    • 1970-01-01
    • 1970-01-01
    • 2015-03-21
    • 2015-05-14
    相关资源
    最近更新 更多