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

How to construct Sparse Matrix with Random values

Tags: None
(comma "," separated)
wendingmai
Registered Member
Posts
16
Karma
0
Hi,

For dense matrix, one could use code like:
Code: Select all
MatrixXd m = MatrixXd::Random(1000,1000);


Although Eigen could define a large sparse matrix like:
Code: Select all
Eigen::SparseMatrix<double> M(1000, 1000);


How could I fill it with Random values?

Thank you.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
There is no convenient methods because the structure of the sparse matrix is a very important aspect, and it usually does not make much sense to initialize a sparse matrix with a random structure.
wendingmai
Registered Member
Posts
16
Karma
0
ggael wrote:There is no convenient methods because the structure of the sparse matrix is a very important aspect, and it usually does not make much sense to initialize a sparse matrix with a random structure.


By using Armadilo+Arpack, one could construct large Sparse Matrix with Random values like:
sp_cx_mat A (sprandu<sp_mat>(1000, 1000, 0.1), sprandu<sp_mat>(1000, 1000, 0.1));

It is convenient for me to testing the accuracy and efficiency of an algorithm.
Anyway, thank you all the same.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
we have a similar helper in test/sparse.h that you can adapt to remove the construction of the dense reference matrix.
wendingmai
Registered Member
Posts
16
Karma
0
ggael wrote:we have a similar helper in test/sparse.h that you can adapt to remove the construction of the dense reference matrix.


Hi, Gael

Sorry for bothering you again.
I wrote a fragment of simple code to try, but it gives out fatal error C1083: Cannot open include file: 'split_test_helper.h': No such file or directory
Yet, I cannot find such file in the whole Eigen folder... :'(

Code: Select all
#include <Eigen/Core>
#include <iostream>
#include <test/sparse.h>
using namespace Eigen;
using namespace std;
int main()
{
   MatrixXd A = MatrixXd::Random(1000, 1000);
   SparseMatrix<double> M(1000, 1000);
   initSparse(0.1, A, M);
   cout << M << endl;
   system("pause");
   return 0;
}
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
indeed, you cannot just directly include this file. Instead, you should rather copy/paste the related function and adjust it to your need.

For the record, split_test_helper.h is procedurally generated by cmake when configuring the unit test.


Bookmarks



Who is online

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