对于开始,对不起我的英语。在python中定义一个使用C库函数的结构(dll)
我在Python脚本中使用动态库(gnu gsl库)中的函数时遇到了一些麻烦。
事实上,我知道使用一个函数,但我需要定义一个结构白色C类型数据以使用特殊函数。
在第一这是一个C++脚本使用的功能,我将在Python脚本中使用的一个示例:
double fn1 (double x, void * params)
{
return cos(x) + 1.0;
}
int
main (void)
{
gsl_min_fminimizer *s;
double m = 2.0, m_expected = M_PI;
double a = 0.0, b = 6.0;
gsl_function F;
F.function = &fn1;
F.params = 0;
T = gsl_min_fminimizer_brent;
s = gsl_min_fminimizer_alloc (T);
gsl_min_fminimizer_set (s, &F, m, a, b);
...
}
gsl_function的在报头中的类型定义是:
struct gsl_function_struct
{
double (* function) (double x, void * params);
void * params;
};
typedef struct gsl_function_struct gsl_function ;
#define GSL_FN_EVAL(F,x) (*((F)->function))(x,(F)->params)
我知道使用带有ctypes的功能:
#Library Call
lib_gsl=CDLL(path_gsl+"gsl.dll")
#Functions Define
gsl_opti_brent=lib_gsl.gsl_min_fminimizer_brent
gsl_opti_alloc=lib_gsl.gsl_min_fminimizer_alloc
gsl_opti_init=lib_gsl.gsl_min_fminimizer_set
T=gsl_opti_brent
T_=pointer(T)
s=gsl_opti_alloc(T_)
在此之前,没有任何麻烦,但我当想要使用函数gsl_min_fminimizer_set,我需要一个名为gsl_function的头文件中定义的特殊类型。
有人知道如何在Python中定义typedef gsl_function以使用名为function gsl_min_fminimizer_set的dll?
谢谢。 我期待我明确
你看看[PyGSL(http://pygsl.sourceforge.net/)? – 2011-02-11 16:29:50
我不知道这个,我会尝试 – Johnraikonen 2011-02-11 16:51:49