Skip to content

Commit

Permalink
allow compose_mod and modular_composition
Browse files Browse the repository at this point in the history
  • Loading branch information
GiacomoPope committed Aug 7, 2024
1 parent d424bda commit 5fdec3b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/sage/libs/ntl/ntl_ZZ_pX.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1472,7 +1472,7 @@ cdef class ntl_ZZ_pX():
#ZZ_pX_preallocate_space(&self.x, n)
sig_off()

def modular_composition(self, ntl_ZZ_pX other, ntl_ZZ_pX modulus):
def compose_mod(self, ntl_ZZ_pX other, ntl_ZZ_pX modulus):
r"""
Compute `f(g) \pmod h`.
Expand All @@ -1490,7 +1490,7 @@ cdef class ntl_ZZ_pX():
sage: f = ntl.ZZ_pX([1,2,3],c)
sage: g = ntl.ZZ_pX([3,2,1],c)
sage: h = ntl.ZZ_pX([1,0,1],c)
sage: f.modular_composition(g, h)
sage: f.compose_mod(g, h)
[5 11]
AUTHORS:
Expand Down
6 changes: 6 additions & 0 deletions src/sage/rings/polynomial/polynomial_gf2x.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,12 @@ cdef class Polynomial_GF2X(Polynomial_template):
verbose("Res %5.3f s"%cputime(t),level=1)
return res

# Other polynomials have compose_mod as methods following the naming of
# NTL/Flint bindings but the above method predates these. We expose
# compose_mod here so all polynomial ring elements which support this can
# use either name
compose_mod = modular_composition

@cached_method
def is_irreducible(self):
r"""
Expand Down
13 changes: 8 additions & 5 deletions src/sage/rings/polynomial/polynomial_modn_dense_ntl.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ cdef class Polynomial_dense_mod_n(Polynomial):
"""
return small_roots(self, *args, **kwds)

def modular_composition(self, other, modulus):
def compose_mod(self, other, modulus):
r"""
Compute `f(g) \pmod h`.
Expand All @@ -441,16 +441,16 @@ cdef class Polynomial_dense_mod_n(Polynomial):
sage: R.<x> = GF(2**127 - 1)[]
sage: f = R.random_element()
sage: g = R.random_element()
sage: g.modular_composition(g, f) == g(g) % f
sage: g.compose_mod(g, f) == g(g) % f
True
sage: R.<x> = GF(163)[]
sage: f = R([i for i in range(100)])
sage: g = R([i**2 for i in range(100)])
sage: h = 1 + x + x**5
sage: f.modular_composition(g, h)
sage: f.compose_mod(g, h)
82*x^4 + 56*x^3 + 45*x^2 + 60*x + 127
sage: f.modular_composition(g, h) == f(g) % h
sage: f.compose_mod(g, h) == f(g) % h
True
AUTHORS:
Expand All @@ -460,9 +460,12 @@ cdef class Polynomial_dense_mod_n(Polynomial):
elt = self.ntl_ZZ_pX()
mod = modulus.ntl_ZZ_pX()
other = other.ntl_ZZ_pX()
res = elt.modular_composition(other, mod)
res = elt.compose_mod(other, mod)
return self.parent()(res, construct=True)

# compose_mod is the natural name from the NTL bindings, but polynomial_gf2x
# has modular_composition as the method name so here we allow both
modular_composition = compose_mod

def small_roots(self, X=None, beta=1.0, epsilon=None, **kwds):
r"""
Expand Down
13 changes: 9 additions & 4 deletions src/sage/rings/polynomial/polynomial_zmod_flint.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,7 @@ cdef class Polynomial_zmod_flint(Polynomial_template):
R = _single_variate(parent.base_ring(), name=name, implementation='NTL')
return parent(R(self % other).minpoly_mod(R(other)))

def modular_composition(self, other, modulus):
def compose_mod(self, other, modulus):
r"""
Compute `f(g) \pmod h`.
Expand All @@ -1007,15 +1007,15 @@ cdef class Polynomial_zmod_flint(Polynomial_template):
sage: R.<x> = GF(163)[]
sage: f = R.random_element()
sage: g = R.random_element()
sage: g.modular_composition(g, f) == g(g) % f
sage: g.compose_mod(g, f) == g(g) % f
True
sage: f = R([i for i in range(100)])
sage: g = R([i**2 for i in range(100)])
sage: h = 1 + x + x**5
sage: f.modular_composition(g, h)
sage: f.compose_mod(g, h)
82*x^4 + 56*x^3 + 45*x^2 + 60*x + 127
sage: f.modular_composition(g, h) == f(g) % h
sage: f.compose_mod(g, h) == f(g) % h
True
AUTHORS:
Expand All @@ -1029,3 +1029,8 @@ cdef class Polynomial_zmod_flint(Polynomial_template):
sig_off()

return res

# compose_mod is the natural name from the Flint bindings, but
# polynomial_gf2x has modular_composition as the method name so here we
# allow both
modular_composition = compose_mod
12 changes: 8 additions & 4 deletions src/sage/rings/polynomial/polynomial_zz_pex.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ cdef class Polynomial_ZZ_pEX(Polynomial_template):
sig_off()
return r

def modular_composition(self, other, modulus):
def compose_mod(self, other, modulus):
r"""
Compute `f(g) \pmod h`.
Expand All @@ -729,17 +729,17 @@ cdef class Polynomial_ZZ_pEX(Polynomial_template):
sage: R.<x> = GF(3**6)[]
sage: f = R.random_element()
sage: g = R.random_element()
sage: g.modular_composition(g, f) == g(g) % f
sage: g.compose_mod(g, f) == g(g) % f
True
sage: F.<z3> = GF(3**6)
sage: R.<x> = F[]
sage: f = 2*z3^2*x^2 + (z3 + 1)*x + z3^2 + 2
sage: g = (z3^2 + 2*z3)*x^2 + (2*z3 + 2)*x + 2*z3^2 + z3 + 2
sage: h = (2*z3 + 2)*x^2 + (2*z3^2 + 1)*x + 2*z3^2 + z3 + 2
sage: f.modular_composition(g, h)
sage: f.compose_mod(g, h)
(z3^5 + z3^4 + z3^3 + z3^2 + z3)*x + z3^5 + z3^3 + 2*z3 + 2
sage: f.modular_composition(g, h) == f(g) % h
sage: f.compose_mod(g, h) == f(g) % h
True
AUTHORS:
Expand Down Expand Up @@ -771,3 +771,7 @@ cdef class Polynomial_ZZ_pEX(Polynomial_template):
sig_off()

return r

# compose_mod is the natural name from the NTL bindings, but polynomial_gf2x
# has modular_composition as the method name so here we allow both
modular_composition = compose_mod

0 comments on commit 5fdec3b

Please sign in to comment.