c++ - Different eigenvector and eigenvalues in Eigen and Matlab could generate errors? -
like it's explained here , here orde of eigenvalues (and relative eigenvectors , sign too) library dependent , (according first linked question) shouldn't problem. in addition, eigenvectors relative almost-zero eigenvalues can considered garbage. far good.
now, consider matlab code below want rewrite in c++ using eigen
library:
%supposing k 3x3 matrix [v_k,d_k] = eig(k); d_k = diag(d_k); ind_k = find(d_k > 1e-8); d_k(ind_k) = d_k(ind_k).^(-1/2); k_half = v_k*diag(d_k)*v_k';
and c++ implementation:
eigensolver<matrix3f> es (k,true); auto v = es.eigenvalues(); //set 0 if eigenvalues smal, otherwise v^(-1/2) v = (v.array().real() > 1e-8).select(v.cwisesqrt().cwiseinverse(), 0); auto khalf = es.eigenvectors()*v.asdiagonal()*es.eigenvectors().inverse();
the problem k_half
values different khafl
, can see printed result:
matlab:
v_k = 0.5774 0.8428 -0.0415 0.5774 -0.3806 -0.7468 0.5774 -0.3806 0.6638 d_k = 17.0000 0 0 0 2.0000 0 0 0 -0.0000 k_half = 0.5831 -0.1460 -0.1460 -0.1460 0.1833 0.1833 -0.1460 0.1833 0.1833 eigenvalues= (2,0) (17,0) (0,0) eigenvectors= (-0.842777,0) (0.57735,0) (-0.041487,0) (0.380609,0) (0.57735,0) (-0.746766,0) (0.380609,0) (0.57735,0) (0.663792,0) khalf= (0.0754555,-3.9918e-310) (0.0764066,1.9959e-310) (0.0906734,1.9959e-310) (-0.144533,0) (0.186401,0) (0.200668,0) (-0.144533,0) (0.186401,0) (0.200668,0)
the problem don't know if difference going difference rest of algorithm or not (which post @ end of question completeness). understand there no way guarantee eigenvectors same 2 libraries (since there exists multiple eigenvectors , costant-invariant). have worry this? eventually, how can solve it?
the rest of matlab algorithm:
% p , b int parameters , w , h returned %create indices t random points each hash bit %then form weight matrix = 1:b rp = randperm(p); i_s(i,:) = rp(1:t); e_s = zeros(p,1); e_s(i_s(i,:)) = 1; w(:,i) = sqrt((p-1)/t)*k_half*e_s; end h = (k*w)>0; w = real(w);
thanks both answer's comments figured out problem:
eigen::matrixxcf khalf = es.eigenvectors()*v.asdiagonal()*es.eigenvectors().transpose();
(using transpose()
, eigen::matrixxcf
made trick)
Comments
Post a Comment