<p dir="auto">My issue is about the <code class="notranslate">minimize_scalar</code> in <code class="notranslate">scipy</code>. Under an older version of <code class="notranslate">scipy</code> this code did to work (at least 1.3.1), now there is some error occurring.</p>
<h4 dir="auto">Reproducing code example:</h4>

<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="from scipy.special import j1
from scipy.optimize import minimize_scalar
res = minimize_scalar(j1, bounds=(1e-10, 1e-05), method='bounded')
res.x"><pre class="notranslate"><code class="notranslate">from scipy.special import j1
from scipy.optimize import minimize_scalar
res = minimize_scalar(j1, bounds=(1e-10, 1e-05), method='bounded')
res.x
</code></pre></div>
<h4 dir="auto">Error message:</h4>

<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="UnboundLocalError                         Traceback (most recent call last)
&lt;ipython-input-2-15220d9f4e21&gt; in &lt;module&gt;
      2 from scipy.optimize import minimize_scalar
      3 
----&gt; 4 res = minimize_scalar(j1, bounds=(1e-10, 1e-5), method='bounded')
      5 res.x

~/anaconda3/lib/python3.7/site-packages/scipy/optimize/_minimize.py in minimize_scalar(fun, bracket, bounds, args, method, tol, options)
    788         if isinstance(disp, bool):
    789             options['disp'] = 2 * int(disp)
--&gt; 790         return _minimize_scalar_bounded(fun, bounds, args, **options)
    791     elif meth == 'golden':
    792         return _minimize_scalar_golden(fun, bracket, args, **options)

~/anaconda3/lib/python3.7/site-packages/scipy/optimize/optimize.py in _minimize_scalar_bounded(func, bounds, args, xatol, maxiter, disp, **unknown_options)
   1878             break
   1879 
-&gt; 1880     if np.isnan(xf) or np.isnan(fx) or np.isnan(fu):
   1881         flag = 2
   1882 

UnboundLocalError: local variable 'fu' referenced before assignment

  ..."><pre class="notranslate"><code class="notranslate">UnboundLocalError                         Traceback (most recent call last)
&lt;ipython-input-2-15220d9f4e21&gt; in &lt;module&gt;
      2 from scipy.optimize import minimize_scalar
      3 
----&gt; 4 res = minimize_scalar(j1, bounds=(1e-10, 1e-5), method='bounded')
      5 res.x

~/anaconda3/lib/python3.7/site-packages/scipy/optimize/_minimize.py in minimize_scalar(fun, bracket, bounds, args, method, tol, options)
    788         if isinstance(disp, bool):
    789             options['disp'] = 2 * int(disp)
--&gt; 790         return _minimize_scalar_bounded(fun, bounds, args, **options)
    791     elif meth == 'golden':
    792         return _minimize_scalar_golden(fun, bracket, args, **options)

~/anaconda3/lib/python3.7/site-packages/scipy/optimize/optimize.py in _minimize_scalar_bounded(func, bounds, args, xatol, maxiter, disp, **unknown_options)
   1878             break
   1879 
-&gt; 1880     if np.isnan(xf) or np.isnan(fx) or np.isnan(fu):
   1881         flag = 2
   1882 

UnboundLocalError: local variable 'fu' referenced before assignment

  ...
</code></pre></div>
<h4 dir="auto">Scipy/Numpy/Python version information:</h4>

<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="1.4.1 1.18.1 sys.version_info(major=3, minor=7, micro=4, releaselevel='final', serial=0)"><pre class="notranslate"><code class="notranslate">1.4.1 1.18.1 sys.version_info(major=3, minor=7, micro=4, releaselevel='final', serial=0)
</code></pre></div>
<p dir="auto">Hi,<br>
I've recently changed my docker configuration and, while using <a href="https://github.com/jonathf/chaospy">chaospy</a> package, I came across an error which never occurred before.<br>
It looks like it comes from the fact that <code class="notranslate">fu</code> is not instantiated before the <code class="notranslate">while</code> loop but I'm not sure.<br>
I hope this report will help.</p>
<h4 dir="auto">Reproducing code example:</h4>

<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import chaospy as cp

distribution = cp.J(cp.Uniform(0, 1), cp.Uniform(0, 1))
abscissas, weights = cp.generate_quadrature(7, distribution, rule=&quot;Leja&quot;, sparse=True)"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">chaospy</span> <span class="pl-k">as</span> <span class="pl-s1">cp</span>

