[英]compatibility between sage and numpy
这是两行代码,用于生成大小为4的随机排列:
from numpy import random
t = random.permutation(4)
这可以在Python中执行,但不能在sage中执行,这会产生以下错误:
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版本的可能性。
为了逃避Sage的准备器,您还可以在数字输入后附加字母r
(代表“原始”)。
from numpy import random
t = random.permutation(4r)
4r
优于int(4)
的优点是4r
绕过了预解析器,而int(4)
被准备为int(Integer(4))
因此Python整数被转换为Sage整数,然后又转换回Python整数。
同样, 1.5r
将为您提供纯Python浮点数,而不是Sage“实数”。
这在Sage中不起作用的原因是Sage准备了输入,将“ 4”从Python int
转换为Sage Integer
。 在Sage中,这将起作用:
from numpy import random
t = random.permutation(int(4))
或者,您可以关闭准备器:
preparser(False)
t = random.permutation(4)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.