2 %include
"gsl/gsl_nan.h"
3 #if defined GSL_MINOR_VERSION
&& (GSL_MINOR_VERSION >= 12)
4 %include
"gsl/gsl_inline.h"
8 #include
"gsl/gsl_nan.h"
9 #include
"gsl/gsl_math.h"
10 #include
"gsl/gsl_monte.h"
13 /*****************************
14 * handle 'double const
[]' as an input array of doubles
15 * We allocate the C array at the begining and free it at the end
17 %typemap
(in
) double const
[] {
23 croak
("Math::GSL : $$1_name is not a reference!");
24 if
(SvTYPE
(SvRV
($input
)) != SVt_PVAV
)
25 croak
("Math::GSL : $$1_name is not an array ref!");
27 tempav
= (AV
*)SvRV
($input
);
29 $
1 = (double
*) malloc
((len
+1)*sizeof
(double
));
30 for
(i
= 0; i
<= len
; i
++) {
31 tv
= av_fetch
(tempav
, i
, 0);
32 $
1[i
] = (double
) SvNV
(*tv
);
36 %typemap
(freearg
) double const
[] {
40 %apply double const
[] {
41 double
*data
, double
*dest
, double
*f_in
, double
*f_out
,
42 double data
[], const double
* src
, double x
[], double a
[], double b
[],
43 const double
* x
, const double
* y
, const double
* w
, const double x_array
[],
44 const double xrange
[], const double yrange
[], double
* base
,
45 const double
* base
, const double xrange
[], const double yrange
[] ,
46 const double
* array
, const double data2
[], const double w
[] ,
48 gsl_complex_packed_array data
51 /*****************************
52 * handle 'float const
[]' as an input array of floats
53 * We allocate the C array at the begining and free it at the end
55 %typemap
(in
) float const
[] {
61 croak
("Math::GSL : $$1_name is not a reference!");
62 if
(SvTYPE
(SvRV
($input
)) != SVt_PVAV
)
63 croak
("Math::GSL : $$1_name is not an array ref!");
65 tempav
= (AV
*)SvRV
($input
);
67 $
1 = (float
*) malloc
((len
+1)*sizeof
(float
));
68 for
(i
= 0; i
<= len
; i
++) {
69 tv
= av_fetch
(tempav
, i
, 0);
70 $
1[i
] = (float
)(double
) SvNV
(*tv
);
74 %typemap
(freearg
) float const
[] {
78 %apply float const
[] {
79 float const
*A
, float const
*B
, float const
*C
, float
*C
82 /*****************************
83 * handle 'size_t const
[]' as an input array of size_t
84 * We allocate the C array at the begining and free it at the end
86 %typemap
(in
) size_t const
[] {
92 croak
("Math::GSL : $$1_name is not a reference!");
93 if
(SvTYPE
(SvRV
($input
)) != SVt_PVAV
)
94 croak
("Math::GSL : $$1_name is not an array ref!");
96 tempav
= (AV
*)SvRV
($input
);
98 $
1 = (size_t
*) malloc
((len
+1)*sizeof
(size_t
));
99 for
(i
= 0; i
<= len
; i
++) {
100 tv
= av_fetch
(tempav
, i
, 0);
105 %typemap
(freearg
) size_t const
[] {
109 %apply size_t const
[] {
113 /*****************************
114 * handle some parameters as input or output
116 %apply int
*OUTPUT { size_t
*imin
, size_t
*imax
, size_t
*neval
};
117 %apply double
* OUTPUT {
118 double
* min_out
, double
* max_out
,
119 double
*abserr
, double
*result
122 /*****************************
126 /* structure to hold required information while the gsl function call
129 struct gsl_function_perl
{
130 gsl_function C_gsl_function
;
134 struct gsl_monte_function_perl
{
135 gsl_monte_function C_gsl_monte_function
;
142 /* These functions
(C callbacks
) calls the perl callbacks.
143 Info for perl callback can be found using the 'void
*params' parameter
145 double call_gsl_function
(double x
, void
*params
){
146 struct gsl_function_perl
*F
=(struct gsl_function_perl
*)params
;
151 //fprintf
(stderr
, "LOOKUP CALLBACK\n");
156 XPUSHs
(sv_2mortal
(newSVnv
((double
)x
)));
158 PUTBACK
; /* make local stack pointer global
*/
160 count
= call_sv
(F-
>function
, G_SCALAR
);
164 croak
("Expected to call subroutine in scalar context!");
168 PUTBACK
; /* make local stack pointer global
*/
174 double call_gsl_monte_function
(double
*x_array
, size_t dim
, void
*params
){
175 struct gsl_monte_function_perl
*F
=(struct gsl_monte_function_perl
*)params
;
182 //fprintf
(stderr
, "LOOKUP CALLBACK\n");
188 sv_2mortal
((SV
*)perl_array
);
189 XPUSHs
(sv_2mortal
(newRV
((SV
*)perl_array
)));
190 for
(i
=0; i
<dim
; i
++) {
191 /* no mortal
: it is referenced by the array
*/
192 av_push
(perl_array
, newSVnv
(x_array
[i
]));
194 XPUSHs
(sv_2mortal
(newSViv
(dim
)));
196 PUTBACK
; /* make local stack pointer global
*/
198 count
= call_sv
(F-
>f
, G_SCALAR
);
202 croak
("Expected to call subroutine in scalar context!");
206 PUTBACK
; /* make local stack pointer global
*/
214 %typemap
(in
) gsl_monte_function
* (struct gsl_monte_function_perl w_gsl_monte_function
) {
220 if
(SvROK
($input
) && (SvTYPE(SvRV($input)) == SVt_PVAV)) {
221 AV
* array
=(AV
*)SvRV
($input
);
223 if
(av_len
(array
)<0) {
224 croak
("Math::GSL : $$1_name is an empty array!");
226 if
(av_len
(array
)>2) {
227 croak
("Math::GSL : $$1_name is an array with more than 3 elements!");
229 p_f
= av_fetch
(array
, 0, 0);
231 if
(av_len
(array
)>0) {
233 p_dim
= av_fetch
(array
, 1, 0);
236 if
(av_len
(array
)>1) {
238 p_params
= av_fetch
(array
, 1, 0);
245 if
(!f ||
!(SvPOK
(f
) ||
(SvROK
(f
) && (SvTYPE(SvRV(f)) == SVt_PVCV)))) {
246 croak
("Math::GSL : $$1_name is not a reference to code!");
256 croak
("Math::GSL : $$1_name is not an integer for dim!");
265 params
= newSVsv
(params
);
267 w_gsl_monte_function.f
= f
;
268 w_gsl_monte_function.dim
= dim
;
269 w_gsl_monte_function.params
= params
;
270 w_gsl_monte_function.C_gsl_monte_function.f
= &call_gsl_monte_function;
271 w_gsl_monte_function.C_gsl_monte_function.dim
= C_dim
;
272 w_gsl_monte_function.C_gsl_monte_function.params
= &w_gsl_monte_function;
273 $
1 = &w_gsl_monte_function.C_gsl_monte_function;
276 %typemap
(in
) gsl_function
* (struct gsl_function_perl w_gsl_function
) {
280 if
(SvROK
($input
) && (SvTYPE(SvRV($input)) == SVt_PVAV)) {
281 AV
* array
=(AV
*)SvRV
($input
);
282 SV
** p_function
= 0;
283 if
(av_len
(array
)<0) {
284 croak
("Math::GSL : $$1_name is an empty array!");
286 if
(av_len
(array
)>1) {
287 croak
("Math::GSL : $$1_name is an array with more than 2 elements!");
289 p_function
= av_fetch
(array
, 0, 0);
290 function
= *p_function
;
291 if
(av_len
(array
)>0) {
293 p_params
= av_fetch
(array
, 1, 0);
300 if
(!function ||
!(SvPOK
(function
) ||
(SvROK
(function
) && (SvTYPE(SvRV(function)) == SVt_PVCV)))) {
301 croak
("Math::GSL : $$1_name is not a reference to code!");
304 function
= newSVsv
(function
);
309 params
= newSVsv
(params
);
311 w_gsl_function.params
= params
;
312 w_gsl_function.function
= function
;
313 w_gsl_function.C_gsl_function.params
= &w_gsl_function;
314 w_gsl_function.C_gsl_function.function
= &call_gsl_function;
315 $
1 = &w_gsl_function.C_gsl_function;
318 %typemap
(freearg
) gsl_monte_function
* {
319 struct gsl_monte_function_perl
*p
=(struct gsl_monte_function_perl
*) $
1->params
;
321 SvREFCNT_dec
(p-
>dim
);
322 SvREFCNT_dec
(p-
>params
);
325 %typemap
(freearg
) gsl_function
* {
326 struct gsl_function_perl
*p
=(struct gsl_function_perl
*) $
1->params
;
327 SvREFCNT_dec
(p-
>function
);
328 SvREFCNT_dec
(p-
>params
);
331 /* TODO
: same thing should be done for these kinds of callbacks
*/
332 %typemap
(in
) gsl_function_fdf
* {
333 fprintf
(stderr
, 'FDF_FUNC'
);