Is there any way to inject a variable in the local environment of a function ?

asked 2019-03-23 18:51:23 +0200

Emmanuel Charpentier gravatar image

updated 2019-03-23 19:25:07 +0200

Inspiration: in this recent question, the author looked for a way to act on variables introduced by a function call (this happens in some Maxima or Sympy functions such as solve or desolve, which need to introduce constants, for example).

It is (relatively) easy to wrap such calls detecting and creating such "new" symbolic variables. It is also easy to inject them in the global namespace. See my answer for a first (somewhat awkward) proposal.

But the case may be thatr these variables are useful in the context of a function wrapping the call to a maxima- or Sympy-derived function, but irrelevant to the global context. Example : the solution to a variational problem might involve calls to desolve, in which such constants are created, the used later on to solve for boundary conditions. At this poiont, the created variables are no longer useful.

It would be therefore useful to inject the new variables in the calling function's local namespace. But I do not know how to do this.

Ultimately, I'd like to write a context for such variable-generating calls managing such new variables.

Any suggestions ?

edit retag flag offensive close merge delete