modifying symbols

32 views
Skip to first unread message

Thomas Ligon

unread,
Feb 10, 2020, 5:59:51 AM2/10/20
to sympy
I need to create a large number of equations in different variations, and they involve symbols such as 
E1m1 = symbols('E_{1\,-1}')
Since subst() does not work for symbols, I create the string 'E1m1' as
('E'+str(1)+str(-1)).replace('-', 'm')
and then use symbols(str). This is working fine for most cases, but not all. Specifically, E11 and E10 work fine, but E1m1 fails. Here is the output of the expression and its latex variant.
Eq(0, E10*a0*am1 + E11*a0*a1 + E1m1*am1*am2 + F1*a0**2 + 2*F1*a1*am1 + G1*a0*am2 + G1*a1*am3 + G1*am1**2)
0 = E_{10} a_{0} am_{1} + E_{11} a_{0} a_{1} + E1m1 am_{1} am_{2} + F_{1} a_{0}^{2} + 2 F_{1} a_{1} am_{1} + G_{1} a_{0} am_{2} + G_{1} a_{1} am_{3} + G_{1} am_{1}^{2}
I have attached a test program in hopes that it will make things clearer.

Thomas Ligon

unread,
Feb 10, 2020, 6:06:28 AM2/10/20
to sympy
In case I forgot the attachment...

symbolTest.py

Thomas Ligon

unread,
Feb 10, 2020, 1:35:09 PM2/10/20
to sympy
I just discovered something. Instead of symbols(str), I should be using sympify(str), but that doesn't seem to change anything.


Aaron Meurer

unread,
Feb 10, 2020, 1:40:01 PM2/10/20
to sympy
What is subst()?

Is the problem here that SymPy does not convert E1m1 into a LaTeX
string with subscripts? You can use _ in the symbol name to indicate
that part of it should be subscripted. For example

>>> latex(Symbol("E_1m1"))
'E_{1m1}'

Aaron Meurer

On Mon, Feb 10, 2020 at 11:35 AM Thomas Ligon <thomas...@gmail.com> wrote:
>
> I just discovered something. Instead of symbols(str), I should be using sympify(str), but that doesn't seem to change anything.
>>
>>
>>
> --
> 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 view this discussion on the web visit https://groups.google.com/d/msgid/sympy/dae77559-feca-4bba-8f09-899b24df1103%40googlegroups.com.

Thomas Ligon

unread,
Feb 10, 2020, 3:52:43 PM2/10/20
to sympy
Hello Aaron,
thanks for the reply. First, subst() is a typo, and should be subs(), but I'm not using it here anyway.
The test program contains this definition:
E1m1 = symbols('E_{1\,-1}')
so you can see that I am using the _ in it. The problem is the conversion to latex, which recognizes E11, converting it the way I need, and does not recognize/convert E1m1.
In any case your response has helped me see a bit clearer. I use E1m1 in the code for expressions, and want it to appear as 'E_{1\,-1}' in latex. That raises the question of how I should do this when I call sympify. In the line of code above, both forms appear E1m1 and 'E_{1\,-1}', but not in sympify.
The next thing I will try is the following:
In the routine tripLet, I will create both strings, the symbol (e.g. E1m1) and the latex display version (e.g. 'E_{1\,-1}'), then issue the call to symbols(), and then combine the symbols to create and return the expression.


Aaron Meurer

unread,
Feb 10, 2020, 6:50:00 PM2/10/20
to sympy
sympify() can only parse things that are valid Python, meaning it can
only work with symbol names that are valid Python variable names.
E_1m1 is a valid Python variable name but E_{1\,-1} is not. If you
need to get a parsable string form for an expression you can use
srepr(), which would create a string with Symbol in it directly, like
Symbol('E_{1\\,-1{'), which can be parsed by sympify(). If your
symbols name is already LaTeX the latex() printer will just include it
directly.

Aaron Meurer
> --
> 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 view this discussion on the web visit https://groups.google.com/d/msgid/sympy/4a42d8e5-9ae9-4faa-a797-05db5aa0d4a2%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages