使用时出现分段错误韦伯.inlin

2024-05-13 19:42:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我将新的C++代码嵌入到Python中。我在测试编织.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')

Tags: 代码fromimportfortype错误npinline
1条回答
网友
1楼 · 发布于 2024-05-13 19:42:08

我在这里看到两个问题:

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

这段代码对我来说运行得很好。在

相关问题 更多 >