### Sample Program Setup with Error Policy Source: https://www.boost.org/doc/libs/latest/libs/math/doc/html/math_toolkit/stat_tut/weg/error_eg.html Includes necessary headers and sets the BOOST_MATH_DOMAIN_ERROR_POLICY macro for error handling. This code demonstrates the setup for the error handling example. ```c++ #define BOOST_MATH_DOMAIN_ERROR_POLICY ignore_error // Boost #include using boost::math::students_t; // Probability of students_t(df, t). // std #include using std::cout; using std::endl; #include #include // using ::errno ``` -------------------------------- ### Inverse Chi-Squared Distribution Bayes Example Setup Source: https://www.boost.org/doc/libs/latest/libs/math/example/inverse_chi_squared_bayes_eg.cpp Includes necessary headers and sets up the main function for the inverse chi-squared distribution Bayesian example. It defines using statements for distributions and quantiles. ```cpp #include "boost/math/distributions.hpp" int main() { using std::cout; using std::endl; using ::boost::math::inverse_chi_squared; using ::boost::math::inverse_gamma; using ::boost::math::quantile; using ::boost::math::cdf; cout << "Inverse_chi_squared_distribution Bayes example: " << endl < #include #include #include int main() { using boost::math::lambert_w_detail::lambert_w_halley_step; using boost::math::epsilon_difference; using boost::math::relative_difference; std::cout << std::showpoint << std::endl; // and show any significant trailing zeros too. std::cout.precision(std::numeric_limits::max_digits10); // 17 decimal digits for double. // ... rest of the Halley step example code ... return 0; } ``` -------------------------------- ### Example Usage of Luroth Expansion Source: https://www.boost.org/doc/libs/latest/libs/math/doc/html/math_toolkit/internals/luroth_expansion.html This example demonstrates how to use the `luroth_expansion` class to compute and print the Luroth representation of Pi. It shows the necessary includes and how to instantiate the class and output its representation. ```cpp using boost::math::constants::pi; using boost::math::tools::luroth_expansion; auto luroth = luroth_expansion(pi()); std::cout << "π ≈ " << luroth << "\n"; // Prints: // π ≈ ((3; 7, 1, 1, 1, 2, 1, 4, 23, 4, 1, 1, 1, 1, 80, 1, 1, 5)) ``` -------------------------------- ### C# Example: Statistical Distribution Explorer Source: https://www.boost.org/doc/libs/latest/libs/math/doc/html/math_toolkit/main_faq.html This snippet is part of an example demonstrating how to build a C# utility for statistical distributions using Boost.Math. It requires building Boost.Math as a dynamic library (.dll) and compiling with the /CLI option. ```csharp See the boost/math/dot_net_example folder which contains an example that builds a simple statistical distribution app with a GUI. ``` -------------------------------- ### Jacobi Zeta Function Example Source: https://www.boost.org/doc/libs/latest/libs/math/doc/html/math_toolkit/ellint/jacobi_zeta.html An example demonstrating the use of the Jacobi Zeta function, comparing Boost.Math with Wolfram Alpha and showing high-precision calculations with Boost.Multiprecision. This example requires a separate file (jacobi_zeta_example.cpp) for execution. ```cpp // A simple example comparing use of Wolfram Alpha with Boost.Math (including much higher precision using Boost.Multiprecision) is jacobi_zeta_example.cpp. ``` -------------------------------- ### Example Usage of Centered Continued Fraction Source: https://www.boost.org/doc/libs/latest/libs/math/doc/html/math_toolkit/internals/centered_continued_fraction.html Demonstrates how to create a `centered_continued_fraction` object for pi and print its representation. This example requires including the Boost math constants and tools namespaces. ```cpp using boost::math::constants::pi; using boost::math::tools::centered_continued_fraction; auto cfrac = centered_continued_fraction(pi()); std::cout << "π ≈ " << cfrac << "\n"; // Prints: // π ≈ [3; 7, 16, -294, 3, -4, 5, -15, -3, 2, 2] ``` -------------------------------- ### Example: Continued Fraction of Pi Source: https://www.boost.org/doc/libs/latest/libs/math/doc/html/math_toolkit/internals/simple_continued_fraction.html Demonstrates how to create a `simple_continued_fraction` object for `pi` and print its continued fraction representation. This example requires including the `pi` constant from `boost::math::constants`. ```cpp using boost::math::constants::pi; using boost::math::tools::simple_continued_fraction; auto cfrac = simple_continued_fraction(pi()); std::cout << "π ≈ " << cfrac << "\n"; // Prints: // π ≈ [3; 7, 15, 1, 292, 1, 1, 1, 2, 1, 3, 1, 14, 2, 1, 1, 2] ``` -------------------------------- ### exp_sinh Integration Example Source: https://www.boost.org/doc/libs/latest/libs/math/doc/html/math_toolkit/double_exponential/de_exp_sinh.html Demonstrates how to use the exp_sinh integrator for a function over a half-infinite interval. ```cpp exp_sinh integrator; auto f = [](double x) { return exp(-3*x); }; double termination = sqrt(std::numeric_limits::epsilon()); double error; double L1; double Q = integrator.integrate(f, termination, &error, &L1); ``` -------------------------------- ### Example: High Precision Airy Zeros with Boost.Multiprecision Source: https://www.boost.org/doc/libs/latest/libs/math/doc/html/math_toolkit/airy/airy_root.html This example shows how to combine Boost.Math with Boost.Multiprecision for calculating Airy function zeros with high precision. It includes necessary headers and type definitions. ```cpp #include typedef boost::multiprecision::cpp_dec_float_50 float_type; ``` ```cpp #include ``` ```cpp template T airy_ai_zero(unsigned m); // 1-based index of the zero. ``` -------------------------------- ### Main Function Setup and Output Formatting Source: https://www.boost.org/doc/libs/latest/libs/math/example/negative_binomial_example1.cpp Initializes the main function, prints a descriptive header, and sets the output precision for floating-point numbers. This prepares the console for displaying calculation results. ```cpp int main() { cout <<"Selling candy bars - using the negative binomial distribution." << "\nby Dr. Diane Evans," "\nProfessor of Mathematics at Rose-Hulman Institute of Technology," << "\nsee http://en.wikipedia.org/wiki/Negative_binomial_distribution\n" << endl; cout << endl; cout.precision(5); // None of the values calculated have a useful accuracy as great this, but // INF shows wrongly with < 5 ! // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=240227 ``` -------------------------------- ### Get Type Information for a float128 variable Source: https://www.boost.org/doc/libs/latest/libs/math/example/float128_example.cpp Retrieves and prints the type information for a variable of type float128. This example shows how to get the mangled name for a custom type. ```cpp const std::type_info& tpi = typeid(pi1); // OK using GCC 6.1.1. // (from GCC 5 according to http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43622) std::cout << tpi.name() << std::endl; // OK, Output implementation-dependent mangled name: // N5boost14multiprecision6numberINS0_8backends16float128_backendELNS0_26expression_template_optionE0EEE ``` -------------------------------- ### Include Headers and Setup Source: https://www.boost.org/doc/libs/latest/libs/math/doc/html/math_toolkit/stat_tut/weg/binom_eg/binomial_coinflip_example.html Includes necessary headers for the binomial distribution, standard input/output, and formatting. Sets up the main function and a try-catch block for error handling. ```C++ #include using boost::math::binomial; #include using std::cout; using std::endl; using std::left; #include using std::setw; int main() { cout << "Using Binomial distribution to predict how many heads and tails." << endl; try { ``` -------------------------------- ### Example: Initialize Hyperexponential Distribution Source: https://www.boost.org/doc/libs/latest/libs/math/doc/html/math_toolkit/dist_ref/dists/hyperexponential_dist.html Demonstrates initializing a hyperexponential distribution with specific rate values and verifies the calculated equal phase probabilities. ```cpp hyperexponential he = { 1.0 / 10, 1.0 / 12 }; BOOST_MATH_ASSERT(he.probabilities()[0] == 0.5); ``` -------------------------------- ### Get Type Information for float128 Source: https://www.boost.org/doc/libs/latest/libs/math/example/float128_example.cpp Retrieves and prints the type information for the float128 type. This example demonstrates successful retrieval and output of the type name. ```cpp const std::type_info& tif128 = typeid(float128); // OK. std::cout << tif128.name() << std::endl; // OK. std::cout << typeid(float128).name() << std::endl; // OK. ``` -------------------------------- ### Fibonacci Generator Usage Example Source: https://www.boost.org/doc/libs/latest/libs/math/doc/html/math_toolkit/number_series/fibonacci_numbers.html Demonstrates the basic usage of the `fibonacci_generator` to obtain consecutive Fibonacci numbers and how to reset the generator's starting point. ```cpp boost::math::fibonacci_generator gen; int x = gen(); // x is 0 int y = gen(); // y is 1 for(int i = 0; i < 5; ++i) // this loop is same as gen.set(7); gen(); int z = gen(); // z is 13 ``` -------------------------------- ### Print Gauss Constants for Order 30 Source: https://www.boost.org/doc/libs/latest/libs/math/tools/gauss_kronrod_constants.cpp Prints Gauss constants for a quadrature rule of order 30. This example showcases the flexibility of the function in retrieving constants for various quadrature setups. ```C++ print_gauss_constants >("f", 9, 0); print_gauss_constants >("", 17, 1); print_gauss_constants >("L", 35, 2); print_gauss_constants >("Q", 35, 3); print_gauss_constants >("", 115, 4); ``` -------------------------------- ### Adjusting Standard Deviation for a Target Quantile (Guess 2) Source: https://www.boost.org/doc/libs/latest/libs/math/doc/html/math_toolkit/stat_tut/weg/find_eg/find_mean_and_sd_eg.html Further refines the standard deviation to get closer to the target quantile. This example increases the standard deviation slightly from the previous guess. ```C++ normal pack06(mean, 0.06); cout << "Quantile of " << p << " = " << quantile(pack06, p) << ", mean = " << pack06.mean() << ", sd = " << pack06.standard_deviation() << endl; // Quantile of 0.05 = 2.90131, mean = 3, sd = 0.06 cout <<"Fraction of packs >= " << minimum_weight << " with a mean of " << mean << " and standard deviation of " << pack06.standard_deviation() << " is " << cdf(complement(pack06, minimum_weight)) << endl; // Fraction of packs >= 2.9 with a mean of 3 and standard deviation of 0.06 is 0.95221 ``` -------------------------------- ### Main Function Entry Point Source: https://www.boost.org/doc/libs/latest/libs/math/example/normal_tables.cpp The main function initializes the example and includes a try-catch block to display any exceptions that might occur during the execution of the normal distribution table generation. ```cpp int main() { std::cout << "\nExample: Normal distribution tables." << std::endl; using namespace boost::math; try {// Tip - always use try'n'catch blocks to ensure that messages from thrown exceptions are shown. ``` -------------------------------- ### Build Boost.Math Library on Windows Source: https://www.boost.org/doc/libs/latest/libs/math/doc/html/math_toolkit/building.html Use this command to build the Boost.Math library on Windows using Boost.Build. This command stages the libraries in the `/stage` sub-folder. ```bash bjam toolset=msvc --with-math --build-type=complete stage ``` -------------------------------- ### Adjusting Standard Deviation for a Target Quantile (Guess 1) Source: https://www.boost.org/doc/libs/latest/libs/math/doc/html/math_toolkit/stat_tut/weg/find_eg/find_mean_and_sd_eg.html Demonstrates adjusting the standard deviation of a normal distribution to get closer to a target quantile. This example shows an iterative approach by halving the initial standard deviation. ```C++ normal pack05(mean, 0.05); cout << "Quantile of " << p << " = " << quantile(pack05, p) << ", mean = " << pack05.mean() << ", sd = " << pack05.standard_deviation() << endl; // Quantile of 0.05 = 2.91776, mean = 3, sd = 0.05 cout <<"Fraction of packs >= " << minimum_weight << " with a mean of " << mean << " and standard deviation of " << pack05.standard_deviation() << " is " << cdf(complement(pack05, minimum_weight)) << endl; // Fraction of packs >= 2.9 with a mean of 3 and standard deviation of 0.05 is 0.97725 ``` -------------------------------- ### Setup and Generate Roots Table File Source: https://www.boost.org/doc/libs/latest/libs/math/example/root_elliptic_finding.cpp Configures and opens a file for writing root-finding performance tables. It includes copyright information, compiler/platform details, and calls a function to populate the table. ```C++ int roots_tables(cpp_bin_float_100 radius, cpp_bin_float_100 arc, double digits_accuracy) { ::digits_accuracy = digits_accuracy; // Save globally so that it is available to root-finding algorithms. Ugly :-( #if defined(_DEBUG) || !defined(NDEBUG) std::string debug_or_optimize("Compiled in debug mode."); #else std::string debug_or_optimize("Compiled in optimise mode."); #endif // Create filename for roots_table std::string qbk_name = full_roots_name; qbk_name += "elliptic_table"; std::stringstream ss; ss.precision(3); // ss << "_" << N // now put all the tables in one .qbk file? ss << "_" << digits_accuracy * 100 << std::flush; // Assume only save optimize mode runs, so don't add any _DEBUG info. qbk_name += ss.str(); #ifdef _MSC_VER qbk_name += "_msvc"; #else // assume GCC qbk_name += "_gcc"; #endif if (fp_hardware != "") { qbk_name += fp_hardware; } qbk_name += ".qbk"; fout.open(qbk_name, std::ios_base::out); if (fout.is_open()) { std::cout << "Output root table to " << qbk_name << std::endl; } else { // Failed to open. std::cout << " Open file " << qbk_name << " for output failed!" << std::endl; std::cout << "errno " << errno << std::endl; return errno; } fout << "[/" << qbk_name << "\n" "Copyright 2015 Paul A. Bristow." "\n" "Copyright 2015 John Maddock." "\n" "Distributed under the Boost Software License, Version 1.0." "\n" "(See accompanying file LICENSE_1_0.txt or copy at""\n" "http://www.boost.org/LICENSE_1_0.txt).""\n" << std::endl; // Print out the program/compiler/stdlib/platform names as a Quickbook comment: fout << "\n[h6 Program [@../../example/" << short_file_name(sourcefilename) << " " << short_file_name(sourcefilename) << ",\n " << BOOST_COMPILER << ", " << BOOST_STDLIB << ", " << BOOST_PLATFORM << "\n" << debug_or_optimize << ((fp_hardware != "") ? ", " + fp_hardware : "") << "]" // [h6 close]. << std::endl; //fout << "Fraction of full accuracy " << digits_accuracy << std::endl; table_root_info(radius, arc); fout.close(); // table_type_info(digits_accuracy); return 0; } // roots_tables ``` -------------------------------- ### Setting up Quickbook Table Header Source: https://www.boost.org/doc/libs/latest/libs/math/example/root_n_finding_algorithms.cpp Prepares a string stream to format a Quickbook table header for Nth root results. It includes the root value, types tested, and hardware information if available. ```C++ std::stringstream table_info; table_info.precision(3); table_info << "[table:root_" << N << " " << N << "th root(" << static_cast(full_value) << ") for float, double, long double and cpp_bin_float_50 types"; if (fp_hardware != "") { table_info << ", using " << fp_hardware; } table_info << std::endl; ``` -------------------------------- ### Distribution Concept Usage Example Source: https://www.boost.org/doc/libs/latest/libs/math/doc/html/math_toolkit/archetypes.html Example of how the DistributionConcept is used to verify non-member accessors for distributions. ```c++ v = pdf(dist, x); // (Result v is ignored). ``` -------------------------------- ### Root Testing Utility Setup Source: https://www.boost.org/doc/libs/latest/libs/math/example/root_finding_algorithms.cpp A utility function for testing root-finding algorithms. It sets up precision, type information, and initial parameters for a given type T, preparing for iterative root-finding tests. ```C++ template int test_root(cpp_bin_float_100 big_value, cpp_bin_float_100 answer, const char* type_name) { std::size_t max_digits = 2 + std::numeric_limits::digits * 3010 / 10000; std::cout.precision(max_digits); std::cout << std::showpoint << std::endl; root_infos.push_back(root_info()); type_no++; root_infos[type_no].max_digits10 = max_digits; root_infos[type_no].full_typename = typeid(T).name(); root_infos[type_no].short_typename = type_name; root_infos[type_no].bin_digits = std::numeric_limits::digits; root_infos[type_no].get_digits = std::numeric_limits::digits; T to_root = static_cast(big_value); T result; T ans = static_cast(answer); int algo = 0; using boost::timer::nanosecond_type; using boost::timer::cpu_times; using boost::timer::cpu_timer; cpu_times now; T sum = 0; ``` -------------------------------- ### Polynomial Representation Example Source: https://www.boost.org/doc/libs/latest/libs/math/example/polynomial_arithmetic.cpp Displays the string representation of two polynomials, 'a' and 'b', used in subsequent arithmetic examples. ```text a = 3x^3 - 4x^2 - 6x + 10 b = x - 2 ``` -------------------------------- ### Invalid PDF Calculation Example Source: https://www.boost.org/doc/libs/latest/libs/math/doc/html/math_toolkit/stat_tut/weg/neg_binom_eg/negative_binomial_example1.html Shows an example of calling the pdf function with an invalid negative argument, which would result in an exception if not handled. ```cpp pdf(nb, -1) ``` -------------------------------- ### Cube Root Finding Example Source: https://www.boost.org/doc/libs/latest/libs/math/example/root_finding_example.cpp Illustrates the usage of cube root finding functions, showing both successful and iterative approximation methods. ```C++ // Cube Root finding (cbrt) Example. std::cout << "Iterations 10\n"; std::cout << "cbrt_1(27) = " << cbrt_1(27) << std::endl; std::cout << "Iterations 10\n"; std::cout << "Unable to locate solution in chosen iterations: Current best guess is between 3.0365889718756613 and 3.0365889718756627\n"; std::cout << "cbrt_1(28) = " << cbrt_1(28) << std::endl; std::cout << "cbrt_1(27) = " << cbrt_1(27) << std::endl; std::cout << "cbrt_2(28) = " << cbrt_2(28) << std::endl; std::cout << "Iterations 4\n"; std::cout << "cbrt_3(27) = " << cbrt_3(27) << std::endl; std::cout << "Iterations 5\n"; std::cout << "cbrt_3(28) = " << cbrt_3(28) << std::endl; ``` -------------------------------- ### Example Output of `print_info_on_type` Source: https://www.boost.org/doc/libs/latest/libs/math/doc/html/math_toolkit/tutorial/user_def.html This is an example of the diagnostic output generated by `print_info_on_type` for a user-defined type, indicating runtime precision and potential performance implications. ```text Information on the Implementation and Handling of Mathematical Constants for Type class boost::math::concepts::real_concept Checking for std::numeric_limits specialisation: no boost::math::policies::precision reports that there is no compile type precision available. boost::math::tools::digits() reports that the current runtime precision is 53 binary digits. No compile time precision is available, the construction method will be decided at runtime and results will not be cached - this may lead to poor runtime performance. Current runtime precision indicates that the constant will be constructed from a string on each call. ``` -------------------------------- ### Laguerre Polynomial Sequence Generation Example Source: https://www.boost.org/doc/libs/latest/libs/math/doc/html/math_toolkit/sf_poly/laguerre.html Example demonstrating how to use laguerre_next to generate a vector of the first 10 Laguerre polynomial values. ```cpp double x = 0.5; // Abscissa value vector v; v.push_back(laguerre(0, x)).push_back(laguerre(1, x)); for(unsigned l = 1; l < 10; ++l) v.push_back(laguerre_next(l, x, v[l], v[l-1])); ``` -------------------------------- ### Program Output Example Source: https://www.boost.org/doc/libs/latest/libs/math/doc/html/math_toolkit/stat_tut/weg/find_eg/find_scale_eg.html Illustrates the expected output of a program that uses the find_scale function with different policies and probability calculations. ```text Example: Find scale (standard deviation). Normal distribution with mean = 0, standard deviation 1, has fraction <= -2, p = 0.0227501 Normal distribution with mean = 0, standard deviation 1, has fraction > -2, p = 0.97725 scale (standard deviation) = 0.647201 Normal distribution with mean = 0 has fraction <= -2, p = 0.001 Normal distribution with mean = 0 has fraction > -2, p = 0.999 Normal distribution with mean = 0.946339 has fraction <= -2 = 0.001 Normal distribution with mean = 0.946339 has fraction > -2 = 0.999 ``` -------------------------------- ### C++11 Requirement for Numeric Limits Source: https://www.boost.org/doc/libs/latest/libs/math/doc/html/math_toolkit/lambert_w.html Example of a build command that requires C++11 numeric limits for compiling Boost.Math Lambert W examples. ```Shell [ run lambert_w_basic_example.cpp : : : [ requires cxx11_numeric_limits ] ] ``` -------------------------------- ### Arcsine Distribution Constructor Example Source: https://www.boost.org/doc/libs/latest/libs/math/doc/html/math_toolkit/dist_ref/dists/arcine_dist.html Demonstrates constructing an arcsine distribution with custom range parameters. Requires x_min < x_max. ```c++ arcsine_distribution<> myarcsine(-2, 4); ``` -------------------------------- ### Basic Usage Example Source: https://www.boost.org/doc/libs/latest/libs/math/doc/html/math_toolkit/lambert_w.html Demonstrates the fundamental usage of the `lambert_w0` and `lambert_wm1` functions from the Boost.Math library for calculating the principal branches of the Lambert W function. ```APIDOC ## Basic Usage Example ### Description This example shows how to include the necessary header and use the `lambert_w0` and `lambert_wm1` functions to compute values for the Lambert W function. ### Code ```cpp #include #include #include using boost::math::lambert_w0; using boost::math::lambert_wm1; int main() { std::cout.precision(std::numeric_limits::max_digits10); std::cout << std::showpoint << std::endl; double z = 10.0; double r_w0 = lambert_w0(z); // Default policy for double. std::cout << "lambert_w0(" << z << ") = " << r_w0 << std::endl; // Expected output: lambert_w0(10.0) = 1.7455280027406994 // Example for lambert_wm1 (requires z >= -1/e) double z_wm1 = -0.5; double r_wm1 = lambert_wm1(z_wm1); std::cout << "lambert_wm1(" << z_wm1 << ") = " << r_wm1 << std::endl; // Expected output: lambert_wm1(-0.5) = -1.2564307457511997 return 0; } ``` ### Usage Notes - Ensure you have the Boost libraries installed and configured for your C++ project. - The `lambert_w0` function computes the principal branch (W0) for `z >= -1/e`. - The `lambert_wm1` function computes the other real branch (W-1) for `-1/e <= z < 0`. ``` -------------------------------- ### Example: Pi with Boost.Multiprecision Source: https://www.boost.org/doc/libs/latest/libs/math/doc/html/math_toolkit/tutorial/user_def.html Shows a concrete example of using the pi constant with Boost.Multiprecision's cpp_dec_float_50 type for 50 decimal digits of precision. ```cpp boost::math::constants::pi(); ``` -------------------------------- ### Using Custom Distributions with Policies Source: https://www.boost.org/doc/libs/latest/libs/math/doc/html/math_toolkit/pol_tutorial/namespace_policies.html Demonstrates using distributions defined with custom policies. This example shows constructing a normal distribution and a binomial distribution, then calling quantile functions to observe the custom policy effects, such as infinity on overflow and integer results for discrete quantiles. ```c++ int main() { // Construct distribution with something we know will overflow // (using double rather than if promoted to long double): my_distributions::normal norm(10, 2); errno = 0; cout << "Result of quantile(norm, 0) is: " << quantile(norm, 0) << endl; // -infinity. cout << "errno = " << errno << endl; errno = 0; cout << "Result of quantile(norm, 1) is: " << quantile(norm, 1) << endl; // +infinity. cout << "errno = " << errno << endl; // Now try a discrete distribution. my_distributions::binomial binom(20, 0.25); cout << "Result of quantile(binom, 0.05) is: " << quantile(binom, 0.05) << endl; // To check we get integer results. cout << "Result of quantile(complement(binom, 0.05)) is: " << quantile(complement(binom, 0.05)) << endl; } ``` -------------------------------- ### Basic Trapezoidal Integration Example Source: https://www.boost.org/doc/libs/latest/libs/math/doc/html/math_toolkit/trapezoidal.html This example demonstrates the basic usage of the trapezoidal quadrature function to integrate a simple real-valued function over a specified interval. ```cpp using boost::math::quadrature::trapezoidal; auto f = [](double x) { return 1/(5 - 4*cos(x)); }; double I = trapezoidal(f, 0.0, boost::math::constants::two_pi()); ``` -------------------------------- ### Error Message Example Source: https://www.boost.org/doc/libs/latest/libs/math/doc/html/math_toolkit/stat_tut/weg/find_eg/find_scale_eg.html Shows an example of a warning message generated when probabilities are provided in the wrong order, indicating a potential issue with complement usage. ```text Message from thrown exception was: Error in function boost::math::find_scale(complement(double, double, double, Policy)): Computed scale (-0.48043523852179076) is <= 0! Was the complement intended? ``` -------------------------------- ### Configuring Visual Studio Include Directories for Boost Source: https://www.boost.org/doc/libs/latest/libs/math/doc/html/math_toolkit/main_faq.html Provides an example of how to add a Boost include path to Visual Studio's project settings. This is necessary for the compiler to find Boost header files. It suggests adding the Boost root directory to the include path. ```text your Boost place /boost-latest_release, for example `X:/boost_1_70_0/` ``` -------------------------------- ### Example Output for find_scale Source: https://www.boost.org/doc/libs/latest/libs/math/example/find_scale_example.cpp This section displays the expected output from running the `find_scale` example code, illustrating the calculated probabilities and the determined scale (standard deviation). ```text Example: Find scale (standard deviation). Normal distribution with mean = 0, standard deviation 1, has fraction <= -2, p = 0.0227501 Normal distribution with mean = 0, standard deviation 1, has fraction > -2, p = 0.97725 scale (standard deviation) = 0.647201 Normal distribution with mean = 0 has fraction <= -2, p = 0.001 Normal distribution with mean = 0 has fraction > -2, p = 0.999 Normal distribution with mean = 0.946339 has fraction <= -2 = 0.001 Normal distribution with mean = 0.946339 has fraction > -2 = 0.999 ``` -------------------------------- ### Diode Resistance Calculation Setup Source: https://www.boost.org/doc/libs/latest/libs/math/doc/html/math_toolkit/lambert_w.html Sets up variables and calculates initial values for a diode resistance model using the Lambert W function. This includes thermal voltage, saturation current, and initial current calculation. ```C++ double nu = 1.0; // Assumed ideal. double vt = v_thermal(25); // v thermal, Shockley equation, expect about 25 mV at room temperature. double boltzmann_k = 1.38e-23; // joules/kelvin double temp = 273 + 25; double charge_q = 1.6e-19; // column vt = boltzmann_k * temp / charge_q; std::cout << "V thermal " << vt << std::endl; // V thermal 0.0257025 = 25 mV double rsat = 0.; double isat = 25.e-15; // 25 fA; std::cout << "Isat = " << isat << std::endl; double re = 0.3; // Estimated from slope of straight section of graph (equation 6). double v = 0.9; double icalc = iv(v, vt, 249., re, isat); std::cout << "voltage = " << v << ", current = " << icalc << ", " << log(icalc) << std::endl; // voltage = 0.9, current = 0.00108485, -6.82631 ``` -------------------------------- ### C++ f_test Example: Ceramic Strength Data Source: https://www.boost.org/doc/libs/latest/libs/math/example/f_test.cpp Demonstrates the usage of the f_test function with ceramic strength data. This example is based on data from the NIST Engineering Statistics Handbook. ```cpp int main() { // // Run tests for ceramic strength data: // see http://www.itl.nist.gov/div898/handbook/eda/section4/eda42a1.htm // The data for this case study were collected by Said Jahanmir of the // NIST Ceramics Division in 1996 in connection with a NIST/industry // ceramics consortium for strength optimization of ceramic strength. // f_test(65.54909, 61.85425, 240, 240, 0.05); // // And again for the process change comparison: // see http://www.itl.nist.gov/div898/handbook/prc/section3/prc32.htm // A new procedure to assemble a device is introduced and tested for // possible improvement in time of assembly. The question being addressed // is whether the standard deviation of the new assembly process (sample 2) is // better (i.e., smaller) than the standard deviation for the old assembly // process (sample 1). // f_test(4.9082, 2.5874, 11, 9, 0.05); return 0; } ``` -------------------------------- ### Example of using Bernoulli Distribution with Binomial Source: https://www.boost.org/doc/libs/latest/libs/math/doc/html/math_toolkit/dist_ref/dists/bernoulli_dist.html Demonstrates how to use the binomial distribution to represent a Bernoulli distribution with a single trial. ```c++ binomial_distribution(1, 0.25) ``` -------------------------------- ### Complex Integration Example (Lambert-W function) Source: https://www.boost.org/doc/libs/latest/libs/math/doc/html/math_toolkit/gauss.html Demonstrates computing the Lambert-W function using complex-valued Gaussian quadrature. Requires `boost::math::quadrature::gauss` and `boost::math::constants::pi`. ```cpp Complex z{2, 3}; auto lw = [&z](Real v)->Complex { using std::cos; using std::sin; using std::exp; Real sinv = sin(v); Real cosv = cos(v); Real cotv = cosv/sinv; Real cscv = 1/sinv; Real t = (1-v*cotv)*(1-v*cotv) + v*v; Real x = v*cscv*exp(-v*cotv); Complex den = z + x; Complex num = t*(z/pi()); Complex res = num/den; return res; }; boost::math::quadrature::gauss integrator; Complex W = integrator.integrate(lw, (Real) 0, pi()); ``` -------------------------------- ### Beta Function Test Data Format Example Source: https://www.boost.org/doc/libs/latest/libs/math/doc/html/math_toolkit/internals/test_data.html An example of the generated test data format for the beta function, showing a C++ array definition with static casted constants. ```C++ #define SC_(x) static_cast(BOOST_JOIN(x, L)) static const std::array, 1830> beta_med_data = { SC_(0.4883005917072296142578125), SC_(0.4883005917072296142578125), SC_(3.245912809500479157065104747353807392371), SC_(3.5808107852935791015625), SC_(0.4883005917072296142578125), SC_(1.007653173802923954909901438393379243537), /* ... lots of rows skipped */ }; ``` -------------------------------- ### Include Binomial Distribution and I/O Source: https://www.boost.org/doc/libs/latest/libs/math/example/binomial_quiz_example.cpp Includes necessary headers for the binomial distribution and standard input/output operations. This sets up the environment for using the distribution. ```cpp #include using boost::math::binomial; #include using std::cout; using std::endl; using std::ios; using std::flush; using std::left; using std::right; using std::fixed; #include using std::setw; using std::setprecision; #include ``` -------------------------------- ### Minimal Engel Expansion Example Source: https://www.boost.org/doc/libs/latest/libs/math/doc/html/math_toolkit/internals/engel_expansion.html Demonstrates a minimal working example of the engel_expansion class using the mathematical constant 'e'. The output shows the resulting Engel series digits. ```cpp using boost::math::constants::e; using boost::math::tools::engel_expansion; auto engel = engel_expansion(e()); std::cout << "e ≈ " << engel << "\n"; // Prints: // e ≈ {1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16} ``` -------------------------------- ### Loopback Example: C99 Locale Output and Input Source: https://www.boost.org/doc/libs/latest/libs/math/example/nonfinite_facet_simple.cpp Demonstrates a loopback test by writing infinity to a stringstream using the C99 output locale and then reading it back using the C99 input locale. Verifies that the output string matches the input string. ```C++ // A 'loop-back example, output to a stringstream, and reading it back in. // Create C99 input and output locales. std::locale C99_out_locale (default_locale, new boost::math::nonfinite_num_put); std::locale C99_in_locale (default_locale, new boost::math::nonfinite_num_get); std::ostringstream oss; oss.imbue(C99_out_locale); oss << plus_infinity; std::istringstream iss(oss.str()); // So stream contains "inf". iss.imbue (C99_in_locale); std::string s; iss >> s; cout.imbue(C99_out_locale); if (oss.str() != s) { cout << plus_infinity << " != " << s << " loopback failed!" << endl; } else { cout << plus_infinity << " == " << s << " as expected." << endl; } ``` -------------------------------- ### Example Usage of Constant Generation Source: https://www.boost.org/doc/libs/latest/libs/math/tools/gauss_kronrod_constants.cpp Demonstrates how to call the `print_gauss_constants` and `print_gauss_kronrod_constants` functions with different types and precision settings to generate quadrature rules. ```C++ int main() { typedef boost::multiprecision::number > mp_type; print_gauss_constants >("f", 9, 0); print_gauss_constants >("", 17, 1); print_gauss_constants >("L", 35, 2); print_gauss_constants >("Q", 35, 3); print_gauss_constants >("", 115, 4); print_gauss_constants >("f", 9, 0); print_gauss_constants >("", 17, 1); print_gauss_constants >("L", 35, 2); print_gauss_constants >("Q", 35, 3); print_gauss_constants >("", 115, 4); print_gauss_constants >("f", 9, 0); print_gauss_constants >("", 17, 1); print_gauss_constants >("L", 35, 2); print_gauss_constants >("Q", 35, 3); print_gauss_constants >("", 115, 4); // Note: print_gauss_kronrod_constants is not called in this example, but would follow a similar pattern. return 0; } ``` -------------------------------- ### Associated Laguerre Polynomial Sequence Generation Example Source: https://www.boost.org/doc/libs/latest/libs/math/doc/html/math_toolkit/sf_poly/laguerre.html Example demonstrating how to use laguerre_next to generate a vector of the first 10 Associated Laguerre polynomial values for a given order m. ```cpp double x = 0.5; // Abscissa value int m = 10; // order vector v; v.push_back(laguerre(0, m, x)).push_back(laguerre(1, m, x)); for(unsigned l = 1; l < 10; ++l) v.push_back(laguerre_next(l, m, x, v[l], v[l-1])); ``` -------------------------------- ### nextafter Examples with 32-bit Float Source: https://www.boost.org/doc/libs/latest/libs/math/doc/html/math_toolkit/next_float/nextafter.html Demonstrates the behavior of nextafter with 32-bit floating-point numbers around unity and 0.1. ```c++ The nearest (exact) representation of 1.F is 1.00000000 nextafter(1.F, 999) is 1.00000012 nextafter(1/f, -999) is 0.99999994 The nearest (not exact) representation of 0.1F is 0.100000001 nextafter(0.1F, 10) is 0.100000009 nextafter(0.1F, 10) is 0.099999994 ``` -------------------------------- ### Example: Calculating Half Pi Source: https://www.boost.org/doc/libs/latest/libs/math/doc/html/math_toolkit/new_const.html An example demonstrating how to define the computation function for 'half_pi'. This function calculates pi divided by 2, ensuring accuracy by specifying precision using policies. ```C++ template template inline T constant_half_pi::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((std::integral_constant))) { BOOST_MATH_STD_USING return pi > >() / static_cast(2); } ``` -------------------------------- ### Include Headers and Using Declarations Source: https://www.boost.org/doc/libs/latest/libs/math/doc/html/math_toolkit/pol_tutorial/understand_dis_quant.html Includes necessary headers for input/output,iomanip, and the binomial distribution. It also brings specific components from boost::math and boost::math::policies into scope for conciseness. ```cpp #include using std::cout; using std::endl; using std::left; using std::fixed; using std::right; using std::scientific; #include using std::setw; using std::setprecision; #include ``` -------------------------------- ### Get Real and Unreal Components of an Octonion Source: https://www.boost.org/doc/libs/latest/libs/math/doc/html/math_toolkit/oct_value_ops.html Use `real()` to retrieve the real part and `unreal()` to get the unreal part of an octonion. These functions return the respective components of the octonion object. ```C++ template T real(octonion const & o); template octonion unreal(octonion const & o); ``` -------------------------------- ### Example of Constructing a Hyperexponential Distribution Source: https://www.boost.org/doc/libs/latest/libs/math/doc/html/math_toolkit/dist_ref/dists/hyperexponential_dist.html Demonstrates how to construct a hyperexponential distribution using phase probabilities and rates provided via std::array iterators. ```cpp std::array phase_prob = { 0.5, 0.5 }; std::array rates = { 1.0 / 10, 1.0 / 12 }; hyperexponential he(phase_prob.begin(), phase_prob.end(), rates.begin(), rates.end()); ```