<h3 dir="auto">Describe your issue.</h3>
<p dir="auto">One way to speed up scipy.optimize.minimize is to compute the gradient when computing the function value.  Because most of the computation are shared, this nearly half the amount of time required.</p>
<p dir="auto">Although shgo document this under the "options" parameter, it actually doesn't work: _shgo_lib/triangulation.py tries to compare the results when calling the function twice.  But because the result is a numpy array, it fails with a ValueError: "The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()".</p>
<h3 dir="auto">Reproducing Code Example</h3>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import numpy as np
from scipy.optimize import minimize, shgo

def f(x):
    return np.sum(x * x), 2 * x

# This succeeds
print(minimize(f, [0.5, 0.5], jac=True))
# This fails
print(shgo(f, [(-1., 1.), (-1., 1.)], options={'jac': True}))"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>
<span class="pl-k">from</span> <span class="pl-s1">scipy</span>.<span class="pl-s1">optimize</span> <span class="pl-k">import</span> <span class="pl-s1">minimize</span>, <span class="pl-s1">shgo</span>

<span class="pl-k">def</span> <span class="pl-en">f</span>(<span class="pl-s1">x</span>):
    <span class="pl-k">return</span> <span class="pl-s1">np</span>.<span class="pl-en">sum</span>(<span class="pl-s1">x</span> <span class="pl-c1">*</span> <span class="pl-s1">x</span>), <span class="pl-c1">2</span> <span class="pl-c1">*</span> <span class="pl-s1">x</span>

<span class="pl-c"># This succeeds</span>
<span class="pl-en">print</span>(<span class="pl-en">minimize</span>(<span class="pl-s1">f</span>, [<span class="pl-c1">0.5</span>, <span class="pl-c1">0.5</span>], <span class="pl-s1">jac</span><span class="pl-c1">=</span><span class="pl-c1">True</span>))
<span class="pl-c"># This fails</span>
<span class="pl-en">print</span>(<span class="pl-en">shgo</span>(<span class="pl-s1">f</span>, [(<span class="pl-c1">-</span><span class="pl-c1">1.</span>, <span class="pl-c1">1.</span>), (<span class="pl-c1">-</span><span class="pl-c1">1.</span>, <span class="pl-c1">1.</span>)], <span class="pl-s1">options</span><span class="pl-c1">=</span>{<span class="pl-s">'jac'</span>: <span class="pl-c1">True</span>}))</pre></div>
<h3 dir="auto">Error message</h3>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="File .../scipy/optimize/_shgo_lib/triangulation.py:597, in &lt;genexpr&gt;(.0)
    595 &quot;&quot;&quot;Check whether this vertex is strictly less than all its neighbors&quot;&quot;&quot;
    596 if self.check_min:
--&gt; 597     self._min = all(self.f &lt; v.f for v in self.nn)
    598     self.check_min = False
    600 return self._min

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()"><pre class="notranslate">File .../scipy/optimize/_shgo_lib/triangulation.py:597, <span class="pl-k">in</span> <span class="pl-k">&lt;</span>genexpr<span class="pl-s"><span class="pl-pds">&gt;(</span>.0<span class="pl-pds">)</span></span>
    595 <span class="pl-s"><span class="pl-pds">"</span><span class="pl-pds">"</span><span class="pl-pds">"</span>Check whether this vertex is strictly less than all its neighbors<span class="pl-pds">"</span><span class="pl-pds">"</span><span class="pl-pds">"</span></span>
    596 <span class="pl-k">if</span> self.check_min:
--<span class="pl-k">&gt;</span> 597     self._min = all(self.f <span class="pl-k">&lt;</span> v.f <span class="pl-k">for</span> <span class="pl-smi">v</span> <span class="pl-k">in</span> self.nn)
    598     self.check_min = False
    600 <span class="pl-k">return</span> self._min

ValueError: The truth value of an array with more than one element is ambiguous. Use <span class="pl-en">a.any</span>() or <span class="pl-en">a.all</span>()</pre></div>
<h3 dir="auto">SciPy/NumPy/Python version information</h3>
<p dir="auto">1.7.3 1.21.6 sys.version_info(major=3, minor=9, micro=5, releaselevel='final', serial=0)</p>
<p dir="auto"><em>Original ticket <a href="http://projects.scipy.org/scipy/ticket/350" rel="nofollow">http://projects.scipy.org/scipy/ticket/350</a> on 2007-01-19 by <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/nilswagner01/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/nilswagner01">@nilswagner01</a>, assigned to unknown.</em></p>
<p dir="auto">Iterative solvers need some kind of a monitoring function to observe the convergence behaviour.</p>