Ask Your Question

Getting a function from a symbolic expression (i.e. "y = x+2")

asked 2012-04-04 21:06:26 -0500

chasemeadors gravatar image

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. result

edit retag flag offensive close merge delete

3 answers

Sort by » oldest newest most voted

answered 2012-04-06 06:01:01 -0500

You 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.

edit flag offensive delete link more


Just for reference, I believe that the function created in both cases is the same, this just is the "proper" syntax.

kcrisman gravatar imagekcrisman ( 2012-04-06 06:24:44 -0500 )edit

answered 2012-04-04 21:49:53 -0500

chaesloc2 gravatar image

(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.

a = y - 2 == x
b = solve(a, y)
print c
print type(c)
print c(x=5)


x + 2
<type 'sage.symbolic.expression.Expression'>
edit flag offensive delete link more

answered 2012-04-05 05:38:11 -0500

kcrisman gravatar image

I 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')
sage: a = y - 2 == x
sage: b = solve(a, y)
sage: b
[y == x + 2]
sage: c(x) = b[0].rhs()
sage: c(5)

I doubt there is a more terse workaround.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2012-04-04 21:06:26 -0500

Seen: 596 times

Last updated: Apr 06 '12