Exemplo n.º 1
0
    static void Main()
    {
        int ncalls;
        Func <vector, double> f;

        ncalls = 0;
        Func <vector, double> f1 = (z) => { ncalls++; return((1 - z[0]) * (1 - z[0]) + 100 * (z[1] - z[0] * z[0]) * (z[1] - z[0] * z[0])); };
        double dx = 1e-3;
        vector p  = new vector("2 3");

        Write("simplex.downhill: Rosenbrock's valley function\n");
        p.print("start point:");
        double psize = simplex.downhill(f1, ref p, dx: dx);

        WriteLine($"simplex size= {(float)psize}");
        WriteLine($"ncalls = {ncalls}");
        p.print("minimum:    ");
        Write($"f(x_min)          = {(float)f1(p)}\n");


        ncalls = 0;
        Func <vector, double> f2 = (z) => { ncalls++; return((z[0] * z[0] + z[1] - 11) * (z[0] * z[0] + z[1] - 11) + (z[0] + z[1] * z[1] - 7) * (z[0] + z[1] * z[1] - 7)); };
        double dx2 = 1e-3;
        vector p2  = new vector("0 1");

        Write("\nsimplex.downhill: Himmelblau's function\n");
        p2.print("start point:");
        double psize2 = simplex.downhill(f2, ref p2, dx: dx2);

        WriteLine($"simplex size= {(float)psize2}");
        WriteLine($"ncalls = {ncalls}");
        p2.print("minimum:    ");
        Write($"f(x_min)          = {(float)f2(p2)}\n");
    }
Exemplo n.º 2
0
    public static void Main()
    {
        Write("==== Finding the minimum of the Rosenbrock's Valley function with a=1 and b=100.  ==== \n\n");
        Write("Start guess of minimum:");
        Func <vector, double> f = delegate(vector x) {
            double val = (1 - x[0]) * (1 - x[0]) + 100 * (x[1] - x[0] * x[0]) * (x[1] - x[0] * x[0]);
            return(val);
        };
        vector xstart = new vector(0.0, 0.0);

        xstart.print();
        vector xmin = minimizer.qnewton(f, xstart);

        Write("\n The found minimum is:\n");
        xmin.print();
        Write("\n Minimum according to Wikipedia is:\n");
        Write("         1          1\n");

        Write("\n \n ==== Finding the minimum of Himmelblau's function.  ====\n\n");
        f = delegate(vector x) {
            double val = Pow(x[0] * x[0] + x[1] - 11, 2) + Pow(x[0] + x[1] * x[1] - 7, 2);
            return(val);
        };
        Write("Start guess of minimum:\n");
        xstart = new vector(0.0, 0.0);
        xstart.print();
        xmin = minimizer.qnewton(f, xstart);
        Write("\n The found minimum is:\n");
        xmin.print();
        Write("\n One minimum according to Wikipedia is:\n");
        Write("       3.0         2.0\n");
    }
Exemplo n.º 3
0
    public static void Main(string[] args)
    {
        // Rosenbrock's valley function
        WriteLine("Rosenbrock's valley function:");
        WriteLine($"Expected minimum: (a, a*a) = (1, 1)");
        double eps = 1e-4;
        Func <vector, double> f = delegate(vector z) {
            double x = z[0], y = z[1];
            return(Pow(1 - x, 2) + 100 * Pow(y - x * x, 2));
        };
        vector x0 = new vector(0, 0), min;
        int    n;      // steps

        x0.print("Starting point:");
        (min, n) = mini.qnewton(f, x0, eps);
        min.print($"Minimum reached in {n} steps:");

        // Himmeblau's function
        WriteLine("\nHimmelblau's function:");
        WriteLine("Reference minimums: (3.0, 2.0), (-2.81, 3.13), (-3.78, -3.28), (3.58, -1.85)");
        f = delegate(vector z) {
            double x = z[0], y = z[1];
            return(Pow(x * x + y - 11, 2) + Pow(x + y * y - 7, 2));
        };
        x0.print("\nStarting point:");
        (min, n) = mini.qnewton(f, x0, eps);
        min.print($"Minimum reached in {n} steps:");

        x0 = new vector(-6, -6);
        x0.print("\nStarting point:");
        (min, n) = mini.qnewton(f, x0, eps);
        min.print($"Minimum reached in {n} steps:");

        // Higgs discovery
        WriteLine("\nHiggs discovery");
        var    lines = File.ReadAllLines("higgs.txt").Select(line => line.Split(' ', StringSplitOptions.RemoveEmptyEntries)).Select(line => new { x = double.Parse(line[0]), y = double.Parse(line[1]), z = double.Parse(line[2]) }).ToList(); // maps every line in higgs.txt to a (x, y, z) double value. RemoveEmptyEntries is required for some reason - probably due to the format of the .txt file?
        int    l = lines.Count();                                                                                                                                                                                                              // number of lines
        vector E = new vector(l), sig = new vector(l), err = new vector(l);

        for (int i = 0; i < l; i++)           // not really necessary, but it tidies up the rest of the code
        {
            E[i]   = lines[i].x;
            sig[i] = lines[i].y;
            err[i] = lines[i].z;
        }
        Func <double, double, double, double, double> F = (e, m, g, a) => a / (Pow(e - m, 2) + g * g / 4); // Breit-Wigner
        Func <vector, double> chi2 = delegate(vector z) {
            double m = z[0], gamma = z[1], A = z[2];
            double sum = 0;
            for (int i = 0; i < l; i++)
            {
                sum += Pow(F(E[i], m, gamma, A) - sig[i], 2) / Pow(err[i], 2);
            }
            return(sum);
        };

        x0       = new vector(120, 2, 6);   // i have no idea which starting point to use, so these are dmitris values
        (min, n) = mini.qnewton(chi2, x0, eps);
        WriteLine($"Fitted values in {n} steps: mass: {min[0]:F3}, gamma: {min[1]:F3}, sqrt(chi2/n): {chi2(min)/l:F3}");
    }
