Ask Your Question
1

Evaluating symbolic expressions

asked 10 years ago

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.

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
1

answered 10 years ago

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
Preview: (hide)
link

Comments

Thank you tmonteil. You are my hero!

Paco Braxe gravatar imagePaco Braxe ( 10 years ago )

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: 10 years ago

Seen: 4,806 times

Last updated: Mar 08 '15