4

ルジャンドル多項式の(近似、数値)解を見つける必要があります。いくつかのJavaライブラリを試しましたが、探しているものはありません(最も近いのは、Laguerreソルバーでソリューションを見つけるためのコードもあるcommons-mathですが、メソッドは公開されていません)。既存のソリューションはありますか、それとも独自のソリューションを実装する必要がありますか?

4

3 に答える 3

8

EJMLEfficient 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;
    }
}
于 2012-12-10T17:11:06.717 に答える
1

リリース3.1以降、Commons-Mathは、多項式関数のすべての複素数根の検索をサポートしています。

LaguerreSolver#solveAllComplexを参照してください

于 2014-06-27T20:48:10.803 に答える
0

Commons-Mathには、多項式用の妥当なAPIがあります。

//  -4 + 3 x + x^2
PolynomialFunction polynomial = new PolynomialFunction(new double[]{ -4, 3, 1});
LaguerreSolver laguerreSolver = new LaguerreSolver();
double root = laguerreSolver.solve(100, polynomial, -100, 100);
System.out.println("root = " + root);
于 2016-03-29T12:59:56.230 に答える