Exemplo n.º 4
0
    public static void Main()
    {
        Func <vector, vector> f = delegate(vector z) {
            double x = z[0];
            return(new vector((2 - x) * (6 - x)));
        };
        vector x0   = new vector(1); x0[0] = 0;       // start pos
        vector root = roots.newton(f, x0);

        WriteLine($"Testing with (2 - x)*(6 - x).");
        root.print("The root is: ");
        f(root).print("The function value at the root:");

        f = delegate(vector z) {
            double x = z[0], y = z[1];
            return(new vector((4 - x) * (6 - x), (y - x) * (y - 3)));
        };
        x0   = new vector(0, 0);
        root = roots.newton(f, x0);
        WriteLine($"\nTesting with (4 - x)*(6 - x), (y - x)*(y - 3)");
        root.print("The roots are: ");
        f(root).print("The function values at these roots: ");

        // Gradient of Rosenbrock
        WriteLine("\nExtremums of Rosenbrock's valley function");
        f = delegate(vector z) {
            double x = z[0], y = z[1];
            return(new vector(2 * (1 - x) + 2 * 100 * (y - x * x) * 2 * x, 2 * 100 * (y - x * x))); // the gradient
        };
        x0   = new vector(2, 2);
        root = roots.newton(f, x0);
        root.print("The roots are");
        f(root).print("The function values at these roots: ");

        // Hydrogen atom
        WriteLine("\n# Hydrogen atom");
        double rmax = 8;
        Func <vector, vector> aux = delegate(vector z) {
            double eps   = z[0];
            double frmax = Feps(eps, rmax);
            return(new vector(frmax));
        };

        x0   = new vector(-1.0);
        root = roots.newton(aux, x0);
        WriteLine($"# rmax is {rmax}, energy is {root[0]}");
        WriteLine(string.Format("# {0, -6} {1, -8} {2, -8}", "r", "Feps(r)", "exact"));
        for (double r = 0; r <= rmax; r += rmax / 100)
        {
            WriteLine($"{r, -8:F3} {Feps(root[0], r), -8:F3} {r*Exp(-r), -8:F3}");
        }
    }
Exemplo n.º 5
0
    static void Main()
    {
        Func <vector, double> f1 = (z) => (1 - z[0]) * (1 - z[0]) + 100 * (z[1] - z[0] * z[0]) * (z[1] - z[0] * z[0]); // Rosenbrock's valley function (a=1,b=100)
        double eps = 1e-4;
        vector p   = new vector("2 3");                                                                                // starting point

        Write("SR1: Rosenbrock's valley function\n");
        p.print("start point:");
        int nsteps = qnewton.sr1(f1, ref p, eps);

        WriteLine($"nsteps={nsteps}");
        p.print("minimum:    ");
        Write($"f(x_min)          = {(float)f1(p)}\n");
        vector g = qnewton.gradient(f1, p);

        Write($"|gradient| goal   = {(float)eps}\n");
        Write($"|gradient| actual = {(float)g.norm()}\n");
        if (g.norm() < eps)
        {
            WriteLine("test passed");
        }
        else
        {
            WriteLine("test failed");
        }
        // Minimum at (1,1)

        Func <vector, double> f2 = (z) => (z[0] * z[0] + z[1] - 11) * (z[0] * z[0] + z[1] - 11) + (z[0] + z[1] * z[1] - 7) * (z[0] + z[1] * z[1] - 7); // Himmelblau's function

        eps = 1e-4;
        p   = new vector("0 1");                // starting point
        Write("\nSR1: Himmelblau's function\n");
        p.print("start point:");
        nsteps = qnewton.sr1(f2, ref p, eps);
        WriteLine($"nsteps={nsteps}");
        p.print("minimum:    ");
        Write($"f(x_min)          = {(float)f2(p)}\n");
        g = qnewton.gradient(f, p);
        Write($"|gradient| goal   = {(float)eps}\n");
        Write($"|gradient| actual = {(float)g.norm()}\n");
        if (g.norm() < eps)
        {
            WriteLine("test passed");
        }
        else
        {
            WriteLine("test failed");
        }
        // Minima at (3,2), (-2.805118,3.131312), (-3.779310,-3.283186), and (3.584428,-1.848126)
    }
