how to use boost for bessel function in C++

2.6k Views Asked by At

I try to write spherical bessel function in C++ and use #include <boost/math/special_functions/bessel.hpp> and sph_bessel(v,x) in my code but error is happened say this not declared in this scope.I compile with g++ test.cpp .please help me.

#include <cmath>
#include <iostream>
#include <boost/math/special_functions/bessel.hpp> 
using namespace std;
int main()
{
    // spot check for n == 1
    double x = 1.2345;
    cout << "j_1(" << x << ") = " << sph_bessel(1, x) << '\n';
}

compile the code with:

g++ test.cpp

and give this error:

 error: ‘sph_bessel’ was not declared in this scope
 cout << "j_1(" << x << ") = " << sph_bessel(1, x) << '\n';
 a.cpp:9:38: note: suggested alternative:
 In file included from a.cpp:3:0:
 /usr/include/boost/math/special_functions/bessel.hpp:544:79: note:            ‘boost::math::sph_bessel’
 ename detail::bessel_traits<T, T, policies::policy<> >::result_type     sph_bessel(unsigned v, T x)
1

There are 1 best solutions below

3
On

The error message tells you what to do:

 a.cpp:9:38: note: suggested alternative:
             ‘boost::math::sph_bessel’

so the code should be:

 cout << "j_1(" << x << ") = " << boost::math::sph_bessel(1, x) << '\n';

or You could add:

using namespace boost::math;

but this is strongly discouraged: Why is “using namespace std” considered bad practice?

So I would instead suggest:

namespace bmath = boost::math;

Then instead of boost::math::sph_bessel(1, x) You can write: bmath::sph_bessel(1, x).