how to use boost for bessel function in C++ - c++

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)

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

Related

Problem compiling a function from Boost Library

I'm trying to use the function norm_2_vector from boost.
But I'm getting the error ‘norm_2_square’ was not declared in this scope.
To compile I used the command below, where testNormSquare is the name of the program:
g++ -o testNorm2Square testNorm2Square.cpp
Question: Is there anything I'm missing? What should I do to make the code compilable?
A toy example that is not working is given below.
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/io.hpp>
int main () {
using namespace boost::numeric::ublas;
vector<double> v (3);
for (unsigned i = 0; i < v.size (); ++ i)
v(i) = i;
std::cout << 2.0 * v << std::endl;
std::cout << v * 2.0 << std::endl;
std::cout << norm_2_square(v);
}
The error message is the following:
testNorm2Square.cpp:12:18: error: ‘norm_2_square’ was not declared in this scope
Another problem happens if I specify explicitly that norm_2_square belongs to boost::numeric::ublas:
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/io.hpp>
int main () {
using namespace boost::numeric::ublas;
vector<double> v (3);
for (unsigned i = 0; i < v.size (); ++ i)
v(i) = i;
std::cout << 2.0 * v << std::endl;
std::cout << v * 2.0 << std::endl;
std::cout << boost::numeric::ublas::norm_2_square(v);
}
In this case the error message is:
testNorm2SquareV2.cpp:12:41: error: ‘norm_2_square’ is not a member of ‘boost::numeric::ublas’

Error: Identifier "cout" is undefined. <iostream> included and using namespace std;

