How should existing functions be overridden for new symbols?

22 views
Skip to first unread message

Michael O'Connor

unread,
Oct 30, 2015, 9:14:54 AM10/30/15
to sympy
Hi,

I'm quite new to Sympy, so this is probably simple, but I couldn't find it in the documentation.  The documentation does a good job of explaining how you can define a new function, and specify how it should reduce on certain arguments.  But I couldn't see how to define new symbols and declare how they should act with respect to +, *, **, ...

For example, I was thinking of trying to implement ordinal arithmetic.  In that case, I would have, for example, new symbols omega and epsilon0, and would like omega**epsilon0 to evaluate to epsilon0.  Can that be accomplished?

Thanks!

Aaron Meurer

unread,
Oct 30, 2015, 11:55:03 AM10/30/15
to sy...@googlegroups.com
You should subclass Expr and define certain methods. For **, you
should define _eval_power(self, expt) on the omega object.

For + and *, there are no direct ways to interface with Add and Mul,
but you can define __add__ and __mul__ to define the Python operators
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 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/47e6cc6a-f6dd-456e-9d4a-66ebeb9ab5ec%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Michael O'Connor

unread,
Oct 30, 2015, 5:36:53 PM10/30/15
to sympy
Thank you!
Reply all
Reply to author
Forward
0 new messages