ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 18 Jul 2024 15:07:13 +0200Alternative to changing the __add__ of Integers class (need $0_{\mathbb Z} + x = x$, $x$ belonging to custom class)https://ask.sagemath.org/question/78322/alternative-to-changing-the-__add__-of-integers-class-need-0_mathbb-z-x-x-x-belonging-to-custom-class/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.)Thu, 18 Jul 2024 08:19:20 +0200https://ask.sagemath.org/question/78322/alternative-to-changing-the-__add__-of-integers-class-need-0_mathbb-z-x-x-x-belonging-to-custom-class/Comment by c.p. for <p>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:</p>
<pre><code>class phi: ...
...
def __add__(self, other):
if other == 0:
return self
</code></pre>
<p>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.)</p>
https://ask.sagemath.org/question/78322/alternative-to-changing-the-__add__-of-integers-class-need-0_mathbb-z-x-x-x-belonging-to-custom-class/?comment=78329#post-id-78329Thanks, I'm taking a look.Thu, 18 Jul 2024 15:07:13 +0200https://ask.sagemath.org/question/78322/alternative-to-changing-the-__add__-of-integers-class-need-0_mathbb-z-x-x-x-belonging-to-custom-class/?comment=78329#post-id-78329Comment by vdelecroix for <p>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:</p>
<pre><code>class phi: ...
...
def __add__(self, other):
if other == 0:
return self
</code></pre>
<p>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.)</p>
https://ask.sagemath.org/question/78322/alternative-to-changing-the-__add__-of-integers-class-need-0_mathbb-z-x-x-x-belonging-to-custom-class/?comment=78328#post-id-78328You might want to have a look at the following entries in sagemath documentation
- [Parents, conversion and coercion](https://doc.sagemath.org/html/en/tutorial/tour_coercion.html)
- [How to implement new algebraic structures in Sage](https://doc.sagemath.org/html/en/thematic_tutorials/coercion_and_categories.html)Thu, 18 Jul 2024 13:01:39 +0200https://ask.sagemath.org/question/78322/alternative-to-changing-the-__add__-of-integers-class-need-0_mathbb-z-x-x-x-belonging-to-custom-class/?comment=78328#post-id-78328