// Assignment B public matrix inverse() { int n = Q.size1, m = Q.size2; matrix A_inv = new matrix(n, m); vector e = new vector(n); var qr_B = new qrdecompositionGS(Q * R); for (int i = 0; i < n; i++) { e[i] = 1; A_inv[i] = qr_B.solve(e); e[i] = 0; } return(A_inv); }
public static int Main() { int n = 5; int m = 4; matrix A = new matrix(n, m); var rand = new Random(1); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { A[i, j] = 10 * (rand.NextDouble()); } } // A.1 WriteLine("Assignment A.1:"); A.print($"Matrix A {n}x{m}:"); var qr_A = new qrdecompositionGS(A); var Q = qr_A.Q; var R = qr_A.R; R.print($"The upper triangular matrix R:"); var qq = Q.transpose() * Q; qq.print($"Q^T Q =1: "); var qr = Q * R; qr.print($"Q*R=A: "); if (A.approx(Q * R)) { Write("Q*R=A, test passed\n"); } else { Write("Q*R!=A, test failed\n"); } WriteLine(""); // A.2 WriteLine("Assignment A.2:"); matrix A2 = new matrix(n, n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { A2[i, j] = 10 * (rand.NextDouble()); } } A2.print($"Matrix A {n}x{n}:"); var qr_A2 = new qrdecompositionGS(A2); vector b = new vector(n); for (int i = 0; i < n; i++) { b[i] = 10 * (rand.NextDouble()); } b.print($"Vector b with size {n}: "); var qrxb = qr_A2.solve(qr_A2.Q, b); qrxb.print($"solve x for QRx=b:\nx = "); var ax = A2 * qrxb; ax.print("Ax = "); WriteLine("which is equal to b.\n"); return(0); }