polynomial root solver

26 views
Skip to first unread message

rkhan...@gmail.com

unread,
Mar 6, 2013, 5:06:39 PM3/6/13
to efficient-java-mat...@googlegroups.com
Hi Peter,

I have been trying to use your code and it really is not working for me. Maybe I am implementing it wrong, but if you could help me out that would be great.

public class Tester {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
       
        double [] roots= {1,5,-14};
        RootFinder root= new RootFinder(roots);
        System.out.println(Arrays.toString(root.findRoots()));
       
}
}

Public class RootFinder {

    private double [] coefficients;
    public RootFinder(double [] coefficients)
    {
        this.coefficients=coefficients;
    }
   
    public Complex64F[] findRoots() {
        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.eig(N,false);

        evd.decompose(c);

        Complex64F[] roots = new Complex64F[N];

        for( int i = 0; i < N; i++ ) {
            roots[i] = evd.getEigenvalue(i);
        }

        return roots;

}
}

The output I am getting is [-0.14285714285714282, 0.5], which are actually the negative reciprocal of the roots of the above polynomial. When I tried to fix that error it only really worked for this case. Any idea of what I am doing wrong? Thanks.

Rahul

Peter A

unread,
Mar 6, 2013, 5:56:26 PM3/6/13
to efficient-java-mat...@googlegroups.com
Try reversing the coefficient's order.

double [] roots= {-14,5,1};

Here it is written out: polynomial = -14 + x*5 + x*x;

I'll update the documentation so that it specifies the coefficient order.

BTW you might want to check out my library: ddogleg it has more ways to compute the roots of polynomial and a cleaner API.

- Peter


--
You received this message because you are subscribed to the Google Groups "efficient-java-matrix-library-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to efficient-java-matrix-li...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
"Now, now my good man, this is no time for making enemies."    — Voltaire (1694-1778), on his deathbed in response to a priest asking that he renounce Satan.

Rahul Khanna

unread,
Mar 8, 2013, 3:53:53 PM3/8/13
to efficient-java-mat...@googlegroups.com
Oh okay thanks a lot for the help and explanation.

Rahul
Reply all
Reply to author
Forward
0 new messages