[SciPy-User] fsolve - pass keyword arguments

Fabrice Silva silva at lma.cnrs-mrs.fr
Thu Feb 17 10:40:09 EST 2011


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




More information about the SciPy-User mailing list