matrix - Eigen: Problems using setFromTriplets() -
i have file in format: 3 5 1 1 1.0 1 3 2.0 2 1 3.0 2 2 4.0 3 3 5.0 10.0 20.0 30.0
where first 2 integers n=3 (dimension of system) , nz=5 (number of non-zero elements) have nz- lines i,j, a(i,j) , n-lines vector b. largest file has n>100000, necessary use sparse matrix. perfect use triplets (http://eigen.tuxfamily.org/dox/tutorial ... rsefilling) , try this, return error.
i post code:
#include <eigen/sparse> #include <fstream> #include <iostream> typedef eigen::sparsematrix<double> spmat; typedef eigen::triplet<double> t; using namespace std; int main() { double val; int n, nz, i, j; vector<t> tripletlist; ifstream myfile ("file.dat"); if (myfile.is_open()) { myfile >> n; myfile >> nz; tripletlist.reserve(nz); int index = 0; while (index < nz) { myfile >> i; myfile >> j; myfile >> val; //cout << i-1 << ' '<< j-1 << ' ' << val << endl; tripletlist.push_back(t(i-1,j-1,val)); index++; } spmat mat(n,n); eigen::vectorxd b(n); mat.setfromtriplets(tripletlist.begin(), tripletlist.end()); index = 0; while (index < n) { myfile>>b[index]; if (myfile.eof()){ break;} index++; } myfile.close(); }else cout << "unable open file"; return 0; }
someone can me?
thanks, simon
--- solved: code above correct.
to understanding, subscript indices of entries should start 0. try modify data file follow:
3 5 0 0 1.0 ...
Comments
Post a Comment