First time here? Check out the FAQ!

Ask Your Question
0

Difficulties evaluating an expression string

asked 11 years ago

Alasdair gravatar image

updated 11 years ago

For some recent computations, I devolved some initial calculus to the Maxima sub-system, and I've ended up with a list eqs of Maxima expressions. I want to transfer this list out of Maxima. This works:

sage: eqs2 = [repr(xx) for xx in eqs]

but gives me strings, which are not useful. And this:

sage: eqs2 = [sage_eval(repr(xx)) for xx in eqs]

returns the error message

NameError: name 'a43' is not defined

which is strange, as it is defined as a variable:

sage: a43
a43

I could create a list of local variables:

vars = {'a21':a21,'a31':a31}

and so on, and use

sage_eval(xx,vars)

but as there are 20 more variables, this would be tedious.

Is there a better way?

Preview: (hide)

Comments

Thanks very much for your answers: I wasn't aware that the `locals` list could be used like this.

Alasdair gravatar imageAlasdair ( 11 years ago )

2 Answers

Sort by » oldest newest most voted
0

answered 11 years ago

ndomes gravatar image

updated 11 years ago

Let sage_eval evaluate in the namespace of the locals dictionary.

expr = sage_eval(xx,locals=locals())

Or do you need something like this:

var_list =['a'+str(k) for k in (11..20)]
vars = dict([(v,var(v)) for v in var_list])
sage_eval('2*a11 +a14^a13-a12',locals=vars)
Preview: (hide)
link
0

answered 11 years ago

Luca gravatar image

sage_eval does not have access by default to the variables declared in your session. Use the locals keword:

sage: eqs2 = [sage_eval(repr(xx), locals=globals()) for xx in eqs]

Depending on what you want to do, it may be more appropriate to use locals() instead of globals(). See http://stackoverflow.com/questions/7969949/whats-the-difference-between-globals-locals-and-vars.

Preview: (hide)
link

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

Seen: 499 times

Last updated: Jan 25 '14