- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 C++ 实现费米气体工具的共享库。我正在使用 GSL 库以数字方式求解函数,并且我的代码在作为脚本运行时没有问题地运行,但是在尝试将其转换为共享库和类时我遇到了问题。
我是 C++ 编程的新手,似乎无法针对我的问题调整不同的答案。可能是因为我不太明白答案。
我的代码是:
/* Define structure for the GSL-function: chempot_integrand */
struct chempot_integrand_params { double mu; double T; };
double
ChemicalPotential::chempot_integrand (double x, void * params){
/* Computes the integrand for the integral used to obtain the chemical potential.
*
* This is a GSL-function, which are integrated using gsl_integration_qag.
*/
// Get input parameters.
struct chempot_integrand_params * p = (struct chempot_integrand_params *) params;
double mu = p->mu;
double T = p->T;
// Initiate output parameters for GSL-function.
gsl_sf_result_e10 result;
int status = gsl_sf_exp_e10_e( ( gsl_pow_2(x) - mu ) / T , &result );
if (status != GSL_SUCCESS){
printf ("Fault in calculating exponential function.");
}
// Return (double) integrand.
return (gsl_pow_2(x) / ( 1 + result.val * gsl_sf_pow_int(10,result.e10) ));
}
/* Define structure for the GSL-function: chempot_integration */
struct chempot_integral_params { double T; };
double
ChemicalPotential::chempot_integration (double mu, double T){
/* Computes the integral used to obtain the chemical potential using the integrand: chempot_integrand.
*/
// Set input parameters for the integrand: chempot_integrand.
struct chempot_integrand_params params_integrand = { mu, T };
// Initiate the numerical integration.
gsl_integration_workspace * w = gsl_integration_workspace_alloc (1000); // Allocate memory for the numerical integration. Can be made larger if neccessary, REMEMBER to change it in the function call: gsl_integration_qag as well.
double result, error;
gsl_function F;
F.function = &ChemicalPotential::chempot_integrand;
F.params = ¶ms_integrand;
// Upper limit for integration
double TOL = 1e-9;
double upp_lim = - T * gsl_sf_log(TOL) + 10;
gsl_integration_qag (&F, 0, upp_lim, 1e-12, 1e-12, 1000, 6, w, &result, &error);
// Free memory used for the integration.
gsl_integration_workspace_free (w);
return result;
}
编译时出现错误
error: cannot convert ‘double (Fermi_Gas::ChemicalPotential::*)(double, void*)’ to ‘double (*)(double, void*)’
排队
F.function = &ChemicalPotential::chempot_integrand;
最佳答案
确实有趣的是人们一遍又一遍地问这个问题。原因之一可能是所提出的解决方案不容易理解。我个人在理解和实现它们时遇到了问题。 (如您所料,这些解决方案对我来说并不是开箱即用的。)
在tlamadon的帮助下我刚刚想出了一个可能在这里也有帮助的解决方案。让我们看看你们的想法。
因此,回顾一下,问题是您有一个包含成员函数的类,您希望在该类上使用 GSL 库中的内容进行操作。如果 GSL 接口(interface)需要一个
gsl_function F;
所以这是示例类:
class MyClass {
private:
gsl_f_pars *p; // not necessary to have as member
public:
double obj(double x, void * pars); // objective fun
double GetSolution( void );
void setPars( gsl_f_pars * xp ) { p = xp; };
double getC( void ) ; // helper fun
};
这个练习的目标是能够
MyClass测试
, test.GetSolution()
,它应该返回 GSL 函数的用途(obj
的最小值、根、积分或其他) 现在的诀窍是在参数 struct gsl_f_pars
中放置一个元素,是指向 MyClass 的指针。这是结构:
struct gsl_f_pars {
double a;
double b;
double c;
MyClass * pt_MyClass;
};
最后一部分是提供一个将在 MyClass::GetSolution()
中调用的包装器(包装器是成员函数 MyClass::obj
,我们不能只在类中用 &obj
指向它)。此包装器将采用参数结构、取消引用 pt_MyClass
并评估 pt_MyClass
的成员 obj
:
// Wrapper that points to member function
// Trick: MyClass is an element of the gsl_f_pars struct
// so we can tease the value of the objective function out
// of there.
double gslClassWrapper(double x, void * pp) {
gsl_f_pars *p = (gsl_f_pars *)pp;
return p->pt_MyClass->obj(x,p);
}
完整的示例有点太长,无法在这里发布,所以我提出了一个要点。这是一个header file和一个 cpp file ,它应该在任何有 GSL 的地方工作。编译运行
g++ MyClass.cpp -lgsl -o test
./test
关于c++ - 在共享库的类中调用 GSL 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19450198/
我正在编写一些代码,其中使用了一些 GNU GSL 函数。 出于纯粹的兴趣,我想知道这些功能是如何实现的,因此我想看看源代码。 问题是我似乎无法找到它所在的目录。 我知道我可以在“/usr/local
我需要计算 ∫ [x₁ to x₂] 1/√(1-k² sinh²(x)) dx 在我的程序中。这可以使用在复数参数中评估的第一类不完全椭圆积分来表示: -i (F(ix₂ | -k²) - F(ix
brew install gsl - 没问题 快速制作一个使用 gsl 的 Mac 应用程序,以检查它是否正常工作。 ( Handy example here. ) - 没问题 它们在/include
给定以下函数,采用:只读 float 跨度(动态或任何静态大小): template void foobar(gsl::span x); 假设我有一个 vector .将其作为参数传递是行不通的,但
C++ Core Guidelines促进实践using span . 问题在于 const 和可变范围。这是我尝试做的: auto foo(gsl::span); // 1st au
我正在尝试获取一个名为 emergent 的软件在职的。它依赖于我已经安装的 qt 和 coin。 但是当我尝试运行 emergent 时,出现以下错误: dyld: Library not load
假设我有一个成员变量std::vector在一个类中,我想使用 gsl::array_view 的组合从成员函数返回它作为不可变 View 和 gsl::cstring_view .不幸的是,以下内容
我正在尝试计算两个 vector a 和 b 之间的马氏距离。最终,我将使用它作为统计算法中的距离度量。我正在使用 gsl 来实现它们。马氏距离的公式是 sqrt((a-b)'c^-1(a-b)),其
我正在将指南支持库检查器集成到我的项目中。 Microsoft.CppCoreCheck Microsoft.Gsl 当我运行它时,我从包含的库(如标准库、glm、boost 等)中收到一堆错误。 一
我记得读过 mat4x3 比 mat3x4 使用了更多的寄存器,因为它有四列,即使它们具有相同数量的元素。我似乎无法在任何地方找到它了。新规范是否对两种类型的矩阵使用相同数量的统一寄存器? 转置是否也
我想使用 GSL 的统一随机数生成器。在他们的网站上,他们包含以下示例代码: #include #include int main (void) { const gsl_rng_t
我最近安装了 GSL,用于我的计算作业。我从预构建的库安装了它,并将其链接到 CodeBlocks,然后运行 GSL(贝塞尔函数)的测试示例以确保它有效。一切都很顺利。 然后我尝试了这里给出的线性
我现在正在与 GSL 合作。我在寻找特征值时遇到一些问题。在对称矩阵的情况下,GSL 似乎没有正确给出特征值。我给出了某些对称矩阵的输入,这些矩阵的特征值应该为 0,但是,GSL 函数返回非零特征值来
构建后: $ gcc generator.c -lm -lgsl -lgslcblas -lm 代码: #include #include gsl_rng * r; /* global gene
我正在使用 GSL 进行大量三次样条插值。假设我有三个自变量 a、b 和 c,所有变量都在相同的物理数据点上制表(可能是同一组位置)以米、英尺和英里为单位),以及两个因变量 y 和 z,在相同点处制表
我目前正在尝试在另一个自己编写的库(Quaternion.c)中使用我之前编写的库(matrix.c),方法是使用使用“matrix.h”的标准方法通过头文件调用它”包含“matrix.c”文件中的函
我正在尝试更新旧代码,该代码使用的是具有已弃用函数的 GSL 版本,但我在寻找如何使用新版本的归一化勒让德多项式函数时遇到了麻烦。这是总结问题的片段: #include #include #inc
我正在使用 GSL 示例页面上的代码来尝试求解一个包含四个微分方程的系统。我一直在努力将五个参数传递给 ODE 系统,并且遇到了一个最终的(我希望!)编译时错误。下面是一个片段,给我错误 114:57
我已经在 Kubuntu 14.04 中安装了最新版本的 GSL (1.16)。我使用库附带的文件 INSTALL(逐步)进行安装。我配置了 eclipse-cdt 环境以供使用。它工作,我认为,因为
我有一个非常简单的问题——我只想在 C 语言的 gsl 中将两个具有复杂条目的矩阵相乘。例如,我想定义一个函数 gsl_matrix_complex *multiply( gsl_matrix_com
我是一名优秀的程序员,十分优秀!