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

set_from_triplets Sparse Matrix

Tags: None
(comma "," separated)
mohammadp100
Registered Member
Posts
5
Karma
0

set_from_triplets Sparse Matrix

Thu Apr 03, 2014 10:14 am
I have tried to construct a sparse matrix using set_from_triplets method but when I tried with large data I get this error :

a.out: /usr/local/include/eigen3/Eigen/src/SparseCore/SparseMatrix.h:952: void Eigen::internal::set_from_triplets(const InputIterator&, const InputIterator&, SparseMatrixType&, int) [with InputIterator = __gnu_cxx::__normal_iterator<Eigen::Triplet<long int>*, std::vector<Eigen::Triplet<long int> > >; SparseMatrixType = Eigen::SparseMatrix<long int, 1>]: Assertion `it->row()>=0 && it->row()<mat.rows() && it->col()>=0 && it->col()<mat.cols()' failed.
Aborted


actually I read a data form file and push them to triplet
Could you help me ?

thanks,
Mohammad
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: set_from_triplets Sparse Matrix

Thu Apr 03, 2014 11:45 am
you have to resize the sparse matrix (rows/cols) before inserting the elements
mohammadp100
Registered Member
Posts
5
Karma
0

Re: set_from_triplets Sparse Matrix

Thu Apr 03, 2014 5:39 pm
ggael wrote:you have to resize the sparse matrix (rows/cols) before inserting the elements



I still get this error
It is my code :
Code: Select all
#include <Eigen/Sparse>
#include <Eigen/SparseCore>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <iostream>
#include <cstring>
#include <vector>

typedef Eigen::Triplet<long int> T;

using namespace Eigen;

using namespace std;
int main() {
    int ni = 0,no = 0,Nx = 6955272, Ny = 17354796 ;
    SparseMatrix<long int,RowMajor> S;
         long int value,*B,txid,userid;
         std::vector<T> coefficients;
         coefficients.reserve(60000000);
    std::ifstream infile2("txout__modified.txt");
    std::string line2;
    while (std::getline(infile2,line2))
    {
    std::istringstream iss(line2);
    iss >> txid >> userid >> value;
             coefficients.push_back(T(txid,userid,value));   
        std::cout << "The Line is :"<<no<<"\n";
        no++;}
        Eigen::VectorXd b(Nx);
        S.resize(Nx,Ny);
        S.setFromTriplets(coefficients.begin(), coefficients.end());
       return 0;
}
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: set_from_triplets Sparse Matrix

Thu Apr 03, 2014 6:33 pm
maybe your file format is not 0-based indexed...
mohammadp100
Registered Member
Posts
5
Karma
0

Re: set_from_triplets Sparse Matrix

Thu Apr 03, 2014 7:30 pm
ggael wrote:maybe your file format is not 0-based indexed...



what is 0-based indexed file ?
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: set_from_triplets Sparse Matrix

Thu Apr 03, 2014 8:59 pm
many file formats assume that the index of the first row/column is 1 while in C++/Eigen it's 0.
mohammadp100
Registered Member
Posts
5
Karma
0

Re: set_from_triplets Sparse Matrix

Thu Apr 03, 2014 9:33 pm
But when i run this code for small lines it worked
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
This assert is telling you that at least one of the coefficient in the triplet list is outside the range you specified. You can check that in your while loop:

txid < Nx && userid < Ny
mohammadp100
Registered Member
Posts
5
Karma
0

Re: set_from_triplets Sparse Matrix

Thu Apr 03, 2014 10:51 pm
I have got it.
Thank you.


Bookmarks



Who is online

Registered users: Baidu [Spider], Bing [Bot], Google [Bot]