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

using namespace flens;
using namespace std;

int
main()
{
    GeMatrix<FullStorage<double> >   A(33);

///
/// Setup the raw data.
///
    A = 2, 0, 0,
        1, 2, 0,
        0, 1, 2;

///
/// $S$ is a symmetric matrix view constructed from the lower triangular
/// part of $A$.  Note that $S$ only references data from $A$.
///
    auto S = A.lower().symmetric();
    cout << "S = " << S << endl;

///
/// Computes the Cholesky factorization $S = L L^T$ where $L$ is lower
/// triangular.  Matrix $S$ (i.e. the lower triangular part of $A$) gets
/// overwritten with $L$.
///
    int info = lapack::potrf(S);

///
/// If `info` is not zero the factorization could not be computed as the matrix
/// was singular.
///
    if (info!=0) {
        cerr << "S is singular" << endl;
        return info;
    }

///
/// Compute $S^{-1}$ from the Cholesky factorization $S = L L^T$.
///
    lapack::potri(S);
    cout << "inv(S) = " << S << endl;

    return 0;
}