I am just trying integrate over a function in C++. I have been trying to use gsl as I have seen this recommended online. I followed the gsl example with little success.
This is my C++ code:
double inverseE(double z){
double inverseE = 1.0/(std::sqrt(Om0*std::pow(1.0+z,3.0)+1.0-Om0));
return inverseE;
}
double comoving_distance(double z){
gsl_integration_workspace * w
= gsl_integration_workspace_alloc (1000);
double result, error;
gsl_function iE;
iE.function = &inverseE;
gsl_integration_qags (&iE, 0, z, 0, 1e-7, 1000,
w, &result, &error);
gsl_integration_workspace_free (w);
cout << result << endl;
return 0;
}
For clarification the same code in Python (which works) looks like this:
def iE(z):
return 1/(np.sqrt(Om0*np.power(1+z,3)+1-Om0))
def comoving_distance(z):
return (c/H0)*quad(iE,0,z)[0]
Where quad performs the integration (it's a scipy module).
I get two error messages:
ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function. Say ‘&cosmo::inverseE’ [-fpermissive]
cannot convert ‘double (cosmo::)(double)’ to ‘double ()(double, void)’ in assignment* cosmo is the name of the class which contains both of these functions.
It seems to be that this should not be a difficult thing to do. Advice as to where I am going wrong would be much appreciated!
EDIT: class
#include <iostream> // using IO functions
#include <string> // using string
#include <gsl/gsl_integration.h>
#include <cmath>
using namespace std;
class cosmo {
private:
double H0;
double Om0;
double Ob0;
double c;
double pi;
double G;
public:
// Constructor with default values for data members
cosmo(double Hubble0 = 70, double OmegaM0 = 0.3,
double OmegaB0 = 0.05) {
H0 = Hubble0;
Om0 = OmegaM0;
Ob0 = OmegaB0;
c = 3e8;
pi = 3.141592653589793;
G = 6.67408e-11;
}
double inverseE(double z){
double inverseE = 1.0/(std::sqrt(Om0*std::pow(1.0+z,3.0)+1.0-Om0));
return inverseE;
}
double comoving_distance(double z){
gsl_integration_workspace * w
= gsl_integration_workspace_alloc (1000);
double result, error;
gsl_function iE;
iE.function = &inverseE;
gsl_integration_qags (&iE, 0, z, 0, 1e-7, 1000,
w, &result, &error);
gsl_integration_workspace_free (w);
cout << result << endl;
return 0;
}
};