public lsfit(vector x, vector y, vector dy, Func <double, double>[] fs) { f = fs; int n = x.size, m = fs.Length; // Console.WriteLine($"n: {n} m: {m}\n"); var A = new matrix(n, m); var b = new vector(n); for (int i = 0; i < n; i++) { b[i] = y[i] / dy[i]; // Equation (7) in chapter for (int k = 0; k < m; k++) { A[i, k] = f[k](x[i]) / dy[i]; // Equation (7) in chapter } } var q = new gramschmidt(A); // We use the Gram-Schmidt routine to decompose A c = q.solve(b); // We solve the system var ai = q.inverse(); sigma = ai * ai.T; // equation (14) in chapter to determine the covariance matrix }
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"); } }
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("Solution of a linear system Ax=b:\nRandom square matrix A:"); var b = new vector(n); for (int i = 0; i < n; i++) { b[i] = 2 * (rnd.NextDouble() - 0.5); } b.print("random right-hand-side vector b:\n"); var qra = new gramschmidt(A); var x = qra.solve(b); x.print("solution x to equation Ax=b:\n"); var C = A * x; C.print("check: A*x=\n"); if (b.approx(A * x)) { Write("A*x=b, test passed\n"); } else { Write("A*x!=b, test failed\n"); } }
static void Main() { int n = 4, m = 3; matrix A = new matrix(n, m); /* Construction of a random matrix A */ var rnd = new Random(1); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { A[i, j] = 2 * (rnd.NextDouble() - 0.5); } } A.print($"QR-decomposition:\nrandom {n}x{m} matrix A:"); var qra = new gramschmidt(A); var Q = qra.Q; var R = qra.R; Q.print("matrix Q:"); R.print("matrix R:"); matrix qtq = (Q.T * Q); qtq.print("Q^T*Q:"); matrix qr = (Q * R); qr.print("Q*R:"); if (A.approx(Q * R)) { Write("Q*R=A, test passed\n"); } else { Write("Q*R!=A, test failed\n"); } }