<p dir="auto">Dear Scipy team,</p>
<p dir="auto">I am the maintainer of a pyFAI and got a bug report that a feature in my code stopped working:<br>
<a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="732799537" data-permission-text="Title is private" data-url="https://github.com/silx-kit/pyFAI/issues/1421" data-hovercard-type="issue" data-hovercard-url="/silx-kit/pyFAI/issues/1421/hovercard" href="https://github.com/silx-kit/pyFAI/issues/1421">silx-kit/pyFAI#1421</a><br>
On my side the bug comes down to the  call to scipy.optimize.fmin_slsqp here:<br>
<a href="https://github.com/silx-kit/pyFAI/blob/master/pyFAI/geometryRefinement.py#L361">https://github.com/silx-kit/pyFAI/blob/master/pyFAI/geometryRefinement.py#L361</a></p>
<p dir="auto">The regression was introduced in scipy v1.5 an now I got a warning with:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="WARNING:py.warnings:/opt/VirtualEnv/py38/lib/python3.8/site-packages/scipy/optimize/_numdiff.py:519: RuntimeWarning: invalid value encountered in true_divide
  J_transposed[i] = df / dx"><pre class="notranslate"><code class="notranslate">WARNING:py.warnings:/opt/VirtualEnv/py38/lib/python3.8/site-packages/scipy/optimize/_numdiff.py:519: RuntimeWarning: invalid value encountered in true_divide
  J_transposed[i] = df / dx
</code></pre></div>
<p dir="auto">and of course the optimization did not work.</p>
<p dir="auto">Until now my code was offering to the use to "fix" some parameter during the optimization, which helps the optimizer to converge faster to the solution. To "fix" the parameter, I was enforcing both bounds to be equal to the fixed value but since the <code class="notranslate">approx_jacobian</code> in slsqp.py changed to call <code class="notranslate">approx_derivative</code> no more optimization occures.</p>
<h4 dir="auto">Reproducing code example:</h4>
<p dir="auto">If I already know the first parameter should be close to 1 and I want to fix it:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; f = lambda x:(x[0]-1)**2*(x[1]-3)**2  # solution expected to be (1,3)                                                                        
&gt;&gt;&gt; scipy.optimize.fmin_slsqp(f, [1,1], bounds=[(1,1), (None, None)])                                            
/opt/VirtualEnv/py38/lib/python3.8/site-packages/scipy/optimize/_numdiff.py:519: RuntimeWarning: invalid value encountered in true_divide
  J_transposed[i] = df / dx
Inequality constraints incompatible    (Exit mode 4)
            Current function value: 0.0
            Iterations: 1
            Function evaluations: 3
            Gradient evaluations: 1
Out[8]: array([1., 1.])"><pre class="notranslate"><code class="notranslate">&gt;&gt;&gt; f = lambda x:(x[0]-1)**2*(x[1]-3)**2  # solution expected to be (1,3)                                                                        
&gt;&gt;&gt; scipy.optimize.fmin_slsqp(f, [1,1], bounds=[(1,1), (None, None)])                                            
/opt/VirtualEnv/py38/lib/python3.8/site-packages/scipy/optimize/_numdiff.py:519: RuntimeWarning: invalid value encountered in true_divide
  J_transposed[i] = df / dx
Inequality constraints incompatible    (Exit mode 4)
            Current function value: 0.0
            Iterations: 1
            Function evaluations: 3
            Gradient evaluations: 1
Out[8]: array([1., 1.])
</code></pre></div>
<h4 dir="auto">Error message:</h4>
<p dir="auto">There was not exception</p>
<h4 dir="auto">Scipy/Numpy/Python version information:</h4>
<p dir="auto">Python: 3.8.0<br>
Numpy: 1.18.4<br>
Scipy: 1.5.0 - 1.5.3</p>
<p dir="auto">You may consider I have been misusing this feature ... but the I need to implement a work around on my side.</p>
<p dir="auto">Hello,</p>
<p dir="auto">It seems that in _numdiff.py module the _dense_difference function does not take into account situation of an optimisation with equal bounds constraints and where initial solution respect this constraint for some .</p>
<p dir="auto">In that case line 519 in _numdiff.py module dx and df are equal to zero and trigger nan in the jacobian.</p>
<p dir="auto">It then triggers an exit n° 4 "Inequality constraints incompatible"</p>
<h4 dir="auto">Scipy/Numpy/Python version information:</h4>
<p dir="auto">1.5.1 1.16.5 sys.version_info(major=3, minor=7, micro=4, releaselevel='final', serial=0)</p>
<p dir="auto">my script does not raise error with following setup</p>
<p dir="auto">1.3.1 1.16.5 sys.version_info(major=3, minor=7, micro=4, releaselevel='final', serial=0)</p>