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, 28 Oct 2015 05:06:16 -0500- substitute expression instead of formal function symbolhttps://ask.sagemath.org/question/8110/substitute-expression-instead-of-formal-function-symbol/Hi, everyone.
The question seems to be basic, but I cannot find out any simple answer for it...
So, the case is as follows. Let's assume, we have some formal function symbol (which is intended later to be replaced by actual expression)
x = var('x'); f = function('f', x)
and some function, which uses 'f(x)' in its definition:
a, b = var('a b')
g(x) = a*f(x) + b*f(x)^2
now we would like to compute derivative of 'g(x)' (and possibly make some other manipulations)
h(x) = diff(g, x)
as a result we have h(x), which includes symbols like 'D[0]f(x)'. This can be answer for some problem in general form. But now we would like to evaluate this expression for particular 'f(x)' (and possibly multiple times with different expressinos), e. g. for 'f(x) = a*x + b'.
It is desirable to have this with something like
answer = h.subs({f:a*x + b}) # do not work :(
but this substitution do not work inside derivatives...
So, the question is: is there some simple solution (like simple subs() function) for the case I've described above? Or may be there is some standard tricks/patterns to solve this issue?
Thanks in advance for any suggestions.Wed, 11 May 2011 08:53:09 -0500https://ask.sagemath.org/question/8110/substitute-expression-instead-of-formal-function-symbol/
- Answer by Nicolas M ThiĆ©ry for <p>Hi, everyone.</p>
<p>The question seems to be basic, but I cannot find out any simple answer for it...</p>
<p>So, the case is as follows. Let's assume, we have some formal function symbol (which is intended later to be replaced by actual expression)</p>
<pre><code>x = var('x'); f = function('f', x)
</code></pre>
<p>and some function, which uses 'f(x)' in its definition:</p>
<pre><code>a, b = var('a b')
g(x) = a*f(x) + b*f(x)^2
</code></pre>
<p>now we would like to compute derivative of 'g(x)' (and possibly make some other manipulations)</p>
<pre><code> h(x) = diff(g, x)
</code></pre>
<p>as a result we have h(x), which includes symbols like 'D[0]f(x)'. This can be answer for some problem in general form. But now we would like to evaluate this expression for particular 'f(x)' (and possibly multiple times with different expressinos), e. g. for 'f(x) = a*x + b'.</p>
<p>It is desirable to have this with something like</p>
<pre><code>answer = h.subs({f:a*x + b}) # do not work :(
</code></pre>
<p>but this substitution do not work inside derivatives...</p>
<p>So, the question is: is there some simple solution (like simple subs() function) for the case I've described above? Or may be there is some standard tricks/patterns to solve this issue?</p>
<p>Thanks in advance for any suggestions.</p>
https://ask.sagemath.org/question/8110/substitute-expression-instead-of-formal-function-symbol/?answer=12607#post-id-12607Hugh Thomas pointed me to the following. When writting:
sage: foo = function('foo',x)
``foo`` is actually ``foo(x)``. However, in ``h``, and in particular
in pieces like ``D[0](foo)'', it's not only ``foo(x)'' that we want to
replace, but ``foo''. So a trick is to store the result of
function('foo',x) in another variable. And now, substituting with
substitute function will work::
sage: var('a b x')
sage: f = function('foo',x)
sage: g = a*foo(x) + b*foo(x)^2
sage: h = diff(g, x)
sage: bar(x) = a*x + b
sage: h.substitute_function(foo, bar)
2*(a*x + b)*a*b + a^2
Now, some questions:
- Is there a way to recover ``foo`` from ``foo(x)``? Something like
foo(x).unapply()? Worst come to worst, one can use:
``sage.symbolic.function_factory.function('foo')``
- Should substitute_function support:
sage: h.substitute_function(foo(x), bar)
Moral: side effects, like those with function, are ugly ...Thu, 25 Aug 2011 20:25:04 -0500https://ask.sagemath.org/question/8110/substitute-expression-instead-of-formal-function-symbol/?answer=12607#post-id-12607
- Comment by pang for <p>Hugh Thomas pointed me to the following. When writting:</p>
<pre><code>sage: foo = function('foo',x)
</code></pre>
<p><code>foo</code> is actually <code>foo(x)</code>. However, in <code>h</code>, and in particular
in pieces like <code>D[0](foo)'', it's not only</code>foo(x)'' that we want to
replace, but ``foo''. So a trick is to store the result of
function('foo',x) in another variable. And now, substituting with
substitute function will work::</p>
<pre><code>sage: var('a b x')
sage: f = function('foo',x)
sage: g = a*foo(x) + b*foo(x)^2
sage: h = diff(g, x)
sage: bar(x) = a*x + b
sage: h.substitute_function(foo, bar)
2*(a*x + b)*a*b + a^2
</code></pre>
<p>Now, some questions:</p>
<ul>
<li><p>Is there a way to recover <code>foo</code> from <code>foo(x)</code>? Something like
foo(x).unapply()? Worst come to worst, one can use:
<code>sage.symbolic.function_factory.function('foo')</code></p></li>
<li><p>Should substitute_function support:</p>
<p>sage: h.substitute_function(foo(x), bar)</p></li>
</ul>
<p>Moral: side effects, like those with function, are ugly ...</p>
https://ask.sagemath.org/question/8110/substitute-expression-instead-of-formal-function-symbol/?comment=30309#post-id-30309f.operator() is actually foo.Wed, 28 Oct 2015 05:06:16 -0500https://ask.sagemath.org/question/8110/substitute-expression-instead-of-formal-function-symbol/?comment=30309#post-id-30309
- Comment by nbruin for <p>Hugh Thomas pointed me to the following. When writting:</p>
<pre><code>sage: foo = function('foo',x)
</code></pre>
<p><code>foo</code> is actually <code>foo(x)</code>. However, in <code>h</code>, and in particular
in pieces like <code>D[0](foo)'', it's not only</code>foo(x)'' that we want to
replace, but ``foo''. So a trick is to store the result of
function('foo',x) in another variable. And now, substituting with
substitute function will work::</p>
<pre><code>sage: var('a b x')
sage: f = function('foo',x)
sage: g = a*foo(x) + b*foo(x)^2
sage: h = diff(g, x)
sage: bar(x) = a*x + b
sage: h.substitute_function(foo, bar)
2*(a*x + b)*a*b + a^2
</code></pre>
<p>Now, some questions:</p>
<ul>
<li><p>Is there a way to recover <code>foo</code> from <code>foo(x)</code>? Something like
foo(x).unapply()? Worst come to worst, one can use:
<code>sage.symbolic.function_factory.function('foo')</code></p></li>
<li><p>Should substitute_function support:</p>
<p>sage: h.substitute_function(foo(x), bar)</p></li>
</ul>
<p>Moral: side effects, like those with function, are ugly ...</p>
https://ask.sagemath.org/question/8110/substitute-expression-instead-of-formal-function-symbol/?comment=16167#post-id-16167Version that is not dependent on side-effects:
sage: foo=function('foo')
sage: f=foo(x)
Or, version that fully relies on the side effect that top-level "function" has
and is explicit about that by not making any assignment (we're really breaking
python's convention that functions with side effects should return None)
sage: function('foo')
foo
Mon, 02 Jun 2014 07:41:55 -0500https://ask.sagemath.org/question/8110/substitute-expression-instead-of-formal-function-symbol/?comment=16167#post-id-16167
- Answer by Jason Bandlow for <p>Hi, everyone.</p>
<p>The question seems to be basic, but I cannot find out any simple answer for it...</p>
<p>So, the case is as follows. Let's assume, we have some formal function symbol (which is intended later to be replaced by actual expression)</p>
<pre><code>x = var('x'); f = function('f', x)
</code></pre>
<p>and some function, which uses 'f(x)' in its definition:</p>
<pre><code>a, b = var('a b')
g(x) = a*f(x) + b*f(x)^2
</code></pre>
<p>now we would like to compute derivative of 'g(x)' (and possibly make some other manipulations)</p>
<pre><code> h(x) = diff(g, x)
</code></pre>
<p>as a result we have h(x), which includes symbols like 'D[0]f(x)'. This can be answer for some problem in general form. But now we would like to evaluate this expression for particular 'f(x)' (and possibly multiple times with different expressinos), e. g. for 'f(x) = a*x + b'.</p>
<p>It is desirable to have this with something like</p>
<pre><code>answer = h.subs({f:a*x + b}) # do not work :(
</code></pre>
<p>but this substitution do not work inside derivatives...</p>
<p>So, the question is: is there some simple solution (like simple subs() function) for the case I've described above? Or may be there is some standard tricks/patterns to solve this issue?</p>
<p>Thanks in advance for any suggestions.</p>
https://ask.sagemath.org/question/8110/substitute-expression-instead-of-formal-function-symbol/?answer=12342#post-id-12342I'm not sure I understand why your example doesn't work, but here is a workaround:
sage: var('a b x')
sage: f = function('foo',x)
sage: g(x) = a*foo(x) + b*foo(x)^2
sage: h = g.diff(x)
sage: bar(x) = a*x + b
sage: h.substitute_function(foo, bar)
2*(a*x + b)*a*b + a^2Wed, 11 May 2011 13:49:54 -0500https://ask.sagemath.org/question/8110/substitute-expression-instead-of-formal-function-symbol/?answer=12342#post-id-12342
- Comment by Dmitry Semikin for <p>I'm not sure I understand why your example doesn't work, but here is a workaround:</p>
<pre><code>sage: var('a b x')
sage: f = function('foo',x)
sage: g(x) = a*foo(x) + b*foo(x)^2
sage: h = g.diff(x)
sage: bar(x) = a*x + b
sage: h.substitute_function(foo, bar)
2*(a*x + b)*a*b + a^2
</code></pre>
https://ask.sagemath.org/question/8110/substitute-expression-instead-of-formal-function-symbol/?comment=21748#post-id-21748One more thing: Is there some ability to create multi-line comments with code snippets?Wed, 11 May 2011 22:49:28 -0500https://ask.sagemath.org/question/8110/substitute-expression-instead-of-formal-function-symbol/?comment=21748#post-id-21748
- Comment by Dmitry Semikin for <p>I'm not sure I understand why your example doesn't work, but here is a workaround:</p>
<pre><code>sage: var('a b x')
sage: f = function('foo',x)
sage: g(x) = a*foo(x) + b*foo(x)^2
sage: h = g.diff(x)
sage: bar(x) = a*x + b
sage: h.substitute_function(foo, bar)
2*(a*x + b)*a*b + a^2
</code></pre>
https://ask.sagemath.org/question/8110/substitute-expression-instead-of-formal-function-symbol/?comment=21749#post-id-21749Thanks for your reply. Your example actually works, and here are two points to be mentioned: 1. if we replace in your example h.substitute_function(foo, bar) by h.subs({foo:bar}) we will get exception (something about coercion). 2. if we replace "f = function('foo', x')" with "foo = function('foo', x)" this will not work (i.e. it will not substitute anything). It seems, that there is some obscurity in what is returned by "function()" function (or may be, more precisely, how "assign" ("=") operator behaves). The line "f = function('foo', x)" makes object "foo" the NewSymbolicFunction, while object "f" - Expression (which I did not met in documentations). And it seems, that effects discussed are derived from this.Wed, 11 May 2011 22:47:22 -0500https://ask.sagemath.org/question/8110/substitute-expression-instead-of-formal-function-symbol/?comment=21749#post-id-21749