NumCpp  2.6.2
A Templatized Header Only C++ Implementation of the Python NumPy Library
legendre_p.hpp
Go to the documentation of this file.
1 
28 #pragma once
29 
30 #if defined(__cpp_lib_math_special_functions) || !defined(NUMCPP_NO_USE_BOOST)
31 
35 #include "NumCpp/NdArray.hpp"
36 
37 #ifdef __cpp_lib_math_special_functions
38 #include <cmath>
39 #else
40 #include "boost/math/special_functions/legendre.hpp"
41 #endif
42 
43 namespace nc
44 {
45  namespace polynomial
46  {
47  //============================================================================
48  // Method Description:
58  template<typename dtype>
59  double legendre_p(uint32 n, dtype x)
60  {
62 
63  if (x < -1.0 || x > 1.0 )
64  {
65  THROW_INVALID_ARGUMENT_ERROR("input x must be of the range [-1, 1].");
66  }
67 
68 #ifdef __cpp_lib_math_special_functions
69  return std::legendre(n, static_cast<double>(x));
70 #else
71  return boost::math::legendre_p(n, static_cast<double>(x));
72 #endif
73  }
74 
75  //============================================================================
76  // Method Description:
87  template<typename dtype>
88  double legendre_p(uint32 m, uint32 n, dtype x)
89  {
91 
92  if (x < -1.0 || x > 1.0 )
93  {
94  THROW_INVALID_ARGUMENT_ERROR("input x must be of the range [-1, 1].");
95  }
96 
97 #ifdef __cpp_lib_math_special_functions
98 
99  auto value = std::assoc_legendre(n, m, static_cast<double>(x));
100 
101 #ifdef __GNUC__
102 #if __GNUC__ != 7 && __GNUC__ != 8
103 
104  // gcc std::assoc_legendre is not standard compliant
105  value *= n % 2 == 0 ? 1 : -1;
106 
107 #endif // #if __GNUC__ != 7 && __GNUC__ != 8
108 #endif // #ifdef __GNUC__
109 
110 #ifdef __clang__
111 #if __clang_major__ != 7 && __clang_major__ != 8
112 
113  // clang uses gcc headers where std::assoc_legendre is not standard compliant
114  value *= n % 2 == 0 ? 1 : -1;
115 
116 #endif // #if __clang_major__ != 6 && __clang_major__ != 7 && __clang_major__ != 8
117 #endif // #ifdef __clang__
118 
119 #ifdef _MSC_VER
120 
121  value *= n % 2 == 0 ? 1 : -1;
122 
123 #endif // #ifdef _MSC_VER
124 
125  return value;
126 
127 #else // #ifdef __cpp_lib_math_special_functions
128 
129  return boost::math::legendre_p(n, m, static_cast<double>(x));
130 
131 #endif // #ifdef __cpp_lib_math_special_functions
132  }
133 
134  //============================================================================
135  // Method Description:
145  template<typename dtype>
147  {
148  NdArray<double> returnArray(inArrayX.shape());
149 
150  const auto function = [n](dtype x) -> double
151  {
152  return legendre_p(n, x);
153  };
154 
155  stl_algorithms::transform(inArrayX.cbegin(), inArrayX.cend(), returnArray.begin(), function);
156 
157  return returnArray;
158  }
159 
160  //============================================================================
161  // Method Description:
172  template<typename dtype>
174  {
175  NdArray<double> returnArray(inArrayX.shape());
176 
177  const auto function = [m, n](dtype x) -> double
178  {
179  return legendre_p(m, n, x);
180  };
181 
182  stl_algorithms::transform(inArrayX.cbegin(), inArrayX.cend(), returnArray.begin(), function);
183 
184  return returnArray;
185  }
186  } // namespace polynomial
187 } // namespace nc
188 
189 #endif // #if defined(__cpp_lib_math_special_functions) || !defined(NUMCPP_NO_USE_BOOST)
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
#define STATIC_ASSERT_ARITHMETIC(dtype)
Definition: StaticAsserts.hpp:37
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition: NdArrayCore.hpp:72
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 legendre_p(uint32 n, dtype x)
Definition: legendre_p.hpp:59
NdArray< double > legendre_p(uint32 m, uint32 n, const NdArray< dtype > &inArrayX)
Definition: legendre_p.hpp:173
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