Numpy computes wrong eigeinvectors
<p>I was computing the eigenvectors of a matrix using numpy and I was getting some weird results. Then I decided to use Matlab and everything was fine.</p>

<pre><code>L = np.array(([2,-1,-1],[-1,2,-1],[-1,-1,2]))
Lam,U = np.linalg.eig(L) #compute eigenvalues and vectors
#sort by ascending eigenvalues
I = [i[0] for i in sorted(zip(xrange(len(Lam)),Lam),key=lambda x: x[1])] 
Lam = Lam[I]
U = U[:,I]
print U.dot(U.T)

&gt;&gt; [[ 1.09 -0.24  0.15]
   [-0.24  1.15  0.09]
   [ 0.15  0.09  0.76]]
</code></pre>

<p>The result was weird because I was expecting <code>U.dot(U.T) = I</code>. In matlab:</p>

<pre><code>L = [2,-1,-1;-1,2,-1;-1,-1,2]
[V,D] = eig(L)
V*V'
ans =
    1.0000    0.0000   -0.0000
    0.0000    1.0000    0.0000
    0.0000    0.0000    1.0000
</code></pre>

<p>By the way U:</p>

<pre><code>[[-0.58  0.82  0.29]
 [-0.58 -0.41 -0.81]
 [-0.58 -0.41  0.51]]
</code></pre>

<p>and V:</p>

<pre><code>0.5774    0.7634    0.2895
0.5774   -0.6325    0.5164
0.5774   -0.1310   -0.8059
</code></pre>

<p>What's going on?</p>

<p>I think the problem is that your matrix is NOT reversible</p>

<pre><code>np.linalg.det(L) # Return 0
Lam # Return [3, 3, 0]
</code></pre>

<p>So:</p>

<pre><code>det(L * U) = det(L) * det(U) = 0 != det(I) = 1
</code></pre>

python matlab numpy