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.Thu, 25 Aug 2011 16:16:14 +0200Substituting multiple valueshttps://ask.sagemath.org/question/8292/substituting-multiple-values/What is the best way of substituting a list (or vector or whatever) of values into an expression? For example, suppose I have
z = var('x y')
zvals = (1, 2)
w = x^2 + y^2
and want to substitute `zvals` for `z` in the expression `w`. I have tried the following commands:
w.subs(z=zvals) #doesn't work
w.subs({z:zvals}) #doesn't work
w.subs(x=1,y=2) #fine, but cumbersome if z has many elements
w.subs(z[0]=zvals[0],z[1]=zvals[1]) #doesn't work
w.subs({z[0]:zvals[0],z[1]:zvals[1]}) #fine, and could turn this into a loop
#if there are many variables, but ugly
w.subs(dict(zip(z,zvals))) #best I can come up with
As far as I can see, none of this behaviour changes if `z` and `zvals` are vectors or lists instead of tuples.
Is there a simpler way of doing this? Also, why doesn't the fourth attempt work when the fifth one does - is this down to a limitation of Python?
EDIT: I also realised that you can do `w.subs(z[0]==zvals[0])` but `w.subs(z[0]==zvals[0],z[1]==zvals[1])` won't work - why is this?Thu, 25 Aug 2011 10:09:33 +0200https://ask.sagemath.org/question/8292/substituting-multiple-values/Answer by Jason Grout for <p>What is the best way of substituting a list (or vector or whatever) of values into an expression? For example, suppose I have</p>
<pre><code>z = var('x y')
zvals = (1, 2)
w = x^2 + y^2
</code></pre>
<p>and want to substitute <code>zvals</code> for <code>z</code> in the expression <code>w</code>. I have tried the following commands:</p>
<pre><code>w.subs(z=zvals) #doesn't work
w.subs({z:zvals}) #doesn't work
w.subs(x=1,y=2) #fine, but cumbersome if z has many elements
w.subs(z[0]=zvals[0],z[1]=zvals[1]) #doesn't work
w.subs({z[0]:zvals[0],z[1]:zvals[1]}) #fine, and could turn this into a loop
#if there are many variables, but ugly
w.subs(dict(zip(z,zvals))) #best I can come up with
</code></pre>
<p>As far as I can see, none of this behaviour changes if <code>z</code> and <code>zvals</code> are vectors or lists instead of tuples.</p>
<p>Is there a simpler way of doing this? Also, why doesn't the fourth attempt work when the fifth one does - is this down to a limitation of Python?</p>
<p>EDIT: I also realised that you can do <code>w.subs(z[0]==zvals[0])</code> but <code>w.subs(z[0]==zvals[0],z[1]==zvals[1])</code> won't work - why is this?</p>
https://ask.sagemath.org/question/8292/substituting-multiple-values/?answer=12605#post-id-12605I would do it this way:
sage: z = var('x y')
sage: zvals = (1, 2)
sage: w(x,y)=x^2+y^2
Note that w explicitly lists the order of arguments...
sage: w(*zvals)
5
Python documentation of the *mylist syntax is [here](http://docs.python.org/dev/tutorial/controlflow.html#unpacking-argument-lists).
Here is another way:
sage: w(1,2)
5
sage: w(x=zvals[0], y=zvals[1])
5
Thu, 25 Aug 2011 11:00:10 +0200https://ask.sagemath.org/question/8292/substituting-multiple-values/?answer=12605#post-id-12605Comment by Sagenoob for <p>I would do it this way:</p>
<pre><code>sage: z = var('x y')
sage: zvals = (1, 2)
sage: w(x,y)=x^2+y^2
</code></pre>
<p>Note that w explicitly lists the order of arguments...</p>
<pre><code>sage: w(*zvals)
5
</code></pre>
<p>Python documentation of the *mylist syntax is <a href="http://docs.python.org/dev/tutorial/controlflow.html#unpacking-argument-lists">here</a>.</p>
<p>Here is another way:</p>
<pre><code>sage: w(1,2)
5
sage: w(x=zvals[0], y=zvals[1])
5
</code></pre>
https://ask.sagemath.org/question/8292/substituting-multiple-values/?comment=21305#post-id-21305Thanks - I had forgotten about the * syntax.Thu, 25 Aug 2011 16:16:14 +0200https://ask.sagemath.org/question/8292/substituting-multiple-values/?comment=21305#post-id-21305