@@ -24,6 +24,7 @@ AUTHOR:
2424
2525from cysignals.signals cimport sig_on, sig_str, sig_off
2626
27+ from libc.limits cimport LONG_MIN
2728from cpython.long cimport PyLong_AsLong
2829from 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