我对将C++代码嵌入到Python中是个新手。我正在测试weave.inline。然而,在运行我的代码时,我得到了一个分段错误。有人能告诉我我哪里做错了吗?下面是我的代码:
from scipy import weave
def cpp_call(np_points):
assert(type(np_points) == type(1))
code = """
double z[np_points+1][np_points+1];
for (int x = 0; x <= np_points; x++)
{
for (int y = 0; y <= np_points; y++)
{
z[x][y] = x*y;
}
}
"""
return weave.inline(code,'np_points')发布于 2013-01-24 22:40:06
我在这里看到两个问题:
1)缩进-您的python函数需要缩进超过def行
2) weave.inline()的参数应该是列表。请参阅here for details
因此,更正后的代码应该如下所示:
from scipy import weave
def cpp_call(np_points):
assert(type(np_points) == type(1))
code = """
double z[np_points+1][np_points+1];
for (int x = 0; x <= np_points; x++)
{
for (int y = 0; y <= np_points; y++)
{
z[x][y] = x*y;
}
}
"""
return weave.inline(code,['np_points']) #Note the very important change here这段代码对我来说运行得很好。
https://stackoverflow.com/questions/14502068
复制相似问题