ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 30 Jan 2015 20:56:30 +0100preparse(False) (still) not workinghttps://ask.sagemath.org/question/24696/preparsefalse-still-not-working/ 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]
EDIT: tmonteil suggested changing preparse(False) to preparser(False). This works if I cut and paste the code into the interpreter but doesn't work if I attach the .sage file in the interpreter. See tmonteil's answer and my comment.
EDIT2: It also works in sage worksheet if I replace preparser(False) with %python (but I very much don't want to use the sage worksheet). Is this a bug, or am I doing something wrong?Thu, 30 Oct 2014 09:15:08 +0100https://ask.sagemath.org/question/24696/preparsefalse-still-not-working/Answer by tmonteil for <p>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: <a href="http://www.sagemath.org/doc/faq/faq-usage.html#i-have-type-issues-using-scipy-cvxopt-or-numpy-from-sage">http://www.sagemath.org/doc/faq/faq-u...</a>) but in at least one case it returns the error AttributeError: 'bool' object has no attribute 'lstrip'. </p>
<p>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). </p>
<p>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')).</p>
<p>import cvxopt as cvx
from cvxopt import solvers</p>
<p>preparse(False)</p>
<p>c=cvx.matrix([1.,-1.,1.])</p>
<p>G=[cvx.matrix([[-7., -11., -11., 3.],[ 7., -18., -18., 8.],[-2., -8., -8., 1.]]) ]</p>
<p>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.]]) ]</p>
<p>h =[cvx.matrix([[33., -9.], [-9., 26.]]) ]</p>
<p>h+=[cvx.matrix([[14., 9., 40.], [9., 91., 10.], [40., 10., 15.]])]</p>
<p>sol = solvers.sdp(c, Gs=G, hs=h)</p>
<p>print(sol['x'])</p>
<p>The expected output is </p>
<pre><code> pcost dcost gap pres dres k/t
</code></pre>
<p>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]</p>
<p>EDIT: tmonteil suggested changing preparse(False) to preparser(False). This works if I cut and paste the code into the interpreter but doesn't work if I attach the .sage file in the interpreter. See tmonteil's answer and my comment.</p>
<p>EDIT2: It also works in sage worksheet if I replace preparser(False) with %python (but I very much don't want to use the sage worksheet). Is this a bug, or am I doing something wrong?</p>
https://ask.sagemath.org/question/24696/preparsefalse-still-not-working/?answer=24697#post-id-24697The ``preparse`` function preparses an expression, for example ``preparse('2^2')``. To set the preparser off, you should use the ``preparser`` function:
sage: preparser(False)
EDIT:
Another possibility is to attach a file ending with ``.py`` and not ``.sage`` so that the preparser will not be used for that file.
Also, since the only problem comes from the fact that floats like ``1.`` are turned into Sage ``RealNumber``'s by the preparser, another possibility is to explicitely tell to keep those number unparsed, by typing : ``1.r`` instead of ``1.``, the letter ``r`` standing for "raw".
Yet another (dirty but lazy) possiblity is to redifine Sage ``RealNumber`` to be Python ``float``, by adding the following line at the beginning:
sage: RealNumber=float
Thu, 30 Oct 2014 10:34:07 +0100https://ask.sagemath.org/question/24696/preparsefalse-still-not-working/?answer=24697#post-id-24697Comment by jack77 for <p>The <code>preparse</code> function preparses an expression, for example <code>preparse('2^2')</code>. To set the preparser off, you should use the <code>preparser</code> function:</p>
<pre><code>sage: preparser(False)
</code></pre>
<p>EDIT:</p>
<p>Another possibility is to attach a file ending with <code>.py</code> and not <code>.sage</code> so that the preparser will not be used for that file.</p>
<p>Also, since the only problem comes from the fact that floats like <code>1.</code> are turned into Sage <code>RealNumber</code>'s by the preparser, another possibility is to explicitely tell to keep those number unparsed, by typing : <code>1.r</code> instead of <code>1.</code>, the letter <code>r</code> standing for "raw".</p>
<p>Yet another (dirty but lazy) possiblity is to redifine Sage <code>RealNumber</code> to be Python <code>float</code>, by adding the following line at the beginning:</p>
<pre><code>sage: RealNumber=float
</code></pre>
https://ask.sagemath.org/question/24696/preparsefalse-still-not-working/?comment=25658#post-id-25658@Christ
what is about
RememberInteger=Integer
...
Integer=int
...
Integer= RememberIntegerFri, 30 Jan 2015 20:56:30 +0100https://ask.sagemath.org/question/24696/preparsefalse-still-not-working/?comment=25658#post-id-25658Comment by Polonius for <p>The <code>preparse</code> function preparses an expression, for example <code>preparse('2^2')</code>. To set the preparser off, you should use the <code>preparser</code> function:</p>
<pre><code>sage: preparser(False)
</code></pre>
<p>EDIT:</p>
<p>Another possibility is to attach a file ending with <code>.py</code> and not <code>.sage</code> so that the preparser will not be used for that file.</p>
<p>Also, since the only problem comes from the fact that floats like <code>1.</code> are turned into Sage <code>RealNumber</code>'s by the preparser, another possibility is to explicitely tell to keep those number unparsed, by typing : <code>1.r</code> instead of <code>1.</code>, the letter <code>r</code> standing for "raw".</p>
<p>Yet another (dirty but lazy) possiblity is to redifine Sage <code>RealNumber</code> to be Python <code>float</code>, by adding the following line at the beginning:</p>
<pre><code>sage: RealNumber=float
</code></pre>
https://ask.sagemath.org/question/24696/preparsefalse-still-not-working/?comment=24700#post-id-24700Thanks. I forgot to mention that I had already tried that and it doesn't work with attach in the above example, although it does work if you copy and paste the code. If I try to attach the above code (with preparse(False) changed to preparser(False)), I get
13 preparser(False)
14
---> 15 c=cvx.matrix([_sage_const_1p ,-_sage_const_1p ,_sage_const_1p ])
16
17 G=[cvx.matrix([[-_sage_const_7p , -_sage_const_11p , -_sage_const_11p , _sage_const_3p ],[ _sage_const_7p , -_sage_const_18p
, -_sage_const_18p , _sage_const_8p ],[-_sage_const_2p , -_sage_const_8p , -_sage_const_8p , _sage_const_1p ]]) ]
TypeError: invalid type in list
So that means (I think) that the preparser is not getting turned off when I attach the .sage file.Thu, 30 Oct 2014 12:06:51 +0100https://ask.sagemath.org/question/24696/preparsefalse-still-not-working/?comment=24700#post-id-24700Comment by Polonius for <p>The <code>preparse</code> function preparses an expression, for example <code>preparse('2^2')</code>. To set the preparser off, you should use the <code>preparser</code> function:</p>
<pre><code>sage: preparser(False)
</code></pre>
<p>EDIT:</p>
<p>Another possibility is to attach a file ending with <code>.py</code> and not <code>.sage</code> so that the preparser will not be used for that file.</p>
<p>Also, since the only problem comes from the fact that floats like <code>1.</code> are turned into Sage <code>RealNumber</code>'s by the preparser, another possibility is to explicitely tell to keep those number unparsed, by typing : <code>1.r</code> instead of <code>1.</code>, the letter <code>r</code> standing for "raw".</p>
<p>Yet another (dirty but lazy) possiblity is to redifine Sage <code>RealNumber</code> to be Python <code>float</code>, by adding the following line at the beginning:</p>
<pre><code>sage: RealNumber=float
</code></pre>
https://ask.sagemath.org/question/24696/preparsefalse-still-not-working/?comment=24724#post-id-24724Many thanks. RealNumber=float and Integer=int seem to be my best best now, and I guess I can always define them back to usual later in the file. But this really seems to me a bug, given that %python works as advertised in a sage worksheet but preparser(False) doesn't in .sage file. Not to be ungrateful for your help, but I'll leave this answer unaccepted for now.Fri, 31 Oct 2014 13:56:15 +0100https://ask.sagemath.org/question/24696/preparsefalse-still-not-working/?comment=24724#post-id-24724Comment by Polonius for <p>The <code>preparse</code> function preparses an expression, for example <code>preparse('2^2')</code>. To set the preparser off, you should use the <code>preparser</code> function:</p>
<pre><code>sage: preparser(False)
</code></pre>
<p>EDIT:</p>
<p>Another possibility is to attach a file ending with <code>.py</code> and not <code>.sage</code> so that the preparser will not be used for that file.</p>
<p>Also, since the only problem comes from the fact that floats like <code>1.</code> are turned into Sage <code>RealNumber</code>'s by the preparser, another possibility is to explicitely tell to keep those number unparsed, by typing : <code>1.r</code> instead of <code>1.</code>, the letter <code>r</code> standing for "raw".</p>
<p>Yet another (dirty but lazy) possiblity is to redifine Sage <code>RealNumber</code> to be Python <code>float</code>, by adding the following line at the beginning:</p>
<pre><code>sage: RealNumber=float
</code></pre>
https://ask.sagemath.org/question/24696/preparsefalse-still-not-working/?comment=24727#post-id-24727OK. It seems it is really a good idea to reset Integer as soon I'm done with the pure python code. But I don't see how to do that. Obviously, Integer=int followed by Integer=Integer isn't going to work. On the other hand, I could just leave Integer as Integer and wrap everything with int(), but that's exactly what I don't want to do.Fri, 31 Oct 2014 14:44:29 +0100https://ask.sagemath.org/question/24696/preparsefalse-still-not-working/?comment=24727#post-id-24727