Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass extra arguments to a function whose root to find using the "broyden1" solver #3562

Closed
ghost opened this issue Apr 18, 2014 · 2 comments
Closed
Labels
query A question or suggestion that requires further information

Comments

@ghost
Copy link

ghost commented Apr 18, 2014

Hello,

I'm interested in passing extra arguments to a function whose root to find using the "broyden1" solver of scipy.optimize. I wasn't able to find the solution online so I hope to find it here. I'm using scipy-0.12.1-1.

The following nominal case works fine:

from numpy import cos
from scipy.optimize import broyden1
def F(x):
    return cos(x) + x[::-1] - [1, 2, 3, 4]
x = broyden1(F, [1,1,1,1], f_tol=1e-14)
print x

However, I would like to be able to define F such as:

def F(x, a, b):
    return cos(x) + x[::-1] - [1, 2, 3, 4] + a + b

The "broyden1" function doesn't have the usual arguments "args" to pass extra arguments to F. I tried to modified the "nonlin.py" source code in scipy.optimize in vain (I tried to add "args=()" in line 223 and "args" in line 273, both as argument of each function in question).

I would like to know if there is an easy fix. Thank you.

@argriffing
Copy link
Contributor

You can do something like

from functools import partial
from numpy import cos
from scipy.optimize import broyden1
def F(a, b, x):
    return cos(x) + x[::-1] - [1, 2, 3, 4] + a + b
x = broyden1(partial(F, a, b), [1,1,1,1], f_tol=1e-14)
print x

@ghost
Copy link
Author

ghost commented Apr 19, 2014

Thanks argriffing for your quick reply! Your solution solved my problem. I didn't know about the function "partial". It is good to know.

@ghost ghost closed this as completed Apr 19, 2014
@rgommers rgommers added the query label Apr 19, 2014
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
query A question or suggestion that requires further information
Projects
None yet
Development

No branches or pull requests

2 participants