Legendre polynomials


Hello everyone,
I’m having trouble using the boost::math::legendre_p function, despite the fact that boost::math::cyl_bessel_k as in the C++ vignette works fine in my code. I’m replacing

double besselK(const double& v, const double& z, std::ostream* pstream__) {
return boost::math::cyl_bessel_k(v, z);

in the hpp file with

double legendreP(const int& v, const double& z, std::ostream* pstream__) {
return boost::math::legendre_p(v, z);

and adding

real legendreP(int n, real v)

in the functions block. A call to stan_model() with the appropriate include, results in the following error,

Error in dyn.load(libLFile) :
unable to load shared object ‘/var/folders/h_/mp3xxlhd1w3dq4vjwk1f8kcm0000gn/T//RtmpDZF35C/file4816dc396ec.so’:
dlopen(/var/folders/h_/mp3xxlhd1w3dq4vjwk1f8kcm0000gn/T//RtmpDZF35C/file4816dc396ec.so, 6): Symbol not found: ZN5boost4math10legendre_pIdEENS0_5tools12promote_argsIT_fffffE4typeEiS4
Referenced from: /var/folders/h
Expected in: flat namespace
in /var/folders/h_/mp3xxlhd1w3dq4vjwk1f8kcm0000gn/T//RtmpDZF35C/file4816dc396ec.so

Any insights would be greatly appreciated!


My guess is that you are calling it with v as a parameter, in which case you are going to have to do the templating. If you just generate the C++ code, it should tell you what the signature should be but I think it would be something like

template<typename T>
auto legendreP(const int& v, const T&z, std::ostream* pstream__) -> decltype(v + z) {
  return boost::math::legendre_p(v, z);


No such luck, even tried the Airy function,

template <typename T1__>
typename boost::math::tools::promote_args<T1__>::type
airy(const T1__& z, std::ostream* pstream__) {
return boost::math::airy_ai(z);

and got back the same error type. And yet the cyl_bessel_k works fine; very odd.


Did you #include the appropriate headers from Boost?


Hi Ben,
After trying to add an include within stan_model() without any breakthrough, I decided to modify the stan/model/model_header.hpp file, adding the line

#include <boost/math/special_functions/legendre.hpp>

below the other boost includes, and it worked fine, thanks for your time!