Open cyrilbouvier opened 10 years ago
For the record, when I try this in 7.3, the error reported is different (but this doesn't change the underlying issue with the documentation):
...
sage: sage: (t^2+1).inverse_mod(t^2)
---------------------------------------------------------------------------
NotImplementedError Traceback (most recent call last)
<ipython-input-8-4ae1aed5e4de> in <module>()
----> 1 (t**Integer(2)+Integer(1)).inverse_mod(t**Integer(2))
/home/kedlaya/sage-complete/src/sage/rings/polynomial/polynomial_element.pyx in sage.rings.polynomial.polynomial_element.Polynomial.inverse_mod (/home/kedlaya/sage-complete/src/build/cythonized/sage/rings/polynomial/polynomial_element.c:14109)()
1346 if a.parent().is_exact():
1347 # use xgcd
-> 1348 g, s, _ = a.xgcd(m)
1349 if g == 1:
1350 return s
/home/kedlaya/sage-complete/src/sage/structure/element.pyx in sage.structure.element.NamedBinopMethod.__call__ (/home/kedlaya/sage-complete/src/build/cythonized/sage/structure/element.c:26637)()
3438 return getattr(x, self._name)(y, **kwds)
3439 else:
-> 3440 return self._func(x,y, **kwds)
3441
3442 def __get__(self, obj, objtype):
/home/kedlaya/sage-complete/src/sage/rings/polynomial/polynomial_element.pyx in sage.rings.polynomial.polynomial_element.Polynomial.xgcd (/home/kedlaya/sage-complete/src/build/cythonized/sage/rings/polynomial/polynomial_element.c:63301)()
7359 return self.base_ring()._xgcd_univariate_polynomial(self, other)
7360 else:
-> 7361 raise NotImplementedError("%s does not provide an xgcd implementation for univariate polynomials"%self.base_ring())
7362
7363 def variables(self):
NotImplementedError: Ring of integers modulo 42 does not provide an xgcd implementation for univariate polynomials
Author: Mark Saaltink
Commit: 87499b2
New commits:
87499b2 | Fix inverse_mod for univariate polynomials over ZZ mod n for composite n. |
ntl has its own implementation of invmod, so we use should use that, note also that ntl allows to use xgcd ever for composite n.
sage: from sage.libs.ntl.ntl_ZZ_pX import ntl_ZZ_pContext, ntl_ZZ_pX
sage: c = ntl_ZZ_pContext(42)
sage: f = ntl_ZZ_pX([1, 0, 1],c)
sage: g = ntl_ZZ_pX([0, 0, 1],c)
sage: f.xgcd(g)
([1], [1], [41])
there is also invmod but it seems that does not work right now in my computer...
update milestone 8.3 -> 8.4
Calculating xgcd of polynomials using NTL implementation still doesn't work in Sage 9.2.
Code:
print(version())
K.<t> = PolynomialRing(IntegerModRing(42), 't', implementation='NTL')
print((t^2+1).inverse_mod(t^2))
Output:
SageMath version 9.2, Release Date: 2020-10-24
---------------------------------------------------------------------------
NotImplementedError Traceback (most recent call last)
<ipython-input-1-b06a1f7d9362> in <module>
1 print(version())
2 K = PolynomialRing(IntegerModRing(Integer(42)), 't', implementation='NTL', names=('t',)); (t,) = K._first_ngens(1)
----> 3 print((t**Integer(2)+Integer(1)).inverse_mod(t**Integer(2)))
/home/sc_serv/sage/local/lib/python3.8/site-packages/sage/rings/polynomial/polynomial_element.pyx in sage.rings.polynomial.polynomial_element.Polynomial.inverse_mod (build/cythonized/sage/rings/polynomial/polynomial_element.c:15403)()
1490 if a.parent().is_exact():
1491 # use xgcd
-> 1492 g, s, _ = a.xgcd(m)
1493 if g == 1:
1494 return s
/home/sc_serv/sage/local/lib/python3.8/site-packages/sage/structure/element.pyx in sage.structure.element.coerce_binop.new_method (build/cythonized/sage/structure/element.c:27687)()
4347 def new_method(self, other, *args, **kwargs):
4348 if have_same_parent(self, other):
-> 4349 return method(self, other, *args, **kwargs)
4350 else:
4351 a, b = coercion_model.canonical_coercion(self, other)
/home/sc_serv/sage/local/lib/python3.8/site-packages/sage/rings/polynomial/polynomial_element.pyx in sage.rings.polynomial.polynomial_element.Polynomial.xgcd (build/cythonized/sage/rings/polynomial/polynomial_element.c:70376)()
8503 return self.base_ring()._xgcd_univariate_polynomial(self, other)
8504 else:
-> 8505 raise NotImplementedError("%s does not provide an xgcd implementation for univariate polynomials"%self.base_ring())
8506
8507 def rational_reconstruct(self, m, n_deg=None, d_deg=None):
NotImplementedError: Ring of integers modulo 42 does not provide an xgcd implementation for univariate polynomials
In sage 6.0, dense polynomials over Z/nZ with n composite raise an AttributeError about missing attribute xgcd when inverse_mod is called. Here is an example:
This works for polynomials ring over Z/nZ that use FLINT (so only for small n) or that use NTL but with prime n. The buggy behavior is that sage indicates that inverse_mod attribute should exists :
Cyril
Component: basic arithmetic
Author: Mark Saaltink
Branch/Commit: u/msaaltink/dense_polynomials_over_z_nz_with_n_composite_and_using_ntlfailed_to_execute_inverse_mod @
87499b2
Issue created by migration from https://trac.sagemath.org/ticket/15788