private static void test01() //****************************************************************************80 // // Purpose: // // TEST01 uses BALL01_SAMPLE with an increasing number of points. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 02 January 2014 // // Author: // // John Burkardt // { int[] e = new int[3]; int[] e_test = { 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 2, 4, 0, 0, 2, 2, 0, 0, 0, 4 }; int i; int j; double[] result = new double[7]; Console.WriteLine(""); Console.WriteLine("TEST01"); Console.WriteLine(" Estimate integrals over the interior of the unit ball"); Console.WriteLine(" using the Monte Carlo method."); int seed = 123456789; Console.WriteLine(""); Console.WriteLine(" N 1 X^2 Y^2 " + " Z^2 X^4 X^2Y^2 Z^4"); Console.WriteLine(""); int n = 1; string cout; while (n <= 65536) { double[] x = MonteCarlo.ball01_sample(n, ref seed); cout = " " + n.ToString().PadLeft(8); for (j = 0; j < 7; j++) { for (i = 0; i < 3; i++) { e[i] = e_test[i + j * 3]; } double[] value = Monomial.monomial_value(3, n, e, x); result[j] = MonteCarlo.ball01_volume() * typeMethods.r8vec_sum(n, value) / n; cout += " " + result[j].ToString(CultureInfo.InvariantCulture).PadLeft(14); } Console.WriteLine(cout); n = 2 * n; } Console.WriteLine(""); cout = " Exact"; for (j = 0; j < 7; j++) { for (i = 0; i < 3; i++) { e[i] = e_test[i + j * 3]; } result[j] = MonteCarlo.ball01_monomial_integral(e); cout += " " + result[j].ToString(CultureInfo.InvariantCulture).PadLeft(14); } Console.WriteLine(cout); }
private static void Main(string[] args) //****************************************************************************80 // // Purpose: // // MAIN is the main program for PYRAMID_EXACTNESS. // // Discussion: // // This program investigates the polynomial exactness of a quadrature // rule for the pyramid. // // The integration region is: // // - ( 1 - Z ) <= X <= 1 - Z // - ( 1 - Z ) <= Y <= 1 - Z // 0 <= Z <= 1. // // When Z is zero, the integration region is a square lying in the (X,Y) // plane, centered at (0,0,0) with "radius" 1. As Z increases to 1, the // radius of the square diminishes, and when Z reaches 1, the square has // contracted to the single point (0,0,1). // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 28 July 2009 // // Author: // // John Burkardt // { int degree; int degree_max; bool error = false; int last = 0; string quad_filename; Console.WriteLine(""); Console.WriteLine("PYRAMID_EXACTNESS"); Console.WriteLine(" Investigate the polynomial exactness of a quadrature"); Console.WriteLine(" rule for the pyramid."); // // Get the quadrature file root name: // try { quad_filename = args[0]; } catch { Console.WriteLine(""); Console.WriteLine("PYRAMID_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; // 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("PYRAMID_EXACTNESS:"); Console.WriteLine(" Please enter the maximum total degree to check."); degree_max = Convert.ToInt32(Console.ReadLine()); } // // Summarize the input. // Console.WriteLine(""); Console.WriteLine("PYRAMID_EXACTNESS: User input:"); Console.WriteLine(" Quadrature rule X file = \"" + quad_x_filename + "\"."); Console.WriteLine(" Quadrature rule W file = \"" + quad_w_filename + "\"."); Console.WriteLine(" Maximum total degree to check = " + degree_max + ""); // // Read the X file. // TableHeader th = typeMethods.r8mat_header_read(quad_x_filename); int dim_num = th.m; int order = th.n; Console.WriteLine(""); Console.WriteLine(" Spatial dimension = " + dim_num + ""); Console.WriteLine(" Number of points = " + order + ""); if (dim_num != 3) { Console.WriteLine(""); Console.WriteLine("PYRAMID_EXACTNESS - Fatal error!"); Console.WriteLine(" The quadrature abscissas must be 3 dimensional."); return; } double[] x = typeMethods.r8mat_data_read(quad_x_filename, dim_num, order); // // Read the W file. // th = typeMethods.r8mat_header_read(quad_w_filename); int dim_num2 = th.m; int order2 = th.n; if (dim_num2 != 1) { Console.WriteLine(""); Console.WriteLine("PYRAMID_EXACTNESS - Fatal error!"); Console.WriteLine(" The quadrature weight file should have exactly"); Console.WriteLine(" one value on each line."); return; } if (order2 != order) { Console.WriteLine(""); Console.WriteLine("PYRAMID_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, order); // // Explore the monomials. // int[] expon = new int[dim_num]; Console.WriteLine(""); Console.WriteLine(" Error Degree Exponents"); Console.WriteLine(""); for (degree = 0; degree <= degree_max; degree++) { 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[] v = Monomial.monomial_value(dim_num, order, expon, x); double quad = Pyramid.pyra_unit_volume() * typeMethods.r8vec_dot_product(order, w, v); double exact = Pyramid.pyra_unit_monomial(expon); double quad_error = Math.Abs(quad - exact); 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(""); Console.WriteLine("PYRAMID_EXACTNESS:"); Console.WriteLine(" Normal end of execution."); Console.WriteLine(""); } }
private static void test01() //****************************************************************************80 // // Purpose: // // TEST01 compares exact and estimated integrals in 3D. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 16 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(" Estimate monomial integrals using Monte Carlo"); Console.WriteLine(" over the interior of the unit simplex in M dimensions."); // // Get sample points. // int seed = 123456789; double[] x = Integrals.simplex01_sample(m, n, ref seed); Console.WriteLine(""); Console.WriteLine(" Number of sample points used is " + n + ""); // // Randomly choose exponents. // Console.WriteLine(""); Console.WriteLine(" We randomly choose the 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); double[] value = Monomial.monomial_value(m, n, e, x); double result = Integrals.simplex01_volume(m) * typeMethods.r8vec_sum(n, value) / n; double exact = Integrals.simplex01_monomial_integral(m, 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) + ""); } }
public static void line_fekete_monomial(int m, double a, double b, int n, double[] x, ref int nf, ref double[] xf, ref double[] wf) //****************************************************************************80 // // Purpose: // // LINE_FEKETE_MONOMIAL: approximate Fekete points in an interval [A,B]. // // Discussion: // // We use the uniform weight and the monomial basis: // // P(j) = x^(j-1) // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 13 April 2014 // // Author: // // John Burkardt // // Reference: // // Alvise Sommariva, Marco Vianello, // Computing approximate Fekete points by QR factorizations of Vandermonde // matrices, // Computers and Mathematics with Applications, // Volume 57, 2009, pages 1324-1336. // // Parameters: // // Input, int M, the number of basis polynomials. // // Input, double A, B, the endpoints of the interval. // // Input, int N, the number of sample points. // M <= N. // // Input, double X(N), the coordinates of the sample points. // // Output, int &NF, the number of Fekete points. // If the computation is successful, NF = M. // // Output, double XF(NF), the coordinates of the Fekete points. // // Output, double WF(NF), the weights of the Fekete points. // { int j; if (n < m) { Console.WriteLine(""); Console.WriteLine("LINE_FEKETE_MONOMIAL - Fatal error!"); Console.WriteLine(" N < M."); return; } // // Form the moments. // double[] mom = Monomial.line_monomial_moments(a, b, m); // // Form the rectangular Vandermonde matrix V for the polynomial basis. // double[] v = new double[m * n]; for (j = 0; j < n; j++) { v[0 + j * m] = 1.0; int i; for (i = 1; i < m; i++) { v[i + j * m] = v[i - 1 + j * m] * x[j]; } } // // Solve the system for the weights W. // double[] w = QRSolve.qr_solve(m, n, v, mom); // // Extract the data associated with the nonzero weights. // nf = 0; for (j = 0; j < n; j++) { if (w[j] == 0.0) { continue; } if (nf >= m) { continue; } xf[nf] = x[j]; wf[nf] = w[j]; nf += 1; } }
private static void test01(int nv, double[] v) //****************************************************************************80 // // Purpose: // // TEST01 estimates integrals over a polygon in 2D. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 09 May 2014 // // Author: // // John Burkardt // { int[] e = new int[2]; int[] e_test = { 0, 0, 2, 0, 0, 2, 4, 0, 2, 2, 0, 4, 6, 0 }; int j; double result; string cout; Console.WriteLine(""); Console.WriteLine("TEST01"); Console.WriteLine(" Use POLYGON_SAMPLE to estimate integrals"); Console.WriteLine(" over the interior of a polygon in 2D."); int seed = 123456789; Console.WriteLine(""); Console.WriteLine(" N" + " 1" + " X^2 " + " Y^2" + " X^4" + " X^2Y^2" + " Y^4" + " X^6"); int n = 1; while (n <= 65536) { double[] x = MonteCarlo.polygon_sample(nv, v, n, ref seed); cout = " " + n.ToString(CultureInfo.InvariantCulture).PadLeft(8); for (j = 0; j < 7; j++) { e[0] = e_test[0 + j * 2]; e[1] = e_test[1 + j * 2]; double[] value = Monomial.monomial_value(2, n, e, x); result = MonteCarlo.polygon_area(nv, v) * typeMethods.r8vec_sum(n, value) / n; cout += " " + result.ToString(CultureInfo.InvariantCulture).PadLeft(14); } Console.WriteLine(cout); n = 2 * n; } Console.WriteLine(""); cout = " Exact"; for (j = 0; j < 7; j++) { e[0] = e_test[0 + j * 2]; e[1] = e_test[1 + j * 2]; result = MonteCarlo.polygon_monomial_integral(nv, v, e); cout += " " + result.ToString(CultureInfo.InvariantCulture).PadLeft(14); } Console.WriteLine(cout); }
private static void test01() //****************************************************************************80 // // Purpose: // // TEST01 compares exact and estimated monomial integrals. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 14 April 2014 // // Author: // // John Burkardt // { const int e_max = 6; int e3; int[] expon = new int[3]; const int m = 3; const int n = 500000; Console.WriteLine(""); Console.WriteLine("TEST01"); Console.WriteLine(" Compare exact and estimated integrals "); Console.WriteLine(" over the unit pyramid in 3D."); // // Get sample points. // int seed = 123456789; double[] x = Integrals.pyramid01_sample(n, ref seed); Console.WriteLine(""); Console.WriteLine(" Number of sample points used is " + n + ""); Console.WriteLine(""); Console.WriteLine(" E1 E2 E3 MC-Estimate Exact Error"); Console.WriteLine(""); // // Check all monomials, with only even dependence on X or Y, // up to total degree E_MAX. // for (e3 = 0; e3 <= e_max; e3++) { expon[2] = e3; int e2; for (e2 = 0; e2 <= e_max - e3; e2 += 2) { expon[1] = e2; int e1; for (e1 = 0; e1 <= e_max - e3 - e2; e1 += 2) { expon[0] = e1; double[] value = Monomial.monomial_value(m, n, expon, x); double q = Integrals.pyramid01_volume() * typeMethods.r8vec_sum(n, value) / n; double exact = Integrals.pyramid01_monomial_integral(expon); double error = Math.Abs(q - exact); Console.WriteLine(" " + expon[0].ToString().PadLeft(2) + " " + expon[1].ToString().PadLeft(2) + " " + expon[2].ToString().PadLeft(2) + " " + q.ToString(CultureInfo.InvariantCulture).PadLeft(14) + " " + exact.ToString(CultureInfo.InvariantCulture).PadLeft(14) + " " + error.ToString(CultureInfo.InvariantCulture).PadLeft(12) + ""); } } } }
private static void test05() //****************************************************************************80 // // Purpose: // // TEST05 tests KEAST_RULE. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 03 July 2008 // // Author: // // John Burkardt // { int degree; const int degree_max = 3; const int dim_num = 3; int[] expon = new int[3]; Console.WriteLine(""); Console.WriteLine("TEST05"); Console.WriteLine(" Demonstrate the KEAST rules on a sequence of"); Console.WriteLine(" monomial integrands X^A Y^B Z^C"); Console.WriteLine(" on the unit tetrahedron."); int rule_num = KeastRule.keast_rule_num(); Console.WriteLine(""); Console.WriteLine(" Rule Order Quad"); Console.WriteLine(""); for (degree = 0; degree <= degree_max; degree++) { bool more = false; int h = 0; int t = 0; for (;;) { Comp.comp_next(degree, dim_num, ref expon, ref more, ref h, ref t); Console.WriteLine(""); Console.WriteLine(" F(X,Y,Z)" + " = X^" + expon[0] + " * Y^" + expon[1] + " * Z^" + expon[2] + ""); Console.WriteLine(""); int rule; for (rule = 1; rule <= rule_num; rule++) { int order_num = KeastRule.keast_order_num(rule); double[] xyz = new double[3 * order_num]; double[] w = new double[order_num]; KeastRule.keast_rule(rule, order_num, ref xyz, ref w); double[] mono = Monomial.monomial_value(dim_num, order_num, xyz, expon); double quad = typeMethods.r8vec_dot(order_num, w, mono); Console.WriteLine(" " + rule.ToString().PadLeft(8) + " " + order_num.ToString().PadLeft(8) + " " + quad.ToString(CultureInfo.InvariantCulture).PadLeft(14) + ""); } if (!more) { break; } } } }
private static void Main(string[] args) //****************************************************************************80 // // Purpose: // // MAIN is the main program for WEDGE_EXACTNESS. // // Discussion: // // This program investigates the polynomial exactness of a quadrature // rule for the unit wedge. // // The interior of the unit wedge in 3D is defined by the constraints: // 0 <= X // 0 <= Y // X + Y <= 1 // -1 <= Z <= +1 // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 24 August 2014 // // Author: // // John Burkardt // { int degree; int degree_max; string quad_filename; Console.WriteLine(""); Console.WriteLine("WEDGE_EXACTNESS"); Console.WriteLine(" Investigate the polynomial exactness of a quadrature"); Console.WriteLine(" rule for the unit wedge."); // // Get the quadrature file root name: // try { quad_filename = args[0]; } catch { Console.WriteLine(""); Console.WriteLine("WEDGE_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; // 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 = Convert.ToInt32(args[1]); } catch { Console.WriteLine(""); Console.WriteLine("WEDGE_EXACTNESS:"); Console.WriteLine(" Please enter the maximum total degree to check."); degree_max = Convert.ToInt32(Console.ReadLine()); } // // Summarize the input. // Console.WriteLine(""); Console.WriteLine("WEDGE_EXACTNESS: User input:"); Console.WriteLine(" Quadrature rule X file = \"" + quad_x_filename + "\"."); Console.WriteLine(" Quadrature rule W file = \"" + quad_w_filename + "\"."); Console.WriteLine(" Maximum total degree to check = " + degree_max + ""); // // Read the X file. // TableHeader hd = typeMethods.r8mat_header_read(quad_x_filename); int dim_num = hd.m; int order = hd.n; Console.WriteLine(""); Console.WriteLine(" Spatial dimension = " + dim_num + ""); Console.WriteLine(" Number of points = " + order + ""); if (dim_num != 3) { Console.WriteLine(""); Console.WriteLine("WEDGE_EXACTNESS - Fatal error!"); Console.WriteLine(" The quadrature abscissas must be 3 dimensional."); return; } double[] x = typeMethods.r8mat_data_read(quad_x_filename, dim_num, order); // // Read the W file. // hd = typeMethods.r8mat_header_read(quad_w_filename); int dim_num2 = hd.m; int order2 = hd.n; if (dim_num2 != 1) { Console.WriteLine(""); Console.WriteLine("WEDGE_EXACTNESS - Fatal error!"); Console.WriteLine(" The quadrature weight file should have exactly"); Console.WriteLine(" one value on each line."); return; } if (order2 != order) { Console.WriteLine(""); Console.WriteLine("WEDGE_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, order); // // Explore the monomials. // int[] expon = new int[dim_num]; Console.WriteLine(""); Console.WriteLine(" Error Degree Exponents"); Console.WriteLine(""); for (degree = 0; degree <= degree_max; degree++) { 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[] v = Monomial.monomial_value(dim_num, order, expon, x); double quad = Integrals.wedge01_volume() * typeMethods.r8vec_dot_product(order, w, v); double exact = Integrals.wedge01_integral(expon); double quad_error = Math.Abs(quad - exact); 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(""); Console.WriteLine("WEDGE_EXACTNESS:"); Console.WriteLine(" Normal end of execution."); Console.WriteLine(""); }
private static void circle01_sample_random_test() //****************************************************************************80 // // Purpose: // // CIRCLE01_SAMPLE_RANDOM_TEST uses CIRCLE01_SAMPLE_RANDOM with an increasing number of points. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 02 January 2014 // // Author: // // John Burkardt // { int[] e = new int[2]; int[] e_test = { 0, 0, 2, 0, 0, 2, 4, 0, 2, 2, 0, 4, 6, 0 } ; int i; int j; Console.WriteLine(""); Console.WriteLine("CIRCLE0_SAMPLE_RANDOM_TEST"); Console.WriteLine(" CIRCLE01_SAMPLE_RANDOM randomly samples the unit circle."); Console.WriteLine(" Use it to estimate integrals."); int seed = 123456789; Console.WriteLine(""); Console.WriteLine(" N 1 X^2 Y^2" + " X^4 X^2Y^2 Y^4 X^6"); Console.WriteLine(""); int n = 1; while (n <= 65536) { double[] x = MonteCarlo.circle01_sample_random(n, ref seed); string cout = " " + n.ToString(CultureInfo.InvariantCulture).PadLeft(8); for (j = 0; j < 7; j++) { for (i = 0; i < 2; i++) { e[i] = e_test[i + j * 2]; } double[] value = Monomial.monomial_value(2, n, e, x); double result = Integrals.circle01_length() * typeMethods.r8vec_sum(n, value) / n; cout += " " + result.ToString(CultureInfo.InvariantCulture).PadLeft(14); } Console.WriteLine(cout); n = 2 * n; } Console.WriteLine(""); string cout2 = " Exact"; for (j = 0; j < 7; j++) { for (i = 0; i < 2; i++) { e[i] = e_test[i + j * 2]; } double exact = Integrals.circle01_monomial_integral(e); cout2 += " " + exact.ToString(CultureInfo.InvariantCulture).PadLeft(14); } Console.WriteLine(cout2); }
private static void test01() //****************************************************************************80 // // Purpose: // // TEST01 uses TETRAHEDRON_SAMPLE_01 to compare exact and estimated integrals. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 15 January 2014 // // Author: // // John Burkardt // { int[] e = new int[3]; int i; const int m = 3; const int n = 4192; Console.WriteLine(""); Console.WriteLine("TEST01"); Console.WriteLine(" Estimate monomial integrals using Monte Carlo"); Console.WriteLine(" over the interior of the unit tetrahedron in 3D."); // // Get sample points. // int seed = 123456789; double[] x = Integrals.tetrahedron01_sample(n, ref seed); Console.WriteLine(""); Console.WriteLine(" Number of sample points used is " + n + ""); // // Run through the exponents. // Console.WriteLine(""); Console.WriteLine(" Ex Ey Ez MC-Estimate Exact Error"); Console.WriteLine(""); for (i = 0; i <= 3; i++) { e[0] = i; int j; for (j = 0; j <= 3; j++) { e[1] = j; int k; for (k = 0; k <= 3; k++) { e[2] = k; double[] value = Monomial.monomial_value(m, n, e, x); double result = Integrals.tetrahedron01_volume() * typeMethods.r8vec_sum(n, value) / n; double exact = Integrals.tetrahedron01_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(10) + ""); } } } }