Ejemplo n.º 1
0
    public static double sphere01_monomial_quadrature(int[] expon, int point_num, double[] xyz,
                                                      double[] w)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    SPHERE01_MONOMIAL_QUADRATURE applies quadrature to a monomial in a sphere.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    05 July 2007
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, int DIM_NUM, the spatial dimension.
    //
    //    Input, int EXPON[DIM_NUM], the exponents.
    //
    //    Input, int POINT_NUM, the number of points in the rule.
    //
    //    Input, double XYZ[DIM_NUM*POINT_NUM], the quadrature points.
    //
    //    Input, double W[POINT_NUM], the quadrature weights.
    //
    //    Output, double SPHERE01_MONOMIAL_QUADRATURE, the quadrature error.
    //
    {
        //
        //  Get the exact value of the integral.
        //
        double exact = Integrals.sphere01_monomial_integral(expon);

        //
        //  Evaluate the monomial at the quadrature points.
        //
        double[] value = MonomialNS.Monomial.monomial_value(3, point_num, expon, xyz);
        //
        //  Compute the weighted sum.
        //
        double quad = typeMethods.r8vec_dot(point_num, w, value);
        //
        //  Error:
        //
        double quad_error = Math.Abs(quad - exact);

        return(quad_error);
    }
Ejemplo n.º 2
0
    private static void test01()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST01 uses SPHERE01_SAMPLE to estimate monomial integrands.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    06 January 2014
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        const int m = 3;
        int       test;
        const int test_num = 20;

        Console.WriteLine("");
        Console.WriteLine("TEST01");
        Console.WriteLine("  Estimate monomial integrands using Monte Carlo");
        Console.WriteLine("  over the surface of the unit sphere in 3D.");
        //
        //  Get sample points.
        //
        const int n    = 8192;
        int       seed = 123456789;

        double[] x = Integrals.sphere01_sample(n, ref seed);
        Console.WriteLine("");
        Console.WriteLine("  Number of sample points used is " + n + "");
        //
        //  Randomly choose X,Y,Z exponents between (0,0,0) and (9,9,9).
        //
        Console.WriteLine("");
        Console.WriteLine("  If any exponent is odd, the integral is zero.");
        Console.WriteLine("  We will restrict this test to randomly chosen even exponents.");
        Console.WriteLine("");
        Console.WriteLine("  Ex  Ey  Ez     MC-Estimate           Exact      Error");
        Console.WriteLine("");
        for (test = 1; test <= test_num; test++)
        {
            int[] e = UniformRNG.i4vec_uniform_ab_new(m, 0, 4, ref seed);
            int   i;
            for (i = 0; i < m; i++)
            {
                e[i] *= 2;
            }

            double[] value = Monomial.monomial_value(m, n, e, x);

            double result = Integrals.sphere01_area() * typeMethods.r8vec_sum(n, value) / n;
            double exact  = Integrals.sphere01_monomial_integral(e);
            double error  = Math.Abs(result - exact);

            Console.WriteLine("  " + e[0].ToString().PadLeft(2)
                              + "  " + e[1].ToString().PadLeft(2)
                              + "  " + e[2].ToString().PadLeft(2)
                              + "  " + result.ToString(CultureInfo.InvariantCulture).PadLeft(14)
                              + "  " + exact.ToString(CultureInfo.InvariantCulture).PadLeft(14)
                              + "  " + error.ToString(CultureInfo.InvariantCulture).PadLeft(14) + "");
        }
    }
