Пример #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
    public static (double, vector, int) eigens(matrix A, double mu, vector b, bool verbose = false)
    {
        // initialize the required matrices and objects
        matrix I    = new matrix(A.size1, A.size1);
        vector zero = new vector(A.size1);

        for (int i = 0; i < I.size1; i++)
        {
            I[i, i] = 1;
        }

        vector bu = b;         // iterative update; b+1
        double C;
        gs     QR  = new gs(A - mu * I);
        matrix inv = QR.inverse();

        if (Double.IsNaN(inv[0, 0]))
        {
            WriteLine("Matrix is singular, cannot continue.");
        }

        // calculate the eigenvector through iteration.
        int iterations = 0;

        do
        {
            iterations++;
            b  = bu;
            C  = (inv * b).norm();
            bu = inv * b / C;
        } while (!sapprox(bu, b, verbose) && iterations < imax);

        return(getEigenval(A, A * bu, bu), rescale(bu), iterations);
    }
Пример #3
0
public static vector newton(Func<vector,vector> f,
	vector x, double eps=1e-3, double dx=1e-7)
{
	double n=x.size;
	vector fx=f(x),z,fz;

	while(true){
		matrix J=jacobian(f,x,fx);
		var qrJ= new gs(J);
		matrix B= qrJ.inverse();
		vector Delta_x=-B*fx; //the newton step
		
		double lambda=1;
		while(true){
			z=x+lambda*Delta_x;
			fz=f(z);
			if(fz.norm()<(1-lambda/2)*fx.norm()) break;		//stop if the step is good
			else if(lambda<1.0/32) break;		//stop if minimum stepsize is reached
			else lambda/=2;						//backtrack by making a half step
		}
		x=z;
		fx=fz;
		if(fx.norm()<eps) break; //stop if tolerance is reached
		else if(x.norm()<dx) break;

	}
	return x;
}// method Newton
Пример #4
0
    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=");
    }
Пример #5
0
    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));
    }
Пример #6
0
    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