Swig,返回一个双精度数组

2022-01-19 00:00:00 python return arrays c swig

问题描述

我知道,通常有很多方法可以解决某些问题.但是在这里我知道我想要哪种方式,但我无法使其与 Python 和 SWIG 一起使用......

我有一个 C 函数,它返回一个双精度值数组:

double *my(int x){双 a,b,*buf;buf = malloc (x * sizeof(double));a=3.14;b=2.7;缓冲区[0]=一个;buf[1]=b;返回缓冲区;}

在这里,我绝对希望将数组作为返回值.不像许多示例中的void"函数,它写入输入数组.现在,我想获得一个 SWIG-python 包装器,它可以用作:

>>>导入示例>>>打印 example.my(7)[3.14,2.7]

无论我做什么,我都会遇到一些概念上的问题 - 我总是会遇到一些问题.比如 <Swig Object of type 'double *' at 0xFABCABA12>

我试图在我的 swg 文件中定义一些类型映射:

%typemap(out) double [ANY] {诠释我;$result = PyList_New($1_dim0);对于 (i = 0; i < $1_dim0; i++) {PyObject *o = PyFloat_FromDouble((double) $1[i]);PyList_SetItem($result,i,o);}}

但我仍然无法按要求得出我的结果.有没有人有一个简单的代码示例来完成这个任务?

解决方案

第一个问题是你的typemap不匹配,你需要一个%typemap(out) double * { ... } 因为你的函数返回一个指向 double 而不是 double 数组的指针.

如果您的列表是固定大小的(即整数文字),如您给出的示例(我认为这不是您想要的),您可以简单地更改我上面给出的类型映射并交换 $1_dim0 为固定大小.

否则你的问题是你的 %typemap(out) double * 不可能知道你的参数 int x 的值.您可以返回一个包含指针和大小的结构.然后您可以轻松定义类型映射以将其转换为列表(或 NumPy 数组,另请参阅我对 用数组成员包装 C 结构以便在 python 中访问:SWIG?cython?ctypes?).

顺便说一句,不可能在 C 中返回固定大小的数组(另请参阅此答案:声明一个 C 函数返回一个数组),所以 %typemap(out) double [ANY] { ... } 永远不会匹配.p>

I know, there are often many ways to solve certain problems. But here I know which way I want to have it, but I am unable to make it work with Python and SWIG...

I have a C-function, which returns me an array of double values:

double *my(int x)
{
   double a,b,*buf;
   buf = malloc (x * sizeof(double));
   a=3.14;
   b=2.7;
   buf[0]=a;
   buf[1]=b;
   return buf;
}

Here, I definitively want to have the array as a return value. Not, as in many examples a 'void' function, which writes into an input array. Now, I would like to get a SWIG-python wrapper, which might be used as:

>>> import example
>>> print example.my(7)
[3.14,2.7]

Whatever I do, I have some conceptual problems here - I always get s.th. like <Swig Object of type 'double *' at 0xFABCABA12>

I tried to define some typemaps in my swg file:

%typemap(out) double [ANY] {
  int i;
  $result = PyList_New($1_dim0);
  for (i = 0; i < $1_dim0; i++) {
    PyObject *o = PyFloat_FromDouble((double) $1[i]);
    PyList_SetItem($result,i,o);
  }
}

But still I am unable to get out my results as required. Does anyone have a simple code-example to achieve this task?

解决方案

The first problem is that your typemap doesn't match, you'll need a %typemap(out) double * { ... } since your function returns a pointer to double and not a double array.

If your list is of fixed size (i.e. an integer literal) as in the example you gave (which I assume is not what you want) you could simply change the typemap as I gave above and exchange $1_dim0 for the fixed size.

Otherwise your problem is that your %typemap(out) double * cannot possibly know the value of your parameter int x. You could return a struct that carries both the pointer and the size. Then you can easily define a typemap to turn that into a list (or a NumPy array, see also my response to Wrap C struct with array member for access in python: SWIG? cython? ctypes?).

Incidentally it's not possible to return a fixed sized array in C (see also this answer: Declaring a C function to return an array), so a %typemap(out) double [ANY] { ... } can never match.

相关文章