Page 1 of 1

cholesky algorithm for c++

PostPosted: Wed Nov 07, 2012 8:36 am
by radomir
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++.
Thanks in advance

Re: cholesky algorithm for c++

PostPosted: Fri Nov 09, 2012 8:08 am
by radomir
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[9];

size = 3;

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

cov[0] = 1;
cov[1] = 3;
cov[2] = 4;
cov[3] = 3;
cov[4] = 13;
cov[5] = 16;
cov[6] = 4;
cov[7] = 16;
cov[8] = 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++){
cout << cov[i] << endl;


And here the makefile. I configured it for running on a 64 bit Linux Ubuntu with a g++ compiler:
INCLUDES = -I/usr/local/include
OPT = -O
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)