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 double const
[] {
110 double
*data
, double
*dest
, double
*f_in
, double
*f_out
,
111 double data
[], const double
* src
, double x
[], double a
[], double b
[],
112 double xu
[], double xl
[],
113 const double
* x
, const double
* y
, const double
* w
, const double x_array
[],
114 const double xrange
[], const double yrange
[], double
* base
,
115 const double
* base
, const double xrange
[], const double yrange
[] ,
116 const double
* array
, const double data2
[], const double w
[] ,
118 gsl_complex_packed_array data
121 %apply float const
[] {
122 float const
*A
, float const
*B
, float const
*C
, float
*C
125 %apply size_t const
[] {
129 /*****************************
130 * handle some parameters as input or output
132 %apply int
*OUTPUT { size_t
*imin
, size_t
*imax
, size_t
*neval
};
133 %apply double
* OUTPUT {
134 double
* min_out
, double
* max_out
,
135 double
*abserr
, double
*result
138 /*****************************
142 /* structure to hold required information while the gsl function call
145 struct gsl_function_perl
{
146 gsl_function C_gsl_function
;
150 struct gsl_monte_function_perl
{
151 gsl_monte_function C_gsl_monte_function
;
158 /* These functions
(C callbacks
) calls the perl callbacks.
159 Info for perl callback can be found using the 'void
*params' parameter
161 double call_gsl_function
(double x
, void
*params
){
162 struct gsl_function_perl
*F
=(struct gsl_function_perl
*)params
;
167 //fprintf
(stderr
, "LOOKUP CALLBACK\n");
172 XPUSHs
(sv_2mortal
(newSVnv
((double
)x
)));
174 PUTBACK
; /* make local stack pointer global
*/
176 count
= call_sv
(F-
>function
, G_SCALAR
);
180 croak
("Expected to call subroutine in scalar context!");
184 PUTBACK
; /* make local stack pointer global
*/
190 double call_gsl_monte_function
(double
*x_array
, size_t dim
, void
*params
){
191 struct gsl_monte_function_perl
*F
=(struct gsl_monte_function_perl
*)params
;
198 //fprintf
(stderr
, "LOOKUP CALLBACK\n");
204 sv_2mortal
((SV
*)perl_array
);
205 XPUSHs
(sv_2mortal
(newRV
((SV
*)perl_array
)));
206 for
(i
=0; i
<dim
; i
++) {
207 /* no mortal
: it is referenced by the array
*/
208 av_push
(perl_array
, newSVnv
(x_array
[i
]));
210 XPUSHs
(sv_2mortal
(newSViv
(dim
)));
212 PUTBACK
; /* make local stack pointer global
*/
214 count
= call_sv
(F-
>f
, G_SCALAR
);
218 croak
("Expected to call subroutine in scalar context!");
222 PUTBACK
; /* make local stack pointer global
*/
230 %typemap
(in
) gsl_monte_function
* (struct gsl_monte_function_perl w_gsl_monte_function
) {
236 if
(SvROK
($input
) && (SvTYPE(SvRV($input)) == SVt_PVAV)) {
237 AV
* array
=(AV
*)SvRV
($input
);
239 if
(av_len
(array
)<0) {
240 croak
("Math::GSL : $$1_name is an empty array!");
242 if
(av_len
(array
)>2) {
243 croak
("Math::GSL : $$1_name is an array with more than 3 elements!");
245 p_f
= av_fetch
(array
, 0, 0);
247 if
(av_len
(array
)>0) {
249 p_dim
= av_fetch
(array
, 1, 0);
252 if
(av_len
(array
)>1) {
254 p_params
= av_fetch
(array
, 1, 0);
261 if
(!f ||
!(SvPOK
(f
) ||
(SvROK
(f
) && (SvTYPE(SvRV(f)) == SVt_PVCV)))) {
262 croak
("Math::GSL : $$1_name is not a reference to code!");
272 croak
("Math::GSL : $$1_name is not an integer for dim!");
281 params
= newSVsv
(params
);
283 w_gsl_monte_function.f
= f
;
284 w_gsl_monte_function.dim
= dim
;
285 w_gsl_monte_function.params
= params
;
286 w_gsl_monte_function.C_gsl_monte_function.f
= &call_gsl_monte_function;
287 w_gsl_monte_function.C_gsl_monte_function.dim
= C_dim
;
288 w_gsl_monte_function.C_gsl_monte_function.params
= &w_gsl_monte_function;
289 $
1 = &w_gsl_monte_function.C_gsl_monte_function;
292 %typemap
(in
) gsl_function
* (struct gsl_function_perl w_gsl_function
) {
296 if
(SvROK
($input
) && (SvTYPE(SvRV($input)) == SVt_PVAV)) {
297 AV
* array
=(AV
*)SvRV
($input
);
298 SV
** p_function
= 0;
299 if
(av_len
(array
)<0) {
300 croak
("Math::GSL : $$1_name is an empty array!");
302 if
(av_len
(array
)>1) {
303 croak
("Math::GSL : $$1_name is an array with more than 2 elements!");
305 p_function
= av_fetch
(array
, 0, 0);
306 function
= *p_function
;
307 if
(av_len
(array
)>0) {
309 p_params
= av_fetch
(array
, 1, 0);
316 if
(!function ||
!(SvPOK
(function
) ||
(SvROK
(function
) && (SvTYPE(SvRV(function)) == SVt_PVCV)))) {
317 croak
("Math::GSL : $$1_name is not a reference to code!");
320 function
= newSVsv
(function
);
325 params
= newSVsv
(params
);
327 w_gsl_function.params
= params
;
328 w_gsl_function.function
= function
;
329 w_gsl_function.C_gsl_function.params
= &w_gsl_function;
330 w_gsl_function.C_gsl_function.function
= &call_gsl_function;
331 $
1 = &w_gsl_function.C_gsl_function;
334 %typemap
(freearg
) gsl_monte_function
* {
335 struct gsl_monte_function_perl
*p
=(struct gsl_monte_function_perl
*) $
1->params
;
337 SvREFCNT_dec
(p-
>dim
);
338 SvREFCNT_dec
(p-
>params
);
341 %typemap
(freearg
) gsl_function
* {
342 struct gsl_function_perl
*p
=(struct gsl_function_perl
*) $
1->params
;
343 SvREFCNT_dec
(p-
>function
);
344 SvREFCNT_dec
(p-
>params
);
347 /* TODO
: same thing should be done for these kinds of callbacks
*/
348 %typemap
(in
) gsl_function_fdf
* {
349 fprintf
(stderr
, 'FDF_FUNC'
);