Ask Your Question

Evaluate with a data set?

asked 2011-01-14 04:47:17 -0600

stanislav gravatar image

updated 2011-04-28 10:32:25 -0600

Kelvin Li gravatar image

Can sage evaluate an expression with a given data set like ev function in maxima?

For example something like:

    A,B = var('A,B')
    data = [A==1,B==2]
    A + B with data
    A + B
        A + B

Thanks for help.

edit retag flag offensive close merge delete

2 answers

Sort by » oldest newest most voted

answered 2011-01-14 04:54:52 -0600

kcrisman gravatar image

I think you are looking for the subs() method of symbolic expressions.

sage: var('A B')
(A, B)
sage: data = {A:1,B:2}
sage: expr = A+B
sage: expr
A + B
sage: expr.subs(data)

This will substitute as a dictionary. You can also do

sage: expr.subs(A=1,B=2)

if you like that syntax. Does that help?

edit flag offensive delete link more


@stanislav: As kcrisman does, note that it's more Pythonic to store key, value pairs in a dict than as a list of equations. That said, (a+b).subs_expr(*data) should work for lists of equations too.

DSM gravatar imageDSM ( 2011-01-14 05:03:34 -0600 )edit

answered 2011-01-14 06:02:19 -0600

stanislav gravatar image

updated 2011-01-14 06:02:36 -0600

Thanks, the second variant is the more comfortable for me, because then I can use a list of sets and put it into a for loop.

I think, the is no better way in sage because the way, you can use it in maxima can not be used in python. In maxima you can give the data list after an expression just by typing a comma like:


But I'm glad, the way with subs is working fine.

edit flag offensive delete link more


Great - especially feel free to use the *list unpacking syntax DSM mentions. (You can also accept the answer if you like it enough :-) )

kcrisman gravatar imagekcrisman ( 2011-01-14 06:29:28 -0600 )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


Asked: 2011-01-14 04:47:17 -0600

Seen: 185 times

Last updated: Jan 14 '11