This forum has been archived. All content is frozen. Please use KDE Discuss instead.

(Solved) Problems with setFromTriplets()

Tags: None
(comma "," separated)
HBK90
Registered Member
Posts
1
Karma
0
Hi all,
I have a file in this 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 the first two integers are N=3 (Dimension of the system) and nz=5 (number of non-zero elements)
Then I have nz- lines i,j, A(i,j) and N-lines for vector b.
The largest file has N>100000, so it is necessary use sparse matrix.
It would be perfect use the triplets (http://eigen.tuxfamily.org/dox/Tutorial ... rseFilling) and i try do this, but it return always error.

I post my code:
Code: Select all
#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 to open file";
 return 0;
}


Someone can help me?

Thanks, Simon

---edit: i undestand my error. The code above is correct.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Sogou [Bot], Yahoo [Bot]