コード例 #1
0
    private static void test02()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST02 examines the sample points in the pyramid
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    14 April 2014
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        const int n = 20;

        Console.WriteLine("");
        Console.WriteLine("TEST02");
        Console.WriteLine("  Print sample points in the unit pyramid in 3D.");
        int seed = 123456789;

        double[] x = Integrals.pyramid01_sample(n, ref seed);
        typeMethods.r8mat_transpose_print(3, n, x, "  Unit pyramid points");
    }
コード例 #2
0
ファイル: IntegralsTests.cs プロジェクト: temeer/mathlib
        public void RectangularInfititeTest()
        {
            var integral = new Integral(t => Pow(E, -t), 0, double.PositiveInfinity);
            var result   = Integrals.RectangularInfinite(integral, 100000, 10);

            Assert.That(result, Is.EqualTo(1).Within(0.0001));
        }
コード例 #3
0
ファイル: OdeSolver.cs プロジェクト: rasuldev/odelaguerre
        public virtual double HCoeffs(int s)
        {
            // TODO: use Gauss quadrature formula
            var coeffSym = mathlib.ScalarMul.LebesgueLaguerre(_right, Laguerre.Get(s));

            return(Integrals.RectangularInfinite(coeffSym, 100000, 10));
        }
コード例 #4
0
        /// <summary>
        /// Calculates $c_k(f_j)$
        /// </summary>
        /// <param name="i"></param>
        /// <param name="k"></param>
        /// <param name="fArgs"></param>
        /// <returns></returns>
        private double CalcCoeff(int i, int k, double[][] fArgs)
        {
            // qk[j] = $f_k(h t_j, \eta_0(t_j), ..., \eta_{m-1}(t_j)) * phi_k(t_j)$,
            // so qk is a vector of values of function $q_k(t)=f_k(ht,\eta_0(t),...) phi(t)$ in _nodes
            var qk = _nodes.Select((t, j) => _f[i].Invoke(fArgs[j]) * _phiCached[k](t));

            return(Integrals.Trapezoid(qk.ToArray(), _nodes));
        }
コード例 #5
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) + "");
        }
    }
コード例 #6
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);
    }
コード例 #7
0
ファイル: IntegralsTests.cs プロジェクト: temeer/mathlib
 public void TrapezoidDiscreteTest()
 {
     RunTestCases(
         testCase =>
     {
         var nodes = GenerateNodes(testCase.A, testCase.B, testCase.NodesCount);
         var fD    = nodes.Select(t => testCase.Func(t)).ToArray();
         return(Integrals.Trapezoid(fD, nodes));
     });
 }
コード例 #8
0
    private static void test11()

    //****************************************************************************
    //
    //  Purpose:
    //
    //    TEST11 tests STOCHASTIC_INTEGRAL_STRAT.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    28 September 2012
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        double error    = 0;
        double estimate = 0;
        double exact    = 0;
        int    i;

        Console.WriteLine("");
        Console.WriteLine("TEST11:");
        Console.WriteLine("  Estimate the Stratonovich integral of W(t) dW over [0,1].");
        Console.WriteLine("");
        Console.WriteLine("                                                 Abs          Rel");
        Console.WriteLine("         N        Exact        Estimate          Error        Error");
        Console.WriteLine("");

        int n    = 100;
        int seed = 123456789;

        typeMethods.r8vecNormalData data = new();

        for (i = 1; i <= 7; i++)
        {
            Integrals.stochastic_integral_strat(n, ref data, ref seed, ref estimate, ref exact, ref error);

            Console.WriteLine("  " + n.ToString().PadLeft(8)
                              + "  " + exact.ToString(CultureInfo.InvariantCulture).PadLeft(16)
                              + "  " + estimate.ToString(CultureInfo.InvariantCulture).PadLeft(16)
                              + "  " + error.ToString(CultureInfo.InvariantCulture).PadLeft(16)
                              + "  " + (error / exact).ToString(CultureInfo.InvariantCulture).PadLeft(16) + "");

            n *= 4;
        }
    }
コード例 #9
0
        public void ShouldSuccessCalculateIntegralViaMidRectMethod(double a, double b, int n, double expectedResult, double eps)
        {
            IntegrateOptions options = new IntegrateOptions()
            {
                StartX   = a,
                EndX     = b,
                Steps    = n,
                Function = TestFunc
            };
            double actualResult = Integrals.MidRect(options);

            Assert.IsTrue(Math.Abs(expectedResult - actualResult) <= eps);
        }
