Skip to content

Commit 78dcd21

Browse files
roed314mezzarobba
authored andcommitted
Convert result of multivariate polynomial evaluation into correct parent
1 parent 104dde9 commit 78dcd21

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

src/sage/rings/polynomial/multi_polynomial_libsingular.pyx

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2056,6 +2056,15 @@ cdef class MPolynomial_libsingular(MPolynomial):
20562056
9
20572057
sage: a.parent() is QQ
20582058
True
2059+
2060+
See :trac:`33373`::
2061+
2062+
sage: k.<a> = GF(2^4)
2063+
sage: R.<x> = PolynomialRing(k, 1)
2064+
sage: f = R(1)
2065+
sage: S.<y> = PolynomialRing(k, 1)
2066+
sage: f(y).parent()
2067+
Multivariate Polynomial Ring in y over Finite Field in a of size 2^4
20592068
"""
20602069
if len(kwds) > 0:
20612070
f = self.subs(**kwds)
@@ -2075,29 +2084,28 @@ cdef class MPolynomial_libsingular(MPolynomial):
20752084
if l != parent._ring.N:
20762085
raise TypeError("number of arguments does not match number of variables in parent")
20772086

2087+
res_parent = coercion_model.common_parent(parent._base, *x)
2088+
cdef poly *res # ownership will be transferred to us in the else block
20782089
try:
20792090
# Attempt evaluation via singular.
20802091
coerced_x = [parent.coerce(e) for e in x]
20812092
except TypeError:
20822093
# give up, evaluate functional
2083-
y = parent.base_ring().zero()
2094+
sage_res = parent.base_ring().zero()
20842095
for (m,c) in self.dict().iteritems():
2085-
y += c*mul([ x[i]**m[i] for i in m.nonzero_positions()])
2086-
return y
2087-
2088-
cdef poly *res # ownership will be transferred to us in the next line
2089-
singular_polynomial_call(&res, self._poly, _ring, coerced_x, MPolynomial_libsingular_get_element)
2090-
res_parent = coercion_model.common_parent(parent._base, *x)
2091-
2092-
if res == NULL:
2093-
return res_parent(0)
2094-
if p_LmIsConstant(res, _ring):
2095-
sage_res = si2sa( p_GetCoeff(res, _ring), _ring, parent._base )
2096-
p_Delete(&res, _ring) # sage_res contains copy
2096+
sage_res += c*mul([ x[i]**m[i] for i in m.nonzero_positions()])
20972097
else:
2098-
sage_res = new_MP(parent, res) # pass on ownership of res to sage_res
2098+
singular_polynomial_call(&res, self._poly, _ring, coerced_x, MPolynomial_libsingular_get_element)
2099+
2100+
if res == NULL:
2101+
return res_parent(0)
2102+
if p_LmIsConstant(res, _ring):
2103+
sage_res = si2sa( p_GetCoeff(res, _ring), _ring, parent._base )
2104+
p_Delete(&res, _ring) # sage_res contains copy
2105+
else:
2106+
sage_res = new_MP(parent, res) # pass on ownership of res to sage_res
20992107

2100-
if parent(sage_res) is not res_parent:
2108+
if sage_res.parent() is not res_parent:
21012109
sage_res = res_parent(sage_res)
21022110
return sage_res
21032111

0 commit comments

Comments
 (0)