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