Different eigenvector and eigenvalues in Eigen and Matlab could generate errors?


Like it's explained <a href="https://stackoverflow.com/questions/28860070/why-some-eigen-vector-signs-from-c-armadillo-are-different-from-python-and-r" rel="nofollow">here</a> and <a href="https://stackoverflow.com/questions/22634638/c-linear-algebra-library-armadillo-how-to-use-eig-pair-to-get-the-same-resul" rel="nofollow">here</a> the orde of the eigenvalues (and relative eigenvectors and their sign too) are library dependent and (according to the first linked question) it shouldn't be a problem. In addition, eigenvectors relative to almost-zero eigenvalues can be considered as garbage. So far so good.

Now, consider the MATLAB code below that I want to rewrite in C++ using Eigen library:

%supposing K is 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 my C++ implementation:

EigenSolver<Matrix3f> es (K,true); auto v = es.eigenvalues(); //set to zero if eigenvalues too 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 is that K_half values are different from KHafl, as you can see from the printed result:


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 is that I don't know if this difference is going to be a difference for the rest of algorithm or not (which I post at the end of the question for completeness). From what I understand there is no way to guarantee that the eigenvectors are the same from the two libraries (since there exists multiple eigenvectors and they are costant-invariant). Do I have to worry about this? Eventually, how can I solve it?

<strong>The rest of the Matlab algorithm:</strong>

% p and b int parameters , W and H returned %create indices for the t random points for each hash bit %then form weight matrix for i = 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 to both answer's comments I figured out the problem:

Eigen::MatrixXcf KHalf = es.eigenvectors()*v.asDiagonal()*es.eigenvectors().transpose();

(using transpose() and Eigen::MatrixXcf made the trick)


  • How can I encode a filename according to RFC 2231?
  • Convert from hex-encoded CLOB to BLOB in Oracle
  • What's the alternative to IValueFormatter in AutoMapper?
  • Is it possible to extract table infomation using Apache Tika?
  • Touch events (touchesMoved) not working on UIViews inside UIScrollView
  • Implement Gauss-Jordan elimination in Haskell
  • Is it possible to specialize on a static lifetime?
  • How to Compose OSGi Based project with C++ based project?
  • How do I include screenshots of the full page in my serenity report (and not only of the viewport) u
  • iOS - Is this a task for enums?
  • Dependable views in Ember
  • Implementation of RTTI using typeid
  • Cocoa OSX: How can I make an image draggable
  • Why can't I use non-integral types with switch [duplicate]
  • Find longest path less than or equal to given value of an acyclic, directed graph in Python
  • What is the undocumented SessionIdInterface in PHP 5.5?
  • Where these are stored?
  • What's the syntax to inherit documentation from another indexer?
  • abstracting over a collection
  • Hibernate to update table schema
  • Can't remove headers after they are sent
  • C++ pointer value changes with static_cast
  • WPF ICommand CanExecute(): RaiseCanExecuteChanged() or automatic handling via DispatchTimer?
  • How solve “Qt: Untested Windows version 10.0 detected!”
  • Jackson Parser: ignore deserializing for type mismatch
  • Time complexity of a program which involves multiple variables
  • Change multiple background-images with jQuery
  • Is my CUDA kernel really runs on device or is being mistekenly executed by host in emulation?
  • Trying to switch camera back to front but getting exception
  • How to get next/previous record number?
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Benchmarking RAM performance - UWP and C#
  • How to set the response of a form post action to a iframe source?
  • How do you join a server to an Active Directory (domain)?
  • Angular 2 constructor injection vs direct access
  • Change div Background jquery
  • How does Linux kernel interrupt the application?
  • Qt: Run a script BEFORE make
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • reshape alternating columns in less time and using less memory