// The following method performs the inverse iteration method on a real symmetric matrix A public static int inverse_iteration(matrix A, ref double s, ref vector v, double tau = 1e-6, double eps = 1e-6, int n_max = 999, int updates = 999) { int n = 0; int m = 0; matrix As; matrix I = new matrix(A.size1, A.size1); I.set_identity(); v = v / v.norm(); As = A - s * I; qr As_QR = new qr(As); double abs = 0; double rel = 0; while (converge(v, A, s, tau, eps, ref abs, ref rel) && n < n_max) { v = As_QR.solve(v); v = v / v.norm(); s = v.dot(A * v); if (m > updates) // Update QR decomposition if Rayleigh updates are used (if updates<999) { m = 0; s = v.dot(A * v) / (v.dot(v)); As = A - s * I; As_QR = new qr(As); } n++; m++; } s = v.dot(A * v) / (v.norm() * v.norm()); v = v / v.norm(); return(n); }
public static void generate_errors(qr As_QR, ref matrix A, ref matrix I, ref List <double> errors, ref List <double> errors_s, double s, int updates, double e_J, vector v_0, double tau = 1e-6, double eps = 1e-6, double n_max = 999) { int n = 0; int m = 0; matrix As; vector u; vector v; u = v_0 / v_0.norm(); double abs = 0; double rel = 0; while (converge(u, A, s, tau, eps, ref abs, ref rel) && n < n_max) { v = As_QR.solve(u); u = v / v.norm(); s = u.dot(A * u); if (m > updates) { m = 0; s = u.dot(A * u) / (u.dot(u)); As = A - s * I; As_QR = new qr(As); } n++; m++; errors.Add(rel); errors_s.Add(Abs(s - e_J)); } errors.Add(rel); errors_s.Add(Abs(s - e_J)); s = u.dot(A * u) / (u.norm() * u.norm()); }
public static vector newton(Func <vector, vector> f, vector x, double eps = 1e-3, double dx = 1e-7) { matrix J = jacobian(f, x, dx); qr Jqr = new qr(J); vector deltax = Jqr.solve(-1.0 * f(x)); double a = 1; // while((f(x) + a*deltax).norm() < (1-a/2)*f(x).norm() && a>1/64){a = a/2;} while ((f(x + a * deltax).norm() > (1 - a / 2.0) * f(x).norm()) && a > 1.0 / 64) { a = a / 2.0; } x += a * deltax; if (deltax.norm() < dx) { return(x); } else if (f(x).norm() < eps) { return(x); } else { return(newton(f, x, eps, dx)); } }
public lsq_qr(double[] x, double[] y, double[] dy, Func <double, double>[] F) { A = new matrix(x.Length, F.Length); for (int i = 0; i < F.Length; i++) { for (int j = 0; j < x.Length; j++) { A[i][j] = F[i](x[j]) / dy[j]; // Row/column convention interchanged } } vector b = new vector(x.Length); for (int i = 0; i < x.Length; i++) { b[i] = y[i] / dy[i]; } qr AQR = new qr(A); c = AQR.solve(b); }
public static int Main() { int n = 5; int m = 3; // Row and column dimensions matrix A = misc.random_matrix(n, m); var data = new qr(A); // Instance of qr decomposition class of matrix A matrix Q = data.Q; matrix R = data.R; matrix QTQ = Q.transpose() * Q; matrix QR = Q * R; // Output var outfile = new System.IO.StreamWriter("../out_A.txt", append: false); outfile.WriteLine($"--------------------------------"); outfile.WriteLine($"1: QR decomposition"); outfile.WriteLine($"--------------------------------"); outfile.WriteLine($"Random tall matrix A:"); for (int ir = 0; ir < A.size1; ir++) { for (int ic = 0; ic < A.size2; ic++) { outfile.Write("{0,10:g3} ", A[ir, ic]); } outfile.WriteLine(""); } outfile.WriteLine(""); outfile.WriteLine($"Upper triangular matrix R:"); for (int ir = 0; ir < R.size1; ir++) { for (int ic = 0; ic < R.size2; ic++) { outfile.Write("{0,10:g3} ", R[ir, ic]); } outfile.WriteLine(""); } outfile.WriteLine(""); outfile.WriteLine($"Q.transpose()*Q:"); for (int ir = 0; ir < QTQ.size1; ir++) { for (int ic = 0; ic < QTQ.size2; ic++) { outfile.Write("{0,10:g3} ", QTQ[ir, ic]); } outfile.WriteLine(""); } outfile.WriteLine(""); outfile.WriteLine($"Q*R:"); for (int ir = 0; ir < QR.size1; ir++) { for (int ic = 0; ic < QR.size2; ic++) { outfile.Write("{0,10:g3} ", QR[ir, ic]); } outfile.WriteLine(""); } outfile.WriteLine(""); n = 5; m = 5; A = misc.random_matrix(n, m); vector b = misc.gen_vector(n); data = new qr(A); vector x = new vector(n); x = data.solve(b); vector B = A * x; outfile.WriteLine($"--------------------------------"); outfile.WriteLine($"2: Linear equations"); outfile.WriteLine($"--------------------------------"); outfile.WriteLine($"Random square matrix A:"); for (int ir = 0; ir < QR.size1; ir++) { for (int ic = 0; ic < QR.size2; ic++) { outfile.Write("{0,10:g3} ", QR[ir, ic]); } outfile.WriteLine(""); } outfile.WriteLine($"Random vector b:"); for (int ir = 0; ir < b.size; ir++) { outfile.Write("{0,10:g3} ", b[ir]); } outfile.WriteLine(""); outfile.WriteLine($"A*x:"); for (int ir = 0; ir < B.size; ir++) { outfile.Write("{0,10:g3} ", B[ir]); } outfile.WriteLine(""); outfile.Close(); return(0); }