# Substitution of variable in polynomial not working as expected.

The subs method behaves differently in two related settings:

• univariate polynomials over SR
• multivariate polynomials over SR

when trying to substitute a value for some variable involved as a coefficient.

In the univariate case:

R.<x> = SR[]
a = SR.var('a')
P = a*x
P.subs(a=1)


returns x, as expected.

But in the multivariate case:

R.<x, y> = SR[]
a = SR.var('a')
P = a*x
P.subs(a=1)


returns a*x.

edit retag close merge delete

The implementations are different. to clarify :

sage: reset()
sage: a=SR.var("a")
sage: R1.<x>=SR[]
sage: R2.<y,z>=SR[]
sage: list(a*x^3)
[0, 0, 0, a]
sage: list(a*y^3)
[(a, y^3)]


You may have discovered that the subs method of polynomial rings is buggy. You may explore this a bot further an open an issue, possibly after a yelp on sage-support...

( 2023-06-25 21:00:09 +0200 )edit

I take that back : subs is not supposed to do that in multivariate polynomials. I'll prepare an answer.

( 2023-06-25 21:03:12 +0200 )edit

To start with, a is not a polynomial variable in your example.

( 2023-06-25 22:34:41 +0200 )edit

Echoing @Max Alekseyev, I think that mixing symbolic variables with polynomials could be buggy. If you instead did R.<a,x,y> = SR[], then (a*x).subs(a=3) should return 3*x.

( 2023-06-25 22:58:14 +0200 )edit

Sort by ยป oldest newest most voted

P.subs() should work only for the variables of the polynomials, ie. x and y. a is a not a polynomial variable but rather a parameter, which polynomial coefficients depend on. Hence, you need to perform symbolic substitution in each of the coefficients. For example, this will do the job:

R.<x, y> = SR[]
a = SR.var('a')
P = a*x
print( sum(map(lambda t: t[0].subs({a:1})*t[1], P)) )


or (as suggested by @achrzesz):

print( P.map_coefficients(lambda t: t.subs({a:1})) )

more

2

P.map_coefficients(lambda t:t.subs({a:1}))

( 2023-06-26 08:27:22 +0200 )edit

The subs method for polynomials is not an analog of SR.subs !

sage: a=SR.var("a")
sage: R1.<x>=SR[]
sage: R2.<y,z>=SR[]
sage: x.subs?


prints

Docstring:
Identical to "self(*x)".

See the docstring for "__call__()".

EXAMPLES:

sage: R.<x> = QQ[]
sage: f = x^3 + x - 3
sage: f.subs(x=5)
127
sage: f.subs(5)
127
sage: f.subs({x:2})
7
sage: f.subs({})
x^3 + x - 3
sage: f.subs({'x':2})
Traceback (most recent call last):


etc... Compare with :

sage: y.subs?
Signature:      y.subs(fixed=None, **kw)
Docstring:
Fix some given variables in a given multivariate polynomial and
return the changed multivariate polynomials. The polynomial itself
is not affected. The variable, value pairs for fixing are to be
provided as a dictionary of the form "{variable: value}".

This is a special case of evaluating the polynomial with some of
the variables constants and the others the original variables.

INPUT:

* "fixed" - (optional) dictionary of inputs

* "**kw" - named parameters

OUTPUT: new "MPolynomial"

EXAMPLES:

sage: R.<x,y> = QQbar[]                                                     # optional - sage.rings.number_field
sage: f = x^2 + y + x^2*y^2 + 5                                             # optional - sage.rings.number_field
sage: f((5, y))                                                             # optional - sage.rings.number_field
25*y^2 + y + 30
sage: f.subs({x: 5})                                                        # optional - sage.rings.number_field
25*y^2 + y + 30


As far as I understand it, (a*x).subs(a=1) should have raised an error.

Consider yelping on sage-support...

HTH,

more