Example #1
0
    private static void test01()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST01 tests MGMRES_ST on the simple -1,2-1 matrix.
    //
    //  Discussion:
    //
    //    This is a very weak test, since the matrix has such a simple
    //    structure, is diagonally dominant (though not strictly),
    //    and is symmetric.
    //
    //    To make the matrix bigger, simply increase the value of N.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    13 July 2007
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        const int N      = 20;
        const int NZ_NUM = 3 * N - 2;

        double[] a = new double[NZ_NUM];
        int      i;

        int[] ia      = new int[NZ_NUM];
        int   itr_max = 0;

        int[] ja = new int[NZ_NUM];
        int   mr = 0;

        double[] rhs = new double[N];
        int      test;

        double[] x_estimate = new double[N];
        double[] x_exact    = new double[N];

        Console.WriteLine("");
        Console.WriteLine("TEST01");
        Console.WriteLine("  Test MGMRES_ST on the simple -1,2-1 matrix.");
        //
        //  Set the matrix.
        //  Note that we use zero based index values in IA and JA.
        //
        int k = 0;

        for (i = 0; i < N; i++)
        {
            switch (i)
            {
            case > 0:
                ia[k] = i;
                ja[k] = i - 1;
                a[k]  = -1.0;
                k    += 1;
                break;
            }

            ia[k] = i;
            ja[k] = i;
            a[k]  = 2.0;
            k    += 1;

            if (i >= N - 1)
            {
                continue;
            }

            ia[k] = i;
            ja[k] = i + 1;
            a[k]  = -1.0;
            k    += 1;
        }

        //
        //  Set the right hand side:
        //
        for (i = 0; i < N - 1; i++)
        {
            rhs[i] = 0.0;
        }

        rhs[N - 1] = N + 1;
        //
        //  Set the exact solution.
        //
        for (i = 0; i < N; i++)
        {
            x_exact[i] = i + 1;
        }

        for (test = 1; test <= 3; test++)
        {
            //
            //  Set the initial solution estimate.
            //
            for (i = 0; i < N; i++)
            {
                x_estimate[i] = 0.0;
            }

            double x_error = 0.0;
            for (i = 0; i < N; i++)
            {
                x_error += Math.Pow(x_exact[i] - x_estimate[i], 2);
            }

            x_error = Math.Sqrt(x_error);

            switch (test)
            {
            case 1:
                itr_max = 1;
                mr      = 20;
                break;

            case 2:
                itr_max = 2;
                mr      = 10;
                break;

            case 3:
                itr_max = 5;
                mr      = 4;
                break;
            }

            const double tol_abs = 1.0E-08;
            const double tol_rel = 1.0E-08;

            Console.WriteLine("");
            Console.WriteLine("  Test " + test + "");
            Console.WriteLine("  Matrix order N = " + N + "");
            Console.WriteLine("  Inner iteration limit = " + mr + "");
            Console.WriteLine("  Outer iteration limit = " + itr_max + "");
            Console.WriteLine("  Initial X_ERROR = " + x_error + "");

            RestartedGeneralizedMinimumResidual.mgmres_st(N, NZ_NUM, ia, ja, a, ref x_estimate, rhs, itr_max, mr,
                                                          tol_abs, tol_rel);

            x_error = 0.0;
            for (i = 0; i < N; i++)
            {
                x_error += Math.Pow(x_exact[i] - x_estimate[i], 2);
            }

            x_error = Math.Sqrt(x_error);

            Console.WriteLine("  Final X_ERROR = " + x_error + "");
        }
    }
