2016-11-13 2 views
2

は、サイズ4のランダム順列を生成するためにコードの2行は、次のとおりです。互換ここ

TypeError         Traceback (most recent call last) 
<ipython-input-3-033ef4665637> in <module>() 
     1 from numpy import random 
----> 2 t = random.permutation(Integer(4)) 

mtrand.pyx in mtrand.RandomState.permutation (numpy/random/mtrand/mtrand.c:34842)() 

mtrand.pyx in mtrand.RandomState.shuffle (numpy/random/mtrand/mtrand.c:33796)() 

TypeError: len() of unsized object 

なぜですか?

もう少し詳しく:私はPython 3でコードを実行し、mtrandはPython 3ディレクトリにもあります。これは、sageがnumpyのPython 2バージョンを呼び出す可能性を排除するものです。

+0

しかし、現在使用されているSageにはPython 2しかありません(ただし、近い将来変更される可能性があります(https://trac.sagemath.org/ticket/15530を参照)。 – kcrisman

答えて

1

セージではうまくいかない理由は、セージが入力を準備して、Python intからセージIntegerに「4」になるということです。

from numpy import random 
t = random.permutation(int(4)) 

をそれとも、プリパーサーをオフにすることができます:セージでは、これは動作します

preparser(False) 
t = random.permutation(4) 
2

をセージのプリパーサーを逃れるために、あなたはまた、数値入力に(「生」のための)手紙rを追加することができます。 int(4)int(Integer(4))としてpreparsedいる間

from numpy import random 
t = random.permutation(4r) 

4rint(4)上の利点は、4rが プリパーサーをバイパスすることである ようにPythonの整数セージ整数に変換し、次いで Pythonの整数に戻し変換されます。

同じように、1.5rは、 賢者の "実数"ではなく、純粋なPythonの浮動小数点を与えます。

関連する問題