コード例 #10
0
        static void Main(string[] args)
        {
            double a = -1, b = 0.9;
            int    n = 100;

            IntegrateOptions options = new IntegrateOptions()
            {
                StartX   = a,
                EndX     = b,
                Steps    = n,
                Function = x => 20 * x - 2.5 / (x - 1)
            };
            double S = Integrals.MidRect(options);

            Console.WriteLine("Result {0:#.###E+00}", S);
        }
コード例 #11
0
        public void CosSytemOrthonormality()
        {
            var cosSystem = new CosSystem();

            for (int i = 0; i < 100; i++)
            {
                var i1 = i;
                var v  = Integrals.Trapezoid(x => cosSystem.Get(i1)(x) * cosSystem.Get(i1)(x), 0, 1, 10000);
                Assert.AreEqual(1, v, 0.0000001);
            }

            var val = Integrals.Trapezoid(x => cosSystem.Get(0)(x) * cosSystem.Get(5)(x), 0, 1, 10000);

            Assert.AreEqual(0, val, 0.0000001);

            val = Integrals.Trapezoid(x => cosSystem.Get(4)(x) * cosSystem.Get(5)(x), 0, 1, 10000);
            Assert.AreEqual(0, val, 0.0000001);
        }
コード例 #12
0
ファイル: WalshSobolev.cs プロジェクト: temeer/mathlib
        public static Func <double, double> Get(int n)
        {
            double One(double x) => 1;

            if (n == 0)
            {
                return(One);
            }

            var nodesCount = (int)Math.Pow(2, (int)Math.Ceiling(Math.Log(n, 2)) + 6);
            var nodes      = Enumerable.Range(0, nodesCount).Select(j => j * 1.0 / nodesCount);

            double Sobolev(double x)
            {
                return(Integrals.Rectangular(Walsh.Get(n - 1), nodes.Where(node => node <= x).ToArray(),
                                             Integrals.RectType.Center));
            }

            return(Sobolev);
        }
コード例 #13
0
    private static void Main()
    //****************************************************************************80
    //
    //  Purpose:
    //
    //    MAIN is the main program for CUBE_FELIPPA_RULE_TEST.
    //
    //  Discussion:
    //
    //    CUBE_FELIPPA_RULE_TEST tests the CUBE_FELIPPA_RULE library.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    05 September 2014
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        Console.WriteLine("");
        Console.WriteLine("CUBE_FELIPPA_RULE_TEST");
        Console.WriteLine("  Test the CUBE_FELIPPA_RULE library.");

        int degree_max = 4;

        Integrals.cube_monomial_test(degree_max);

        degree_max = 6;
        QuadratureRule.cube_quad_test(degree_max);

        Console.WriteLine("");
        Console.WriteLine("CUBE_FELIPPA_RULE_TEST");
        Console.WriteLine("  Normal end of execution.");
        Console.WriteLine("");
    }
コード例 #14
0
    private static void triangle_area_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TRIANGLE_AREA_TEST tests TRIANGLE_AREA.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    04 November 2015
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        double[] t =
        {
            0.0, 1.0,
            0.0, 0.0,
            1.0, 0.0
        };

        Console.WriteLine("");
        Console.WriteLine("TRIANGLE_AREA_TEST");
        Console.WriteLine("  TRIANGLE_AREA computes the area of a triangle.");

        typeMethods.r8mat_transpose_print(2, 3, t, "  Triangle vertices:");

        double area = Integrals.triangle_area(t);

        Console.WriteLine("");
        Console.WriteLine("  Triangle area is " + area + "");
    }
コード例 #15
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;
            }
        }
    }
コード例 #16
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;
                }
            }
        }
    }
コード例 #17
0
    private static void test01()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST01 estimates integrals over the unit cube in 3D.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    19 January 2014
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        const int m = 3;
        const int n = 4192;
        int       test;
        const int test_num = 20;

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

        double[] x = Integrals.cube01_sample(n, ref seed);
        Console.WriteLine("");
        Console.WriteLine("  Number of sample points is " + n + "");
        //
        //  Randomly choose 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, 7, ref seed);

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

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

            Console.WriteLine("  " + e[0].ToString(CultureInfo.InvariantCulture).PadLeft(2)
                              + "  " + e[1].ToString(CultureInfo.InvariantCulture).PadLeft(2)
                              + "  " + e[2].ToString(CultureInfo.InvariantCulture).PadLeft(2)
                              + "  " + result.ToString(CultureInfo.InvariantCulture).PadLeft(14)
                              + "  " + exact.ToString(CultureInfo.InvariantCulture).PadLeft(14)
                              + "  " + error.ToString(CultureInfo.InvariantCulture).PadLeft(14) + "");
        }
    }
