Пример #1
0
    static void Main()
    {
        int    n    = 4;
        int    m    = 4;
        var    rand = new Random();
        matrix A    = new matrix(n, m);
        vector b    = new vector(n);

        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                A[i, j] = 10 * rand.NextDouble();
            }
            b[i] = 10 * rand.NextDouble();
        }
        WriteLine("Givens rotations.");
        A.print("Random square matrix A:");
        b.print("Random vector b:\n");

        var S = new givens(A);         /* decomposition via Givens rotations*/

        WriteLine("The Givens rotation decomposition is stored in the matrix called 'S' (so as not to confuse it with the typical notation for the other relevant matrices), which contains the elements of the component R in the upper triangular part and the angles for the rotations in the relevant sub-diagonal entries.");
        S.G.print("Matrix S:");

        vector x = S.solve(b);

        x.print("Solution to A*x=b using Givens rotations:\n");

        Write("Checking that A^(-1)*A is the identity matrix:\n");
        (S.inverse() * A).printfloat("A^(-1)*A:");

        var C = A * x;

        C.print("Checking that A*x=b. A*x:\n");
        if (b.approx(A * x))
        {
            Write("A*x=b, test passed\n");
        }
        else
        {
            Write("A*x!=b, test failed\n");
        }
    }
Пример #2
0
    static void Main()
    {
        WriteLine("Setting up a random system of linear equations, Ax=b:");

        // We construct a random square matrix
        var    rand = new Random();
        int    n    = 4;
        matrix A    = new matrix(n, n);

        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                A[i, j] = 2 - 4 * rand.NextDouble();
            }
        }

        WriteLine("Printing random matrix A:");
        A.print();

        // Let's create a random vector b also, that we can use as the right side in Ax=b.
        vector b = new vector(n);

        for (int i = 0; i < n; i++)
        {
            b[i] = 3 * rand.NextDouble() - 1.5;
        }
        WriteLine("\nPrinting random vector b:");
        b.print();

        // We can now attempt to solve for x using back substitution with the Givens rotations
        WriteLine("\nAttept to solve by the use of Givens rotations:");

        // Next we try to factorize A using the Givens algorithm - this gives us a matrix R,
        // which is "upper triangular", but with the Givens angles stored in the lower part
        var    givens = new givens(A);
        matrix R      = givens.R;

        WriteLine("\nPrinting calculated matrix R (with Givens angles in the left" +
                  " triangular part):");
        R.print();

        vector x = givens.solve(b);

        WriteLine("\nThe calculated x-vector is:");
        x.print();

        WriteLine("\nLet's check if this x-vector actually solves the set of linear equations:");
        vector Ax = A * x;

        WriteLine("\nA*x gives:");
        Ax.print();

        bool approx = Ax.approx(b);

        if (approx == true)
        {
            WriteLine("A*x is approximately equal to b. The system is solved.");
        }
        else
        {
            WriteLine("A*x is not approximately equal to b. The system is not solved.");
        }
    }
