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
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
#include <iostream>

///
/// Define `USE_CXXLAPACK` before you include the FLENS headers
///
#ifndef USE_CXXLAPACK
#define USE_CXXLAPACK
#endif
#include <flens/flens.cxx>

using namespace std;
using namespace flens;

typedef double   T;

int
main()
{
    typedef GeMatrix<FullStorage<T> >           Matrix;
    typedef DenseVector<Array<T> >              Vector;

    typedef Matrix::IndexType                   IndexType;
    typedef DenseVector<Array<IndexType> >      IndexVector;

    const IndexType n = 4;

    Matrix         A(n,n);
    Vector         b(n);
    IndexVector    piv(n);

    A =  2,   3,  -1,   0,
        -6,  -5,   0,   2,
         2,  -5,   6,  -6,
         4,   6,   2,  -3;

    b = 20,
       -33,
       -43,
        49;

    cerr << "A = " << A << endl;
    cerr << "b = " << b << endl;

///
/// The CXXLAPACK functions require the usual information describing the
/// matrices, i.e. dimensions, leading dimension and a data pointer.  All
/// these can be retrieved from the FLENS matrix type.
///
    IndexType info = cxxlapack::getrf(A.numRows(),
                                      A.numCols(),
                                      A.data(),
                                      A.leadingDimension(),
                                      piv.data());

    if (info==0) {
///
///     Note that __cxxlapack::getrs__ has a template parameter for the used
///     index type.  That's why we call it with `IndexType(1)`.  This makes
///     sure that values `A.numRows()`, `IndexType(1)`, `A.leadingDimension`
///     and `b.stride()` all have the same type.
///
///     :links: __cxxlapack::getrs__ -> file:cxxlapack/interface/getrs.h
///
        cxxlapack::getrs(lapack::getF77Char(NoTrans),
                         A.numRows(),
                         IndexType(1),
                         A.data(),
                         A.leadingDimension(),
                         piv.data(),
                         b.data(),
                         b.length());

///
///     As an alternative we could specify the template parameter explictly:
///
/*
        cxxlapack::getrs<IndexType>(lapack::getF77Char(NoTrans),
                                    A.numRows(),
                                    1,
                                    A.data(),
                                    A.leadingDimension(),
                                    piv.data(),
                                    b.data(),
                                    b.stride());
*/

        cerr << "x = " << b << endl;
    } else {
        cerr << "A is numerically singular." << endl;
    }
}