Skip to content

Commit 59aafbd

Browse files
committed
Add DOCTESTS and fix some pycodestyle problems
1 parent 9e05a60 commit 59aafbd

File tree

1 file changed

+84
-40
lines changed

1 file changed

+84
-40
lines changed

src/sage/rings/polynomial/polynomial_zz_pex.pyx

Lines changed: 84 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ AUTHOR:
1616
from cysignals.signals cimport sig_on, sig_off
1717

1818
from sage.libs.ntl.ntl_ZZ_pEContext cimport ntl_ZZ_pEContext_class
19+
from sage.libs.ntl.ntl_ZZ_pE cimport ntl_ZZ_pE
1920
from sage.libs.ntl.ZZ_pE cimport ZZ_pE_to_ZZ_pX
2021
from sage.libs.ntl.ZZ_pX cimport ZZ_pX_deg, ZZ_pX_coeff
2122
from sage.libs.ntl.ZZ_p cimport ZZ_p_rep
@@ -25,23 +26,22 @@ from sage.libs.ntl.convert cimport ZZ_to_mpz
2526
# to make sure the function get_cparent is found since it is used in
2627
# 'polynomial_template.pxi'.
2728

28-
cdef cparent get_cparent(parent) except? NULL:
29+
cdef cparent get_cparent(parent) except ? NULL:
2930
if parent is None:
3031
return NULL
3132
cdef ntl_ZZ_pEContext_class pec
3233
try:
3334
pec = parent._modulus
3435
except AttributeError:
3536
return NULL
36-
return &(pec.ptrs)
37+
return & (pec.ptrs)
3738

3839
# first we include the definitions
3940
include "sage/libs/ntl/ntl_ZZ_pEX_linkage.pxi"
4041

4142
# and then the interface
4243
include "polynomial_template.pxi"
4344

44-
from sage.libs.ntl.ntl_ZZ_pE cimport ntl_ZZ_pE
4545

4646
cdef inline ZZ_pE_c_to_list(ZZ_pE_c x):
4747
cdef list L = []
@@ -52,12 +52,12 @@ cdef inline ZZ_pE_c_to_list(ZZ_pE_c x):
5252

5353
c_pX = ZZ_pE_to_ZZ_pX(x)
5454
d = ZZ_pX_deg(c_pX)
55-
if d>=0:
55+
if d >= 0:
5656
for 0 <= j <= d:
5757
c_p = ZZ_pX_coeff(c_pX, j)
5858
c_c = ZZ_p_rep(c_p)
5959
ans = Integer.__new__(Integer)
60-
ZZ_to_mpz(ans.value, &c_c)
60+
ZZ_to_mpz(ans.value, & c_c)
6161
L.append(ans)
6262
return L
6363

@@ -73,6 +73,7 @@ cdef class Polynomial_ZZ_pEX(Polynomial_template):
7373
sage: (x^3 + a*x^2 + 1) * (x + a)
7474
x^4 + 2*a*x^3 + a^2*x^2 + x + a
7575
"""
76+
7677
def __init__(self, parent, x=None, check=True, is_gen=False, construct=False):
7778
r"""
7879
Create a new univariate polynomials over `\GF{p^n}`.
@@ -124,8 +125,8 @@ cdef class Polynomial_ZZ_pEX(Polynomial_template):
124125
try:
125126
if (x.parent() is parent.base_ring()) or (x.parent() == parent.base_ring()):
126127
Polynomial.__init__(self, parent, is_gen=is_gen)
127-
(<Polynomial_template>self)._cparent = get_cparent(parent)
128-
celement_construct(&self.x, (<Polynomial_template>self)._cparent)
128+
( < Polynomial_template > self)._cparent = get_cparent(parent)
129+
celement_construct(&self.x, ( < Polynomial_template > self)._cparent)
129130
d = parent._modulus.ZZ_pE(list(x.polynomial()))
130131
ZZ_pEX_SetCoeff(self.x, 0, d.x)
131132
return
@@ -137,10 +138,10 @@ cdef class Polynomial_ZZ_pEX(Polynomial_template):
137138

