Description
The implementation of e.g. nper
:
https://github.com/numpy/numpy-financial/blob/master/numpy_financial/_financial.py#L229
requires the usual “split the array up using boolean indexes and then do different things to each hunk” stuff that you have to do when writing a function with conditional logic that operates on an entire array. This tends to lead to convoluted, hard-to-maintain code. (And nper
still has more edge cases that need to be handled, so there will need to be more boolean arrays!)
One solution to this problem is to write ufuncs so that you can work with scalars and just use conditional logic. The numpy_financial
functions can’t be full ufuncs because they have default arguments, but most of them can be very thin wrappers around a ufunc.
The disadvantage of introducing ufuncs is that you introduce compiled code, but perhaps it is worth it? Code for generating the loops from scalar kernels can be grabbed from SciPy (though it can be stripped down for numpy_financial
since less languages need to be supported).