private static void test01()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST01 uses a 4x4 test matrix.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    15 July 2013
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        const int N = 4;

        double[] a =
        {
            4.0,    -30.0,    60.0,   -35.0,
            -30.0,  300.0,  -675.0,   420.0,
            60.0,  -675.0,  1620.0, -1050.0,
            -35.0,  420.0, -1050.0, 700.0
        }

        ;
        double[] d       = new double[N];
        int      it_num  = 0;
        int      rot_num = 0;

        double[] v = new double[N * N];

        Console.WriteLine("");
        Console.WriteLine("TEST01");
        Console.WriteLine("  For a symmetric matrix A,");
        Console.WriteLine("  JACOBI_EIGENVALUE computes the eigenvalues D");
        Console.WriteLine("  and eigenvectors V so that A * V = D * V.");

        typeMethods.r8mat_print(N, N, a, "  Input matrix A:");

        const int it_max = 100;

        Jacobi.jacobi_eigenvalue(N, a, it_max, ref v, ref d, ref it_num, ref rot_num);

        Console.WriteLine("");
        Console.WriteLine("  Number of iterations = " + it_num + "");
        Console.WriteLine("  Number of rotations  = " + rot_num + "");

        typeMethods.r8vec_print(N, d, "  Eigenvalues D:");

        typeMethods.r8mat_print(N, N, v, "  Eigenvector matrix V:");
        //
        //  Compute eigentest.
        //
        double error_frobenius = typeMethods.r8mat_is_eigen_right(N, N, a, v, d);

        Console.WriteLine("");
        Console.WriteLine("  Frobenius norm error in eigensystem A*V-D*V = "
                          + error_frobenius + "");
    }
    private static void test03()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST03 uses a 5x5 test matrix.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    15 July 2013
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        const int N = 5;

        double[] a      = new double[N * N];
        double[] d      = new double[N];
        int      it_num = 0;
        int      j;
        int      rot_num = 0;

        double[] v = new double[N * N];

        Console.WriteLine("");
        Console.WriteLine("TEST03");
        Console.WriteLine("  For a symmetric matrix A,");
        Console.WriteLine("  JACOBI_EIGENVALUE computes the eigenvalues D");
        Console.WriteLine("  and eigenvectors V so that A * V = D * V.");
        Console.WriteLine("");
        Console.WriteLine("  Use the discretized second derivative matrix.");

        for (j = 0; j < N; j++)
        {
            int i;
            for (i = 0; i < N; i++)
            {
                if (i == j)
                {
                    a[i + j * N] = -2.0;
                }
                else if (i == j + 1 || i == j - 1)
                {
                    a[i + j * N] = 1.0;
                }
                else
                {
                    a[i + j * N] = 0.0;
                }
            }
        }

        typeMethods.r8mat_print(N, N, a, "  Input matrix A:");

        const int it_max = 100;

        Jacobi.jacobi_eigenvalue(N, a, it_max, ref v, ref d, ref it_num, ref rot_num);

        Console.WriteLine("");
        Console.WriteLine("  Number of iterations = " + it_num + "");
        Console.WriteLine("  Number of rotations  = " + rot_num + "");

        typeMethods.r8vec_print(N, d, "  Eigenvalues D:");

        typeMethods.r8mat_print(N, N, v, "  Eigenvector matrix V:");
        //
        //  Compute eigentest.
        //
        double error_frobenius = typeMethods.r8mat_is_eigen_right(N, N, a, v, d);

        Console.WriteLine("");
        Console.WriteLine("  Frobenius norm error in eigensystem A*V-D*V = "
                          + error_frobenius + "");
    }
