Alternative to changing the __add__ of Integers class (need $0_{\mathbb Z} + x = x$, $x$ belonging to custom class)

asked 2024-07-18 08:19:20 +0100

c.p. gravatar image

updated 2024-07-18 12:58:21 +0100

I have a class called phi defined on certain ring $R$, which morally should be $\mathbb Z$-valued, $\phi: R \to \mathbb Z$. From that I need only that adding $0_{\mathbb Z}$ 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 $0_\mathbb Z$ 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.)

edit retag flag offensive close merge delete

Comments

2

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

vdelecroix gravatar imagevdelecroix ( 2024-07-18 13:01:39 +0100 )edit

Thanks, I'm taking a look.

c.p. gravatar imagec.p. ( 2024-07-18 15:07:13 +0100 )edit