コード例 #18
0
ファイル: IntegralsTests.cs プロジェクト: temeer/mathlib
 public void TrapezoidEquiDistantNetTest()
 {
     RunTestCases(
         testCase => Integrals.Trapezoid(testCase.Func, testCase.A, testCase.B, testCase.NodesCount)
         );
 }
コード例 #19
0
    private static void square01_monomial_integral_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    SQUARE01_MONOMIAL_INTEGRAL_TEST tests SQUARE01_MONOMIAL_INTEGRAL.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    21 February 2018
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        const int m = 2;
        const int n = 4192;
        int       test;
        const int test_num = 20;

        Console.WriteLine("");
        Console.WriteLine("SQUARE01_MONOMIAL_INTEGRAL_TEST");
        Console.WriteLine("  SQUARE01_MONOMIAL_INTEGRAL returns the exact integral");
        Console.WriteLine("  of a monomial over the interior of the unit square in 2D.");
        Console.WriteLine("  Compare exact and estimated values.");
        //
        //  Get sample points.
        //
        int seed = 123456789;

        double[] x = Integrals.square01_sample(n, ref seed);
        Console.WriteLine("");
        Console.WriteLine("  Number of sample points is " + n + "");
        //
        //  Randomly choose exponents.
        //
        Console.WriteLine("");
        Console.WriteLine("  Ex  Ey     MC-Estimate           Exact      Error");
        Console.WriteLine("");

        for (test = 1; test <= test_num; test++)
        {
            int[] e = UniformRNG.i4vec_uniform_ab_new(m, 0, 7, ref seed);

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

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

            Console.WriteLine("  " + e[0].ToString().PadLeft(2)
                              + "  " + e[1].ToString().PadLeft(2)
                              + "  " + result.ToString(CultureInfo.InvariantCulture).PadLeft(14)
                              + "  " + exact.ToString(CultureInfo.InvariantCulture).PadLeft(14)
                              + "  " + error.ToString(CultureInfo.InvariantCulture).PadLeft(14) + "");
        }
    }
コード例 #20
0
    public static double square_minimal_rule_error_max(int degree)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    SQUARE_MINIMAL_RULE_ERROR_MAX returns the maximum error.
    //
    //  Discussion:
    //
    //    The rule of given DEGREE should theoretically have zero error
    //    for all monomials of degrees 0 <= D <= DEGREE.  This function
    //    checks every such monomial and reports the maximum error.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU GPL license.
    //
    //  Modified:
    //
    //    22 February 2018
    //
    //  Author:
    //
    //    John Burkardt.
    //
    //  Reference:
    //
    //    Mattia Festa, Alvise Sommariva,
    //    Computing almost minimal formulas on the square,
    //    Journal of Computational and Applied Mathematics,
    //    Volume 17, Number 236, November 2012, pages 4296-4302.
    //
    //  Parameters:
    //
    //    Input, int DEGREE, the desired total polynomial degree exactness
    //    of the quadrature rule.
    //
    //    Output, double SQUARE_MINIMAL_RULE_ERROR_MAX, the maximum error observed
    //    when using the rule to compute the integrals of all monomials of degree
    //    between 0 and DEGREE.
    //
    {
        int d;

        int[] e = new int[2];

        int order = square_minimal_rule_order(degree);

        double[] xyw = square_minimal_rule(degree);

        double error_max = 0.0;

        for (d = 0; d <= degree; d++)
        {
            int i;
            for (i = 0; i <= d; i++)
            {
                int j = d - i;
                e[0] = i;
                e[1] = j;
                double exact = Integrals.squaresym_monomial_integral(e);
                double s     = 0.0;
                int    k;
                for (k = 0; k < order; k++)
                {
                    s += xyw[2 + k * 3] * Math.Pow(xyw[0 + k * 3], i) * Math.Pow(xyw[1 + k * 3], j);
                }

                double err = Math.Abs(exact - s);
                if (error_max < err)
                {
                    error_max = err;
                }
            }
        }

        return(error_max);
    }