Пример #3
0
    static void Main()
    {
        WriteLine("_____________________________________");
        WriteLine("Part C1 - decomposition of random matrix A");

        int    n    = 5;
        matrix A    = new matrix(n, n);
        var    rand = new Random(1);

        // make random matrix A
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                A[i, j] = 2 * (rand.NextDouble() - 0.5);
            }
        }


        vector b = new vector(n);

        // make random vector b
        for (int i = 0; i < n; i++)
        {
            b[i] = 2 * (rand.NextDouble() - 0.5);
        }

        // make the QR decomposition through givens-rotation
        var qr1 = new givens(A);
        // Matrix with R in the right diagonal and givens-angels in the lower sub-diagonal
        var G = qr1.G;

        // make the QR decomposition through GS
        var qr2 = new gs(A);
        var R   = qr2.R;



        A.print("Matrix A="); WriteLine();
        WriteLine("Upper triangular part of matrix G contains the elements of R.");
        WriteLine("The enteries below the diagonal of G contains givens-rotation angles.");
        G.print("Matrix G="); WriteLine();
        R.print("Matrix R="); WriteLine();
        WriteLine("The upper triangular part of G should match the elements in R achived through GS.");

        //Filter out the lower part of G
        matrix R2 = G.copy();

        for (int j = 0; j < G.size2; j++)
        {
            for (int i = j + 1; i < G.size1; i++)
            {
                R2[i, j] = 0;
            }
        }

        if (R2.approx(R))
        {
            WriteLine("Upper triangular part identical - TEST PASSED");
        }
        else
        {
            WriteLine("Upper triangular part not identical - TEST FAILED");
        }

        WriteLine("_____________________________________");
        WriteLine("Part C2 - solve the linear equations of A*x=b"); WriteLine();

        // solve the equation A*x=b for vector x
        var x = qr1.solve(b);

        b.print("Vector b="); WriteLine();
        x.print("Solution x="); WriteLine();

        vector Ax = (A * x);

        Ax.print("Check A*x=");
        if (b.approx(Ax))
        {
            WriteLine("A*x=b  -  TEST PASSED");
        }
        else
        {
            WriteLine("A*x!=b  -  TEST FAILED");
        }
    } //Method: Main
Пример #4
0
    static void Main(string[] args)
    {
        int n = 3, m = 3;

        // QR DECOMPOSITION
        writetitle("\nQR decomposition");
        matrix A = GenRandMatrix(n, m);
        matrix R = new matrix(m, m);
        matrix Q = A.copy();

        A.print($"QR-decomposition of the matrix A:");
        gs.decomp(Q, R);
        R.print($"Matrix R:");
        var QQT = Q.T * Q;    // does not work within parantheses
        var QR  = Q * R;      // same

        QQT.print("Q^T Q:");
        QR.print("QR:");
        if (A.approx(QR))
        {
            writepass("QR = A, test passed.");
        }
        else
        {
            writefail("QR != A, test failed.");
        }

        // SOLVING
        writetitle("\nQR solving");
        A = GenRandMatrix(n, m);
        vector b = GenRandVector(m);

        R = new matrix(m, m);
        //A[0, 0] = 1; A[0, 1] = 1; A[0, 2] = 1;
        //A[1, 0] = 0; A[1, 1] = 2; A[1, 2] = 5;
        //A[2, 0] = 2; A[2, 1] = 5; A[2, 2] = -1;
        //vector b = new vector(6, -4, 27);
        A.print($"Solving the equation Ax = b, with A:");
        b.print($"and b:\n");
        Q = A.copy();
        gs.decomp(Q, R);
        vector x = gs.solve(Q, R, b);

        x.print($"\nSolution is x:\n");
        var Ax = A * x;

        Ax.print($"Ax:\n");
        if (b.approx(Ax))
        {
            writepass("Ax = Q^T b, test passed.");
        }
        else
        {
            writefail("Ax != Q^T b, test failed.");
        }

        // INVERSE
        writetitle("\nQR inverse");
        A = GenRandMatrix(n, m);
        R = new matrix(m, m);
        Q = A.copy();
        gs.decomp(Q, R);
        matrix B = gs.inverse(Q, R);         // B = A^-1

        A.print($"Solving for the inverse of A:");
        B.print($"Inverse is B:");
        matrix AB = A * B;

        AB.print($"AB:");
        matrix I = new matrix(n, m);

        for (int i = 0; i < n; i++)
        {
            I[i, i] = 1;
        }
        if (AB.approx(I))
        {
            writepass("AB = I, test passed.");
        }
        else
        {
            writefail("AB != I, test failed.");
        }

        // GIVENS
        writetitle("\nGivens rotations");
        A = GenRandMatrix(n, m);
        b = GenRandVector(m);
        A.print($"Solving the equation Ax = b, with A:");
        b.print($"and b:\n");
        givens decomp = new givens(A);

        x = decomp.solve(b);
        x.print("\nSolution is x:\n");
        Ax = A * x;
        Ax.print("Ax:\n");
        if (Ax.approx(b))
        {
            writepass("Ax = b, test passed.");
        }
        else
        {
            writefail("Ax != b, test failed.");
        }
    }