Sort eigenvalues and eigenvectors

This commit is contained in:
Cory Balaton 2023-09-20 20:00:07 +02:00
parent 162e884d47
commit 5eb23c1985
No known key found for this signature in database
GPG Key ID: 3E5FCEBFD80F432B

View File

@ -6,7 +6,9 @@
* @bug The eigenvalues fail the test. When comparing them to arma::eigsym
* there is one value that is way off when testing with a 6x6 matrix.
*/
#include <algorithm>
#include <cmath>
#include <utility>
#include "utils.hpp"
#include "jacobi.hpp"
@ -75,9 +77,16 @@ void jacobi_eigensolver(const arma::mat& A,
} while (max_offdiag >= eps && ++iterations < maxiter);
arma::vec res = arma::diagvec(A_m);
arma::uvec sorted_indeces = arma::sort_index(res);
eigenvalues.resize(res.n_elem);
eigenvectors.resize(res.n_elem, res.n_elem);
// The diagonal elements of A_m are the eigenvalues
eigenvalues = arma::diagvec(A_m);
eigenvectors = R;
for (int i=0; i < res.n_elem; i++) {
eigenvalues[i] = res[sorted_indeces[i]];
eigenvectors.insert_cols(i, R.col(sorted_indeces[i]));
}
converged = max_offdiag < eps;
A_m.print();