Ask Your Question
0

How to get the same environment in "sage -ipython notebook" as in "sage"?

asked 2014-01-08 07:58:50 +0100

anonymous user

Anonymous

updated 2014-07-08 23:26:18 +0100

slelievre gravatar image

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.


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.


In other words, it is clear that sage -ipython notebook does not provide the same environment as sage.

How can I fix this? I just want an IPython notebook that works like my sage command line.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2014-01-08 08:55:33 +0100

tmonteil gravatar image

updated 2014-01-08 08:57:20 +0100

I 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

edit flag offensive delete link more

Comments

This 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?

testtest2 gravatar imagetesttest2 ( 2014-01-08 09:21:35 +0100 )edit
4

answered 2014-01-08 08:48:25 +0100

ppurka gravatar image

Well 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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2014-01-08 07:58:50 +0100

Seen: 1,192 times

Last updated: Jan 08 '14