Exemple #3
0
    private static void wishart_unit_sample_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    WISHART_UNIT_SAMPLE_TEST demonstrates the unit Wishart sampling function.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    02 August 2013
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        int it_num  = 0;
        int rot_num = 0;

        Console.WriteLine("");
        Console.WriteLine("WISHART_UNIT_SAMPLE_TEST:");
        Console.WriteLine("  WISHART_UNIT_SAMPLE samples unit Wishart matrices by:");
        Console.WriteLine("  W = Wishart.wishart_unit_sample ( n, df );");
        //
        //  Set the parameters and call.
        //
        int n  = 5;
        int df = 8;

        double[] w = Wishart.wishart_unit_sample(n, df);
        typeMethods.r8mat_print(n, n, w, "  Wishart.wishart_unit_sample ( 5, 8 ):");
        //
        //  Calling again yields a new matrix.
        //
        w = Wishart.wishart_unit_sample(n, df);
        typeMethods.r8mat_print(n, n, w, "  Wishart.wishart_unit_sample ( 5, 8 ):");
        //
        //  Reduce DF
        //
        n  = 5;
        df = 5;
        w  = Wishart.wishart_unit_sample(n, df);
        typeMethods.r8mat_print(n, n, w, "  Wishart.wishart_unit_sample ( 5, 5 ):");
        //
        //  Try a smaller matrix.
        //
        n  = 3;
        df = 5;
        w  = Wishart.wishart_unit_sample(n, df);
        typeMethods.r8mat_print(n, n, w, "  Wishart.wishart_unit_sample ( 3, 5 ):");
        //
        //  What is the eigendecomposition of the matrix?
        //
        int it_max = 50;

        double[] v      = new double[n * n];
        double[] lambda = new double[n];

        Jacobi.jacobi_eigenvalue(n, w, it_max, ref v, ref lambda, ref it_num, ref rot_num);
        typeMethods.r8mat_print(n, n, v, "  Eigenvectors of previous matrix:");
        typeMethods.r8vec_print(n, lambda, "  Eigenvalues of previous matrix:");
    }
    public static void moment_method(int n, double[] moment, ref double[] x, ref double[] w)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    MOMENT_METHOD computes a quadrature rule by the method of moments.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    18 September 2013
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Reference:
    //
    //    Gene Golub, John Welsch,
    //    Calculation of Gaussian Quadrature Rules,
    //    Mathematics of Computation,
    //    Volume 23, Number 106, April 1969, pages 221-230.
    //
    //  Parameters:
    //
    //    Input, int N, the order of the quadrature rule.
    //
    //    Input, double MOMENT[2*N+1], moments 0 through 2*N.
    //
    //    Output, double X[N], W[N], the points and weights of the quadrature rule.
    //
    {
        int flag = 0;
        int i;
        int it_num = 0;
        int j;
        int rot_num = 0;

        const bool debug = false;

        switch (debug)
        {
        case true:
            typeMethods.r8vec_print(2 * n + 1, moment, "  Moments:");
            break;
        }

        //
        //  Define the N+1 by N+1 Hankel matrix H(I,J) = moment(I+J).
        //
        double[] h = new double[(n + 1) * (n + 1)];

        for (i = 0; i <= n; i++)
        {
            for (j = 0; j <= n; j++)
            {
                h[i + j * (n + 1)] = moment[i + j];
            }
        }

        switch (debug)
        {
        case true:
            typeMethods.r8mat_print(n + 1, n + 1, h, "  Hankel matrix:");
            break;
        }

        //
        //  Compute R, the upper triangular Cholesky factor of H.
        //
        double[] r = typeMethods.r8mat_cholesky_factor_upper(n + 1, h, ref flag);

        if (flag != 0)
        {
            Console.WriteLine("");
            Console.WriteLine("MOMENT_METHOD - Fatal error!");
            Console.WriteLine("  R8MAT_CHOLESKY_FACTOR_UPPER returned FLAG = " + flag + "");
            return;
        }

        switch (debug)
        {
        case true:
            typeMethods.r8mat_print(n + 1, n + 1, r, "  Cholesky factor:");
            break;
        }

        //
        //  Compute ALPHA and BETA from R, using Golub and Welsch's formula.
        //
        double[] alpha = new double[n];

        alpha[0] = r[0 + 1 * (n + 1)] / r[0 + 0 * (n + 1)];
        for (i = 1; i < n; i++)
        {
            alpha[i] = r[i + (i + 1) * (n + 1)] / r[i + i * (n + 1)]
                       - r[i - 1 + i * (n + 1)] / r[i - 1 + (i - 1) * (n + 1)];
        }

        double[] beta = new double[n - 1];

        for (i = 0; i < n - 1; i++)
        {
            beta[i] = r[i + 1 + (i + 1) * (n + 1)] / r[i + i * (n + 1)];
        }

        //
        //  Compute the points and weights from the moments.
        //
        double[] jacobi = new double[n * n];

        for (j = 0; j < n; j++)
        {
            for (i = 0; i < n; i++)
            {
                jacobi[i + j * n] = 0.0;
            }
        }

        for (i = 0; i < n; i++)
        {
            jacobi[i + i * n] = alpha[i];
        }

        for (i = 0; i < n - 1; i++)
        {
            jacobi[i + (i + 1) * n] = beta[i];
            jacobi[i + 1 + i * n]   = beta[i];
        }

        switch (debug)
        {
        case true:
            typeMethods.r8mat_print(n, n, jacobi, "  The Jacobi matrix:");
            break;
        }

        //
        //  Get the eigendecomposition of the Jacobi matrix.
        //
        const int it_max = 100;

        double[] v = new double[n * n];

        Jacobi.jacobi_eigenvalue(n, jacobi, it_max, ref v, ref x, ref it_num, ref rot_num);

        switch (debug)
        {
        case true:
            typeMethods.r8mat_print(n, n, v, "  Eigenvector");
            break;
        }

        for (i = 0; i < n; i++)
        {
            w[i] = moment[0] * Math.Pow(v[0 + i * n], 2);
        }
    }