138139
if isinstance(x, (list, tuple)):
139140
Polynomial.__init__(self, parent, is_gen=is_gen)
140-
(<Polynomial_template>self)._cparent = get_cparent(parent)
141-
celement_construct(&self.x, (<Polynomial_template>self)._cparent)
141+
( < Polynomial_template > self)._cparent = get_cparent(parent)
142+
celement_construct(&self.x, ( < Polynomial_template > self)._cparent)
142143
K = parent.base_ring()
143-
for i,e in enumerate(x):
144+
for i, e in enumerate(x):
144145
# self(x) is supposed to be a conversion,
145146
# not necessarily a coercion. So, we must
146147
# not do K.coerce(e) but K(e).
@@ -195,7 +196,7 @@ cdef class Polynomial_ZZ_pEX(Polynomial_template):
195196

196197
K = self._parent.base_ring()
197198
return [K(ZZ_pE_c_to_list(ZZ_pEX_coeff(self.x, i)))
198-
for i in range(celement_len(&self.x, (<Polynomial_template>self)._cparent))]
199+
for i in range(celement_len(&self.x, ( < Polynomial_template > self)._cparent))]
199200

200201
cpdef _lmul_(self, Element left):
201202
r"""
@@ -211,9 +212,9 @@ cdef class Polynomial_ZZ_pEX(Polynomial_template):
211212
cdef ntl_ZZ_pE d
212213
cdef Polynomial_ZZ_pEX r
213214
r = Polynomial_ZZ_pEX.__new__(Polynomial_ZZ_pEX)
214-
celement_construct(&r.x, (<Polynomial_template>self)._cparent)
215-
r._parent = (<Polynomial_template>self)._parent
216-
r._cparent = (<Polynomial_template>self)._cparent
215+
celement_construct(&r.x, ( < Polynomial_template > self)._cparent)
216+
r._parent = ( < Polynomial_template > self)._parent
217+
r._cparent = ( < Polynomial_template > self)._cparent
217218
d = self._parent._modulus.ZZ_pE(list(left.polynomial()))
218219
ZZ_pEX_mul_ZZ_pE(r.x, self.x, d.x)
219220
return r
@@ -262,15 +263,15 @@ cdef class Polynomial_ZZ_pEX(Polynomial_template):
262263

263264
if kwds:
264265
if x:
265-
raise TypeError("%s__call__() takes exactly 1 argument"%type(self))
266+
raise TypeError("%s__call__() takes exactly 1 argument" % type(self))
266267
try:
267268
x = [kwds.pop(self.variable_name())]
268269
except KeyError:
269270
pass
270271
if kwds:
271-
raise TypeError("%s__call__() accepts no named argument except '%s'"%(type(self),self.variable_name()))
272-
if len(x)!=1:
273-
raise TypeError("%s__call__() takes exactly 1 positional argument"%type(self))
272+
raise TypeError("%s__call__() accepts no named argument except '%s'" % (type(self), self.variable_name()))
273+
if len(x) != 1:
274+
raise TypeError("%s__call__() takes exactly 1 positional argument" % type(self))
274275

275276
a = x[0]
276277
try:
@@ -310,7 +311,7 @@ cdef class Polynomial_ZZ_pEX(Polynomial_template):
310311
if other.parent() is not self._parent:
311312
other = self._parent.coerce(other)
312313

313-
ZZ_pEX_resultant(r, self.x, (<Polynomial_ZZ_pEX>other).x)
314+
ZZ_pEX_resultant(r, self.x, ( < Polynomial_ZZ_pEX > other).x)
314315

315316
K = self._parent.base_ring()
316317
return K(K.polynomial_ring()(ZZ_pE_c_to_list(r)))
@@ -349,15 +350,15 @@ cdef class Polynomial_ZZ_pEX(Polynomial_template):
349350
False
350351
"""
351352
self._parent._modulus.restore()
352-
if algorithm=="fast_when_false":
353+
if algorithm == "fast_when_false":
353354
sig_on()
354355
res = ZZ_pEX_IterIrredTest(self.x)
355356
sig_off()
356-
elif algorithm=="fast_when_true":
357+
elif algorithm == "fast_when_true":
357358
sig_on()
358359
res = ZZ_pEX_DetIrredTest(self.x)
359360
sig_off()
360-
elif algorithm=="probabilistic":
361+
elif algorithm == "probabilistic":
361362
sig_on()
362363
res = ZZ_pEX_ProbIrredTest(self.x, iter)
363364
sig_off()
@@ -402,11 +403,11 @@ cdef class Polynomial_ZZ_pEX(Polynomial_template):
402403

