NumCpp  2.9.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
legendre_p.hpp
Go to the documentation of this file.
1 #pragma once
29 
30 #include <cmath>
31 
32 #if defined(__cpp_lib_math_special_functions) || !defined(NUMCPP_NO_USE_BOOST)
33 
37 #include "NumCpp/NdArray.hpp"
38 
39 #ifndef __cpp_lib_math_special_functions
40 #include "boost/math/special_functions/legendre.hpp"
41 #endif
42 
43 namespace nc
44 {
45  namespace polynomial
46  {
47  //============================================================================
48  // Method Description:
57  template<typename dtype>
58  double legendre_p(uint32 n, dtype x)
59  {
61 
62  if (x < -1. || x > 1.)
63  {
64  THROW_INVALID_ARGUMENT_ERROR("input x must be of the range [-1, 1].");
65  }
66 
67 #ifdef __cpp_lib_math_special_functions
68  return std::legendre(n, static_cast<double>(x));
69 #else
70  return boost::math::legendre_p(n, static_cast<double>(x));
71 #endif
72  }
73 
74  //============================================================================
75  // Method Description:
85  template<typename dtype>
86  double legendre_p(uint32 m, uint32 n, dtype x)
87  {
89 
90  if (x < -1. || x > 1.)
91  {
92  THROW_INVALID_ARGUMENT_ERROR("input x must be of the range [-1, 1].");
93  }
94 
95 #ifdef __cpp_lib_math_special_functions
96 
97  auto value = std::assoc_legendre(n, m, static_cast<double>(x));
98 
99 #ifdef __GNUC__
100 #if __GNUC__ != 7 && __GNUC__ != 8
101 
102  // gcc std::assoc_legendre is not standard compliant
103  value *= n % 2 == 0 ? 1 : -1;
104 
105 #endif // #if __GNUC__ != 7 && __GNUC__ != 8
106 #endif // #ifdef __GNUC__
107 
108 #ifdef __clang__
109 #if __clang_major__ != 7 && __clang_major__ != 8
110 
111  // clang uses gcc headers where std::assoc_legendre is not standard compliant
112  value *= n % 2 == 0 ? 1 : -1;
113 
114 #endif // #if __clang_major__ != 6 && __clang_major__ != 7 && __clang_major__ != 8
115 #endif // #ifdef __clang__
116 
117 #ifdef _MSC_VER
118 
119  value *= n % 2 == 0 ? 1 : -1;
120 
121 #endif // #ifdef _MSC_VER
122 
123  return value;
124 
125 #else // #ifdef __cpp_lib_math_special_functions
126 
127  return boost::math::legendre_p(n, m, static_cast<double>(x));
128 
129 #endif // #ifdef __cpp_lib_math_special_functions
130  }
131 
132  //============================================================================
133  // Method Description:
142  template<typename dtype>
144  {
145  NdArray<double> returnArray(inArrayX.shape());
146 
147  const auto function = [n](dtype x) -> double { return legendre_p(n, x); };
148 
149  stl_algorithms::transform(inArrayX.cbegin(), inArrayX.cend(), returnArray.begin(), function);
150 
151  return returnArray;
152  }
153 
154  //============================================================================
155  // Method Description:
165  template<typename dtype>
167  {
168  NdArray<double> returnArray(inArrayX.shape());
169 
170  const auto function = [m, n](dtype x) -> double { return legendre_p(m, n, x); };
171 
172  stl_algorithms::transform(inArrayX.cbegin(), inArrayX.cend(), returnArray.begin(), function);
173 
174  return returnArray;
175  }
176  } // namespace polynomial
177 } // namespace nc
178 
179 #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
const_iterator cbegin() const noexcept
Definition: NdArrayCore.hpp:1221
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4092
const_iterator cend() const noexcept
Definition: NdArrayCore.hpp:1529
iterator begin() noexcept
Definition: NdArrayCore.hpp:1171
double legendre_p(uint32 n, dtype x)
Definition: legendre_p.hpp:58
NdArray< double > legendre_p(uint32 m, uint32 n, const NdArray< dtype > &inArrayX)
Definition: legendre_p.hpp:166
OutputIt transform(InputIt first, InputIt last, OutputIt destination, UnaryOperation unaryFunction)
Definition: StlAlgorithms.hpp:784
Definition: Coordinate.hpp:45
std::uint32_t uint32
Definition: Types.hpp:40