登录论坛

查看完整版本 : Different EigenVALUES between matlab and Python


poster
2019-11-27, 00:02
<p>I have two 19x19 covariance matrices <code>Ron2</code> and <code>Roff2</code>, and need to translate matlab <code>eig(Ron2, Roff2)</code> to a python <code>scipy.linalg.eig(Ron2, Roff2)</code> so that the eigenvalues e follow Ron2*e = Roff2. I know the returned eigenvectors don't have to be the same values because of normalization differences (although they should point in the same direction)</p>

<p>However, the eigenvalues that are returned are not the same, usually. I've verified that the two matrices are indeed the same between the two languages. Here's what I've found:</p>

<ul>
<li><p>On a separate machine with RHEL6 python 3.6.5, scipy 1.1.0, and numpy 1.14.3: Correct eigenvalues (this machine is also not connected to the data server)</p></li>
<li><p>On the desired machine with a custom conda environment with RHEL7, python 3.5.5/3.6.5, numpy 1.14.3/1.17.4, and scipy 1.1.0/1.3.1/1.3.3/1.3.4rc0 : Incorrect eigenvalues</p></li>
<li><p>On separate machines with RHEL6/RHEL7, python3.6.5, scipy 1.1.0, and numpy 1.14.3: Incorrect eigenvalues (these are connected to the data server, but not preferred)</p></li>
<li><p>Here's the fun part. If I isolate and save the matrices, then run the eigenvalue calc in a dummy matlab side script, I get matching eigenvalues to the 'incorrect' ones from the machines I mentioned above.</p></li>
</ul>

<p>Does anyone have any ideas why the values won't be the same, despite the redhat/python/numpy/scipy versions matching? Are there other dependencies I don't know about that I should match? Since it is two square matrices, I can't use the numpy eigenvalue solver. The end goal of this code is to grab the normalized e-vector corresponding to the maximum eigenvalue. The matlab version used is '9.2.0.556344(R2017a)'. We also don't have to assume which set of eigenvalues is 'correct'.</p>

<p>Code snippets: </p>

<pre><code>[V,e] = eig(Ron2,Roff2,'vector');
[d,idx]=max(e);
v=V(:,idx);
a=Roff2*v;
a=a./norm(a);
</code></pre>

<p>and</p>

<pre><code>e,V=scipy.linalg.eig(Ron2,Roff2)
d=e.max()
idx=np.argmax(e)
v=V[:,idx]
a=np.matmul(Roff2,v)
a=a/np.linalg.norm(a)
</code></pre>

<p>These snippets run many times with the data I have, so I'm just testing the eigenvalues on the Ron2,Roff2 that goes through the first time. What is with the discrepancy?</p>



More answer... (https://stackoverflow.com/questions/59054702/different-eigenvalues-between-matlab-and-python)