public lsfit(vector x, vector y, vector yerr, Func <double, double>[] fs) { int dsize = x.size; int nf = fs.Length; matrix A = new matrix(dsize, nf); vector b = new vector(dsize); fun = fs; for (int i = 0; i < dsize; i++) { b[i] = y[i] / yerr[i]; for (int k = 0; k < nf; k++) { A[i, k] = fs[k](x[i]) / yerr[i]; } } qr_decomp_GS decomp = new qr_decomp_GS(A); q = decomp.Q; r = decomp.R; c = decomp.solve(b); qr_decomp_GS decomp_covar = new qr_decomp_GS(A.T * A); covar = decomp_covar.inverse(); cerr = new vector(covar.size1); for (int i = 0; i < covar.size1; i++) { cerr[i] = Sqrt(covar[i, i]); } }
static void A2() { WriteLine("Problem A2"); var rand = new System.Random(); int n = 2 + rand.Next(20); matrix A = make_random_matrix(n, n); vector b = make_random_vector(n); WriteLine("Random square matrix A:"); A.print("A = "); WriteLine("Random vector b:"); b.print("b = "); qr_decomp_GS decomposition = new qr_decomp_GS(A); vector x = decomposition.solve(b); x.print("Solution x = "); (A * x - b).print("A*x-b = "); }