Exemplo n.º 6
0
    static void Main()
    {
        /// Test on random matrix ///
        WriteLine("/////////////// Part A - Test on random matrix ///////////////");
        int n    = 5;
        var rand = new Random(1);

        matrix A = new matrix(n, n);
        vector e = new vector(n);
        matrix V = new matrix(n, n);


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

        A.print($"random {n}x{n} matrix"); WriteLine();
        matrix B      = A.copy();
        int    sweeps = jacobi.cyclic(B, e, V);

        WriteLine($"Number of sweeps in jacobi={sweeps}"); WriteLine();

        matrix D = (V.T * A * V);

        (D).print("Should be a diagonal matrix V.T*B*V="); WriteLine();
        e.print("Eigenvalues should equal the diagonal elements above");

        vector D_diagonal = new vector(n);

        for (int i = 0; i < n; i++)
        {
            D_diagonal[i] = D[i, i];
        }
        if (D_diagonal.approx(e))
        {
            WriteLine("Eigenvalues agree \tTest passed");
        }
        else
        {
            WriteLine("Test failed");
        }
        WriteLine();

        matrix A2 = (V * D * V.T);

        (A2).print("Check that V*D*V.T=A");
        if (A2.approx(A))
        {
            WriteLine("V*D*V.T = A \tTest passed");
        }
        else
        {
            WriteLine("V*D*V.T != A \tTest failed");
        }
    }
Exemplo n.º 7
0
    public static void Main()
    {
        //now test highest EV
        int    n    = 5;
        matrix A    = new matrix(n, n);
        matrix V    = new matrix(n, n);
        vector e    = new vector(n);
        var    rand = new Random(1);

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

        //2 highest EV
        matrix AA2   = A.copy();
        string order = "high";
        int    count = jacobiB.jacobirow(AA2, e, 2, V, order);

        e.print("First 2 values should be highest EVs");

        //full diag
        matrix Vh     = new matrix(n, n);
        vector eh     = new vector(n);
        matrix AAh    = A.copy();
        int    counth = jacobiB.jacobirow(AAh, eh, n, Vh);

        eh.print("all eigenvalues");
    }
Exemplo n.º 8
0
    public static void Main()
    {
        // First part of A
        int n   = 5; // Size of quadratic matrix M.
        var rnd = new Random(1);

        Console.WriteLine("Part A1");
        matrix M = new matrix(n, n);

        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                M[i, j] = 4 * rnd.NextDouble() - 1.5;
                M[j, i] = M[i, j];
            }
        }
        matrix M2 = M.copy();

        M.print("We choose an arbitrary matrix M = ");
        vector D      = new vector(n);
        matrix V      = new matrix(n, n);
        int    sweeps = Jacobi_diagonalization.cyclic_sweep(M, V, D);
        matrix temp2  = V.T * M2 * V;

        Console.WriteLine($"Total number of sweeps done is = {sweeps}");
        temp2.print("V.T*M*V = ");
        D.print("Eigenvalues = ");
    }
Exemplo n.º 9
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 = ");
    }
Exemplo n.º 10
0
    public static void Main()
    {
        double EPS = Pow(10, -6);
        //
        Func <vector, double> f = (z) => Pow(1 - z[0], 2) + 100 * Pow(z[1] - Pow(z[0], 2), 2);
        vector x01 = new vector(0, 0);

        int StepCounts = Quasi.qnewton(f, ref x01);

        WriteLine("");
        x01.print("Starting point:    ");
        WriteLine($"steps = {StepCounts}");
        WriteLine($"Found minimum = ({x01[0]:f5}, {x01[1]:f5},)");
        WriteLine($"Exact minimum = (1,1)");
        WriteLine($"Tolerance for gradient = {EPS}");


        //
        Func <vector, double> f2 = (z) => Pow((Pow(z[0], 2) + z[1] - 11), 2) + Pow(z[0] + Pow(z[1], 2) - 7, 2);
        vector x02 = new vector(-2, 3);

        int StepCounts2 = Quasi.qnewton(f2, ref x02);

        x02.print("Starting point:   ");


        WriteLine($"steps = {StepCounts2}");

        WriteLine($"The minimum was found at minimum = ({x02[0]:f5}, {x02[1]:f5})");
        WriteLine($"Where the analytical minimum = (-2.805118, 3.131312)");
        WriteLine($"The tolorence for gradient is = {EPS}");
    }
