mor*_*njt 7 c python swig numpy
我正在尝试使用SWIG和Numpy类型映射在python中创建的一个小C函数接口
该功能定义如下
void nw(int* D, int Dx, int Dy, int* mat, int mx, int my, char *xstr, int xL,char *ystr, int yL);
Run Code Online (Sandbox Code Playgroud)
我的界面文件如下
%module nw
%{
#define SWIG_FILE_WITH_INIT
#include "nw.h"
%}
%include "numpy.i"
%init %{
import_array();
%}
/*type maps for input arrays and strings*/
%apply (int* INPLACE_ARRAY2, int DIM1, int DIM2) {(int* D, int Dx, int Dy)}
%apply (int* IN_ARRAY2, int DIM1, int DIM2) {(int* mat, int mx, int my)}
%apply (char* IN_ARRAY, int DIM1){(char *xstr, int xL),(char *ystr, int yL)}
%include "nw.h"
Run Code Online (Sandbox Code Playgroud)
为了测试它,我使用了以下输入
D = numpy.zeros((5,5),numpy.int)
mat = numpy.array([[1, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0],
[0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 1]],numpy.int)
x = numpy.array(list("ABCD"))
y = numpy.array(list("ABCD"))
import nw
nw.nw(D,mat,x,y)
Run Code Online (Sandbox Code Playgroud)
但是当我运行它时,我得到以下内容
TypeError: nw() takes exactly 6 arguments (4 given)
Run Code Online (Sandbox Code Playgroud)
我真的很困惑这些参数是如何定义的.这里有没有人知道为什么有6个参数以及这些参数是什么?谢谢!
好吧,我想我已经解决了问题。
事实证明,SWIG确实不喜欢我为 cstrings 制定的 apply 指令。
我应该应该使用以下指令。
%apply (char *STRING, int LENGTH) {(char *xstr, int xL),(char *ystr, int yL)}
Run Code Online (Sandbox Code Playgroud)
应该更仔细地遵循食谱哈哈
| 归档时间: |
|
| 查看次数: |
1102 次 |
| 最近记录: |