Exemple #1
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:
    }
Exemple #2
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();
    }