这里有两行代码用于生成大小为 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调用Python 2版本的numpy的可能性.
最佳答案
要转义 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 的 float 而不是
圣人“实数”。
关于python - 鼠尾草和numpy之间的兼容性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40578746/