Skip to content
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: Vectorized computations in approx_derivative #6345

Closed
wants to merge 4 commits into from

Conversation

nmayorov
Copy link
Contributor

@nmayorov nmayorov commented Jul 2, 2016

Jacobian is estimated numerically for a function fun(x). If fun is implemented such that it accepts and returns 2-d arrays (it is very easy to achieve using vstack when fun is coded in terms of numpy functions), then fun will be called only once to compute all necessary values.

The idea is simple, but it can give substantial speed-ups for estimating Jacobians. Currently only least_squares uses approx_derivative. I will fill an issue that least_squares should be adjusted to allow vectorized Jacobian estimation (when this PR is in).

@ev-br @pv, please look when you have time.

@nmayorov nmayorov added enhancement A new feature or improvement scipy.optimize labels Jul 2, 2016
@ev-br
Copy link
Member

ev-br commented Aug 12, 2016

Do we really need to support non-vectorized computations?

@nmayorov
Copy link
Contributor Author

@ev-br missed your comment. I'm glad that you looked into this.

Someone may have a complicated function (integration of ODE for example), which can't be vectorized efficiently (and vectorizing it is just a tedious and pointless job). So I guess it's necessary to provide both variants, although I agree it would be good to decrease the number of options. Another thing is that non-vectorized version is already used in least_squares.

So what do you think of this pull request?

@ev-br
Copy link
Member

ev-br commented Aug 22, 2016

I think it's a worthy feature. If there's a function which can handle vectorized evaluations, why taking a perf hit of python looping.

However, I'm a bit wary of adding this much of almost-but-not-completely duplicated code.
Maybe it'd be possible to default to a vectorized evaluation, and recommend users wrap their function into np.vectorize otherwise. Or even bake it in, and use this new flag for deciding whether to np.vectorize or not?

@mike1808
Copy link

what is the status of this PR?

@nmayorov
Copy link
Contributor Author

I think it is a useful addition and generally the right way to do things in scipy (i. e. allowing for "doubly" vectorized functions). I don't know, I think it even can be merged right now, then we can include it into least_squares. A general idea is to make this function public and replace calls of other implementations in optimizeby this function. I guess we will have num. diff. functions in a good shape after this year's GSoC (either in scipy.diff or in scipy.diff and scipy.optimize, the latter being used for numerical differentiation in optimization problems, i. .e Jacobian and Hessians including sparse versions).

By the way, why are you asking?

@rgommers rgommers added this to the 1.1.0 milestone Sep 17, 2017
@pv pv modified the milestones: 1.1.0, 1.2.0 Apr 12, 2018
@rgommers rgommers removed this from the 1.2.0 milestone Oct 30, 2018
@mdhaber
Copy link
Contributor

mdhaber commented Oct 8, 2022

However, I'm a bit wary of adding this much of almost-but-not-completely duplicated code. Maybe it'd be possible to default to a vectorized evaluation, and recommend users wrap their function into np.vectorize otherwise. Or even bake it in...

Yes, I'd suggest the same. If the user function can't do vectorized calculations, then we np.vectorize it. I'd also consider going all the way with the vectorization to support Nd arrays unless it's particularly difficult for some reason.

Since this has been inactive for a while and there are merge conflicts, I'll go ahead and close, but all should feel free to reopen to complete this enhancement.

@mdhaber mdhaber closed this Oct 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement A new feature or improvement scipy.optimize
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants