Ask Your Question

Revision history [back]

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 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, 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).