public static void pyramid_handle(int legendre_order, int jacobi_order, string filename) //****************************************************************************80 // // Purpose: // // PYRAMID_HANDLE computes the requested pyramid rule and outputs it. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 24 July 2009 // // Author: // // John Burkardt // // Parameters: // // Input, int LEGENDRE_ORDER, JACOBI_ORDER, the orders // of the component Legendre and Jacobi rules. // // Input, string FILENAME, the rootname for the files, // write files 'file_w.txt' and 'file_x.txt', and 'file_r.txt', weights, // abscissas, and region. // { const int DIM_NUM = 3; int k; double[] pyramid_r = { -1.0, -1.0, 0.0, +1.0, -1.0, 0.0, -1.0, +1.0, 0.0, +1.0, +1.0, 0.0, 0.0, 0.0, 1.0 }; // // Compute the factor rules. // double[] legendre_w = new double[legendre_order]; double[] legendre_x = new double[legendre_order]; Legendre.QuadratureRule.legendre_compute(legendre_order, ref legendre_x, ref legendre_w); double[] jacobi_w = new double[jacobi_order]; double[] jacobi_x = new double[jacobi_order]; const double jacobi_alpha = 2.0; const double jacobi_beta = 0.0; JacobiQuadrature.jacobi_compute(jacobi_order, jacobi_alpha, jacobi_beta, ref jacobi_x, ref jacobi_w); // // Compute the pyramid rule. // int pyramid_order = legendre_order * legendre_order * jacobi_order; double[] pyramid_w = new double[pyramid_order]; double[] pyramid_x = new double[DIM_NUM * pyramid_order]; const double volume = 4.0 / 3.0; int l = 0; for (k = 0; k < jacobi_order; k++) { double xk = (jacobi_x[k] + 1.0) / 2.0; double wk = jacobi_w[k] / 2.0; int j; for (j = 0; j < legendre_order; j++) { double xj = legendre_x[j]; double wj = legendre_w[j]; int i; for (i = 0; i < legendre_order; i++) { double xi = legendre_x[i]; double wi = legendre_w[i]; pyramid_w[l] = wi * wj * wk / 4.0 / volume; pyramid_x[0 + l * 3] = xi * (1.0 - xk); pyramid_x[1 + l * 3] = xj * (1.0 - xk); pyramid_x[2 + l * 3] = xk; l += 1; } } } // // Write the rule to files. // string filename_w = filename + "_w.txt"; string filename_x = filename + "_x.txt"; string filename_r = filename + "_r.txt"; Console.WriteLine(""); Console.WriteLine(" Creating quadrature files."); Console.WriteLine(""); Console.WriteLine(" \"Root\" file name is \"" + filename + "\"."); Console.WriteLine(""); Console.WriteLine(" Weight file will be \"" + filename_w + "\"."); Console.WriteLine(" Abscissa file will be \"" + filename_x + "\"."); Console.WriteLine(" Region file will be \"" + filename_r + "\"."); typeMethods.r8mat_write(filename_w, 1, pyramid_order, pyramid_w); typeMethods.r8mat_write(filename_x, DIM_NUM, pyramid_order, pyramid_x); typeMethods.r8mat_write(filename_r, DIM_NUM, 5, pyramid_r); }
private static void test185() //****************************************************************************80 // // Purpose: // // TEST185 tests JACOBI_POINTS and JACOBI_WEIGHTS. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 02 April 2010 // // Author: // // John Burkardt // { const int TEST_NUM = 3; double[] alpha_test = { 0.5, 1.0, 2.5 }; double[] beta_test = { 0.5, 1.0, 2.5 }; const int order_max = 10; int test1; JacobiQuadrature.ParameterData data = new(); Console.WriteLine(""); Console.WriteLine("TEST185"); Console.WriteLine(" JACOBI_POINTS and JACOBI_WEIGHTS compute a Gauss-Jacobi rule"); Console.WriteLine(" which is appropriate for integrands of the form"); Console.WriteLine(""); Console.WriteLine(" Integral ( -1 <= x <= +1 ) f(x) (1-x)^alpha (1+x)^beta dx."); Console.WriteLine(""); Console.WriteLine(" For technical reasons, the parameters ALPHA and BETA are to"); Console.WriteLine(" supplied by a function called PARAMETER."); Console.WriteLine(""); Console.WriteLine(" Order ALPHA BETA ||X1-X2|| ||W1-W2||"); Console.WriteLine(""); for (test1 = 0; test1 < TEST_NUM; test1++) { double alpha = alpha_test[test1]; int test2; for (test2 = 0; test2 < TEST_NUM; test2++) { double beta = beta_test[test2]; const int dim = 0; data.NP = new int[1]; data.NP[0] = 2; data.P = new double[2]; data.P[0] = alpha; data.P[1] = beta; int order; for (order = 1; order <= order_max; order++) { double[] w1 = new double[order]; double[] w2 = new double[order]; double[] x1 = new double[order]; double[] x2 = new double[order]; JacobiQuadrature.jacobi_compute(order, alpha, beta, ref x1, ref w1); data = JacobiQuadrature.jacobi_points(parameter, data, order, dim, ref x2); data = JacobiQuadrature.jacobi_weights(parameter, data, order, dim, ref w2); double x_diff = typeMethods.r8vec_diff_norm_li(order, x1, x2); double w_diff = typeMethods.r8vec_diff_norm_li(order, w1, w2); Console.WriteLine(" " + order.ToString().PadLeft(6) + " " + alpha.ToString(CultureInfo.InvariantCulture).PadLeft(10) + " " + beta.ToString(CultureInfo.InvariantCulture).PadLeft(10) + " " + x_diff.ToString(CultureInfo.InvariantCulture).PadLeft(10) + " " + w_diff.ToString(CultureInfo.InvariantCulture).PadLeft(10) + ""); } } } }
private static void Main(string[] args) //****************************************************************************80 // // Purpose: // // MAIN is the main program for JACOBI_EXACTNESS. // // Discussion: // // This program investigates a standard Gauss-Jacobi quadrature rule // by using it to integrate monomials over [-1,+1], and comparing the // approximate result to the known exact value. // // The user specifies: // * the "root" name of the R, W and X files that specify the rule; // * DEGREE_MAX, the maximum monomial degree to be checked. // * ALPHA, the exponent of the (1-X) factor; // * BETA, the exponent of the (1+X) factor. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 05 August 2009 // // Author: // // John Burkardt // { double alpha; double beta; int degree; int degree_max; int i; string quad_filename; Console.WriteLine(""); Console.WriteLine("JACOBI_EXACTNESS"); Console.WriteLine(""); Console.WriteLine(" Investigate the polynomial exactness of a Gauss-Jacobi"); Console.WriteLine(" quadrature rule by integrating weighted"); Console.WriteLine(" monomials up to a given degree over the [-1,+1] interval."); // // Get the quadrature file rootname. // try { quad_filename = args[0]; } catch (Exception) { Console.WriteLine(""); Console.WriteLine(" Enter the quadrature file rootname:"); quad_filename = Console.ReadLine(); } Console.WriteLine(""); Console.WriteLine(" The quadrature file rootname is \"" + quad_filename + "\"."); // // Create the names of: // the quadrature X file; // the quadrature W file; // the quadrature R file; // string quad_w_filename = quad_filename + "_w.txt"; string quad_x_filename = quad_filename + "_x.txt"; string quad_r_filename = quad_filename + "_r.txt"; // // Get the maximum degree: // try { degree_max = Convert.ToInt32(args[1]); } catch (Exception) { Console.WriteLine(""); Console.WriteLine(" Enter DEGREE_MAX, the maximum monomial degree to check."); degree_max = Convert.ToInt32(Console.ReadLine()); } Console.WriteLine(""); Console.WriteLine(" The requested maximum monomial degree is = " + degree_max + ""); // // The third command line option is ALPHA. // try { alpha = Convert.ToDouble(args[2]); } catch (Exception) { Console.WriteLine(""); Console.WriteLine("ALPHA is the power of (1-X) in the weighting function."); Console.WriteLine(""); Console.WriteLine("ALPHA is a real number greater than -1.0."); alpha = Convert.ToDouble(Console.ReadLine()); } // // The fourth command line option is BETA. // try { beta = Convert.ToDouble(args[3]); } catch (Exception) { Console.WriteLine(""); Console.WriteLine("BETA is the power of (1+X) in the weighting function."); Console.WriteLine(""); Console.WriteLine("BETA is a real number greater than -1.0."); beta = Convert.ToDouble(Console.ReadLine()); } // // Summarize the input. // Console.WriteLine(""); Console.WriteLine("JACOBI_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 degree to check = " + degree_max + ""); Console.WriteLine(" Exponent of (1-x), ALPHA = " + alpha + ""); Console.WriteLine(" Exponent of (1+x), BETA = " + beta + ""); // // Read the X file. // TableHeader h = typeMethods.r8mat_header_read(quad_x_filename); int dim_num = h.m; int order = h.n; if (dim_num != 1) { Console.WriteLine(""); Console.WriteLine("JACOBI_EXACTNESS - Fatal error!"); Console.WriteLine(" The spatial dimension of X should be 1."); Console.WriteLine(" The implicit input dimension was DIM_NUM = " + dim_num + ""); return; } Console.WriteLine(""); Console.WriteLine(" Spatial dimension = " + dim_num + ""); Console.WriteLine(" Number of points = " + order + ""); double[] x = typeMethods.r8mat_data_read(quad_x_filename, dim_num, order); // // Read the W file. // h = typeMethods.r8mat_header_read(quad_w_filename); int dim_num2 = h.m; int point_num = h.n; if (dim_num2 != 1) { Console.WriteLine(""); Console.WriteLine("JACOBI_EXACTNESS - Fatal error!"); Console.WriteLine(" The quadrature weight file should have exactly"); Console.WriteLine(" one value on each line."); return; } if (point_num != order) { Console.WriteLine(""); Console.WriteLine("JACOBI_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, dim_num, order); // // Read the R file. // h = typeMethods.r8mat_header_read(quad_r_filename); dim_num2 = h.m; int point_num2 = h.n; if (dim_num2 != dim_num) { Console.WriteLine(""); Console.WriteLine("JACOBI_EXACTNESS - Fatal error!"); Console.WriteLine(" The quadrature region file should have the"); Console.WriteLine(" same number of values on each line as the"); Console.WriteLine(" abscissa file does."); return; } if (point_num2 != 2) { Console.WriteLine(""); Console.WriteLine("JACOBI_EXACTNESS - Fatal error!"); Console.WriteLine(" The quadrature region file should have two lines."); return; } double[] r = typeMethods.r8mat_data_read(quad_r_filename, dim_num, point_num2); // // Print the input quadrature rule. // Console.WriteLine(""); Console.WriteLine(" The quadrature rule to be tested is"); Console.WriteLine(" a Gauss-Jacobi rule"); Console.WriteLine(" ORDER = " + order + ""); Console.WriteLine(" ALPHA = " + alpha + ""); Console.WriteLine(" BETA = " + beta + ""); Console.WriteLine(""); Console.WriteLine(" Standard rule:"); Console.WriteLine(" Integral ( -1 <= x <= +1 ) (1-x)^alpha (1+x)^beta f(x) dx"); Console.WriteLine(" is to be approximated by"); Console.WriteLine(" sum ( 1 <= I <= ORDER ) w(i) * f(x(i))."); Console.WriteLine(""); Console.WriteLine(" Weights W:"); Console.WriteLine(""); for (i = 0; i < order; i++) { Console.WriteLine(" w[" + i.ToString().PadLeft(2) + "] = " + w[i].ToString("0.################").PadLeft(24) + ""); } Console.WriteLine(""); Console.WriteLine(" Abscissas X:"); Console.WriteLine(""); for (i = 0; i < order; i++) { Console.WriteLine(" x[" + i.ToString().PadLeft(2) + "] = " + x[i].ToString("0.################").PadLeft(24) + ""); } Console.WriteLine(""); Console.WriteLine(" Region R:"); Console.WriteLine(""); for (i = 0; i < 2; i++) { Console.WriteLine(" r[" + i.ToString().PadLeft(2) + "] = " + r[i].ToString("0.################").PadLeft(24) + ""); } // // Explore the monomials. // Console.WriteLine(""); Console.WriteLine(" A Gauss-Jacobi rule would be able to exactly"); Console.WriteLine(" integrate monomials up to and including degree = " + (2 * order - 1) + ""); Console.WriteLine(""); Console.WriteLine(" Error Degree"); Console.WriteLine(""); for (degree = 0; degree <= degree_max; degree++) { double quad_error = JacobiQuadrature.monomial_quadrature_jacobi(degree, alpha, beta, order, w, x); Console.WriteLine(" " + quad_error.ToString("0.################").PadLeft(24) + " " + degree.ToString().PadLeft(2) + ""); } Console.WriteLine(""); Console.WriteLine("JACOBI_EXACTNESS:"); Console.WriteLine(" Normal end of execution."); Console.WriteLine(""); }
public static void product_mixed_growth_weight(int dim_num, int[] order_1d, int order_nd, int[] rule, int[] np, double[] p, Func <int, int, double[], double[], double[]>[] gw_compute_weights, ref double[] weight_nd) //****************************************************************************80 // // Purpose: // // PRODUCT_MIXED_GROWTH_WEIGHT computes the weights of a mixed product rule. // // Discussion: // // This routine computes the weights for a quadrature rule which is // a product of 1D rules of varying order and kind. // // The user must preallocate space for the output array WEIGHT_ND. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 20 June 2010 // // Author: // // John Burkardt // // Reference: // // Fabio Nobile, Raul Tempone, Clayton Webster, // A Sparse Grid Stochastic Collocation Method for Partial Differential // Equations with Random Input Data, // SIAM Journal on Numerical Analysis, // Volume 46, Number 5, 2008, pages 2309-2345. // // Parameters: // // Input, int DIM_NUM, the spatial dimension. // // Input, int ORDER_1D[DIM_NUM], the order of the 1D rules. // // Input, int ORDER_ND, the order of the product rule. // // Input, int RULE[DIM_NUM], the rule in each dimension. // 1, "CC", Clenshaw Curtis, Closed Fully Nested. // 2, "F2", Fejer Type 2, Open Fully Nested. // 3, "GP", Gauss Patterson, Open Fully Nested. // 4, "GL", Gauss Legendre, Open Weakly Nested. // 5, "GH", Gauss Hermite, Open Weakly Nested. // 6, "GGH", Generalized Gauss Hermite, Open Weakly Nested. // 7, "LG", Gauss Laguerre, Open Non Nested. // 8, "GLG", Generalized Gauss Laguerre, Open Non Nested. // 9, "GJ", Gauss Jacobi, Open Non Nested. // 10, "HGK", Hermite Genz-Keister, Open Fully Nested. // 11, "UO", User supplied Open, presumably Non Nested. // 12, "UC", User supplied Closed, presumably Non Nested. // // Input, int NP[DIM_NUM], the number of parameters used by each rule. // // Input, double P[sum(NP[*])], the parameters needed by each rule. // // Input, void ( *GW_COMPUTE_WEIGHTS[] ) ( int order, int np, double p[], double w[] ), // an array of pointers to functions which return the 1D quadrature weights // associated with each spatial dimension for which a Golub Welsch rule // is used. // // Output, double WEIGHT_ND[ORDER_ND], the product rule weights. // { int dim; int i; typeMethods.r8vecDPData data = new(); for (i = 0; i < order_nd; i++) { weight_nd[i] = 1.0; } int p_index = 0; for (dim = 0; dim < dim_num; dim++) { double[] weight_1d = new double[order_1d[dim]]; switch (rule[dim]) { case 1: ClenshawCurtis.clenshaw_curtis_compute_weights_np( order_1d[dim], np[dim], p.Skip(+p_index).ToArray(), weight_1d); break; case 2: Fejer2.fejer2_compute_weights_np( order_1d[dim], np[dim], p.Skip(+p_index).ToArray(), weight_1d); break; case 3: PattersonQuadrature.patterson_lookup_weights_np( order_1d[dim], np[dim], p.Skip(+p_index).ToArray(), weight_1d); break; case 4: Legendre.QuadratureRule.legendre_compute_weights_np( order_1d[dim], np[dim], p.Skip(+p_index).ToArray(), weight_1d); break; case 5: HermiteQuadrature.hermite_compute_weights_np( order_1d[dim], np[dim], p.Skip(+p_index).ToArray(), weight_1d); break; case 6: HermiteQuadrature.gen_hermite_compute_weights_np( order_1d[dim], np[dim], p.Skip(+p_index).ToArray(), weight_1d); break; case 7: Laguerre.QuadratureRule.laguerre_compute_weights_np( order_1d[dim], np[dim], p.Skip(+p_index).ToArray(), weight_1d); break; case 8: Laguerre.QuadratureRule.gen_laguerre_compute_weights_np( order_1d[dim], np[dim], p.Skip(+p_index).ToArray(), weight_1d); break; case 9: JacobiQuadrature.jacobi_compute_weights_np( order_1d[dim], np[dim], p.Skip(+p_index).ToArray(), weight_1d); break; case 10: HermiteQuadrature.hermite_genz_keister_lookup_weights_np( order_1d[dim], np[dim], p.Skip(+p_index).ToArray(), weight_1d); break; case 11: case 12: gw_compute_weights[dim]( order_1d[dim], np[dim], p.Skip(+p_index).ToArray(), weight_1d); break; default: Console.WriteLine(""); Console.WriteLine("PRODUCT_MIXED_GROWTH_WEIGHT - Fatal error!"); Console.WriteLine(" Unexpected value of RULE[" + dim + "] = " + rule[dim] + "."); return; } p_index += np[dim]; typeMethods.r8vec_direct_product2(ref data, dim, order_1d[dim], weight_1d, dim_num, order_nd, ref weight_nd); } }
private static void Main(string[] args) //****************************************************************************80 // // Purpose: // // MAIN is the main program for JACOBI_RULE_SS. // // Discussion: // // This program computes a standard Gauss-Jacobi quadrature rule // and writes it to a file. // // The user specifies: // * the ORDER (number of points) in the rule // * ALPHA and BETA parameters, // * the root name of the output files. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 28 June 2009 // // Author: // // John Burkardt // { double alpha; double beta; int order; string output; Console.WriteLine(""); Console.WriteLine(""); Console.WriteLine("JACOBI_RULE_SS"); Console.WriteLine(""); Console.WriteLine(" Compute a Gauss-Jacobi quadrature rule for approximating"); Console.WriteLine(""); Console.WriteLine(" Integral ( -1 <= x <= +1 ) (1-x)^ALPHA (1+x)^BETA f(x) dx"); Console.WriteLine(""); Console.WriteLine(" of order ORDER."); Console.WriteLine(""); Console.WriteLine(" The user specifies ORDER, ALPHA, BETA, and OUTPUT."); Console.WriteLine(""); Console.WriteLine(" OUTPUT is:"); Console.WriteLine(""); Console.WriteLine(" \"C++\" for printed C++ output;"); Console.WriteLine(" \"F77\" for printed Fortran77 output;"); Console.WriteLine(" \"F90\" for printed Fortran90 output;"); Console.WriteLine(" \"MAT\" for printed MATLAB output;"); Console.WriteLine(""); Console.WriteLine(" or:"); Console.WriteLine(""); Console.WriteLine(" \"filename\" to generate 3 files:"); Console.WriteLine(""); Console.WriteLine(" filename_w.txt - the weight file"); Console.WriteLine(" filename_x.txt - the abscissa file."); Console.WriteLine(" filename_r.txt - the region file."); // // Get the order. // try { order = Convert.ToInt32(args[0]); } catch { Console.WriteLine(""); Console.WriteLine(" Enter the value of ORDER (1 or greater)"); order = Convert.ToInt32(Console.ReadLine()); } Console.WriteLine(""); Console.WriteLine(" The requested order of the rule is = " + order + ""); // // Get ALPHA. // try { alpha = Convert.ToDouble(args[1]); } catch { Console.WriteLine(""); Console.WriteLine(" ALPHA is the exponent of (1-x) in the integral:"); Console.WriteLine(""); Console.WriteLine(" Integral ( -1 <= x <= +1 ) (1-x)^ALPHA (1+x)^BETA f(x) dx"); Console.WriteLine(""); Console.WriteLine(" Note that -1.0 < ALPHA is required."); Console.WriteLine(""); Console.WriteLine(" Enter the value of ALPHA:"); alpha = Convert.ToDouble(Console.ReadLine()); } Console.WriteLine(""); Console.WriteLine(" The requested value of ALPHA = " + alpha + ""); // // Get BETA. // try { beta = Convert.ToDouble(args[2]); } catch { Console.WriteLine(""); Console.WriteLine(" BETA is the exponent of (1+x) in the integral:"); Console.WriteLine(""); Console.WriteLine(" Integral ( -1 <= x <= +1 ) (1-x)^ALPHA (1+x)^BETA f(x) dx"); Console.WriteLine(""); Console.WriteLine(" Note that -1.0 < BETA is required."); Console.WriteLine(""); Console.WriteLine(" Enter the value of BETA:"); beta = Convert.ToDouble(Console.ReadLine()); } Console.WriteLine(""); Console.WriteLine(" The requested value of BETA = " + beta + ""); // // Get the output option or quadrature file root name: // try { output = args[3]; } catch { Console.WriteLine(""); Console.WriteLine(" Enter OUTPUT (one of C++, F77, F90, MAT"); Console.WriteLine(" or else the \"root name\" of the quadrature files)."); output = Console.ReadLine(); } Console.WriteLine(""); Console.WriteLine(" OUTPUT option is \"" + output + "\"."); // // Construct the rule and output it. // JacobiQuadrature.jacobi_handle(order, alpha, beta, output); Console.WriteLine(""); Console.WriteLine("JACOBI_RULE_SS:"); Console.WriteLine(" Normal end of execution."); Console.WriteLine(""); }