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, 11 Feb 2014 18:38:22 +0100Substituting a particular value for a parameterhttps://ask.sagemath.org/question/11024/substituting-a-particular-value-for-a-parameter/This question is a follow-on to [this one](http://ask.sagemath.org/question/3501/two-questions-about-parameters-in-solutions) I asked earlier. Suppose I have a return value (a list called `sols`) to a `solve` command which includes an extra parameter, like
[2+r1,2-r1,2*r1]
If I want to substitute a value for `r1`, I need to, for example:
var('r1')
[xx.subs(r1=2/3) for xx in sols]
However, if I happen to run the solve command again, the new parameter changes to r2, and then I have to change the above commands to work with r2 instead of r1. And of course if other parameters have been created in the course of my Sage session, the r1 above could be anything.
What I need is some way of both isolating that extra parameter, and substituting for it. One way seems to be something like:
params = [xx.variables()[0] for xx in sols]
[xx.subs_expr(p==2/3) for xx,p in zip(sols,params)]
which works if the extra parameter is listed first in each variable list. The command
solvars = reduce(lambda x,y:union(x,y),[xx.variables() for xx in svals])
produces a list in which the extra parameter is last. So I could use
[xx.subs_expr(solvars[-1]==2/3) for xx in sols]
But none of these particularly automatic, in terms of isolating the extra parameter. They seem to require some extra knowledge of where the parameter is in the variables list.
So what is the best way of doing this, which is robust and automatic?AlasdairTue, 11 Feb 2014 18:38:22 +0100https://ask.sagemath.org/question/11024/Two questions about parameters in solutionshttps://ask.sagemath.org/question/10997/two-questions-about-parameters-in-solutions/In the Sage documentation, this example is given:
sols = solve([x+y == 3, 2*x+2*y == 6],x,y); sols
of a solution which includes an extra parameter:
[[x=?r1+3,y=r1]]
1. If I run this comand again, the solution is given with r2, then again with r3 etc. Is it possible to "reset" the parameter number?
2. How does one substitute a particular value for this parameter? I can do it by iterating through the list and using rhs():
[i.rhs().subs(r1=1/2) for i in sols]
or by setting solution_dict=True and fiddling another way:
[sols[i].subs(r1=1/2) for i in [x,y]]
Neither of these seem particularly elegant - is there a more "natural" way?AlasdairThu, 06 Feb 2014 00:04:20 +0100https://ask.sagemath.org/question/10997/