Friday, March 6, 2015

Power Iteration for the largest eigenvector in Java

Wikipedia

public double[] powerIt(double[][] a) {

    int n=a.length;
    double e = 0.5 * pow(10, -7);
    double[] b = new double[n];
    Random r = new Random(System.currentTimeMillis());
    for (int i = 0; i < n; i++) {
        b[i] = r.nextDouble();
    }

    double l0;
    double l1 = firstNonZero(b);
    do {
        l0 = l1;
        b = multiply(a, b);
        l1 = firstNonZero(b);
        b=multiply(b,1/l1);
    } while (abs(l1 - l0) > e);
    return b;
}

This is part of the Matrices project.Refer to Basic matrix calculations in Java for the missing secondary methods.

No comments: