SWIG:如何将复杂列表从C ++传递给python [英] SWIG: How to pass list of complex from c++ to python
问题描述
我在c ++中有一个函数,该函数返回一个复杂列表:
I have a function in c++ who returns a list of complex:
#include <complex>
std::list< std::complex<double> > func(...);
我应该在'* .i'文件中做什么?
what should i do in the '*.i' file ?
谢谢大家
=================
=================
以下是详细信息:
我想在x.h中的python中使用的函数:
the function i would like to use in python in x.h:
std::list<std::complex<double> > roots1(const double& d, const double& e);
我尝试了一些方法:
(1)x.i:
%include <std_list.i>
%include <std_complex.i>
比我在IPython中尝试的时间
than I try in IPython:
tmp = x.roots1(1.0, 2.0)
len(tmp)
然后我得到了
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-5-9ebf7dab7cd9> in <module>()
----> 1 len(tmp)
TypeError: object of type 'SwigPyObject' has no len()
和:
dir(tmp)
返回:
[
...
'acquire',
'append',
'disown',
'next',
'own']
(2)x.i:
%include <std_list.i>
%include <std_complex.i>
namespace std {
%template(ComplexDouble) complex<double>;
}
然后,我得到了编译错误:
than, i got compile error:
error: command 'swig' failed with exit status 1
make: *** [py] Error 1
(3)in x.i:
%include <std_list.i>
%include <std_complex.i>
namespace std {
%template(ComplexDouble) list<complex<double> >;
}
或:
%include <std_list.i>
%include <std_complex.i>
namespace std {
%template(ComplexDouble) list<complex>;
}
然后我得到了
x_wrap.cpp:5673:32: error: ‘complex’ was not declared in this scope
template <> struct traits<complex > {
^
================================================ =
================================================
(先生/女士/夫人)m7thon帮助我找到了问题. 在我的python上下文中:
(Mr./Ms./Mrs) m7thon help me find the problem. In my python context:
Ubuntu: 45~14.04.1
Python: 3.4.3
SWIG: SWIG Version 2.0.11
Compiled with g++ [x86_64-unknown-linux-gnu]
Configured options: +pcre
如果将x.i定义为:
%include <std_list.i>
%include <std_complex.i>
namespace std {
%template(ComplexList) list<complex<double> >;
}
会有编译错误.但是,如果x.i:
there will be compiling error. But it works if x.i:
%include <std_list.i>
%include <std_complex.i>
%template(ComplexList) std::list< std::complex<double> >;
谢谢你.
推荐答案
这有效:
%include <std_list.i>
%include <std_complex.i>
%template(ComplexList) std::list<std::complex<double> >;
... (your includes / function declarations)
我认为您的版本(3)实际上也应该有效.奇怪的是没有.也许是SWIG错误.
I think your version (3) should actually also work. Strange that it doesn't. Maybe a SWIG bug.
这篇关于SWIG:如何将复杂列表从C ++传递给python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!