Example #1
0
    static void probC()
    {
        Write("Problem C:\n");
        var rand = new System.Random();
        int n    = 5 + rand.Next(3);

        matrix A = makeRandomMatrix(n, n);

        A.print("Make a random square matrix of random size: A = ");

        qr_givens decomp_givens = new qr_givens(A);
        qr_gs     decomp_gs     = new qr_gs(A);

        Write("\nDo the Givens decomposition and store the restult in the matrix G, which cotains the elements of the component R in the upper triangular part, and the angles for the Givens rotations in the relevant sub-diagonal entries. Compare with the R matrix found from the Gramm-Schmitt method:");
        decomp_givens.G.print("Givens G: ");
        decomp_gs.R.print("Gram-Schmitt R: ");
        Write("The upper triangular parts are the same.\n");

        vector b = makeRandomVector(n);

        b.print("\nMake a random vector b with the same size as A: b = ");

        Write("\nCompare using the Givens rotations to apply Q^(T) to b, and doing the explicit matrix multiplication with the Q matrix found by the Gramm-Schmitt method:\n");
        decomp_givens.applyQT(b).print("Givens: Q^(T)*b = ");
        (decomp_gs.Q.transpose() * b).print("GS: Q^(T)*b = ");

        vector x = decomp_givens.solve(b);

        x.print("\nSolution to A*x=b using Givens: ");
        (A * x - b).print("\nCheck solution satisfies A*x = b: A*x-b = ");

        Write("\nFind the inverse of A, and check that A^(-1)*A is the identity matrix:\n");
        decomp_givens.inverse().print("A^(-1)");
        (decomp_givens.inverse() * A).print("A^(-1)*A = ");
    }
Example #2
0
    static void probA2()
    {
        Write("\nProblem A2:\n");
        // Need to make the tests for A2
        var rand = new System.Random();

        // Remember, square matrix:
        Write("\nMake a random square matrix, A, with random dimmensions, and a random vector, b, of the same dimmension:\n");
        int    n = 2 + rand.Next(10);
        matrix A = makeRandomMatrix(n, n);
        vector b = makeRandomVector(n);

        A.print("A = ");
        b.print("b = ");

        qr_gs decomposer = new qr_gs(A);

        Write("\nSolve the system A*x = b for the vector x:\n");
        //decomposer.Q.print("Decomposition Q: ");
        //decomposer.R.print("Decomposition R: ");
        //(decomposer.Q.transpose()*b).print("Q^(T)*b = ");
        vector x = decomposer.solve(b);

        x.print("Solution: x = ");
        Write("\nCheck that the vector x satisfies A*x=b:\n");
        (A * x - b).print("A*x-b = ");
    }
Example #3
0
    static void probA1()
    {
        Write("Problem A1:\n");
        var rand = new System.Random();
        int m    = 2 + rand.Next(6);
        int n    = m + rand.Next(6);

        Write("First I create a random tall matrix with random dimmensions\n");
        matrix A = makeRandomMatrix(n, m);

        A.print("Random matrix A:");

        qr_gs decomposer = new qr_gs(A);

        Write("\nThen I decompose it into Q and R using the Gramm Schmitt method trough my qr_gs class:\n");
        matrix Q = decomposer.Q;
        matrix R = decomposer.R;

        Q.print("Decomposition Q = ");
        R.print("Decomposition R = ");

        Write("\nCheck that Q^(T)Q is equal to the identity matrix:\n");
        (Q.transpose() * Q).print("Q^(T)Q = ");
        Write("\nCheck that Q*R = A, or equivalently that Q*R-A is a matrix of all zeros:\n");
        (Q * R - A).print("Q*R-A");
    }
Example #4
0
    public static matrix calculateSigma(data dat, Func <double, double>[] fs)
    {
        matrix A      = calculateA(dat, fs);
        qr_gs  solver = new qr_gs(A.transpose() * A);
        matrix sigma  = solver.inverse();

        return(sigma);
    }
Example #5
0
    public static vector calculateC(data dat, Func <double, double>[] fs)
    {
        matrix A      = calculateA(dat, fs);
        vector b      = calculateb(dat);
        qr_gs  solver = new qr_gs(A);
        vector c      = solver.solve(b);

        return(c);
    }
Example #6
0
    static void probB()
    {
        Write("\nProblem B:\n");
        var    rand = new System.Random();
        int    n    = 3 + rand.Next(6);
        matrix A    = makeRandomMatrix(n, n);

        qr_gs decomposer = new qr_gs(A);

        matrix inverse = decomposer.inverse();

        Write("\nMake a random square matrix A, with a random size:\n");
        A.print("Random Matrix A: ");
        Write("\nCalculate the inverse of A:\n");
        inverse.print("Inverse of A: ");
        Write("\nCheck that A*A^(-1) is equal to the identity matrix:\n");
        (A * inverse).print("A*A^(-1): ");
    }