-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
ENH: Root finding #417
ENH: Root finding #417
Conversation
Based on Scipy's newton methods, we add two variants of newton root finding - Newton Raphson and secant method. All methods are jitted through Numba with nopython mode. Relevant information (apart from the root) is also returned in the result as python namedtuple since Numba supports it.
Great, thanks. Nice looking code. @mmcky using If we do use named tuples then let's also change our existing maximization function accordingly. I can't think of a better approach to accommodating the fact that only only primitive arguments can be optional. @oyamad Do you have any suggestions on how to better structure this code? This is a trivial point, but I thought I read somewhere that having multiple return statements within one function is frowned on these days in software circles. It that not so? In any case you might also save a few bytes by replacing your multiple return statements with |
@jstac I am not sure if I really understand the issue, but the following generates different code for different signatures (which behaves like optional arguments from the user's perspective): from numba import jit, generated_jit, types
@generated_jit(nopython=True)
def test(x, f=None, f2=None):
if isinstance(f, types.Callable):
if isinstance(f2, types.Callable):
def return_f2(x, f, f2):
return f2(x)
return return_f2
# else
def return_f(x, f, f2):
return f(x)
return return_f
#else
def return_0(x, f, f2):
return 0
return return_0 @jit(nopython=True)
def f(x):
return x
@jit(nopython=True)
def f2(x):
return 2*x test(10)
0 test(10, f)
10 test(10, f, f2)
20 But, in my opinion, this would unnecessarily complicate the structure of the code and increase the maintenance costs. Having different names for different sets of arguments (
|
The third and last newton method for root finding is now added with its test cases. Each newton method is modified to include only a single return statement.
thanks for your review @jstac and @oyamad. Thanks @chrishyland and @spvdchachan for this PR. |
thanks again for everyone's input. I will merge this PR now. |
Hi @jstac, in this PR @spvdchachan and I added in the Jitted Newton methods for root finding, In particular, the
newton
method and thenewton_secant
method. Additionally, we wrote some test cases too.As several people mentioned in #416 , extra information about convergence, iterations, etc. is crucial.
Therefore, we provided this extra information but instead used the
namedtuple
collection for fields to be accessed easier through names rather than numerical indexes in normal tuples.We followed the source code directly from Scipy but there were several technical issues with Numba.
fprime
andfprime2
optional. Currently, we've only implemented 2 of the functions.warnings
. If this is crucial, we have to find a workaround.@jstac