-4

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)
A.R.N
  • 1
  • 1
  • 2
  • You probably missed to specify the necessary namespace qualifier for `sph_bessel(v,x)`. – πάντα ῥεῖ Sep 23 '18 at 13:57
  • Please paste a simple version of Your cpp file, which when compiled shows this error. ALSO please paste the full error message generated by compiler for this simple version. In short, please create a [mcve] – Robert Andrzejuk Sep 23 '18 at 14:48

1 Answers1

0

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).

Robert Andrzejuk
  • 5,076
  • 2
  • 22
  • 31
  • thank you.its work.now I want to change x from double to complex and error happened again. do you know how to solve it? – A.R.N Sep 24 '18 at 07:46
  • With C++17, this function is available as a standard library:[std::sph_bessel, std::sph_besself, std::sph_bessell](https://en.cppreference.com/w/cpp/numeric/special_math/sph_bessel) – Robert Andrzejuk Sep 24 '18 at 09:33
  • It looks like complex might not be used: https://stackoverflow.com/questions/19981747/why-wont-boosts-bessel-function-compile-with-a-complex-input – Robert Andrzejuk Sep 24 '18 at 10:21