Skip to content

Commit 7a17221

Browse files
author
Release Manager
committed
gh-39711: Allow negative shift for flint rational polynomial Fixes #39710 Note that the behavior is compatible with the existing behavior of e.g. `ZZ[]`. ### 📝 Checklist - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. - [x] I have linked a relevant issue or discussion. - [x] I have created tests covering the changes. - [ ] I have updated the documentation and checked the documentation preview. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on. For example, --> <!-- - #12345: short description why this is a dependency --> <!-- - #34567: ... --> URL: #39711 Reported by: user202729 Reviewer(s): Travis Scrimshaw
2 parents 4e7e01c + 8ce71e4 commit 7a17221

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

src/sage/rings/laurent_series_ring_element.pyx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,16 @@ cdef class LaurentSeries(AlgebraElement):
10801080
sage: f = 1/(1-t)
10811081
sage: f.truncate_neg(15)
10821082
t^15 + t^16 + t^17 + t^18 + t^19 + O(t^20)
1083+
1084+
TESTS:
1085+
1086+
Check that :issue:`39710` is fixed::
1087+
1088+
sage: S.<t> = LaurentSeriesRing(QQ)
1089+
sage: (t+t^2).truncate_neg(-1)
1090+
t + t^2
1091+
sage: (t+t^2).truncate_neg(-2)
1092+
t + t^2
10831093
"""
10841094
return type(self)(self._parent, self.__u >> (n - self.__n), n)
10851095

src/sage/rings/polynomial/polynomial_rational_flint.pyx

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ AUTHOR:
2424

2525
from cysignals.signals cimport sig_on, sig_str, sig_off
2626

27+
from libc.limits cimport LONG_MIN
2728
from cpython.long cimport PyLong_AsLong
2829
from sage.arith.long cimport pyobject_to_long
2930

@@ -779,7 +780,7 @@ cdef class Polynomial_rational_flint(Polynomial):
779780
# Shifting #
780781
###########################################################################
781782

782-
def __lshift__(self, n):
783+
def __lshift__(self, long n):
783784
"""
784785
Notationally multiply ``self`` by `t^n`.
785786
@@ -792,27 +793,34 @@ cdef class Polynomial_rational_flint(Polynomial):
792793
TESTS::
793794
794795
sage: R.<t> = QQ[]
796+
sage: t << (-1)
797+
1
798+
sage: t << (-10)
799+
0
795800
sage: f = R.random_element(1000)
796801
sage: (f << 23) >> 23 == f # indirect doctest
797802
True
798803
"""
799-
cdef unsigned long k = <unsigned long> n
804+
if n < 0:
805+
assert n != LONG_MIN
806+
return self >> (-n)
807+
800808
cdef Polynomial_rational_flint f = <Polynomial_rational_flint> self
801809
cdef Polynomial_rational_flint res
802810
cdef bint do_sig
803811

804-
if k == 0 or fmpq_poly_is_zero(f._poly):
812+
if n == 0 or fmpq_poly_is_zero(f._poly):
805813
return self
806814
else:
807815
res = f._new()
808816
do_sig = fmpq_poly_length(f._poly) > 5000 or n > 5000
809817

810818
if do_sig: sig_str("FLINT exception")
811-
fmpq_poly_shift_left(res._poly, f._poly, k)
819+
fmpq_poly_shift_left(res._poly, f._poly, n)
812820
if do_sig: sig_off()
813821
return res
814822

815-
def __rshift__(self, n):
823+
def __rshift__(self, long n):
816824
"""
817825
Notationally return the quotient of Euclidean division of ``self``
818826
by `t^n`.
@@ -823,20 +831,25 @@ cdef class Polynomial_rational_flint(Polynomial):
823831
sage: f = 1 + t + t^2/2 + t^3/3 + t^4/4
824832
sage: f >> 2
825833
1/4*t^2 + 1/3*t + 1/2
834+
sage: f >> (-2)
835+
1/4*t^6 + 1/3*t^5 + 1/2*t^4 + t^3 + t^2
826836
"""
827-
cdef unsigned long k = <unsigned long> n
837+
if n < 0:
838+
assert n != LONG_MIN
839+
return self << (-n)
840+
828841
cdef Polynomial_rational_flint f = <Polynomial_rational_flint> self
829842
cdef Polynomial_rational_flint res
830843
cdef bint do_sig
831844

832-
if k == 0 or fmpq_poly_is_zero(f._poly):
845+
if n == 0 or fmpq_poly_is_zero(f._poly):
833846
return self
834847
else:
835848
res = f._new()
836849
do_sig = _do_sig(f._poly)
837850

838851
if do_sig: sig_str("FLINT exception")
839-
fmpq_poly_shift_right(res._poly, f._poly, k)
852+
fmpq_poly_shift_right(res._poly, f._poly, n)
840853
if do_sig: sig_off()
841854
return res
842855

0 commit comments

Comments
 (0)