public static int solverTest(double eps) { int success = 0; Write("\n ===== Solver Test =====\n"); Write("Performed by calculating the difference: Ax - b = 0. \n"); for (int i = 2; i < 6; i++) { matrix A = rndMat.randomMatrix(i, i); vector b = rndMat.randomVector(i); var gkl = new bidiag(A); vector diff = A * gkl.solver(b) - b; for (int m = 0; m < i; m++) { if (Abs(diff[m]) > eps) { success = 1; } } } if (success == 0) { Write("Solver test PASSED!\n"); } else { Write("Solver test FAILED!\n"); } return(success); }//solverTest
}//solverTest public static int inverseTest(double eps) { int success = 0; Write("\n ===== Inverse Test =====\n"); Write("Performed by calculating the difference: A*A^-1 - I = 0. \n"); for (int i = 2; i < 6; i++) { matrix A = rndMat.randomMatrix(i, i); matrix I = new matrix(i, i); I.set_identity(); var gkl = new bidiag(A); matrix diff = A * gkl.inverse() - I; for (int m = 0; m < i; m++) { for (int n = 0; n < i; n++) { if (Abs(diff[m, n]) > eps) { success = 1; } } } } if (success == 0) { Write("Inverse test PASSED!\n"); } else { Write("Inverse test FAILED!\n"); } return(success); }//inverseTest
}//inverseTest public static int differenceTest(double eps) { Write("\n ===== Difference Test =====\n"); Write("Performed by calculating the difference: A - U*B*V^T = 0. \n"); int successSquare = 0; for (int i = 2; i < 6; i++) { matrix A = rndMat.randomMatrix(i, i); var gkl = new bidiag(A); matrix diff = A - gkl.U * gkl.B * gkl.V.transpose(); for (int m = 0; m < i; m++) { for (int n = 0; n < i; n++) { if (Abs(diff[m, n]) > eps) { successSquare = 1; } } } } if (successSquare == 0) { Write("Square matrix difference test PASSED!\n"); } else { Write("Square matrix difference test FAILED!\n"); } int successTall = 0; for (int i = 3; i < 6; i++) { for (int j = 2; j < i; j++) { matrix A = rndMat.randomMatrix(i, j); var gkl = new bidiag(A); matrix diff = A - gkl.U * gkl.B * gkl.V.transpose(); for (int m = 0; m < i; m++) { for (int n = 0; n < j; n++) { if (Abs(diff[m, n]) > eps) { successTall = 1; } } } } } if (successTall == 0) { Write("Tall matrix difference test PASSED!\n"); } else { Write("Tall matrix difference test FAILED!\n"); } return(successSquare + successTall); } //differenceTest
public static void Main() { matrix A = rndMat.randomMatrix(5, 3); Write("Example of Golub-Kahan-Lanczos bidiagonalization on tall matrix:\n"); Write("The matrix A is written as A = UBV^T. Example for random A given as:\n"); A.print("A ="); var gkl = new bidiag(A); Write("The found matrix U and V are: \n"); gkl.U.print("\n U = "); gkl.V.print("\n V = "); matrix B_calc = gkl.U.transpose() * A * gkl.V; Write("The bidiogonal matrix calculated via U and V, and directly from the method is given below: \n"); B_calc.print("\n U^T * A * V = "); gkl.B.print("\n B = "); Write("The difference between A and the representation of A through B:\n"); (A - gkl.U * gkl.B * gkl.V.transpose()).print("\n 0 = A - U*B*V^T ="); Write("\n Use bidiagonalization to solve Ax=b equation for square random matrix matrix A:\n"); A = rndMat.randomMatrix(3, 3); A.print("A = "); Write("With random vector b:\n"); vector b = rndMat.randomVector(3); b.print("b ="); gkl = new bidiag(A); vector x = gkl.solver(b); Write("Solution to equation:\n"); x.print("x ="); vector diff = A * x - b; Write("Check if solution is correct: \n"); diff.print("0 = A*x - b ="); Write("\n Determinant (found as product of diagonal of B) and inverse:\n"); Write($"|Det(A)| = {gkl.determinant()}\n"); gkl.inverse().print("A^-1 = "); Write("Check if inverse is correct: \n"); (gkl.inverse() * A).print("1 = A^-1 * A = "); int success = 0; double eps = 1e-7; success = tests.differenceTest(eps); success += tests.solverTest(eps); success += tests.inverseTest(eps); Write("\n ===== Tests result =====\n"); if (success == 0) { Write("All test PASSED! \n"); } else { Write("One or several test FAILED! \n"); } } //Main