コード例 #21
0
    private static void Main(string[] args)
    //****************************************************************************80
    //
    //  Purpose:
    //
    //    MAIN is the main program for TRIANGLE_EXACTNESS.
    //
    //  Discussion:
    //
    //    This program investigates the polynomial exactness of a quadrature
    //    rule for the triangle.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    31 July 2009
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        int    degree;
        int    degree_max;
        bool   error = false;
        int    last  = 0;
        int    point;
        string quad_filename;

        Console.WriteLine("");
        Console.WriteLine("TRIANGLE_EXACTNESS");
        Console.WriteLine("");
        Console.WriteLine("  Investigate the polynomial exactness of a quadrature");
        Console.WriteLine("  rule for the triangle by integrating all monomials");
        Console.WriteLine("  of a given degree.");
        Console.WriteLine("");
        Console.WriteLine("  The rule will be adjusted to the unit triangle.");
        //
        //  Get the quadrature file root name:
        //
        try
        {
            quad_filename = args[0];
        }
        catch
        {
            Console.WriteLine("");
            Console.WriteLine("TRIANGLE_EXACTNESS:");
            Console.WriteLine("  Enter the \"root\" name of the quadrature files.");

            quad_filename = Console.ReadLine();
        }

        //
        //  Create the names of:
        //    the quadrature X file;
        //    the quadrature W file;
        //    the quadrature R file;
        //
        string quad_r_filename = quad_filename + "_r.txt";
        string quad_w_filename = quad_filename + "_w.txt";
        string quad_x_filename = quad_filename + "_x.txt";

        //
        //  The second command line argument is the maximum degree.
        //
        try
        {
            degree_max = typeMethods.s_to_i4(args[1], ref last, ref error);
        }
        catch
        {
            Console.WriteLine("");
            Console.WriteLine("TRIANGLE_EXACTNESS:");
            Console.WriteLine("  Please enter the maximum total degree to check.");

            degree_max = Convert.ToInt32(Console.ReadLine());
        }

        //
        //  Summarize the input.
        //
        Console.WriteLine("");
        Console.WriteLine("TRIANGLE_EXACTNESS: User input:");
        Console.WriteLine("  Quadrature rule X file = \"" + quad_x_filename
                          + "\".");
        Console.WriteLine("  Quadrature rule W file = \"" + quad_w_filename
                          + "\".");
        Console.WriteLine("  Quadrature rule R file = \"" + quad_r_filename
                          + "\".");
        Console.WriteLine("  Maximum total degree to check = " + degree_max + "");
        //
        //  Read the X file.
        //
        TableHeader hdr       = typeMethods.r8mat_header_read(quad_x_filename);
        int         dim_num   = hdr.m;
        int         point_num = hdr.n;

        Console.WriteLine("");
        Console.WriteLine("  Spatial dimension = " + dim_num + "");
        Console.WriteLine("  Number of points  = " + point_num + "");

        if (dim_num != 2)
        {
            Console.WriteLine("");
            Console.WriteLine("TRIANGLE_EXACTNESS - Fatal error!");
            Console.WriteLine("  The quadrature abscissas must be two dimensional.");
            return;
        }

        double[] x = typeMethods.r8mat_data_read(quad_x_filename, dim_num, point_num);
        //
        //  Read the W file.
        //
        hdr = typeMethods.r8mat_header_read(quad_w_filename);
        int dim_num2   = hdr.m;
        int point_num2 = hdr.n;

        if (dim_num2 != 1)
        {
            Console.WriteLine("");
            Console.WriteLine("TRIANGLE_EXACTNESS - Fatal error!");
            Console.WriteLine("  The quadrature weight file should have exactly");
            Console.WriteLine("  one value on each line.");
            return;
        }

        if (point_num2 != point_num)
        {
            Console.WriteLine("");
            Console.WriteLine("TRIANGLE_EXACTNESS - Fatal error!");
            Console.WriteLine("  The quadrature weight file should have exactly");
            Console.WriteLine("  the same number of lines as the abscissa file.");
            return;
        }

        double[] w = typeMethods.r8mat_data_read(quad_w_filename, 1, point_num);
        //
        //  Read the R file.
        //
        hdr = typeMethods.r8mat_header_read(quad_r_filename);
        int dim_num3   = hdr.m;
        int point_num3 = hdr.n;

        if (dim_num3 != dim_num)
        {
            Console.WriteLine("");
            Console.WriteLine("TRIANGLE_EXACTNESS - Fatal error!");
            Console.WriteLine("  The quadrature region file should have the same");
            Console.WriteLine("  number of values on each line as the abscissa file");
            Console.WriteLine("  does.");
            return;
        }

        if (point_num3 != 3)
        {
            Console.WriteLine("");
            Console.WriteLine("TRIANGLE_EXACTNESS - Fatal error!");
            Console.WriteLine("  The quadrature region file should have 3 lines.");
            return;
        }

        double[] r = typeMethods.r8mat_data_read(quad_r_filename, dim_num, 3);
        //
        //  Rescale the weights.
        //
        double area = Integrals.triangle_area(r);

        for (point = 0; point < point_num; point++)
        {
            w[point] = 0.5 * w[point] / area;
        }

        //
        //  Translate the abscissas.
        //
        double[] x_ref = new double[2 * point_num];

        Triangulation.triangle_order3_physical_to_reference(r, point_num, x, ref x_ref);
        //
        //  Explore the monomials.
        //
        int[] expon = new int[dim_num];

        Console.WriteLine("");
        Console.WriteLine("      Error    Degree  Exponents");

        for (degree = 0; degree <= degree_max; degree++)
        {
            Console.WriteLine("");
            bool more = false;
            int  h    = 0;
            int  t    = 0;

            for (;;)
            {
                Comp.comp_next(degree, dim_num, ref expon, ref more, ref h, ref t);

                double quad_error = Integrals.triangle01_monomial_quadrature(dim_num, expon, point_num,
                                                                             x_ref, w);

                string cout = "  " + quad_error.ToString(CultureInfo.InvariantCulture).PadLeft(12)
                              + "     " + degree.ToString().PadLeft(2)
                              + "  ";

                int dim;
                for (dim = 0; dim < dim_num; dim++)
                {
                    cout += expon[dim].ToString().PadLeft(3);
                }

                Console.WriteLine(cout);

                if (!more)
                {
                    break;
                }
            }
        }

        Console.WriteLine("");
        Console.WriteLine("TRIANGLE_EXACTNESS:");
        Console.WriteLine("  Normal end of execution.");
        Console.WriteLine("");
    }
