将结构从C函数返回到SWIG中的Python
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了将结构从C函数返回到SWIG中的Python,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3960字,纯文字阅读大概需要6分钟。
内容图文
![将结构从C函数返回到SWIG中的Python](/upload/InfoBanner/zyjiaocheng/732/0352b1877c6049cc8f9c2f0c48173f55.jpg)
我有一个C头,它返回一个有3个元素的结构.如何让python正确接受struct变量?
这就是我在C函数中所拥有的:
// Function name myfunc
struct velocity
{
std::vector< std::vector<double> > u;
std::vector< std::vector<double> > v;
std::vector< std::vector<double> > w;
};
velocity velo; //Creating object
velo.u = sum(umean,pu);
velo.v = sum(vmean,pv);
velo.w = sum(wmean,pw);
return(velo)
这是我使用SWIG后的Python实现
import numpy
from myfunc import * # importing C++ myfunc library
My = 100 # Matrix dimensions
Mz = 100
z = myfunc(My,Mz) # Supplying the matrix dimensions to the myfunc library
print(z)
执行此操作时收到的错误消息:
<myfunc.velocity; proxy of <Swig Object of type 'velocity *' at 0x2951ae0> >
我知道我必须以某种方式在SWIG中定义以使python“按原样”获取结构.
有什么办法吗?或者您可能建议的任何替代方法?这是我的SWIG文件
%module myfunc
%{
#include "myfunc.h"
%}
%include "std_vector.i"
// Instantiate templates used by example
namespace std {
%template(IntVector) vector<int>;
%template(DoubleVector) vector<double>;
%template(twodvector) std::vector< std::vector<double> >;
}
struct velocity
{
std::vector< std::vector<double> > u;
std::vector< std::vector<double> > v;
std::vector< std::vector<double> > w;
};
%include "myfunc.h"
请注意,我在这里声明了一个结构.这在SWIG上成功编译,但我不知道如何在Python中使用它来实际获取C结构!
解决方法:
这不是错误消息:
<myfunc.velocity; proxy of <Swig Object of type 'velocity *' at 0x2951ae0> >
它只是表明你有一个SWIG代理对象,它包含一个指向速度对象的指针.例如,您可以访问z.u [0] [0]以访问其中一个双向量的元素.
编辑
这是一个为vector< vector< double>>定义的类型映射示例.它们并不漂亮,但允许直接为速度成员分配Python“列表列表”:
%module x
%begin %{
#pragma warning(disable:4127 4701 4706 4996)
#include <vector>
#include <algorithm>
#include <sstream>
%}
%include <std_vector.i>
%include <std_string.i>
%template(vector_double) std::vector<double>;
%template(vector_vector_double) std::vector<std::vector<double> >;
// Input typemap converts from Python object to C++ object.
// Note error checking not shown for brevity.
// $input is the Python object, $1 is the C++ result.
//
%typemap(in) std::vector<std::vector<double> >* (std::vector<std::vector<double> > tmp) %{
for(Py_ssize_t i = 0; i < PySequence_Size($input); ++i)
{
auto t = PySequence_GetItem($input,i);
std::vector<double> vd;
for(Py_ssize_t j = 0; j < PySequence_Size(t); ++j) {
auto d = PySequence_GetItem(t,j);
vd.push_back(PyFloat_AsDouble(d));
Py_DECREF(d);
}
Py_DECREF(t);
tmp.push_back(vd);
}
$1 = &tmp;
%}
// Output typemap converts from C++object to Python object.
// Note error checking not shown for brevity.
// $1 is the C++ object, $result is the Python result.
//
%typemap(out) std::vector<std::vector<double> >* %{
$result = PyList_New($1->size()); // Create outer Python list of correct size
for(size_t i = 0; i < $1->size(); ++i)
{
auto t = PyList_New((*$1)[i].size()); // Create inner Python list of correct size for this element.
for(size_t j = 0; j < (*$1)[i].size(); ++j) {
PyList_SET_ITEM(t,j,PyFloat_FromDouble((*$1)[i][j]));
}
PyList_SET_ITEM($result,i,t);
}
%}
%inline %{
struct velocity
{
std::vector<std::vector<double> > u;
std::vector<std::vector<double> > v;
std::vector<std::vector<double> > w;
};
// A test function with an in/out velocity parameter.
void myfunc(velocity& vel)
{
for(auto& v : vel.u)
std::transform(begin(v),end(v),begin(v),[](double d){return d*1.1;});
for(auto& v : vel.v)
std::transform(begin(v),end(v),begin(v),[](double d){return d*2.2;});
for(auto& v : vel.w)
std::transform(begin(v),end(v),begin(v),[](double d){return d*3.3;});
}
%}
使用示例:
>>> import x
>>> vel=x.velocity()
>>> vel.u = [1,2,3],[4.5,6,7]
>>> vel.v = [1,2],[3,4,5]
>>> vel.w = [1],[2,3]
>>> vel.u
[[1.0, 2.0, 3.0], [4.5, 6.0, 7.0]]
>>> vel.v
[[1.0, 2.0], [3.0, 4.0, 5.0]]
>>> vel.w
[[1.0], [2.0, 3.0]]
>>> x.myfunc(vel)
>>> vel.u
[[1.1, 2.2, 3.3000000000000003], [4.95, 6.6000000000000005, 7.700000000000001]]
>>> vel.v
[[2.2, 4.4], [6.6000000000000005, 8.8, 11.0]]
>>> vel.w
[[3.3], [6.6, 9.899999999999999]]
内容总结
以上是互联网集市为您收集整理的将结构从C函数返回到SWIG中的Python全部内容,希望文章能够帮你解决将结构从C函数返回到SWIG中的Python所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。