Return GSL_NAN instead of NAN when a callback fails
[Math-GSL.git] / swig / gsl_typemaps.i
blob388056e7c546cded8f2f5f95aa87941d10c0ddd7
1 %include "system.i"
2 %include "gsl/gsl_nan.h"
3 #ifdef GSL_MINOR_VERSION && GSL_MINOR_VERSION >= 12
4 %include "gsl_inline.h"
5 #endif
7 %{
8 #include "gsl/gsl_nan.h"
9 %}
11 %typemap(in) double const [] {
12 AV *tempav;
13 I32 len;
14 int i;
15 SV **tv;
16 if (!SvROK($input))
17 croak("Math::GSL : $$1_name is not a reference!");
18 if (SvTYPE(SvRV($input)) != SVt_PVAV)
19 croak("Math::GSL : $$1_name is not an array ref!");
21 tempav = (AV*)SvRV($input);
22 len = av_len(tempav);
23 $1 = (double *) malloc((len+1)*sizeof(double));
24 for (i = 0; i <= len; i++) {
25 tv = av_fetch(tempav, i, 0);
26 $1[i] = (double) SvNV(*tv);
30 %apply double const [] {
31 size_t *p ,double *data, double *dest, double *f_in, double *f_out,
32 double data[], const double * src, double x[], double a[], double b[] ,
33 const double * x, const double * y, const double * w , const double x_array[],
34 const double xrange[], const double yrange[], double * base,
35 const double * base, const double xrange[], const double yrange[] ,
36 const double * array , const double data2[], const double w[] ,
37 float const *A, float const *B, float const *C, float *C, double *v,
38 gsl_complex_packed_array data
41 %apply int *OUTPUT { size_t *imin, size_t *imax, size_t *neval };
42 %apply double * OUTPUT {
43 double * min_out, double * max_out,
44 double *abserr, double *result
47 static HV * Callbacks = (HV*)NULL;
48 /* this function returns the value
49 of evaluating the function pointer
50 stored in func with argument x
52 double callthis(double x , int func, void *params){
53 SV ** sv;
54 unsigned int count;
55 double y;
56 dSP;
58 //fprintf(stderr, "LOOKUP CALLBACK\n");
59 sv = hv_fetch(Callbacks, (char*)func, sizeof(func), FALSE );
60 if (sv == (SV**)NULL) {
61 fprintf(stderr, "Math::GSL(callthis): %d not in Callbacks!\n", func);
62 return GSL_NAN;
65 PUSHMARK(SP);
66 XPUSHs(sv_2mortal(newSVnv((double)x)));
67 PUTBACK; /* make local stack pointer global */
69 count = call_sv(*sv, G_SCALAR);
70 SPAGAIN;
72 if (count != 1)
73 croak("Expected to call subroutine in scalar context!");
75 PUTBACK; /* make local stack pointer global */
77 y = POPn;
78 return y;
80 double callmonte(double x[], size_t dim, void *params ){
81 fprintf(stderr, "callmonte!!!");
84 %typemap(in) gsl_monte_function * {
85 gsl_monte_function MF;
86 int count;
87 SV ** callback;
88 double x;
89 if (!SvROK($input)) {
90 croak("Math::GSL : $$1_name is not a reference value!");
92 if (Callbacks == (HV*)NULL)
93 Callbacks = newHV();
94 fprintf(stderr,"STORE $$1_name gsl_monte_function CALLBACK: %d\n", (int)$input);
95 hv_store( Callbacks, (char*)&$input, sizeof($input), newSVsv($input), 0 );
97 MF.params = &$input;
98 MF.dim = 1; // XXX
99 MF.f = &callmonte;
100 $1 = &MF;
103 %typemap(in) gsl_function * {
104 gsl_function F;
105 int count;
106 SV ** callback;
107 double x;
109 if (!SvROK($input)) {
110 croak("Math::GSL : $$1_name is not a reference value!");
112 if (Callbacks == (HV*)NULL)
113 Callbacks = newHV();
114 //fprintf(stderr,"STORE CALLBACK: %d\n", (int)$input);
115 hv_store( Callbacks, (char*)&$input, sizeof($input), newSVsv($input), 0 );
117 F.params = &$input;
118 F.function = &callthis;
119 $1 = &F;
122 %typemap(in) gsl_function_fdf * {
123 fprintf(stderr, 'FDF_FUNC');
127 typedef int size_t;