403404
cdef Polynomial_ZZ_pEX r
404405
r = Polynomial_ZZ_pEX.__new__(Polynomial_ZZ_pEX)
405-
celement_construct(&r.x, (<Polynomial_template>self)._cparent)
406-
r._parent = (<Polynomial_template>self)._parent
407-
r._cparent = (<Polynomial_template>self)._cparent
406+
celement_construct(&r.x, ( < Polynomial_template > self)._cparent)
407+
r._parent = ( < Polynomial_template > self)._parent
408+
r._cparent = ( < Polynomial_template > self)._cparent
408409

409-
ZZ_pEX_MinPolyMod(r.x, (<Polynomial_ZZ_pEX>(self % other)).x, (<Polynomial_ZZ_pEX>other).x)
410+
ZZ_pEX_MinPolyMod(r.x, ( < Polynomial_ZZ_pEX > (self % other)).x, ( < Polynomial_ZZ_pEX > other).x)
410411
return r
411412

412413
cpdef _richcmp_(self, other, int op):
@@ -452,9 +453,9 @@ cdef class Polynomial_ZZ_pEX(Polynomial_template):
452453
self._parent._modulus.restore()
453454
cdef Polynomial_ZZ_pEX r
454455
r = Polynomial_ZZ_pEX.__new__(Polynomial_ZZ_pEX)
455-
celement_construct(&r.x, (<Polynomial_template>self)._cparent)
456-
r._parent = (<Polynomial_template>self)._parent
457-
r._cparent = (<Polynomial_template>self)._cparent
456+
celement_construct(&r.x, ( < Polynomial_template > self)._cparent)
457+
r._parent = ( < Polynomial_template > self)._parent
458+
r._cparent = ( < Polynomial_template > self)._cparent
458459
ZZ_pEX_LeftShift(r.x, self.x, n)
459460
return r
460461

