Re: [sympy] Fancy ipython notebook symbols (dot, hat, bar)

2,093 views
Skip to first unread message

Ondřej Čertík

unread,
Feb 21, 2013, 3:04:50 AM2/21/13
to sy...@googlegroups.com
Hi Mat,

On Wed, Feb 20, 2013 at 8:49 AM, Mathew Topper <mathew...@ed.ac.uk> wrote:
> Hi,
>
> I'm really loving the combination of sympy with the ipython notebook, but I
> was wondering if it is possible to produce more elaborate symbols than are
> shown in the examples?
>
> I really want Edot = Symbol($\dot{E}$), for instance, but I can't get it to
> work. Is this possible and, if so, could someone show me how?

Here is an example notebook that does it:

http://nbviewer.ipython.org/5003072/

you need to use Symbol("\dot{E}").

Ondrej

Mathew Topper

unread,
Feb 21, 2013, 10:56:20 AM2/21/13
to sy...@googlegroups.com
Thanks, Ondrej! That is so simple that I feel a little embarrassed. :)

Being not afraid to ask simple questions, however, is there a method to get just a short section of the sympy output when using the notebook? I can't copy and paste, so can I output python code, instead, so that I can edit it? If you think this should be another topic, please let me know.

Thanks again,

Mat

Chris Smith

unread,
Feb 21, 2013, 11:18:21 AM2/21/13
to sy...@googlegroups.com
Just an interjection -- not sure if this might help, but there is a
capture function which will capture all printing. You can then write
this to disk or do whatever you like with the capture text:

>>> def p(): # arg-less function that involves printing
... print 'hello world!'
...
>>> s = capture(p)
>>> print s
hello world!

Mathew Topper

unread,
Feb 21, 2013, 11:34:10 AM2/21/13
to sy...@googlegroups.com
Thanks, I will give that a go. I have also found "print python(_)" which should allow me to extract the part I want.

I don't know if this is being worked on in the notebook, but it would be great to be able to copy and paste sympy output. Sounds like a pretty hard task, though!

Matthew Rocklin

unread,
Feb 21, 2013, 11:58:13 AM2/21/13
to sy...@googlegroups.com
You can always use str or latex to return a string or latex representation of the output.  These function return strings that you could save to files or whatever.  


On Thu, Feb 21, 2013 at 8:34 AM, Mathew Topper <mathew...@ed.ac.uk> wrote:
Thanks, I will give that a go. I have also found "print python(_)" which should allow me to extract the part I want.

I don't know if this is being worked on in the notebook, but it would be great to be able to copy and paste sympy output. Sounds like a pretty hard task, though!

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at http://groups.google.com/group/sympy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Aaron Meurer

unread,
Feb 21, 2013, 9:09:59 PM2/21/13
to sy...@googlegroups.com
One would have to create a special javascript object that prints as
MathJax, but when selected, gives the SymPy string form. I don't know
what the status of Javascript output type things in the notebook is
(actually, I don't even know Javascript, so I don't even know if this
is possible), so I can't say if this is possible or not. But it's
worth starting a discussion on the IPython list about. I think it
would be really cool.

Of course, selecting just part of an expression would be much harder.
To do that, you would need to have much more knowledge of how the
expression is represented by MathJax.

Regarding the Symbol name, the reason that it's just Symbol("\dot{E}")
is that the Symbol name is just printed literally in the latex output.
So the latex of something like x**2 is just {x}^{2}, and so the latex
of Symbol("\dot{E}")**2 is just {\dot{E}}^{2}. If you need to mix
string and latex printing, the symbol_names option to the latex()
function is useful (though it's currently not very easy to pass this
to IPython for MathJax).

The easiest way to get the version of a SymPy expression that you can
edit is to just print it (this will give the str for of the
expression). Assuming your variable names are the same as your symbol
names, this will give you something that you can just copy and paste*.
If they aren't, srepr() might be of use.

* The goal of str() is to give copy-pastable expressions, and to also
be readable. A key exception to the copy-paste rule is int/int (like
1/2), which will print just as that, and when you enter it, it will be
evaluated by Python as 0.5. To get around this, manually edit the
expression to use something like S(1)/2, paste the expression as a
string to sympify(), or if you are just doing stuff interactively, use
the -i option to isympy.

Aaron Meurer
Reply all
Reply to author
Forward
0 new messages