Beispiel #1
0
 public static vector newton(Func<vector, vector> f, vector x, double eps = 1e-3, double dx = 1e-7){
     int n = x.size;
     matrix J = new matrix(n,n);
     vector root = x.copy();
     vector f_r = f(root);
     vector z,fz;
     while(f_r.norm()>eps){
         for(int i = 0; i<n; i++){
             root[i] = root[i]+dx;
             vector df = f(root)-f_r;
             for(int j=0; j<n; j++){
                 J[i,j] = df[j]/dx;
             } // for
             root[i] -= dx;
         } // for
         QRdecompositionGS qr = new QRdecompositionGS(J);
         vector dr = qr.solve(-1*f_r);
         double lambda = 1.0;
         z = root+lambda*dr;
         fz = f(z);
         while(fz.norm()>(1-lambda/2)*f_r.norm()){
             double lambda_min = 1/64.0;
             if(lambda>lambda_min){
                 lambda = lambda/2;
                 fz = f(root+lambda*dr);
             } // if
             else{Error.WriteLine($"Bad step: lambda = {lambda_min}"); 
             break;
             } // else
         } // while
         root += lambda*dr;
         f_r = f(root);
     } // while
     return root;
 } // newton
Beispiel #2
0
    static void Main()
    {
        int n = 6;
        int m = 4;

        // Problem A1:
        WriteLine($"Problem A1: QR-factorization on a random {n}x{m} matrix A: \n");

        matrix A = generatematrix(n, m);

        // Printing our random matrix A
        A.print("Matrix A =");
        QRdecompositionGS decomp = new QRdecompositionGS(A);

        // Printing the orthogonal matrix Q
        matrix Q = decomp.Q;

        Q.print("\nDecomposed component  Q = ");

        // Printing the upper triangle matrix R
        matrix R = decomp.R;

        R.print("\nDecomposed component R = ");

        // Checking that Q^T*Q is equal to the identity
        (Q.transpose() * Q).print("\nQ^(T)*Q = ");

        // Checking that QR=A by substracting A from QR to yield the null matrix
        (Q * R - A).print("\n Q*R-A = ");

        n = 3;
        // Problem A2:
        WriteLine($"\nProblem A2: QR-factorization on a random {n}x{n} matrix A solving Ax=b: \n");
        vector b = generatevector(n);

        A      = generatematrix(n, n);
        decomp = new QRdecompositionGS(A);
        b.print("Vector b = ");
        A.print("\nSquare matrix A = ");
        vector x = decomp.solve(b);

        // Printing the solution to Ax=b
        x.print("\nVector x = ");
        WriteLine("\nChecking that x is indeed the solution: ");
        (A * x - b).print("\nA*x-b = ");


        // Problem B:
        A      = generatematrix(n, n);
        decomp = new QRdecompositionGS(A);
        WriteLine($"\nProblem B: Matrix inverse by Gram-Schmidt QR factorization \n");
        matrix B = decomp.inverse();

        B.print("A^(-1) = ");
        (A * B).print("\n A*A^(-1) = ");

        // Problem C:
    }
Beispiel #3
0
    public OrdLeastSquares(vector x, vector y, vector dy, Func <double, double>[] fs)
    {
        int    n = y.size;
        int    m = fs.Length;
        matrix A = new matrix(n, m);
        vector b = new vector(n);   // Creating the matrix and vector going into QRdecomp.

        for (int i = 0; i < n; i = i + 1)
        {
            b[i] = y[i] / dy[i];
            for (int j = 0; j < m; j = j + 1)
            {
                A[i, j] = fs[j](x[i]) / dy[i];
            }
        } // Matrix and vector ready for QRdecomp

        QRdecompositionGS QRdecomp = new QRdecompositionGS(A);

        c = QRdecomp.solve(b);
        matrix            sigmainv = QRdecomp.R.transpose() * QRdecomp.R;
        QRdecompositionGS cov      = new QRdecompositionGS(sigmainv);

        sigma = cov.inverse();
    }