从C++指针到NUMY数组的哪种SWIG类型映射?

2024-06-07 07:12:54 发布

您现在位置:Python中文网/ 问答频道 /正文

<>我想用一个麻木数组来映射一个C++双*VEC类型,所以我读了这些文档: http://docs.scipy.org/doc/numpy/reference/swig.interface-file.html,尤其是“常见示例”部分 和http://www.swig.org/Doc1.3/Python.html#Python_nn18。在

我没办法让斯威格明白,(int len,double*ivec)实际上应该是(int,数字阵列),而不是一个numpy数组?,尽管我使用了numpy.i打印地图。我尝试了以下方法:

/* test3.cpp */

#include <cstring>
#include <iostream>
#include <cmath>


void abel(int len1, double* ivec, int len2, double* ovec)
{
    std::cout << "Hello" << std::endl;

    return;
}

以及

^{pr2}$ <> >我将C++类型<强>(int大小,双*IVEC)< /St> > NoMy.I类型映射>强>(int DIM1,双* INPATION ARARY1)> Suthe,如ScPy DOC

/*test3.i*/

%module Amod

%{
#define SWIG_FILE_WITH_INIT
#include "test3.h"
%}

%include "numpy.i"

%init %{
import_array();
%}

%apply (int DIM1, double* INPLACE_ARRAY1) {(int len1, double* ivec), (int len2, double* ovec)};

%include "test3.h"

我在文档中编译解释:

swig -c++ -python test3.i
g++ -fPIC -c test3.cpp
g++ -fPIC -c test3_wrap.cxx -I/usr/include/python2.7
g++ -shared test3.o test3_wrap.o -o _Amod.so

我可以在python中导入Amod,但是:

>>>import Amod as ab
>>>import numpy as np
>>>a = np.array([1.0,2.0,3.0])
>>>ab.abel()

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: abel() takes exactly 2 arguments (0 given)


>>>ab.abel(a,a)
Hello


>>>ab.abel(3,a,3,a)

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: abel() takes exactly 2 arguments (4 given)

怎么了?为什么整数arg消失了?我觉得我错过了一些重要的事情。在

非常感谢,对于这个问题,我尽量减少代码。我是新来的,请告诉我如何激活颜色代码,使其更具可读性。在


Tags: 文档importnumpy类型abinclude数组swig