示例#1
0
文件: lsfit.cs 项目: HeleneHJ/PPNM
    public lsfit(vector x, vector y, vector dy, Func <double, double>[] fs)
    {
        f = fs;
        int n = x.size, m = fs.Length;
        // Console.WriteLine($"n: {n} m: {m}\n");
        var A = new matrix(n, m);
        var b = new vector(n);

        for (int i = 0; i < n; i++)
        {
            b[i] = y[i] / dy[i];                                                                                // Equation (7) in chapter
            for (int k = 0; k < m; k++)
            {
                A[i, k] = f[k](x[i]) / dy[i];                                   // Equation (7) in chapter
            }
        }

        var q = new gramschmidt(A);                       // We use the Gram-Schmidt routine to decompose A

        c = q.solve(b);                                   // We solve the system

        var ai = q.inverse();

        sigma = ai * ai.T;                                          // equation (14) in chapter to determine the covariance matrix
    }
示例#2
0
    static void Main()
    {
        var rnd = new Random(1);
        int n   = 4;
        var A   = new matrix(n, n);

        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                A[i, j] = 2 * (rnd.NextDouble() - 0.5);
            }
        }
        A.print("Matrix inverse.\nRandom square matrix A:");
        var qra = new gramschmidt(A);

        var B = qra.inverse();

        B.print("\nThe inverse of A, A^-1:");
        var Id = new matrix(n, n);

        for (int i = 0; i < n; i++)
        {
            Id[i, i] = 1;
        }
        var C = A * B;

        C.print("\ncheck: A*A^-1:");
        if (Id.approx(A * B))
        {
            Write("\nA*A^-1 = Id, test passed\n");
        }
        else
        {
            Write("\nA*A^-1 != Id, test failed\n");
        }
        var D = B * A;

        D.print("\ncheck: A^-1*A=");
        if (Id.approx(B * A))
        {
            Write("\nA^-1*A = I, test passed\n");
        }
        else
        {
            Write("\nA^-1*A != I, test failed\n");
        }
    }