Exemplo n.º 11
0
    static void Main()
    {
        int    ncalls = 0;
        double eps    = 1e-7;
        vector p      = new vector(2);

        p[0] = 0.5; p[1] = 1.7;

        Func <vector, vector> f = delegate(vector z){
            ncalls++;
            vector r = new vector(2);
            double x = z[0], y = z[1], b = 100;
            // Rosenbrock's valley function: f(x,y) = (1-x)^2+100*(y-x^2)^2
            // The gradient of Rosenbrock's valley function:
            r[0] = 2 * (1 - x) + 2 * 100 * (y - x * x) * (-2 * x); // d/dx
            r[1] = b * 2 * (y - x * x);                            // d/dy
            return(r);
        };
        vector root = roots.newton(f, p, eps);

        WriteLine($"start values={p[0]}	{p[1]}");
        WriteLine($"ncalls={ncalls}");
        root.print("root=");
        f(root).print("f(root)=");
        WriteLine($"eps            = {eps}");
        WriteLine($"f(root).norm() = {f(root).norm()}");
        if (f(root).norm() < eps)
        {
            WriteLine("test passed");
        }
        else
        {
            WriteLine("test failed");
        }
    }
Exemplo n.º 12
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 = ");
    }
Exemplo n.º 13
0
Arquivo: NN.cs Projeto: fred8337/PPNM
    // public double eval(double x, vector weights)
    // {
    //     vector hold = x*weights;
    //     // hold.print();
    //     hold = relu(hold);
    //     double res = 0;
    //     for(int i = 0; i<hold.size; i++)
    //     {
    //         res+=hold[i];
    //     }
    //     return res;
    // }
    // static Func<vector, double> trainingFunc = delegate(vector x)
    // {
    //     double res = 0;
    //     for(int i = 0; i<data.size;i++)
    //     {
    //         res+=MSE(eval(data[i], x),labels[i]);
    //     }
    //     return res;
    // };
    public void fit(double[] data, double[] labels)
    {
        parameters.print();
        Func <vector, double> trainingFunc = (x) =>
        {
            parameters = x;
            double res = 0;
            for (int i = 0; i < data.Length; i++)
            {
                // parameters.fprint(Console.Error);
                // Error.Write($"{data[i]}\n");
                // Error.Write($"{eval(data[i])}\n");
                res += MSE(eval(data[i]), labels[i]);
            }
            // Error.Write($"current error is {res/data.Length} \n");
            return(res);
        };

        // var guesses = new vector(parameters.size);
        // for(int i = 0;i<guesses.size;i++)
        // {
        //     guesses[i]=1;
        // }
        parameters = qnewton.QNewton(trainingFunc, parameters, 0.01);
        parameters.print();
    }
Exemplo n.º 14
0
    public static void Main(string[] args)
    {
        int    n   = 5; //size of real symmetric matrix
        matrix A   = new matrix(n, n);
        var    rnd = new Random(1);

        for (int i = 0; i < n; i++)                                            /* Construction of a random vector */
        {
            for (int j = 0; j < n; j++)
            {
                A[i, j] = (rnd.NextDouble() * 10);
                A[j, i] = A[i, j];                                              /* We make sure that the matrix is symmetric */
            }
        }
        A.print("Random symmetric matrix, A:");
        matrix V = new matrix(n, n);
        vector e = new vector(n);
        matrix B = A.copy();                                        /* We make a copy of A */

        int sweeps = jacobi.cyclic(A, e, V);                        /* We use the jacobi matlib for the implementation of the cyclic sweeps: public static int cyclic(matrix A, vector e, matrix V=null){...} */

        WriteLine($"\nNumber of sweeps: {sweeps}");
        matrix D = (V.T * B * V);

        // D.print("\nEigenvalue-decomposition, V^T*A*V (should be diagonal):");
        D.printfloat("\nEigenvalue-decomposition, V^T*A*V (should be diagonal):");              /* using my print2 to get 0's instead of values with e.g. "e-16"*/
        e.print("\nEigenvalues:\n");
    }
Exemplo n.º 15
0
    public static void Main()
    {
        int    n    = 3;
        matrix A    = new matrix(n, n);
        matrix V    = new matrix(n, n);
        vector e    = new vector(n);
        var    rand = new Random();

        for (int i = 0; i < n; i++)
        {
            for (int j = i; j < n; j++)
            {
                A[i, j] = rand.NextDouble() * 10;
                A[j, i] = A[i, j];
            }
        }
        A.print("Symmetric matrix A:");
        matrix Ao = new matrix(n, n);

        Ao = A.copy();
        int count = jacobi.cycsweep(A, e, V);

        WriteLine($"Number of sweeps: {count}");
        V.print("Eigenvectors");
        A.print("new A");
        e.print("Eigenvalues");
        (V.T * Ao * V).print("V^T*A*V diagonal, test");
    }
