コード例 #1
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;
    }
コード例 #2
0
    void covariance()
    {
        matrix            sigmaInv = qr.r.transpose() * qr.r;
        qrDecompositionGS cov      = new qrDecompositionGS(sigmaInv);

        sigma = cov.inverse();
    }
コード例 #3
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.");
        }
    }
コード例 #4
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
コード例 #5
0
    public ols(Func <double, double>[] fs, vector x, vector y, vector dy)
    {
        int    m = fs.Length;
        int    n = y.size;
        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 k = 0; k < m; k++)
            {
                a[i, k] = fs[k](x[i]) / dy[i];
            }
        } // matrices and vector for qr-demposition
        qr = new qrDecompositionGS(a);
        c  = qr.solve(b);
    } //constructor
コード例 #6
0
ファイル: newton.cs プロジェクト: nronne/ppnm
    public static vector newton(Func <vector, vector> f, vector x, double epsilon = 1e-3, double dx = 1e-7)
    {
        vector root = x;
        vector step;

        do
        {
            /* calculate jacobian */
            vector fx = f(root);
            int    i  = fx.size;
            int    k  = root.size;
            /* Add assertion of i!=k (only square J) */
            matrix jacobian = new matrix(i, k);
            for (int r = 0; r < i; r++)
            {
                for (int c = 0; c < k; c++)
                {
                    vector deltax = root.copy();
                    deltax[c]     += dx;
                    jacobian[r, c] = (f(deltax)[r] - f(root)[r]) / dx;
                }
            }

            /* solve J * deltax = - f(x) with QR-factorization */
            qrDecompositionGS qr = new qrDecompositionGS(jacobian);
            step = qr.solve(-1 * fx);

            /* Do line search and choose lambda when (8) is fulfilled. */
            double lambda = 1;
            while (f(root + lambda * step).norm() > (1 - lambda / 2) * fx.norm() && lambda > 1 / 64)
            {
                lambda /= 2;
            }
            root += lambda * step;
        } while(f(x).norm() > epsilon && step.norm() > dx);

        return(root);
    }
コード例 #7
0
ファイル: main.cs プロジェクト: nronne/ppnm
    public static void Main()
    {
        Write("==== A.1 ====\n");
        Write("Testing on random tall matrix A:\n");
        matrix a = rndMat.randomMatrix(5, 3);

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

        Write("Q and R matrices found from QR-factorization:\n");
        (qr.q).print("Q = ");;
        (qr.r).print("R = ");;
        Write("Testing that Q is orthogonal:\n");
        (qr.q.transpose() * qr.q).print("Q^T*Q = ");
        Write("Calculating the difference between A and QR, which should be 0:\n");
        (a - qr.q * qr.r).print("A-QR = ");


        Write("\n==== A.2 ====\n");
        Write("Testing solver on random matrix A:\n");
        a = rndMat.randomMatrix(5, 5);
        a.print("A = ");
        qr = new qrDecompositionGS(a);
        Write("and random vector b:\n");
        vector b = rndMat.randomVector(5);

        b.print("b = ");
        Write("Solution to Ax=b is found to be:\n");
        vector x = qr.solve(b);

        x.print("x = ");
        Write("Checking that x is a solution by calculating 0=Ax-b:\n");
        vector diff = a * x - b;

        diff.print("A*x-b = ");
    } //Main
コード例 #8
0
    static void Main()
    {
        // Part 1 of exercise A
        WriteLine("-------Part 1 of exercise A:-------\n");
        // We start out by creating a random matrix.
        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    nA = 5;
        int    mA = 4;
        matrix A  = new matrix(nA, mA);

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

        // Print A
        WriteLine("We will setup a random tall matrix and perform QR factorization on it.");
        WriteLine("Printing random tall matrix A:");
        A.print();

        // We can now try to factorize the matrix A into the two matríces Q and R
        var    qrGSA = new qrDecompositionGS(A);
        matrix Q     = qrGSA.Q;
        matrix R     = qrGSA.R;

        // Let's check that R is upper triangular
        WriteLine("\nPrinting matrix R:");
        R.print();


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

        // Checking that Q^T * Q = 1
        WriteLine("\nPrinting (Q^T)*Q:");
        matrix QTQ = Q.transpose() * Q;

        QTQ.print();

        // Let's check if (Q^T)*Q is approximately the identity matrix
        matrix I = new matrix(mA, mA);

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

        if (approx == true)
        {
            WriteLine("(Q^T)*Q is approximately equal to the identity matrix.");
        }
        else
        {
            WriteLine("(Q^T)*Q is not approximately equal to the identity matrix.");
        }


        // Checking that Q*R=A, or in other words that A-Q*R = 0
        WriteLine("\nPrinting out A-Q*R:");
        matrix AmQR = A - Q * R;

        AmQR.print();

        // Let's check if Q*R is approximately equal to A, or that A-QR is approx. the zero
        // matrix
        matrix nullMatrix = new matrix(AmQR.size1, AmQR.size2);

        nullMatrix.set_zero();

        approx = AmQR.approx(nullMatrix);
        if (approx == true)
        {
            WriteLine("QR is thus approximately equal to A.");
        }
        else
        {
            WriteLine("QR is thus not approximately equal to A.");
        }



        // Part 2 of exercise A
        WriteLine("\n\n-------Part 2 of exercise A:-------\n");

        // At first we create a random square matrix - let's call it C now to avoid confusion
        // with part 1 above
        WriteLine("Setting up system C*x = b.");
        int    nC = 4;
        matrix C  = new matrix(nC, nC);

        for (int i = 0; i < nC; i++)
        {
            for (int j = 0; j < nC; j++)
            {
                C[i, j] = 2 - 4 * rand.NextDouble();
            }
        }
        WriteLine("Printing matrix C:");
        C.print();

        // Next we generate a vector b of a length corresponding to the dimension of the
        // square matrix
        vector b = new vector(nC);

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

        // Once again we factorize our matrix into two matrices Q and R
        var qrGSC = new qrDecompositionGS(C);

        Q = qrGSC.Q;
        R = qrGSC.R;

        WriteLine("\nSolving by QR decomposition and back-substitution.");
        vector x = qrGSC.solve(b);

        WriteLine("\nThe obtained x-vector from the routine is:");
        x.print();

        WriteLine("\nPrinting vector C*x :");
        vector Cx = C * x;

        Cx.print();

        // Let's check if C*x is approximately equal to b
        approx = Cx.approx(b);
        if (approx == true)
        {
            WriteLine("Cx is approximately equal to b. The system has been solved.");
        }
        else
        {
            WriteLine("Cx is not approximately equal to b. The system has not been solved.");
        }
    }
コード例 #9
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