Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 09:25: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 "ActsFatras/Utilities/detail/FpeSafeGammaDistribution.hpp"
0012 #include "ActsTests/CommonHelpers/FloatComparisons.hpp"
0013 
0014 #include <limits>
0015 #include <random>
0016 
0017 namespace ActsTests {
0018 
0019 BOOST_AUTO_TEST_SUITE(UtilitiesSuite)
0020 
0021 // Test if the standard-library gamma distribution gives the same results as
0022 // underflow-safe  FpeSafeGammaDistribution
0023 BOOST_AUTO_TEST_CASE(FpeSafeGammaDistributionSequence) {
0024   std::mt19937 rnd{30059};
0025 
0026   const int num = 20;
0027 #ifdef __GLIBCXX__
0028   // results from std::gamma_distribution<double>, in libstdc++
0029   double results[num] = {1.4631785,
0030                          4.3811862e-01,
0031                          2.1447861e-01,
0032                          1.1303697e-01,
0033                          3.5990972e-06,
0034                          6.6019759e-09,
0035                          2.2688236e-15,
0036                          2.3389734e-11,
0037                          3.1843972e-02,
0038                          2.8021629e-85,
0039                          3.1397155e-185,
0040                          3.1850069e-284,
0041                          0.,
0042                          2.2658164e-53,
0043                          0.,
0044                          0.,
0045                          0.,
0046                          0.,
0047                          0.,
0048                          0.};
0049 #elif defined _LIBCPP_VERSION
0050   // results from std::gamma_distribution<double>, in libc++
0051   double results[num] = {3.3655543,
0052                          6.7167479e-01,
0053                          1.3436782,
0054                          8.9406670e-04,
0055                          7.4754048e-05,
0056                          9.8973516e-20,
0057                          2.9430952e-08,
0058                          8.4810760e-07,
0059                          7.2823453e-41,
0060                          0.,
0061                          0.,
0062                          3.0934012e-14,
0063                          0.,
0064                          0.,
0065                          0.,
0066                          0.,
0067                          0.,
0068                          0.,
0069                          0.,
0070                          0.};
0071 #else
0072   // unknown library, will fail
0073   double results[num];
0074   for (int i = 0; i < num; ++i) {
0075     results[i] = -1.0;
0076   }
0077 #endif
0078 
0079   double alpha = 3.0;
0080   for (int i = 0; i < num; ++i) {
0081     alpha /= 2.0;
0082     ActsFatras::detail::FpeSafeGammaDistribution gDist(alpha, 1.);
0083     const auto u = gDist(rnd);
0084     CHECK_CLOSE_OR_SMALL(u, results[i], 1e-6,
0085                          std::numeric_limits<double>::min());
0086   }
0087 }
0088 
0089 BOOST_AUTO_TEST_SUITE_END()
0090 
0091 }  // namespace ActsTests