First time here? Check out the FAQ!

Ask Your Question
0

Evaluate with a data set?

asked 14 years ago

stanislav gravatar image

updated 13 years ago

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.

Preview: (hide)

2 Answers

Sort by » oldest newest most voted
1

answered 14 years ago

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?

Preview: (hide)
link

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 ( 14 years ago )
0

answered 14 years ago

stanislav gravatar image

updated 14 years ago

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.

Preview: (hide)
link

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 ( 14 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

Stats

Asked: 14 years ago

Seen: 567 times

Last updated: Jan 14 '11