Skip to content

Commit 232f4e1

Browse files
committed
Sort factors from nmod_poly.factor
1 parent 8c66006 commit 232f4e1

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

src/flint/types/nmod_poly.pyx

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
cimport cython
12
from cpython.list cimport PyList_GET_SIZE
23
from flint.flint_base.flint_base cimport flint_poly
34
from flint.utils.typecheck cimport typecheck
@@ -47,6 +48,36 @@ cdef nmod_poly_set_list(nmod_poly_t poly, list val):
4748
else:
4849
raise TypeError("unsupported coefficient in list")
4950

51+
52+
@cython.final
53+
@cython.no_gc
54+
cdef class _nmod_poly_sort_key:
55+
cdef nmod_poly p
56+
cdef ulong mult
57+
cdef slong len
58+
def __init__(self, tuple fac_m):
59+
self.p = fac_m[0]
60+
self.len = nmod_poly_length(self.p.val)
61+
self.mult = fac_m[1]
62+
63+
def __lt__(k1, _nmod_poly_sort_key k2):
64+
cdef slong i
65+
cdef ulong c1, c2
66+
if k1.len != k2.len:
67+
return k1.len < k2.len
68+
elif k1.mult != k2.mult:
69+
return k1.mult < k2.mult
70+
i = k1.len
71+
while i >= 0:
72+
i -= 1
73+
c1 = nmod_poly_get_coeff_ui(k1.p.val, i)
74+
c2 = nmod_poly_get_coeff_ui(k2.p.val, i)
75+
if c1 != c2:
76+
return c1 < c2
77+
else:
78+
raise RuntimeError("Bad cmp in _nmod_poly_sort_key!")
79+
80+
5081
cdef class nmod_poly(flint_poly):
5182
"""
5283
The nmod_poly type represents dense univariate polynomials
@@ -670,7 +701,7 @@ cdef class nmod_poly(flint_poly):
670701
>>> nmod_poly(list(range(10)), 3).factor()
671702
(2, [(x, 1), (x + 2, 7)])
672703
>>> nmod_poly(list(range(10)), 19).factor()
673-
(9, [(x, 1), (x^4 + 15*x^3 + 2*x^2 + 7*x + 3, 1), (x^4 + 7*x^3 + 12*x^2 + 15*x + 12, 1)])
704+
(9, [(x, 1), (x^4 + 7*x^3 + 12*x^2 + 15*x + 12, 1), (x^4 + 15*x^3 + 2*x^2 + 7*x + 3, 1)])
674705
>>> nmod_poly(list(range(10)), 53).factor()
675706
(9, [(x, 1), (x^8 + 48*x^7 + 42*x^6 + 36*x^5 + 30*x^4 + 24*x^3 + 18*x^2 + 12*x + 6, 1)])
676707
@@ -680,7 +711,7 @@ cdef class nmod_poly(flint_poly):
680711
>>> nmod_poly([3,2,1,2,3], 7).factor(algorithm='berlekamp')
681712
(3, [(x + 2, 1), (x + 4, 1), (x^2 + 4*x + 1, 1)])
682713
>>> nmod_poly([3,2,1,2,3], 7).factor(algorithm='cantor-zassenhaus')
683-
(3, [(x + 4, 1), (x + 2, 1), (x^2 + 4*x + 1, 1)])
714+
(3, [(x + 2, 1), (x + 4, 1), (x^2 + 4*x + 1, 1)])
684715
685716
"""
686717
if algorithm is None:
@@ -700,7 +731,7 @@ cdef class nmod_poly(flint_poly):
700731
>>> p
701732
2*x^7 + 5*x^6 + 4*x^5 + 2*x^4 + 2*x^3 + x^2
702733
>>> p.factor_squarefree()
703-
(2, [(x^2 + 5*x, 2), (x + 1, 3)])
734+
(2, [(x + 1, 3), (x^2 + 5*x, 2)])
704735
>>> p.factor()
705736
(2, [(x, 2), (x + 5, 2), (x + 1, 3)])
706737
@@ -735,6 +766,8 @@ cdef class nmod_poly(flint_poly):
735766
exp = fac.exp[i]
736767
res[i] = (u, exp)
737768

769+
res.sort(key=_nmod_poly_sort_key)
770+
738771
c = nmod.__new__(nmod)
739772
(<nmod>c).mod = self.val.mod
740773
(<nmod>c).val = lead

0 commit comments

Comments
 (0)