Ask Your Question

Difficulties evaluating an expression string

asked 2014-01-24 13:17:42 -0500

Alasdair gravatar image

updated 2014-01-25 10:49:06 -0500

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

I could create a list of local variables:

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

and so on, and use


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

Is there a better way?

edit retag flag offensive close merge delete


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

Alasdair gravatar imageAlasdair ( 2014-01-25 11:49:22 -0500 )edit

2 answers

Sort by ยป oldest newest most voted

answered 2014-01-25 07:57:14 -0500

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

edit flag offensive delete link more

answered 2014-01-25 07:55:24 -0500

ndomes gravatar image

updated 2014-01-25 11:34:17 -0500

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)
edit flag offensive delete link more

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: 2014-01-24 13:17:42 -0500

Seen: 106 times

Last updated: Jan 25 '14