<span class="pl-s1">distribution</span> <span class="pl-c1">=</span> <span class="pl-s1">cp</span>.<span class="pl-v">J</span>(<span class="pl-s1">cp</span>.<span class="pl-v">Uniform</span>(<span class="pl-c1">0</span>, <span class="pl-c1">1</span>), <span class="pl-s1">cp</span>.<span class="pl-v">Uniform</span>(<span class="pl-c1">0</span>, <span class="pl-c1">1</span>))
<span class="pl-s1">abscissas</span>, <span class="pl-s1">weights</span> <span class="pl-c1">=</span> <span class="pl-s1">cp</span>.<span class="pl-en">generate_quadrature</span>(<span class="pl-c1">7</span>, <span class="pl-s1">distribution</span>, <span class="pl-s1">rule</span><span class="pl-c1">=</span><span class="pl-s">"Leja"</span>, <span class="pl-s1">sparse</span><span class="pl-c1">=</span><span class="pl-c1">True</span>)</pre></div>
<h4 dir="auto">Error message:</h4>

<p dir="auto">This error message occured in a Jupyter notebook:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
&lt;ipython-input-1-b2f64640c4d5&gt; in &lt;module&gt;
      1 import chaospy as cp
      2 dist=cp.J(cp.Uniform(0, 1), cp.Uniform(0, 1))
----&gt; 3 points = cp.generate_quadrature(2, dist, rule=&quot;Leja&quot;, sparse=True)

/usr/local/lib/python3.7/dist-packages/chaospy/quadrature/frontend.py in generate_quadrature(order, dist, rule, sparse, accuracy, growth, recurrence_algorithm)
    134         from . import sparse_grid
    135         return sparse_grid.construct_sparse_grid(
--&gt; 136             order, dist, rule=rule, accuracy=accuracy, growth=growth)
    137 
    138     if not isinstance(rule, str):

/usr/local/lib/python3.7/dist-packages/chaospy/quadrature/sparse_grid.py in construct_sparse_grid(order, dist, rule, accuracy, growth, recurrence_algorithm)
    122 
    123     x_lookup, w_lookup = _construct_lookup(
--&gt; 124         orders, dist, rule, accuracy, growth, recurrence_algorithm)
    125     collection = _construct_collection(
    126         order, dist, x_lookup, w_lookup)

/usr/local/lib/python3.7/dist-packages/chaospy/quadrature/sparse_grid.py in _construct_lookup(orders, dists, rules, accuracy, growth, recurrence_algorithm)
    182                 rule=rule,
    183                 growth=growth,
--&gt; 184                 recurrence_algorithm=recurrence_algorithm,
    185             )
    186             x_lookup[-1].append(abscissas)

/usr/local/lib/python3.7/dist-packages/chaospy/quadrature/frontend.py in generate_quadrature(order, dist, rule, sparse, accuracy, growth, recurrence_algorithm)
    157 
    158     quad_function = QUAD_FUNCTIONS[rule]
--&gt; 159     abscissas, weights = quad_function(order, dist, **kwargs)
    160 
    161     assert len(weights) == abscissas.shape[1]

/usr/local/lib/python3.7/dist-packages/chaospy/quadrature/leja.py in quad_leja(order, dist, rule, accuracy, recurrence_algorithm)
    104             *[fminbound(
    105                 objective, abscissas[idx], abscissas[idx+1], full_output=1)[:2]
--&gt; 106               for idx in range(len(abscissas)-1)]
    107         )
    108         index = numpy.argmin(vals)

/usr/local/lib/python3.7/dist-packages/chaospy/quadrature/leja.py in &lt;listcomp&gt;(.0)
    104             *[fminbound(
    105                 objective, abscissas[idx], abscissas[idx+1], full_output=1)[:2]
--&gt; 106               for idx in range(len(abscissas)-1)]
    107         )
    108         index = numpy.argmin(vals)

/usr/local/lib/python3.7/dist-packages/scipy/optimize/optimize.py in fminbound(func, x1, x2, args, xtol, maxfun, full_output, disp)
   1734                'disp': disp}
   1735 
-&gt; 1736     res = _minimize_scalar_bounded(func, (x1, x2), args, **options)
   1737     if full_output:
   1738         return res['x'], res['fun'], res['status'], res['nfev']

