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*xtest(10)
0test(10, f)
10test(10, f, f2)
20But, 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
newtonmethod and thenewton_secantmethod. 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
namedtuplecollection 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.
fprimeandfprime2optional. Currently, we've only implemented 2 of the functions.warnings. If this is crucial, we have to find a workaround.@jstac