VSLRandomStream =============== .. cpp:class:: numpy::VSLRandomStream numpy C++ class. Example ------- .. code-block:: cpp #include using namespace numpy; // Create random generator Generator gen(12345); // seed // Generate random values auto uniform = gen.random({3, 3}); auto normal = gen.standard_normal({10}); auto integers = gen.integers(0, 100, {5}); Constructors ------------ .. list-table:: :widths: 55 25 20 :header-rows: 1 * - Signature - Location - Example * - ``VSLRandomStream(uint32_tseed = 0)`` - NP_VSL_RANDOM.H:190 - * - ``VSLRandomStream(const VSLRandomStream &)`` - NP_VSL_RANDOM.H:213 - * - ``noexcept VSLRandomStream(VSLRandomStream &&other)`` - NP_VSL_RANDOM.H:216 - * - ``VSLRandomStream(uint32_tseed = 0)`` - NP_VSL_RANDOM.H:2026 - * - ``VSLRandomStream(const VSLRandomStream &)`` - NP_VSL_RANDOM.H:2033 - * - ``VSLRandomStream(VSLRandomStream &&)`` - NP_VSL_RANDOM.H:2035 - Operators --------- .. list-table:: :widths: 40 25 15 20 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``VSLRandomStream & operator=(const VSLRandomStream &)`` - VSLRandomStream & - NP_VSL_RANDOM.H:214 - * - ``VSLRandomStream & noexcept operator=(VSLRandomStream &&other)`` - VSLRandomStream & noexcept - NP_VSL_RANDOM.H:220 - * - ``VSLRandomStream & operator=(const VSLRandomStream &)`` - VSLRandomStream & - NP_VSL_RANDOM.H:2034 - * - ``VSLRandomStream & operator=(VSLRandomStream &&)`` - VSLRandomStream & - NP_VSL_RANDOM.H:2036 - Array Creation -------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``void triangular(T \*buffer, size_tn, Tleft, Tmode, Tright)`` - void - NP_VSL_RANDOM.H:2231 - :ref:`View ` Indexing / Selection -------------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``VSLStreamStatePtr get_stream()`` - VSLStreamStatePtr - NP_VSL_RANDOM.H:1085 - Math Operations --------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``void exponential(T \*buffer, size_tn, Tscale)`` - void - NP_VSL_RANDOM.H:621 - :ref:`View ` * - ``void exponential(T \*buffer, size_tn, Tscale)`` - void - NP_VSL_RANDOM.H:2146 - :ref:`View ` * - ``void exponential_f32(float \*buffer, size_tn, floatbeta)`` - void - NP_VSL_RANDOM.H:347 - * - ``void exponential_f32(float \*buffer, size_tn, floatbeta)`` - void - NP_VSL_RANDOM.H:2068 - * - ``void exponential_f64(double \*buffer, size_tn, doublebeta)`` - void - NP_VSL_RANDOM.H:361 - * - ``void exponential_f64(double \*buffer, size_tn, doublebeta)`` - void - NP_VSL_RANDOM.H:2072 - * - ``void lognormal(T \*buffer, size_tn, Tmean, Tsigma)`` - void - NP_VSL_RANDOM.H:709 - :ref:`View ` * - ``void lognormal(T \*buffer, size_tn, Tmean, Tsigma)`` - void - NP_VSL_RANDOM.H:2186 - :ref:`View ` * - ``void lognormal_f32(float \*buffer, size_tn, floatmean, floatsigma)`` - void - NP_VSL_RANDOM.H:584 - * - ``void lognormal_f32(float \*buffer, size_tn, floatmean, floatsigma)`` - void - NP_VSL_RANDOM.H:2132 - * - ``void lognormal_f64(double \*buffer, size_tn, doublemean, doublesigma)`` - void - NP_VSL_RANDOM.H:599 - * - ``void lognormal_f64(double \*buffer, size_tn, doublemean, doublesigma)`` - void - NP_VSL_RANDOM.H:2136 - * - ``void negative_binomial(T \*buffer, size_tn, doublea, doublep)`` - void - NP_VSL_RANDOM.H:885 - :ref:`View ` * - ``void negative_binomial(T \*buffer, size_tn, doublea, doublep)`` - void - NP_VSL_RANDOM.H:2206 - :ref:`View ` * - ``void negative_binomial_i32(int \*buffer, size_tn, doublea, doublep)`` - void - NP_VSL_RANDOM.H:862 - Linear Algebra -------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``void normal(T \*buffer, size_tn, Tloc, Tscale)`` - void - NP_VSL_RANDOM.H:610 - :ref:`View ` * - ``void normal(T \*buffer, size_tn, Tloc, Tscale)`` - void - NP_VSL_RANDOM.H:2141 - :ref:`View ` * - ``void normal_f32(float \*buffer, size_tn, floatloc, floatscale)`` - void - NP_VSL_RANDOM.H:318 - * - ``void normal_f32(float \*buffer, size_tn, floatloc, floatscale)`` - void - NP_VSL_RANDOM.H:2060 - * - ``void normal_f64(double \*buffer, size_tn, doubleloc, doublescale)`` - void - NP_VSL_RANDOM.H:333 - * - ``void normal_f64(double \*buffer, size_tn, doubleloc, doublescale)`` - void - NP_VSL_RANDOM.H:2064 - Random ------ .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``void beta(T \*buffer, size_tn, Talpha, Tbeta_param)`` - void - NP_VSL_RANDOM.H:643 - :ref:`View ` * - ``void beta(T \*buffer, size_tn, Talpha, Tbeta_param)`` - void - NP_VSL_RANDOM.H:2156 - :ref:`View ` * - ``void beta_f32(float \*buffer, size_tn, floatalpha, floatbeta)`` - void - NP_VSL_RANDOM.H:406 - * - ``void beta_f32(float \*buffer, size_tn, floatalpha, floatbeta)`` - void - NP_VSL_RANDOM.H:2084 - * - ``void beta_f64(double \*buffer, size_tn, doublealpha, doublebeta)`` - void - NP_VSL_RANDOM.H:421 - * - ``void beta_f64(double \*buffer, size_tn, doublealpha, doublebeta)`` - void - NP_VSL_RANDOM.H:2088 - * - ``void binomial(T \*buffer, size_tn, intntrial, doublep)`` - void - NP_VSL_RANDOM.H:750 - :ref:`View ` * - ``void binomial(T \*buffer, size_tn, intntrial, doublep)`` - void - NP_VSL_RANDOM.H:2191 - :ref:`View ` * - ``void binomial_i32(int \*buffer, size_tn, intntrial, doublep)`` - void - NP_VSL_RANDOM.H:727 - * - ``void chisquare(T \*buffer, size_tn, doubledf)`` - void - NP_VSL_RANDOM.H:993 - :ref:`View ` * - ``void chisquare(T \*buffer, size_tn, doubledf)`` - void - NP_VSL_RANDOM.H:2216 - :ref:`View ` * - ``void chisquare_f32(float \*buffer, size_tn, floatdf)`` - void - NP_VSL_RANDOM.H:955 - * - ``void chisquare_f64(double \*buffer, size_tn, doubledf)`` - void - NP_VSL_RANDOM.H:974 - * - ``void gamma(T \*buffer, size_tn, Talpha, Tbeta)`` - void - NP_VSL_RANDOM.H:632 - :ref:`View ` * - ``void gamma(T \*buffer, size_tn, Talpha, Tbeta)`` - void - NP_VSL_RANDOM.H:2151 - :ref:`View ` * - ``void gamma_f32(float \*buffer, size_tn, floatalpha, floatbeta)`` - void - NP_VSL_RANDOM.H:376 - * - ``void gamma_f32(float \*buffer, size_tn, floatalpha, floatbeta)`` - void - NP_VSL_RANDOM.H:2076 - * - ``void gamma_f64(double \*buffer, size_tn, doublealpha, doublebeta)`` - void - NP_VSL_RANDOM.H:391 - * - ``void gamma_f64(double \*buffer, size_tn, doublealpha, doublebeta)`` - void - NP_VSL_RANDOM.H:2080 - * - ``void poisson(T \*buffer, size_tn, doublelambda)`` - void - NP_VSL_RANDOM.H:791 - :ref:`View ` * - ``void poisson(T \*buffer, size_tn, doublelambda)`` - void - NP_VSL_RANDOM.H:2196 - :ref:`View ` * - ``void poisson_i32(int \*buffer, size_tn, doublelambda)`` - void - NP_VSL_RANDOM.H:772 - * - ``void seed(uint32_tseed)`` - void - NP_VSL_RANDOM.H:235 - :ref:`View ` * - ``void seed(uint32_tseed)`` - void - NP_VSL_RANDOM.H:2038 - :ref:`View ` * - ``void uniform(T \*buffer, size_tn, Tlow, Thigh)`` - void - NP_VSL_RANDOM.H:298 - :ref:`View ` * - ``void uniform(T \*buffer, size_tn, Tlow, Thigh)`` - void - NP_VSL_RANDOM.H:2054 - :ref:`View ` * - ``int uniform_bits32(uint32_t \*buffer, MKL_INTn)`` - int - NP_VSL_RANDOM.H:1065 - * - ``int uniform_bits64(uint64_t \*buffer, MKL_INTn)`` - int - NP_VSL_RANDOM.H:1076 - * - ``void uniform_f32(float \*buffer, size_tn, floatlow, floathigh)`` - void - NP_VSL_RANDOM.H:253 - * - ``void uniform_f32(float \*buffer, size_tn, floatlow, floathigh)`` - void - NP_VSL_RANDOM.H:2042 - * - ``void uniform_f64(double \*buffer, size_tn, doublelow, doublehigh)`` - void - NP_VSL_RANDOM.H:268 - :ref:`View ` * - ``void uniform_f64(double \*buffer, size_tn, doublelow, doublehigh)`` - void - NP_VSL_RANDOM.H:2046 - :ref:`View ` * - ``void uniform_i32(int \*buffer, size_tn, intlow, inthigh)`` - void - NP_VSL_RANDOM.H:283 - * - ``void uniform_i32(int \*buffer, size_tn, intlow, inthigh)`` - void - NP_VSL_RANDOM.H:2050 - Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``void cauchy(T \*buffer, size_tn, Tloc, Tscale)`` - void - NP_VSL_RANDOM.H:687 - :ref:`View ` * - ``void cauchy(T \*buffer, size_tn, Tloc, Tscale)`` - void - NP_VSL_RANDOM.H:2176 - :ref:`View ` * - ``void cauchy_f32(float \*buffer, size_tn, floatloc, floatscale)`` - void - NP_VSL_RANDOM.H:524 - * - ``void cauchy_f32(float \*buffer, size_tn, floatloc, floatscale)`` - void - NP_VSL_RANDOM.H:2116 - * - ``void cauchy_f64(double \*buffer, size_tn, doubleloc, doublescale)`` - void - NP_VSL_RANDOM.H:539 - * - ``void cauchy_f64(double \*buffer, size_tn, doubleloc, doublescale)`` - void - NP_VSL_RANDOM.H:2120 - * - ``void f_dist(T \*buffer, size_tn, doubledfnum, doubledfden)`` - void - NP_VSL_RANDOM.H:2226 - * - ``void geometric(T \*buffer, size_tn, doublep)`` - void - NP_VSL_RANDOM.H:839 - :ref:`View ` * - ``void geometric(T \*buffer, size_tn, doublep)`` - void - NP_VSL_RANDOM.H:2201 - :ref:`View ` * - ``void geometric_i32(int \*buffer, size_tn, doublep)`` - void - NP_VSL_RANDOM.H:813 - * - ``void gumbel(T \*buffer, size_tn, Tloc, Tscale)`` - void - NP_VSL_RANDOM.H:698 - :ref:`View ` * - ``void gumbel(T \*buffer, size_tn, Tloc, Tscale)`` - void - NP_VSL_RANDOM.H:2181 - :ref:`View ` * - ``void gumbel_f32(float \*buffer, size_tn, floatloc, floatscale)`` - void - NP_VSL_RANDOM.H:554 - * - ``void gumbel_f32(float \*buffer, size_tn, floatloc, floatscale)`` - void - NP_VSL_RANDOM.H:2124 - * - ``void gumbel_f64(double \*buffer, size_tn, doubleloc, doublescale)`` - void - NP_VSL_RANDOM.H:569 - * - ``void gumbel_f64(double \*buffer, size_tn, doubleloc, doublescale)`` - void - NP_VSL_RANDOM.H:2128 - * - ``void hypergeometric(T \*buffer, size_tn, intl, ints, intm)`` - void - NP_VSL_RANDOM.H:933 - :ref:`View ` * - ``void hypergeometric(T \*buffer, size_tn, intl, ints, intm)`` - void - NP_VSL_RANDOM.H:2211 - :ref:`View ` * - ``void hypergeometric_i32(int \*buffer, size_tn, intl, ints, intm)`` - void - NP_VSL_RANDOM.H:909 - * - ``void laplace(T \*buffer, size_tn, Tloc, Tscale)`` - void - NP_VSL_RANDOM.H:654 - :ref:`View ` * - ``void laplace(T \*buffer, size_tn, Tloc, Tscale)`` - void - NP_VSL_RANDOM.H:2161 - :ref:`View ` * - ``void laplace_f32(float \*buffer, size_tn, floatloc, floatscale)`` - void - NP_VSL_RANDOM.H:436 - * - ``void laplace_f32(float \*buffer, size_tn, floatloc, floatscale)`` - void - NP_VSL_RANDOM.H:2092 - * - ``void laplace_f64(double \*buffer, size_tn, doubleloc, doublescale)`` - void - NP_VSL_RANDOM.H:451 - * - ``void laplace_f64(double \*buffer, size_tn, doubleloc, doublescale)`` - void - NP_VSL_RANDOM.H:2096 - * - ``int multinomial(int \*buffer, MKL_INTn, intntrial, intk, const double \*p)`` - int - NP_VSL_RANDOM.H:1019 - :ref:`View ` * - ``int multivariate_normal(float \*buffer, MKL_INTn, intdimen, const float \*mean, const float \*chol_factor)`` - int - NP_VSL_RANDOM.H:1034 - :ref:`View ` * - ``int multivariate_normal(double \*buffer, MKL_INTn, intdimen, const double \*mean, const double \*chol_factor)`` - int - NP_VSL_RANDOM.H:1051 - :ref:`View ` * - ``void rayleigh(T \*buffer, size_tn, Tsigma)`` - void - NP_VSL_RANDOM.H:665 - :ref:`View ` * - ``void rayleigh(T \*buffer, size_tn, Tsigma)`` - void - NP_VSL_RANDOM.H:2166 - :ref:`View ` * - ``void rayleigh_f32(float \*buffer, size_tn, floatsigma)`` - void - NP_VSL_RANDOM.H:465 - * - ``void rayleigh_f32(float \*buffer, size_tn, floatsigma)`` - void - NP_VSL_RANDOM.H:2100 - * - ``void rayleigh_f64(double \*buffer, size_tn, doublesigma)`` - void - NP_VSL_RANDOM.H:479 - * - ``void rayleigh_f64(double \*buffer, size_tn, doublesigma)`` - void - NP_VSL_RANDOM.H:2104 - * - ``void student_t(T \*buffer, size_tn, doubledf)`` - void - NP_VSL_RANDOM.H:2221 - * - ``void weibull(T \*buffer, size_tn, Talpha, Tbeta)`` - void - NP_VSL_RANDOM.H:676 - :ref:`View ` * - ``void weibull(T \*buffer, size_tn, Talpha, Tbeta)`` - void - NP_VSL_RANDOM.H:2171 - :ref:`View ` * - ``void weibull_f32(float \*buffer, size_tn, floatalpha, floatbeta)`` - void - NP_VSL_RANDOM.H:494 - * - ``void weibull_f32(float \*buffer, size_tn, floatalpha, floatbeta)`` - void - NP_VSL_RANDOM.H:2108 - * - ``void weibull_f64(double \*buffer, size_tn, doublealpha, doublebeta)`` - void - NP_VSL_RANDOM.H:509 - * - ``void weibull_f64(double \*buffer, size_tn, doublealpha, doublebeta)`` - void - NP_VSL_RANDOM.H:2112 - Code Examples ------------- The following examples are extracted from the test suite. .. _example-vslrandomstream-triangular-0: .. dropdown:: triangular (np_test_1_all.cpp:22034) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22024 :emphasize-lines: 11 for (size_t i = 0; i < 3; ++i) { if (!(lognormal_arr.getElementAt({ i }) > 0.0)) { std::string description = std::string("testContinuousDistributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(lognormal_arr.getElementAt({ i }) > 0.0)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } } // std::cout << "[OK] Log-normal distribution\n"; // Test triangular distribution auto triangular_arr = triangular(0.0, 0.5, 1.0, { {3} }); for (size_t i = 0; i < 3; ++i) { double val = triangular_arr.getElementAt({ i }); if (!(val >= 0.0 && val <= 1.0)) { std::string description = std::string("testContinuousDistributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(val >= 0.0 && val <= 1.0)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } } // std::cout << "[OK] Triangular distribution\n"; .. _example-vslrandomstream-exponential-1: .. dropdown:: exponential (np_test_1_all.cpp:8006) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 7996 :emphasize-lines: 11 void testComplex256PrecisionComplex256() { std::cout << "========= testComplex256PrecisionComplex256 ======================="; // Test high precision calculations complex256 high_precision(1.234567890123456789012345L, 9.876543210987654321098765L); // std::cout << std::setprecision(20); // std::cout << "High precision complex: " << high_precision << std::endl; // Test complex exponential (Euler's formula: e^(ix) = cos(x) + i*sin(x)) complex256 i_pi(0.0L, 3.141592653589793238462643383279502884197L); // i*π auto euler = std::exp(i_pi); // std::cout << "e^(iπ) = " << euler << " (should be approximately -1)" << std::endl; // Test complex roots complex256 unity_root = std::exp(complex256(0.0L, 2.0L * 3.141592653589793238462643383279502884197L / 3.0L)); // std::cout << "Cube root of unity: " << unity_root << std::endl; // std::cout << "Cubed: " << unity_root * unity_root * unity_root << " (should be ≈ 1)" << std::endl; // std::cout << std::setprecision(6); // Reset precision .. _example-vslrandomstream-exponential-2: .. dropdown:: exponential (np_test_1_all.cpp:8006) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 7996 :emphasize-lines: 11 void testComplex256PrecisionComplex256() { std::cout << "========= testComplex256PrecisionComplex256 ======================="; // Test high precision calculations complex256 high_precision(1.234567890123456789012345L, 9.876543210987654321098765L); // std::cout << std::setprecision(20); // std::cout << "High precision complex: " << high_precision << std::endl; // Test complex exponential (Euler's formula: e^(ix) = cos(x) + i*sin(x)) complex256 i_pi(0.0L, 3.141592653589793238462643383279502884197L); // i*π auto euler = std::exp(i_pi); // std::cout << "e^(iπ) = " << euler << " (should be approximately -1)" << std::endl; // Test complex roots complex256 unity_root = std::exp(complex256(0.0L, 2.0L * 3.141592653589793238462643383279502884197L / 3.0L)); // std::cout << "Cube root of unity: " << unity_root << std::endl; // std::cout << "Cubed: " << unity_root * unity_root * unity_root << " (should be ≈ 1)" << std::endl; // std::cout << std::setprecision(6); // Reset precision .. _example-vslrandomstream-lognormal-3: .. dropdown:: lognormal (np_test_1_all.cpp:14134) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 14124 :emphasize-lines: 11 } // std::cout << "[OK] Beta distribution float precision\n"; std::cout << " -> tests passed\n"; } void test_vsl_additional_distributions() { std::cout << "========= test_vsl_additional_distributions ===="; // Test lognormal distribution auto lognorm_arr = lognormal(0.0, 1.0, { {100} }); for (size_t i = 0; i < 100; ++i) { double val = lognorm_arr.getElementAt({ i }); if (!(val > 0.0)) { std::string description = std::string("test_vsl_additional_distributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(val > 0.0)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(val < 100.0)) { std::string description = std::string("test_vsl_additional_distributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(val < 100.0)"; std::cout << std::string("[FAIL] ") + description << std::endl; .. _example-vslrandomstream-lognormal-4: .. dropdown:: lognormal (np_test_1_all.cpp:14134) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 14124 :emphasize-lines: 11 } // std::cout << "[OK] Beta distribution float precision\n"; std::cout << " -> tests passed\n"; } void test_vsl_additional_distributions() { std::cout << "========= test_vsl_additional_distributions ===="; // Test lognormal distribution auto lognorm_arr = lognormal(0.0, 1.0, { {100} }); for (size_t i = 0; i < 100; ++i) { double val = lognorm_arr.getElementAt({ i }); if (!(val > 0.0)) { std::string description = std::string("test_vsl_additional_distributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(val > 0.0)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(val < 100.0)) { std::string description = std::string("test_vsl_additional_distributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(val < 100.0)"; std::cout << std::string("[FAIL] ") + description << std::endl; .. _example-vslrandomstream-negative_binomial-5: .. dropdown:: negative_binomial (np_test_1_all.cpp:22127) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22117 :emphasize-lines: 11 int val = binom_arr.getElementAt({ i }); if (!(val >= 0 && val <= 20)) { std::string description = std::string("testDiscreteDistributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(val >= 0 && val <= 20)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } } // std::cout << "[OK] Binomial distribution\n"; // Test negative binomial distribution auto negbinom_arr = negative_binomial(10, 0.3, { {3} }); if (!(negbinom_arr.getShape()[0] == 3)) { std::string description = std::string("testDiscreteDistributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(negbinom_arr.getShape()[0] == 3)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } for (size_t i = 0; i < 3; ++i) { if (!(negbinom_arr.getElementAt({ i }) >= 0)) { std::string description = std::string("testDiscreteDistributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(negbinom_arr.getElementAt({ i }) >= 0)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); .. _example-vslrandomstream-negative_binomial-6: .. dropdown:: negative_binomial (np_test_1_all.cpp:22127) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22117 :emphasize-lines: 11 int val = binom_arr.getElementAt({ i }); if (!(val >= 0 && val <= 20)) { std::string description = std::string("testDiscreteDistributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(val >= 0 && val <= 20)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } } // std::cout << "[OK] Binomial distribution\n"; // Test negative binomial distribution auto negbinom_arr = negative_binomial(10, 0.3, { {3} }); if (!(negbinom_arr.getShape()[0] == 3)) { std::string description = std::string("testDiscreteDistributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(negbinom_arr.getShape()[0] == 3)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } for (size_t i = 0; i < 3; ++i) { if (!(negbinom_arr.getElementAt({ i }) >= 0)) { std::string description = std::string("testDiscreteDistributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(negbinom_arr.getElementAt({ i }) >= 0)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); .. _example-vslrandomstream-normal-7: .. dropdown:: normal (np_test_1_all.cpp:13866) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 13856 :emphasize-lines: 11 #include "../numpy/np_random.h" namespace numpy_tests { using namespace numpy::random; void test_vsl_normal_distribution() { std::cout << "========= test_vsl_normal_distribution ===="; // Test basic normal distribution auto norm_arr = normal(0.0, 1.0, { {100} }); if (!(norm_arr.getShape()[0] == 100)) { std::string description = std::string("test_vsl_normal_distribution():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(norm_arr.getShape()[0] == 100)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // Check that values are reasonable for standard normal double sum = 0.0, sum_sq = 0.0; for (size_t i = 0; i < 100; ++i) { double val = norm_arr.getElementAt({ i }); .. _example-vslrandomstream-normal-8: .. dropdown:: normal (np_test_1_all.cpp:13866) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 13856 :emphasize-lines: 11 #include "../numpy/np_random.h" namespace numpy_tests { using namespace numpy::random; void test_vsl_normal_distribution() { std::cout << "========= test_vsl_normal_distribution ===="; // Test basic normal distribution auto norm_arr = normal(0.0, 1.0, { {100} }); if (!(norm_arr.getShape()[0] == 100)) { std::string description = std::string("test_vsl_normal_distribution():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(norm_arr.getShape()[0] == 100)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // Check that values are reasonable for standard normal double sum = 0.0, sum_sq = 0.0; for (size_t i = 0; i < 100; ++i) { double val = norm_arr.getElementAt({ i }); .. _example-vslrandomstream-beta-9: .. dropdown:: beta (np_test_1_all.cpp:10394) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 10384 :emphasize-lines: 11 NDArray beta_a({ 4 }); NDArray beta_b({ 4 }); beta_a.setElementAt({ 0 }, 1.0); beta_a.setElementAt({ 1 }, 2.0); beta_a.setElementAt({ 2 }, 1.0); beta_a.setElementAt({ 3 }, 2.0); beta_b.setElementAt({ 0 }, 1.0); beta_b.setElementAt({ 1 }, 1.0); beta_b.setElementAt({ 2 }, 2.0); beta_b.setElementAt({ 3 }, 2.0); auto beta_result = special::beta(beta_a, beta_b, MathBackend::MKL); if (!(isApproxEqual(beta_result.getElementAt({ 0 }), 1.0, 1e-10))) { std::string description = std::string("testGammaFunctions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(isApproxEqual(beta_result.getElementAt({ 0 }), 1.0, 1e-10))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(isApproxEqual(beta_result.getElementAt({ 1 }), 0.5, 1e-10))) { std::string description = std::string("testGammaFunctions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(isApproxEqual(beta_result.getElementAt({ 1 }), 0.5, 1e-10))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); .. _example-vslrandomstream-beta-10: .. dropdown:: beta (np_test_1_all.cpp:10394) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 10384 :emphasize-lines: 11 NDArray beta_a({ 4 }); NDArray beta_b({ 4 }); beta_a.setElementAt({ 0 }, 1.0); beta_a.setElementAt({ 1 }, 2.0); beta_a.setElementAt({ 2 }, 1.0); beta_a.setElementAt({ 3 }, 2.0); beta_b.setElementAt({ 0 }, 1.0); beta_b.setElementAt({ 1 }, 1.0); beta_b.setElementAt({ 2 }, 2.0); beta_b.setElementAt({ 3 }, 2.0); auto beta_result = special::beta(beta_a, beta_b, MathBackend::MKL); if (!(isApproxEqual(beta_result.getElementAt({ 0 }), 1.0, 1e-10))) { std::string description = std::string("testGammaFunctions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(isApproxEqual(beta_result.getElementAt({ 0 }), 1.0, 1e-10))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(isApproxEqual(beta_result.getElementAt({ 1 }), 0.5, 1e-10))) { std::string description = std::string("testGammaFunctions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(isApproxEqual(beta_result.getElementAt({ 1 }), 0.5, 1e-10))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); .. _example-vslrandomstream-binomial-11: .. dropdown:: binomial (np_test_1_all.cpp:22110) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22100 :emphasize-lines: 11 for (size_t i = 0; i < 4; ++i) { if (!(geom_arr.getElementAt({ i }) >= 1)) { std::string description = std::string("testDiscreteDistributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(geom_arr.getElementAt({ i }) >= 1)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } } // std::cout << "[OK] Geometric distribution\n"; // Test binomial distribution auto binom_arr = binomial(20, 0.3, { {5} }); if (!(binom_arr.getShape()[0] == 5)) { std::string description = std::string("testDiscreteDistributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(binom_arr.getShape()[0] == 5)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } for (size_t i = 0; i < 5; ++i) { int val = binom_arr.getElementAt({ i }); if (!(val >= 0 && val <= 20)) { std::string description = std::string("testDiscreteDistributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(val >= 0 && val <= 20)"; std::cout << std::string("[FAIL] ") + description << std::endl; .. _example-vslrandomstream-binomial-12: .. dropdown:: binomial (np_test_1_all.cpp:22110) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22100 :emphasize-lines: 11 for (size_t i = 0; i < 4; ++i) { if (!(geom_arr.getElementAt({ i }) >= 1)) { std::string description = std::string("testDiscreteDistributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(geom_arr.getElementAt({ i }) >= 1)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } } // std::cout << "[OK] Geometric distribution\n"; // Test binomial distribution auto binom_arr = binomial(20, 0.3, { {5} }); if (!(binom_arr.getShape()[0] == 5)) { std::string description = std::string("testDiscreteDistributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(binom_arr.getShape()[0] == 5)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } for (size_t i = 0; i < 5; ++i) { int val = binom_arr.getElementAt({ i }); if (!(val >= 0 && val <= 20)) { std::string description = std::string("testDiscreteDistributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(val >= 0 && val <= 20)"; std::cout << std::string("[FAIL] ") + description << std::endl; .. _example-vslrandomstream-chisquare-13: .. dropdown:: chisquare (np_test_1_all.cpp:21974) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 21964 :emphasize-lines: 11 #include #include "../numpy/np_random.h" namespace numpy_tests { using namespace numpy::random; void testContinuousDistributions() { std::cout << "========= testContinuousDistributions ===="; // Test chi-square distribution auto chi2_arr = chisquare(3.0, { {2, 3} }); if (!(chi2_arr.getShape()[0] == 2)) { std::string description = std::string("testContinuousDistributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(chi2_arr.getShape()[0] == 2)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(chi2_arr.getShape()[1] == 3)) { std::string description = std::string("testContinuousDistributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(chi2_arr.getShape()[1] == 3)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } .. _example-vslrandomstream-chisquare-14: .. dropdown:: chisquare (np_test_1_all.cpp:21974) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 21964 :emphasize-lines: 11 #include #include "../numpy/np_random.h" namespace numpy_tests { using namespace numpy::random; void testContinuousDistributions() { std::cout << "========= testContinuousDistributions ===="; // Test chi-square distribution auto chi2_arr = chisquare(3.0, { {2, 3} }); if (!(chi2_arr.getShape()[0] == 2)) { std::string description = std::string("testContinuousDistributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(chi2_arr.getShape()[0] == 2)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(chi2_arr.getShape()[1] == 3)) { std::string description = std::string("testContinuousDistributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(chi2_arr.getShape()[1] == 3)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } .. _example-vslrandomstream-gamma-15: .. dropdown:: gamma (np_test_1_all.cpp:10297) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 10287 :emphasize-lines: 11 std::cout << "========= testGammaFunctions ===="; // Test gamma function for known values using NDArray with MKL backend NDArray gamma_test({ 6 }); gamma_test.setElementAt({ 0 }, 1.0); gamma_test.setElementAt({ 1 }, 2.0); gamma_test.setElementAt({ 2 }, 3.0); gamma_test.setElementAt({ 3 }, 4.0); gamma_test.setElementAt({ 4 }, 5.0); gamma_test.setElementAt({ 5 }, 0.5); auto gamma_result = special::gamma(gamma_test, MathBackend::MKL); if (!(isApproxEqual(gamma_result.getElementAt({ 0 }), 1.0, 1e-10))) { std::string description = std::string("testGammaFunctions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(isApproxEqual(gamma_result.getElementAt({ 0 }), 1.0, 1e-10))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(isApproxEqual(gamma_result.getElementAt({ 1 }), 1.0, 1e-10))) { std::string description = std::string("testGammaFunctions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(isApproxEqual(gamma_result.getElementAt({ 1 }), 1.0, 1e-10))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); .. _example-vslrandomstream-gamma-16: .. dropdown:: gamma (np_test_1_all.cpp:10297) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 10287 :emphasize-lines: 11 std::cout << "========= testGammaFunctions ===="; // Test gamma function for known values using NDArray with MKL backend NDArray gamma_test({ 6 }); gamma_test.setElementAt({ 0 }, 1.0); gamma_test.setElementAt({ 1 }, 2.0); gamma_test.setElementAt({ 2 }, 3.0); gamma_test.setElementAt({ 3 }, 4.0); gamma_test.setElementAt({ 4 }, 5.0); gamma_test.setElementAt({ 5 }, 0.5); auto gamma_result = special::gamma(gamma_test, MathBackend::MKL); if (!(isApproxEqual(gamma_result.getElementAt({ 0 }), 1.0, 1e-10))) { std::string description = std::string("testGammaFunctions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(isApproxEqual(gamma_result.getElementAt({ 0 }), 1.0, 1e-10))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(isApproxEqual(gamma_result.getElementAt({ 1 }), 1.0, 1e-10))) { std::string description = std::string("testGammaFunctions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(isApproxEqual(gamma_result.getElementAt({ 1 }), 1.0, 1e-10))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); .. _example-vslrandomstream-poisson-17: .. dropdown:: poisson (np_test_1_all.cpp:23404) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 23394 :emphasize-lines: 11 #include "../numpy/np_random.h" namespace numpy_tests { using namespace numpy::random; void np_test_poisson_basic_generation() { std::cout << "========= np_test_poisson_basic_generation ======================="; // Test poisson with default parameters (lambda=1.0, size=1) auto poisson_default = poisson(1.0); if (poisson_default.getSize() != 1) { throw std::runtime_error("Poisson default generation failed"); } // Test poisson with custom parameters auto poisson_custom = poisson(5.0, { {3, 2} }); if (poisson_custom.getShape() != std::vector({ 3, 2 })) { throw std::runtime_error("Poisson custom generation failed"); } .. _example-vslrandomstream-poisson-18: .. dropdown:: poisson (np_test_1_all.cpp:23404) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 23394 :emphasize-lines: 11 #include "../numpy/np_random.h" namespace numpy_tests { using namespace numpy::random; void np_test_poisson_basic_generation() { std::cout << "========= np_test_poisson_basic_generation ======================="; // Test poisson with default parameters (lambda=1.0, size=1) auto poisson_default = poisson(1.0); if (poisson_default.getSize() != 1) { throw std::runtime_error("Poisson default generation failed"); } // Test poisson with custom parameters auto poisson_custom = poisson(5.0, { {3, 2} }); if (poisson_custom.getShape() != std::vector({ 3, 2 })) { throw std::runtime_error("Poisson custom generation failed"); } .. _example-vslrandomstream-seed-19: .. dropdown:: seed (np_test_1_all.cpp:14339) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 14329 :emphasize-lines: 11 } // std::cout << "[OK] Generator integers method\n"; std::cout << " -> tests passed\n"; } void test_vsl_reproducibility() { std::cout << "========= test_vsl_reproducibility ===="; // Test seed reproducibility seed(42); auto arr1 = normal(0.0, 1.0, { {10} }); seed(42); auto arr2 = normal(0.0, 1.0, { {10} }); for (size_t i = 0; i < 10; ++i) { double val1 = arr1.getElementAt({ i }); double val2 = arr2.getElementAt({ i }); .. _example-vslrandomstream-seed-20: .. dropdown:: seed (np_test_1_all.cpp:14339) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 14329 :emphasize-lines: 11 } // std::cout << "[OK] Generator integers method\n"; std::cout << " -> tests passed\n"; } void test_vsl_reproducibility() { std::cout << "========= test_vsl_reproducibility ===="; // Test seed reproducibility seed(42); auto arr1 = normal(0.0, 1.0, { {10} }); seed(42); auto arr2 = normal(0.0, 1.0, { {10} }); for (size_t i = 0; i < 10; ++i) { double val1 = arr1.getElementAt({ i }); double val2 = arr2.getElementAt({ i }); .. _example-vslrandomstream-uniform-21: .. dropdown:: uniform (np_test_1_all.cpp:14301) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 14291 :emphasize-lines: 11 double val = beta_gen.getElementAt({ i }); if (!(val >= 0.0 && val <= 1.0)) { std::string description = std::string("test_vsl_generator_class():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(val >= 0.0 && val <= 1.0)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } } // std::cout << "[OK] Generator beta method\n"; // Test uniform and random methods auto unif_gen = gen.uniform(0.0, 10.0, { {50} }); auto rand_gen = gen.random({ {50} }); for (size_t i = 0; i < 50; ++i) { double unif_val = unif_gen.getElementAt({ i }); double rand_val = rand_gen.getElementAt({ i }); if (!(unif_val >= 0.0 && unif_val < 10.0)) { std::string description = std::string("test_vsl_generator_class():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(unif_val >= 0.0 && unif_val < 10.0)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } .. _example-vslrandomstream-uniform-22: .. dropdown:: uniform (np_test_1_all.cpp:14301) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 14291 :emphasize-lines: 11 double val = beta_gen.getElementAt({ i }); if (!(val >= 0.0 && val <= 1.0)) { std::string description = std::string("test_vsl_generator_class():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(val >= 0.0 && val <= 1.0)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } } // std::cout << "[OK] Generator beta method\n"; // Test uniform and random methods auto unif_gen = gen.uniform(0.0, 10.0, { {50} }); auto rand_gen = gen.random({ {50} }); for (size_t i = 0; i < 50; ++i) { double unif_val = unif_gen.getElementAt({ i }); double rand_val = rand_gen.getElementAt({ i }); if (!(unif_val >= 0.0 && unif_val < 10.0)) { std::string description = std::string("test_vsl_generator_class():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(unif_val >= 0.0 && unif_val < 10.0)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } .. _example-vslrandomstream-uniform_f64-23: .. dropdown:: uniform_f64 (np_test_1_all.cpp:14610) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 14600 :emphasize-lines: 11 void test_vsl_uniform_reproducibility() { std::cout << "========= test_vsl_uniform_reproducibility ===="; #ifdef NUMPY_USE_MKL if (numpy::vsl::is_vsl_available()) { // Test seed reproducibility with VSL streams numpy::vsl::VSLRandomStream stream1(12345); numpy::vsl::VSLRandomStream stream2(12345); std::vector buf1(100), buf2(100); stream1.uniform_f64(buf1.data(), 100, 0.0, 1.0); stream2.uniform_f64(buf2.data(), 100, 0.0, 1.0); for (size_t i = 0; i < 100; ++i) { if (!(std::abs(buf1[i] - buf2[i]) < 1e-15)) { std::string description = std::string("test_vsl_uniform_reproducibility():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(std::abs(buf1[i] - buf2[i]) < 1e-15)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } } // std::cout << "[OK] VSL stream reproducibility\n"; .. _example-vslrandomstream-uniform_f64-24: .. dropdown:: uniform_f64 (np_test_1_all.cpp:14610) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 14600 :emphasize-lines: 11 void test_vsl_uniform_reproducibility() { std::cout << "========= test_vsl_uniform_reproducibility ===="; #ifdef NUMPY_USE_MKL if (numpy::vsl::is_vsl_available()) { // Test seed reproducibility with VSL streams numpy::vsl::VSLRandomStream stream1(12345); numpy::vsl::VSLRandomStream stream2(12345); std::vector buf1(100), buf2(100); stream1.uniform_f64(buf1.data(), 100, 0.0, 1.0); stream2.uniform_f64(buf2.data(), 100, 0.0, 1.0); for (size_t i = 0; i < 100; ++i) { if (!(std::abs(buf1[i] - buf2[i]) < 1e-15)) { std::string description = std::string("test_vsl_uniform_reproducibility():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(std::abs(buf1[i] - buf2[i]) < 1e-15)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } } // std::cout << "[OK] VSL stream reproducibility\n"; .. _example-vslrandomstream-cauchy-25: .. dropdown:: cauchy (np_test_3_all.cpp:5774) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 5764 :emphasize-lines: 11 namespace numpy_tests { using namespace numpy::random; namespace test_cauchy_gumbel { void np_test_cauchy_gumbel_basic_generation() { std::cout << "========= np_test_cauchy_gumbel_basic_generation ======================="; // Test cauchy with default parameters auto cauchy_default = cauchy(); if (cauchy_default.getSize() != 1) { throw std::runtime_error("Cauchy default generation failed"); } // Test cauchy with custom parameters auto cauchy_custom = cauchy(2.0, 0.5, { {3, 2} }); if (cauchy_custom.getShape() != std::vector({ 3, 2 })) { throw std::runtime_error("Cauchy custom generation failed"); } .. _example-vslrandomstream-cauchy-26: .. dropdown:: cauchy (np_test_3_all.cpp:5774) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 5764 :emphasize-lines: 11 namespace numpy_tests { using namespace numpy::random; namespace test_cauchy_gumbel { void np_test_cauchy_gumbel_basic_generation() { std::cout << "========= np_test_cauchy_gumbel_basic_generation ======================="; // Test cauchy with default parameters auto cauchy_default = cauchy(); if (cauchy_default.getSize() != 1) { throw std::runtime_error("Cauchy default generation failed"); } // Test cauchy with custom parameters auto cauchy_custom = cauchy(2.0, 0.5, { {3, 2} }); if (cauchy_custom.getShape() != std::vector({ 3, 2 })) { throw std::runtime_error("Cauchy custom generation failed"); } .. _example-vslrandomstream-geometric-27: .. dropdown:: geometric (np_test_1_all.cpp:22094) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22084 :emphasize-lines: 11 } // std::cout << "[OK] Student's t-distribution\n"; std::cout << " -> tests passed\n"; } void testDiscreteDistributions() { std::cout << "========= testDiscreteDistributions ===="; // Test geometric distribution auto geom_arr = geometric(0.3, { {4} }); if (!(geom_arr.getShape()[0] == 4)) { std::string description = std::string("testDiscreteDistributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(geom_arr.getShape()[0] == 4)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } for (size_t i = 0; i < 4; ++i) { if (!(geom_arr.getElementAt({ i }) >= 1)) { std::string description = std::string("testDiscreteDistributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(geom_arr.getElementAt({ i }) >= 1)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); .. _example-vslrandomstream-geometric-28: .. dropdown:: geometric (np_test_1_all.cpp:22094) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22084 :emphasize-lines: 11 } // std::cout << "[OK] Student's t-distribution\n"; std::cout << " -> tests passed\n"; } void testDiscreteDistributions() { std::cout << "========= testDiscreteDistributions ===="; // Test geometric distribution auto geom_arr = geometric(0.3, { {4} }); if (!(geom_arr.getShape()[0] == 4)) { std::string description = std::string("testDiscreteDistributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(geom_arr.getShape()[0] == 4)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } for (size_t i = 0; i < 4; ++i) { if (!(geom_arr.getElementAt({ i }) >= 1)) { std::string description = std::string("testDiscreteDistributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(geom_arr.getElementAt({ i }) >= 1)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); .. _example-vslrandomstream-gumbel-29: .. dropdown:: gumbel (np_test_3_all.cpp:5786) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 5776 :emphasize-lines: 11 throw std::runtime_error("Cauchy default generation failed"); } // Test cauchy with custom parameters auto cauchy_custom = cauchy(2.0, 0.5, { {3, 2} }); if (cauchy_custom.getShape() != std::vector({ 3, 2 })) { throw std::runtime_error("Cauchy custom generation failed"); } // Test gumbel with default parameters auto gumbel_default = gumbel(); if (gumbel_default.getSize() != 1) { throw std::runtime_error("Gumbel default generation failed"); } // Test gumbel with custom parameters auto gumbel_custom = gumbel(1.0, 2.0, { {2, 3} }); if (gumbel_custom.getShape() != std::vector({ 2, 3 })) { throw std::runtime_error("Gumbel custom generation failed"); } .. _example-vslrandomstream-gumbel-30: .. dropdown:: gumbel (np_test_3_all.cpp:5786) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 5776 :emphasize-lines: 11 throw std::runtime_error("Cauchy default generation failed"); } // Test cauchy with custom parameters auto cauchy_custom = cauchy(2.0, 0.5, { {3, 2} }); if (cauchy_custom.getShape() != std::vector({ 3, 2 })) { throw std::runtime_error("Cauchy custom generation failed"); } // Test gumbel with default parameters auto gumbel_default = gumbel(); if (gumbel_default.getSize() != 1) { throw std::runtime_error("Gumbel default generation failed"); } // Test gumbel with custom parameters auto gumbel_custom = gumbel(1.0, 2.0, { {2, 3} }); if (gumbel_custom.getShape() != std::vector({ 2, 3 })) { throw std::runtime_error("Gumbel custom generation failed"); } .. _example-vslrandomstream-hypergeometric-31: .. dropdown:: hypergeometric (np_test_1_all.cpp:22143) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22133 :emphasize-lines: 11 for (size_t i = 0; i < 3; ++i) { if (!(negbinom_arr.getElementAt({ i }) >= 0)) { std::string description = std::string("testDiscreteDistributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(negbinom_arr.getElementAt({ i }) >= 0)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } } // std::cout << "[OK] Negative binomial distribution\n"; // Test hypergeometric distribution auto hypergeo_arr = hypergeometric(20, 30, 10, { {3} }); if (!(hypergeo_arr.getShape()[0] == 3)) { std::string description = std::string("testDiscreteDistributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(hypergeo_arr.getShape()[0] == 3)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } for (size_t i = 0; i < 3; ++i) { int val = hypergeo_arr.getElementAt({ i }); if (!(val >= 0 && val <= 10)) { std::string description = std::string("testDiscreteDistributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(val >= 0 && val <= 10)"; std::cout << std::string("[FAIL] ") + description << std::endl; .. _example-vslrandomstream-hypergeometric-32: .. dropdown:: hypergeometric (np_test_1_all.cpp:22143) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22133 :emphasize-lines: 11 for (size_t i = 0; i < 3; ++i) { if (!(negbinom_arr.getElementAt({ i }) >= 0)) { std::string description = std::string("testDiscreteDistributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(negbinom_arr.getElementAt({ i }) >= 0)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } } // std::cout << "[OK] Negative binomial distribution\n"; // Test hypergeometric distribution auto hypergeo_arr = hypergeometric(20, 30, 10, { {3} }); if (!(hypergeo_arr.getShape()[0] == 3)) { std::string description = std::string("testDiscreteDistributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(hypergeo_arr.getShape()[0] == 3)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } for (size_t i = 0; i < 3; ++i) { int val = hypergeo_arr.getElementAt({ i }); if (!(val >= 0 && val <= 10)) { std::string description = std::string("testDiscreteDistributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(val >= 0 && val <= 10)"; std::cout << std::string("[FAIL] ") + description << std::endl; .. _example-vslrandomstream-laplace-33: .. dropdown:: laplace (np_test_1_all.cpp:14151) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 14141 :emphasize-lines: 11 } if (!(val < 100.0)) { std::string description = std::string("test_vsl_additional_distributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(val < 100.0)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } } // std::cout << "[OK] Lognormal distribution test\n"; // Test Laplace distribution auto laplace_arr = laplace(0.0, 1.0, { {100} }); for (size_t i = 0; i < 100; ++i) { double val = laplace_arr.getElementAt({ i }); if (!(std::abs(val) < 10.0)) { std::string description = std::string("test_vsl_additional_distributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(std::abs(val) < 10.0)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } } // std::cout << "[OK] Laplace distribution test\n"; .. _example-vslrandomstream-laplace-34: .. dropdown:: laplace (np_test_1_all.cpp:14151) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 14141 :emphasize-lines: 11 } if (!(val < 100.0)) { std::string description = std::string("test_vsl_additional_distributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(val < 100.0)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } } // std::cout << "[OK] Lognormal distribution test\n"; // Test Laplace distribution auto laplace_arr = laplace(0.0, 1.0, { {100} }); for (size_t i = 0; i < 100; ++i) { double val = laplace_arr.getElementAt({ i }); if (!(std::abs(val) < 10.0)) { std::string description = std::string("test_vsl_additional_distributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(std::abs(val) < 10.0)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } } // std::cout << "[OK] Laplace distribution test\n"; .. _example-vslrandomstream-multinomial-35: .. dropdown:: multinomial (np_test_1_all.cpp:22161) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22151 :emphasize-lines: 11 if (!(val >= 0 && val <= 10)) { std::string description = std::string("testDiscreteDistributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(val >= 0 && val <= 10)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } } // std::cout << "[OK] Hypergeometric distribution\n"; // Test multinomial distribution std::vector pvals = { 0.2, 0.3, 0.5 }; auto multi_arr = multinomial(100, pvals, { {2} }); if (!(multi_arr.getShape()[0] == 2)) { std::string description = std::string("testDiscreteDistributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(multi_arr.getShape()[0] == 2)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(multi_arr.getShape()[1] == 3)) { std::string description = std::string("testDiscreteDistributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(multi_arr.getShape()[1] == 3)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } .. _example-vslrandomstream-multivariate_normal-36: .. dropdown:: multivariate_normal (np_test_1_all.cpp:22212) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22202 :emphasize-lines: 11 std::cout << " -> tests passed\n"; } void testMultivariateDistributions() { std::cout << "========= testMultivariateDistributions ===="; // Test multivariate normal distribution std::vector mean = { 0.0, 1.0 }; std::vector> cov = { {1.0, 0.5}, {0.5, 2.0} }; auto mvn_arr = multivariate_normal(mean, cov, { {3} }); if (!(mvn_arr.getShape()[0] == 3)) { std::string description = std::string("testMultivariateDistributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(mvn_arr.getShape()[0] == 3)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(mvn_arr.getShape()[1] == 2)) { std::string description = std::string("testMultivariateDistributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(mvn_arr.getShape()[1] == 2)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); .. _example-vslrandomstream-multivariate_normal-37: .. dropdown:: multivariate_normal (np_test_1_all.cpp:22212) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22202 :emphasize-lines: 11 std::cout << " -> tests passed\n"; } void testMultivariateDistributions() { std::cout << "========= testMultivariateDistributions ===="; // Test multivariate normal distribution std::vector mean = { 0.0, 1.0 }; std::vector> cov = { {1.0, 0.5}, {0.5, 2.0} }; auto mvn_arr = multivariate_normal(mean, cov, { {3} }); if (!(mvn_arr.getShape()[0] == 3)) { std::string description = std::string("testMultivariateDistributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(mvn_arr.getShape()[0] == 3)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(mvn_arr.getShape()[1] == 2)) { std::string description = std::string("testMultivariateDistributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(mvn_arr.getShape()[1] == 2)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); .. _example-vslrandomstream-rayleigh-38: .. dropdown:: rayleigh (np_test_1_all.cpp:14163) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 14153 :emphasize-lines: 11 double val = laplace_arr.getElementAt({ i }); if (!(std::abs(val) < 10.0)) { std::string description = std::string("test_vsl_additional_distributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(std::abs(val) < 10.0)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } } // std::cout << "[OK] Laplace distribution test\n"; // Test Rayleigh distribution auto rayleigh_arr = rayleigh(1.0, { {100} }); for (size_t i = 0; i < 100; ++i) { double val = rayleigh_arr.getElementAt({ i }); if (!(val >= 0.0)) { std::string description = std::string("test_vsl_additional_distributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(val >= 0.0)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(val < 10.0)) { std::string description = std::string("test_vsl_additional_distributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(val < 10.0)"; std::cout << std::string("[FAIL] ") + description << std::endl; .. _example-vslrandomstream-rayleigh-39: .. dropdown:: rayleigh (np_test_1_all.cpp:14163) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 14153 :emphasize-lines: 11 double val = laplace_arr.getElementAt({ i }); if (!(std::abs(val) < 10.0)) { std::string description = std::string("test_vsl_additional_distributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(std::abs(val) < 10.0)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } } // std::cout << "[OK] Laplace distribution test\n"; // Test Rayleigh distribution auto rayleigh_arr = rayleigh(1.0, { {100} }); for (size_t i = 0; i < 100; ++i) { double val = rayleigh_arr.getElementAt({ i }); if (!(val >= 0.0)) { std::string description = std::string("test_vsl_additional_distributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(val >= 0.0)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(val < 10.0)) { std::string description = std::string("test_vsl_additional_distributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(val < 10.0)"; std::cout << std::string("[FAIL] ") + description << std::endl; .. _example-vslrandomstream-weibull-40: .. dropdown:: weibull (np_test_1_all.cpp:14180) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 14170 :emphasize-lines: 11 } if (!(val < 10.0)) { std::string description = std::string("test_vsl_additional_distributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(val < 10.0)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } } // std::cout << "[OK] Rayleigh distribution test\n"; // Test Weibull distribution auto weibull_arr = weibull(2.0, { {100} }); for (size_t i = 0; i < 100; ++i) { double val = weibull_arr.getElementAt({ i }); if (!(val >= 0.0)) { std::string description = std::string("test_vsl_additional_distributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(val >= 0.0)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(val < 10.0)) { std::string description = std::string("test_vsl_additional_distributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(val < 10.0)"; std::cout << std::string("[FAIL] ") + description << std::endl; .. _example-vslrandomstream-weibull-41: .. dropdown:: weibull (np_test_1_all.cpp:14180) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 14170 :emphasize-lines: 11 } if (!(val < 10.0)) { std::string description = std::string("test_vsl_additional_distributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(val < 10.0)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } } // std::cout << "[OK] Rayleigh distribution test\n"; // Test Weibull distribution auto weibull_arr = weibull(2.0, { {100} }); for (size_t i = 0; i < 100; ++i) { double val = weibull_arr.getElementAt({ i }); if (!(val >= 0.0)) { std::string description = std::string("test_vsl_additional_distributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(val >= 0.0)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(val < 10.0)) { std::string description = std::string("test_vsl_additional_distributions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(val < 10.0)"; std::cout << std::string("[FAIL] ") + description << std::endl;