Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia : eigs() function returning different values after every evaluation

I noticed that after running eigs() function multiple times, every time it gives different but approximate result.

Is there way to return it every time the same result ? Output is sometimes with "+" sign or "-" sign.

Content of M :

[2, 1]  =  1.0
[3, 1]  =  0.5
[1, 2]  =  1.0
[3, 2]  =  2.5
[1, 3]  =  0.5
[2, 3]  =  2.5

M = M+M'
(d, v) = eigs(M, nev=1, which=:LR)

I tried running same function on same sparse matrix in Python , although the matrix looks bit different I think it is same. Just left values are numbered from 0. In julia they are numbered from 1. I do not know if that is a big difference. Values are approximately same in Julia and Python but in Python they are always the same after every evaluation. Also return values in python are complex numbers, in Julia real.

Python code:

Content of M.T :

from scipy.sparse import linalg

(1, 0)  1.0
(2, 0)  0.5
(0, 1)  1.0
(2, 1)  2.5
(0, 2)  0.5
(1, 2)  2.5

eigenvalue, eigenvector = linalg.eigs(M.T, k=1, which='LR')

Any idea why this behavior is occurring ?

Edit :

These are results of four evaluations of eigs

==========eigvalues==============
[2.8921298144977587]
===========eigvector=============
[-0.34667468634025667
-0.679134250677923
-0.6469878912367839]
=================================

==========eigvalues==============
[2.8921298144977596]
===========eigvector=============
[0.34667468634025655
0.6791342506779232
0.646987891236784]
=================================

==========eigvalues==============
[2.8921298144977596]
===========eigvector=============
[0.34667468634025655
0.6791342506779233
0.6469878912367841]
=================================

==========eigvalues==============
[2.8921298144977583]
===========eigvector=============
[0.3466746863402567
0.679134250677923
0.646987891236784]
=================================
like image 297
M.Puk Avatar asked Mar 22 '16 21:03

M.Puk


1 Answers

The result of eigs depends on the initial vector for the Lanczos iterations. When not specified, it is random so even though all the vectors returned are correct the phase is not guaranteed to be the same over different iterations.

If you want the result to be the same every time, you can set v0 in eigs, e.g.

eigs(M, nev=1, which=:LR, v0 = ones(3))

As long as v0 doesn't change you should get deterministic results.

Note that if you want a deterministic result for testing purposes, you might want to consider a testing scheme that allows phase shifts since the phase can shift with the smallest perturbations. E.g. if you link a different BLAS or change the number of threads the result might change again.

like image 127
Andreas Noack Avatar answered Sep 20 '22 20:09

Andreas Noack