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.Thu, 30 Jul 2020 11:52:59 +0200Composite function: how to show function names instead of its full expression?https://ask.sagemath.org/question/52714/composite-function-how-to-show-function-names-instead-of-its-full-expression/Suppose I define a function `f(x) = x**2` and a function `g(y) = f(y) + 2*y`.
When I type `show(g)` the output is: `y**2 +2*y`.
What should I do to have the `show` output as `f(y) + 2*y` instead?Mon, 27 Jul 2020 16:44:25 +0200https://ask.sagemath.org/question/52714/composite-function-how-to-show-function-names-instead-of-its-full-expression/Answer by slelievre for <p>Suppose I define a function <code>f(x) = x**2</code> and a function <code>g(y) = f(y) + 2*y</code>.</p>
<p>When I type <code>show(g)</code> the output is: <code>y**2 +2*y</code>.</p>
<p>What should I do to have the <code>show</code> output as <code>f(y) + 2*y</code> instead?</p>
https://ask.sagemath.org/question/52714/composite-function-how-to-show-function-names-instead-of-its-full-expression/?answer=52720#post-id-52720Not sure Sage supports that kind of "holding".
Here is a workaround.
Define two versions of the function `f`,
one "anonymous" and one "explicit":
sage: f = function('f')
sage: ff(x) = x**2
Use the symbolic one to define `g`:
sage: g(y) = f(y) + 2*y
sage: g
y |--> 2*y + f(y)
When you need it, substitute:
sage: g.substitute_function(f, ff)
y^2 + 2*y
Tue, 28 Jul 2020 12:35:47 +0200https://ask.sagemath.org/question/52714/composite-function-how-to-show-function-names-instead-of-its-full-expression/?answer=52720#post-id-52720Comment by delta_omega for <p>Not sure Sage supports that kind of "holding".</p>
<p>Here is a workaround.</p>
<p>Define two versions of the function <code>f</code>,
one "anonymous" and one "explicit":</p>
<pre><code>sage: f = function('f')
sage: ff(x) = x**2
</code></pre>
<p>Use the symbolic one to define <code>g</code>:</p>
<pre><code>sage: g(y) = f(y) + 2*y
sage: g
y |--> 2*y + f(y)
</code></pre>
<p>When you need it, substitute:</p>
<pre><code>sage: g.substitute_function(f, ff)
y^2 + 2*y
</code></pre>
https://ask.sagemath.org/question/52714/composite-function-how-to-show-function-names-instead-of-its-full-expression/?comment=52752#post-id-52752It does the trick, but it's a bit frustrating that there is not a more natural way to this. It is a very important feature when you are dealing with long expressions. But thank you very much anyway!Thu, 30 Jul 2020 11:52:59 +0200https://ask.sagemath.org/question/52714/composite-function-how-to-show-function-names-instead-of-its-full-expression/?comment=52752#post-id-52752