[SciPy-User] Alternatives to scipy.optimize

Nathaniel Smith njs@pobox....
Mon Feb 27 10:33:00 CST 2012

On Sun, Feb 26, 2012 at 3:14 PM, Matthew Newville
<matt.newville@gmail.com> wrote:
>     from lmfit import Parameter
>     Parameter(name='a', value=10, vary=True)
>     Parameter(name='b', expr='sqrt(a) + 1')
> will compile 'sqrt(a)+1' into its AST representation and evaluate that for
> the value of 'b' when needed.  So lmfit doesn't so much manipulate the AST
> as interpret it.  What is  manipulated is the namespace, so that 'a' is
> interpreted as "look up the current value of Parameter 'a'" when the AST is
> evaluated.   Again, this applies only for algebraic constraints on
> parameters.
> Having written fitting programs that support user-supplied algebraic
> constraints between parameters in Fortran77, I find interpreting python's
> AST to be remarkably simple and robust.  I'm scared much more by statistical
> modeling of economic data ;)

So you use the 'ast' module to convert Python source into a syntax
tree, and then you wrote an interpreter for that syntax tree?

...Wouldn't it be easier to use Python's interpreter instead of
writing your own, i.e., just call eval() on the source code? Or are
you just using the AST to figure out which variables are referenced?
(I have some code to do just that without going through the ast
module, on the theory that it's nice to be compatible with python 2.5,
but I'm not sure it's really worth it.)

-- Nathaniel

More information about the SciPy-User mailing list