<p dir="auto">The following code fails with an error (<code class="notranslate">numpy.linalg.LinAlgError: SVD did not converge</code>). This occurs if:</p>
<ul dir="auto">
<li>there is this unused import (<code class="notranslate">import scipy.linalg</code>)</li>
<li>a modulo operation is performed (<code class="notranslate">x1 % 2</code>)</li>
</ul>
<p dir="auto">This is unexpcected to me, because the modulo operation shouldn't have any side effects, but even copying the vector won't help.</p>
<p dir="auto">Another interesting observation is that only the first excecution of <code class="notranslate">np.random.multivariate_normal</code> fails. As a workaround I could just catch the first exception and run it again:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="try:
    sample = np.random.multivariate_normal(np.zeros(len(points)), cov)
except np.linalg.LinAlgError as e:
    print(&quot;First try failed:&quot;, e)
sample = np.random.multivariate_normal(np.zeros(len(points)), cov)"><pre class="notranslate"><span class="pl-k">try</span>:
    <span class="pl-s1">sample</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">multivariate_normal</span>(<span class="pl-s1">np</span>.<span class="pl-en">zeros</span>(<span class="pl-en">len</span>(<span class="pl-s1">points</span>)), <span class="pl-s1">cov</span>)
<span class="pl-k">except</span> <span class="pl-s1">np</span>.<span class="pl-s1">linalg</span>.<span class="pl-v">LinAlgError</span> <span class="pl-k">as</span> <span class="pl-s1">e</span>:
    <span class="pl-en">print</span>(<span class="pl-s">"First try failed:"</span>, <span class="pl-s1">e</span>)
<span class="pl-s1">sample</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">multivariate_normal</span>(<span class="pl-s1">np</span>.<span class="pl-en">zeros</span>(<span class="pl-en">len</span>(<span class="pl-s1">points</span>)), <span class="pl-s1">cov</span>)</pre></div>
<p dir="auto">I was able to reproduce this error another windows pc with anaconda python, but not on linux.</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
# this unused import is important to reproduce the weird side effect of modulo calculation
import scipy.linalg


def k(x1, x2):
    base = np.pi * (x1[:, None, :] - x2[None, :, :])
    exp_dist = np.exp(-0.5 * np.sum(np.square(np.sin(base)), axis=-1))

    # the following statement shouldn't have any side effects
    x1 % 2
    #np.mod(x1, 2)
    #np.mod(x1.copy(), 2)

    return exp_dist


n = 10
points = np.atleast_2d(np.linspace(0, 360, 10)).T
cov = k(points, points)
np.random.multivariate_normal(np.zeros(len(points)), cov)"><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-c"># this unused import is important to reproduce the weird side effect of modulo calculation</span>
<span class="pl-k">import</span> <span class="pl-s1">scipy</span>.<span class="pl-s1">linalg</span>


<span class="pl-k">def</span> <span class="pl-en">k</span>(<span class="pl-s1">x1</span>, <span class="pl-s1">x2</span>):
    <span class="pl-s1">base</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-s1">pi</span> <span class="pl-c1">*</span> (<span class="pl-s1">x1</span>[:, <span class="pl-c1">None</span>, :] <span class="pl-c1">-</span> <span class="pl-s1">x2</span>[<span class="pl-c1">None</span>, :, :])
    <span class="pl-s1">exp_dist</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">exp</span>(<span class="pl-c1">-</span><span class="pl-c1">0.5</span> <span class="pl-c1">*</span> <span class="pl-s1">np</span>.<span class="pl-en">sum</span>(<span class="pl-s1">np</span>.<span class="pl-en">square</span>(<span class="pl-s1">np</span>.<span class="pl-en">sin</span>(<span class="pl-s1">base</span>)), <span class="pl-s1">axis</span><span class="pl-c1">=</span><span class="pl-c1">-</span><span class="pl-c1">1</span>))

    <span class="pl-c"># the following statement shouldn't have any side effects</span>
    <span class="pl-s1">x1</span> <span class="pl-c1">%</span> <span class="pl-c1">2</span>
    <span class="pl-c">#np.mod(x1, 2)</span>
    <span class="pl-c">#np.mod(x1.copy(), 2)</span>

    <span class="pl-k">return</span> <span class="pl-s1">exp_dist</span>


<span class="pl-s1">n</span> <span class="pl-c1">=</span> <span class="pl-c1">10</span>
<span class="pl-s1">points</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">atleast_2d</span>(<span class="pl-s1">np</span>.<span class="pl-en">linspace</span>(<span class="pl-c1">0</span>, <span class="pl-c1">360</span>, <span class="pl-c1">10</span>)).<span class="pl-v">T</span>
<span class="pl-s1">cov</span> <span class="pl-c1">=</span> <span class="pl-en">k</span>(<span class="pl-s1">points</span>, <span class="pl-s1">points</span>)
<span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">multivariate_normal</span>(<span class="pl-s1">np</span>.<span class="pl-en">zeros</span>(<span class="pl-en">len</span>(<span class="pl-s1">points</span>)), <span class="pl-s1">cov</span>)</pre></div>

