示例#1
0
    public vector delta_c;      //uncertainties

    public lsfit(vector x, vector y, vector dy, Func <double, double>[] fs)
    {
        f = fs;
        int    n = x.size;
        int    m = fs.Length;
        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] = f[k](x[i]) / dy[i];
            }
        }
        var qra = new gs(A);

        c = qra.solve(b);
        matrix A_inv = qra.inverse();

        sigma = A_inv * A_inv.T;


        vector delta = new vector(m);

        for (int i = 0; i < m; i++)
        {
            delta[i] = Sqrt(sigma[i, i]);
        }
        delta_c = delta;
    }
示例#2
0
    static void Main()
    {
        for (int n = 200; n < 1001; n += 100)
        {
            matrix A = rand_matrix(n);
            vector v = rand_vector(n);
            vector b = v.copy();


            Stopwatch cd_time = new Stopwatch();
            cd_time.Start();
            var    cd   = new cholesky(A);
            vector x_cd = cd.solve(v);
            cd_time.Stop();


            Stopwatch qr_time = new Stopwatch();
            qr_time.Start();
            var qr   = new gs(A);
            var x_qr = qr.solve(b);
            qr_time.Stop();

            /////  matrix dimension n*n         cd_runtime         qr_runtime
            WriteLine($"{n} \t {cd_time.ElapsedMilliseconds} \t {qr_time.ElapsedMilliseconds}");
        }
    } //Method: Main
示例#3
0
文件: main.cs 项目: joha3332/PPNM
    static void Main()
    {
        int n = 5, m = 5;
        var A   = new matrix(n, m);
        var rnd = new System.Random();

        for (int i = 0; i < A.size1; i++)
        {
            for (int j = 0; j < A.size2; j++)
            {
                A[i, j] = 2 * (rnd.NextDouble() - 1);
            }
        }
        A.print("random matrix A:");
        var b = new vector(n);

        for (int i = 0; i < b.size; i++)
        {
            b[i] = rnd.NextDouble();
        }
        b.print("random vector b:\n");
        var qra = new gs(A);
        var x   = qra.solve(b);

        x.print("solution x to system A*x=b:\n");
        var Ax = A * x;

        Ax.print("check: A*x (should be equal b):\n");
        if (vector.approx(Ax, b))
        {
            WriteLine("test passed");
        }
        else
        {
            WriteLine("test failed");
        }
        var B  = qra.inverse();
        var BA = B * A;
        var AB = A * B;

        (BA).print("A^-1*A=");
        (AB).print("A*A^-1=");
    }
示例#4
0
文件: ls.cs 项目: krellemeister/pp
    public static fit qrfit(vector x, vector y, vector dy, Func <double, double>[] funcs)
    {
        // we want to set the system up in a more familiar way
        int    n = x.size, m = funcs.Length;
        matrix A = new matrix(n, m);
        vector b = new vector(n);

        for (int i = 0; i < n; i++)
        {
            b[i] = y[i] / dy[i];           // eq 7
            for (int j = 0; j < m; j++)
            {
                A[i, j] = funcs[j](x[i]) / dy[i];               // eq 7
            }
        }
        gs     solver    = new gs(A);
        vector c         = solver.solve(b);
        gs     covsolver = new gs(A.transpose() * A);
        matrix cov       = covsolver.inverse();

        return(new fit(c, cov, funcs));
    }
示例#5
0
文件: Main.cs 项目: joha3332/PPNM
    static void Main(){

    //// Del A1 ////
        int n=4, m=3;
        matrix A= new matrix(n,m);
        var rand= new Random(1);
    
    
    for(int i=0;i<n;i++){
    for(int j=0;j<m;j++){
        A[i,j]=2*(rand.NextDouble()-0.5);
    }
    }
        
        var qra1=new gs(A);

        var Q=qra1.Q;
        var R=qra1.R;
        
        WriteLine("_____________________________________");
        WriteLine("Part A1 - decomposit A into Q*R");
        (A).print("Matrix A=");WriteLine();
        (Q).print("Matrix Q=");WriteLine();
        (R).print("Matrix R=");WriteLine();

        WriteLine("Check if Q.T is the inverse of Q");
        ((Q.T)*Q).print("Q.T*Q=");WriteLine();
        
        WriteLine("Check if Q*R=A");
        matrix QR=Q*R;
        QR.print("Q*R=");
        if(A.approx(QR)){WriteLine("Q*R=A\nTest passed");}
        else {WriteLine("Q*R!=A\nTest failed");}



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

        n=4;
        A=new matrix(n,n);
        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);
        for(int i=0;i<n;i++){
        b[i]=2*(rand.NextDouble()-0.5);
        }

        var qra2=new gs(A);

        var x=qra2.solve(b);
        A.print("Matrix A=");WriteLine();
        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");}

    //// Del B ////
        WriteLine("_____________________________________");
        WriteLine("Part B - find the inverse matrix of A");
        var A_inv=qra2.inverse();
        A_inv.print("A_inv=");WriteLine();
        
        WriteLine("Check if A_inv is the inverse:");WriteLine();
        matrix AA_inv1=A*A_inv;
        AA_inv1.print("A*A_inv=");WriteLine();
        matrix AA_inv2=A*A_inv;
        AA_inv2.print("A_inv*A=");WriteLine();

        matrix I= new matrix(n,n);
        for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
        if(i==j) I[i,j]=1;
        else    I[i,j]=0;
        }
        }

        if(I.approx(AA_inv1) || I.approx(AA_inv2)){WriteLine("A*A_inv=I\nTest passed");}
        else {WriteLine("A*A_inv=I\nTest failed");}

    } //Method: Main