Skip to content

Commit 69b2e50

Browse files
committed
Address bug from merging in test. Rename shift and (in/de)flation functions
1 parent e57a071 commit 69b2e50

File tree

2 files changed

+56
-33
lines changed

2 files changed

+56
-33
lines changed

src/flint/test/test.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2060,8 +2060,10 @@ def test_fmpz_mod_poly():
20602060
assert f*f == f**fmpz(2)
20612061

20622062
# Shifts
2063-
assert R_test([1,2,3]) << 3 == R_test([0,0,0,1,2,3])
2064-
assert R_test([1,2,3]) >> 1 == R_test([2,3])
2063+
assert raises(lambda: R_test([1,2,3]).left_shift(-1), ValueError)
2064+
assert raises(lambda: R_test([1,2,3]).right_shift(-1), ValueError)
2065+
assert R_test([1,2,3]).left_shift(3) == R_test([0,0,0,1,2,3])
2066+
assert R_test([1,2,3]).right_shift(1) == R_test([2,3])
20652067

20662068
# Mod
20672069
assert raises(lambda: f % f_bad, ValueError)
@@ -2119,7 +2121,6 @@ def test_fmpz_mod_poly():
21192121
assert raises(lambda: (f_cmp).xgcd(f_cmp), ValueError)
21202122
assert raises(lambda: (f).xgcd("f_cmp"), TypeError)
21212123

2122-
21232124
# disc.
21242125
assert raises(lambda: (f_cmp).discriminant(), NotImplementedError)
21252126

@@ -2147,10 +2148,10 @@ def test_fmpz_mod_poly():
21472148
assert raises(lambda: f1.sqrt(), ValueError)
21482149
assert (f1*f1).sqrt() in [f1, -f1]
21492150

2150-
# deflate
2151+
# deflation
21512152
f1 = R_test([1,0,2,0,3])
2152-
assert raises(lambda: f1.deflate(100), ValueError)
2153-
assert f1.deflate(2) == R_test([1,2,3])
2153+
assert raises(lambda: f1.deflation(100), ValueError)
2154+
assert f1.deflation(2) == R_test([1,2,3])
21542155

21552156
# factor
21562157
ff = R_test([3,2,1]) * R_test([3,2,1]) * R_test([5,4,3])
@@ -2407,6 +2408,6 @@ def setbad(obj, i, val):
24072408
test_arb,
24082409
test_fmpz_mod,
24092410
test_fmpz_mod_dlog,
2410-
test_fmpz_mod_poly
2411+
test_fmpz_mod_poly,
24112412
test_polys,
24122413
]

src/flint/types/fmpz_mod_poly.pyx

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -510,52 +510,74 @@ cdef class fmpz_mod_poly(flint_poly):
510510
res.val, self.val, e_ulong, self.ctx.mod.val
511511
)
512512
return res
513-
514-
def shift(self, slong n):
513+
514+
def left_shift(self, slong n):
515515
"""
516-
Returns ``self`` shifted by ``n`` coefficients. If ``n`` is positive,
517-
zero coefficients are inserted, when ``n`` is negative, if ``n`` is
518-
greater than or equal to the length of ``self``, the zero polynomial
519-
is returned.
516+
Returns ``self`` shifted left by ``n`` coefficients by inserting
517+
zero coefficients. This is equivalent to multiplying the polynomial
518+
by x^n
520519
521520
>>> R = fmpz_mod_poly_ctx(163)
522521
>>> f = R([1,2,3])
523-
>>> f.shift(0)
522+
>>> f.left_shift(0)
524523
3*x^2 + 2*x + 1
525-
>>> f.shift(1)
524+
>>> f.left_shift(1)
526525
3*x^3 + 2*x^2 + x
527-
>>> f.shift(4)
526+
>>> f.left_shift(4)
528527
3*x^6 + 2*x^5 + x^4
529-
>>> f.shift(-1)
530-
3*x + 2
531-
>>> f.shift(-4)
532-
0
533528
534529
"""
535530
cdef fmpz_mod_poly res
536531
res = fmpz_mod_poly.__new__(fmpz_mod_poly)
537532
res.ctx = self.ctx
538533

534+
if n < 0:
535+
raise ValueError("Value must be shifted by a non-negative integer")
536+
539537
if n > 0:
540538
fmpz_mod_poly_shift_left(
541539
res.val, self.val, n, self.ctx.mod.val
542540
)
543-
elif n < 0:
544-
fmpz_mod_poly_shift_right(
545-
res.val, self.val, -n, self.ctx.mod.val
546-
)
547541
else: # do nothing, just copy self
548542
fmpz_mod_poly_set(
549543
res.val, self.val, self.ctx.mod.val
550544
)
551545

552-
return res
546+
return res
553547

554-
def __lshift__(self, n):
555-
return self.shift(n)
548+
def right_shift(self, slong n):
549+
"""
550+
Returns ``self`` shifted right by ``n`` coefficients.
551+
This is equivalent to the floor division of the polynomial
552+
by x^n
556553
557-
def __rshift__(self, n):
558-
return self.shift(-n)
554+
>>> R = fmpz_mod_poly_ctx(163)
555+
>>> f = R([1,2,3])
556+
>>> f.right_shift(0)
557+
3*x^2 + 2*x + 1
558+
>>> f.right_shift(1)
559+
3*x + 2
560+
>>> f.right_shift(4)
561+
0
562+
"""
563+
cdef fmpz_mod_poly res
564+
565+
if n < 0:
566+
raise ValueError("Value must be shifted by a non-negative integer")
567+
568+
res = fmpz_mod_poly.__new__(fmpz_mod_poly)
569+
res.ctx = self.ctx
570+
571+
if n > 0:
572+
fmpz_mod_poly_shift_right(
573+
res.val, self.val, n, self.ctx.mod.val
574+
)
575+
else: # do nothing, just copy self
576+
fmpz_mod_poly_set(
577+
res.val, self.val, self.ctx.mod.val
578+
)
579+
580+
return res
559581

560582
@staticmethod
561583
def _mod_(left, right):
@@ -1344,14 +1366,14 @@ cdef class fmpz_mod_poly(flint_poly):
13441366
)
13451367
return res
13461368

1347-
def inflate(self, ulong n):
1369+
def inflation(self, ulong n):
13481370
r"""
13491371
Returns the result of the polynomial `f = \textrm{self}` to
13501372
`f(x^n)`
13511373
13521374
>>> R = fmpz_mod_poly_ctx(163)
13531375
>>> f = R([1,2,3])
1354-
>>> f.inflate(10)
1376+
>>> f.inflation(10)
13551377
3*x^20 + 2*x^10 + 1
13561378
13571379
"""
@@ -1364,7 +1386,7 @@ cdef class fmpz_mod_poly(flint_poly):
13641386
)
13651387
return res
13661388

1367-
def deflate(self, ulong n):
1389+
def deflation(self, ulong n):
13681390
r"""
13691391
Returns the result of the polynomial `f = \textrm{self}` to
13701392
`f(x^{1/n})`
@@ -1373,7 +1395,7 @@ cdef class fmpz_mod_poly(flint_poly):
13731395
>>> f = R([1,0,2,0,3])
13741396
>>> f
13751397
3*x^4 + 2*x^2 + 1
1376-
>>> f.deflate(2)
1398+
>>> f.deflation(2)
13771399
3*x^2 + 2*x + 1
13781400
13791401
"""

0 commit comments

Comments
 (0)