Ask Your Question

Sagenoob's profile - activity

2017-02-17 16:03:49 -0600 received badge  Nice Question (source)
2016-06-07 20:42:02 -0600 received badge  Famous Question (source)
2014-11-30 10:19:12 -0600 received badge  Notable Question (source)
2013-09-17 03:28:40 -0600 received badge  Popular Question (source)
2011-08-26 12:04:13 -0600 received badge  Student (source)
2011-08-25 09:16:14 -0600 commented answer Substituting multiple values

Thanks - I had forgotten about the * syntax.

2011-08-25 09:13:36 -0600 received badge  Supporter (source)
2011-08-25 09:13:34 -0600 received badge  Scholar (source)
2011-08-25 09:13:34 -0600 marked best answer Substituting multiple values

I 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.

Here is another way:

sage: w(1,2)
5
sage: w(x=zvals[0], y=zvals[1])
5
2011-08-25 03:46:29 -0600 received badge  Editor (source)
2011-08-25 03:09:33 -0600 asked a question 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?