public lsfit(vector x, vector y, vector dy, Func <double, double>[] fs) { int n = x.size; int m = fs.Length; // We move the functions into the variable f f = fs; matrix A = new matrix(n, m); vector b = new vector(n); for (int i = 0; i < n; i++) { b[i] = y[i] / dy[i]; for (int j = 0; j < m; j++) { A[i, j] = f[j](x[i]) / dy[i]; } } // Solve the system of equations var qrGS = new qrDecompositionGS(A); c = qrGS.solve(b); // At last we calculate the covariance matrix, A^T * A matrix Ainv = qrGS.inverse(); sigma = Ainv * Ainv.T; }
public ols(Func <double, double>[] fs, vector x, vector y, vector dy) { int m = fs.Length; int n = y.size; matrix a = new matrix(n, m); vector b = new vector(n); for (int i = 0; i < n; i++) { b[i] = y[i] / dy[i]; for (int k = 0; k < m; k++) { a[i, k] = fs[k](x[i]) / dy[i]; } } // matrices and vector for qr-demposition qr = new qrDecompositionGS(a); c = qr.solve(b); } //constructor
public static vector newton(Func <vector, vector> f, vector x, double epsilon = 1e-3, double dx = 1e-7) { vector root = x; vector step; do { /* calculate jacobian */ vector fx = f(root); int i = fx.size; int k = root.size; /* Add assertion of i!=k (only square J) */ matrix jacobian = new matrix(i, k); for (int r = 0; r < i; r++) { for (int c = 0; c < k; c++) { vector deltax = root.copy(); deltax[c] += dx; jacobian[r, c] = (f(deltax)[r] - f(root)[r]) / dx; } } /* solve J * deltax = - f(x) with QR-factorization */ qrDecompositionGS qr = new qrDecompositionGS(jacobian); step = qr.solve(-1 * fx); /* Do line search and choose lambda when (8) is fulfilled. */ double lambda = 1; while (f(root + lambda * step).norm() > (1 - lambda / 2) * fx.norm() && lambda > 1 / 64) { lambda /= 2; } root += lambda * step; } while(f(x).norm() > epsilon && step.norm() > dx); return(root); }
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() { // Part 1 of exercise A WriteLine("-------Part 1 of exercise A:-------\n"); // We start out by creating a random matrix. 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 nA = 5; int mA = 4; matrix A = new matrix(nA, mA); for (int i = 0; i < nA; i++) { for (int j = 0; j < mA; j++) { A[i, j] = 2 - 4 * rand.NextDouble(); } } // Print A WriteLine("We will setup a random tall matrix and perform QR factorization on it."); WriteLine("Printing random tall matrix A:"); A.print(); // We can now try to factorize the matrix A into the two matrĂces Q and R var qrGSA = new qrDecompositionGS(A); matrix Q = qrGSA.Q; matrix R = qrGSA.R; // Let's check that R is upper triangular WriteLine("\nPrinting matrix R:"); R.print(); WriteLine("\nPrinting matrix Q:"); Q.print(); // Checking that Q^T * Q = 1 WriteLine("\nPrinting (Q^T)*Q:"); matrix QTQ = Q.transpose() * Q; QTQ.print(); // Let's check if (Q^T)*Q is approximately the identity matrix matrix I = new matrix(mA, mA); I.set_identity(); bool approx = QTQ.approx(I); if (approx == true) { WriteLine("(Q^T)*Q is approximately equal to the identity matrix."); } else { WriteLine("(Q^T)*Q is not approximately equal to the identity matrix."); } // Checking that Q*R=A, or in other words that A-Q*R = 0 WriteLine("\nPrinting out A-Q*R:"); matrix AmQR = A - Q * R; AmQR.print(); // Let's check if Q*R is approximately equal to A, or that A-QR is approx. the zero // matrix matrix nullMatrix = new matrix(AmQR.size1, AmQR.size2); nullMatrix.set_zero(); approx = AmQR.approx(nullMatrix); if (approx == true) { WriteLine("QR is thus approximately equal to A."); } else { WriteLine("QR is thus not approximately equal to A."); } // Part 2 of exercise A WriteLine("\n\n-------Part 2 of exercise A:-------\n"); // At first we create a random square matrix - let's call it C now to avoid confusion // with part 1 above WriteLine("Setting up system C*x = b."); int nC = 4; matrix C = new matrix(nC, nC); for (int i = 0; i < nC; i++) { for (int j = 0; j < nC; j++) { C[i, j] = 2 - 4 * rand.NextDouble(); } } WriteLine("Printing matrix C:"); C.print(); // Next we generate a vector b of a length corresponding to the dimension of the // square matrix vector b = new vector(nC); for (int i = 0; i < nC; i++) { b[i] = 3 * rand.NextDouble() - 1.5; } WriteLine("\nPrinting out b:"); b.print(); // Once again we factorize our matrix into two matrices Q and R var qrGSC = new qrDecompositionGS(C); Q = qrGSC.Q; R = qrGSC.R; WriteLine("\nSolving by QR decomposition and back-substitution."); vector x = qrGSC.solve(b); WriteLine("\nThe obtained x-vector from the routine is:"); x.print(); WriteLine("\nPrinting vector C*x :"); vector Cx = C * x; Cx.print(); // Let's check if C*x is approximately equal to b approx = Cx.approx(b); if (approx == true) { WriteLine("Cx is approximately equal to b. The system has been solved."); } else { WriteLine("Cx is not approximately equal to b. The system has not been solved."); } }