Example #2
0
    private static void test02()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST02 tests MGMRES_ST on a 9 by 9 matrix.
    //
    //  Discussion:
    //
    //    A =
    //      2  0  0 -1  0  0  0  0  0
    //      0  2 -1  0  0  0  0  0  0
    //      0 -1  2  0  0  0  0  0  0
    //     -1  0  0  2 -1  0  0  0  0
    //      0  0  0 -1  2 -1  0  0  0
    //      0  0  0  0 -1  2 -1  0  0
    //      0  0  0  0  0 -1  2 -1  0
    //      0  0  0  0  0  0 -1  2 -1
    //      0  0  0  0  0  0  0 -1  2
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    18 October 2006
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        const int N      = 9;
        const int NZ_NUM = 23;

        double[] a =
        {
            2.0,  -1.0,
            2.0,  -1.0,
            -1.0,  2.0,
            -1.0,  2.0, -1.0,
            -1.0,  2.0, -1.0,
            -1.0,  2.0, -1.0,
            -1.0,  2.0, -1.0,
            -1.0,  2.0, -1.0,
            -1.0, 2.0
        };
        int[] ia =
        {
            0, 0,
            1, 1,
            2, 2,
            3, 3, 3,
            4, 4, 4,
            5, 5, 5,
            6, 6, 6,
            7, 7, 7,
            8, 8
        };
        int[] ja =
        {
            0, 3,
            1, 2,
            1, 2,
            0, 3, 4,
            3, 4, 5,
            4, 5, 6,
            5, 6, 7,
            6, 7, 8,
            7, 8
        };
        double[] rhs =
        {
            1.0,
            1.0,
            1.0,
            1.0,
            1.0,
            1.0,
            1.0,
            1.0,
            1.0
        };
        int seed = 123456789;
        int test;

        double[] x_estimate = new double[1];
        double[] x_exact    =
        {
            3.5,
            1.0,
            1.0,
            6.0,
            7.5,
            8.0,
            7.5,
            6.0,
            3.5
        };

        Console.WriteLine("");
        Console.WriteLine("TEST02");
        Console.WriteLine("  Test MGMRES_ST on matrix that is not quite the -1,2,-1 matrix,");
        Console.WriteLine("  of order N = " + N + "");

        for (test = 1; test <= 2; test++)
        {
            int i;
            switch (test)
            {
            case 1:
            {
                Console.WriteLine("");
                Console.WriteLine("  First try, use zero initial vector:");
                Console.WriteLine("");

                x_estimate = new double[N];
                for (i = 0; i < N; i++)
                {
                    x_estimate[i] = 0.0;
                }

                break;
            }

            default:
                Console.WriteLine("");
                Console.WriteLine("  Second try, use random initial vector:");
                Console.WriteLine("");

                UniformRNG.r8vec_uniform_01(N, ref seed, ref x_estimate);
                break;
            }

            //
            //  Set the initial solution estimate.
            //
            double x_error = 0.0;
            for (i = 0; i < N; i++)
            {
                x_error += Math.Pow(x_exact[i] - x_estimate[i], 2);
            }

            x_error = Math.Sqrt(x_error);

            Console.WriteLine("  Before solving, X_ERROR = " + x_error + "");

            const int    itr_max = 20;
            const int    mr      = N - 1;
            const double tol_abs = 1.0E-08;
            const double tol_rel = 1.0E-08;

            RestartedGeneralizedMinimumResidual.mgmres_st(N, NZ_NUM, ia, ja, a, ref x_estimate, rhs, itr_max, mr,
                                                          tol_abs, tol_rel);

            x_error = 0.0;
            for (i = 0; i < N; i++)
            {
                x_error += Math.Pow(x_exact[i] - x_estimate[i], 2);
            }

            x_error = Math.Sqrt(x_error);

            Console.WriteLine("  After solving, X_ERROR = " + x_error + "");

            Console.WriteLine("");
            Console.WriteLine("  Final solution estimate:");
            Console.WriteLine("");
            for (i = 0; i < N; i++)
            {
                Console.WriteLine("  " + i.ToString().PadLeft(8)
                                  + "  " + x_estimate[i].ToString(CultureInfo.InvariantCulture).PadLeft(12) + "");
            }
        }
    }