use blib; # when using inside the dist tree
use PDL;  # this must be called before (!) 'use Inline Pdlpp' calls

# for this example you need the numerical recipes library
# edit the INC and LIBS info below to point
# Inline towards the location of include and library files

use Inline Pdlpp => Config =>
  INC => "-I$ENV{HOME}/include",
  LIBS => "-L$ENV{HOME}/lib -llapack -lm",
  AUTO_INCLUDE => <<'EOINC',
#include <math.h>
#include "f2c.h"    /* type definitions for a files */
#include "lapack.h"  /* Definitions of all routines */

static void nr_barf(char *err_txt)
{
  fprintf(stderr,"Now calling croak...\n");
  croak("NR runtime error: %s",err_txt);
}
EOINC
  BOOT => 'set_nr_err_handler(nr_barf);'; # catch errors at the perl level


use Inline Pdlpp; # the actual code is in the __Pdlpp__ block below

$n=4;

$A = zeroes(double ,n,n);

  for $i (0.. $n-1 ) {
      for $j (0.. $n-1 ) {
         set $A,$i,$j , pow(i+1, j+1) + pow(j+1, i+1);
   }
  }

$b = zeroes(double ,n);
  for $i (0.. $n-1 ) {
         set $b,$i , pow(i+1, j+1) + pow(j+1, i+1);
  }


$ipiv = zeroes(int ,n);
$info = zeroes(int ,1);
print $A;
print $b;

dgsev($n,1,$A,$n,$ipiv,$b,$n,$info);

print $info;
print $b;

# Other approach
#	Pars => ' [o] a (lda,n); int [o] ipiv(n);  [o] b (ldb,nrhs);    int [o] info;',
#	Code => 'dgesv_($SIZE(n), $SIZE(nrhs), $P(a), $SIZE(lda), $P(ipiv), $P(b), $SIZE(ldb), $P(info) );',


__DATA__

__Pdlpp__

# Call dgsev
pp_def('dgsev',
	Pars => 'int n;int nrhs;double [o]a(lda,n);int lda;int [o]ipiv(n); double [o]b(ldb,nrhs);int [o]info;',
	GenericTypes => [D],
	Code => 'dgesv_($P(n), $P(nrhs), $P(a), $P(lda), $P(ipiv), $P(b), $P(ldb), $P(info) );',
);



