[SciPy-User] fsolve - pass keyword arguments

Ashley DaSilva amd405 at psu.edu
Thu Feb 17 10:43:14 EST 2011


Wow I never knew about lambda functions! That certainly solves the problem.

I had considered a wrapper, but I was trying to avoid it mostly because I
don't want to remember so many function names (this is a piece of a fairly
long python script). Also, I am not sure how much slower it will be with the
extra function call. It is certainly something to keep in mind, though.

Thanks to both of you!
Ashley


On Thu, Feb 17, 2011 at 10:40 AM, Fabrice Silva <silva at lma.cnrs-mrs.fr>wrote:

> El jeu., 17-02-2011 a las 10:19 -0500, Ashley DaSilva escribió:
> > Hello all,
> > I have a function which takes some keyword arguments,
> >
> > def f(x,kw1=None,kw2=None,kw3=None):
> >     (some stuff)
> >     return F
> >
> > Depending on which of the kw arguments is None the function might do
> > extra steps to determine them. I want to use scipy.optimize.fsolve to
> > find the root of this function when kw3 is an integer. I am calling
> > fsolve like this:
> >
> > ans=scipy.optimize.fsolve(f, x0, args=(kw3=N,))
> >
> > where x0 is an initial guess to the root and N is the integer. But I
> > get the error: "SyntaxError: invalid syntax". If I move kw3 to the 2nd
> > position
> >
> > def f(x,kw3=None,kw1=None,kw2=None):
> >     (some stuff)
> >     return F
> >
> > and call like this:
> > ans=scipy.optimize.fsolve(f, x0, args=(N,))
> >
> > then the function works. Does this mean that passing keyword arguments
> > using fsolve is not allowed? Or am I doing something incorrectly?
>
> You could use a wrapper like
> >>> def wrapper(**kw):
> >>>     return [kw.get('kw1'), kw.get('kw2'), kw.get('kw3')]
> >>> ans=scipy.optimize.fsolve(f, x0, args=wrapper(kw=N))
> --
> Fabrice Silva
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20110217/83e29fe3/attachment.html>


More information about the SciPy-User mailing list