# convert expression to function

Hola,

is there a way to convert symbolic expression to proper functions?

E. g. s = sin(x) into x |--> sin(x)

So far I've been using f(x) = s(x), however, deprection warnings occur:

DeprecationWarning: Substitution using function-call syntax and unnamed arguments is deprecated and will be removed from a future release of Sage; you can use named arguments instead, like EXPR(x=..., y=...)

edit retag close merge delete

Sort by » oldest newest most voted

Does it answer the question ?

sage:s=sin(x)
sage: s
sin(x)
sage: f=s.function(x)
sage: f
x |--> sin(x)

more

Still giving out same DeprecationWarning but seems to be proper method...

sage: s = sin(x)
sage: f(x) = s
sage: f
x |--> sin(x)


No deprecation warning! What's going on here is that s(x) was really trying to evaluate your symbolic expression s at the point x - which happened to be a variable, but is still deprecated. If you use the transitive property, the above is just saying

sage: f(x) = sin(x)


which is what you want, as opposed to

sage: f(x) = sin(x)(x)


in your first attempt, which is perhaps ambiguous.

more

Great, sounds perfect!! Any way to do this without knowledge of expression's default variable? Say, if I get only the "s", can I do a makeover to function in a universal way?

You could use s.variables() and s.variables() to find out what the variable was. Only if you are sure there is only one variable - expressions like a*sin(x) wouldn't work, even if a is a "constant", since Sage can't tell the difference without the function notation. Also, the "obvious" thing to do f(s.variables()) = s, or even to name that and then plug it in, doesn't work because of the way the preparser works. In short, it's mostly not worth the effort.