Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-06-21 08:10:30

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #include <boost/test/unit_test.hpp>
0010 
0011 #include "Acts/Tests/CommonHelpers/FloatComparisons.hpp"
0012 #include "ActsFatras/Utilities/detail/FpeSafeGammaDistribution.hpp"
0013 
0014 #include <limits>
0015 #include <random>
0016 
0017 BOOST_AUTO_TEST_SUITE(FatrasFpeSafeGammaDistribution)
0018 
0019 // Test if the standard-library gamma distribution gives the same results as
0020 // underflow-safe  FpeSafeGammaDistribution
0021 BOOST_AUTO_TEST_CASE(FpeSafeGammaDistributionSequence) {
0022   std::mt19937 rnd{30059};
0023 
0024   const int num = 20;
0025 #ifdef __GLIBCXX__
0026   // results from std::gamma_distribution<double>, in libstdc++
0027   double results[num] = {1.4631785,
0028                          4.3811862e-01,
0029                          2.1447861e-01,
0030                          1.1303697e-01,
0031                          3.5990972e-06,
0032                          6.6019759e-09,
0033                          2.2688236e-15,
0034                          2.3389734e-11,
0035                          3.1843972e-02,
0036                          2.8021629e-85,
0037                          3.1397155e-185,
0038                          3.1850069e-284,
0039                          0.,
0040                          2.2658164e-53,
0041                          0.,
0042                          0.,
0043                          0.,
0044                          0.,
0045                          0.,
0046                          0.};
0047 #elif defined _LIBCPP_VERSION
0048   // results from std::gamma_distribution<double>, in libc++
0049   double results[num] = {3.3655543,
0050                          6.7167479e-01,
0051                          1.3436782,
0052                          8.9406670e-04,
0053                          7.4754048e-05,
0054                          9.8973516e-20,
0055                          2.9430952e-08,
0056                          8.4810760e-07,
0057                          7.2823453e-41,
0058                          0.,
0059                          0.,
0060                          3.0934012e-14,
0061                          0.,
0062                          0.,
0063                          0.,
0064                          0.,
0065                          0.,
0066                          0.,
0067                          0.,
0068                          0.};
0069 #else
0070   // unknown library, will fail
0071   double results[num];
0072   for (int i = 0; i < num; ++i) {
0073     results[i] = -1.0;
0074   }
0075 #endif
0076 
0077   double alpha = 3.0;
0078   for (int i = 0; i < num; ++i) {
0079     alpha /= 2.0;
0080     ActsFatras::detail::FpeSafeGammaDistribution gDist(alpha, 1.);
0081     const auto u = gDist(rnd);
0082     CHECK_CLOSE_OR_SMALL(u, results[i], 1e-6,
0083                          std::numeric_limits<double>::min());
0084   }
0085 }
0086 
0087 BOOST_AUTO_TEST_SUITE_END()