ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 12 Feb 2014 04:02:58 -0600Substituting a particular value for a parameterhttp://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?Tue, 11 Feb 2014 11:38:22 -0600http://ask.sagemath.org/question/11024/substituting-a-particular-value-for-a-parameter/Comment by kcrisman for <p>This question is a follow-on to <a href="http://ask.sagemath.org/question/3501/two-questions-about-parameters-in-solutions">this one</a> I asked earlier. Suppose I have a return value (a list called <code>sols</code>) to a <code>solve</code> command which includes an extra parameter, like</p>
<pre><code>[2+r1,2-r1,2*r1]
</code></pre>
<p>If I want to substitute a value for <code>r1</code>, I need to, for example:</p>
<pre><code>var('r1')
[xx.subs(r1=2/3) for xx in sols]
</code></pre>
<p>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.</p>
<p>What I need is some way of both isolating that extra parameter, and substituting for it. One way seems to be something like:</p>
<pre><code>params = [xx.variables()[0] for xx in sols]
[xx.subs_expr(p==2/3) for xx,p in zip(sols,params)]
</code></pre>
<p>which works if the extra parameter is listed first in each variable list. The command</p>
<pre><code>solvars = reduce(lambda x,y:union(x,y),[xx.variables() for xx in svals])
</code></pre>
<p>produces a list in which the extra parameter is last. So I could use</p>
<pre><code>[xx.subs_expr(solvars[-1]==2/3) for xx in sols]
</code></pre>
<p>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.</p>
<p>So what is the best way of doing this, which is robust and automatic?</p>
http://ask.sagemath.org/question/11024/substituting-a-particular-value-for-a-parameter/?comment=16278#post-id-16278Maxima should have a list of things like this - https://www.ma.utexas.edu/pipermail/maxima/2013/033850.html - but I had trouble accessing it. Also, you may find setting the `nicedummies` Maxima option useful here.Wed, 12 Feb 2014 04:02:58 -0600http://ask.sagemath.org/question/11024/substituting-a-particular-value-for-a-parameter/?comment=16278#post-id-16278Comment by Alasdair for <p>This question is a follow-on to <a href="http://ask.sagemath.org/question/3501/two-questions-about-parameters-in-solutions">this one</a> I asked earlier. Suppose I have a return value (a list called <code>sols</code>) to a <code>solve</code> command which includes an extra parameter, like</p>
<pre><code>[2+r1,2-r1,2*r1]
</code></pre>
<p>If I want to substitute a value for <code>r1</code>, I need to, for example:</p>
<pre><code>var('r1')
[xx.subs(r1=2/3) for xx in sols]
</code></pre>
<p>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.</p>
<p>What I need is some way of both isolating that extra parameter, and substituting for it. One way seems to be something like:</p>
<pre><code>params = [xx.variables()[0] for xx in sols]
[xx.subs_expr(p==2/3) for xx,p in zip(sols,params)]
</code></pre>
<p>which works if the extra parameter is listed first in each variable list. The command</p>
<pre><code>solvars = reduce(lambda x,y:union(x,y),[xx.variables() for xx in svals])
</code></pre>
<p>produces a list in which the extra parameter is last. So I could use</p>
<pre><code>[xx.subs_expr(solvars[-1]==2/3) for xx in sols]
</code></pre>
<p>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.</p>
<p>So what is the best way of doing this, which is robust and automatic?</p>
http://ask.sagemath.org/question/11024/substituting-a-particular-value-for-a-parameter/?comment=16279#post-id-16279I thought that might be the case... well, I'll just have to make do with what I've got. Could I isolate, do you think, the "non-parameter" variables using `locals` in some way?Tue, 11 Feb 2014 15:25:21 -0600http://ask.sagemath.org/question/11024/substituting-a-particular-value-for-a-parameter/?comment=16279#post-id-16279Comment by kcrisman for <p>This question is a follow-on to <a href="http://ask.sagemath.org/question/3501/two-questions-about-parameters-in-solutions">this one</a> I asked earlier. Suppose I have a return value (a list called <code>sols</code>) to a <code>solve</code> command which includes an extra parameter, like</p>
<pre><code>[2+r1,2-r1,2*r1]
</code></pre>
<p>If I want to substitute a value for <code>r1</code>, I need to, for example:</p>
<pre><code>var('r1')
[xx.subs(r1=2/3) for xx in sols]
</code></pre>
<p>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.</p>
<p>What I need is some way of both isolating that extra parameter, and substituting for it. One way seems to be something like:</p>
<pre><code>params = [xx.variables()[0] for xx in sols]
[xx.subs_expr(p==2/3) for xx,p in zip(sols,params)]
</code></pre>
<p>which works if the extra parameter is listed first in each variable list. The command</p>
<pre><code>solvars = reduce(lambda x,y:union(x,y),[xx.variables() for xx in svals])
</code></pre>
<p>produces a list in which the extra parameter is last. So I could use</p>
<pre><code>[xx.subs_expr(solvars[-1]==2/3) for xx in sols]
</code></pre>
<p>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.</p>
<p>So what is the best way of doing this, which is robust and automatic?</p>
http://ask.sagemath.org/question/11024/substituting-a-particular-value-for-a-parameter/?comment=16280#post-id-16280To my knowledge, there isn't a particularly robust way of doing this. We've discussed doing various things with the auto-generated variables of Maxima, but there hasn't been consensus on what the "right" thing to do is - we don't want them overwriting pre-existing user-defined variables, but we also want accessibility, and this is not easily resolvable.Tue, 11 Feb 2014 13:57:31 -0600http://ask.sagemath.org/question/11024/substituting-a-particular-value-for-a-parameter/?comment=16280#post-id-16280Answer by ndomes for <p>This question is a follow-on to <a href="http://ask.sagemath.org/question/3501/two-questions-about-parameters-in-solutions">this one</a> I asked earlier. Suppose I have a return value (a list called <code>sols</code>) to a <code>solve</code> command which includes an extra parameter, like</p>
<pre><code>[2+r1,2-r1,2*r1]
</code></pre>
<p>If I want to substitute a value for <code>r1</code>, I need to, for example:</p>
<pre><code>var('r1')
[xx.subs(r1=2/3) for xx in sols]
</code></pre>
<p>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.</p>
<p>What I need is some way of both isolating that extra parameter, and substituting for it. One way seems to be something like:</p>
<pre><code>params = [xx.variables()[0] for xx in sols]
[xx.subs_expr(p==2/3) for xx,p in zip(sols,params)]
</code></pre>
<p>which works if the extra parameter is listed first in each variable list. The command</p>
<pre><code>solvars = reduce(lambda x,y:union(x,y),[xx.variables() for xx in svals])
</code></pre>
<p>produces a list in which the extra parameter is last. So I could use</p>
<pre><code>[xx.subs_expr(solvars[-1]==2/3) for xx in sols]
</code></pre>
<p>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.</p>
<p>So what is the best way of doing this, which is robust and automatic?</p>
http://ask.sagemath.org/question/11024/substituting-a-particular-value-for-a-parameter/?answer=16033#post-id-16033A suggestion.
import re
var('x y z r')
eqns = [x+y==1, x-z==0]
sol=solve(eqns,[x,y,z])[0]
S=str(sol)
eval(S.replace(re.search('r[0-9]+',S).group(),'r'))Tue, 11 Feb 2014 19:15:35 -0600http://ask.sagemath.org/question/11024/substituting-a-particular-value-for-a-parameter/?answer=16033#post-id-16033