Ask Your Question
0

Evaluate with a data set?

asked 2011-01-14 11:47:17 +0100

stanislav gravatar image

updated 2011-04-28 17:32:25 +0100

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
        3
    A + B
        A + B

Thanks for help.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2011-01-14 11:54:52 +0100

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)
3

This will substitute as a dictionary. You can also do

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

if you like that syntax. Does that help?

edit flag offensive delete link more

Comments

@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 12:03:34 +0100 )edit
0

answered 2011-01-14 13:02:19 +0100

stanislav gravatar image

updated 2011-01-14 13:02:36 +0100

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:

A+B,data

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

edit flag offensive delete link more

Comments

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 13:29:28 +0100 )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

Stats

Asked: 2011-01-14 11:47:17 +0100

Seen: 540 times

Last updated: Jan 14 '11