Ask Your Question

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

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 close merge delete

## 3 answers

Sort by » oldest newest most voted

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.

more

## Comments

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

(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.rhs()
print c
print type(c)
print c(x=5)


Output:

x + 2
<type 'sage.symbolic.expression.Expression'>
7

more

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


I doubt there is a more terse workaround.

more

## Your Answer

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

Add Answer

## Stats

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

Seen: 656 times

Last updated: Apr 06 '12