Content |
Setup a GeCCSMatrix
In this example we show how you:
-
How to setup a general sparse matrix in coordinate storage. This is a common step in FEM when the stiffness matrix gets assembled.
-
Show how to convert the sparse matrix from the coordinate storage format to the compressed column storage format.
Example Code
#include <flens/flens.cxx>
using namespace flens;
using namespace std;
int
main()
{
typedef int IndexType;
typedef IndexBaseZero<IndexType> IndexBase;
typedef CoordStorage<double, CoordColRowCmp, 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; // <-
GeCCSMatrix<CCS<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:
-
Element are of type double
-
Indices
-
are of type int and
-
start at zero (like in C arrays).
-
-
When elements get accumulated they get sorted column-wise.
typedef IndexBaseZero<IndexType> IndexBase;
typedef CoordStorage<double, CoordColRowCmp, IndexBase> Coord;
We define a general sparse \(m \times n\) matrix \(A\).
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,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 column storage. Values in the storage format of \(A\) get accumulated
cout << "A = " << A << endl;
cout << "B = " << B << endl;
You can continue to add values to entries in \(A\).
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 column storage.
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.
cout << "C = " << C << endl;
}
Compile
$shell> cd flens/examples $shell> g++ -Wall -std=c++11 -I../.. -o geccs-setup-example01 geccs-setup-example01.cc
Run
$shell> cd flens/examples $shell> ./geccs-setup-example01 A = isSorted_ = 1 #0: (0, 0) = 2 #1: (1, 0) = 3 #2: (0, 1) = 3 #3: (2, 1) = -1 #4: (4, 1) = 4 #5: (1, 2) = 4 #6: (2, 2) = -3 #7: (3, 2) = 1 #8: (4, 2) = 2 #9: (2, 3) = 2 #10: (1, 4) = 6 #11: (4, 4) = 1 B = compressed cols: cols: 0 2 5 9 10 12 rows: 0 1 0 2 4 1 2 3 4 2 1 4 values: 2 3 3 -1 4 4 -3 1 2 2 6 1 A = isSorted_ = 1 #0: (0, 0) = 2 #1: (1, 0) = 6 #2: (0, 1) = 3 #3: (2, 1) = -2 #4: (4, 1) = 8 #5: (0, 2) = 2 #6: (1, 2) = 8 #7: (2, 2) = -6 #8: (3, 2) = 2 #9: (4, 2) = 4 #10: (2, 3) = 4 #11: (0, 4) = 3 #12: (1, 4) = 12 #13: (4, 4) = 2 B = compressed cols: cols: 0 2 5 10 11 14 rows: 0 1 0 2 4 0 1 2 3 4 2 0 1 4 values: 2 6 3 -2 8 2 8 -6 2 4 4 3 12 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