Alternative to changing the __add__ of Integers class (need 0Z+x=x, x belonging to custom class)

asked 0 years ago

c.p. gravatar image

updated 0 years ago

I have a class called phi defined on certain ring R, which morally should be Z-valued, ϕ:RZ. From that I need only that adding 0Z to the phi-elements does nothing, and the next (pseudocode) works for adding 0 by the right to the phi elements:

class phi: ...
    ...
   def __add__(self, other):  
          if other ==  0:
                return self

How can I define the addition to be trivial if the left argument is 0Z and the right in the phi class without changing the __add__ of the Integer class? (Or a workaround. Actually my target is not the integers but the sage symbolic ring, SR.)

Preview: (hide)

Comments

2

You might want to have a look at the following entries in sagemath documentation

vdelecroix gravatar imagevdelecroix ( 0 years ago )

Thanks, I'm taking a look.

c.p. gravatar imagec.p. ( 0 years ago )