mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
legendre.hpp
Go to the documentation of this file.
1 /** \file legendre.hpp
2  * \brief Declares and defines Legendre polynomials.
3  * \ingroup gen_math_files
4  * \author Jared R. Males (jaredmales@gmail.com)
5  *
6  */
7 
8 //***********************************************************************//
9 // Copyright 2021 Jared R. Males (jaredmales@gmail.com)
10 //
11 // This file is part of mxlib.
12 //
13 // mxlib is free software: you can redistribute it and/or modify
14 // it under the terms of the GNU General Public License as published by
15 // the Free Software Foundation, either version 3 of the License, or
16 // (at your option) any later version.
17 //
18 // mxlib is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU General Public License for more details.
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with mxlib. If not, see <http://www.gnu.org/licenses/>.
25 //***********************************************************************//
26 
27 #ifndef mx_legendre_hpp
28 #define mx_legendre_hpp
29 
30 #include <type_traits>
31 
32 #ifdef MX_INCLUDE_BOOST
33 #include <boost/math/special_functions/legendre.hpp>
34 #endif
35 
36 namespace mx
37 {
38 namespace math
39 {
40 namespace func
41 {
42 
43 /// Legendre Polynomials
44 /** See https://en.wikipedia.org/wiki/Legendre_polynomials
45  * and https://www.boost.org/doc/libs/1_75_0/libs/math/doc/html/math_toolkit/sf_poly/legendre.html
46  *
47  * \returns the value of the n-th Legendre polynomial at x.
48  *
49  * \ingroup functions
50  */
51 template<typename T>
52 T legendre_p( int n, ///< [in] the order of the Legendre polynomial, n>=0.
53  T x ///< [in] the argument, -1 <= x <= 1.
54  )
55 {
56 #ifdef MX_INCLUDE_BOOST
57  return boost::math::legendre_p<T>(n, x);
58 #else
59  static_assert(std::is_fundamental<T>::value || !std::is_fundamental<T>::value, "legendre_p<T> not specialized for type T, and MX_INCLUDE_BOOST is not defined, so I can't just use boost.");
60  return 0;
61 #endif
62 }
63 
64 template<>
65 float legendre_p<float>( int n,
66  float x
67  );
68 
69 template<>
70 double legendre_p<double>( int n,
71  double x
72  );
73 
74 template<>
75 long double legendre_p<long double>( int n,
76  long double x
77  );
78 
79 #ifdef HASQUAD
80 template<>
81 __float128 legendre_p<__float128>( int n,
82  __float128 x
83  );
84 #endif
85 
86 
87 /// The orthonormal Legendre polynomials
88 /** A version of the Legendre polynomials which are orthonormal on the interval
89  * -1 <= x <= 1.
90  *
91  * \returns the value of the n-th orthonormal Legendre polynomial at x.
92  */
93 template<typename T>
95  T x
96  )
97 {
98  return sqrt((static_cast<T>(2*n+1))/static_cast<T>(2)) * legendre_p<T>(n,x);
99 }
100 
101 } //namespace func
102 } //namespace math
103 } //namespace mx
104 
105 #endif //mx_legendre_hpp
T legendre_p(int n, T x)
Legendre Polynomials.
Definition: legendre.hpp:52
T orthoNormalLegendre(int n, T x)
The orthonormal Legendre polynomials.
Definition: legendre.hpp:94
The mxlib c++ namespace.
Definition: mxError.hpp:107