@@ -502,26 +503,46 @@ cdef class Polynomial_ZZ_pEX(Polynomial_template):
502503
4*x^15 + 32*x^9 + 11*x^5 + x^2
503504
sage: f.reverse(degree=2)
504505
4*x^2
506+
507+
TESTS::
508+
509+
sage: R.<x> = GF(163^2)[]
510+
sage: f.reverse(degree=10)
511+
2*x^10 + 3*x^9
512+
sage: f = R([p for p in primes(20)])
513+
sage: f.reverse()
514+
2*x^7 + 3*x^6 + 5*x^5 + 7*x^4 + 11*x^3 + 13*x^2 + 17*x + 19
515+
sage: f.reverse(degree=200)
516+
2*x^200 + 3*x^199 + 5*x^198 + 7*x^197 + 11*x^196 + 13*x^195 + 17*x^194 + 19*x^193
517+
sage: f.reverse(degree=0)
518+
ValueError Traceback (most recent call last)
519+
...
520+
sage: f.reverse(degree=-5)
521+
ValueError Traceback (most recent call last)
522+
...
523+
ValueError: degree argument must be a non-negative integer, got -5
505524
"""
506525
self._parent._modulus.restore()
507526

508527
# Construct output polynomial
509528
cdef Polynomial_ZZ_pEX r
510529
r = Polynomial_ZZ_pEX.__new__(Polynomial_ZZ_pEX)
511-
celement_construct(&r.x, (<Polynomial_template>self)._cparent)
512-
r._parent = (<Polynomial_template>self)._parent
513-
r._cparent = (<Polynomial_template>self)._cparent
530+
celement_construct(&r.x, ( < Polynomial_template > self)._cparent)
531+
r._parent = ( < Polynomial_template > self)._parent
532+
r._cparent = ( < Polynomial_template > self)._cparent
514533

515534
# When a degree has been supplied, ensure it is a valid input
516535
cdef unsigned long d
517536
if degree is not None:
537+
if degree <= 0:
538+
raise ValueError("degree argument must be a non-negative integer, got %s" % (degree))
518539
try:
519540
d = degree
520541
except ValueError:
521-
raise ValueError("degree argument must be a non-negative integer, got %s"%(degree))
522-
ZZ_pEX_reverse_hi(r.x, (<Polynomial_ZZ_pEX>self).x, d)
542+
raise ValueError("degree argument must be a non-negative integer, got %s" % (degree))
543+
ZZ_pEX_reverse_hi(r.x, ( < Polynomial_ZZ_pEX > self).x, d)
523544
else:
524-
ZZ_pEX_reverse(r.x, (<Polynomial_ZZ_pEX>self).x)
545+
ZZ_pEX_reverse(r.x, ( < Polynomial_ZZ_pEX > self).x)
525546
return r
526547

527548
def inverse_series_trunc(self, prec):
@@ -534,7 +555,6 @@ cdef class Polynomial_ZZ_pEX(Polynomial_template):
534555
sage: R.<x> = GF(101^2)[]
535556
sage: z2 = R.base_ring().gen()
536557
sage: f = (3*z2 + 57)*x^3 + (13*z2 + 94)*x^2 + (7*z2 + 2)*x + 66*z2 + 15
537-
sage:
538558
sage: f.inverse_series_trunc(1)
539559
51*z2 + 92
540560
sage: f.inverse_series_trunc(2)
@@ -543,6 +563,30 @@ cdef class Polynomial_ZZ_pEX(Polynomial_template):
543563
(42*z2 + 94)*x^2 + (30*z2 + 30)*x + 51*z2 + 92
544564
sage: f.inverse_series_trunc(4)
545565
(99*z2 + 96)*x^3 + (42*z2 + 94)*x^2 + (30*z2 + 30)*x + 51*z2 + 92
566+
567+
TESTS::
568+
569+
sage: R.<x> = GF(163^2)[]
570+
sage: f = R([p for p in primes(20)])
571+
sage: f.inverse_series_trunc(1)
572+
82
573+
sage: f.inverse_series_trunc(2)
574+
40*x + 82
575+
sage: f.inverse_series_trunc(3)
576+
61*x^2 + 40*x + 82
577+
sage: f.inverse_series_trunc(0)
578+
ValueError Traceback (most recent call last)
579+
...
580+
ValueError: the precision must be positive, got 0
581+
sage: f.inverse_series_trunc(-1)
582+
ValueError Traceback (most recent call last)
583+
...
584+
ValueError: the precision must be positive, got -1
585+
sage: f = x + x^2 + x^3
586+
sage: f.inverse_series_trunc(5)
587+
ValueError Traceback (most recent call last)
588+
...
589+
ValueError: constant term 0 is not a unit
546590
"""
547591
self._parent._modulus.restore()
548592

@@ -558,13 +602,13 @@ cdef class Polynomial_ZZ_pEX(Polynomial_template):
558602
# Construct output polynomial
559603
cdef Polynomial_ZZ_pEX r
560604
r = Polynomial_ZZ_pEX.__new__(Polynomial_ZZ_pEX)
561-
celement_construct(&r.x, (<Polynomial_template>self)._cparent)
562-
r._parent = (<Polynomial_template>self)._parent
563-
r._cparent = (<Polynomial_template>self)._cparent
605+
celement_construct(&r.x, (< Polynomial_template > self)._cparent)
606+
r._parent = (< Polynomial_template > self)._parent
607+
r._cparent = (< Polynomial_template > self)._cparent
564608

565609
# Call to NTL for the inverse truncation
566610
if prec > 0:
567611
sig_on()
568612
ZZ_pEX_InvTrunc(r.x, self.x, prec)
569613
sig_off()
570-
return r
614+
return r

0 commit comments

Comments
 (0)