Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a proper way to set compound greek letters as a symbol in SymPy?

As silly as it may sound, I would like to use compound greek letters as a single symbol in SymPy. For example, if the following is entered in a Jupyter notebook:

import sympy as sp
ab = sp.Symbol("alpha beta")
sp.pprint(ab)

ab behaves as desired when used in symbolic manipulations, but the output is:

alpha beta

I would like the output to be:

α⋅β

I could use the subs command after manipulations, like so:

ab.subs({ab : sp.Symbol("alpha") * sp.Symbol("beta")})

but this is tedious and undesirable.

like image 966
Andrew Franklin Avatar asked Jun 09 '16 16:06

Andrew Franklin


1 Answers

Symbol names can be any string, but the automatic conversion of greek letter names to greek letters for printing doesn't work for all input. I guess it doesn't try to split the string in spaces.

If you are using the Jupyter notebook, you can just set the symbol name to be the LaTeX of what you want

ab = Symbol(r'\alpha\cdot\beta')

(don't forget to prefix the string with r, so that Python doesn't eat the backslashes)

If you are using plain text output, you can set it to the Unicode string. This should work in the Jupyter notebook as well, although it will render slightly differently since it will be rendering the actual Unicode characters instead of the LaTeX.

ab = Symbol(u'α⋅β')
like image 94
asmeurer Avatar answered Sep 24 '22 16:09

asmeurer