Ejemplo n.º 3
0
    private static void test05(ref int[] e_save)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST05 tests SPHERE01_QUAD_ICOS2V.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    24 September 2010
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        int[] e = new int[3];
        int   i;
        int   n = 0;

        Console.WriteLine("");
        Console.WriteLine("TEST05");
        Console.WriteLine("  Approximate the integral of a function on the unit sphere.");
        Console.WriteLine("  SPHERE01_QUAD_ICOS2V uses vertices of spherical triangles.");
        Console.WriteLine("");
        Console.WriteLine("FACTOR         N        QUAD          EXACT         ERROR");

        for (i = 1; i <= 17; i++)
        {
            switch (i)
            {
            case 1:
                e[0] = 0;
                e[1] = 0;
                e[2] = 0;
                break;

            case 2:
                e[0] = 1;
                e[1] = 0;
                e[2] = 0;
                break;

            case 3:
                e[0] = 0;
                e[1] = 1;
                e[2] = 0;
                break;

            case 4:
                e[0] = 0;
                e[1] = 0;
                e[2] = 1;
                break;

            case 5:
                e[0] = 2;
                e[1] = 0;
                e[2] = 0;
                break;

            case 6:
                e[0] = 0;
                e[1] = 2;
                e[2] = 2;
                break;

            case 7:
                e[0] = 2;
                e[1] = 2;
                e[2] = 2;
                break;

            case 8:
                e[0] = 0;
                e[1] = 2;
                e[2] = 4;
                break;

            case 9:
                e[0] = 0;
                e[1] = 0;
                e[2] = 6;
                break;

            case 10:
                e[0] = 1;
                e[1] = 2;
                e[2] = 4;
                break;

            case 11:
                e[0] = 2;
                e[1] = 4;
                e[2] = 2;
                break;

            case 12:
                e[0] = 6;
                e[1] = 2;
                e[2] = 0;
                break;

            case 13:
                e[0] = 0;
                e[1] = 0;
                e[2] = 8;
                break;

            case 14:
                e[0] = 6;
                e[1] = 0;
                e[2] = 4;
                break;

            case 15:
                e[0] = 4;
                e[1] = 6;
                e[2] = 2;
                break;

            case 16:
                e[0] = 2;
                e[1] = 4;
                e[2] = 8;
                break;

            case 17:
                e[0] = 16;
                e[1] = 0;
                e[2] = 0;
                break;
            }

            polyterm_exponent("SET", ref e_save, ref e);

            polyterm_exponent("PRINT", ref e_save, ref e);

            int factor = 1;
            int factor_log;
            for (factor_log = 0; factor_log <= 5; factor_log++)
            {
                double result = Quad.sphere01_quad_icos2v(factor, polyterm_value_3d, ref n);

                double exact = Integrals.sphere01_monomial_integral(e);

                double error = Math.Abs(exact - result);

                Console.WriteLine("  " + factor.ToString().PadLeft(4)
                                  + "  " + n.ToString().PadLeft(8)
                                  + "  " + result.ToString(CultureInfo.InvariantCulture).PadLeft(14)
                                  + "  " + exact.ToString(CultureInfo.InvariantCulture).PadLeft(14)
                                  + "  " + error.ToString(CultureInfo.InvariantCulture).PadLeft(14) + "");

                factor *= 2;
            }
        }
    }
