NumCpp  2.11.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 NUMCPP_NO_USE_BOOST
35 #include "boost/math/special_functions/factorials.hpp"
36 #endif
37 
38 namespace nc::special
39 {
40  //============================================================================
41  // Method Description:
48  inline double pnr(uint32 n, uint32 r)
49  {
50  if (r > n)
51  {
52  return 0.;
53  }
54  else if (r == n)
55  {
56  return factorial(n);
57  }
58 
59  double combinations = 1.;
60 
61 #ifndef NUMCPP_NO_USE_BOOST
62  if (n <= boost::math::max_factorial<double>::value)
63  {
64  const double nFactorial = factorial(n);
65  const double nMinusRFactoral = factorial(n - r);
66 
67  combinations = nFactorial / nMinusRFactoral;
68  }
69  else
70  {
71 #endif
72  const uint32 lower = n - r + 1;
73  combinations = static_cast<double>(lower);
74  for (uint32 i = lower + 1; i <= n; ++i)
75  {
76  combinations *= static_cast<double>(i);
77  }
78 #ifndef NUMCPP_NO_USE_BOOST
79  }
80 #endif
81 
82  return combinations;
83  }
84 } // namespace nc::special
Definition: airy_ai.hpp:39
double factorial(uint32 inValue)
Definition: factorial.hpp:49
double pnr(uint32 n, uint32 r)
Definition: pnr.hpp:48
std::uint32_t uint32
Definition: Types.hpp:40