Exemplo n.º 1
0
        /// <summary>
        /// LU decomposition and LU inverse example
        /// </summary>
        public static void LUExample()
        {
            double[,] m = {{2, 3, 3, 5},
                           {6, 6, 8, 9},
                           {10, 11, 12, 13},
                           {14, 15, 17, 17}};
            PZMath_matrix matrix = new PZMath_matrix(m);
            matrix.ScreenWriteLine();

            // LU decomposition
            // correct answer:
            // L =
            // 1.0000         0         0         0
            // 0.1429    1.0000         0         0
            // 0.4286   -0.5000    1.0000         0
            // 0.7143    0.3333   -0.3333    1.0000

            // U =
            // 14.0000   15.0000   17.0000   17.0000
            //       0    0.8571    0.5714    2.5714
            //       0         0    1.0000    3.0000
            //       0         0         0    1.0000

            // P =
            // 0     0     0     1
            // 1     0     0     0
            // 0     1     0     0
            // 0     0     1     0
            PZMath_permutation p = new PZMath_permutation(matrix.RowCount);
            PZMath_matrix LU = matrix.LUDecomp(p);
            LU.ScreenWriteLine();

            // LU Invert
            // correct answer:
            // inv(m) =
            // -2.0833    0.6667    3.5000   -2.4167
            // 1.0000   -1.0000   -1.0000    1.0000
            // 1.0000         0   -3.0000    2.0000
            // -0.1667    0.3333    1.0000   -0.8333
            PZMath_matrix inverse = matrix.LUInvert();
            inverse.ScreenWriteLine();
        }
Exemplo n.º 2
0
        // fit data from y = exp(x), x :[0 : 2]
        public static void TestLinearLeastSquareFit()
        {
            int i, n;
            n = 19;

            // -- prepare model parameters
            double xi, yi, chisq;
            PZMath_matrix X, cov;
            PZMath_vector y, w, c;

            X = new PZMath_matrix(n, 3);
            y = new PZMath_vector(n);
            w = new PZMath_vector(n);

            c = new PZMath_vector(3);
            cov = new PZMath_matrix(3, 3);

            // -- data to be fitted
            //PZMath_random_ParkMiller_Normal r = new PZMath_random_ParkMiller_Normal();
            for (i = 0; i < n; i++)
            {
                xi = 0.1 + 0.1 * i;
                yi = System.Math.Exp(xi);	// f(x)
                System.Console.WriteLine(xi + " " + yi);

                X[i, 0] = 1.0;
                X[i, 1] = xi;
                X[i, 2] = xi * xi;
                y[i] = yi;
                w[i] = 1.0;
            }
            PZMath_multifit_linear l = new PZMath_multifit_linear();
            l.Alloc(n, 3);
            l.Wlinear(X, w, y, c, cov, out chisq);

            System.Console.WriteLine("# best fit: Y = " + c[0] + " + " + c[1] + " X + " + c[2] + " X ^ 2");
            System.Console.WriteLine("# covariance matrix:");
            cov.ScreenWriteLine();
            System.Console.WriteLine("# chisq = " + chisq);
        }
Exemplo n.º 3
0
        public static void IOExample()
        {
            double[,] m = {{2, 3, 3, 5},
                           {6, 6, 8, 9},
                           {10, 11, 12, 13},
                           {14, 15, 17, 17}};
            PZMath_matrix matrix = new PZMath_matrix(m);
            matrix.WriteFile("matrix.txt");

            PZMath_matrix readMatrix = new PZMath_matrix();
            readMatrix.ReadFile("matrix.txt");
            readMatrix.ScreenWriteLine();
        }