/*
 * Paradeigma xrhshs tou LAPACK apo programmata se C
 *
 * gcc -o lapackfromC lapackfromC.c -llapack -lg2c
*/

#include <stdio.h>

#define MAXDIM  10

int main()
{
   double A[MAXDIM][MAXDIM], b[MAXDIM];
   int    ipiv[MAXDIM];

   int    n, lda, info, nrhs, ldb, i;
   char   trans;
/*
 * Bazoume sto didiastato pinaka A ton pinaka toy systhmatos kata sthles!!
*/ 
   A[0][0] =  1.0;
   A[0][1] = -1.0;
   A[0][2] = -2.0;

   A[1][0] =  0.0;
   A[1][1] =  0.0;
   A[1][2] = -1.0;

   A[2][0] =  3.0;
   A[2][1] =  1.0;
   A[2][2] =  0.0;
/*
 * Kaloume thn DGETRF gia thn analysh LU kai elegxoume thn timh tou info
*/
   n = 3;  lda = MAXDIM;  info = 0;

   dgetrf_(&n, &n, A, &lda, ipiv, &info);

   if (info < 0) {
      printf("To %d orisma eixe mh epitrepth timh\n", info);
      exit(2);
   }
   else if (info > 0) {
      printf("To %d stoixeio ths diagwniou tou U einai mhden\n", info);
      exit(2);
   }
/*
 * To dexi melos
*/
   b[0] = 2.0;
   b[1] = 0.0;
   b[2] = 6.0;
/*
 * Lynoume to systhma twra me thn DGETRS
*/
   trans = 'N';  nrhs = 1;  ldb = MAXDIM;

   dgetrs_(&trans, &n, &nrhs, A, &lda, ipiv, b, &ldb, &info);

   if (info < 0) {
      printf("To %d orisma eixe mh epitrepth timh\n", info);
      exit(2);
   }

   printf("\nLysh tou systhmatos:\n");
   for (i = 0; i < n; i++)
      printf("%16.9e\n", b[i]);

   return 0;
}
