static int Main(string[] args) { int N; if (args.Length > 0) { N = int.Parse(args[0]); } else { N = 3; } int nm = N; matrix A1 = randmatrix_sym(nm, nm); matrix V = new matrix(nm, nm); //vector e=jac_dia_red(A1,V,1); jac_dia ja = new jac_dia(A1); vector e = ja.e; WriteLine($"{ja.n_rot}"); return(0); }
static int Main() { int nm = 5; matrix A1 = randmatrix_sym(nm, nm); //matrix V= new matrix(nm,nm); jac_dia jd = new jac_dia(A1); vector ed = jd.e; //matrix Ad=jd.A; jac_clas jc = new jac_clas(A1); vector ec = jc.e; matrix Ac = jc.A; WriteLine($"Part C:"); WriteLine("i) Test that calculated eigenvalues with classic Jacobi are the same as the ones calulated by the cyclic sweep for a given random matrix A:"); WriteLine($"Classic: cyclic:"); for (int n2 = 0; n2 < nm; n2++) { WriteLine($"{ec[n2]} {ed[n2]}"); } WriteLine("That the clissic method diagonalize a given matrix A:"); for (int n1 = 0; n1 < nm; n1++) { for (int n2 = 0; n2 < nm; n2++) { Write($"{Ac[n1,n2]} "); } Write("\n"); } WriteLine($""); WriteLine($"ii) Does classic improve the number of rotation and the time?"); WriteLine($"In plot_rotations.svg and plot_time.svg is the number of rotations and the time used by the classic and the cyclic sweep methods compared."); WriteLine($"The result indicate that classic Jacobi is slower then the cyclic sweep, but use fewer rotations."); return(0); }
static int Main(string[] args) { // input to vector... var input = new System.IO.StreamReader("time_cyc.txt"); int input_lenght = 0; while (input.ReadLine() != null) { input_lenght++; } input.Close(); input = new System.IO.StreamReader("time_cyc.txt"); vector N = new vector(input_lenght); vector t = new vector(input_lenght); string line; for (int n = 0; n < input_lenght; n++) { line = input.ReadLine(); string[] words = line.Split(' '); N[n] = double.Parse(words[0]); t[n] = double.Parse(words[1]); } //end input to vector... //using oslf to check O(n³)... //function to fit: var ft = new Func <double, double>[] { x => 1, x => x }; //error on time set to 1%: vector dt = new vector(t.size); vector lN = new vector(t.size); vector lt = new vector(t.size); for (int i = 0; i < t.size; i++) { lN[i] = Log(N[i]); lt[i] = Log(t[i]); dt[i] = lt[i] / 100; } //end for //using olsf: olsf fit = new olsf(lN, lt, dt, ft); WriteLine("Part B:"); WriteLine(""); WriteLine("i) Check that the time goes as O(n³)."); WriteLine($"By using my least square fit from Problem 3, i get that the time roughly depends of the matrix size N as t=N^(c), with c={fit.c[1]}."); WriteLine(""); //check that eigenvalue by eigenvalue give same result as cyc //new random matrix: matrix A = randmatrix_sym(4, 4); //using cyc: jac_dia ja = new jac_dia(A); int nr = 0; //number of rotation: matrix V = new matrix(4, 4); vector e1 = jac_dia_red(A, V, 4, ref nr); WriteLine("ii) Implement the value-by-value method"); WriteLine("Check that the eigenvalues becomes the same as using cyclic sweeps."); WriteLine("Random generated symmetic matrix A:"); for (int i = 0; i < A.size1; i++) { for (int k = 0; k < A.size1; k++) { Write($"{A[i,k]} "); } //end for Write("\n"); } //end for Write("\n \n"); WriteLine("Calutaled eigenvalues:"); WriteLine("by cyclic: by value-by-value:"); for (int i = 0; i < ja.e.size; i++) { WriteLine($"{ja.e[i]} {e1[i]}"); } //end for WriteLine(""); WriteLine("iii) and iv) Compare speed."); WriteLine("In plot_time.svg the time used by the cyclic sweeps method to calculate alle the eigenvalues, is compared to the time used by the value-by-value method to calculate the first and all eigenvalue(s)."); WriteLine("It is faster to only calculate the first value by the value-by-value method then using cyclic sweeps. But cyclic sweeps is significant the calculated all eigenvalues by the value-by-value method."); WriteLine("In plot_rotation.svg the number of rotationes is compared."); WriteLine(""); //largest eigenvalue matrix V2 = new matrix(4, 4); int nr2 = 0; vector eh = jac_dia_high_e(A, V2, 3, ref nr2); WriteLine($"v) Largest eigenvalue."); WriteLine($"By changing the theta angle from Atan2(2*Apq, Aq-Ap) to Atan2(-2*Apq, App-Aqq) the larges eigenvlaue can be calculated."); WriteLine($"For vector A the value becomes: {eh[0]} the same as found by cyclic sweeps."); return(0); } // end Main
static int Main() { //testing the Jacobi dia.. WriteLine("Part A:"); WriteLine("i) Test of Jacobi diagonalization with cyclic sweeps:"); WriteLine("In the file test.txt the implementation is tested for a random sysmetic matrix."); var test = new System.IO.StreamWriter("test.txt"); int nm = 4; //matrix size matrix A1 = randmatrix_sym(nm, nm); //random matrix jac_dia ja = new jac_dia(A1); //diagonalization matrix A = ja.A; matrix D = new matrix(A.size1, A.size2); vector e = ja.e; for (int n = 0; n < A.size1; n++) { D[n, n] = e[n]; } matrix V = ja.V; matrix vdvt = V * D * V.T; matrix vtav = V.T * A1 * V; test.WriteLine("Test ofJacobi diagonalization with cyclic sweeps."); test.WriteLine($""); test.WriteLine("Random symmetric matrix A:"); for (int n1 = 0; n1 < A.size1; n1++) { for (int n2 = 0; n2 < A.size1; n2++) { test.Write($"{A1[n1,n2]} "); } test.Write("\n \n"); } test.WriteLine("Reduced matrix A:"); for (int n1 = 0; n1 < A.size1; n1++) { for (int n2 = 0; n2 < A.size1; n2++) { test.Write($"{A[n1,n2]} "); } test.Write("\n \n"); } test.WriteLine("the Diagonal matrix with the corresponding eigenvaluesmatrix D:"); for (int n1 = 0; n1 < A.size1; n1++) { for (int n2 = 0; n2 < A.size1; n2++) { test.Write($"{D[n1,n2]} "); } test.Write("\n \n"); } test.WriteLine("The orthogonal matrix of eigenvectorsmatrix V:"); for (int n1 = 0; n1 < A.size1; n1++) { for (int n2 = 0; n2 < A.size1; n2++) { test.Write($"{V[n1,n2]} "); } test.Write("\n \n"); } test.WriteLine("Test VDVT=A... VDVT is given as:"); for (int n1 = 0; n1 < A.size1; n1++) { for (int n2 = 0; n2 < A.size1; n2++) { test.Write($"{vdvt[n1,n2]} "); } test.Write("\n \n"); } test.WriteLine("Test VTAV=D... VTAV is given as:"); for (int n1 = 0; n1 < A.size1; n1++) { for (int n2 = 0; n2 < A.size1; n2++) { test.Write($"{vtav[n1,n2]} "); } test.Write("\n"); } test.Close(); //particle in box: //build H int nh = 50; double s = 1.0 / (nh + 1); matrix H = new matrix(nh, nh); for (int i = 0; i < nh - 1; i++) { matrix.set(H, i, i, -2); matrix.set(H, i, i + 1, 1); matrix.set(H, i + 1, i, 1); } //end for matrix.set(H, nh - 1, nh - 1, -2); matrix.scale(H, -1 / s / s); //diagonalize H jac_dia jah = new jac_dia(H); //matrix H_dia=jah.A; vector eh = jah.e; matrix Vh = jah.V; WriteLine("ii) Particle in a box."); WriteLine("The following is a comparison between the first 5 the caculated and exact energies:"); WriteLine("Number: Calculated: Exact:"); //check energies for (int n1 = 0; n1 < 5; n1++) { double exact = Pow(PI * (n1 + 1), 2); WriteLine($"{n1} {eh[n1]} {exact}"); } //end for WriteLine(""); WriteLine("In plotA.svg the 5 lowest eigenfunctions are plotted. The result have same form as the analytic result."); var data = new System.IO.StreamWriter("data.txt"); for (int k = 0; k < (5 + 1); k++) { data.Write($"{0} "); } //end for data.Write("\n"); for (int j = 0; j < nh; j++) { data.Write($"{(j+1.0)/(nh+1)} "); for (int k = 0; k < 5; k++) { data.Write($"{matrix.get(Vh,j,k)} "); } //end for data.Write("\n"); } // end for data.Write($"{1} "); for (int k = 0; k < (5); k++) { data.Write($"{0} "); } //end for data.Close(); return(0); }