public static int[] subset_random(int n, ref int seed) //****************************************************************************80 // // Purpose: // // SUBSET_RANDOM returns a random subset. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 24 December 2015 // // Author: // // John Burkardt // // Parameters: // // Input, int N, the size of the set. // // Input/output, int &SEED, a seed for the random number // generator. // // Output, int SUBSET_RANDOM[N], defines the subset using 0 and 1 values. // { int[] s = UniformRNG.i4vec_uniform_ab_new(n, 0, 1, ref seed); return(s); }
public static void i4vec_sort_heap_index_a_test() //****************************************************************************80 // // Purpose: // // I4VEC_SORT_HEAP_INDEX_A_TEST tests I4VEC_SORT_HEAP_INDEX_A. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 27 October 2014 // // Author: // // John Burkardt // { int i; const int n = 20; Console.WriteLine(""); Console.WriteLine("I4VEC_SORT_HEAP_INDEX_A_TEST"); Console.WriteLine(" I4VEC_SORT_HEAP_INDEX_A creates an ascending"); Console.WriteLine(" sort index for an I4VEC."); const int b = 0; const int c = 3 * n; int seed = 123456789; int[] a = UniformRNG.i4vec_uniform_ab_new(n, b, c, ref seed); typeMethods.i4vec_print(n, a, " Unsorted array:"); int[] indx = typeMethods.i4vec_sort_heap_index_a(n, a); typeMethods.i4vec_print(n, indx, " Sort vector INDX:"); Console.WriteLine(""); Console.WriteLine(" I INDX(I) A(INDX(I))"); Console.WriteLine(""); for (i = 0; i < n; i++) { Console.WriteLine(" " + i.ToString().PadLeft(8) + " " + indx[i].ToString().PadLeft(8) + " " + a[indx[i]].ToString().PadLeft(8) + ""); } }
public static void i4vec_permute_test() //****************************************************************************80 // // Purpose: // // I4VEC_PERMUTE_TEST tests I4VEC_PERMUTE. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 26 October 2014 // // Author: // // John Burkardt // { const int n = 12; int seed = 123456789; Console.WriteLine(""); Console.WriteLine("I4VEC_PERMUTE_TEST"); Console.WriteLine(" I4VEC_PERMUTE reorders an integer vector"); Console.WriteLine(" according to a given permutation."); Console.WriteLine(" Using initial random number seed = " + seed + ""); const int b = 0; int[] a = UniformRNG.i4vec_uniform_ab_new(n, b, n, ref seed); typeMethods.i4vec_print(n, a, " A, before rearrangement:"); int[] p = typeMethods.perm_uniform_new(n, ref seed); typeMethods.i4vec_print(n, p, " Permutation vector P:"); typeMethods.i4vec_permute(n, p, ref a); typeMethods.i4vec_print(n, a, " A, after rearrangement:"); }
private static void i4vec_dot_product_test() //****************************************************************************80 // // Purpose: // // I4VEC_DOT_PRODUCT_TEST tests I4VEC_DOT_PRODUCT. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 24 December 2015 // // Author: // // John Burkardt // { const int n = 5; Console.WriteLine(""); Console.WriteLine("I4VEC_DOT_PRODUCT_TEST"); Console.WriteLine(" I4VEC_DOT_PRODUCT computes the dot product of two I4VECs."); const int lo = 0; const int hi = 10; int seed = 123456789; int[] a = UniformRNG.i4vec_uniform_ab_new(n, lo, hi, ref seed); typeMethods.i4vec_print(n, a, " The vector A:"); int[] b = UniformRNG.i4vec_uniform_ab_new(n, lo, hi, ref seed); typeMethods.i4vec_print(n, b, " The vector B:"); int d = typeMethods.i4vec_dot_product(n, a, b); Console.WriteLine(""); Console.WriteLine(" The dot product is " + d + ""); }
private static void i4vec_uniform_ab_new_test() //****************************************************************************80 // // Purpose: // // I4VEC_UNIFORM_AB_NEW_TEST tests I4VEC_UNIFORM_AB_NEW. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 27 October 2014 // // Author: // // John Burkardt // { const int a = -100; const int b = 200; const int n = 20; int seed = 123456789; Console.WriteLine(""); Console.WriteLine("I4VEC_UNIFORM_AB_NEW_TEST"); Console.WriteLine(" I4VEC_UNIFORM_AB_NEW computes pseudorandom values"); Console.WriteLine(" in an interval [A,B]."); Console.WriteLine(""); Console.WriteLine(" The lower endpoint A = " + a + ""); Console.WriteLine(" The upper endpoint B = " + b + ""); Console.WriteLine(" The initial seed is " + seed + ""); Console.WriteLine(""); int[] v = UniformRNG.i4vec_uniform_ab_new(n, a, b, ref seed); typeMethods.i4vec_print(n, v, " The random vector:"); }
private static void i4vec_unique_count_test() //****************************************************************************80 // // Purpose: // // I4VEC_UNIQUE_COUNT_TEST tests I4VEC_UNIQUE_COUNT. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 09 March 2016 // // Author: // // John Burkardt // { Console.WriteLine(""); Console.WriteLine("I4VEC_UNIQUE_COUNT_TEST"); Console.WriteLine(" I4VEC_UNIQUE_COUNT counts unique entries in an I4VEC."); const int n = 20; const int a_lo = 0; int seed = 123456789; int[] a = UniformRNG.i4vec_uniform_ab_new(n, a_lo, n, ref seed); typeMethods.i4vec_print(n, a, " Array:"); int a_unique = typeMethods.i4vec_unique_count(n, a); Console.WriteLine(""); Console.WriteLine(" Number of unique entries is " + a_unique + ""); }
public static void i4vec_sum_test() //****************************************************************************80 // // Purpose: // // I4VEC_SUM_TEST tests I4VEC_SUM. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 27 October 2014 // // Author: // // John Burkardt // { Console.WriteLine(""); Console.WriteLine("I4VEC_SUM_TEST"); Console.WriteLine(" I4VEC_SUM sums the entries of an I4VEC."); const int n = 5; const int lo = 0; const int hi = 10; int seed = 123456789; int[] a = UniformRNG.i4vec_uniform_ab_new(n, lo, hi, ref seed); typeMethods.i4vec_print(n, a, " The vector:"); int s = typeMethods.i4vec_sum(n, a); Console.WriteLine(""); Console.WriteLine(" The vector entries sum to " + s + ""); }
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) + ""); } }
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) + ""); } }
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) + ""); } }
public static void mono_next_grlex_test() //****************************************************************************80 // // Purpose: // // MONO_NEXT_GRLEX_TEST tests MONO_NEXT_GRLEX. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 08 December 2013 // // Author: // // John Burkardt // { const int m = 4; int i; Console.WriteLine(""); Console.WriteLine("MONO_NEXT_GRLEX_TEST"); Console.WriteLine(" MONO_NEXT_GRLEX computes the next monomial"); Console.WriteLine(" in M variables, in grlex order."); Console.WriteLine(""); Console.WriteLine(" Let M = " + m + ""); const int a = 0; const int b = 3; int seed = 123456789; for (i = 1; i <= 10; i++) { int[] x = UniformRNG.i4vec_uniform_ab_new(m, a, b, ref seed); Console.WriteLine(""); string cout = " "; int k; for (k = 0; k < m; k++) { cout += x[k].ToString().PadLeft(2); } Console.WriteLine(cout); int j; for (j = 1; j <= 5; j++) { Monomial.mono_next_grlex(m, ref x); cout = " "; for (k = 0; k < m; k++) { cout += x[k].ToString().PadLeft(2); } Console.WriteLine(cout); } } }
private static void test02() //****************************************************************************80 // // Purpose: // // TEST02 compares exact and estimated integrals in 6D. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 16 January 2014 // // Author: // // John Burkardt // { const int m = 6; const int n = 4192; int test; const int test_num = 20; Console.WriteLine(""); Console.WriteLine("TEST02"); 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(" E1 E2 E3 E4 E5 E6 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) + " " + e[3].ToString().PadLeft(2) + " " + e[4].ToString().PadLeft(2) + " " + e[5].ToString().PadLeft(2) + " " + result.ToString(CultureInfo.InvariantCulture).PadLeft(14) + " " + exact.ToString(CultureInfo.InvariantCulture).PadLeft(14) + " " + error.ToString(CultureInfo.InvariantCulture).PadLeft(14) + ""); } }
private static void test01() //****************************************************************************80 // // Purpose: // // TEST01 uses CIRCLE01_SAMPLE with an increasing number of points. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 12 January 2014 // // Author: // // John Burkardt // { const int m = 2; const int n = 4192; int test; const int test_num = 20; Console.WriteLine(""); Console.WriteLine("TEST01"); Console.WriteLine(" Use CIRCLE01_SAMPLE to compare exact and"); Console.WriteLine(" estimated integrals along the circumference"); Console.WriteLine(" of the unit circle in 2D."); // // Get sample points. // int seed = 123456789; double[] x = Integrals.circle01_sample(n, ref seed); Console.WriteLine(""); Console.WriteLine(" Number of sample points used is " + n + ""); // // Randomly choose X, Y exponents. // Console.WriteLine(""); Console.WriteLine(" If any exponent is odd, the integral is zero."); Console.WriteLine(" We restrict this test to randomly chosen even 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, 5, 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.circle01_length() * typeMethods.r8vec_sum(n, value) / n; double exact = Integrals.circle01_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) + " " + result.ToString(CultureInfo.InvariantCulture).PadLeft(14) + " " + exact.ToString(CultureInfo.InvariantCulture).PadLeft(14) + " " + error.ToString(CultureInfo.InvariantCulture).PadLeft(10) + ""); } }
private static void sort_safe_rc_test() //****************************************************************************80 // // Purpose: // // SORT_SAFE_RC_TEST tests SORT_SAFE_RC. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 10 March 2015 // // Author: // // John Burkardt // { int i = 0; int i_save = 0; int isgn = 0; int j = 0; int j_save = 0; int k_save = 0; int l_save = 0; const int n = 20; int n_save = 0; Console.WriteLine(""); Console.WriteLine("SORT_SAFE_RC_TEST"); Console.WriteLine(" SORT_SAFE_RC sorts objects externally."); Console.WriteLine(" This version of the algorithm does not rely on"); Console.WriteLine(" internally saved or 'persistent' or 'static' memory."); // // Generate some data to sort. // const int i4_lo = 1; int seed = 123456789; int[] a = UniformRNG.i4vec_uniform_ab_new(n, i4_lo, n, ref seed); typeMethods.i4vec_print(n, a, " Unsorted array:"); // // Sort the data. // int indx = 0; for (;;) { Sort.sort_safe_rc(n, ref indx, ref i, ref j, isgn, ref i_save, ref j_save, ref k_save, ref l_save, ref n_save); if (indx < 0) { isgn = 1; if (a[i - 1] <= a[j - 1]) { isgn = -1; } } else if (0 < indx) { (a[i - 1], a[j - 1]) = (a[j - 1], a[i - 1]); } else { break; } } // // Display the sorted data. // typeMethods.i4vec_print(n, a, " Sorted array:"); }
public static void sort_heap_external_test( ) //****************************************************************************80 // // Purpose: // // SORT_HEAP_EXTERNAL_TEST tests SORT_HEAP_EXTERNAL. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 05 January 2007 // // Author: // // John Burkardt // { const int n = 20; SortHeapExternalData data = new(); Console.WriteLine(""); Console.WriteLine("SORT_HEAP_EXTERNAL_TEST"); Console.WriteLine(" SORT_HEAP_EXTERNAL sorts objects externally."); int seed = 123456789; int[] a = UniformRNG.i4vec_uniform_ab_new(n, 1, n, ref seed); typeMethods.i4vec1_print(n, a, " Before sorting:"); int indx = 0; int i = 0; int j = 0; int isgn = 0; for ( ; ;) { Sort.sort_heap_external(ref data, n, ref indx, ref i, ref j, isgn); if (indx < 0) { if (a[i - 1] <= a[j - 1]) { isgn = -1; } else { isgn = +1; } } else if (0 < indx) { (a[i - 1], a[j - 1]) = (a[j - 1], a[i - 1]); } else { break; } } typeMethods.i4vec1_print(n, a, " After sorting:"); }
private static void sort_rc_test() //****************************************************************************80 // // Purpose: // // SORT_RC_TEST tests SORT_RC. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 10 March 2015 // // Author: // // John Burkardt // { int i = 0; int isgn = 0; int j = 0; const int n = 20; Sort.SortRCData data = new(); Console.WriteLine(""); Console.WriteLine("SORT_RC_TEST"); Console.WriteLine(" SORT_RC sorts objects externally."); Console.WriteLine(" This function relies on the use of persistent"); Console.WriteLine(" data stored internally."); // // Generate some data to sort. // const int i4_lo = 1; int seed = 123456789; int[] a = UniformRNG.i4vec_uniform_ab_new(n, i4_lo, n, ref seed); typeMethods.i4vec_print(n, a, " Unsorted array:"); // // Sort the data. // int indx = 0; for (;;) { Sort.sort_rc(ref data, n, ref indx, ref i, ref j, isgn); if (indx < 0) { isgn = 1; if (a[i - 1] <= a[j - 1]) { isgn = -1; } } else if (0 < indx) { (a[i - 1], a[j - 1]) = (a[j - 1], a[i - 1]); } else { break; } } // // Display the sorted data. // typeMethods.i4vec_print(n, a, " Sorted array:"); }
public static int birthday_sample(int n, ref int seed) //****************************************************************************80 // // Purpose: // // BIRTHDAY_SAMPLE samples the Birthday Concurrence PDF. // // Discussion: // // The probability is the probability that the N-th person is the // first one to match a birthday with someone earlier. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 10 March 2016 // // Author: // // John Burkardt // // Parameters: // // Input, int N, the number of people whose birthdays have been // disclosed. // // Input/output, int &SEED, a seed for the random number generator. // // Output, int BIRTHDAY_SAMPLE, // * 1 if the first N-1 people had distinct // birthdays, but person N had a birthday in common with a previous person, // * 0 otherwise. // { int value; switch (n) { case < 1: value = 0; return(value); } // // Choose N birthdays at random. // int[] b = UniformRNG.i4vec_uniform_ab_new(n, 1, 365, ref seed); // // Are the first N-1 birthdays unique? // int u1 = typeMethods.i4vec_unique_count(n - 1, b); if (u1 < n - 1) { value = 0; return(value); } // // Does the N-th birthday match an earlier one? // int u2 = typeMethods.i4vec_unique_count(n, b); value = u2 == n - 1 ? 1 : 0; return(value); }