## cholesky algorithm for c++

Post here if you have a question about LAPACK or ScaLAPACK algorithm or data format

### cholesky algorithm for c++

Hi
Can anybody post me an algorithm (with the makefile) for a cholesky factorization on c++? Something simple with nothing else.
I have been looking around for days for some examples and tried to write some myself after checking many guides but I still don't understand well the structure of LAPACK or maybe how to compile my algorithm correctly on c++.

Posts: 2
Joined: Wed Nov 07, 2012 8:04 am

### Re: cholesky algorithm for c++

I found it after a lot of search and trying many things. This code works. Maybe needs of course tunning, for example, when printing the matrix the values on the upper right part of the matrix should be 0. But at least runns and gives correct data.
I post for helping other not losing the time I lost searching through many mesy algorithms which where using different libraries and subroutines not necessary for initial tests. Before doing anything CLapack and CBlas must be correctly installed.

Here the code:
#include <iostream>
#include <string>
#include <vector>
#include <f2c.h>

using namespace std;

extern "C" void dpotrf_(char *uplo,int *jb,double *A,int *lda,int *info);

int main()
{
int INFO,size;
char UPLO;
//double *cov;

double cov;

size = 3;

//cov = (double *)calloc(size * size, sizeof(double));

cov = 1;
cov = 3;
cov = 4;
cov = 3;
cov = 13;
cov = 16;
cov = 4;
cov = 16;
cov = 21;

UPLO = 'L';

cout << "original matrix" <<endl;
for(int i=0;i<9;i++){
cout << cov[i] << endl;
}
cout << "-------------------------"<< endl;

for(int i=0;i<9;i++){
dpotrf_(&UPLO,&size,cov,&size,&INFO);
cout << cov[i] << endl;
}

}

And here the makefile. I configured it for running on a 64 bit Linux Ubuntu with a g++ compiler:
DEFINES =
INCLUDES = -I/usr/local/include
OPT = -O
CFLAGS = \$(OPT) \$(INCLUDES) \$(DEFINES)
CLIBS = -lm

# Systems with NAG library:
FLIBS = -lnag

# GNU/Linux Intel x86 systems
CLIBS = -lgcc -lstdc++
FLIBS = -llapack -lblas

ccode: ccode.o
\$(CC) \$(CFLAGS) -o ccode cholesky5.o \$(FLIBS) \$(CLIBS) \$(LDFLAGS)

ccode.o: cholesky5.cpp
\$(CC) \$(CFLAGS) -c cholesky5.cpp

fcode: fcode.o
\$(FC) -o fcode fcode.o \$(FLIBS) \$(LDFLAGS) 