Reputation: 1265
I have recently installed armadillo and tried eigenvalue problem for sparse matrices. Unfortunately, decomposition fails is the parameter 'N' (code below) is too large e.q. 1000. I wonder what is going on here. Matrix is not very complicated - it has diagonal structure.
UPDATE
Mathematica also have problems with this matrix. It tells me that Arnoldi algorithm does not converge. Maybe I need to manually specify some parameters in arnoldi arpack routines to ensure convergence?
Here is my code:
#include <armadillo>
int main ()
{
double N = 1000.0;
// create matrix
int kmin = 0;
int kmax = static_cast<int>( std::floor( N/2.0 ) );
int dim = (kmax - kmin) + 1;
// locations and values in sparse matrices
arma::umat hc_locations (2, 3*dim-2);
arma::vec hc_values (3*dim-2);
// diagonal part
for (int k=0; k<dim; k++)
{
hc_locations (0,k) = k;
hc_locations (1,k) = k;
hc_values (k) = 2.0/N*static_cast<double>(kmin + k)*( 2.0*( N-2.0*static_cast<double>(k + kmin) ) - 1.0 );
}
// upper and lower diagonal
for (int k=0; k<dim-1; k++)
{
hc_locations (0,k+dim) = k;
hc_locations (1,k+dim) = k+1;
hc_values (k+dim) = 2.0/N*std::sqrt( ( static_cast<double>(k+1+kmin) ) *
( static_cast<double>(k+1+kmin) ) *
( N - static_cast<double>(2*(k+1+kmin)) + 1.0 ) *
( N - static_cast<double>(2*(k+1+kmin)) + 2.0 ) );
hc_locations (0, k+2*dim-1) = k+1;
hc_locations (1, k+2*dim-1) = k;
hc_values (k+2*dim-1) = 2.0/N*std::sqrt ( ( static_cast<double>(k+1+kmin) ) *
( static_cast<double>(k+1+kmin) ) *
( N - static_cast<double>(2*(k+kmin)) ) *
( N - static_cast<double>(2*(k+kmin)) - 1.0 ) );
}
arma::sp_mat Ham(hc_locations, hc_values);
// eigenvalue problem
arma::vec eigval;
arma::mat eigvec;
arma::eigs_sym( eigval, eigvec, Ham, 3, "sa");
}
Upvotes: 2
Views: 1289
Reputation: 11
For small matrices of size 2000 or so, it is often time easier to find all the eigenvalues and eigenvectors, as those methods are less susceptible to near singular matrices.
I replaced your code
arma::eigs_sym( eigval, eigvec, Ham, 3, "sa");
with
arma::mat fullMat = arma::mat(Ham);
arma::eig_sym( eigval, eigvec, fullMat);
and the compiled program takes less than a second to solve on my late 2015 Macbook Pro.
Upvotes: 1