NumCpp  2.5.1
A Templatized Header Only C++ Implementation of the Python NumPy Library
factorial.hpp
Go to the documentation of this file.
1 #pragma once
29 
31 #include "NumCpp/Core/Types.hpp"
32 #include "NumCpp/NdArray.hpp"
33 
34 #ifndef NUMCPP_NO_USE_BOOST
35 #include "boost/math/special_functions/factorials.hpp"
36 #endif
37 
38 #include <limits>
39 
40 namespace nc
41 {
42  namespace special
43  {
44  //============================================================================
45  // Method Description:
53  inline double factorial(uint32 inValue)
54  {
55 #ifndef NUMCPP_NO_USE_BOOST
56  if (inValue <= boost::math::max_factorial<double>::value)
57  {
58  return boost::math::factorial<double>(inValue);
59  }
60 
61  return std::numeric_limits<double>::infinity();
62 #else
63  double result = 1.0;
64  for (uint32 i = 2; i <= inValue; ++i)
65  {
66  result *= static_cast<double>(i);
67  }
68 
69  return result;
70 #endif
71  }
72 
73  //============================================================================
74  // Method Description:
82  inline NdArray<double> factorial(const NdArray<uint32>& inArray)
83  {
84  NdArray<double> returnArray(inArray.shape());
85 
86  stl_algorithms::transform(inArray.cbegin(), inArray.cend(), returnArray.begin(),
87  [](uint32 inValue) -> double
88  {
89  return factorial(inValue);
90  });
91 
92  return returnArray;
93  }
94  } // namespace special
95 } // namespace nc
const_iterator cbegin() const noexcept
Definition: NdArrayCore.hpp:1270
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4483
const_iterator cend() const noexcept
Definition: NdArrayCore.hpp:1614
iterator begin() noexcept
Definition: NdArrayCore.hpp:1214
double factorial(uint32 inValue)
Definition: factorial.hpp:53
OutputIt transform(InputIt first, InputIt last, OutputIt destination, UnaryOperation unaryFunction)
Definition: StlAlgorithms.hpp:702
Definition: Coordinate.hpp:45
std::uint32_t uint32
Definition: Types.hpp:40