How can I convert from an Armadillo Matrix to an Eigen MatrixXd and vice versa?
I have nu
as an arma::vec
of size N
, z>
I just spend a couple of hours trying to convert Eigen sparse matrix to Armadillo sparse matrix and I'll post the code here if someone else find a need to do the same.
I was doing this because I could not find an eigensolver for the sparse complex matrices, and Armadillo was the only one that had it, but the rest of my code was already done in Eigen so I had to do the conversion.
#include
#include
using namespace std;
using namespace arma;
int main() {
auto matrixA = new SparseMatrix>(numCols, numRows); //your Eigen matrix
/*
SOME CODE TO FILL THE Eeigen MATRIX
*/
// now create a separate vectors for row indeces, first non-zero column element indeces and non-zero values
// why long long unsigned int, because armadilo will expect that type when constructing sparse matrix
vector rowind_vect((*matrixA).innerIndexPtr(),
(*matrixA).innerIndexPtr() + (*matrixA).nonZeros());
vector colptr_vect((*matrixA).outerIndexPtr(),
(*matrixA).outerIndexPtr() + (*matrixA).outerSize() + 1);
vector> values_vect((*matrixA).valuePtr(),
(*matrixA).valuePtr() + (*matrixA).nonZeros());
// you can delete the original matrixA to free up space
delete matrixA;
//new Armadillo vectors from std::vector, we set the flag copy_aux_mem=false, so we don't copy the values again
cx_dvec values(values_vect.data(), values_vect.size(), false);
uvec rowind(rowind_vect.data(), rowind_vect.size(), false);
uvec colptr(colptr_vect.data(), colptr_vect.size(), false);
// now create Armadillo matrix from these vectors
sp_cx_dmat arma_hamiltonian(rowind, colptr, values, numCols, numRows);
// you can delete the vectors here if you like to free up the space
return 0;
}