示例#1
0
    public static void Main()
    {
        Write("==== B ====\n");
        Write("Testing inverse by Gram-Schmidt in random matrix A:\n");
        matrix a = rndMat.randomMatrix(5, 5);

        a.print("A = ");
        var qr = new qrDecompositionGS(a);

        Write("Inverse is found to be:\n");
        (qr.inverse()).print("A^(-1) = ");
        Write("Checking that this is indeed the desired inverse matrix by calculating I=A^-1 * A:\n");
        (qr.inverse() * a).print("A^(-1)*A = ");
    } //Main
示例#2
0
    void covariance()
    {
        matrix            sigmaInv = qr.r.transpose() * qr.r;
        qrDecompositionGS cov      = new qrDecompositionGS(sigmaInv);

        sigma = cov.inverse();
    }
示例#3
0
    public lsfit(vector x, vector y, vector dy, Func <double, double>[] fs)
    {
        int n = x.size;
        int m = fs.Length;

        // We move the functions into the variable f
        f = fs;

        matrix A = new matrix(n, m);
        vector b = new vector(n);

        for (int i = 0; i < n; i++)
        {
            b[i] = y[i] / dy[i];
            for (int j = 0; j < m; j++)
            {
                A[i, j] = f[j](x[i]) / dy[i];
            }
        }

        // Solve the system of equations
        var qrGS = new qrDecompositionGS(A);

        c = qrGS.solve(b);

        // At last we calculate the covariance matrix, A^T * A
        matrix Ainv = qrGS.inverse();

        sigma = Ainv * Ainv.T;
    }
示例#4
0
文件: main.cs 项目: Henrik-AU/ppnm
    static void Main()
    {
        var rand = new Random();

        // We can pull out new random numbers between 0 and 1 with rand.NextDouble() and
        // stuff it into a matrix
        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("Set up a random matrix, and try to find the inverse matrix via QR" +
                  " decomposition.\n");
        WriteLine("Printing random matrix A:");
        A.print();

        // Factorize the matrix into two matrices Q and R, and calculate the inverse matrix B
        var    qrGS = new qrDecompositionGS(A);
        matrix B    = qrGS.inverse();
        matrix Q    = qrGS.Q;
        matrix R    = qrGS.R;

        WriteLine("\nPrinting matrix R:");
        R.print();

        WriteLine("\nPrinting matrix Q:");
        Q.print();

        WriteLine("\nPrinting matrix B (inverse to A):");
        B.print();

        // Let's check if B is actually the inverse by calculating A*B which then should
        // give the identity matrix
        matrix AB = A * B;

        WriteLine("\nPrinting matrix A*B:");
        AB.print();

        matrix I = new matrix(n, n);

        I.set_identity();
        bool approx = AB.approx(I);

        if (approx == true)
        {
            WriteLine("A*B is approximately equal to the identity matrix.");
        }
        else
        {
            WriteLine("A*B is not approximately equal to the identity matrix.");
        }
    }
示例#5
0
文件: newton.cs 项目: Henrik-AU/ppnm
    public static vector newton(Func <vector, vector> f, vector x, double epsilon = 1e-3,
                                double dx = 1e-7)
    {
        int    n  = x.size;
        vector fx = f(x);
        vector x1;
        vector fx1;

        // Run a loop to find the roots
        while (true)
        {
            // Create the Jacobian
            matrix J = jacobian(f, x, fx);

            // Run a QR-decomposition algorithm on J, and find it's inverse
            var    qr = new qrDecompositionGS(J);
            matrix B  = qr.inverse();

            // Find the Newton stepsize
            vector deltaX = -B * fx;

            // Lambda factor to take a more conservative step
            double lambda = 1;

            // Backtracking linesearch algorithm
            while (true)
            {
                // Attempt a step and check if we arrive at a point closer to a root
                x1  = x + deltaX * lambda;
                fx1 = f(x1);
                if (fx1.norm() < fx.norm() * (1 - lambda / 2))
                {
                    // This should be a good step, so we accept it by breaking
                    // the loop.
                    break;
                }
                if (lambda < 1.0 / 64)
                {
                    // This is not a good step, but we surrender and accept it
                    // anyway, hoping that the next step will be better.
                    break;
                }

                // Reduce lambda by a factor of 2 before next attempt
                lambda /= 2;
            }

            // Move us to the newfound better positon before attempting the next step
            x  = x1;
            fx = fx1;

            // Check if we have converged to a value closer to zero than the given
            // accuracy epsilon. If yes, we are done, so we break the loop and return
            // the position of the root, x.
            if (fx.norm() < epsilon)
            {
                break;
            }
        }
        return(x);
    }     // end newton