# Evaluating symbolic expressions

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

Sort by ยป oldest newest most voted

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

more

Thank you tmonteil. You are my hero!

( 2015-03-08 20:09:49 +0200 )edit