I am trying to fill up a Sparse RowMajor matrix. Following the guide I was using the triplets method:
Eigen::SparseMatrix<double, Eigen::RowMajor> data_matrix(rows, cols);
....
void get_data(const char *dir_name, std::vector<T> tripletList, Eigen::SparseMatrix<double, Eigen::RowMajor> data_matrix) {
uint64_t row_iter = 0;
for (std::string file_n : sorted_files) {
...
if (words.find(word_freq[0]) != words.end())
tripletList.push_back(T(row_iter, words[word_freq[0]], std::stoi(word_freq[1])));
}
row_iter++;
}
data_matrix.setFromTriplets(tripletList.begin(), tripletList.end());
However, this approach generates an empty matrix. I couldn't find examples of filling RowMajor matrices with the triplet list method, is it not possible?
Works for me, here is a selfcontained example:
#include <iostream>
#include <Eigen/SparseCore>
#include <vector>
using namespace Eigen;
using namespace std;
int main()
{
int m = 3, n = 7;
SparseMatrix<double, RowMajor> M(m,n);
typedef Triplet<double,int> T;
vector<T> entries;
for(int k=1; k<=9;++k)
entries.push_back( T(internal::random<int>(0,m-1), internal::random<int>(0,n-1), k) );
M.setFromTriplets(entries.begin(), entries.end());
cout << MatrixXd(M) << "\n";
}
that produces:
1 0 0 8 4 0 0
0 3 0 6 0 0 0
16 0 0 2 0 0 5
EDIT:
So the problem is in the structure of your code, I see that get_data
gets the triplet list and the sparse matrix by value, whereas they are modified by this function, so you most likely want to pass them by reference.