Exemple #5
0
    private static void wishart_test05()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    WISHART_TEST05 demonstrates the Bartlett sampling function.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    02 August 2013
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        int it_num = 0;

        //
        //  Note that R is an upper triangular matrix,
        //  whose entries here are listed in column major order.
        //
        double[] r =
        {
            5.0, 0.0, 0.0,
            1.0, 4.0, 0.0,
            3.0, 2.0, 6.0
        }

        ;
        int rot_num = 0;

        double[] sigma_diag =
        {
            1.0, 2.0, 3.0, 4.0, 5.0
        }

        ;

        Console.WriteLine("");
        Console.WriteLine("WISHART_TEST05:");
        Console.WriteLine("  We can compute sample Bartlett matrices by:");
        Console.WriteLine("    T = Bartlett.bartlett_sample ( n, df, sigma );");
        //
        //   Set the parameters and call.
        //
        int n  = 5;
        int df = 8;

        double[] sigma = typeMethods.r8mat_identity_new(n);
        double[] t     = Bartlett.bartlett_sample(n, df, sigma);
        typeMethods.r8mat_print(n, n, t, "  Bartlett.bartlett_sample ( 5, 8, Identity ):");
        //
        //   Calling again yields a new matrix.
        //
        t = Bartlett.bartlett_sample(n, df, sigma);
        typeMethods.r8mat_print(n, n, t, "  Bartlett.bartlett_sample ( 5, 8, Identity ):");
        //
        //   Try a diagonal matrix.
        //
        sigma = typeMethods.r8mat_diagonal_new(n, sigma_diag);
        t     = Bartlett.bartlett_sample(n, df, sigma);
        typeMethods.r8mat_print(n, n, t, "  Bartlett.bartlett_sample ( 5, 8, diag(1,2,3,4,5) ):");
        //
        //   Try a smaller matrix.
        //
        n     = 3;
        df    = 3;
        sigma = typeMethods.r8mat_mtm_new(n, n, n, r, r);
        typeMethods.r8mat_print(n, n, sigma, "  Set covariance SIGMA:");
        t = Bartlett.bartlett_sample(n, df, sigma);
        typeMethods.r8mat_print(n, n, t, "  Bartlett.bartlett_sample ( 3, 3, sigma ):");
        //
        //   What is the eigendecomposition of T' * T?
        //
        double[] w      = typeMethods.r8mat_mtm_new(n, n, n, t, t);
        int      it_max = 50;

        double[] v      = new double[n * n];
        double[] lambda = new double[n];

        Jacobi.jacobi_eigenvalue(n, w, it_max, ref v, ref lambda, ref it_num, ref rot_num);
        typeMethods.r8mat_print(n, n, v, "  Eigenvectors of previous matrix:");
        typeMethods.r8vec_print(n, lambda, "  Eigenvalues of previous matrix:");
    }