ENH: optimize._chandrupatla: add array API support #20689
Merged
+375
−246
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Reference issue
gh-7242
What does this implement/fix?
This adds array API support to
scipy.optimize._chandrupatla
and paves the way toward adding array API support to the other elementwise iterative methods (e.g._differentiate
,_tanhsinh
,_nsum
,_chandrupatla_minimize
, and the bracket finders). I'll propose making the rootfinder, minimizer, and bracket finders public shortly.Additional information
The performance of this function compared to the existing bracketing rootfinders is worst when function evaluations are inexpensive, so I compared the performance between
brentq
_chandrupatla
(NumPy)_chandrupatla
(PyTorch, CPU)_chandrupatla
(CuPy)when finding the root of$[0, \pi]$ .
xp.cos(x) - p
for many values ofp
. The bracket is alwaysbrentq
's default absolute x-tolerance is2e-12
, so I set_chandrupatla
to1e-12
to be more than fair, and I confirmed that both solvers meet their respective tolerance._chandrupatla
finds the roots in a single vectorized call whereasbrentq
loops with a list comprehension.The function has a lot of overhead due to bells and whistles (e.g. input validation with nice error messages, rich result object, callback function support, etc.). But for solving a lot of equations, the overhead of function calls eventually becomes problematic for
brentq
.In this case, you could probably get better performance with
cython_optimize
, but for more expensive functions with overhead, like finding theargus(1)
distribution ppf (#17719 (comment)), the advantage is much more pronounced.newton
is definitely faster than this function, but it should be - the algorithm converges faster. (The advantage of bracketing methods is that convergence is guaranteed if the bracket is valid.) We can add that as another method with the same framework.The tests are quite strict; I'm ironing out a few failures with alternative backends.Done if CI looks good.The function currently uses fancy indexing assignment. I can investigate working around that for strict array API support later.