'Using Scipy.Optimize.Minimize efficiently when compting gradient requires function evaluation

so i want to use scipy to minimize a function. In my application i am required to do a function evaluation every time the gradient is required.

I undersand that i can pass a function that will return both, functionvalue and gradient, when i set the arg jac=True. However, sometimes i assume that this procedure will compute gradients when they are not required, e.g. for linesearch, which is very expensive. Is there any way to pass an argument to evaluate the function and an argument to evaluate function and gradient?

EDIT: i also dont want do compute gradient and functionvalue independently by passing fun and jac since then the fun evaluation inside of jac is often wasted.



Solution 1:[1]

You can pass a callable to the jac argument and it will be used to compute the gradient, while fun will still be called to compute the function value.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 yut23