Skip to content

Commit

Permalink
gh-35455: Faster get_unsafe for NTL GF(p) polynomials
Browse files Browse the repository at this point in the history
    
### 📚 Description

Unlike similar Zmod(n) polynomials, the GF(p) NTL polynomials would call
the IntegerModRing constructor for each get_unsafe call resulting in
very slow polynomial evaluation.


Benchmarks:
```
p = next_prime(2**80)
pol = GF(p)["x"].random_element(80000)
# Before
# evaluate pol at a random GF(p) element: 260.6ms
# evaluate pol at a random GF(p^2) element: 790.1ms
# evaluate pol at a random GF(p^5) element: 1155.8ms
# After
# evaluate pol at a random GF(p) element: 66.8ms
# evaluate pol at a random GF(p^2) element: 541.2ms
# evaluate pol at a random GF(p^5) element: 889.9ms
```

### 📝 Checklist

- [x] The title is concise, informative, and self-explanatory.
- [x] The description explains in detail what this PR is about.
- [ ] I have linked a relevant issue or discussion.
- [ ] I have created tests covering the changes.
- [ ] I have updated the documentation accordingly.
    
URL: #35455
Reported by: Rémy Oudompheng
Reviewer(s): Marc Mezzarobba, Rémy Oudompheng
  • Loading branch information
Release Manager committed Apr 12, 2023
2 parents 266e61f + bf927cc commit 7ecf985
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 3 deletions.
2 changes: 0 additions & 2 deletions src/sage/libs/ntl/ntl_ZZ_pX.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,7 @@ cdef class ntl_ZZ_pX():
if i < 0:
r.set_from_int(0)
else:
sig_on()
r.x = ZZ_pX_coeff( self.x, i)
sig_off()
return r

cdef int getitem_as_int(ntl_ZZ_pX self, long i):
Expand Down
2 changes: 1 addition & 1 deletion src/sage/rings/polynomial/polynomial_modn_dense_ntl.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ cdef class Polynomial_dense_mod_n(Polynomial):
sage: f[:3]
13*x^2 + 10*x + 5
"""
return self._parent._base(self.__poly[n]._sage_())
return self._parent._base((<ntl_ZZ_pX> self.__poly)[n]._integer_())

def _unsafe_mutate(self, n, value):
n = int(n)
Expand Down

0 comments on commit 7ecf985

Please sign in to comment.