static void B2() { WriteLine("\n Problem B2\n"); int n = 8; matrix A = rand_sym_mat(n); matrix B = A.copy(); WriteLine("Random symmetric matrix A with size {0}", n); WriteLine("Perform eigenvalue by eigenvalue calculation for first 4 values"); int evalnum = 4; bool val_by_val = true; jcbi_cycl test_vbv = new jcbi_cycl(B, val_by_val, evalnum); Write("Found eigenvalues = "); for (int i = 0; i < evalnum; i++) { Write(" {0:g3} ", test_vbv.Eigvals[i]); } Write("\n"); ((test_vbv.V).T * A * test_vbv.V - test_vbv.D).print("V.T*A*V - D = ", "{0,10:f6}"); WriteLine("\n Total rotations used = {0}", test_vbv.Rotations); B.print("\n Matrix copy used, after value by value operations = ", "{0,10:f6}"); matrix C = A.copy(); WriteLine("\nSimilar calculation with cyclic sweeps"); jcbi_cycl test_cycl = new jcbi_cycl(C); test_cycl.Eigvals.print("Found eigenvalues = "); (test_cycl.V.T * A * test_cycl.V - test_cycl.D).print("V.T*A*V - D = ", "{0,10:f6}"); WriteLine("\n Total rotations used = {0}", test_cycl.Rotations); C.print("\n Matrix copy used, after cyclic sweeps = ", "{0,10:f6}"); }
static void B5() { WriteLine("\n Problem B5\n"); int n = 5; matrix A = rand_sym_mat(n); matrix B = A.copy(); matrix C = A.copy(); WriteLine($"Random symmetric matrix A with size {n}"); WriteLine("We find all eigenvalues with cyclic routine"); jcbi_cycl test_cycl = new jcbi_cycl(B); test_cycl.Eigvals.print("Eigenvalues found by cyclic routine, low to high"); WriteLine("\nNow we use the value-by-value routine to calculate eigenvalues from last to first"); WriteLine("We do this by changing the calculation of " + "rotation angle theta to 0.5*Atan2(-Apq, App-Aqq)\n"); bool invert = true; int evalnum = n; bool vbv = true; jcbi_cycl test_vbv = new jcbi_cycl(C, vbv, evalnum, invert); test_vbv.Eigvals.print("Eigenvalues value-by-value, in order of calculation"); }
static void B4() { WriteLine("\n Problem B4\n"); WriteLine("See PlotB4r.svg and PlotB4t.svg for results"); int l = 50; // max matrix size int k = 5; // number of tries for each avg Stopwatch sw = new Stopwatch(); var timewriter = new System.IO.StreamWriter("out.b4time.txt"); var rotwriter = new System.IO.StreamWriter("out.b4rot.txt"); vector tvbv = new vector(l); vector tcyc = new vector(l); vector rvbv = new vector(l); vector rcyc = new vector(l); for (int i = 0; i < l; i++) { tvbv[i] = 0; tcyc[i] = 0; rvbv[i] = 0; rcyc[i] = 0; for (int j = 0; j < k; j++) { matrix A = rand_sym_mat(i + 1); sw.Reset(); matrix B = A.copy(); bool vbv = true; int evalnum = l; sw.Start(); jcbi_cycl test_vbv = new jcbi_cycl(B, vbv, evalnum); sw.Stop(); tvbv[i] += sw.Elapsed.TotalMilliseconds; rvbv[i] += test_vbv.Rotations; matrix C = A.copy(); sw.Reset(); sw.Start(); jcbi_cycl test_cyc = new jcbi_cycl(C); sw.Stop(); tcyc[i] += sw.Elapsed.TotalMilliseconds; rcyc[i] += test_cyc.Rotations; } tvbv[i] = tvbv[i] / k; tcyc[i] = tcyc[i] / k; rvbv[i] = rvbv[i] / k; rcyc[i] = rcyc[i] / k; timewriter.WriteLine($"{i} {tvbv[i]} {tcyc[i]}"); rotwriter.WriteLine($"{i} {rvbv[i]} {rcyc[i]}"); } timewriter.Close(); rotwriter.Close(); }
static int Main() { WriteLine("\n Problem A1: Test Jacobi functionality \n"); var rand = new System.Random(); int n = rand.Next(1, 15); matrix A = rand_sym_mat(n); WriteLine("Symmetric random test matrix A"); A.print("A = "); matrix B = A.copy(); jcbi_cycl test = new jcbi_cycl(B); WriteLine("\nJacobi diagonalisation A=V*D*V.T"); matrix V = test.V; matrix D = test.D; V.print("\nV = "); D.print("\nD = "); ((V.T * A) * V - D).print("\n(V.T*A)*V - D =", "{0,10:f6}"); WriteLine("\n\n Problem A2: Quantum Particle in box \n"); n = 99; double s = 1.0 / (n + 1); matrix H = new matrix(n, n); for (int i = 0; i < n - 1; i++) { H[i, i] = -2; H[i, i + 1] = 1; H[i + 1, i] = 1; } H[n - 1, n - 1] = -2; H = H * (-1 / s / s); matrix H1 = H.copy(); jcbi_cycl h_jac = new jcbi_cycl(H1); matrix Vh = h_jac.V; matrix Dh = h_jac.D; vector eigvals = h_jac.Eigvals; //H.print("\n H = "); //WriteLine("\n Jacobi diagonalisation H=V*D*V.T"); //Vh.print("\n V = "); //Dh.print("\n D = "); WriteLine("\n"); // Calculate analytic energies and compare WriteLine("Compare first {0} eigvals with analytic energies", n / 3); for (int i = 0; i < n / 3; i++) { double exact = PI * PI * (i + 1) * (i + 1); double calculated = eigvals[i]; WriteLine("{0} {1:f6} {2:f6}", i, calculated, exact); } var datw = new System.IO.StreamWriter("out.fun.txt"); // Write analytic function to file //double a = 0.149; //double a=0.141; double a = Sqrt(2 * s); int q = 1; Func <double, double> psiev = (x) => a *Sin(q *x *PI); Func <double, double> psiod = (x) => a *Sin(q *x *PI - PI); for (int k = 0; k < 4; k++) { vector psinum = Vh.col_toVector(k); Func <double, double> psi = pick_analytic_func(a, n, q, psinum); datw.WriteLine($"{0} {0}"); for (double x = 0.005; x < 1; x += 0.005) { datw.WriteLine($"{x} {psi(x)/a}"); /* if (k % 2 == 0) * { * datw.WriteLine($"{x} {psi(x)}"); * } * else * { * datw.WriteLine($"{x} {psiod(x)}"); * } */ } datw.WriteLine($"{1} {0}\n\n"); q++; } // Write function plot data to file for (int k = 0; k < 4; k++) { datw.WriteLine($"{0} {0}"); for (int i = 0; i < n; i++) { datw.WriteLine($"{(i+1.0)/(n+1)} {Vh[i, k]/a}"); } datw.WriteLine($"{1} {0} \n\n"); } datw.Close(); return(0); }
static void B1() { WriteLine("\n Problem B1: Test Jacobi cycle time \n"); int n = 50; WriteLine("Symmetric random test matrix A of size {0}", n); Stopwatch sw = new Stopwatch(); double time_avg = 0; int k = 15; vector times = new vector(k); for (int i = 0; i < k; i++) { sw.Reset(); matrix A = rand_sym_mat(n); matrix B = A.copy(); sw.Start(); jcbi_cycl test = new jcbi_cycl(B); sw.Stop(); times[i] = sw.Elapsed.TotalMilliseconds; time_avg += times[i]; } time_avg = time_avg / times.size; WriteLine("Average Jacobi diagonalisation time"); WriteLine("time_avg = {0} ms\n", time_avg); WriteLine("Perform calculation for different matrix sizes"); var timewriter = new System.IO.StreamWriter("out.time.txt"); k = 5; // number of tries for each avg int l = 30; // upper range of matrix sizes; vector tavgs = new vector(l - 5); vector xs = new vector(l - 5); vector terr = new vector(l - 5); for (int i = 5; i < l; i++) { xs[i - 5] = i * 1.0; tavgs[i - 5] = 0; for (int j = 0; j < k; j++) { matrix A = rand_sym_mat(i); sw.Reset(); matrix B = A.copy(); sw.Start(); jcbi_cycl test = new jcbi_cycl(B); sw.Stop(); tavgs[i - 5] += sw.Elapsed.TotalMilliseconds; } tavgs[i - 5] = tavgs[i - 5] / k; terr[i - 5] = tavgs[i - 5] * 0.025; timewriter.WriteLine($"{i} {tavgs[i-5]}"); } timewriter.Close(); Func <double, double>[] tfun = new Func <double, double> [2]; tfun[0] = (x) => 1; tfun[1] = (x) => x * x * x; //tfun[0] = (x) => 1; tfun[1] = (x) => x; tfun[2] = (x) => x*x; tfun[3] = (x) => x*x*x; lsfit tfit = new lsfit(xs, tavgs, terr, tfun); WriteLine("exec time fit params {0} {1}", tfit.C[0], tfit.C[1]); vector fiteval = tfit.evaluate(xs); var tfunw = new System.IO.StreamWriter("out.tfun.txt"); for (int i = 0; i < xs.size; i++) { tfunw.WriteLine($"{xs[i]} {fiteval[i]}"); } tfunw.Close(); WriteLine("See PlotB1.svg for execution time calculation and a+b*x^3 fit"); }