问题描述
我有一个 C++ 类,我正在尝试使用 SWIG 为 Python 包装它.我在尝试包装以数组作为输入的函数之一时遇到问题.
这是头文件,
class dyndiff_data_t{私人的:双H[3];双伽马;双k;双P;民众:dyndiff_data_t(双H_[3],常量双 GAMMA_,常量双 k_,常量双 P_);无效测试();};
这里是 swig 接口文件,
%module twowave%{#define SWIG_FILE_WITH_INIT#include <twowave.h>%}%include "numpy.i"%在里面 %{import_array();%}%apply (double IN_ARRAY1[3]) {(double H_[3])};%include <twowave.h>
问题是对于数组输入,SWIG 抱怨没有类型映射.我不明白为什么.numpy.i 文件取自 here 和here>
任何帮助将不胜感激.
问题是 numpy.i 中的 typemap 定义了一个两个参数的 typemap,而您正试图将它应用于单个参数.如果您的函数中有参数 int len1 和 double* vec1,这将起作用:
%apply (int DIM1, double* IN_ARRAY1) {(int len, double* H_)}
与其编写自己的 typemap,不如使用 carrays.i.
如果您要编写类型映射,例如将双精度元组作为输入,它看起来像:
%typemap(in) double TUPLE[ANY]{...}
在这种情况下,您可以按照您期望的方式将其应用到您的函数中.
%apply double TUPLE[3] {double H_[3]}
在尝试找出不能使用类型映射的原因时,一个不错的起点是使用 -tmsearch
选项运行 SWIG.在尝试匹配您的函数参数时,它会告诉您它在寻找什么.
I have a c++ class that I am trying to wrap for Python using SWIG. I am having trouble trying to wrap one of the functions which takes an array as input.
Here's the header file,
class dyndiff_data_t
{
private:
double H[3];
double GAMMA;
double k;
double P;
public:
dyndiff_data_t(double H_[3],
const double GAMMA_,
const double k_,
const double P_);
void test();
};
and here's the swig interface file,
%module twowave
%{
#define SWIG_FILE_WITH_INIT
#include <twowave.h>
%}
%include "numpy.i"
%init %{
import_array();
%}
%apply (double IN_ARRAY1[3]) {(double H_[3])};
%include <twowave.h>
The problem is that for the array input, SWIG complains that there is no typemap. I don't understand why. The numpy.i file was taken from here and the typemap I am using is described here
Any help would be appreciated.
The problem is that the typemap in numpy.i defines a two argument typemap, and you're trying to apply it to a single argument. This would work if you had parameters int len1, and double* vec1 in your function:
%apply (int DIM1, double* IN_ARRAY1) {(int len, double* H_)}
Rather than writing your own typemap, just use carrays.i.
If you WERE to write a typemap, e.g. to take a tuple of doubles as input, it would look something like:
%typemap(in) double TUPLE[ANY]
{
...
}
in which case you would apply it to your function the way you expect.
%apply double TUPLE[3] {double H_[3]}
A good place to start when trying to figure out why you can't use a typemap is to run SWIG with the -tmsearch
option. It will tell you what it's looking for when trying to match your function parameters.
这篇关于SWIG:未定义类型映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!