I need help with compressed row storage

(Luigi Cimorelli) #1

I need someone to help me with sparsecompressedrowstorage class.

I’m quite new to math.net. I used to write linear system solver on my own. Recently, I’ve decided to use available libraries.

Can someone write an example on how to use the aforementioned class?

Specifically, I would like to write my matrix directly in CRS arrays, without using the original one, but I don’t even know how to use the class.

0 Likes

(Luigi Cimorelli) #2

Is someone interested in this topic? I found this very important because if one is solving a PDE by finite difference or finite element methods a sparse linear system solver is required. Furthermore, it is crucial to work directly with the csr array without allocating the original sparse matrix. Is there a way to achieve that with mathnet?

Please someone help me

0 Likes

(wo80) #3

Just create the sparse matrix and then access the storage:

var A = new SparseMatrix(rows, columns);

var storage = (SparseCompressedRowMatrixStorage<double>)A.Storage;

var ap = storage.RowPointers; // readonly int[] array of size (rows + 1)
var ai = storage.ColumnIndices; // int[] array of size >= non-zeros count
var ax = storage.Values; // double[] array of size >= non-zeros count

Since the storage is not really efficient to modify, you might want to use some sort of coordinate storage when assembling the matrix and then convert to CSR. Take a look at wo80/mathnet-extensions.

If you are looking for direct solvers of sparse linear systems, take a look at wo80/CSparse.NET

0 Likes

(Luigi Cimorelli) #4

I’m sorry I have to Re-Edit again:

Dear Wo80,

It doesn’t work, It just allows you to assign the values to ax,ai, and ap, but the properties are not assigned to A and store. As a result when I try to solve the system it return Not a number in the results vector.

I’ve already used CSparse, but usually direct methods (such as mumps, pardiso etc.) works better for large scale systems (e.g. 10^6x10^6 matrices or bigger). When I have to handle not too big matrices (i.e. 4000x4000) iterative methods (such biconjugate or similar) works quite well (particularly with a good preconditioner).

I have to try the extension you suggested.

0 Likes

(wo80) #5

I don’t know why this design decision was made (making the RowPointers array readonly), but it shouldn’t be a big problem. Just work with that array (modifying it at your will), or - if you really want to allocate your own array - copy the values to the original array when you’re done (using Array.Copy(...)).

Regarding your edit:

I guess that at some point you are creating new arrays for the column indices and values. If you do that, you will have to assign those objects back to the storage:

// Create your own CSR storage arrays.

var ap = new int[n + 1]; // Row pointers.
var ai = new int[nnz]; // Column indices.
var ax = new double[nnz]; // Non-zeros values.

AssembleMatrix(ap, ai, ax);

Array.Copy(ap, storage.RowPointers, n + 1);

storage.ColumnIndices = ai;
storage.Values = ax;
0 Likes

(Luigi Cimorelli) #6

I was not thinking to use array.copy. Thanks, now it works.

0 Likes