Compatibility Between Sage And Numpy
Here are two lines of code for the purpose of generating a random permutation of size 4: from numpy import random t = random.permutation(4) This can be executed in Python, but not
Solution 1:
To escape Sage's preparser, you can also append the letter r
(for "raw") to numerical input.
from numpy importrandomt= random.permutation(4r)
The advantage of 4r
over int(4)
is that 4r
bypasses the
preparser, while int(4)
is preparsed as int(Integer(4))
so that
the Python integer is transformed into a Sage integer and then
transformed back into a Python integer.
In the same way, 1.5r
will give you a pure Python float rather than
a Sage "real number".
Solution 2:
The reason this doesn't work in Sage is that Sage preparses its input, turning "4" from a Python int
to a Sage Integer
. In Sage, this will work:
from numpy importrandomt= random.permutation(int(4))
Or you can turn the preparser off:
preparser(False)
t = random.permutation(4)
Post a Comment for "Compatibility Between Sage And Numpy"