/usr/local/lib/python3.7/dist-packages/scipy/optimize/optimize.py in _minimize_scalar_bounded(func, bounds, args, xatol, maxiter, disp, **unknown_options)
   1868             break
   1869 
-&gt; 1870     if np.isnan(xf) or np.isnan(fx) or np.isnan(fu):
   1871         flag = 2
   1872 

UnboundLocalError: local variable 'fu' referenced before assignment"><pre class="notranslate"><code class="notranslate">---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
&lt;ipython-input-1-b2f64640c4d5&gt; in &lt;module&gt;
      1 import chaospy as cp
      2 dist=cp.J(cp.Uniform(0, 1), cp.Uniform(0, 1))
----&gt; 3 points = cp.generate_quadrature(2, dist, rule="Leja", sparse=True)

/usr/local/lib/python3.7/dist-packages/chaospy/quadrature/frontend.py in generate_quadrature(order, dist, rule, sparse, accuracy, growth, recurrence_algorithm)
    134         from . import sparse_grid
    135         return sparse_grid.construct_sparse_grid(
--&gt; 136             order, dist, rule=rule, accuracy=accuracy, growth=growth)
    137 
    138     if not isinstance(rule, str):

/usr/local/lib/python3.7/dist-packages/chaospy/quadrature/sparse_grid.py in construct_sparse_grid(order, dist, rule, accuracy, growth, recurrence_algorithm)
    122 
    123     x_lookup, w_lookup = _construct_lookup(
--&gt; 124         orders, dist, rule, accuracy, growth, recurrence_algorithm)
    125     collection = _construct_collection(
    126         order, dist, x_lookup, w_lookup)

/usr/local/lib/python3.7/dist-packages/chaospy/quadrature/sparse_grid.py in _construct_lookup(orders, dists, rules, accuracy, growth, recurrence_algorithm)
    182                 rule=rule,
    183                 growth=growth,
--&gt; 184                 recurrence_algorithm=recurrence_algorithm,
    185             )
    186             x_lookup[-1].append(abscissas)

/usr/local/lib/python3.7/dist-packages/chaospy/quadrature/frontend.py in generate_quadrature(order, dist, rule, sparse, accuracy, growth, recurrence_algorithm)
    157 
    158     quad_function = QUAD_FUNCTIONS[rule]
--&gt; 159     abscissas, weights = quad_function(order, dist, **kwargs)
    160 
    161     assert len(weights) == abscissas.shape[1]

/usr/local/lib/python3.7/dist-packages/chaospy/quadrature/leja.py in quad_leja(order, dist, rule, accuracy, recurrence_algorithm)
    104             *[fminbound(
    105                 objective, abscissas[idx], abscissas[idx+1], full_output=1)[:2]
--&gt; 106               for idx in range(len(abscissas)-1)]
    107         )
    108         index = numpy.argmin(vals)

/usr/local/lib/python3.7/dist-packages/chaospy/quadrature/leja.py in &lt;listcomp&gt;(.0)
    104             *[fminbound(
    105                 objective, abscissas[idx], abscissas[idx+1], full_output=1)[:2]
--&gt; 106               for idx in range(len(abscissas)-1)]
    107         )
    108         index = numpy.argmin(vals)

/usr/local/lib/python3.7/dist-packages/scipy/optimize/optimize.py in fminbound(func, x1, x2, args, xtol, maxfun, full_output, disp)
   1734                'disp': disp}
   1735 
-&gt; 1736     res = _minimize_scalar_bounded(func, (x1, x2), args, **options)
   1737     if full_output:
   1738         return res['x'], res['fun'], res['status'], res['nfev']

/usr/local/lib/python3.7/dist-packages/scipy/optimize/optimize.py in _minimize_scalar_bounded(func, bounds, args, xatol, maxiter, disp, **unknown_options)
   1868             break
   1869 
-&gt; 1870     if np.isnan(xf) or np.isnan(fx) or np.isnan(fu):
   1871         flag = 2
   1872 

UnboundLocalError: local variable 'fu' referenced before assignment
</code></pre></div>
<h4 dir="auto">Scipy/Numpy/Python version information:</h4>

<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="1.3.3 1.17.4 sys.version_info(major=3, minor=7, micro=3, releaselevel='final', serial=0)"><pre class="notranslate"><code class="notranslate">1.3.3 1.17.4 sys.version_info(major=3, minor=7, micro=3, releaselevel='final', serial=0)
</code></pre></div>