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, 10 Sep 2019 22:05:57 +0200Substitution of free variablehttps://ask.sagemath.org/question/47819/substitution-of-free-variable/I have sagecells on a page that are used to solve some equations.
In a further cell, I want to substitute a given value in place of a
free variable. Sage's free variables have names such as `r1`, `r2`, etc.
Is there a way (some kind of wildcard) to substitute a value for
a free variable without knowing its name? I'd like this because
depending on the order of execution a user would press the cells,
the names would change.
It would look like this
CELL1
sol1 = solve([x + y == 1], x, y)
CELL2
sol2 = solve([x - y == 3], x, y)
and then further down I would like a new cell that would substitute
the free variable in `sol1` to a given value, say `0`.
However if I use something along the line of
sol1part = sol1.substitute(r1=0)
and the user executed for some reason cell2 before cell1, it will not work.Tue, 10 Sep 2019 19:23:14 +0200https://ask.sagemath.org/question/47819/substitution-of-free-variable/Answer by tmonteil for <p>I have sagecells on a page that are used to solve some equations.
In a further cell, I want to substitute a given value in place of a
free variable. Sage's free variables have names such as <code>r1</code>, <code>r2</code>, etc. </p>
<p>Is there a way (some kind of wildcard) to substitute a value for
a free variable without knowing its name? I'd like this because
depending on the order of execution a user would press the cells,
the names would change.</p>
<p>It would look like this </p>
<p>CELL1 </p>
<pre><code>sol1 = solve([x + y == 1], x, y)
</code></pre>
<p>CELL2 </p>
<pre><code>sol2 = solve([x - y == 3], x, y)
</code></pre>
<p>and then further down I would like a new cell that would substitute
the free variable in <code>sol1</code> to a given value, say <code>0</code>.</p>
<p>However if I use something along the line of </p>
<pre><code>sol1part = sol1.substitute(r1=0)
</code></pre>
<p>and the user executed for some reason cell2 before cell1, it will not work.</p>
https://ask.sagemath.org/question/47819/substitution-of-free-variable/?answer=47821#post-id-47821If you are sure that there is only one free variable, then you can easily identify it.
First, use `solution_dict` option to deal with the result more easily:
sage: sol1 = solve([x+y==1], x, y, solution_dict=True)
sage: sol1
[{y: r3, x: -r3 + 1}]
Now, you can ask for the variables appearing in a symbolic expression, see:
sage: sol1[0][x]
-r3 + 1
sage: sol1[0][x].variables()[0]
So, you can give it a Python name and use it afterwards:
sage: freevar = sol1[0][x].variables()[0]
sage: expr = sol1[0][x]
sage: expr
-r3 + 1
sage: expr.subs({freevar:10})
-9Tue, 10 Sep 2019 19:39:35 +0200https://ask.sagemath.org/question/47819/substitution-of-free-variable/?answer=47821#post-id-47821Comment by Jean-Sébastien for <p>If you are sure that there is only one free variable, then you can easily identify it.</p>
<p>First, use <code>solution_dict</code> option to deal with the result more easily:</p>
<pre><code>sage: sol1 = solve([x+y==1], x, y, solution_dict=True)
sage: sol1
[{y: r3, x: -r3 + 1}]
</code></pre>
<p>Now, you can ask for the variables appearing in a symbolic expression, see:</p>
<pre><code>sage: sol1[0][x]
-r3 + 1
sage: sol1[0][x].variables()[0]
</code></pre>
<p>So, you can give it a Python name and use it afterwards:</p>
<pre><code>sage: freevar = sol1[0][x].variables()[0]
sage: expr = sol1[0][x]
sage: expr
-r3 + 1
sage: expr.subs({freevar:10})
-9
</code></pre>
https://ask.sagemath.org/question/47819/substitution-of-free-variable/?comment=47822#post-id-47822This should work. I think I should be able to adapt it if there are more than 1 free variable as wellTue, 10 Sep 2019 19:55:05 +0200https://ask.sagemath.org/question/47819/substitution-of-free-variable/?comment=47822#post-id-47822Comment by tmonteil for <p>If you are sure that there is only one free variable, then you can easily identify it.</p>
<p>First, use <code>solution_dict</code> option to deal with the result more easily:</p>
<pre><code>sage: sol1 = solve([x+y==1], x, y, solution_dict=True)
sage: sol1
[{y: r3, x: -r3 + 1}]
</code></pre>
<p>Now, you can ask for the variables appearing in a symbolic expression, see:</p>
<pre><code>sage: sol1[0][x]
-r3 + 1
sage: sol1[0][x].variables()[0]
</code></pre>
<p>So, you can give it a Python name and use it afterwards:</p>
<pre><code>sage: freevar = sol1[0][x].variables()[0]
sage: expr = sol1[0][x]
sage: expr
-r3 + 1
sage: expr.subs({freevar:10})
-9
</code></pre>
https://ask.sagemath.org/question/47819/substitution-of-free-variable/?comment=47824#post-id-47824Yes, but you might have to do some assumption on their order i guess.Tue, 10 Sep 2019 20:06:06 +0200https://ask.sagemath.org/question/47819/substitution-of-free-variable/?comment=47824#post-id-47824Answer by Emmanuel Charpentier for <p>I have sagecells on a page that are used to solve some equations.
In a further cell, I want to substitute a given value in place of a
free variable. Sage's free variables have names such as <code>r1</code>, <code>r2</code>, etc. </p>
<p>Is there a way (some kind of wildcard) to substitute a value for
a free variable without knowing its name? I'd like this because
depending on the order of execution a user would press the cells,
the names would change.</p>
<p>It would look like this </p>
<p>CELL1 </p>
<pre><code>sol1 = solve([x + y == 1], x, y)
</code></pre>
<p>CELL2 </p>
<pre><code>sol2 = solve([x - y == 3], x, y)
</code></pre>
<p>and then further down I would like a new cell that would substitute
the free variable in <code>sol1</code> to a given value, say <code>0</code>.</p>
<p>However if I use something along the line of </p>
<pre><code>sol1part = sol1.substitute(r1=0)
</code></pre>
<p>and the user executed for some reason cell2 before cell1, it will not work.</p>
https://ask.sagemath.org/question/47819/substitution-of-free-variable/?answer=47827#post-id-47827You might collect all the variables from your solutions, and inject those whose string representation does not appear in `show_identifiers()` in the global namespace (see `sage.misc.misc.inject_variable?`), or simply declare them with `var`.Tue, 10 Sep 2019 22:05:57 +0200https://ask.sagemath.org/question/47819/substitution-of-free-variable/?answer=47827#post-id-47827