Exemplo n.º 16
0
    static void Main()
    {
        int n = 6;

        WriteLine($"Generate symmetric {n}x{n} matrix A");
        matrix A = randomMatrix(n, n);

        restoreUpperTriang(A);
        A.print("A: ");

        matrix V = new matrix(n, n);
        vector e = new vector(n);

        int sweeps;

        // Cyclic
        sweeps = Jacobi.cyclic(A, V, e);
        WriteLine($"Cyclic Jacobi done in {sweeps} sweeps");
        e.print("Cyclic eigenvalues: ");

        // Lowest k
        restoreUpperTriang(A);
        V = new matrix(n, n);
        e = new vector(n);
        int    k = n / 3;
        vector v = Jacobi.lowestK(A, V, e, k);

        v.print($"Lowest {k} values:");
        A.print($"Test that {k} rows are cleared");

        // Highest k
        restoreUpperTriang(A);
        V = new matrix(n, n);
        e = new vector(n);
        k = n / 3;
        v = Jacobi.highestK(A, V, e, k);
        v.print($"Highest {k} values:");
        A.print($"Test that {k} rows are cleared");

        restoreUpperTriang(A);
        V = new matrix(n, n);
        e = new vector(n);
        int rots = Jacobi.classic(A, V, e);

        e.print($"Classical done! rotations: {rots}");
        A.print($"Test that all rows are cleared");
    }
Exemplo n.º 17
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:
    }
Exemplo n.º 18
0
    static void Main()
    {
        int callCount           = 0;
        Func <vector, vector> f = (x) => { callCount++; return(new vector(x[0] * x[0] - 4)); };
        double epsilon          = 1e-3;
        vector x0    = new vector(1.0);
        vector root  = newton(f, x0, epsilon);
        vector exact = new vector(2.0);

        WriteLine("\n__________________________________________________________________________________________________________");
        WriteLine("Question A\n Newton's method with numerical Jacobian and back-tracking linesearch");

        WriteLine("Test function f(x)=x^2-4, root = +-2 (only looking for 2)");
        root.print("Numericaly found root        : ");
        exact.print("Exact root                   : ");
        f(root).print("Function value at root       : ");
        WriteLine("Error goal                   : {0}", epsilon);
        WriteLine("Actual error (norm at rooot) : {0}", f(root).norm());
        WriteLine("Number of func call          : {0}", callCount);
        WriteLine("");
        callCount = 0;
        // Gradient
        // Func<vector,vector> f = (x) => {callCount++;return new vector(pow(1-x,2)+100,);};

        f = (x) => {
            callCount++;
            double dx = -2 * (1 - x[0]) - 400 * x[0] * (x[1] - x[0] * x[0]);
            double dy = 200 * (x[1] - x[0] * x[0]);
            return(new vector(dx, dy));
        };

        epsilon = 1e-3;
        x0      = new vector(1.1, 0.5);
        root    = newton(f, x0, epsilon);
        exact   = new vector(1.0, 1.0);
        WriteLine("Test function extreem of Rosenbrock's valley function");
        root.print("Numericaly found root        : ");
        exact.print("Exact root                   : ");
        f(root).print("Function value at root       : ");
        WriteLine("Error goal                   : {0}", epsilon);
        WriteLine("Actual error (norm at rooot) : {0}", f(root).norm());
        WriteLine("Number of func call          : {0}", callCount);
        WriteLine("__________________________________________________________________________________________________________\n");
        callCount = 0;
    }
Exemplo n.º 19
0
    static void Main()
    {
        WriteLine("Testing to find roots of f(x,y) = (x*x - 9, 81 - y*y)");
        Func <vector, vector> f = (x) => new vector(x[0] * x[0] - 9, 81 - x[1] * x[1]);
        vector p     = new vector(10, 20);
        vector roots = newton(f, p);

        roots.print("Found solution: ");
        p     = new vector(-10, -20);
        roots = newton(f, p);
        roots.print("Found second solution: ");
        p     = new vector(-13, 27);
        roots = newton(f, p);
        roots.print("Found third solution: ");
        p     = new vector(42, -65);
        roots = newton(f, p);
        roots.print("Found fourth solution: ");
    }
Exemplo n.º 20
0
    static void Main()
    {
        var rnd = new Random();
        var A   = new matrix(3, 3);

        for (int i = 0; i < A.size1; i++)
        {
            for (int j = 0; j < A.size2; j++)
            {
                A[i, j] = 10 * (rnd.NextDouble() - 0.5);
            }
        }
        var b = new vector(3);

        for (int i = 0; i < 3; i++)
        {
            b[i] = 10 * (rnd.NextDouble() - 0.5);
        }
        WriteLine("\n__________________________________________________________________________________________________________");

        WriteLine("Question C: \nTest QR-decomposition by Givens rotations:");
        var QR = A.copy();

        A.print("A = ");

        qr_givens.qr_givens_QR(QR);
        WriteLine("QR-decomposition");
        QR.print("QR = ");
        WriteLine("Random vector");
        b.print("b = ");
        qr_givens.qr_givens_solve(QR, b);
        WriteLine("Solve Ax = b:");
        b.print("x = ");
        WriteLine("Check result:");
        (A * b).print("Ax= ");

        var B = qr_givens.qr_givens_inverse(QR);

        WriteLine("Inverse of A:");
        B.print("A^-1=B=");
        WriteLine("Product should give identity");
        (A * B).print("I=AB=");
        WriteLine("__________________________________________________________________________________________________________\n");
    }
