python 输入列表和输出列表_对python列表输入和输出使用swig

我正在使用SWIG构建一个Python模块,用于基于C代码的一些函数的求值。在

我需要的主要功能定义如下:void eval(double *x, int nx, int mx, double *f, int func_id)

目标python函数应该是:

^{pr2}$

在这里,eval将调用基准函数并返回它们的值。func_id是要调用的函数eval的id,nx是函数的维数,mx是将被计算的点数。在

实际上,我不清楚如何SWIG在类型映射之间传递值(比如temp$argnum,为什么总是使用$argnum?)。但通过查看wrap代码,我完成了typemap.I文件:%module cec17

%{

#include "cec17.h"

%}

%typemap(in) (double *x, int nx, int mx) (int count){

if (PyList_Check($input)) {

$3 = PyList_Size($input);

$2 = PyList_Size(PyList_GetItem($input, 0));

count = $3;

int i = 0, j = 0;

$1 = (double *) malloc($2*$3*sizeof(double));

for (i = 0; i < $3; i++){

for (j = 0; j < $2; j++){

PyObject *o = PyList_GetItem(PyList_GetItem($input, i), j);

if (PyFloat_Check(o))

$1[i*$2+j] = PyFloat_AsDouble(o);

else {

PyErr_SetString(PyExc_TypeError, "list must contrain strings");

free($1);

return NULL;

}

}

}

} else {

PyErr_SetString(PyExc_TypeError, "not a list");

return NULL;

}

}

%typemap(freearg) double *x {

free((void *) $1);

}

%typemap(in, numinputs=0) double *f (double temp) {

$1 = &temp;

}

%typemap(argout) double *f {

int i = 0;

int s = count1;

printf("pass arg %d", s);

$result = PyList_New(0);

for (i = 0; i < s; i++){

PyList_Append($result, PyFloat_FromDouble($1[i]));

}

}

void eval(double *x, int nx, int mx, double *f, int func_num);

然而,奇怪的事情发生了。通常,我测试30维函数。当评估值低于10分(mx&lt;10)时,模块工作正常。计算更多点时,会出现错误:[1] 13616 segmentation fault (core dumped) python test.py

我很确定问题不在c代码中,因为“mx”出现的唯一位置是“for loop”行中,其中是对每个点的求值。在

我也试着阅读包装代码并进行调试,但我就是找不到问题所在。下面是SWIG生成的wrap代码的一部分,我添加了一个printf行。即使是这个字符串也不会在错误发生之前打印出来。在#ifdef __cplusplus

extern "C" {

#endif

SWIGINTERN PyObject *_wrap_eval(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {

PyObject *resultobj = 0;

double *arg1 = (double *) 0 ;

int arg2 ;

int arg3 ;

double *arg4 = (double *) 0 ;

int arg5 ;

int count1 ;

double temp4 ;

int val5 ;

int ecode5 = 0 ;

PyObject * obj0 = 0 ;

PyObject * obj1 = 0 ;

printf("check point 0");

{

arg4 = &temp4;

}

if (!PyArg_ParseTuple(args,(char *)"OO:eval",&obj0,&obj1)) SWIG_fail;

{

if (PyList_Check(obj0)) {

arg3 = PyList_Size(obj0);

arg2 = PyList_Size(PyList_GetItem(obj0, 0));

count1 = arg3;

int i = 0, j = 0;

arg1 = (double *) malloc(arg2*arg3*sizeof(double));

for (i = 0; i < arg3; i++){

for (j = 0; j < arg2; j++){

PyObject *o = PyList_GetItem(PyList_GetItem(obj0, i), j);

if (PyFloat_Check(o))

arg1[i*arg2+j] = PyFloat_AsDouble(o);

else {

PyErr_SetString(PyExc_TypeError, "list must contrain strings");

free(arg1);

return NULL;

}

}

}

} else {

PyErr_SetString(PyExc_TypeError, "not a list");

return NULL;

}

}

ecode5 = SWIG_AsVal_int(obj1, &val5);

if (!SWIG_IsOK(ecode5)) {

SWIG_exception_fail(SWIG_ArgError(ecode5), "in method '" "eval" "', argument " "5"" of type '" "int""'");

}

arg5 = (int)(val5);

eval(arg1,arg2,arg3,arg4,arg5);

resultobj = SWIG_Py_Void();

{

int i = 0;

int s = count1;

resultobj = PyList_New(0);

for (i = 0; i < s; i++){

PyList_Append(resultobj, PyFloat_FromDouble(arg4[i]));

}

}

return resultobj;

fail:

return NULL;

}

谢谢你读到这里。:)

这个问题似乎有点乏味。也许你可以教我怎么写正确的类型图。我编码。在

谢谢你的帮助!在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值