# _invert_ for ring which is not integral

Suppose I have implemented my own ring MyRing which derives (among others) from CommutativeRing. MyRing is not an integral domain, so MyRing.is_integral_domain() will return False. The element class is called MyRingElement and is derived from CommutativeRingElement. All arithmetic operations on MyRingElement, like _add_, _mult_, etc., work well. Some of the elements are invertible and for those, _invert_ will return the inverse (or raise an error if no inverse exists). What is the Sage-way to call _invert_?

R = MyRing()
el = R.an_element()
~el # or 1/el or el.inverse_of_unit()


yields an error as MyRing is not an integral domain:

~/sage-9.2/local/lib/python3.8/site-packages/sage/structure/element.pyx in sage.structure.element.RingElement.__invert__ (build/cythonized/sage/structure/element.c:19405)() 2738 2739 def __invert__(self): -> 2740 return self._parent.one() / self 2741

~/sage-9.2/local/lib/python3.8/site-packages/sage/structure/element.pyx in sage.structure.element.Element.__truediv__ (build/cythonized/sage/structure/element.c:13147)() 1733 cdef int cl = classify_elements(left, right) 1734 if HAVE_SAME_PARENT(cl): -> 1735 return (<element>left)._div_(right) 1736
if BOTH_ARE_ELEMENT(cl): 1737
return coercion_model.bin_op(left, right, truediv)

~/sage-9.2/local/lib/python3.8/site-packages/sage/structure/element.pyx in sage.structure.element.RingElement._div_ (build/cythonized/sage/structure/element.c:19149)() 2732 """ 2733 try: -> 2734 frac = self._parent.fraction_field() 2735 except AttributeError: 2736
raise bin_op_exception('/', self, other)

~/sage-9.2/local/lib/python3.8/site-packages/sage/rings/ring.pyx in sage.rings.ring.CommutativeRing.fraction_field (build/cythonized/sage/rings/ring.c:12126)() 1328 1329 if not self.is_integral_domain(): -> 1330 raise TypeError("self must be an integral domain.") 1331 1332 if self.__fraction_field is not None:

TypeError: self must be an integral domain.

edit retag close merge delete

The __invert__ method needs to be defined with double underscores instead of single underscores. If you implement it, then ~el and probably el.inverse_of_unit() should work.
1/el will not work unless you implement division, but I think the convention in Sage is that this should first be coerced to a larger parent in which division is possible, but in your case such a parent does not exist.