1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
#include <cmath>
#include <iostream>
#include <flens/flens.cxx>
#include <flens/examples/cg.h>

using namespace flens;
using namespace std;

int
main()
{
    const int n = 5;

///
/// Let the diagonal of A have values of `2` and the off-diagonal `-1`.
///
    SyCoordMatrix<CoordStorage<double> >    A(n, Upper);
    for (int i=1; i<=n; ++i) {
        A(i,i) += 2;
        if (i<n) {
            A(i,i+1) -= 1;
        }
    }

///
/// Compress A and stor it in B
///
    SyCRSMatrix<CRS<double> >   B = A;

///
/// Vector `z` is the control solution.
///
    DenseVector<Array<double> > x(n), z(n), b(n);
    z = 1;
    b = B*z;

///
/// Solve $Bx=b$
///
    cg(B, b, x);
    cout << "x = " << x << endl;

///
/// Get the infinity norm of the error
///
    x -= z;
    cout << "max abs error = " << abs(x(blas::iamax(x))) << std::endl;
}