static void Main() { var ma = new matrix("1 2;5 6"); ma.print(); var ma2 = ma * ma.T; (ma2).print(); WriteLine($"{ma[0].norm()}"); WriteLine($"{ma[1].norm()}"); WriteLine($"{ma[0,0]} {ma[0,1]}"); WriteLine($"{ma[1,0]} {ma[1,1]}"); matrix v1 = (ma).cols(0, 0); (v1).print(); vector v2 = ma[1]; (v2).print(); matrix v3 = (ma2).cols(0, 0); (v3).print(); matrix v4 = (ma2).rows(1, 1); (v4).print(); ma[1, 1] = 22; ma.print(); }
public static void Main() { int n = 3; matrix A = new matrix(n, n); matrix V = new matrix(n, n); vector e = new vector(n); var rand = new Random(); for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { A[i, j] = rand.NextDouble() * 10; A[j, i] = A[i, j]; } } A.print("Symmetric matrix A:"); matrix Ao = new matrix(n, n); Ao = A.copy(); int count = jacobi.cycsweep(A, e, V); WriteLine($"Number of sweeps: {count}"); V.print("Eigenvectors"); A.print("new A"); e.print("Eigenvalues"); (V.T * Ao * V).print("V^T*A*V diagonal, test"); }
static void Main() { WriteLine("Exam problem 21: \nTwo-sided Jacobi algorithm for Singular Value Decomposition (SVD) \n");; int n = 7; WriteLine($"Generating Random {n}x{n} matrix"); matrix A = randomMatrix(n, n), U = new matrix(n, n), V = new matrix(n, n); matrix D = A.copy(); A.print("Matrix A generated:"); WriteLine("Making composition..."); int sweeps = twoSidedJacobiSVD(D, U, V); WriteLine($"Procedure done!\nNo. of sweeps: {sweeps}.\nNow A --> U*D*V^T, where"); D.print("D: "); U.print("U: "); V.print("V: "); matrix R = (U * D) * V.transpose(); R.print("Test that UDV^T = A"); A.print("A, for reference"); matrix UUt = U * U.transpose(), VVt = V * V.transpose(); UUt.print("Test U*U^T = 1"); VVt.print("Test V*V^T = 1"); }
public static void Main() { WriteLine(); WriteLine("Create a matrix and diagonalize it via the cyclic, value-by-value and" + " classic method."); var rand = new Random(); int n = 5; // Create a random matrix matrix A = new matrix(n, n); for (int i = 0; i < n; i++) { A[i, i] = 2 - 4 * rand.NextDouble(); for (int j = i + 1; j < n; j++) { A[i, j] = 2 - 3.32 * rand.NextDouble(); A[j, i] = A[i, j]; } } // Print the random matrix. WriteLine("\nSetting up a random symmetric matrix:"); A.print(); WriteLine(); // Diagonalize it via the three different methods matrix Acopy = A.copy(); matrix AcopyII = A.copy(); matrix V = new matrix(n, n); vector e = new vector(n); vector eRot = new vector(n); vector eClassic = new vector(n); int sweeps = jacobi.cycle(A, e, V); int entries = (n * n - n) / 2; int rotations = jacobi.findEigenvalue(Acopy, eRot, V, n, true); int rotationsClassic = jacobi.classic(AcopyII, eClassic, V); WriteLine("The cyclic method used {0} operations", sweeps * entries); WriteLine("The value-by-value method used {0} operations", rotations); WriteLine("The classic method used {0} operations", rotationsClassic); WriteLine("\nAfter cyclic diagonalization the matrix looks like:"); A.print(); WriteLine("\nAfter value-by-value diagonalization the matrix looks like:"); Acopy.print(); WriteLine("\nAfter classic Jacobi diagonalization the matrix looks like:"); AcopyII.print(); Write("\n\n"); WriteLine("The found eigenvalues are:"); WriteLine("E(cyclic)\t\tE(val_by_val)\t\tE(classic)"); for (int j = 0; j < n; j++) { WriteLine("{0}\t{1}\t{2}", e[j], eRot[j], eClassic[j]); } }
static void Main() { int n = 6; int m = 4; // Problem A1: WriteLine($"Problem A1: QR-factorization on a random {n}x{m} matrix A: \n"); matrix A = generatematrix(n, m); // Printing our random matrix A A.print("Matrix A ="); QRdecompositionGS decomp = new QRdecompositionGS(A); // Printing the orthogonal matrix Q matrix Q = decomp.Q; Q.print("\nDecomposed component Q = "); // Printing the upper triangle matrix R matrix R = decomp.R; R.print("\nDecomposed component R = "); // Checking that Q^T*Q is equal to the identity (Q.transpose() * Q).print("\nQ^(T)*Q = "); // Checking that QR=A by substracting A from QR to yield the null matrix (Q * R - A).print("\n Q*R-A = "); n = 3; // Problem A2: WriteLine($"\nProblem A2: QR-factorization on a random {n}x{n} matrix A solving Ax=b: \n"); vector b = generatevector(n); A = generatematrix(n, n); decomp = new QRdecompositionGS(A); b.print("Vector b = "); A.print("\nSquare matrix A = "); vector x = decomp.solve(b); // Printing the solution to Ax=b x.print("\nVector x = "); WriteLine("\nChecking that x is indeed the solution: "); (A * x - b).print("\nA*x-b = "); // Problem B: A = generatematrix(n, n); decomp = new QRdecompositionGS(A); WriteLine($"\nProblem B: Matrix inverse by Gram-Schmidt QR factorization \n"); matrix B = decomp.inverse(); B.print("A^(-1) = "); (A * B).print("\n A*A^(-1) = "); // Problem C: }
class main { public static void Main() { int n = RandomInt(3, 5); matrix A = new matrix(n, n); for (int i = 0; i < n; i = i + 1) { for (int j = i; j < n; j = j + 1) { if (i == j) { A[i, j] = RandomDouble(5, 20); } else { A[i, j] = RandomDouble(5, 20); A[j, i] = A[i, j]; } } } A.print("Randomly generated symmetric square matrix A=\n"); matrix A_2 = A.copy(); matrix V = new matrix(n, n); vector e = new vector(n); // Diagonalization int sweeps = JacobiDiag(A, e, V); A.print("\nD=\n"); matrix A1 = V.T * A_2 * V; V.print("\nV=\n"); A1.print("\nV(^T)AV=\n"); e.print("\nObtained eigenvalues of A:\n\n"); double sum = 0; for (int i = 0; i < n; i = i + 1) { if (Round(A1[i, i]) == Round(e[i])) { sum = sum + 1; } } if (sum == n) { Out.WriteLine($"\nDiagonalization succesfull: Completed in {sweeps} iterations"); } else { Out.WriteLine("\nDiagonalization failed"); } }
static void probA1() { Write("Problem A1:\n"); var rand = new System.Random(); int m = 2 + rand.Next(6); int n = m + rand.Next(6); Write("First I create a random tall matrix with random dimmensions\n"); matrix A = makeRandomMatrix(n, m); A.print("Random matrix A:"); qr_gs decomposer = new qr_gs(A); Write("\nThen I decompose it into Q and R using the Gramm Schmitt method trough my qr_gs class:\n"); matrix Q = decomposer.Q; matrix R = decomposer.R; Q.print("Decomposition Q = "); R.print("Decomposition R = "); Write("\nCheck that Q^(T)Q is equal to the identity matrix:\n"); (Q.transpose() * Q).print("Q^(T)Q = "); Write("\nCheck that Q*R = A, or equivalently that Q*R-A is a matrix of all zeros:\n"); (Q * R - A).print("Q*R-A"); }
static void probA2() { Write("\nProblem A2:\n"); // Need to make the tests for A2 var rand = new System.Random(); // Remember, square matrix: Write("\nMake a random square matrix, A, with random dimmensions, and a random vector, b, of the same dimmension:\n"); int n = 2 + rand.Next(10); matrix A = makeRandomMatrix(n, n); vector b = makeRandomVector(n); A.print("A = "); b.print("b = "); qr_gs decomposer = new qr_gs(A); Write("\nSolve the system A*x = b for the vector x:\n"); //decomposer.Q.print("Decomposition Q: "); //decomposer.R.print("Decomposition R: "); //(decomposer.Q.transpose()*b).print("Q^(T)*b = "); vector x = decomposer.solve(b); x.print("Solution: x = "); Write("\nCheck that the vector x satisfies A*x=b:\n"); (A * x - b).print("A*x-b = "); }
static void Main() { /// Test on random matrix /// WriteLine("/////////////// Part A - Test on random matrix ///////////////"); int n = 5; var rand = new Random(1); matrix A = new matrix(n, n); vector e = new vector(n); matrix V = new matrix(n, n); for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { A[i, j] = rand.NextDouble(); A[j, i] = A[i, j]; } } A.print($"random {n}x{n} matrix"); WriteLine(); matrix B = A.copy(); int sweeps = jacobi.cyclic(B, e, V); WriteLine($"Number of sweeps in jacobi={sweeps}"); WriteLine(); matrix D = (V.T * A * V); (D).print("Should be a diagonal matrix V.T*B*V="); WriteLine(); e.print("Eigenvalues should equal the diagonal elements above"); vector D_diagonal = new vector(n); for (int i = 0; i < n; i++) { D_diagonal[i] = D[i, i]; } if (D_diagonal.approx(e)) { WriteLine("Eigenvalues agree \tTest passed"); } else { WriteLine("Test failed"); } WriteLine(); matrix A2 = (V * D * V.T); (A2).print("Check that V*D*V.T=A"); if (A2.approx(A)) { WriteLine("V*D*V.T = A \tTest passed"); } else { WriteLine("V*D*V.T != A \tTest failed"); } }
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 Main() { var rnd = new Random(); var A = new matrix(3, 3); for (int i = 0; i < A.size1; i++) { for (int j = 0; j < A.size2; j++) { A[i, j] = 10 * (rnd.NextDouble() - 0.5); } } var Q = A.copy(); var R = new matrix(Q.size2, Q.size2); WriteLine("\n__________________________________________________________________________________________________________"); WriteLine("Question B:\nMatrix inverse by Gram-Schmidt QR factorization"); WriteLine("Random matrix"); A.print("A = "); qr_gs.qr_gs_decomp(Q, R); WriteLine("Factorized into"); Q.print("Q = "); R.print("R = "); var B = qr_gs.qr_gs_inverse(Q, R); WriteLine("Inverse of A:"); B.print("A^-1=B="); WriteLine("Product should give identity"); (A * B).print("I=AB="); WriteLine("__________________________________________________________________________________________________________\n"); }
public static void Main(string[] args) { int n = 5; //size of real symmetric matrix matrix A = new matrix(n, n); var rnd = new Random(1); for (int i = 0; i < n; i++) /* Construction of a random vector */ { for (int j = 0; j < n; j++) { A[i, j] = (rnd.NextDouble() * 10); A[j, i] = A[i, j]; /* We make sure that the matrix is symmetric */ } } A.print("Random symmetric matrix, A:"); matrix V = new matrix(n, n); vector e = new vector(n); matrix B = A.copy(); /* We make a copy of A */ int sweeps = jacobi.cyclic(A, e, V); /* We use the jacobi matlib for the implementation of the cyclic sweeps: public static int cyclic(matrix A, vector e, matrix V=null){...} */ WriteLine($"\nNumber of sweeps: {sweeps}"); matrix D = (V.T * B * V); // D.print("\nEigenvalue-decomposition, V^T*A*V (should be diagonal):"); D.printfloat("\nEigenvalue-decomposition, V^T*A*V (should be diagonal):"); /* using my print2 to get 0's instead of values with e.g. "e-16"*/ e.print("\nEigenvalues:\n"); }
public static void Main() { // First part of A int n = 5; // Size of quadratic matrix M. var rnd = new Random(1); Console.WriteLine("Part A1"); matrix M = new matrix(n, n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { M[i, j] = 4 * rnd.NextDouble() - 1.5; M[j, i] = M[i, j]; } } matrix M2 = M.copy(); M.print("We choose an arbitrary matrix M = "); vector D = new vector(n); matrix V = new matrix(n, n); int sweeps = Jacobi_diagonalization.cyclic_sweep(M, V, D); matrix temp2 = V.T * M2 * V; Console.WriteLine($"Total number of sweeps done is = {sweeps}"); temp2.print("V.T*M*V = "); D.print("Eigenvalues = "); }
static void Main() { WriteLine("My last 2 digits in my student number is 22 so 22 mod 22 = 0, which is then the exam project I have done."); WriteLine("In this project I use the Lanczos algorithm to produce the Matrixes V and T where A = V T V^{T}"); WriteLine("The real symmetric matrix we choose as en example is"); //Define symmetric matrix var A = new matrix("1 2 3;2 4 5;3 5 8"); A.print("A = "); WriteLine("We then apply the Lanczos algortihm to get the V and T matrices."); //Call V and T matrix from Lanczos algorithm matrix T = Lanczos.execute(A, 3, 0); matrix V = Lanczos.execute(A, 3, 1); //Print them T.print("T = "); V.print("V = "); //Recreate A to check that T and V are correct matrix A2 = V * T * V.transpose(); A2.print("Were A should be V T V^T. We calculate this to check, V T V^T = "); WriteLine("Which gives exactley A again"); }
static void probC() { Write("Problem C:\n"); var rand = new System.Random(); int n = 5 + rand.Next(3); matrix A = makeRandomMatrix(n, n); A.print("Make a random square matrix of random size: A = "); qr_givens decomp_givens = new qr_givens(A); qr_gs decomp_gs = new qr_gs(A); Write("\nDo the Givens decomposition and store the restult in the matrix G, which cotains the elements of the component R in the upper triangular part, and the angles for the Givens rotations in the relevant sub-diagonal entries. Compare with the R matrix found from the Gramm-Schmitt method:"); decomp_givens.G.print("Givens G: "); decomp_gs.R.print("Gram-Schmitt R: "); Write("The upper triangular parts are the same.\n"); vector b = makeRandomVector(n); b.print("\nMake a random vector b with the same size as A: b = "); Write("\nCompare using the Givens rotations to apply Q^(T) to b, and doing the explicit matrix multiplication with the Q matrix found by the Gramm-Schmitt method:\n"); decomp_givens.applyQT(b).print("Givens: Q^(T)*b = "); (decomp_gs.Q.transpose() * b).print("GS: Q^(T)*b = "); vector x = decomp_givens.solve(b); x.print("\nSolution to A*x=b using Givens: "); (A * x - b).print("\nCheck solution satisfies A*x = b: A*x-b = "); Write("\nFind the inverse of A, and check that A^(-1)*A is the identity matrix:\n"); decomp_givens.inverse().print("A^(-1)"); (decomp_givens.inverse() * A).print("A^(-1)*A = "); }
static void Main() { var ma = new matrix("1 2;5 6"); ma.print(); (ma * ma.T).print(); }
// Comparison of time and rotations needed to calculate the lowest // eigenvalue for the cyclic and single value method. static void probB5() { Write("\n\nProblem B.5:\n"); int n = 7; matrix A1 = matrixHelp.makeRandSymMatrix(n); matrix A2 = A1.copy(); Write("Finding the largest eigenvalue of the matrix:\n"); A1.print(); matrix V1 = new matrix(n, n); matrix V2 = new matrix(n, n); vector e1 = new vector(n); vector e2 = new vector(n); int rot1 = jacobi.cyclic(A1, e1, V1); int rot2 = jacobi.values(A2, e2, 1, V2, true); Write("\n"); Write($"Largest found by cyclic jacobi: {e1[n-1]}\n"); Write($"Largest found by single row jacobi: {e2[0]}\n"); Write("\nThe single row method was changed by changing the calculation of the rotation angle theta to: 0.5*Atan2(-Apq, App-Aqq)\n"); }
public static int Main() { // int n=5; int m = 4; var rand = new Random(1); // Assignment B matrix B = new matrix(m, m); for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { B[i, j] = 10 * (rand.NextDouble()); } } WriteLine("Assignment B: "); B.print($"Square matrix A with size {m}x{m}, to do assigment B:"); var qr_B = new qrdecompositionGS(B); var B_inv = qr_B.inverse(); B_inv.print("The invers matrix of A, B=:"); var bbinv = B * B_inv; bbinv.print("A*B=I"); return(0); }
public static void Main() { Write("Part 1:\n"); Write("Demonstrating that the Lanczos iterations produces the right V and T matrix:\n"); int n = 6; matrix A = matrixHelp.makeRandSymMatrix(6); Write($"Here for n = {6}, m = n\n"); string fmt = ":F4"; A.print("The randomly generated symmetric matrix: A = ", fmt); int m = n; matrix V = new matrix(n, m); matrix T = new matrix(m, m); lanczos.iterations(A, V, T); T.print("The found tridiagonal matrix T:", fmt); V.print("The found orthogonal matrix V:", fmt); Write("\nIf V is orthogonal, then V*V^T = I:\n"); (V * V.T).print("V*V^T = ", fmt); Write("\nAs n = m here, so V*T*V^T should give A:\n"); (V * T * V.T - A).print("V*T*V^T - A = ", fmt); }
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"); }
class main { public static void Main() { int n = RandomInt(4, 8); matrix A = new matrix(n, n); for (int i = 0; i < n; i = i + 1) { for (int j = i; j < n; j = j + 1) { if (i == j) { A[i, j] = RandomDouble(5, 20); } else { A[i, j] = RandomDouble(5, 20); A[j, i] = A[i, j]; } } } A.print("Randomly generated symmetric square matrix A=\n"); matrix A_2 = A.copy(); matrix V = new matrix(n, n); vector e = new vector(n); // Diagonalization int c = 1; // Number of lowest eigenvalues int sweeps = JacobiDiag(A, e, V, c); A.print("\nD=\n"); matrix A1 = V.T * A_2 * V; V.print("\nV=\n"); A1.print("\nV(^T)AV=\n"); //A.print("\nD=\n"); if (Round(A[0, 0]) == Round(e[0])) { Out.WriteLine($"\nElimination of first row succesfull: Completed in {sweeps} iterations"); } else { Out.WriteLine("\nElimination failed"); } WriteLine("A[p,p] and A[q,q] are analytically given and defined through recursion of their entries in the pre transformation matrix."); WriteLine("Assumning an even weighing from s^2 and c^2 tt is evident that A'[p,p]<A'[q,q] by their definitions within the algorithm."); WriteLine("\nAll comparisons are shown in the figure for a static matrix, along with the computation time of the sweep method for randomly generated matrices.\n"); WriteLine("\n To obtain the largest eigenvalue first instead of the lowest we use the analytical expression given for A'[p,p]=c^2A[p,p]-2scA[p,q]+s^2A[q,q] again. Since A'[p,p] for our given theta is minimal and is a composition of sin and cos functions and thus, have the periodicity of cos and sin functions we must conclude that a phaseshift of pi/2 will maximize the expression to yield the largest eigenvalue first and still diagonalize the matrix."); }
static void Main() { var rand = new Random(); // We can pull out new random numbers between 0 and 1 with rand.NextDouble() and // stuff it into a matrix int n = 4; matrix A = new matrix(n, n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { A[i, j] = 2 - 4 * rand.NextDouble(); } } WriteLine("Set up a random matrix, and try to find the inverse matrix via QR" + " decomposition.\n"); WriteLine("Printing random matrix A:"); A.print(); // Factorize the matrix into two matrices Q and R, and calculate the inverse matrix B var qrGS = new qrDecompositionGS(A); matrix B = qrGS.inverse(); matrix Q = qrGS.Q; matrix R = qrGS.R; WriteLine("\nPrinting matrix R:"); R.print(); WriteLine("\nPrinting matrix Q:"); Q.print(); WriteLine("\nPrinting matrix B (inverse to A):"); B.print(); // Let's check if B is actually the inverse by calculating A*B which then should // give the identity matrix matrix AB = A * B; WriteLine("\nPrinting matrix A*B:"); AB.print(); matrix I = new matrix(n, n); I.set_identity(); bool approx = AB.approx(I); if (approx == true) { WriteLine("A*B is approximately equal to the identity matrix."); } else { WriteLine("A*B is not approximately equal to the identity matrix."); } }
static void Main(string[] args) { int n = 5; matrix D = GenRandSymMatrix(n, n); matrix A = D.copy(); A.print($"Symmetric matrix A:"); (vector e, matrix V, int n1) = cyclic(D); for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { D[j, i] = D[i, j]; // complete the matrix } } matrix VTAV = V.transpose() * A * V; e.print($"Eigenvalues e:"); V.print($"Eigenvectors V:"); A.print($"D matrix: "); VTAV.print($"Matrix V^TAV:"); if (D.approx(VTAV)) { WriteLine("V^TAV = D, test passed."); } else { WriteLine("V^TAV != D, test failed."); } matrix B = A.copy(); (vector e2, _, int n2) = lowest_eigens(A, 3); (vector e4, _, int n4) = highest_eigens(A, 3); e.print($"\nEigenvalues from cyclic method found after {n1, 3} rotations: "); e2.print($"3 lowest eigenvalues from row-by-row method found after {n2, 3} rotations: "); e4.print($"3 highest eigenvalues from row-by-row method found after {n4, 3} rotations: "); (vector e3, _, int n3) = classic(B); e3.print($"Eigenvalues from the classic method found after {n3, 3} rotations: "); WriteLine(); }
static void Main() { // Testing that the jacobiAlgorithm works: int n = 5; vector e = new vector(n); matrix V = new matrix(n, n); matrix A = generateRandommatrix(n, n); matrix B = A.copy(); int sweeps = jacobi.jacobi_cyclic(A, e, V); WriteLine("---------Problem A1----------"); WriteLine("\nTesting that the Jacobi algorithm works as intended:"); A.print("\nRandom square matrix A:"); WriteLine("\nEigenvalue decomposition of A=V*D*V^T:"); (V.transpose() * B * V).print("\nD = V^T*A*V = "); e.print("\nListed eigenvalues:\t"); WriteLine($"\nNumber of sweeps required to diagonalize A: {sweeps}"); // Particle in a box n = 50; matrix H = Hamiltonian.boxHamilton(n); B = H.copy(); V = new matrix(n, n); e = new vector(n); sweeps = jacobi.jacobi_cyclic(H, e, V); WriteLine("\n---------Problem A2----------"); WriteLine("\nNow solving for a particle in a box."); WriteLine($"\nThe dimensions of the soon to be diagonalized Hamiltonian is {n}x{n}"); WriteLine("\nComparison of the calculated eigenvalues and the exact:\n"); WriteLine("n Calculated Exact:"); for (int k = 0; k < n / 3; k++) { double exact = PI * PI * (k + 1) * (k + 1); double calculated = e[k]; WriteLine($"{k} {calculated:f4}\t {exact:f4}"); } // plotting time StreamWriter solutions = new StreamWriter("SolutionsA.txt"); for (int i = 0; i < n; i++) { solutions.Write($"{i*(1.0/n)}"); for (int k = 0; k < 5; k++) { solutions.Write($" {V[i,k]}"); } solutions.Write("\n"); } solutions.Close(); }
}//randomMatrix static void randomTest(int n) { matrix a = randomSymmetricMatrix(n); a.print("A = "); diagJacobi test = new diagJacobi(a, eigVec: true); Write($"Number of sweeps: {test.sweeps}\n"); (((test.v).transpose()) * a * (test.v)).print("V^T*A*V = "); (test.l).print("eigenvalues = "); } //randomTest
}//randomMatrix static void randomTest(int n) { Write("==== Test of diagonalizatrion on random matrix A: ====\n"); matrix a = randomSymmetricMatrix(n); a.print("A = "); diagJacobi test = new diagJacobi(a, eigVec: true); Write("Performing Jacobi diagonalization:\n"); Write($"Number of sweeps: {test.sweeps}\n"); (((test.v).transpose()) * a * (test.v)).print("V^T * A * V = "); (test.l).print("eigenvalues = "); }//randomTest
public static void Main() { Write("==== B ====\n"); Write("Testing inverse by Gram-Schmidt in random matrix A:\n"); matrix a = rndMat.randomMatrix(5, 5); a.print("A = "); var qr = new qrDecompositionGS(a); Write("Inverse is found to be:\n"); (qr.inverse()).print("A^(-1) = "); Write("Checking that this is indeed the desired inverse matrix by calculating I=A^-1 * A:\n"); (qr.inverse() * a).print("A^(-1)*A = "); } //Main
public static void Main() { Write("==== A.1 ====\n"); Write("Testing on random tall matrix A:\n"); matrix a = rndMat.randomMatrix(5, 3); a.print("A ="); qrDecompositionGS qr = new qrDecompositionGS(a); Write("Q and R matrices found from QR-factorization:\n"); (qr.q).print("Q = ");; (qr.r).print("R = ");; Write("Testing that Q is orthogonal:\n"); (qr.q.transpose() * qr.q).print("Q^T*Q = "); Write("Calculating the difference between A and QR, which should be 0:\n"); (a - qr.q * qr.r).print("A-QR = "); Write("\n==== A.2 ====\n"); Write("Testing solver on random matrix A:\n"); a = rndMat.randomMatrix(5, 5); a.print("A = "); qr = new qrDecompositionGS(a); Write("and random vector b:\n"); vector b = rndMat.randomVector(5); b.print("b = "); Write("Solution to Ax=b is found to be:\n"); vector x = qr.solve(b); x.print("x = "); Write("Checking that x is a solution by calculating 0=Ax-b:\n"); vector diff = a * x - b; diff.print("A*x-b = "); } //Main
static void Main() { var rnd = new Random(1); int n = 4; var A = new matrix(n, n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { A[i, j] = 2 * (rnd.NextDouble() - 0.5); } } A.print("Matrix inverse.\nRandom square matrix A:"); var qra = new gramschmidt(A); var B = qra.inverse(); B.print("\nThe inverse of A, A^-1:"); var Id = new matrix(n, n); for (int i = 0; i < n; i++) { Id[i, i] = 1; } var C = A * B; C.print("\ncheck: A*A^-1:"); if (Id.approx(A * B)) { Write("\nA*A^-1 = Id, test passed\n"); } else { Write("\nA*A^-1 != Id, test failed\n"); } var D = B * A; D.print("\ncheck: A^-1*A="); if (Id.approx(B * A)) { Write("\nA^-1*A = I, test passed\n"); } else { Write("\nA^-1*A != I, test failed\n"); } }
public static void Main(string[] args) { int n = 5, max_print = 8; if (args.Length > 0) { n = int.Parse(args[0]); } Console.WriteLine("n={0}", n); var rnd = new Random(1); var a = new matrix(n, n); vector e = new vector(n); matrix v = new matrix(n, n); if (n > max_print) { for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { a[i, j] = 2 * (rnd.NextDouble() - 0.59); } } int r = jacobi.cyclic(a, e, v); Console.WriteLine("sweeps={0} e[n-1]={1}", r, e[n - 1]); return; } else { for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { a[i, j] = rnd.NextDouble(); a[j, i] = a[i, j]; } } Console.WriteLine("Eigenvalue Decomposition A=V*D*V^T"); a.print("Random matrix A:"); matrix b = a.copy(); int sweeps = jacobi.cyclic(a, e, v); System.Console.WriteLine("Number of sweeps={0}", sweeps); var p = (v.T * b * v); p.print("V^T*A*V (should be diagonal):"); e.print("Eigenvalues (should equal the diagonal elements above):\n"); } }