コード例 #22
0
    public static double[] sample_quad_new(double[] quad_xy, int n, ref int seed)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    SAMPLE_QUAD_NEW returns random points in a quadrilateral.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    22 February 2009
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, double QUAD_XY[2*4], the coordinates of the nodes.
    //
    //    Input, int N, the number of points to sample.
    //
    //    Input/output, int *SEED, a seed for the random
    //     number generator.
    //
    //    Output, double SAMPLE_QUAD[2*N], the sample points.
    //
    {
        int i;

        double[] t1 = new double[2 * 3];
        double[] t2 = new double[2 * 3];

        t1[0 + 0 * 2] = quad_xy[0 + 0 * 2];
        t1[1 + 0 * 2] = quad_xy[1 + 0 * 2];
        t1[0 + 1 * 2] = quad_xy[0 + 1 * 2];
        t1[1 + 1 * 2] = quad_xy[1 + 1 * 2];
        t1[0 + 2 * 2] = quad_xy[0 + 2 * 2];
        t1[1 + 2 * 2] = quad_xy[1 + 2 * 2];

        double area1 = Integrals.triangle_area(t1);

        t2[0 + 0 * 2] = quad_xy[0 + 2 * 2];
        t2[1 + 0 * 2] = quad_xy[1 + 2 * 2];
        t2[0 + 1 * 2] = quad_xy[0 + 3 * 2];
        t2[1 + 1 * 2] = quad_xy[1 + 3 * 2];
        t2[0 + 2 * 2] = quad_xy[0 + 0 * 2];
        t2[1 + 2 * 2] = quad_xy[1 + 0 * 2];

        double area2 = Integrals.triangle_area(t2);

        if (area1 < 0.0 || area2 < 0.0)
        {
            t1[0 + 0 * 2] = quad_xy[0 + 1 * 2];
            t1[1 + 0 * 2] = quad_xy[1 + 1 * 2];
            t1[0 + 1 * 2] = quad_xy[0 + 2 * 2];
            t1[1 + 1 * 2] = quad_xy[1 + 2 * 2];
            t1[0 + 2 * 2] = quad_xy[0 + 3 * 2];
            t1[1 + 2 * 2] = quad_xy[1 + 3 * 2];

            area1 = Integrals.triangle_area(t1);

            t2[0 + 0 * 2] = quad_xy[0 + 3 * 2];
            t2[1 + 0 * 2] = quad_xy[1 + 3 * 2];
            t2[0 + 1 * 2] = quad_xy[0 + 0 * 2];
            t2[1 + 1 * 2] = quad_xy[1 + 0 * 2];
            t2[0 + 2 * 2] = quad_xy[0 + 1 * 2];
            t2[1 + 2 * 2] = quad_xy[1 + 1 * 2];

            area2 = Integrals.triangle_area(t2);

            if (area1 < 0.0 || area2 < 0.0)
            {
                Console.WriteLine("");
                Console.WriteLine("SAMPLE_QUAD - Fatal error!");
                Console.WriteLine("  The quadrilateral nodes seem to be listed in");
                Console.WriteLine("  the wrong order, or the quadrilateral is");
                Console.WriteLine("  degenerate.");
                return(null);
            }
        }

        double area_total = area1 + area2;

        //
        //  Choose a triangle at random, weighted by the areas.
        //  Then choose a point in that triangle.
        //
        double[] xy = new double[2 * n];

        for (i = 0; i < n; i++)
        {
            double r = UniformRNG.r8_uniform_01(ref seed);

            if (r * area_total < area1)
            {
                typeMethods.triangle_sample(t1, 1, ref seed, ref xy, +i * 2);
            }
            else
            {
                typeMethods.triangle_sample(t2, 1, ref seed, ref xy, +i * 2);
            }
        }

        return(xy);
    }
