ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 22 May 2013 02:37:47 -0500substitution in expressionhttps://ask.sagemath.org/question/10134/substitution-in-expression/Hi,
After reading [Simplify shenanigans](http://ask.sagemath.org/question/2587/simplify-shenanigans), I am very confused about the possibility of doing
sage: sin(x).subs(sin(x) == 3)
3
Which becomes even more funny with
sage: SR(2).subs(SR(2) == pi)
pi
I thought one may only set values to variable but no: I can set value to integers !
Is there a concrete example where this is useful ? Or this is just one more argument to avoid the use of the symbolic ring ?
Anyway the substitution is not at all consistent with the behavior of the symbolic ring. If such substitutions are allowed, then the expression `SR(2) + SR(2)` should not be simplified !Fri, 17 May 2013 23:57:53 -0500https://ask.sagemath.org/question/10134/substitution-in-expression/Comment by tmonteil for <p>Hi,</p>
<p>After reading <a href="http://ask.sagemath.org/question/2587/simplify-shenanigans">Simplify shenanigans</a>, I am very confused about the possibility of doing</p>
<pre><code>sage: sin(x).subs(sin(x) == 3)
3
</code></pre>
<p>Which becomes even more funny with</p>
<pre><code>sage: SR(2).subs(SR(2) == pi)
pi
</code></pre>
<p>I thought one may only set values to variable but no: I can set value to integers ! </p>
<p>Is there a concrete example where this is useful ? Or this is just one more argument to avoid the use of the symbolic ring ?</p>
<p>Anyway the substitution is not at all consistent with the behavior of the symbolic ring. If such substitutions are allowed, then the expression <code>SR(2) + SR(2)</code> should not be simplified !</p>
https://ask.sagemath.org/question/10134/substitution-in-expression/?comment=17669#post-id-17669Perhaps such a feature could be used for the [variation of constants](http://en.wikipedia.org/wiki/Variation_of_constants) method :pSun, 19 May 2013 00:52:42 -0500https://ask.sagemath.org/question/10134/substitution-in-expression/?comment=17669#post-id-17669Answer by burcin for <p>Hi,</p>
<p>After reading <a href="http://ask.sagemath.org/question/2587/simplify-shenanigans">Simplify shenanigans</a>, I am very confused about the possibility of doing</p>
<pre><code>sage: sin(x).subs(sin(x) == 3)
3
</code></pre>
<p>Which becomes even more funny with</p>
<pre><code>sage: SR(2).subs(SR(2) == pi)
pi
</code></pre>
<p>I thought one may only set values to variable but no: I can set value to integers ! </p>
<p>Is there a concrete example where this is useful ? Or this is just one more argument to avoid the use of the symbolic ring ?</p>
<p>Anyway the substitution is not at all consistent with the behavior of the symbolic ring. If such substitutions are allowed, then the expression <code>SR(2) + SR(2)</code> should not be simplified !</p>
https://ask.sagemath.org/question/10134/substitution-in-expression/?answer=14958#post-id-14958The substitute command is a generic interface to the rewrite engine in [Pynac](http://pynac.org)/[GiNaC](http://www.ginac.de). You can replace an arbitrary subexpression, also containing wildcards (see the documentation of the `match()` method of symbolic expressions), with another expression.
Consider the case where a subexpression occurs in several places and you have a better formula or value for it derived from other sources. You can just replace the existing subexpression with the new formula using the `substitute()` function.Wed, 22 May 2013 00:12:29 -0500https://ask.sagemath.org/question/10134/substitution-in-expression/?answer=14958#post-id-14958Comment by vdelecroix for <p>The substitute command is a generic interface to the rewrite engine in <a href="http://pynac.org">Pynac</a>/<a href="http://www.ginac.de">GiNaC</a>. You can replace an arbitrary subexpression, also containing wildcards (see the documentation of the <code>match()</code> method of symbolic expressions), with another expression.</p>
<p>Consider the case where a subexpression occurs in several places and you have a better formula or value for it derived from other sources. You can just replace the existing subexpression with the new formula using the <code>substitute()</code> function.</p>
https://ask.sagemath.org/question/10134/substitution-in-expression/?comment=17663#post-id-17663Thanks for the precision of the implementation. And I see how it may be used.Wed, 22 May 2013 02:37:47 -0500https://ask.sagemath.org/question/10134/substitution-in-expression/?comment=17663#post-id-17663