Exemplo n.º 21
0
    static void Main()
    {
        WriteLine("--- Part A: Search for extremums of the Rosenbrock Valley Function ---");
        Func <vector, vector> f = (x) =>
                                  new vector(2 * (200 * x[0] * x[0] * x[0] - 200 * x[0] * x[1] + x[0] - 1),
                                             200 * (x[1] - x[0] * x[0]));
        vector p = new vector(10, 20);

        p.print("Starting point: ");
        vector roots = newton(f, p);

        roots.print("Found solution: ");
        p = new vector(-123, 123);
        p.print("Starting point: ");
        roots = newton(f, p);
        roots.print("Found solution: ");
        p = new vector(-123, 123);
        p.print("Starting point: ");
        roots = newton(f, p);
        roots.print("Found solution: ");
        p = new vector(-100, 0);
        p.print("Starting point: ");
        roots = newton(f, p);
        roots.print("Found solution: ");
        p = new vector(100, -123);
        p.print("Starting point: ");
        roots = newton(f, p);
        roots.print("Found solution: ");

        WriteLine("\n\n--- Part B ---");
        double rmax             = 8;
        Func <vector, vector> M = (x) => new vector(f_E(x[0], rmax));

        p     = new vector(-2.0);
        roots = newton(M, p);
        WriteLine($"For rmax = {rmax} we get an energy estimate of {roots[0]},\ncompared to the analytical result of -1/2");
        using (StreamWriter sw = new StreamWriter("data.txt")){
            for (double r = 0; r <= rmax; r += 1.0 / 64)
            {
                sw.WriteLine($"{r}\t{f_E(roots[0],r)}");
            }
        }
    }
Exemplo n.º 22
0
    static void Main()
    {
        // Testing that the jacobiAlgorithm works:

        int n = 5;

        vector e      = new vector(n);
        matrix V      = new matrix(n, n);
        matrix A      = generateRandommatrix(n, n);
        matrix B      = A.copy();
        int    sweeps = jacobi.jacobi_cyclic(A, e, V);

        WriteLine("---------Problem A1----------");
        WriteLine("\nTesting that the Jacobi algorithm works as intended:");
        A.print("\nRandom square matrix A:");
        WriteLine("\nEigenvalue decomposition of A=V*D*V^T:");
        (V.transpose() * B * V).print("\nD = V^T*A*V = ");
        e.print("\nListed eigenvalues:\t");
        WriteLine($"\nNumber of sweeps required to diagonalize A: {sweeps}");

        // Particle in a box
        n = 50;
        matrix H = Hamiltonian.boxHamilton(n);

        B      = H.copy();
        V      = new matrix(n, n);
        e      = new vector(n);
        sweeps = jacobi.jacobi_cyclic(H, e, V);
        WriteLine("\n---------Problem A2----------");
        WriteLine("\nNow solving for a particle in a box.");
        WriteLine($"\nThe dimensions of the soon to be diagonalized Hamiltonian is {n}x{n}");
        WriteLine("\nComparison of the calculated eigenvalues and the exact:\n");
        WriteLine("n Calculated     Exact:");
        for (int k = 0; k < n / 3; k++)
        {
            double exact      = PI * PI * (k + 1) * (k + 1);
            double calculated = e[k];
            WriteLine($"{k} {calculated:f4}\t {exact:f4}");
        }

        // plotting time
        StreamWriter solutions = new StreamWriter("SolutionsA.txt");

        for (int i = 0; i < n; i++)
        {
            solutions.Write($"{i*(1.0/n)}");
            for (int k = 0; k < 5; k++)
            {
                solutions.Write($" {V[i,k]}");
            }
            solutions.Write("\n");
        }
        solutions.Close();
    }
Exemplo n.º 23
0
    public static void Main()
    {
        vector guesses = new vector(new double[] { 0 });
        vector roots   = Newton.newton(F, guesses);

        roots.print();
        vector guesses_RosenBrock = new vector(new double[] { 0, 0 });
        vector roots_RosenBrock   = Newton.newton(DRosenbrock, guesses_RosenBrock);

        roots_RosenBrock.print();
    }
Exemplo n.º 24
0
    static int Main()
    {
        int    n = 3;
        vector v = new vector(n);
        vector u = new vector(n);

        for (int i = 0; i < n; i++)
        {
            v[i] = i;
            u[i] = -2 * i;
        }

        v.print("v=");
        v[0] = 5;
        v.print("v=");
        vector w = u + v;

        w.print("w=");
        return(0);
    }