コード例 #23
0
ファイル: IntegralsTests.cs プロジェクト: temeer/mathlib
 public void RectangularCenter()
 {
     RunTestCases(testCase => Integrals.Rectangular(testCase.Func, testCase.A, testCase.B, testCase.NodesCount,
                                                    Integrals.RectType.Center));
 }
コード例 #24
0
ファイル: IntegralsTests.cs プロジェクト: temeer/mathlib
 public void RectangularRight()
 {
     RunTestCases(testCase => Integrals.Rectangular(testCase.Func, testCase.A, testCase.B, testCase.NodesCount,
                                                    Integrals.RectType.Right));
 }
コード例 #25
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) + "");
        }
    }
コード例 #26
0
        public void TestLinear()
        {
            double value = Integrals.SimpsonAt((x) => 3.0 - x / 2, 1, 4);

            Assert.AreEqual(5.25, value, 0.0001);
        }
コード例 #27
0
    public static double[] triangle_monte_carlo(double[] t, int p_num, int f_num,
                                                Func <int, int, tusData> triangle_unit_sample,
                                                Func <int, double[], int, double[]> triangle_integrand, ref int seed)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TRIANGLE_MONTE_CARLO applies the Monte Carlo rule to integrate a function.
    //
    //  Discussion:
    //
    //    The function f(x,y) is to be integrated over a triangle T.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    15 August 2009
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, double T[2*3], the triangle vertices.
    //
    //    Input, int P_NUM, the number of sample points.
    //
    //    Input, int F_NUM, the number of functions to integrate.
    //
    //    Input, external TRIANGLE_UNIT_SAMPLE, the sampling routine.
    //
    //    Input, external TRIANGLE_INTEGRAND, the integrand routine.
    //
    //    Input/output, int &SEED, a seed for the random
    //    number generator.
    //
    //    Output, dobule TRIANGLE_MONTE_CARLO[F_NUM], the approximate integrals.
    //
    {
        int i;

        double area = Integrals.triangle_area(t);

        tusData data = triangle_unit_sample(p_num, seed);

        seed = data.seed;
        double[] p = data.result;

        double[] p2 = new double[2 * p_num];

        Reference.reference_to_physical_t3(t, p_num, p, ref p2);

        double[] fp = triangle_integrand(p_num, p2, f_num);

        double[] result = new double[f_num];

        for (i = 0; i < f_num; i++)
        {
            double fp_sum = 0.0;
            int    j;
            for (j = 0; j < p_num; j++)
            {
                fp_sum += fp[i + j * f_num];
            }

            result[i] = area * fp_sum / p_num;
        }

        return(result);
    }
コード例 #28
0
        public void TestConstant()
        {
            double value = Integrals.SimpsonAt((x) => 1.0, 0, 5);

            Assert.AreEqual(5.0, value, 0.0001);
        }
