I write .sage files and then attach them in the interpreter (rather than using the notebook). Sometimes I want to have a section in .sage file consisting of pure python code (using for example the python module cvxopt), and of course this sometimes leads to type errors. I've tried including preparse(False) at the beginning of my code (as suggested here: http://www.sagemath.org/doc/faq/faq-usage.html#i-have-type-issues-using-scipy-cvxopt-or-numpy-from-sage) but in at least one case it returns the error AttributeError: 'bool' object has no attribute 'lstrip'.
I would very much appreciate a natural solution to this problem that doesn't involve changing anything in python (by decorating it with int,r, and so on).
Here is my code (which works just fine as pure python or works fine if I type preparser(False) in the sage interpreter and then cut and paste the code into the interpreter instead of typing attach('code.sage')).
import cvxopt as cvx from cvxopt import solvers
preparse(False)
c=cvx.matrix([1.,-1.,1.])
G=[cvx.matrix([[-7., -11., -11., 3.],[ 7., -18., -18., 8.],[-2., -8., -8., 1.]]) ]
G+=[cvx.matrix([[-21., -11., 0., -11., 10., 8., 0., 8., 5.],[ 0., 10., 16., 10., -10., -10., 16., -10., 3.],[ -5., 2., -17., 2., -6., 8., -17., 8., 6.]]) ]
h =[cvx.matrix([[33., -9.], [-9., 26.]]) ]
h+=[cvx.matrix([[14., 9., 40.], [9., 91., 10.], [40., 10., 15.]])]
sol = solvers.sdp(c, Gs=G, hs=h)
print(sol['x'])
The expected output is
pcost dcost gap pres dres k/t
0: -1.2037e+00 -1.8539e+02 2e+02 2e-16 8e+00 1e+00 1: -1.2937e+00 -6.8551e+00 5e+00 5e-16 3e-01 3e-02 2: -2.8964e+00 -3.7331e+00 7e-01 1e-15 4e-02 5e-02 3: -3.0150e+00 -3.2556e+00 2e-01 7e-16 1e-02 2e-02 4: -3.1389e+00 -3.1932e+00 5e-02 4e-16 3e-03 5e-03 5: -3.1533e+00 -3.1547e+00 1e-03 3e-16 7e-05 1e-04 6: -3.1535e+00 -3.1536e+00 5e-05 6e-16 3e-06 6e-06 7: -3.1535e+00 -3.1535e+00 1e-06 3e-16 7e-08 2e-07 Optimal solution found. [-3.68e-01] [ 1.90e+00] [-8.88e-01]