ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 26 Jun 2023 08:27:22 +0200Substitution of variable in polynomial not working as expected.https://ask.sagemath.org/question/69493/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`.Sun, 25 Jun 2023 17:18:23 +0200https://ask.sagemath.org/question/69493/substitution-of-variable-in-polynomial-not-working-as-expected/Comment by John Palmieri for <p>The <code>subs</code> method behaves differently in two related settings:</p>
<ul>
<li>univariate polynomials over SR</li>
<li>multivariate polynomials over SR</li>
</ul>
<p>when trying to substitute a value for some variable involved as a coefficient.</p>
<p>In the univariate case:</p>
<pre><code>R.<x> = SR[]
a = SR.var('a')
P = a*x
P.subs(a=1)
</code></pre>
<p>returns <code>x</code>, as expected.</p>
<p>But in the multivariate case:</p>
<pre><code>R.<x, y> = SR[]
a = SR.var('a')
P = a*x
P.subs(a=1)
</code></pre>
<p>returns <code>a*x</code>.</p>
https://ask.sagemath.org/question/69493/substitution-of-variable-in-polynomial-not-working-as-expected/?comment=69501#post-id-69501Echoing @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`.Sun, 25 Jun 2023 22:58:14 +0200https://ask.sagemath.org/question/69493/substitution-of-variable-in-polynomial-not-working-as-expected/?comment=69501#post-id-69501Comment by Max Alekseyev for <p>The <code>subs</code> method behaves differently in two related settings:</p>
<ul>
<li>univariate polynomials over SR</li>
<li>multivariate polynomials over SR</li>
</ul>
<p>when trying to substitute a value for some variable involved as a coefficient.</p>
<p>In the univariate case:</p>
<pre><code>R.<x> = SR[]
a = SR.var('a')
P = a*x
P.subs(a=1)
</code></pre>
<p>returns <code>x</code>, as expected.</p>
<p>But in the multivariate case:</p>
<pre><code>R.<x, y> = SR[]
a = SR.var('a')
P = a*x
P.subs(a=1)
</code></pre>
<p>returns <code>a*x</code>.</p>
https://ask.sagemath.org/question/69493/substitution-of-variable-in-polynomial-not-working-as-expected/?comment=69500#post-id-69500To start with, `a` is not a polynomial variable in your example.Sun, 25 Jun 2023 22:34:41 +0200https://ask.sagemath.org/question/69493/substitution-of-variable-in-polynomial-not-working-as-expected/?comment=69500#post-id-69500Comment by Emmanuel Charpentier for <p>The <code>subs</code> method behaves differently in two related settings:</p>
<ul>
<li>univariate polynomials over SR</li>
<li>multivariate polynomials over SR</li>
</ul>
<p>when trying to substitute a value for some variable involved as a coefficient.</p>
<p>In the univariate case:</p>
<pre><code>R.<x> = SR[]
a = SR.var('a')
P = a*x
P.subs(a=1)
</code></pre>
<p>returns <code>x</code>, as expected.</p>
<p>But in the multivariate case:</p>
<pre><code>R.<x, y> = SR[]
a = SR.var('a')
P = a*x
P.subs(a=1)
</code></pre>
<p>returns <code>a*x</code>.</p>
https://ask.sagemath.org/question/69493/substitution-of-variable-in-polynomial-not-working-as-expected/?comment=69497#post-id-69497I take that back : `subs` is **not** supposed to do that in multivariate polynomials. I'll prepare an answer.Sun, 25 Jun 2023 21:03:12 +0200https://ask.sagemath.org/question/69493/substitution-of-variable-in-polynomial-not-working-as-expected/?comment=69497#post-id-69497Comment by Emmanuel Charpentier for <p>The <code>subs</code> method behaves differently in two related settings:</p>
<ul>
<li>univariate polynomials over SR</li>
<li>multivariate polynomials over SR</li>
</ul>
<p>when trying to substitute a value for some variable involved as a coefficient.</p>
<p>In the univariate case:</p>
<pre><code>R.<x> = SR[]
a = SR.var('a')
P = a*x
P.subs(a=1)
</code></pre>
<p>returns <code>x</code>, as expected.</p>
<p>But in the multivariate case:</p>
<pre><code>R.<x, y> = SR[]
a = SR.var('a')
P = a*x
P.subs(a=1)
</code></pre>
<p>returns <code>a*x</code>.</p>
https://ask.sagemath.org/question/69493/substitution-of-variable-in-polynomial-not-working-as-expected/?comment=69496#post-id-69496The 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](https://groups.google.com/g/sage-support)...Sun, 25 Jun 2023 21:00:09 +0200https://ask.sagemath.org/question/69493/substitution-of-variable-in-polynomial-not-working-as-expected/?comment=69496#post-id-69496Answer by Max Alekseyev for <p>The <code>subs</code> method behaves differently in two related settings:</p>
<ul>
<li>univariate polynomials over SR</li>
<li>multivariate polynomials over SR</li>
</ul>
<p>when trying to substitute a value for some variable involved as a coefficient.</p>
<p>In the univariate case:</p>
<pre><code>R.<x> = SR[]
a = SR.var('a')
P = a*x
P.subs(a=1)
</code></pre>
<p>returns <code>x</code>, as expected.</p>
<p>But in the multivariate case:</p>
<pre><code>R.<x, y> = SR[]
a = SR.var('a')
P = a*x
P.subs(a=1)
</code></pre>
<p>returns <code>a*x</code>.</p>
https://ask.sagemath.org/question/69493/substitution-of-variable-in-polynomial-not-working-as-expected/?answer=69502#post-id-69502`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})) )Mon, 26 Jun 2023 02:26:57 +0200https://ask.sagemath.org/question/69493/substitution-of-variable-in-polynomial-not-working-as-expected/?answer=69502#post-id-69502Comment by achrzesz for <p><code>P.subs()</code> should work only for the variables of the polynomials, ie. <code>x</code> and <code>y</code>.
<code>a</code> 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:</p>
<pre><code>R.<x, y> = SR[]
a = SR.var('a')
P = a*x
print( sum(map(lambda t: t[0].subs({a:1})*t[1], P)) )
</code></pre>
<p>or (as suggested by @achrzesz):</p>
<pre><code>print( P.map_coefficients(lambda t: t.subs({a:1})) )
</code></pre>
https://ask.sagemath.org/question/69493/substitution-of-variable-in-polynomial-not-working-as-expected/?comment=69511#post-id-69511P.map_coefficients(lambda t:t.subs({a:1}))Mon, 26 Jun 2023 08:27:22 +0200https://ask.sagemath.org/question/69493/substitution-of-variable-in-polynomial-not-working-as-expected/?comment=69511#post-id-69511Answer by Emmanuel Charpentier for <p>The <code>subs</code> method behaves differently in two related settings:</p>
<ul>
<li>univariate polynomials over SR</li>
<li>multivariate polynomials over SR</li>
</ul>
<p>when trying to substitute a value for some variable involved as a coefficient.</p>
<p>In the univariate case:</p>
<pre><code>R.<x> = SR[]
a = SR.var('a')
P = a*x
P.subs(a=1)
</code></pre>
<p>returns <code>x</code>, as expected.</p>
<p>But in the multivariate case:</p>
<pre><code>R.<x, y> = SR[]
a = SR.var('a')
P = a*x
P.subs(a=1)
</code></pre>
<p>returns <code>a*x</code>.</p>
https://ask.sagemath.org/question/69493/substitution-of-variable-in-polynomial-not-working-as-expected/?answer=69498#post-id-69498The `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,Sun, 25 Jun 2023 21:05:32 +0200https://ask.sagemath.org/question/69493/substitution-of-variable-in-polynomial-not-working-as-expected/?answer=69498#post-id-69498