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
#include <iostream>
#include <flens/flens.cxx>

using namespace std;
using namespace flens;

int
main()
{
    typedef complex<double>                  Complex;
    const Complex                            I(0,1);

    typedef GeMatrix<FullStorage<Complex> >  GeMatrix;
    typedef typename GeMatrix::IndexType     IndexType;
    typedef DenseVector<Array<IndexType> >   IndexVector;
    typedef DenseVector<Array<Complex> >     DenseVector;

    GeMatrix        A(4,4);
    IndexVector     jPiv;
    DenseVector     tau;
    DenseVector     b(4);
    //DenseVector   work;

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

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

///
/// Make that $A$ and $b$ somehow look complex ;-)
///
    A *= I;
    b *= 2.0*I;

    cout << "A = " << A << endl;
    cout << "b = " << b << endl;

///
/// Compute the factorization $AP = QR$.  As vector `jPiv` has initially length
/// zero it gets internally resized to length $n$ and initialized with Zero.
/// This means that *all columns are free columns*.  Also note that the
/// workspace gets created implicitly and temporarily.  So you might not
/// want to do this inside a loop.
///
    lapack::qp3(A, jPiv, tau);
    //lapack::qp3(A, jPiv, tau, work);

///
/// Compute $\tilde{b} = Q^H b$. Vector $b$ gets overwritten with $\tilde{b}$.
///
    lapack::unmqr(Left, ConjTrans, A, tau, b);
    //lapack::unmqr(Left, ConjTrans, A, tau, b, work);

///
/// Solve $R u = \tilde{b}$.  Vector $b$ gets overwritten with $u =P^T x$.
///
    blas::sv(NoTrans, A.upper(), b);

///
/// Compute $x = Pu$.  Note that we need an extra vector here!
///
    DenseVector     x(4);
    for (IndexType i=1; i<=x.length(); ++i) {
        x(jPiv(i)) = b(i);
    }

    cout << "x = " << x << endl;

///
/// Output some additional information:
///
    cout << "jPiv = " << jPiv << endl;
}