Ejemplo n.º 4
0
    private static void test01(ref int[] e_save)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST01 tests SPHERE01_QUAD_LL*.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    25 September 2010
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        int[]  e = new int[3];
        double h = 0;
        int    i;
        int    n_llc = 0;
        int    n_llm = 0;
        int    n_llv = 0;

        int seed = 123456789;

        Console.WriteLine("");
        Console.WriteLine("TEST01");
        Console.WriteLine("  Approximate the integral of a function on the unit sphere.");
        Console.WriteLine("");
        Console.WriteLine("  SPHERE01_QUAD_MC uses a Monte Carlo method.");
        Console.WriteLine("  SPHERE01_QUAD_LLC uses centroids of spherical triangles.");
        Console.WriteLine("  SPHERE01_QUAD_LLM uses midsides of spherical triangles.");
        Console.WriteLine("  SPHERE01_QUAD_LLV uses vertices of spherical triangles.");
        Console.WriteLine("");
        Console.WriteLine("  H              QUAD_MC       QUAD_LLC      QUAD_LLM      QUAD_LLV         EXACT");

        for (i = 0; i <= 17; i++)
        {
            switch (i)
            {
            case 0:
                e[0] = 0;
                e[1] = 0;
                e[2] = 0;
                break;

            case 1:
                e[0] = 0;
                e[1] = 0;
                e[2] = 0;
                break;

            case 2:
                e[0] = 1;
                e[1] = 0;
                e[2] = 0;
                break;

            case 3:
                e[0] = 0;
                e[1] = 1;
                e[2] = 0;
                break;

            case 4:
                e[0] = 0;
                e[1] = 0;
                e[2] = 1;
                break;

            case 5:
                e[0] = 2;
                e[1] = 0;
                e[2] = 0;
                break;

            case 6:
                e[0] = 0;
                e[1] = 2;
                e[2] = 2;
                break;

            case 7:
                e[0] = 2;
                e[1] = 2;
                e[2] = 2;
                break;

            case 8:
                e[0] = 0;
                e[1] = 2;
                e[2] = 4;
                break;

            case 9:
                e[0] = 0;
                e[1] = 0;
                e[2] = 6;
                break;

            case 10:
                e[0] = 1;
                e[1] = 2;
                e[2] = 4;
                break;

            case 11:
                e[0] = 2;
                e[1] = 4;
                e[2] = 2;
                break;

            case 12:
                e[0] = 6;
                e[1] = 2;
                e[2] = 0;
                break;

            case 13:
                e[0] = 0;
                e[1] = 0;
                e[2] = 8;
                break;

            case 14:
                e[0] = 6;
                e[1] = 0;
                e[2] = 4;
                break;

            case 15:
                e[0] = 4;
                e[1] = 6;
                e[2] = 2;
                break;

            case 16:
                e[0] = 2;
                e[1] = 4;
                e[2] = 8;
                break;

            case 17:
                e[0] = 16;
                e[1] = 0;
                e[2] = 0;
                break;
            }

            polyterm_exponent("SET", ref e_save, ref e);

            switch (i)
            {
            case 0:
                Console.WriteLine("");
                Console.WriteLine("Point counts per method:");
                break;

            default:
                polyterm_exponent("PRINT", ref e_save, ref e);
                break;
            }

            int h_test;
            for (h_test = 1; h_test <= 3; h_test++)
            {
                h = h_test switch
                {
                    1 => 1.0,
                    2 => 0.1,
                    3 => 0.01,
                    _ => h
                };

                int n_mc = Quad.sphere01_quad_mc_size(h);

                double result_mc = Quad.sphere01_quad_mc(polyterm_value_3d, h, ref seed, n_mc);

                double result_llc = Quad.sphere01_quad_llc(polyterm_value_3d, h, ref n_llc);

                double result_llm = Quad.sphere01_quad_llm(polyterm_value_3d, h, ref n_llm);

                double result_llv = Quad.sphere01_quad_llv(polyterm_value_3d, h, ref n_llv);

                double exact = Integrals.sphere01_monomial_integral(e);

                switch (i)
                {
                case 0:
                    Console.WriteLine("  " + h.ToString(CultureInfo.InvariantCulture).PadLeft(12)
                                      + "  " + n_mc.ToString().PadLeft(12)
                                      + "  " + n_llc.ToString().PadLeft(12)
                                      + "  " + n_llm.ToString().PadLeft(12)
                                      + "  " + n_llv.ToString().PadLeft(12) + "");
                    break;

                default:
                    Console.WriteLine("  " + h.ToString(CultureInfo.InvariantCulture).PadLeft(12)
                                      + "  " + result_mc.ToString(CultureInfo.InvariantCulture).PadLeft(12)
                                      + "  " + result_llc.ToString(CultureInfo.InvariantCulture).PadLeft(12)
                                      + "  " + result_llm.ToString(CultureInfo.InvariantCulture).PadLeft(12)
                                      + "  " + result_llv.ToString(CultureInfo.InvariantCulture).PadLeft(12)
                                      + "  " + exact.ToString(CultureInfo.InvariantCulture).PadLeft(12) + "");
                    break;
                }
            }
        }
    }