Beispiel #1
0
    private static void test01()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST01 compares exact and estimated monomial integrals.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    18 January 2014
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        const int n = 4192;
        int       test;
        const int test_num = 11;

        Console.WriteLine("");
        Console.WriteLine("TEST01");
        Console.WriteLine("  Compare exact and estimated integrals ");
        Console.WriteLine("  over the length of the unit line in 1D.");
        //
        //  Get sample points.
        //
        int seed = 123456789;

        double[] x = Integrals.line01_sample(n, ref seed);

        Console.WriteLine("");
        Console.WriteLine("  Number of sample points used is " + n + "");
        Console.WriteLine("");
        Console.WriteLine("   E     MC-Estimate      Exact           Error");
        Console.WriteLine("");

        for (test = 1; test <= test_num; test++)
        {
            int e = test - 1;

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

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

            Console.WriteLine("  " + e.ToString(CultureInfo.InvariantCulture).PadLeft(2)
                              + "  " + result.ToString(CultureInfo.InvariantCulture).PadLeft(14)
                              + "  " + exact.ToString(CultureInfo.InvariantCulture).PadLeft(14)
                              + "  " + error.ToString(CultureInfo.InvariantCulture).PadLeft(10) + "");
        }
    }
Beispiel #2
0
    private static void triangle_unit_quad_test(int degree_max)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TRIANGLE_UNIT_QUAD_TEST tests the rules for the unit triangle.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    18 April 2008
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, int DEGREE_MAX, the maximum total degree of the
    //    monomials to check.
    //
    {
        const int DIM_NUM = 2;

        int[]       expon = new int[DIM_NUM];
        int         h     = 0;
        int         t     = 0;
        SubCompData data  = new();

        Console.WriteLine("");
        Console.WriteLine("TRIANGLE_UNIT_QUAD_TEST");
        Console.WriteLine("  For the unit triangle,");
        Console.WriteLine("  we approximate monomial integrals with:");
        Console.WriteLine("  QuadratureRule.triangle_unit_o01,");
        Console.WriteLine("  QuadratureRule.triangle_unit_o03,");
        Console.WriteLine("  QuadratureRule.triangle_unit_o03b,");
        Console.WriteLine("  QuadratureRule.triangle_unit_o06,");
        Console.WriteLine("  QuadratureRule.triangle_unit_o06b,");
        Console.WriteLine("  QuadratureRule.triangle_unit_o07,");
        Console.WriteLine("  QuadratureRule.triangle_unit_o12,");

        bool more = false;

        for (;;)
        {
            SubComp.subcomp_next(ref data, degree_max, DIM_NUM, ref expon, ref more, ref h, ref t);

            Console.WriteLine("");
            string cout = "  Monomial exponents: ";
            int    dim;
            for (dim = 0; dim < DIM_NUM; dim++)
            {
                cout += "  " + expon[dim].ToString(CultureInfo.InvariantCulture).PadLeft(2);
            }

            Console.WriteLine(cout);
            Console.WriteLine("");

            int      order = 1;
            double[] w     = new double[order];
            double[] xy    = new double[DIM_NUM * order];
            QuadratureRule.triangle_unit_o01(ref w, ref xy);
            double[] v    = Monomial.monomial_value(DIM_NUM, order, expon, xy);
            double   quad = QuadratureRule.triangle_unit_volume() * typeMethods.r8vec_dot_product(order, w, v);
            Console.WriteLine("  " + order.ToString(CultureInfo.InvariantCulture).PadLeft(6)
                              + "  " + quad.ToString(CultureInfo.InvariantCulture).PadLeft(14) + "");

            order = 3;
            w     = new double[order];
            xy    = new double[DIM_NUM * order];
            QuadratureRule.triangle_unit_o03(ref w, ref xy);
            v    = Monomial.monomial_value(DIM_NUM, order, expon, xy);
            quad = QuadratureRule.triangle_unit_volume() * typeMethods.r8vec_dot_product(order, w, v);
            Console.WriteLine("  " + order.ToString(CultureInfo.InvariantCulture).PadLeft(6)
                              + "  " + quad.ToString(CultureInfo.InvariantCulture).PadLeft(14) + "");

            order = 3;
            w     = new double[order];
            xy    = new double[DIM_NUM * order];
            QuadratureRule.triangle_unit_o03b(ref w, ref xy);
            v    = Monomial.monomial_value(DIM_NUM, order, expon, xy);
            quad = QuadratureRule.triangle_unit_volume() * typeMethods.r8vec_dot_product(order, w, v);
            Console.WriteLine("  " + order.ToString(CultureInfo.InvariantCulture).PadLeft(6)
                              + "  " + quad.ToString(CultureInfo.InvariantCulture).PadLeft(14) + "");

            order = 6;
            w     = new double[order];
            xy    = new double[DIM_NUM * order];
            QuadratureRule.triangle_unit_o06(ref w, ref xy);
            v    = Monomial.monomial_value(DIM_NUM, order, expon, xy);
            quad = QuadratureRule.triangle_unit_volume() * typeMethods.r8vec_dot_product(order, w, v);
            Console.WriteLine("  " + order.ToString(CultureInfo.InvariantCulture).PadLeft(6)
                              + "  " + quad.ToString(CultureInfo.InvariantCulture).PadLeft(14) + "");

            order = 6;
            w     = new double[order];
            xy    = new double[DIM_NUM * order];
            QuadratureRule.triangle_unit_o06b(ref w, ref xy);
            v    = Monomial.monomial_value(DIM_NUM, order, expon, xy);
            quad = QuadratureRule.triangle_unit_volume() * typeMethods.r8vec_dot_product(order, w, v);
            Console.WriteLine("  " + order.ToString(CultureInfo.InvariantCulture).PadLeft(6)
                              + "  " + quad.ToString(CultureInfo.InvariantCulture).PadLeft(14) + "");

            order = 7;
            w     = new double[order];
            xy    = new double[DIM_NUM * order];
            QuadratureRule.triangle_unit_o07(ref w, ref xy);
            v    = Monomial.monomial_value(DIM_NUM, order, expon, xy);
            quad = QuadratureRule.triangle_unit_volume() * typeMethods.r8vec_dot_product(order, w, v);
            Console.WriteLine("  " + order.ToString(CultureInfo.InvariantCulture).PadLeft(6)
                              + "  " + quad.ToString(CultureInfo.InvariantCulture).PadLeft(14) + "");

            order = 12;
            w     = new double[order];
            xy    = new double[DIM_NUM * order];
            QuadratureRule.triangle_unit_o12(ref w, ref xy);
            v    = Monomial.monomial_value(DIM_NUM, order, expon, xy);
            quad = QuadratureRule.triangle_unit_volume() * typeMethods.r8vec_dot_product(order, w, v);
            Console.WriteLine("  " + order.ToString(CultureInfo.InvariantCulture).PadLeft(6)
                              + "  " + quad.ToString(CultureInfo.InvariantCulture).PadLeft(14) + "");

            Console.WriteLine("");
            quad = QuadratureRule.triangle_unit_monomial(expon);
            Console.WriteLine("  " + " Exact"
                              + "  " + quad.ToString(CultureInfo.InvariantCulture).PadLeft(14) + "");

            if (!more)
            {
                break;
            }
        }
    }
    public static double monomial_quadrature(int dim_num, int point_num, int[] rule,
                                             double[] alpha, double[] beta, int[] expon, double[] weight, double[] x)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    MONOMIAL_QUADRATURE applies a quadrature rule to a monomial.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    17 October 2008
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, int DIM_NUM, the spatial dimension.
    //
    //    Input, int POINT_NUM, the number of points in the rule.
    //
    //    Input, int RULE[DIM_NUM], the component rules.
    //    1, Gauss-Legendre rule on [-1,+1];
    //    2, Gauss-Jacobi rule on [-1,+1];
    //    3, Gauss-Laguerre rule on [0,+oo);
    //    4, Generalized Gauss-Laguerre rule on [0,+oo);
    //    5, Gauss-Hermite rule on (-oo,+oo);
    //    6, Generalized Gauss-Hermite rule on (-oo,+oo).
    //
    //    Input, double ALPHA[DIM_NUM], BETA[DIM_NUM], parameters that
    //    may be needed for Jacobi, Generalized-Laguerre, or Generalized Hermite rules.
    //
    //    Input, int EXPON[DIM_NUM], the exponents.
    //
    //    Input, double WEIGHT[POINT_NUM], the quadrature weights.
    //
    //    Input, double X[DIM_NUM*POINT_NUM], the quadrature points.
    //
    //    Output, double MONOMIAL_QUADRATURE, the quadrature error.
    //
    {
        //
        //  Get the exact value of the integral of the unscaled monomial.
        //
        double exact = IntegralNS.Monomial.monomial_integral_mixed(dim_num, rule, alpha, beta, expon);

        //
        //  Evaluate the monomial at the quadrature points.
        //
        double[] value = Monomial.monomial_value(dim_num, point_num, expon, x);
        //
        //  Compute the weighted sum and divide by the exact value.
        //
        double quad       = typeMethods.r8vec_dot(point_num, weight, value);
        double quad_error = exact switch
        {
            //
            //  Error:
            //
            0.0 => Math.Abs(quad - exact),
            _ => Math.Abs(quad - exact) / Math.Abs(exact)
        };

        return(quad_error);
    }
}
    public static double monomial_quadrature(int dim_num, int[] expon, int point_num,
                                             double[] weight, double[] x)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    MONOMIAL_QUADRATURE applies a quadrature rule to a monomial.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    09 November 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 WEIGHT[POINT_NUM], the quadrature weights.
    //
    //    Input, double X[DIM_NUM*POINT_NUM], the quadrature points.
    //
    //    Output, double MONOMIAL_QUADRATURE, the quadrature error.
    //
    {
        int point;
        //
        //  Get the exact value of the integral of the unscaled monomial.
        //
        double scale = IntegralNS.Monomial.monomial_int01(dim_num, expon);

        //
        //  Evaluate the monomial at the quadrature points.
        //
        double[] value = Monomial.monomial_value(dim_num, point_num, x, expon);
        //
        //  Compute the weighted sum and divide by the exact value.
        //
        double quad = 0.0;

        for (point = 0; point < point_num; point++)
        {
            quad += weight[point] * value[point];
        }
        quad /= scale;
        //
        //  Error:
        //
        const double exact      = 1.0;
        double       quad_error = Math.Abs(quad - exact);

        return(quad_error);
    }
    public static double monomial_quadrature(int dim_num, int[] expon, int point_num,
                                             double[] weight, double[] x, int rule)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    MONOMIAL_QUADRATURE applies a quadrature rule to a monomial.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    09 November 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 WEIGHT[POINT_NUM], the quadrature weights.
    //
    //    Input, double X[DIM_NUM*POINT_NUM], the quadrature points.
    //
    //    Input, int RULE, the index of the rule.
    //    1, "CC", Clenshaw Curtis Closed Fully Nested rule.
    //    2, "F1", Fejer 1 Open Fully Nested rule.
    //    3, "F2", Fejer 2 Open Fully Nested rule.
    //    4, "GP", Gauss Patterson Open Fully Nested rule.
    //    5, "GL", Gauss Legendre Open Weakly Nested rule.
    //    6, "GH", Gauss Hermite Open Weakly Nested rule.
    //    7, "LG", Gauss Laguerre Open Non Nested rule.
    //
    //    Output, double MONOMIAL_QUADRATURE, the quadrature error.
    //
    {
        double exact;
        int    point;

        switch (rule)
        {
        //
        //  Get the exact value of the integral of the unscaled monomial.
        //
        case >= 1 and <= 5:
            exact = IntegralNS.Monomial.monomial_integral_legendre(dim_num, expon);
            break;

        case 6:
            exact = IntegralNS.Monomial.monomial_integral_hermite(dim_num, expon);
            break;

        case 7:
            exact = IntegralNS.Monomial.monomial_integral_laguerre(dim_num, expon);
            break;

        default:
            Console.WriteLine("");
            Console.WriteLine("MONOMIAL_QUADRATURE - Fatal error!");
            Console.WriteLine("  Unrecognized value of RULE.");
            return(1);
        }

        //
        //  Evaluate the monomial at the quadrature points.
        //
        double[] value = Monomial.monomial_value(dim_num, point_num, x, expon);
        //
        //  Compute the quadrature sum.
        //
        double quad = 0.0;

        for (point = 0; point < point_num; point++)
        {
            quad += weight[point] * value[point];
        }

        double quad_error = exact switch
        {
            //
            //  Absolute error if EXACT = 0, relative error otherwise:
            //
            0.0 => Math.Abs(quad - exact),
            _ => Math.Abs(quad - exact) / Math.Abs(exact)
        };

        return(quad_error);
    }