3

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')
4

1 に答える 1

0

ここに2つの問題があります。

1)インデント-Python関数はdef行を超えてインデントする必要があります

2)へのあなたの議論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']) #Note the very important change here

このコードは私にとっては問題なく動作します。

于 2013-01-24T14:40:06.477 に答える