## eigs bug for 0 as lowest eigenvalue in parts of the matlab versions

### pages larry (view profile)

on 18 May 2019 at 1:52
Latest activity Commented on by pages larry

### Matt J (view profile)

I'm eigs a sparse diagonal matrix that has 0 as the lowest eigenvalue but in some new versions of matlab it gets 1 as the wrong answer.
This is a code that get wrong results in matlab 2017b and 2018b2017b and 2018b, while gets the right answer in matlab 2016a and the online matlab. I'm comparing eig and eigs for identical matrix. The right answer will be always 0 0, and the wrong answer will always be 1 and 0. Sparse eigs has missed 0 as my lowest eigenvalue.
So is there any way to gurantee it right?
%%% my code
clearvars
clc
matrix = diag(0:20);
% matrix(1,1)=1;
% matrix(2,2)=0;
sp_matrix = sparse(matrix);
Eg_sp = eigs(sp_matrix,1,'sa');
Es = eig(matrix);
Es = sort(Es);
Eg_full = Es(1);
fprintf('Eg by sparse matrix is %f\n',Eg_sp)
fprintf('Eg by full matrix is %f\n',Eg_full)

Walter Roberson

### Walter Roberson (view profile)

on 18 May 2019 at 5:44
R2019a for Mac gives -9.04753446879501e-17 for the Eg_sp, but R2018b for Mac gives 1 - 1.4432899320127e-15

### Matt J (view profile)

on 18 May 2019 at 17:35
Edited by Matt J

### Matt J (view profile)

on 18 May 2019 at 17:45

Increasing the SubspaceDimension also works, so it's hard to be certain if this is actually a bug.
sp_matrix = sparse( diag(0:20) );
[~,result1,flag]=eigs(sp_matrix,1,'sa','SubspaceDimension',21)
results in
result1 =
0
flag =
0

pages larry

### pages larry (view profile)

Setting SubspaceDimension does work! Thank you.
I think the bug is that when you
eigs(sparse(diag(0:200)),1,'sa','SubspaceDimension',D)
one should set SubspaceDimension D to 201 accordingly. If one didn't set it or D set is too small, eigs will miss the 0 eigenvalue of the matrix.
But since now according to your example I know which D should be set (D should be 201) so that this bug can be bypassed.
nice job!
:)
pages larry

### pages larry (view profile)

But the price is that eigs would be much much slower when D is large.

### Matt J (view profile)

on 18 May 2019 at 2:28

That does seem like an interesting bug. This seems to work though
Eg_sp = eigs(sp_matrix,1,1e-12);

pages larry

### pages larry (view profile)

on 18 May 2019 at 2:35
Thank you for your replying. But this bug was found in my exact diagonalization program, where the ground state energy in the program is not neccessarily near 0. So I cannot do that.

### Matt J (view profile)

on 18 May 2019 at 2:38

If the matrix is known a priori to be diagonal, it makes more sense to just do
Eg_sp=min(abs(diag(sp_matrix)))

pages larry

### pages larry (view profile)

on 18 May 2019 at 2:47
But unfortuanely, the original matrix was not neccessarily diagonal :). It was a very general sparse matrix to be diagonalized. I found eigs would miss Eg=0 in diagonal cases but I'm not sure in orther cases where matrix is non-diagonal but has Eg=0.
I think the best solution is that matlab developers could solve this bug though.
Matt J

### Matt J (view profile)

on 18 May 2019 at 17:21
I think the best solution is that matlab developers could solve this bug though.
I agree. I encourage you to submit a bug report.

### David Goodmanson (view profile)

on 18 May 2019 at 2:54
Edited by David Goodmanson

### David Goodmanson (view profile)

on 18 May 2019 at 2:58

Hi pl,
2018b does not list 'sa' as an option for eigs, so I am speculating that that 'sa' is legacy. However, compariing 'sa' to 'smallestabs',
matrix = diag(0:20);
sp_matrix = sparse(matrix);
Eg_sp = eigs(sp_matrix,1,'sa')
Eg_sp = 1.0000 % incorrect
Eg_sp = eigs(sp_matrix,1,'smallestabs') % this errors out,
% error message suggests that the smallest eigenvalue is 0
% checking for small nonzero smallest eigenvalue
sp_matrix(1,1)=1e-10;
Eg_sp = eigs(sp_matrix,1,'smallestabs')
Eg_sp = 1.0000e-10
sp_matrix(1,1)=1e-100;
Eg_sp = eigs(sp_matrix,1,'smallestabs')
Eg_sp = 1.0000e-100 % but with a warning that the matrix is almost singular
so it appears that you could trap for zero eigenvalue, and then the other cases are covered.

pages larry

### pages larry (view profile)

on 18 May 2019 at 2:58
In matlab 2018b both 'sa' and 'smallestreal' will encounter this bug and give the same answer.
David Goodmanson

### David Goodmanson (view profile)

on 18 May 2019 at 3:13
well, that sucks.

### Matt J (view profile)

on 18 May 2019 at 17:20

Here is another possible workaround based on svds
[V,d] = svds(sp_matrix.'*sp_matrix,1,'smallest');
Eg_sp= V'*sp_matrix*V;

pages larry