@@ -605,6 +605,14 @@ cdef class Polynomial(CommutativePolynomial):
605
605
606
606
TESTS:
607
607
608
+ One test for a simple evaluation::
609
+
610
+ sage: x, y = polygens(ZZ, 'x,y')
611
+ sage: t = polygen(x.parent(), 't')
612
+ sage: F = x*y*t
613
+ sage: F(y=1)
614
+ x*t
615
+
608
616
The following shows that :trac:`2360` is indeed fixed. ::
609
617
610
618
sage: R.<x,y> = ZZ[]
@@ -783,14 +791,20 @@ cdef class Polynomial(CommutativePolynomial):
783
791
- Francis Clarke (2012-08-26): fix keyword substitution in the
784
792
leading coefficient.
785
793
"""
786
- cdef long i, j
794
+ cdef long i, j, d, deg
787
795
cdef Polynomial pol = self
788
- cdef long d
789
796
cdef ETuple etup
790
797
cdef list cs
791
798
cdef dict coeff_sparse, coeff_dict
792
799
793
- cst = self ._parent._base.zero() if self .degree() < 0 else self .get_unsafe(0 )
800
+ deg = self .degree()
801
+ if deg < 0 :
802
+ top = self ._parent._base.one()
803
+ cst = self ._parent._base.zero()
804
+ else :
805
+ top = self .get_unsafe(deg)
806
+ cst = self .get_unsafe(0 )
807
+
794
808
a = args[0 ] if len (args) == 1 else None
795
809
if kwds or not (isinstance (a, Element) or PyNumber_Check(a)):
796
810
# slow path
@@ -816,18 +830,22 @@ cdef class Polynomial(CommutativePolynomial):
816
830
try :
817
831
# Note that we may be calling a different implementation that
818
832
# is more permissive about its arguments than we are.
819
- cst = cst(* args, ** kwds)
820
- eval_coeffs = True
833
+ top = top(* args, ** kwds)
821
834
except TypeError :
822
835
if args: # bwd compat: nonsense *keyword* arguments are okay
823
836
raise TypeError (" Wrong number of arguments" )
837
+ else :
838
+ eval_coeffs = True
824
839
825
840
# Evaluate the coefficients, then fall through to evaluate the
826
841
# resulting univariate polynomial
827
842
828
843
if eval_coeffs:
844
+ new_base = parent(top)
845
+ # tentative common parent of the evaluated coefficients
829
846
pol = pol.map_coefficients(lambda c : c(* args, ** kwds),
830
- new_base_ring = parent(cst))
847
+ new_base_ring = new_base)
848
+ cst = cst(* args, ** kwds)
831
849
832
850
R = parent(a)
833
851
@@ -840,8 +858,6 @@ cdef class Polynomial(CommutativePolynomial):
840
858
if isinstance (a, Polynomial) and a.base_ring() is pol._parent._base:
841
859
if (< Polynomial> a).is_gen():
842
860
return R(pol)
843
- if (< Polynomial> a).is_zero():
844
- return R(cst)
845
861
d = (< Polynomial> a).degree()
846
862
if d < 0 : # f(0)
847
863
return R(cst)
@@ -10211,7 +10227,7 @@ cdef class Polynomial(CommutativePolynomial):
10211
10227
R = R.change_ring(new_base_ring)
10212
10228
elif isinstance (f, Map):
10213
10229
R = R.change_ring(f.codomain())
10214
- return R({k: f(v) for (k,v) in self .dict().items()})
10230
+ return R({k: f(v) for k, v in self .dict().items()})
10215
10231
10216
10232
def is_cyclotomic (self , certificate = False , algorithm = " pari" ):
10217
10233
r """
0 commit comments