Exemplo n.º 25
0
    }     // end Main function

    public static void printResults(vector estimate, vector a, vector b, int N, double expected)
    {
        Write("Lower integration limits: ");
        a.print();
        Write("Upper integration limits: ");
        b.print();
        WriteLine("Analytic value: \t\t{0}", expected);
        WriteLine("Estimate of integral: \t\t{0}", estimate[0]);
        WriteLine("Estimate of error: \t\t{0}", estimate[1]);
        WriteLine("Actual error: \t\t\t{0}", Abs(expected - estimate[0]));
        WriteLine("The function was sampled in {0} points.", N);
    }     // end printResults
Exemplo n.º 26
0
    public static int Main()
    {
        double epsilon = 1e-5;
        Func <vector, double> f_rosen = (x) =>
        {
            return((1 - x[0]) * (1 - x[0]) + 100 * (x[1] - x[0] * x[0]) * (x[1] - x[0] * x[0]));
        };

        WriteLine("The Rosenbruck Valley function: f(x,y)=(1-x)^2+100(y-x^2)^2");
        vector start = new vector(new double [] { 1.3, 0.2 });

        start.print("starting point              : ");
        var    min_rosen = minimization.qnewton(f_rosen, ref start, epsilon);
        vector x_rosen   = new vector(new double[] { 1.0, 1.0 });

        start.print("Quasi newton minimum        : ");
        x_rosen.print($"The exact minimum           : ");
        WriteLine($"Number of steps             : {min_rosen} steps.");
        WriteLine("");


        Func <vector, double> f_himmel = (x) =>
        {
            return((x[0] * x[0] + x[1] - 11) * (x[0] * x[0] + x[1] - 11) + (x[0] + x[1] * x[1] - 7) * (x[0] + x[1] * x[1] - 7));
        };

        epsilon = 1e-4;
        WriteLine("The Himmelblau function: f(x,y)=(x^2+y-11)^2+(x+y^2-7)^2.");
        vector start_h = new vector(new double [] { 1, 1 });

        start_h.print("starting point              : ");
        vector x_himmel   = new vector(new double [] { 3.0, 2.0 });
        var    min_himmel = minimization.qnewton(f_himmel, ref start_h, epsilon);

        start_h.print("Quasi newton minimum        : ");
        x_himmel.print($"The exact minimum           : ");
        WriteLine($"Number of steps             : {min_himmel} steps.");

        return(0);
    }
Exemplo n.º 27
0
    static int Main()
    {
        // Load data (not from file...)
        vector t  = new vector(new double[] { 1, 2, 3, 4, 6, 9, 10, 13, 15 });
        vector A  = new vector(new double[] { 117, 100, 88, 72, 53, 29.5, 25.2, 15.2, 11.1 });
        vector dA = new vector(A.size);         // uncertainty of A

        for (int i = 0; i < A.size; i++)
        {
            dA[i] = 0.05;                                 // 5% uncertainty
        }
        // Transform data from y = a*exp(-lambda*t) --> ln(y) = ln(a) - lambda*t
        for (int i = 0; i < A.size; i++)
        {
            dA[i] = dA[i] / A[i];           // delta ln(y) --> (delta y )/ y
            A[i]  = Log(A[i]);
        }

        // Make function array
        Func <double, double>[] funcs = new Func <double, double>[] { x => 1, x => x };
        // t -> 1 is for ln(a), t -> t is for lambda

        // Make fit
        Fit    FitResult = qr_stuff.qrFitter(t, A, dA, funcs);
        vector cErrors   = FitResult.getParamErrors();
        double a         = Exp(FitResult.c[0]);
        double aErr      = cErrors[0];
        double lambda    = -FitResult.c[1];
        double lambdaErr = cErrors[1];

        // Generate Answer to answer questions:
        FitResult.c.print("Fit result parameters: ");
        WriteLine($"This means that a: {a} and lambda: {lambda}");
        WriteLine($"This gives a half life of {Log(2)/lambda} days");
        WriteLine("The half life of Ra224 is actually around 3.6319 days");
        WriteLine("");
        FitResult.cov.print("The Covariance matrix is estimated to");
        cErrors.print("This give the following errors of the parameters");
        WriteLine($"Thus, the half of ThX is estimated to be between {Log(2)/lambda} +- {Log(2)/lambda/lambda*lambdaErr} days");


        // Generate plotting data to plot with
        TextWriter DataWriter = Error;

        //DataWriter.WriteLine("This is in the data stream");
        for (double x = 0; x < 16; x += 1.0 / 16)
        {
            DataWriter.WriteLine($"{x} {Exp(FitResult.evaluate(x))} {Exp(FitResult.upper(x))} {Exp(FitResult.lower(x))}");
        }

        return(0);
    }
