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, 06 Apr 2012 13:24:44 +0200Getting a function from a symbolic expression (i.e. "y = x+2")https://ask.sagemath.org/question/8858/getting-a-function-from-a-symbolic-expression-ie-y-x2/Hi all,
I'm having trouble understanding how to solve the following problem. Let's say, for instance, that I have an expression
a = y - 2 == x
If I wish to solve this for y,
b = solve(a, y)
This returns another object of type Expression that looks like
"y = x + 2"
My question is, is it possible to obtain a callable symbolic expression x --> x + 2 from this result, b?
I'm trying to solve an implicit equation for a variable and obtain a plottable/differentiable etc. resultThu, 05 Apr 2012 04:06:26 +0200https://ask.sagemath.org/question/8858/getting-a-function-from-a-symbolic-expression-ie-y-x2/Answer by chaesloc2 for <p>Hi all,</p>
<p>I'm having trouble understanding how to solve the following problem. Let's say, for instance, that I have an expression</p>
<p>a = y - 2 == x</p>
<p>If I wish to solve this for y,</p>
<p>b = solve(a, y)</p>
<p>This returns another object of type Expression that looks like</p>
<p>"y = x + 2"</p>
<p>My question is, is it possible to obtain a callable symbolic expression x --> x + 2 from this result, b?</p>
<p>I'm trying to solve an implicit equation for a variable and obtain a plottable/differentiable etc. result</p>
https://ask.sagemath.org/question/8858/getting-a-function-from-a-symbolic-expression-ie-y-x2/?answer=13424#post-id-13424(I'm not sure if this is the best way.)
b is a list of solutions, and you want the right hand side of the first solution.
var('x,y')
a = y - 2 == x
b = solve(a, y)
c=b[0].rhs()
print c
print type(c)
print c(x=5)
Output:
x + 2
<type 'sage.symbolic.expression.Expression'>
7Thu, 05 Apr 2012 04:49:53 +0200https://ask.sagemath.org/question/8858/getting-a-function-from-a-symbolic-expression-ie-y-x2/?answer=13424#post-id-13424Answer by kcrisman for <p>Hi all,</p>
<p>I'm having trouble understanding how to solve the following problem. Let's say, for instance, that I have an expression</p>
<p>a = y - 2 == x</p>
<p>If I wish to solve this for y,</p>
<p>b = solve(a, y)</p>
<p>This returns another object of type Expression that looks like</p>
<p>"y = x + 2"</p>
<p>My question is, is it possible to obtain a callable symbolic expression x --> x + 2 from this result, b?</p>
<p>I'm trying to solve an implicit equation for a variable and obtain a plottable/differentiable etc. result</p>
https://ask.sagemath.org/question/8858/getting-a-function-from-a-symbolic-expression-ie-y-x2/?answer=13426#post-id-13426I don't know if there is a better way, except you can make it callable in this sense by doing chaesloc2's thing with the `c(x)` notation.
sage: var('y')
y
sage: a = y - 2 == x
sage: b = solve(a, y)
sage: b
[y == x + 2]
sage: c(x) = b[0].rhs()
sage: c(5)
7
I doubt there is a more terse workaround.Thu, 05 Apr 2012 12:38:11 +0200https://ask.sagemath.org/question/8858/getting-a-function-from-a-symbolic-expression-ie-y-x2/?answer=13426#post-id-13426Answer by burcin for <p>Hi all,</p>
<p>I'm having trouble understanding how to solve the following problem. Let's say, for instance, that I have an expression</p>
<p>a = y - 2 == x</p>
<p>If I wish to solve this for y,</p>
<p>b = solve(a, y)</p>
<p>This returns another object of type Expression that looks like</p>
<p>"y = x + 2"</p>
<p>My question is, is it possible to obtain a callable symbolic expression x --> x + 2 from this result, b?</p>
<p>I'm trying to solve an implicit equation for a variable and obtain a plottable/differentiable etc. result</p>
https://ask.sagemath.org/question/8858/getting-a-function-from-a-symbolic-expression-ie-y-x2/?answer=13430#post-id-13430You can use the `function()` method of symbolic expressions to obtain a callable symbolic expression, which is what you get with `f(x) = x+2`.
sage: b = y == x + 2
sage: b
y == x + 2
sage: f = b.rhs().function(x)
sage: f
x |--> x + 2
Note that you need to specify the argument of the function, in this case `x`, explicitly.
Fri, 06 Apr 2012 13:01:01 +0200https://ask.sagemath.org/question/8858/getting-a-function-from-a-symbolic-expression-ie-y-x2/?answer=13430#post-id-13430Comment by kcrisman for <p>You can use the <code>function()</code> method of symbolic expressions to obtain a callable symbolic expression, which is what you get with <code>f(x) = x+2</code>.</p>
<pre><code>sage: b = y == x + 2
sage: b
y == x + 2
sage: f = b.rhs().function(x)
sage: f
x |--> x + 2
</code></pre>
<p>Note that you need to specify the argument of the function, in this case <code>x</code>, explicitly. </p>
https://ask.sagemath.org/question/8858/getting-a-function-from-a-symbolic-expression-ie-y-x2/?comment=19976#post-id-19976Just for reference, I believe that the function created in both cases is the same, this just is the "proper" syntax.Fri, 06 Apr 2012 13:24:44 +0200https://ask.sagemath.org/question/8858/getting-a-function-from-a-symbolic-expression-ie-y-x2/?comment=19976#post-id-19976