NumCpp  2.4.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
pnr.hpp
Go to the documentation of this file.
1 #pragma once
29 
30 #include "NumCpp/Core/Types.hpp"
31 #include "NumCpp/NdArray.hpp"
33 
34 #ifndef NO_USE_BOOST
35 #include "boost/math/special_functions/factorials.hpp"
36 #endif
37 
38 namespace nc
39 {
40  namespace special
41  {
42  //============================================================================
43  // Method Description:
51  inline double pnr(uint32 n, uint32 r)
52  {
53  if (r > n)
54  {
55  return 0.0;
56  }
57  else if (r == n)
58  {
59  return factorial(n);
60  }
61 
62  double combinations = 1.0;
63 
64 #ifndef NO_USE_BOOST
65  if (n <= boost::math::max_factorial<double>::value)
66  {
67  const double nFactorial = factorial(n);
68  const double nMinusRFactoral = factorial(n - r);
69 
70  combinations = nFactorial / nMinusRFactoral;
71  }
72  else
73  {
74 #endif
75  const uint32 lower = n - r + 1;
76  combinations = static_cast<double>(lower);
77  for (uint32 i = lower + 1; i <= n; ++i)
78  {
79  combinations *= static_cast<double>(i);
80  }
81 #ifndef NO_USE_BOOST
82  }
83 #endif
84 
85  return combinations;
86  }
87  } // namespace special
88 } // namespace nc
nc::uint32
std::uint32_t uint32
Definition: Types.hpp:40
NdArray.hpp
nc
Definition: Coordinate.hpp:44
factorial.hpp
nc::special::factorial
double factorial(uint32 inValue)
Definition: factorial.hpp:53
Types.hpp
nc::special::pnr
double pnr(uint32 n, uint32 r)
Definition: pnr.hpp:51