I am trying to cout some variables but the compiler says that cout is undefined. I have included iostream and am using namespace std. Removing using namespace std and using std::cout instead changes the issue to "namespace "std" has no member "cout" ". I found some answers saying to add # include "stdafx.h" to the code but Error: cannot open source file "stdafx.h" occurs.
Code is:
#include "Complex.h"
#include <cmath>
#include <iostream>
using namespace std;
Complex::Complex(int PolarOrRectang, float RealOrArg, float ImagOrAng) {
if (PolarOrRectang == 0) {
real = RealOrArg;
imag = ImagOrAng;
else {
real = RealOrArg * cos(ImagOrAng);
imag = RealOrArg * sin(ImagOrAng);
}
};
void Complex::getValue(int PolarOrRectang) {
if (PolarOrRectang == 0) {
cout << real << " +_" << imag << "i" << endl;
} else {
cout << sqrt((real^2) + (imag^2)) << "*e^-" << atan(imag / real)<< endl;
}
};
I'm trying to define a class, so my main is elsewhere.
Running a very basic program that just couts "hello world" works fine, the problem is specific to this code.
Put #include<iostream> at the first position, the order is important
#include "Complex.h"
#include <iostream>
#include <cmath>
PS: Why do you use std:: when you are using "using namespace std;"?

Reference to ' ' is ambiguous

I am sorry but i don't know why this algorithm is not working.
The error at compiling is : "Reference to 'function' is ambiguous " and is on y = function() line, where I am calling the function
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define PI 3.141
float function(int g, int m, int s, float z)
{
using namespace std;
z = (g + m/60.0 + s/3600.0)*PI/180.0;
return z;
}
int main()
{
using namespace std;
float y;
int g,m,s;
cout << "g = ";
cin >> g;
cout <<"m = ";
cin >> m;
cout<<"s= ";
cin >>s;
y = function();
cout << "y= " << y << endl;
//cout<< (g + m/60.0 + s/3600.0)*PI/180.0 << endl;
return 0;
}
Vers2 - updated:
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define PI 3.141
float function(int g, int m, int s)
{
//using namespace std;
float z = (g + m/60.0 + s/3600.0)*PI/180.0;
//std::cout << z <<std::endl;
return z;
}
int main()
{
// using namespace std;
float y;
int g,m,s;
std::cout << "g = ";
std::cin >> g;
std::cout <<"m = ";
std::cin >> m;
std::cout<<"s= ";
std::cin >>s;
function();
// std::cout << "y= " << y << std::endl;
//cout<< (g + m/60.0 + s/3600.0)*PI/180.0 << endl;
return 0;
}
There is a member function in std and you inserted it into your namespace. Avoid using using namespace std;; you can import what you need this way:
using std::cout;
using std::cin;
I am getting a similar type of error while I used "prev" as a global variable of Node* type. Just renaming it with "prevv" solved issue in my case.
It is mostly due to the name of a "variable or function" is present in some library you used.
I can't reproduce your error message (for any of your versions with 3 different compilers), but the basic problem with your code is that you apparently assume the g,m,s-variables in your main functions are automatically used as parameters when you call function() just because they happen to have the same name.
This is NOT the case!
The variables inside your main and in the parameter list of function() are completely independent entities. The proper way to call the function and passing the right values is this:
y=function(g,m,s);
This basically copies the values stored inside the main g,m,s variables into the g,m,s parameters, which are accessed inside the function and after the function has completed, it then copies the value stored inside the variable you "return" from the function (here z) into the variable y.
This should work whether you are using using namespace std; or not, as your function has a completely different signature, But I'd still highly recommend to choose another name for your function.
I hope this doesn't sound like an insult, but I highly recommend that you read a introductory book about c++ programming, as it seems you are missing out on basic concepts of the language.

Identifier Not defined in Macro

I was trying the below program
#include <iostream>
using namespace std;
#define MKSTR(x) #x
#define CONCATE( x , y ) (x)##(y)
int main()
{
int xy = 100;
cout << MKSTR(HELLO C++) << endl;
cout << CONCATE(HELLO,C++) << endl;
cout << CONCATE(x,y) << endl;
return 0;
}
and getting the error
error C2065: 'HELLO' : undeclared identifier
. I don't see see why the VS 2012 compiler is expecting the macro argument or is treating them as identifier. Also the MKSTR macro worked fine but the CONCATE macro is giving me trouble. Can't understand why compiler is doing so.
You need to concatenate symbols first, and then expand it in a string, like this (compiles in GCC 4.8.1) :
#include <iostream>
using namespace std;
#define MKSTR(x) #x
#define CONCATE( x , y ) x ## y
#define CONCATESTR( x , y ) MKSTR(x ## y)
int main()
{
int xy = 100;
cout << MKSTR(HELLO C++) << endl;
cout << CONCATESTR(HELLO,C++) << endl;
cout << CONCATE(x,y) << endl;
return 0;
}
output :
HELLO C++
HELLOC++
100
EDIT :
So for the question of why MKSTR(CONCAT(...)) is not working, the reason is because of the expansion order of macros.
Because CONCAT is a parameter of MKSTR and MKSTR uses operator #, the argument is not expanded but instead immediately stringified. You could do this instead to make it work :
#include <iostream>
using namespace std;
#define CONCATE( x , y ) x ## y
#define MKSTR(x) #x
#define MKSTR2(x) MKSTR(x)
#define CONCATESTR( x , y ) MKSTR(x ## y)
int main()
{
int xy = 100;
cout << MKSTR2(HELLO C++) << endl;
cout << MKSTR2(CONCATE(HELLO,C++)) << endl;
cout << CONCATE(x,y) << endl;
return 0;
}
and it will output what you expect.

Boost Lambda/Phoenix - how to do lambda which returns another lambda?

Does Boost Lambda/Phoenix supports out of box something like lambda which returns another lambda?
For instance, that can be used to do some kind of currying:
std::cout << [](int x){return [=](int y){return x+y;};}(1)(2);
How to achieve similar purpose with Boost Lambda/Phoenix (+ as a bonus - we would get polymorphic behaviour)?
Boost Phoenix Scope: let/lambda
Live demo:
#include <boost/phoenix.hpp>
#include <iostream>
#include <ostream>
using namespace std;
using namespace boost;
using namespace phoenix;
using namespace arg_names;
using namespace local_names;
int main()
{
// capture by reference:
cout <<
(lambda(_a=_1)[_1 + _a ])(1)(2)
<< endl;
cout <<
(lambda(_b=_1)[lambda(_a=_1)[_1 + _a + _b ]])(1)(2)(3)
<< endl;
// capture by value:
cout <<
(lambda(_a=val(_1))[_1 + _a ])(1)(2)
<< endl;
cout <<
(lambda(_b=val(_1))[lambda(_a=val(_1))[_1 + _a + _b ]])(1)(2)(3)
<< endl;
}
Output is:
3
6
3
6