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.Wed, 08 Jan 2014 09:21:35 +0100How to get the same environment in "sage -ipython notebook" as in "sage"?https://ask.sagemath.org/question/10894/how-to-get-the-same-environment-in-sage-ipython-notebook-as-in-sage/If I start the command line version of sage with **sage** and type
integrate(x^2,x)
I get
1/3*x^3
as desired.
<br>
However, if I start sage in IPython's notebook with **sage -ipython notebook** and type the same thing I get
NameError: name 'integrate' is not defined
Trying to fix this by defining some prerequisites
from sage.all import *
,var x
I now get
RuntimeError: Use ** for exponentiation, not '^', which means xor
in Python, and has the wrong precedence.
<br>
**In other words, it is clear that *sage -ipython notebook* does not provide the same environment as *sage*.**
<h1>How can I fix this? I just want an IPython notebook that works like my sage command line.</h1>Wed, 08 Jan 2014 07:58:50 +0100https://ask.sagemath.org/question/10894/how-to-get-the-same-environment-in-sage-ipython-notebook-as-in-sage/Answer by ppurka for <p>If I start the command line version of sage with <strong>sage</strong> and type</p>
<pre><code>integrate(x^2,x)
</code></pre>
<p>I get</p>
<pre><code>1/3*x^3
</code></pre>
<p>as desired.</p>
<p><br/></p>
<p>However, if I start sage in IPython's notebook with <strong>sage -ipython notebook</strong> and type the same thing I get</p>
<pre><code>NameError: name 'integrate' is not defined
</code></pre>
<p>Trying to fix this by defining some prerequisites</p>
<pre><code>from sage.all import *
,var x
</code></pre>
<p>I now get</p>
<pre><code>RuntimeError: Use ** for exponentiation, not '^', which means xor
in Python, and has the wrong precedence.
</code></pre>
<p><br/></p>
<p><strong>In other words, it is clear that <em>sage -ipython notebook</em> does not provide the same environment as <em>sage</em>.</strong></p>
<h1>How can I fix this? I just want an IPython notebook that works like my sage command line.</h1>
https://ask.sagemath.org/question/10894/how-to-get-the-same-environment-in-sage-ipython-notebook-as-in-sage/?answer=15890#post-id-15890Well yes, of course. The sage environment uses a preparser. That is why the sage command line is not the same as the ipython command line. You can see what the preparser does by running it manually, like this:
sage: preparse('integrate(x^2,x)')
'integrate(x**Integer(2),x)'
As you can see `**` is the actual python command for taking powers. Whereas `^` does only xor operation. Similarly, the number `2` is not of type `'int'` anymore. It is actually of type `'Integer'`, which is a sage object. The difference is quite stark - in particular, fractions are automatically changed to type `QQ` in sage, but not in ipython:
sage: preparser(False) # Turn off preparser
sage: 1/2
0
sage: preparser(True) # This is the default when you start sage
sage: 1/2
1/2
If you want to use sage commands inside ipython then you have to stick to python commands. You can use sage commands, but not everything will automatically work unless coerced/typecast into the correct type. You can get more examples and [information here](http://www.sagemath.org/doc/reference/misc/sage/misc/preparser.html).Wed, 08 Jan 2014 08:48:25 +0100https://ask.sagemath.org/question/10894/how-to-get-the-same-environment-in-sage-ipython-notebook-as-in-sage/?answer=15890#post-id-15890Answer by tmonteil for <p>If I start the command line version of sage with <strong>sage</strong> and type</p>
<pre><code>integrate(x^2,x)
</code></pre>
<p>I get</p>
<pre><code>1/3*x^3
</code></pre>
<p>as desired.</p>
<p><br/></p>
<p>However, if I start sage in IPython's notebook with <strong>sage -ipython notebook</strong> and type the same thing I get</p>
<pre><code>NameError: name 'integrate' is not defined
</code></pre>
<p>Trying to fix this by defining some prerequisites</p>
<pre><code>from sage.all import *
,var x
</code></pre>
<p>I now get</p>
<pre><code>RuntimeError: Use ** for exponentiation, not '^', which means xor
in Python, and has the wrong precedence.
</code></pre>
<p><br/></p>
<p><strong>In other words, it is clear that <em>sage -ipython notebook</em> does not provide the same environment as <em>sage</em>.</strong></p>
<h1>How can I fix this? I just want an IPython notebook that works like my sage command line.</h1>
https://ask.sagemath.org/question/10894/how-to-get-the-same-environment-in-sage-ipython-notebook-as-in-sage/?answer=15892#post-id-15892I agree with @ppurka explanation (i was going to answer along the same lines before his/her solution appears ;) and the workaround to have the preparser work in a ipython shell (or notebook) is to type:
%load_ext sage.misc.sage_extension
as suggested [here](http://ask.sagemath.org/question/3142/is-it-possible-to-run-a-sage-in-ipython-notebook)
Wed, 08 Jan 2014 08:55:33 +0100https://ask.sagemath.org/question/10894/how-to-get-the-same-environment-in-sage-ipython-notebook-as-in-sage/?answer=15892#post-id-15892Comment by testtest2 for <p>I agree with <a href="/users/1186/ppurka/">@ppurka</a> explanation (i was going to answer along the same lines before his/her solution appears ;) and the workaround to have the preparser work in a ipython shell (or notebook) is to type:</p>
<pre><code>%load_ext sage.misc.sage_extension
</code></pre>
<p>as suggested <a href="http://ask.sagemath.org/question/3142/is-it-possible-to-run-a-sage-in-ipython-notebook">here</a></p>
https://ask.sagemath.org/question/10894/how-to-get-the-same-environment-in-sage-ipython-notebook-as-in-sage/?comment=16469#post-id-16469This worked, thank you! One more problem though: The results are not pretty-printed using MathJax automatically, and if I surround the command with "pretty_print(...)", it prints the HTML code rather than rendering it. How can I activate MathJax rendering by default?Wed, 08 Jan 2014 09:21:35 +0100https://ask.sagemath.org/question/10894/how-to-get-the-same-environment-in-sage-ipython-notebook-as-in-sage/?comment=16469#post-id-16469