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.Mon, 15 Jun 2020 22:36:51 +0200Show both sides of a function definitionhttps://ask.sagemath.org/question/51946/show-both-sides-of-a-function-definition/When I define a function and show on it, I only get the right hand side. Is there a way to show the full definition? Basically I want to show pretty-printed version of the code used to define the function. I'm using a Jupyter notebook. I could simply rewrite it in a markdown cell of course, but that's tedious and error-prone my real use-case which is more complex than this toy example.
f(x) = a*x^2 + b*x + c
show(f)
What I get:
𝑥 ↦ 𝑎𝑥<sup>2</sup>+𝑏𝑥+𝑐
What I want:
f(𝑥) = 𝑎𝑥<sup>2</sup>+𝑏𝑥+𝑐Sat, 13 Jun 2020 19:01:25 +0200https://ask.sagemath.org/question/51946/show-both-sides-of-a-function-definition/Answer by rburing for <p>When I define a function and show on it, I only get the right hand side. Is there a way to show the full definition? Basically I want to show pretty-printed version of the code used to define the function. I'm using a Jupyter notebook. I could simply rewrite it in a markdown cell of course, but that's tedious and error-prone my real use-case which is more complex than this toy example.</p>
<pre><code>f(x) = a*x^2 + b*x + c
show(f)
</code></pre>
<p>What I get:</p>
<p>𝑥 ↦ 𝑎𝑥<sup>2</sup>+𝑏𝑥+𝑐</p>
<p>What I want:</p>
<p>f(𝑥) = 𝑎𝑥<sup>2</sup>+𝑏𝑥+𝑐</p>
https://ask.sagemath.org/question/51946/show-both-sides-of-a-function-definition/?answer=51961#post-id-51961The reason it works this way is technical; with the current implementation you cannot avoid it. The unfortunate fact is that the callable symbolic expression `f` doesn't know its own name. Namely, the code means the following:
sage: preparse("f(x) = a*x^2 + b*x + c")
'__tmp__=var("x"); f = symbolic_expression(a*x**Integer(2) + b*x + c).function(x)'
so `f` is only the name of the Python identifier, and the object that `f` refers to is an element of a [CallableSymbolicExpressionRing](https://doc.sagemath.org/html/en/reference/calculus/sage/symbolic/callable.html), which only knows the names of its arguments.
---
As a workaround I suggest the following:
sage: show(LatexExpr('f(x) ='), f(x))
$$f(x) = a x^{2} + b x + c$$
Yes, it involves repeating the name, but in my opinion it's not so bad.
---
You could also define this shorthand:
def show_func(func_str):
func = sage_eval(func_str, globals())
args = func.arguments()
args_str = ','.join(str(arg) for arg in args)
show(LatexExpr('{}({}) = '.format(func_str, args_str)), func(*args))
Then you can do:
sage: show_func('f')
$$f(x) = a x^{2} + b x + c$$
(The argument has to be a string.)
---
Or maybe you'd like to use manifolds instead?
R.<x> = RealLine(latex_name=r'\mathbb{R}')
var('a,b,c')
f = R.scalar_field(a*x^2 + b*x + c, name='f')
show(f.display())
$$\begin{array}{llcl} f:& \mathbb{R} & \longrightarrow & \mathbb{R} \\\\ & x & \longmapsto & a x^{2} + b x + c \end{array}$$
That way you also get domains and codomains (and even more options).Sun, 14 Jun 2020 11:45:41 +0200https://ask.sagemath.org/question/51946/show-both-sides-of-a-function-definition/?answer=51961#post-id-51961Comment by vinaypai for <p>The reason it works this way is technical; with the current implementation you cannot avoid it. The unfortunate fact is that the callable symbolic expression <code>f</code> doesn't know its own name. Namely, the code means the following:</p>
<pre><code>sage: preparse("f(x) = a*x^2 + b*x + c")
'__tmp__=var("x"); f = symbolic_expression(a*x**Integer(2) + b*x + c).function(x)'
</code></pre>
<p>so <code>f</code> is only the name of the Python identifier, and the object that <code>f</code> refers to is an element of a <a href="https://doc.sagemath.org/html/en/reference/calculus/sage/symbolic/callable.html">CallableSymbolicExpressionRing</a>, which only knows the names of its arguments.</p>
<hr>
<p>As a workaround I suggest the following:</p>
<pre><code>sage: show(LatexExpr('f(x) ='), f(x))
</code></pre>
<p>$$f(x) = a x^{2} + b x + c$$</p>
<p>Yes, it involves repeating the name, but in my opinion it's not so bad.</p>
<hr>
<p>You could also define this shorthand:</p>
<pre><code>def show_func(func_str):
func = sage_eval(func_str, globals())
args = func.arguments()
args_str = ','.join(str(arg) for arg in args)
show(LatexExpr('{}({}) = '.format(func_str, args_str)), func(*args))
</code></pre>
<p>Then you can do:</p>
<pre><code>sage: show_func('f')
</code></pre>
<p>$$f(x) = a x^{2} + b x + c$$</p>
<p>(The argument has to be a string.)</p>
<hr>
<p>Or maybe you'd like to use manifolds instead?</p>
<pre><code>R.<x> = RealLine(latex_name=r'\mathbb{R}')
var('a,b,c')
f = R.scalar_field(a*x^2 + b*x + c, name='f')
show(f.display())
</code></pre>
<p>$$\begin{array}{llcl} f:& \mathbb{R} & \longrightarrow & \mathbb{R} \\ & x & \longmapsto & a x^{2} + b x + c \end{array}$$</p>
<p>That way you also get domains and codomains (and even more options).</p>
https://ask.sagemath.org/question/51946/show-both-sides-of-a-function-definition/?comment=52026#post-id-52026@rburing thank you for the detailed explanation and answer. You're right, repeating just the function name isn't too bad, so your suggestion does the trick.Mon, 15 Jun 2020 22:36:51 +0200https://ask.sagemath.org/question/51946/show-both-sides-of-a-function-definition/?comment=52026#post-id-52026