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

MappedSparseMatrix

Tags: None
(comma "," separated)
rupnik
Registered Member
Posts
2
Karma
0

MappedSparseMatrix

Wed May 06, 2015 10:20 am
I'm trying to use the MappedSparseMatrix by providing my own raw data pointers.

The code below doesn't work as I would expect:
Code: Select all
#include <iostream>
#include "../eigen/Eigen/Sparse"

int main() {   
   int outerPtr[] = { 0 };
   int innerPtr[] = { 1, 2 };
   double valsPtr[] = { 2.5, 3.6 };

   Eigen::MappedSparseMatrix<double, Eigen::ColMajor, int> mat(5, 1, 2, outerPtr, innerPtr, valsPtr);
   
   std::cout << mat.norm() << "\n"; // displays 0
   
   return 0;
}


Inserting into a matrix the normal way works:
Code: Select all
#include <iostream>
#include "../eigen/Eigen/Sparse"

int main() {
   int outerPtr[] = { 0 };
   int innerPtr[] = { 1, 2 };
   double valsPtr[] = { 2.5, 3.6 };
   
   Eigen::SparseMatrix<double, Eigen::ColMajor, int> mat(5, 1);
   mat.insert(innerPtr[0], 0) = valsPtr[0];
   mat.insert(innerPtr[1], 0) = valsPtr[1];

   std::cout << mat.norm() << "\n"; // displays 4.38292
   
   return 0;
}


What am I missing here?

Thanks,
Jan
rupnik
Registered Member
Posts
2
Karma
0

Re: MappedSparseMatrix  Topic is solved

Wed May 06, 2015 1:06 pm
OK, I figured it out. I forgot to add one element at the end of outerPtr.

Code: Select all
#include <iostream>
#include "../eigen2/Eigen/Sparse"

int main() {   
   int outerPtr[] = { 0, 2 };  // the final entry must be equal to the number of nonzeros!
   int innerPtr[] = { 1, 2 };
   double valsPtr[] = { 2.5, 3.6 };

   Eigen::MappedSparseMatrix<double, Eigen::ColMajor, int> mat(5, 1, 2, outerPtr, innerPtr, valsPtr);
   
   std::cout << mat.norm() << "\n"; // displays 4.38292
   
   return 0;
}


Bookmarks



Who is online

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