Exemplo n.º 28
0
    static int Main()
    {
        Func <double, double>[] lnexp = new Func <double, double> [2];
        lnexp[0] = (x) => 1;
        lnexp[1] = (x) => - x;
        double[] t     = { 1, 2, 3, 4, 6, 9, 10, 13 };
        double[] yt    = { 117, 100, 88, 72, 53, 29.5, 25.2, 15.2 };
        vector   xs    = new vector(t);
        vector   ys    = new vector(yt);
        vector   lny   = new vector(yt.Length);
        vector   yerr  = new vector(yt.Length);
        vector   lnerr = new vector(yt.Length);

        for (int i = 0; i < yt.Length; i++)
        {
            lny[i]   = Log(yt[i]);
            yerr[i]  = yt[i] / 20;
            lnerr[i] = yerr[i] / yt[i];
        }

        lsfit fit1 = new lsfit(xs, lny, lnerr, lnexp);

        vector c = fit1.C;

        WriteLine("\n Data:");
        xs.print("time in days = ");
        ys.print("y = ");
        WriteLine("\n Attempting to make fit y=a*Exp(-lambda*t) by using ln(y)=ln(a)-lambda*t");
        c.print("c =   ln(a)   lambda   = ");
        WriteLine("a = {0:f6}", Exp(c[0]));
        WriteLine("half life = {0:f6}\n", Log(2) * 1 / c[1]);

        Func <double, double> fit1fun = x => c[0] * lnexp[0](x) + c[1] * lnexp[1](x);

        var fitwriter = new System.IO.StreamWriter("out.fit.txt");

        for (double x = 0; x < 20; x += 0.1)
        {
            fitwriter.Write("{0:f16} {1:f16}\n", x, Exp(fit1fun(x)));
        }
        fitwriter.Close();

        var datwriter = new System.IO.StreamWriter("out.data.txt");

        for (int i = 0; i < xs.size; i++)
        {
            datwriter.Write("{0:f16} {1:f16} {2:f16}\n", xs[i], yt[i], yerr[i]);
        }
        datwriter.Close();

        return(0);
    }
Exemplo n.º 29
0
    static void Main()
    {
        Write("==== Finding the extrenum of the Rosenbrock's Valley function with a=1 and b=100.  ==== \n\n");
        Write("Start guess of root:\n");
        Func <vector, vector> f = x => new vector(-2 * (1 - x[0]) - 200 * x[0] * (x[1] - x[0] * x[0]), 200 * (x[1] - x[0] * x[0]));
        vector startx           = new vector(0, 0);

        startx.print();
        vector x0 = root.newton(f, startx);

        Write("\n After running newtons method, the found extremum is:\n");
        x0.print("");
    }
Exemplo n.º 30
0
    static void Main()
    {
        int callCount           = 0;
        Func <vector, double> f = (X) => Pow(1 - X[0], 2) + 100 * Pow(X[1] - X[0] * X[0], 2);
        double epsilon          = 1e-4;
        vector x            = new vector(1.5, 0.5);
        int    num_of_steps = qnewton(f, ref x, epsilon);
        vector x_exact      = new vector(1.0, 1.0);

        WriteLine("\n__________________________________________________________________________________________________________");
        WriteLine("Question A\n Quasi-Newton method with numerical gradient, back-tracking linesearch, and rank-1 update");

        WriteLine("Test function f(x,y)=(1-x)^2+100(y-x^2)^2, min at 1,1");
        x.print("Numericaly found min x       : ");
        x.print("Exact min x                  : ");
        WriteLine("Function value at min        : {0}", f(x));
        WriteLine("Gradient tollerence          : {0}", epsilon);
        WriteLine("Actual gradient              : {0}", gradient(f, x).norm());
        WriteLine("Actual error                 : {0}", Abs(f(x) - f(x_exact)));
        WriteLine("Number of steps              : {0}", num_of_steps);
        WriteLine("");

        f            = (X) => Pow(X[0] * X[0] + X[1] - 11, 2) + Pow(X[0] + X[1] * X[1] - 7, 2);
        epsilon      = 1e-4;
        x            = new vector(2.4, 2.4);
        num_of_steps = qnewton(f, ref x, epsilon);
        x_exact      = new vector(3, 2);
        WriteLine("");
        WriteLine("Test function f(x,y)=(x^2+y-11)^2+(x+y^2-7)^2, min at 3,2");
        x.print("Numericaly found min x       : ");
        x.print("Exact min x                  : ");
        WriteLine("Function value at min        : {0}", f(x));
        WriteLine("Gradient tollerence          : {0}", epsilon);
        WriteLine("Actual gradient              : {0}", gradient(f, x).norm());
        WriteLine("Actual error                 : {0}", Abs(f(x) - f(x_exact)));
        WriteLine("Number of steps              : {0}", num_of_steps);
        WriteLine("__________________________________________________________________________________________________________\n");
    }