parsing expressions with multiple subscripts

21 views
Skip to first unread message

Peter Brady

unread,
Dec 18, 2017, 7:01:09 PM12/18/17
to sympy
Hello all,
I'm trying to parse expressions with multiple subscripts and am running into errors.
For example, the following works:

sympy.symbol("a_{2,2}")

But parsing an expression does not:

sympy.S("a_{2,2} = a_{3,1}+2")

Can someone explain how to make this work?

Thanks,
Peter.

Aaron Meurer

unread,
Dec 19, 2017, 2:49:32 PM12/19/17
to sy...@googlegroups.com
S/sympify only work with valid Python code. A Symbol name can contain
any string, but it will only be parsable in a larger expression if it
is a valid Python name.

You can try using parse_expr from sympy.parsing.sympy_parser. It is
designed to parse some things that aren't valid Python. However, as
far as I know, it hasn't been designed to work with non-Python
variable names, so it would probably need some extension to work with
this.

The other way that always works is to wrap the name with Symbol, like
sympify("Symbol('a_{2, 2}')"). You also need do some extra things to
parse the =.

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 post to this group, send email to sy...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/1eebe626-ad4d-47bb-ab1d-5597b2745f0d%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Peter Brady

unread,
Dec 19, 2017, 2:53:35 PM12/19/17
to sy...@googlegroups.com
Thanks for clarifying Aaron.  I think I can write the symbols without the comma and brackets since only the second the index can be larger than 10.


> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/1eebe626-ad4d-47bb-ab1d-5597b2745f0d%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "sympy" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sympy/clrzC8Vvzyg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sympy+unsubscribe@googlegroups.com.

To post to this group, send email to sy...@googlegroups.com.
Visit this group at https://groups.google.com/group/sympy.
Reply all
Reply to author
Forward
0 new messages