All pastes #457077 Raw Edit

Untitled

public java v1 · immutable
#457077 ·published 2007-04-25 05:02 UTC
rendered paste body
import java.math.BigDecimal;public class Power {	/* The threshhold constraint for the approximation */	private static double SIGMA = 10E-8;	/* The size of the matrices to be generated, generates SIZE by SIZE matrices */	private static int SIZE = 6;	/**	 * Finds an approximate eigenvector of the matrix A.	 * 	 * @param A	 *            Symmetric, square matrix.	 * @return An Object array containing composed such that element 0 is the	 *         approximate eigenvector, and element 1 is the number of	 *         iterations needed to find the approximation.	 */	public static Object[] getApproxEigenVector(Matrix A) {		int count = 0;		Matrix A16 = Matrix.pow(A, 16);		Matrix u = getUnitVector(A16, 0);		Matrix Au = A.times(u);		double c = (u.transpose()).times(Au).get(0, 0);		Matrix r = Au.minus(u.times(c));				/* Hacked up initial shift */		A = (A.minus(Matrix.identity(SIZE, SIZE).times(c))).inverse();		Au = A.times(u);		c = (u.transpose()).times(Au).get(0, 0);		r = Au.minus(u.times(c));		/* Hacked up initial shift */				while (magnitude(r) > SIGMA) {			count++;			try {				A = (A.minus(Matrix.identity(SIZE, SIZE).times(c))).inverse();			} catch (Exception e) {				e.printStackTrace();				//System.out.println("The randomly generated matrix was singular, please re-run to generate a non-singular matrix.");				System.exit(1);			}						Au = A.times(u);			c = (u.transpose()).times(Au).get(0, 0);			r = Au.minus(u.times(c));		} 		return new Object[] { u, c, count };	}	/**	 * Returns a column vector of matrix A after normalizing.	 * 	 * @param A	 *            Square, symmetric matrix.	 * @param col	 *            The index of the col to normalize and return.	 * @return A normalized column vector.	 */	public static Matrix getUnitVector(Matrix A, int col) {		Matrix u = A.getMatrix(0, SIZE - 1, col, col);		return u.timesEquals(1 / magnitude(u));	}	/**	 * Returns the length of the vector.	 * 	 * @param v	 *           A SIZE by one Matrix.	 * @return the length of the vector.	 */	private static double magnitude(Matrix v) {		BigDecimal size = new BigDecimal(0.0);		BigDecimal tmp = new BigDecimal(0.0);		BigSquareRoot app = new BigSquareRoot();		for (int i = 0; i < SIZE; i++) {			tmp = new BigDecimal(v.get(i, 0));			size = size.add(tmp.pow(2));		}		return app.get(size).doubleValue();	}	/**	 * Sandbox	 */	public static void main(String[] args) {		for (int i = 0; i < 10; i++) { 					// Generate a random SIZE by SIZE symm matrix			Matrix A = Matrix.randomSymm(SIZE);			//A.print(10, 5);						// Approximate an eigenvector of A			Object[] ans = getApproxEigenVector(A);				// The eigvenvector is in ans[0]			//((Matrix) ans[0]).print(10, 5);				// The iteration count is in ans[1]			System.out.println("count: " + ans[2]);						double[][] mv = A.getArray();			double[][] ev = A.getArray();			double answ = 0;			for (int j = 0; j < SIZE; j++) {				answ += mv[0][j] * ev[j][0]; 			}			System.out.println("ans: " + answ);						System.out.println("-----------------");		}	}}