Content

Setup a GeCRSMatrix

In this example we show how you:

Example Code

#include <iostream>
#include <flens/flens.cxx>

using namespace flens;
using namespace std;

int
main()
{
    typedef int                                              IndexType;
    typedef IndexBaseZero<IndexType>                         IndexBase;
    typedef CoordStorage<double, CoordRowColCmp, IndexBase>  Coord;

    const IndexType m = 5;
    const IndexType n = 5;
    GeCoordMatrix<Coord>  A(m, n);

    A(0,0) += 2;
    A(0,1) += 3;
    A(1,0) += 3;
    A(1,2) += 4;
    A(1,4) += 3;       // <-
    A(1,4) += 3;       // <-
    A(2,1) -= 1;
    A(2,2) -= 3;
    A(2,3) += 2;
    A(3,2) += 1;
    A(4,1) += 2;       // <-
    A(4,2) += 2;
    A(4,4) += 1;
    A(4,1) += 2;       // <-

    GeCRSMatrix<CRS<double, IndexBase> >  B = A;
    cout << "A = " << A << endl;
    cout << "B = " << B << endl;

    A(0,2) += 2;
    A(0,4) += 3;
    A(1,0) += 3;
    A(1,2) += 4;
    A(1,4) += 6;
    A(2,1) -= 1;
    A(2,2) -= 3;
    A(2,3) += 2;
    A(3,2) += 1;
    A(4,1) += 4;
    A(4,2) += 2;
    A(4,4) += 1;

    B = A;
    cout << "A = " << A << endl;
    cout << "B = " << B << endl;

    GeMatrix<FullStorage<double> >  C = B;
    cout << "C = " << C << endl;
}

Some Notes

The following typedef Coord is a shortcut for a coordiante storage formate with the following properties:

    typedef int                                              IndexType;
    typedef IndexBaseZero<IndexType>                         IndexBase;
    typedef CoordStorage<double, CoordRowColCmp, IndexBase>  Coord;

We define a general sparse \(m \times n\) matrix \(A\).

    const IndexType m = 5;
    const IndexType n = 5;
    GeCoordMatrix<Coord>  A(m, n);

We add some values to some entries \(a_{i_k, j_k}\) of \(A\). Arrows indicate that certain entries occur more then once.

    A(0,0) += 2;
    A(0,1) += 3;
    A(1,0) += 3;
    A(1,2) += 4;
    A(1,4) += 3;       // <-
    A(1,4) += 3;       // <-
    A(2,1) -= 1;
    A(2,2) -= 3;
    A(2,3) += 2;
    A(3,2) += 1;
    A(4,1) += 2;       // <-
    A(4,2) += 2;
    A(4,4) += 1;
    A(4,1) += 2;       // <-

Next we convert the sparse matrix \(A\) with coordinate storage to a sparse matrix \(B\) with compressed row storage. Values in the storage format of \(A\) get accumulated

    GeCRSMatrix<CRS<double, IndexBase> >  B = A;
    cout << "A = " << A << endl;
    cout << "B = " << B << endl;

You can continue to add values to entries in \(A\).

    A(0,2) += 2;
    A(0,4) += 3;
    A(1,0) += 3;
    A(1,2) += 4;
    A(1,4) += 6;
    A(2,1) -= 1;
    A(2,2) -= 3;
    A(2,3) += 2;
    A(3,2) += 1;
    A(4,1) += 4;
    A(4,2) += 2;
    A(4,4) += 1;

Again we convert \(A\) to compressed row storage.

    B = A;
    cout << "A = " << A << endl;
    cout << "B = " << B << endl;

For debugging it's sometime convenient to densify the sparse matrix. I.e. we convert it to GeMatrix.

    GeMatrix<FullStorage<double> >  C = B;
    cout << "C = " << C << endl;
}

Compile

$shell> cd flens/examples                                                      
$shell> g++ -Wall -std=c++11 -I../.. -o gecrs-setup-example01 gecrs-setup-example01.cc                                           

Run

$shell> cd flens/examples                                                      
$shell> ./gecrs-setup-example01                                                
A = _isSorted = 1
  #0: (0, 0) = 2
  #1: (0, 1) = 3
  #2: (1, 0) = 3
  #3: (1, 2) = 4
  #4: (1, 4) = 6
  #5: (2, 1) = -1
  #6: (2, 2) = -3
  #7: (2, 3) = 2
  #8: (3, 2) = 1
  #9: (4, 1) = 4
  #10: (4, 2) = 2
  #11: (4, 4) = 1
B = 
compressed rows:
 rows:   
            0              2              5              8              9             12 
 cols:   
            0              1              0              2              4              1              2              3              2              1              2              4 
 values: 
            2              3              3              4              6             -1             -3              2              1              4              2              1 
A = _isSorted = 1
  #0: (0, 0) = 2
  #1: (0, 1) = 3
  #2: (0, 2) = 2
  #3: (0, 4) = 3
  #4: (1, 0) = 6
  #5: (1, 2) = 8
  #6: (1, 4) = 12
  #7: (2, 1) = -2
  #8: (2, 2) = -6
  #9: (2, 3) = 4
  #10: (3, 2) = 2
  #11: (4, 1) = 8
  #12: (4, 2) = 4
  #13: (4, 4) = 2
B = 
compressed rows:
 rows:   
            0              4              7             10             11             14 
 cols:   
            0              1              2              4              0              2              4              1              2              3              2              1              2              4 
 values: 
            2              3              2              3              6              8             12             -2             -6              4              2              8              4              2 
C = 
            2             3             2             0             3 
            6             0             8             0            12 
            0            -2            -6             4             0 
            0             0             2             0             0 
            0             8             4             0             2