我有一门课叫Universe。该类包括一个计算距离的成员函数,并且需要对一个丑陋的函数进行数值积分。我试图使用 GSL 执行集成,但是当我尝试编译库时出现以下错误 -
$ g++ -c -O3 -std=c++11 Universe.cpp -o Universe.o
$ error: cannot convert ‘Universe::Hz’ from type ‘double (Universe::)(double, void*)’ to type ‘double (*)(double, void*)’
这是没有构造函数的类 Universe(为简洁起见):
宇宙.h
#ifndef UNIVERSE_H
#define UNIVERSE_H
#include <cmath>
#include <gsl/gsl_integration.h>
using namespace std;
class Universe {
private:
static constexpr double c = 299792458.0, Mpc2Km = 3.08567758e+19, Yrs2Sec = 3.15569e7;
double H0 = 67.77, OmegaM = (0.022161+0.11889)/(H0*H0), OmegaL = 0.6914, OmegaG = 8.24e-5, OmegaK = 0.0009;
double Ez(double z);
double Hz(double z, void* params);
public:
double distH, timeH;
Universe() = default;
Universe(double h0);
Universe(double omegaM, double omegaL);
Universe(double h0, double omegaM, double omegaL);
Universe(double omegaM, double omegaL, double omegaG, double omegaK);
Universe(double h0, double omegaM, double omegaL, double omegaG, double omegaK);
//double radius();
//double age();
double distC(double z);
};
#endif
宇宙.cpp
#include <cmath>
#include <gsl/gsl_integration.h>
#include "Universe.h"
using namespace std;
double Universe::Hz(double z, void* params) {
double result = 1.0/pow(OmegaL + pow(1.0+z,3.0)*OmegaM + pow(1.0+z,4.0)*OmegaG + pow(1.0+z,2.0)*OmegaK, 0.5);
return result;
}
double Universe::distC(double z) {
double lower_limit = 0.0, abs_error = 1.0e-8, rel_error = 1.0e-8, alpha = 0.0, result, error;
gsl_integration_workspace *work_ptr = gsl_integration_workspace_alloc(1000);
gsl_function Hz_function;
void* params_ptr = α
Hz_function.function = Universe::Hz;
Hz_function.params = params_ptr;
gsl_integration_qags(&Hz_function, lower_limit, z, abs_error, rel_error, 1000, work_ptr, &result, &error);
return distH*result;
}
我不太清楚如何解决这个问题,我第一次使用 GSL 根据以下文档: http ://www.gnu.org/software/gsl/manual/html_node/Numerical-integration-examples .html 和以下指南: http ://www.physics.ohio-state.edu/~ntg/780/gsl_examples/qags_test.cpp 感谢您的浏览和任何答案!