# Difficulties evaluating an expression string

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?

edit retag close merge delete

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

Sort by » oldest newest most voted

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.

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

more