Ask Your Question
1

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

asked 2012-04-05 04:06:26 +0100

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
3

answered 2012-04-06 13:01:01 +0100

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

Comments

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 13:24:44 +0100 )edit
2

answered 2012-04-05 04:49:53 +0100

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.

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'>
7
edit flag offensive delete link more
0

answered 2012-04-05 12:38:11 +0100

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')
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.

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

Stats

Asked: 2012-04-05 04:06:26 +0100

Seen: 1,095 times

Last updated: Apr 06 '12