Ask Your Question
1

Evaluating symbolic expressions

asked 2015-03-08 13:53:13 -0500

Paco Braxe gravatar image

Hi all,

I have to evaluate in Sage a symbolic expression and, by some constrains of my problem, I have to use a dictionary to do so. Everything goes smooth with symbolic expressions like the following:

var('x,y')
h = x^3+y^3 
type(h) #returns sage.symbolic.expression.Expression 
h({x:0,y:1}) #returns 1

But, when I use a symbolic expression defined with arguments, it fails:

g(x,y) = x^3+y^3 
type(g) #returns also sage.symbolic.expression.Expression 
g({x:0,y:1})

And returns "TypeError: no canonical coercion from <type 'dict'=""> to Callable function ring with arguments (x, y)"

Since both expressions are "sage.symbolic.expression.Expression", why does that occur? Any help would be appreciated.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2015-03-08 14:01:52 -0500

tmonteil gravatar image

Symbolic expressions is a big bundle of stuff. For example, you can notice that pi is also symbolic expression:

sage: type(pi)
<type 'sage.symbolic.expression.Expression'>

Some equations are also symbolic expressions:

sage: type(x^2 + 1 == x)
<type 'sage.symbolic.expression.Expression'>

You can notice, that g and h have the same type, but not the same parent:

sage: g.parent()
Callable function ring with arguments (x, y)
sage: h.parent()
Symbolic Ring

Since g is callable, the way to evaluate it is supposed to be:

sage: g(0,1)
1

If you want do deal uniformly with g and h, you can use the .subs() method:

sage: h.subs({x:0,y:1})
1
sage: g.subs({x:0,y:1})
(x, y) |--> 1
edit flag offensive delete link more

Comments

Thank you tmonteil. You are my hero!

Paco Braxe gravatar imagePaco Braxe ( 2015-03-08 14:09:49 -0500 )edit

Your Answer

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

Add Answer

Question Tools

1 follower

Stats

Asked: 2015-03-08 13:53:13 -0500

Seen: 341 times

Last updated: Mar 08 '15