Skip to content

MAINT: optimize.root_scalar: ensure that root is a scalar #18543

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

Merged
merged 1 commit into from
May 26, 2023

Conversation

mdhaber
Copy link
Contributor

@mdhaber mdhaber commented May 25, 2023

Reference issue

gh-17691

What does this implement/fix?

gh-17691 removed the restriction to use scipy.optimize.root_scalar with method='newton' without fprime; the derivative is calculated numerically by approx_fprime. However, approx_fprime returns an array even with scalar input, so root_scalar would end up returning an array with a single element rather than a scalar. This fixes the inconsistency by ensuring that the scalar derivative is extracted from the output of approx_fprime.

Additional information

import scipy as sp

def f(x):
    return (x - 1) * (x - 2)

res = sp.optimize.root_scalar(f, x0=0)
res.root.shape  # was (1,), now ()

@mdhaber mdhaber added this to the 1.11.0 milestone May 25, 2023
@mdhaber mdhaber requested a review from andyfaff as a code owner May 25, 2023 23:57
Copy link
Member

@tupui tupui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, merging once green :)

@tylerjereddy tylerjereddy added maintenance Items related to regular maintenance tasks scipy.optimize labels May 26, 2023
Copy link
Member

@j-bowhay j-bowhay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @mdhaber

@j-bowhay j-bowhay merged commit ddb6393 into scipy:main May 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maintenance Items related to regular maintenance tasks scipy.optimize
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants