adding attributes to sympy objects

34 views
Skip to first unread message

Nico

unread,
Jul 13, 2015, 7:22:28 AM7/13/15
to sy...@googlegroups.com
Hi everyone,

Given a symbolic expression (e.g., "laplace(u) + lambda * u), I'm parsing that expression to generate code for a particular HPC framework. Up until now, I'm using custom objects and Python's own AST module to parse the syntax tree. SymPy, however, could add functionality, so right now I'm evaluating if it's feasible for me to transition to using SymPy objects for expressions.

In the custom parser, I'm adding attributes to nodes to pass around information. For sympy objects, this is not possible since they don't have a __dict__. Is there any other way to embellish SymPy objects (such as Add, Symbol) with extra attributes?

Cheers,
Nico

Aaron Meurer

unread,
Jul 13, 2015, 1:51:09 PM7/13/15
to sy...@googlegroups.com
This is because SymPy uses __slots__ as a performance/memory optimization.

One way to do this would be to create subclasses for the objects that
have the attributes you want. There are many issues in SymPy with
things not working properly with subclasses, though (but they are bugs
and need to be fixed).

Another way might be to put them in the assumptions0 dictionary. I
think that might only support boolean attributes, though.

Of course, another option would be to store the attributes separately.

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 http://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/ee33f72a-7986-4451-8035-623086b28420%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Nico

unread,
Jul 14, 2015, 6:58:32 AM7/14/15
to sy...@googlegroups.com
Thanks for the suggestions!

I'll probably go ahead and see if I can wrap the objects where needed.

Cheers,
Nico
Reply all
Reply to author
Forward
0 new messages