コード例 #29
0
ファイル: IntegralsTests.cs プロジェクト: temeer/mathlib
 public void TrapezoidTest()
 {
     RunTestCases(
         testCase => Integrals.Trapezoid(testCase.Func, GenerateNodes(testCase.A, testCase.B, testCase.NodesCount))
         );
 }
コード例 #30
0
    private static void test05()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST05 demonstrates REFERENCE_TO_PHYSICAL_T3.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    30 January 2007
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        const int NODE_NUM = 3;

        int node;

        double[] node_xy =
        {
            0.0, 0.0,
            1.0, 0.0,
            0.0, 1.0
        };
        double[] node_xy2 =
        {
            1.0, 2.0,
            1.0, 1.0,
            3.0, 2.0
        };
        int order;

        Console.WriteLine("");
        Console.WriteLine("TEST05");
        Console.WriteLine("  REFERENCE_TO_PHYSICAL_T3 transforms a rule");
        Console.WriteLine("  on the unit (reference) triangle to a rule on ");
        Console.WriteLine("  an arbitrary (physical) triangle.");

        const int rule = 3;

        int order_num = NewtonCotesOpen.triangle_nco_order_num(rule);

        double[] xy  = new double[2 * order_num];
        double[] xy2 = new double[2 * order_num];
        double[] w   = new double[order_num];

        NewtonCotesOpen.triangle_nco_rule(rule, order_num, ref xy, ref w);
        //
        //  Here is the reference triangle, and its rule.
        //
        Console.WriteLine("");
        Console.WriteLine("  The reference triangle:");
        Console.WriteLine("");

        for (node = 0; node < NODE_NUM; node++)
        {
            Console.WriteLine("  " + (node + 1).ToString(CultureInfo.InvariantCulture).PadLeft(8)
                              + "  " + node_xy[0 + node * 2].ToString(CultureInfo.InvariantCulture).PadLeft(14)
                              + "  " + node_xy[1 + node * 2].ToString(CultureInfo.InvariantCulture).PadLeft(14) + "");
        }

        double area = Integrals.triangle_area(node_xy);

        Console.WriteLine("");
        Console.WriteLine("  Rule " + rule + " for reference triangle");
        Console.WriteLine("  with area = " + area + "");
        Console.WriteLine("");
        Console.WriteLine("                X               Y               W");
        Console.WriteLine("");

        for (order = 0; order < order_num; order++)
        {
            Console.WriteLine("  " + order.ToString(CultureInfo.InvariantCulture).PadLeft(8)
                              + "  " + xy[0 + order * 2].ToString(CultureInfo.InvariantCulture).PadLeft(14)
                              + "  " + xy[1 + order * 2].ToString(CultureInfo.InvariantCulture).PadLeft(14)
                              + "  " + w[order].ToString(CultureInfo.InvariantCulture).PadLeft(14) + "");
        }

        //
        //  Transform the rule.
        //
        Reference.reference_to_physical_t3(node_xy2, order_num, xy, ref xy2);
        //
        //  Here is the physical triangle, and its transformed rule.
        //
        Console.WriteLine("");
        Console.WriteLine("  The physical triangle:");
        Console.WriteLine("");

        for (node = 0; node < NODE_NUM; node++)
        {
            Console.WriteLine("  " + (node + 1).ToString(CultureInfo.InvariantCulture).PadLeft(8)
                              + "  " + node_xy2[0 + node * 2].ToString(CultureInfo.InvariantCulture).PadLeft(14)
                              + "  " + node_xy2[1 + node * 2].ToString(CultureInfo.InvariantCulture).PadLeft(14) + "");
        }

        double area2 = Integrals.triangle_area(node_xy2);

        Console.WriteLine("");
        Console.WriteLine("  Rule " + rule + " for physical triangle");
        Console.WriteLine("  with area = " + area2 + "");
        Console.WriteLine("");
        Console.WriteLine("                X               Y               W");
        Console.WriteLine("");

        for (order = 0; order < order_num; order++)
        {
            Console.WriteLine("  " + order.ToString(CultureInfo.InvariantCulture).PadLeft(8)
                              + "  " + xy2[0 + order * 2].ToString(CultureInfo.InvariantCulture).PadLeft(14)
                              + "  " + xy2[1 + order * 2].ToString(CultureInfo.InvariantCulture).PadLeft(14)
                              + "  " + w[order].ToString(CultureInfo.InvariantCulture).PadLeft(14) + "");
        }
    }