static void Main(string[] args) { /* Compare the time it takes to diagonalize a matrix * of size n by different methods: * Cyclic jacobi, value-by-value and v-b-v * for both highest and lowest eig val. */ Stopwatch sw = new Stopwatch(); foreach (string arg in args) { int n = Int32.Parse(arg); // Generate symmetric nxn matrix A matrix A = randomMatrix(n, n); restoreUpperTriang(A); // Cyclic jacobi matrix V = new matrix(n, n); vector e = new vector(n); sw.Start(); int sweeps = Jacobi.cyclic(A, V, e); sw.Stop(); double cyclic = sw.Elapsed.Ticks / 10000.0; sw.Reset(); // Value-by-value full V = new matrix(n, n); e = new vector(n); sw.Start(); vector eigVals = Jacobi.lowestK(A, V, e, n); sw.Stop(); double VBVfull = sw.Elapsed.Ticks / 10000.0; sw.Reset(); // Value-by-value 1 V = new matrix(n, n); e = new vector(n); sw.Start(); eigVals = Jacobi.lowestK(A, V, e, 1); sw.Stop(); double VBVlow = sw.Elapsed.Ticks / 10000.0; sw.Reset(); // Value-by-value 1, highest first V = new matrix(n, n); e = new vector(n); sw.Start(); eigVals = Jacobi.highestK(A, V, e, 1); sw.Stop(); double VBVhigh = sw.Elapsed.Ticks / 10000.0; sw.Reset(); // size of matrix and time in ms WriteLine($"{n} {cyclic} {VBVfull} {VBVlow} {VBVhigh}"); } }
static void Main() { int n = 6; WriteLine($"Generate symmetric {n}x{n} matrix A"); matrix A = randomMatrix(n, n); restoreUpperTriang(A); A.print("A: "); matrix V = new matrix(n, n); vector e = new vector(n); int sweeps; // Cyclic sweeps = Jacobi.cyclic(A, V, e); WriteLine($"Cyclic Jacobi done in {sweeps} sweeps"); e.print("Cyclic eigenvalues: "); // Lowest k restoreUpperTriang(A); V = new matrix(n, n); e = new vector(n); int k = n / 3; vector v = Jacobi.lowestK(A, V, e, k); v.print($"Lowest {k} values:"); A.print($"Test that {k} rows are cleared"); // Highest k restoreUpperTriang(A); V = new matrix(n, n); e = new vector(n); k = n / 3; v = Jacobi.highestK(A, V, e, k); v.print($"Highest {k} values:"); A.print($"Test that {k} rows are cleared"); restoreUpperTriang(A); V = new matrix(n, n); e = new vector(n); int rots = Jacobi.classic(A, V, e); e.print($"Classical done! rotations: {rots}"); A.print($"Test that all rows are cleared"); }