Esempio n. 1
0
//constructor
//note to self: apparetly you can save functions as a array...
    public olsf(vector x, vector y, vector dy, Func <double, double>[] f)
    {
        int    len = x.size;
        int    m   = f.Length;
        vector b   = new vector(len);
        matrix A   = new matrix(len, m);

        for (int n = 0; n < len; n++)
        {
            b[n] = y[n] / dy[n];
            for (int n1 = 0; n1 < m; n1++)
            {
                A[n, n1] = f[n1](x[n]) / dy[n];
            }    //end for
        }        //end for
        gram_s gr = new gram_s(A);

        c = gr.solve(b);

        //coverient matrix:
        gram_s gr1 = new gram_s(A.T * A);

        Sigma = gr1.inverse();
        vector dc1 = new vector(m);

        for (int n1 = 0; n1 < m; n1++)
        {
            dc1[n1] = Sqrt(Sigma[n1, n1]);
        } //end for
        dc = dc1;
    }     //end constructor...
Esempio n. 2
0
    //function to find roots of f. x is the start guess..
    public static vector newton(Func <vector, vector> f, vector x, double eps = 1e-3, double dx = 1e-7)
    {
        bool seed2 = true;

        while (seed2)
        {
            //seed2=false;
            //First the jacobian matrix J is calculated:
            vector fx = f(x);
            matrix J  = new matrix(x.size, x.size);
            for (int k = 0; k < x.size; k++)  // for-loop over vector x
            {
                x[k] = x[k] + dx;             //change x
                vector df = (f(x) - fx) / dx; // the differnce in f(x)
                for (int i = 0; i < x.size; i++)
                {
                    J[i, k] = df[i]; // calculate the matrix element
                }                    //end for
                x[k] = x[k] - dx;    // reset x
            }                        // end for

            // solving J*DX=-f(x)
            gram_s GJ = new gram_s(J);
            vector Dx = GJ.solve((-1) * fx); // using my own method to solve the equation ...

            //calculating the step size s
            double s     = 2;
            bool   seed1 = true;
            while (seed1)
            {
                s = s / 2;
                if ((f(x + s * Dx).norm() < (1 - s / 2) * f(x).norm()) && (s < 1.0 / 64))
                {
                    seed1 = false;
                }           //end if
            }               //end while

            x = x + s * Dx; //calculate the new x!

            //if((Dx.norm()<dx) && (f(x).norm() < eps)){
            if (Dx.norm() < dx)
            {
                seed2 = false;
            }    //end if
            else if (f(x).norm() < eps)
            {
                seed2 = false;
            }    //end else if

            //seed2=false;
        }//end while
        return(x);
    }    //end newton...
Esempio n. 3
0
    public readonly vector c;     //field

//constructor
//note to self: apparetly you can save functions as a array...
    public olsf(vector x, vector y, vector dy, Func <double, double>[] f)
    {
        int    len = x.size;
        int    m   = f.Length;
        vector b   = new vector(len);
        matrix A   = new matrix(len, m);

        for (int n = 0; n < len; n++)
        {
            b[n] = y[n] / dy[n];
            for (int n1 = 0; n1 < m; n1++)
            {
                A[n, n1] = f[n1](x[n]) / dy[n];
            }    //end for
        }        //end for
        gram_s gr = new gram_s(A);

        c = gr.solve(b);
    }    //end constructor...
Esempio n. 4
0
    static int Main()
    {
        //generating a random matrix
        matrix A = randmatrix(4, 3);



        gram_s a1 = new gram_s(A);

        matrix Q = a1.Q;
        matrix R = a1.R;

        WriteLine("Part A");
        WriteLine($"i) in-place modified Gram-Schmidt");
        WriteLine($"Random generated matrix A:");
        for (int i = 0; i < A.size1; i++)
        {
            Write("\n");
            for (int j = 0; j < A.size2; j++)
            {
                Write($"{A[i,j]}	");
            }
        }
        Write("\n \n");
        WriteLine("R-matrix takes the form:");
        for (int i = 0; i < R.size1; i++)
        {
            Write("\n");
            for (int n1 = 0; n1 < R.size2; n1++)
            {
                Write($"{R[i,n1]}	");
            }
        }
        Write("\n \n");

        WriteLine($"Test that Q^T*Q =1 :"); matrix qt = Q.T;
        matrix Q1 = qt * Q;

        for (int i = 0; i < Q1.size1; i++)
        {
            Write("\n");
            for (int n1 = 0; n1 < Q1.size2; n1++)
            {
                Write($"{Q1[i,n1]}	");
            }
        }
        Write("\n \n");


        WriteLine("Test that QR=A");
        matrix Q2 = Q * R;

        for (int i = 0; i < Q2.size1; i++)
        {
            Write("\n");
            for (int n1 = 0; n1 < Q2.size2; n1++)
            {
                Write($"{Q2[i,n1]}	");
            }
        }
        Write("\n \n");


        //end part i)


        //new random matrix:
        matrix A1 = randmatrix(4, 4);
        //new random vector:
        vector b = randvector(4);

        //factorize A into QR:
        gram_s a2 = new gram_s(A1);

        matrix Qn = a2.Q;
        matrix Rn = a2.R;

        //solve QRx=b
        vector x = a2.solve(b);
        //test ax=b
        vector ax = A1 * x;

        WriteLine($"ii) Solve linear equation:");
        WriteLine($"New random matrix A:");
        WriteLine("Part A");
        for (int i = 0; i < A1.size1; i++)
        {
            Write("\n");
            for (int j = 0; j < A1.size2; j++)
            {
                Write($"{A1[i,j]}	");
            }
        }
        Write("\n \n");
        WriteLine($"random vector b:");
        for (int i = 0; i < b.size; i++)
        {
            WriteLine($"{b[i]}");
        }
        Write("\n");
        WriteLine($"A factorize into QR gives Q:");
        for (int i = 0; i < Qn.size1; i++)
        {
            Write("\n");
            for (int j = 0; j < Qn.size2; j++)
            {
                Write($"{Qn[i,j]}	");
            }
        }
        Write("\n \n");
        WriteLine($"and R:");
        for (int i = 0; i < Rn.size1; i++)
        {
            Write("\n");
            for (int j = 0; j < Rn.size2; j++)
            {
                Write($"{Rn[i,j]}	");
            }
        }
        Write("\n \n");
        WriteLine($"the system QRx=b was solved yeilding x:");
        for (int i = 0; i < x.size; i++)
        {
            WriteLine($"{x[i]}");
        }
        Write("\n");
        WriteLine($"Ax becomes:");
        for (int i = 0; i < ax.size; i++)
        {
            WriteLine($"{ax[i]}");
        }
        Write("\n");



        return(0);
    }