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.Fri, 01 Jun 2012 09:16:15 +0200convert expression to functionhttps://ask.sagemath.org/question/8985/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=...)`Sun, 20 May 2012 06:52:44 +0200https://ask.sagemath.org/question/8985/convert-expression-to-function/Answer by Laurent Claessens for <p>Hola,</p>
<p>is there a way to convert symbolic expression to proper functions?</p>
<p>E. g. <code>s = sin(x)</code> into <code>x |--> sin(x)</code></p>
<p>So far I've been using f(x) = s(x), however, deprection warnings occur:</p>
<p><code>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=...)</code></p>
https://ask.sagemath.org/question/8985/convert-expression-to-function/?answer=13594#post-id-13594Does it answer the question ?
sage:s=sin(x)
sage: s
sin(x)
sage: f=s.function(x)
sage: f
x |--> sin(x)Sun, 20 May 2012 09:55:23 +0200https://ask.sagemath.org/question/8985/convert-expression-to-function/?answer=13594#post-id-13594Comment by Mathemage for <p>Does it answer the question ?</p>
<pre><code>sage:s=sin(x)
sage: s
sin(x)
sage: f=s.function(x)
sage: f
x |--> sin(x)
</code></pre>
https://ask.sagemath.org/question/8985/convert-expression-to-function/?comment=19761#post-id-19761Still giving out same DeprecationWarning but seems to be proper method...Sun, 20 May 2012 17:47:03 +0200https://ask.sagemath.org/question/8985/convert-expression-to-function/?comment=19761#post-id-19761Answer by kcrisman for <p>Hola,</p>
<p>is there a way to convert symbolic expression to proper functions?</p>
<p>E. g. <code>s = sin(x)</code> into <code>x |--> sin(x)</code></p>
<p>So far I've been using f(x) = s(x), however, deprection warnings occur:</p>
<p><code>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=...)</code></p>
https://ask.sagemath.org/question/8985/convert-expression-to-function/?answer=13597#post-id-13597 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.
Mon, 21 May 2012 10:25:35 +0200https://ask.sagemath.org/question/8985/convert-expression-to-function/?answer=13597#post-id-13597Comment by Mathemage for <pre><code>sage: s = sin(x)
sage: f(x) = s
sage: f
x |--> sin(x)
</code></pre>
<p>No deprecation warning! What's going on here is that <code>s(x)</code> was really trying to evaluate your symbolic expression <code>s</code> at the point <code>x</code> - which happened to be a variable, but is still deprecated. If you use the transitive property, the above is just saying</p>
<pre><code>sage: f(x) = sin(x)
</code></pre>
<p>which is what you want, as opposed to</p>
<pre><code>sage: f(x) = sin(x)(x)
</code></pre>
<p>in your first attempt, which is perhaps ambiguous.</p>
https://ask.sagemath.org/question/8985/convert-expression-to-function/?comment=19718#post-id-19718Great, 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?Thu, 31 May 2012 18:59:00 +0200https://ask.sagemath.org/question/8985/convert-expression-to-function/?comment=19718#post-id-19718Comment by kcrisman for <pre><code>sage: s = sin(x)
sage: f(x) = s
sage: f
x |--> sin(x)
</code></pre>
<p>No deprecation warning! What's going on here is that <code>s(x)</code> was really trying to evaluate your symbolic expression <code>s</code> at the point <code>x</code> - which happened to be a variable, but is still deprecated. If you use the transitive property, the above is just saying</p>
<pre><code>sage: f(x) = sin(x)
</code></pre>
<p>which is what you want, as opposed to</p>
<pre><code>sage: f(x) = sin(x)(x)
</code></pre>
<p>in your first attempt, which is perhaps ambiguous.</p>
https://ask.sagemath.org/question/8985/convert-expression-to-function/?comment=19714#post-id-19714You could use `s.variables()` and `s.variables()[0]` 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()[0]) = 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.Fri, 01 Jun 2012 09:16:15 +0200https://ask.sagemath.org/question/8985/convert-expression-to-function/?comment=19714#post-id-19714