フィルターのクリア

Different run time of eig() for the same matrix (calculated in 2 different ways)

2 ビュー (過去 30 日間)
Ilya
Ilya 2015 年 8 月 8 日
コメント済み: Ilya 2015 年 8 月 10 日
I hope, a good answer will help understanding of how to use MATLAB in a truly efficient way...
From the same input (stored in input.mat in the attachment), I calculate the matrix M in 2 different ways and then calculate its evals and evecs.
Case 1:
clear;
load('input.mat');
D = d.^(-1/2);
D = diag(D);
M = D*L*D;
[V, v] = eig(M);
Case 2:
clear;
load('input.mat');
d = sqrt(d);
D = d * d';
M = L./D;
[V, v] = eig(M);
Clearly, the 2nd way is more efficient, BUT the resulting matrix M is the same for the 2 cases. So the eig() function should probably have the same running time in the both cases. However, for the first case the run time of eig() is about 12.5 s, and for the second case the run time of eig() is about 1.8 s (the numbers were taken from the profile results).
The input matrix L is a matrix with many zeros (but it's a full matrix). The input vector d is such that isequal(diag(L),d) is true.
I've uploaded the inputs because the above mysterious behaviour is apparently not present for a randomly generated input square matrix.
I guess, it might have something to do with memory efficiency or cache usage, but it would be interesting to know a more precise reason.
  2 件のコメント
the cyclist
the cyclist 2015 年 8 月 8 日
I don't see the uploaded matrix.
Ilya
Ilya 2015 年 8 月 8 日
編集済み: Ilya 2015 年 8 月 8 日
Yes, indeed, I've uploaded now. By the way, I think I now know why.. issymmetric(M) is true for the 2nd case, but false in the 1st case. Due to very small numerical errors (~1e-16), M fails to qualify as a symmetric matrix in the 1st case..

サインインしてコメントする。

採用された回答

John D'Errico
John D'Errico 2015 年 8 月 8 日
編集済み: John D'Errico 2015 年 8 月 8 日
I think what you are missing is that in the second case, it turned out that M was EXACTLY symmetric.
norm(M1-M2) ans = 2.4365e-14
norm(M1-M') ans = 7.1248e-15
norm(M2-M2') ans = 0
I named the two versions of M as M1 and M2.
See that the latter one was symmetric, whereas the former, not.
MANY times in MATLAB the code can take advantage of symmetry. And ALMOST symmetric is not symmetric. It looks like you saw the difference when I read your comments. But MATLAB does not know how close to a symmetric matrix it must be to consider it symmetric. So the code will use the symmetric branch only when that is exactly true.
  4 件のコメント
Walter Roberson
Walter Roberson 2015 年 8 月 10 日
For real square matrices, (M+M.')/2 is symmetric
Ilya
Ilya 2015 年 8 月 10 日
@Walter: Thanks, this is a good (fast) one

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by