@@ -386,10 +386,6 @@ def __invert__(self):
386386 """
387387 Return the inverse of this element.
388388
389- .. WARNING::
390-
391- Only implemented when the base ring is a field.
392-
393389 EXAMPLES::
394390
395391 sage: R.<x> = QQ[]
@@ -414,35 +410,21 @@ def __invert__(self):
414410 sage: (2*y)^(-1)
415411 Traceback (most recent call last):
416412 ...
417- NotImplementedError: The base ring (=Ring of integers modulo 16) is not a field
413+ NotImplementedError
418414
419415 Check that :issue:`29469` is fixed::
420416
421417 sage: ~S(3)
422418 11
423419 """
424- if self ._polynomial .is_zero ():
425- raise ZeroDivisionError ("element %s of quotient polynomial ring not invertible" % self )
426- if self ._polynomial .is_one ():
427- return self
428-
429- parent = self .parent ()
430-
420+ P = self .parent ()
431421 try :
432- if self ._polynomial .is_unit ():
433- inv_pol = self ._polynomial .inverse_of_unit ()
434- return parent (inv_pol )
435- except (TypeError , NotImplementedError ):
436- pass
437-
438- base = parent .base_ring ()
439- if not base .is_field ():
440- raise NotImplementedError ("The base ring (=%s) is not a field" % base )
441- g , _ , a = parent .modulus ().xgcd (self ._polynomial )
442- if g .degree () != 0 :
443- raise ZeroDivisionError ("element %s of quotient polynomial ring not invertible" % self )
444- c = g [0 ]
445- return self .__class__ (self .parent (), (~ c )* a , check = False )
422+ return type (self )(P , self ._polynomial .inverse_mod (P .modulus ()), check = False )
423+ except ValueError as e :
424+ if e .args [0 ] == "Impossible inverse modulo" :
425+ raise ZeroDivisionError (f"element { self } of quotient polynomial ring not invertible" )
426+ else :
427+ raise NotImplementedError
446428
447429 def field_extension (self , names ):
448430 r"""
0 commit comments