1
+ cimport cython
2
+
1
3
from cpython.list cimport PyList_GET_SIZE
2
4
from flint.flint_base.flint_base cimport flint_poly
3
5
from flint.utils.typecheck cimport typecheck
@@ -18,9 +20,10 @@ from flint.utils.flint_exceptions import DomainError
18
20
cdef dict _nmod_poly_ctx_cache = {}
19
21
20
22
23
+ @cython.no_gc
21
24
cdef class nmod_poly_ctx:
22
25
"""
23
- Context object for creating :class:`~.nmod_poly` initalised
26
+ Context object for creating :class:`~.nmod_poly` initalised
24
27
with modulus :math:`N`.
25
28
26
29
>>> nmod_poly_ctx.new(17)
@@ -170,6 +173,7 @@ cdef class nmod_poly_ctx:
170
173
return nmod_poly(arg, self )
171
174
172
175
176
+ @cython.no_gc
173
177
cdef class nmod_poly(flint_poly):
174
178
"""
175
179
The nmod_poly type represents dense univariate polynomials
@@ -337,7 +341,7 @@ cdef class nmod_poly(flint_poly):
337
341
"""
338
342
cdef nmod_poly res
339
343
cdef slong d
340
-
344
+
341
345
if degree is not None :
342
346
d = degree
343
347
if d != degree or d < 0 :
@@ -387,7 +391,7 @@ cdef class nmod_poly(flint_poly):
387
391
"""
388
392
if n <= 0 :
389
393
raise ValueError (f" {n = } must be positive" )
390
-
394
+
391
395
if nmod_poly_get_coeff_ui(self .val, 0 ) == 0 :
392
396
raise ZeroDivisionError (f" nmod_poly inverse_series_trunc: leading coefficient is zero" )
393
397
@@ -402,7 +406,7 @@ cdef class nmod_poly(flint_poly):
402
406
"""
403
407
Returns the composition of two polynomials
404
408
405
- To be precise about the order of composition, given ``self``, and ``other``
409
+ To be precise about the order of composition, given ``self``, and ``other``
406
410
by `f(x)`, `g(x)`, returns `f(g(x))`.
407
411
408
412
>>> f = nmod_poly([1,2,3], 163)
@@ -417,15 +421,15 @@ cdef class nmod_poly(flint_poly):
417
421
if other is NotImplemented :
418
422
raise TypeError (" cannot convert input to nmod_poly" )
419
423
res = self .ctx.new_nmod_poly()
420
- nmod_poly_compose(res.val, self .val, (< nmod_poly> other).val)
421
- return res
424
+ nmod_poly_compose(res.val, self .val, (< nmod_poly> other).val)
425
+ return res
422
426
423
427
def compose_mod (self , other , modulus ):
424
428
"""
425
429
Returns the composition of two polynomials modulo a third.
426
430
427
- To be precise about the order of composition, given ``self``, and ``other``
428
- and ``modulus`` by `f(x)`, `g(x)` and `h(x)`, returns `f(g(x)) \mod h(x)`.
431
+ To be precise about the order of composition, given ``self``, and ``other``
432
+ and ``modulus`` by `f(x)`, `g(x)` and `h(x)`, returns `f(g(x)) \mod h(x)`.
429
433
We require that `h(x)` is non-zero.
430
434
431
435
>>> f = nmod_poly([1,2,3,4,5], 163)
@@ -440,17 +444,17 @@ cdef class nmod_poly(flint_poly):
440
444
g = self .ctx.any_as_nmod_poly(other)
441
445
if g is NotImplemented :
442
446
raise TypeError (f" cannot convert {other = } to nmod_poly" )
443
-
447
+
444
448
h = self .ctx.any_as_nmod_poly(modulus)
445
449
if h is NotImplemented :
446
450
raise TypeError (f" cannot convert {modulus = } to nmod_poly" )
447
-
451
+
448
452
if modulus.is_zero():
449
453
raise ZeroDivisionError (" cannot reduce modulo zero" )
450
454
451
455
res = self .ctx.new_nmod_poly()
452
- nmod_poly_compose_mod(res.val, self .val, (< nmod_poly> other).val, (< nmod_poly> modulus).val)
453
- return res
456
+ nmod_poly_compose_mod(res.val, self .val, (< nmod_poly> other).val, (< nmod_poly> modulus).val)
457
+ return res
454
458
455
459
def __call__ (self , other ):
456
460
cdef nmod_poly r
@@ -638,8 +642,7 @@ cdef class nmod_poly(flint_poly):
638
642
>>> f = 30*x**6 + 104*x**5 + 76*x**4 + 33*x**3 + 70*x**2 + 44*x + 65
639
643
>>> g = 43*x**6 + 91*x**5 + 77*x**4 + 113*x**3 + 71*x**2 + 132*x + 60
640
644
>>> mod = x**4 + 93*x**3 + 78*x**2 + 72*x + 149
641
- >>>
642
- >>> f.pow_mod(123, mod)
645
+ >>> f.pow_mod(123, mod)
643
646
3*x^3 + 25*x^2 + 115*x + 161
644
647
>>> f.pow_mod(2**64, mod)
645
648
52*x^3 + 96*x^2 + 136*x + 9
@@ -663,7 +666,7 @@ cdef class nmod_poly(flint_poly):
663
666
664
667
# Output polynomial
665
668
res = self .ctx.new_nmod_poly()
666
-
669
+
667
670
# For small exponents, use a simple binary exponentiation method
668
671
if e.bit_length() < 32 :
669
672
nmod_poly_powmod_ui_binexp(
0 commit comments