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.Tue, 28 May 2013 10:36:07 +0200referencing polynomial variableshttps://ask.sagemath.org/question/9979/referencing-polynomial-variables/Suppose I have a polynomial $f$ in the variables $x_1, \dots, x_5$ defined by something like
f = 0
for counter in range(1,5):
f += mylist[counter] * var('x' + str(counter))
where mylist is some list of integers. Then I can evaluate $f$ by e.g.
f.subs(x1=1,x2=2,x3=3,x4=4,x5=5)
**Question:** How do I do this evaluation when $f$ is in $n>>0$ variables?
I want to write something like
for counter in range(1,n):
f = f.subs(var('x' + str(counter))=counter)
but this isn't a valid argument for subs. This must be some way to coerce this to work.
Wed, 03 Apr 2013 19:12:04 +0200https://ask.sagemath.org/question/9979/referencing-polynomial-variables/Answer by fidbc for <p>Suppose I have a polynomial $f$ in the variables $x_1, \dots, x_5$ defined by something like</p>
<pre><code>f = 0
for counter in range(1,5):
f += mylist[counter] * var('x' + str(counter))
</code></pre>
<p>where mylist is some list of integers. Then I can evaluate $f$ by e.g.</p>
<pre><code>f.subs(x1=1,x2=2,x3=3,x4=4,x5=5)
</code></pre>
<p><strong>Question:</strong> How do I do this evaluation when $f$ is in $n>>0$ variables?</p>
<p>I want to write something like </p>
<pre><code>for counter in range(1,n):
f = f.subs(var('x' + str(counter))=counter)
</code></pre>
<p>but this isn't a valid argument for subs. This must be some way to coerce this to work.</p>
https://ask.sagemath.org/question/9979/referencing-polynomial-variables/?answer=14729#post-id-14729A possible approach might be the following:
vars=[]
f = 0
for counter in range(1,5):
vars.append( var('x'+str(counter) ))
f += mylist[counter] * vars[counter]
Then you can use a dictionary to evaluate. Say you have `n` variables and want `vars[i]` to take value `val[i]` where `val` is a list of values. So first construct a dictionary with
eval_dict = dict( [ (vars[i], val[i]) for i in range(n) ] )
Then you can evaluate by executing
f( eval_dict )Wed, 03 Apr 2013 22:14:07 +0200https://ask.sagemath.org/question/9979/referencing-polynomial-variables/?answer=14729#post-id-14729Answer by OliverP for <p>Suppose I have a polynomial $f$ in the variables $x_1, \dots, x_5$ defined by something like</p>
<pre><code>f = 0
for counter in range(1,5):
f += mylist[counter] * var('x' + str(counter))
</code></pre>
<p>where mylist is some list of integers. Then I can evaluate $f$ by e.g.</p>
<pre><code>f.subs(x1=1,x2=2,x3=3,x4=4,x5=5)
</code></pre>
<p><strong>Question:</strong> How do I do this evaluation when $f$ is in $n>>0$ variables?</p>
<p>I want to write something like </p>
<pre><code>for counter in range(1,n):
f = f.subs(var('x' + str(counter))=counter)
</code></pre>
<p>but this isn't a valid argument for subs. This must be some way to coerce this to work.</p>
https://ask.sagemath.org/question/9979/referencing-polynomial-variables/?answer=14973#post-id-14973The simplest solution is just to use dictionary arguments for subs.
Instead of
for counter in range(1,n):
f = f.subs(var('x' + str(counter))=counter)
which throws a SyntaxError, you can write
for counter in range(1,n):
f = f.subs({var('x' + str(counter)):counter})Sun, 26 May 2013 19:10:50 +0200https://ask.sagemath.org/question/9979/referencing-polynomial-variables/?answer=14973#post-id-14973Answer by tmonteil for <p>Suppose I have a polynomial $f$ in the variables $x_1, \dots, x_5$ defined by something like</p>
<pre><code>f = 0
for counter in range(1,5):
f += mylist[counter] * var('x' + str(counter))
</code></pre>
<p>where mylist is some list of integers. Then I can evaluate $f$ by e.g.</p>
<pre><code>f.subs(x1=1,x2=2,x3=3,x4=4,x5=5)
</code></pre>
<p><strong>Question:</strong> How do I do this evaluation when $f$ is in $n>>0$ variables?</p>
<p>I want to write something like </p>
<pre><code>for counter in range(1,n):
f = f.subs(var('x' + str(counter))=counter)
</code></pre>
<p>but this isn't a valid argument for subs. This must be some way to coerce this to work.</p>
https://ask.sagemath.org/question/9979/referencing-polynomial-variables/?answer=14954#post-id-14954Note that it is also possible to do this in a genuine `Polynomial Ring`, instead of the (sometimes weird) `Symbolic Ring`:
sage: mylist = [1,4,2,3,6,12,21,6,2]
sage: n = len(mylist)
sage: R = PolynomialRing(ZZ, n, 'x'); R
Multivariate Polynomial Ring in x0, x1, x2, x3, x4, x5, x6, x7, x8 over Integer Ring
sage: P = sum(c*R.gen(i) for i, c in enumerate(mylist)); P
x0 + 4*x1 + 2*x2 + 3*x3 + 6*x4 + 12*x5 + 21*x6 + 6*x7 + 2*x8
sage: P(range(n))
285
Sun, 26 May 2013 20:18:25 +0200https://ask.sagemath.org/question/9979/referencing-polynomial-variables/?answer=14954#post-id-14954Comment by tmonteil for <p>Note that it is also possible to do this in a genuine <code>Polynomial Ring</code>, instead of the (sometimes weird) <code>Symbolic Ring</code>:</p>
<pre><code>sage: mylist = [1,4,2,3,6,12,21,6,2]
sage: n = len(mylist)
sage: R = PolynomialRing(ZZ, n, 'x'); R
Multivariate Polynomial Ring in x0, x1, x2, x3, x4, x5, x6, x7, x8 over Integer Ring
sage: P = sum(c*R.gen(i) for i, c in enumerate(mylist)); P
x0 + 4*x1 + 2*x2 + 3*x3 + 6*x4 + 12*x5 + 21*x6 + 6*x7 + 2*x8
sage: P(range(n))
285
</code></pre>
https://ask.sagemath.org/question/9979/referencing-polynomial-variables/?comment=17628#post-id-17628Thanks for your suggestion, this indeed less hackish, i updated my answer accordingly.Tue, 28 May 2013 10:36:07 +0200https://ask.sagemath.org/question/9979/referencing-polynomial-variables/?comment=17628#post-id-17628Comment by Francis Clarke for <p>Note that it is also possible to do this in a genuine <code>Polynomial Ring</code>, instead of the (sometimes weird) <code>Symbolic Ring</code>:</p>
<pre><code>sage: mylist = [1,4,2,3,6,12,21,6,2]
sage: n = len(mylist)
sage: R = PolynomialRing(ZZ, n, 'x'); R
Multivariate Polynomial Ring in x0, x1, x2, x3, x4, x5, x6, x7, x8 over Integer Ring
sage: P = sum(c*R.gen(i) for i, c in enumerate(mylist)); P
x0 + 4*x1 + 2*x2 + 3*x3 + 6*x4 + 12*x5 + 21*x6 + 6*x7 + 2*x8
sage: P(range(n))
285
</code></pre>
https://ask.sagemath.org/question/9979/referencing-polynomial-variables/?comment=17637#post-id-17637It is worth mentioning that R = PolynomialRing(ZZ, n, 'x') and P = sum(c\*R.gen(i) for i, c in enumerate(mylist)) are perhaps a little less cryptic, and that R.inject_variables() allows one to write things like Q = 3\*x1\*x2^3 - x3^4Mon, 27 May 2013 13:39:45 +0200https://ask.sagemath.org/question/9979/referencing-polynomial-variables/?comment=17637#post-id-17637