<h3 dir="auto">Error message:</h3>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Traceback (most recent call last):
  File &quot;C:\Users\user\PycharmProjects\fg-localization\other\weird_behavior.py&quot;, line 21, in &lt;module&gt;
    np.random.multivariate_normal(np.zeros(len(points)), cov)
  File &quot;mtrand.pyx&quot;, line 4084, in numpy.random.mtrand.RandomState.multivariate_normal
  File &quot;C:\Users\user\Anaconda3\envs\fg-localization\lib\site-packages\scipy\linalg\decomp_svd.py&quot;, line 132, in svd
    raise LinAlgError(&quot;SVD did not converge&quot;)
numpy.linalg.LinAlgError: SVD did not converge"><pre class="notranslate"><code class="notranslate">Traceback (most recent call last):
  File "C:\Users\user\PycharmProjects\fg-localization\other\weird_behavior.py", line 21, in &lt;module&gt;
    np.random.multivariate_normal(np.zeros(len(points)), cov)
  File "mtrand.pyx", line 4084, in numpy.random.mtrand.RandomState.multivariate_normal
  File "C:\Users\user\Anaconda3\envs\fg-localization\lib\site-packages\scipy\linalg\decomp_svd.py", line 132, in svd
    raise LinAlgError("SVD did not converge")
numpy.linalg.LinAlgError: SVD did not converge
</code></pre></div>
<h3 dir="auto">Numpy/Python version information:</h3>

<p dir="auto">Fails on windows with:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="1.19.1 3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)]"><pre class="notranslate"><code class="notranslate">1.19.1 3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)]
</code></pre></div>
<p dir="auto">Runs on linux with:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="1.19.1 3.6.9 (default, Jul 17 2020, 12:50:27)
[GCC 8.4.0]"><pre class="notranslate"><code class="notranslate">1.19.1 3.6.9 (default, Jul 17 2020, 12:50:27)
[GCC 8.4.0]
</code></pre></div>
<p dir="auto">Tests are failing:<br>
FAILED ....\lib\tests\test_regression.py::TestRegression::test_polyfit_build - numpy.linalg.LinAlgError: SVD did not...<br>
FAILED ....\linalg\tests\test_regression.py::TestRegression::test_eig_build - numpy.linalg.LinAlgError: Eigenvalues ...<br>
FAILED ....\ma\tests\test_extras.py::TestPolynomial::test_polyfit - numpy.linalg.LinAlgError: SVD did not converge i...</p>
<p dir="auto">with exceptions:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="err = 'invalid value', flag = 8
    def _raise_linalgerror_lstsq(err, flag):
&gt;       raise LinAlgError(&quot;SVD did not converge in Linear Least Squares&quot;)
E       numpy.linalg.LinAlgError: SVD did not converge in Linear Least Squares
err        = 'invalid value'
flag       = 8"><pre class="notranslate"><code class="notranslate">err = 'invalid value', flag = 8
    def _raise_linalgerror_lstsq(err, flag):
&gt;       raise LinAlgError("SVD did not converge in Linear Least Squares")
E       numpy.linalg.LinAlgError: SVD did not converge in Linear Least Squares
err        = 'invalid value'
flag       = 8
</code></pre></div>
<p dir="auto">and</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="err = 'invalid value', flag = 8
    def _raise_linalgerror_eigenvalues_nonconvergence(err, flag):
&gt;       raise LinAlgError(&quot;Eigenvalues did not converge&quot;)
E       numpy.linalg.LinAlgError: Eigenvalues did not converge
err        = 'invalid value'
flag       = 8"><pre class="notranslate"><code class="notranslate">err = 'invalid value', flag = 8
    def _raise_linalgerror_eigenvalues_nonconvergence(err, flag):
&gt;       raise LinAlgError("Eigenvalues did not converge")
E       numpy.linalg.LinAlgError: Eigenvalues did not converge
err        = 'invalid value'
flag       = 8
</code></pre></div>
<p dir="auto">Steps taken:</p>
<ul dir="auto">
<li>Create a VM</li>
<li>Install latest Windows 10 and update to the latest version 2004 (10.0.19041)</li>
<li>Install Python 3.8.3</li>
<li><code class="notranslate">pip install pytest</code></li>
<li><code class="notranslate">pip install numpy</code></li>
<li><code class="notranslate">pip install hypothesis</code></li>
<li>run tests in the package</li>
</ul>
<p dir="auto">Same happens issue happens when running on tests in the repository.</p>
<p dir="auto">Version 1.19.0 of numpy</p>
<p dir="auto">Am I missing any dependencies? Or is it just Windows going bonkers?</p>