Asked by pages larry
on 18 May 2019 at 1:52

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)

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

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

Accepted Answer

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
about 21 hours ago

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
about 3 hours ago

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

Sign in to comment.

Answer by Matt J
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
on 18 May 2019 at 2:35

Sign in to comment.

Answer by Matt J
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
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
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.

Sign in to comment.

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

Edited by David Goodmanson
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
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
on 18 May 2019 at 3:13

well, that sucks.

Sign in to comment.

Answer by Matt J
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
about 2 hours ago

However this code can only find positive eigenvalues, which is often no the case in original ED code. Additionally, by extending the SubspaceDimension would slow down eigs by hundres times when matrix dimension is about 3800.So finally the best way is still turning to matlab versions that don't have this bug.

Thank you for your reply though.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 1 Comment

## Walter Roberson (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/462764-eigs-bug-for-0-as-lowest-eigenvalue-in-parts-of-the-matlab-versions#comment_706410

Sign in to comment.