I compute the eigenvectors and eigenvalues of the same matrix in its dense and sparse version. The eigenvalues are the same to great precision, but the eigenvectors are not: when I compute the eigenvalue of the solution eigenvector, I am losing precision around the 10th decimal place. This has serious consequences for the next computation with that eigenvector (computation of entropy for those who know about quantum information physics), it gives a totally different result when computing with dense or sparse matrices.
dim = 10;
Do[
If[n == 1,
ox = {{0., 1}, {1, 0}};
oz = {{1, 0.}, {0, -1}};
id = IdentityMatrix[2],
ox = SparseArray[ox];
oz = SparseArray[oz];
id = SparseArray[id];];
\[Lambda] = 0.1;
hx = 0.; hz = 0.; h = 0.;
Do[
If[i == 1, a = ox; c = oz, a = id; c = id];
hxpart = a; hzpart = c;
Do[
If[j == i || j == i + 1, b = ox, b = id;];
hxpart = KroneckerProduct[hxpart, b];
If[j == i, d = oz, d = id;];
hzpart = KroneckerProduct[hzpart, d];
, {j, 2, dim}];
If[i == dim, hxpart = 0];
hx = hx + hxpart; hz = hz + hzpart;
, {i, 1, dim}];
h = hx + \[Lambda] hz;
nordre = Ordering[Eigensystem[h, 2][[1]], 1][[1]];
gsreala = Chop[Eigensystem[h, 2][[2, nordre]]];
Print[NumberForm[Eigenvalues[h, 4], 15]];
Print[NumberForm[Conjugate[gsreala].h.gsreala, 15]];
(*Entropy computation*)
gsm = Table[gsreala[[2^(dim/2)*i - (2^(dim/2) - 1) + j - 1]], {i, 1,
2^(dim/2)}, {j, 1, 2^(dim/2)}];
singvalues = SingularValueList[gsm];
dimsing = Length[singvalues];
eigenvalues = Table[singvalues[[i]]^2, {i, 1, dimsing}];
epsilon = 10^-10;
H[x_] := If[Norm[x] > epsilon, -x*Log[2, x], 0.];
entreal = N[Sum[H[eigenvalues[[i]]], {i, 1, dimsing}]];
Print[entreal];
, {n, 1, 2}];
These are the results I obtain:
Dense matrix:
Four smallest eigenvalues: {9.03002193787516,-9.03002193787516,-9.03002193767716,9.03002193767714}
Eigenvalue of the smallest eigenvector: -9.03002193787517
Entropy: 1.00001
Sparse matrix:
Four smallest eigenvalues:{-9.03002193787516,9.03002193787516,9.03002193767717,-9.03002193767716}
Eigenvalue of the smallest eigenvector: -9.03002193781702
Entropy: 0.26277
So I am having a loss of precision on the computation of the eigenvector, that ultimately produces a totally different result in the entropy.
0.
in your code, and using the second argument ofN[]
, judiciously, with perhaps slightly more precision than what you need. $\endgroup$