File: mlgsl_integration.c

package info (click to toggle)
ocamlgsl 0.6.0-7
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, squeeze, wheezy
  • size: 4,028 kB
  • ctags: 3,090
  • sloc: ml: 8,539; ansic: 7,338; makefile: 261; sh: 149; awk: 13
file content (340 lines) | stat: -rw-r--r-- 10,164 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/* ocamlgsl - OCaml interface to GSL                        */
/* Copyright (©) 2002-2005 - Olivier Andrieu                */
/* distributed under the terms of the GPL version 2         */

#include <gsl/gsl_errno.h>
#include <gsl/gsl_integration.h>

#include <caml/mlvalues.h>
#include <caml/alloc.h>
#include <caml/callback.h>
#include <caml/fail.h>
#include <caml/memory.h>

#include "mlgsl_fun.h"
#include "wrappers.h"


/* QNG integration */
CAMLprim value ml_gsl_integration_qng(value fun, value a, value b, 
				      value epsabs, value epsrel)
{
  CAMLparam1(fun);
  CAMLlocal3(res, r, e);
  GSLFUN_CLOSURE(gf, fun);
  double result, abserr;
  size_t neval;
  
  gsl_integration_qng(&gf, Double_val(a), Double_val(b),
		      Double_val(epsabs), Double_val(epsrel),
		      &result, &abserr, &neval);
  r = copy_double(result);
  e = copy_double(abserr);
  res = alloc_small(3, 0);
  Field(res, 0) = r;
  Field(res, 1) = e;
  Field(res, 2) = Val_int(neval);
  CAMLreturn(res);
}




/* WORKSPACE */
#define GSL_WS(v) (gsl_integration_workspace *)(Field((v), 0))

ML1_alloc(gsl_integration_workspace_alloc, Int_val, Abstract_ptr)
ML1(gsl_integration_workspace_free, GSL_WS, Unit)

CAMLprim value ml_gsl_integration_ws_size(value ws)
{
  return Val_int((GSL_WS(ws))->size);
}


/* QAG Integration */
CAMLprim value ml_gsl_integration_qag(value fun, value a, value b,
				      value epsabs, value epsrel,
				      value limit, value key, value ws)
{
  CAMLparam2(fun, ws);
  GSLFUN_CLOSURE(gf, fun);
  double result, abserr;
  size_t c_limit;
  static const int key_conv [] = { GSL_INTEG_GAUSS15, GSL_INTEG_GAUSS21, 
				   GSL_INTEG_GAUSS31, GSL_INTEG_GAUSS41, 
				   GSL_INTEG_GAUSS51, GSL_INTEG_GAUSS61 };
  int c_key = key_conv [ Int_val(key) ];
  gsl_integration_workspace *gslws = GSL_WS(ws);

  c_limit = Opt_arg(limit, Int_val, gslws->limit);
  gsl_integration_qag(&gf, 
		      Double_val(a), Double_val(b), 
		      Double_val(epsabs), Double_val(epsrel),
		      c_limit, c_key, gslws, &result, &abserr);
  CAMLreturn(copy_two_double_arr(result, abserr));
}

CAMLprim value ml_gsl_integration_qag_bc(value *args, int nb)
{
  return ml_gsl_integration_qag(args[0], args[1], args[2], args[3], args[4],
				args[5], args[6], args[7]);
}

CAMLprim value ml_gsl_integration_qags(value fun, value a, value b,
				       value epsabs, value epsrel,
				       value limit, value ws)
{
  CAMLparam2(fun, ws);
  GSLFUN_CLOSURE(gf, fun);
  double result, abserr;
  size_t c_limit;
  gsl_integration_workspace *gslws = GSL_WS(ws);

  c_limit = Opt_arg(limit, Int_val, gslws->limit);
  gsl_integration_qags(&gf,
		       Double_val(a), Double_val(b), 
		       Double_val(epsabs), Double_val(epsrel),
		       c_limit, gslws, &result, &abserr);
  CAMLreturn(copy_two_double_arr(result, abserr));
}

CAMLprim value ml_gsl_integration_qags_bc(value *args, int nb)
{
  return ml_gsl_integration_qags(args[0], args[1], args[2], args[3], args[4],
				 args[5], args[6]);
}


CAMLprim value ml_gsl_integration_qagp(value fun, value pts, 
				       value epsabs, value epsrel,
				       value limit, value ws)
{
  CAMLparam2(fun, ws);
  GSLFUN_CLOSURE(gf, fun);
  double result, abserr;
  size_t c_limit;
  gsl_integration_workspace *gslws = GSL_WS(ws);
  
  c_limit = Opt_arg(limit, Int_val, gslws->limit);
  gsl_integration_qagp(&gf, 
		       Double_array_val(pts), Double_array_length(pts),
		       Double_val(epsabs), Double_val(epsrel),
		       c_limit, gslws, &result, &abserr);
  CAMLreturn(copy_two_double_arr(result, abserr));
}  

CAMLprim value ml_gsl_integration_qagp_bc(value *args, int nb)
{
  return ml_gsl_integration_qagp(args[0], args[1], args[2],
				 args[3], args[4], args[5]);
}

CAMLprim value ml_gsl_integration_qagi(value fun, 
				       value epsabs, value epsrel,
				       value limit, value ws)
{
  CAMLparam2(fun, ws);
  GSLFUN_CLOSURE(gf, fun);
  double result, abserr;
  size_t c_limit;
  gsl_integration_workspace *gslws = GSL_WS(ws);
  
  c_limit = Opt_arg(limit, Int_val,gslws->limit);
  gsl_integration_qagi(&gf,
		       Double_val(epsabs), Double_val(epsrel),
		       c_limit, gslws, &result, &abserr);
  CAMLreturn(copy_two_double_arr(result, abserr));
}  

CAMLprim value ml_gsl_integration_qagiu(value fun, value a, 
					value epsabs, value epsrel,
					value limit, value ws)
{
  CAMLparam2(fun, ws);
  GSLFUN_CLOSURE(gf, fun);
  double result, abserr;
  size_t c_limit;
  gsl_integration_workspace *gslws = GSL_WS(ws);
  
  c_limit = Opt_arg(limit, Int_val, gslws->limit);
  gsl_integration_qagiu(&gf, Double_val(a),
			Double_val(epsabs), Double_val(epsrel),
			c_limit, gslws, &result, &abserr);
  CAMLreturn(copy_two_double_arr(result, abserr));
}  

CAMLprim value ml_gsl_integration_qagiu_bc(value *args, int nb)
{
  return ml_gsl_integration_qagiu(args[0], args[1], args[2],
				  args[3], args[4], args[5]);
}


CAMLprim value ml_gsl_integration_qagil(value fun, value b, 
					value epsabs, value epsrel,
					value limit, value ws)
{
  CAMLparam2(fun, ws);
  GSLFUN_CLOSURE(gf, fun);
  double result, abserr;
  size_t c_limit;
  gsl_integration_workspace *gslws = GSL_WS(ws);
  
  c_limit = Opt_arg(limit, Int_val, gslws->limit);
  gsl_integration_qagil(&gf, Double_val(b),
			Double_val(epsabs), Double_val(epsrel),
			c_limit, gslws, &result, &abserr);
  CAMLreturn(copy_two_double_arr(result, abserr));
}  

CAMLprim value ml_gsl_integration_qagil_bc(value *args, int nb)
{
  return ml_gsl_integration_qagil(args[0], args[1], args[2],
				  args[3], args[4], args[5]);
}



/* QAWC integration */

CAMLprim value ml_gsl_integration_qawc(value fun, value a, value b, value c,
				       value epsabs, value epsrel,
				       value limit, value ws)
{
  CAMLparam2(fun, ws);
  GSLFUN_CLOSURE(gf, fun);
  double result, abserr;
  size_t c_limit;
  gsl_integration_workspace *gslws = GSL_WS(ws);
  
  c_limit = Opt_arg(limit, Int_val, gslws->limit);
  gsl_integration_qawc(&gf,
		       Double_val(a), Double_val(b), Double_val(c),
		       Double_val(epsabs), Double_val(epsrel),
		       c_limit, gslws, &result, &abserr);
  CAMLreturn(copy_two_double_arr(result, abserr));
}  

CAMLprim value ml_gsl_integration_qawc_bc(value *args, int nb)
{
  return ml_gsl_integration_qawc(args[0], args[1], args[2], args[3], 
				 args[4], args[5], args[6], args[7]);
}



/* QAWS integration */
CAMLprim value ml_gsl_integration_qaws_table_alloc(value alpha, value beta,
						   value mu, value nu)
{
  value res;
  Abstract_ptr(res, gsl_integration_qaws_table_alloc(Double_val(alpha),
                                                     Double_val(beta),
                                                     Int_val(mu),
                                                     Int_val(nu)));
  return res;
}
#define QAWSTABLE_VAL(v) (gsl_integration_qaws_table *)Field((v), 0)

ML5(gsl_integration_qaws_table_set, QAWSTABLE_VAL, Double_val, Double_val, Int_val, Int_val, Unit)
ML1(gsl_integration_qaws_table_free, QAWSTABLE_VAL, Unit)

CAMLprim value ml_gsl_integration_qaws(value fun, value a, value b, value table ,
				       value epsabs, value epsrel,
				       value limit, value ws)
{
  CAMLparam3(fun, ws, table);
  GSLFUN_CLOSURE(gf, fun);
  double result, abserr;
  size_t c_limit;
  gsl_integration_workspace *gslws = GSL_WS(ws);
  
  c_limit = Opt_arg(limit, Int_val, gslws->limit);
  gsl_integration_qaws(&gf, 
		       Double_val(a), Double_val(b), QAWSTABLE_VAL(table),
		       Double_val(epsabs), Double_val(epsrel),
		       c_limit, gslws, &result, &abserr);
  CAMLreturn(copy_two_double_arr(result, abserr));
}  

CAMLprim value ml_gsl_integration_qaws_bc(value *args, int nb)
{
  return ml_gsl_integration_qaws(args[0], args[1], args[2], args[3], 
				 args[4], args[5], args[6], args[7]);
}



/* QAWO integration */
static inline enum gsl_integration_qawo_enum qawo_of_val(value sine)
{
  static const enum gsl_integration_qawo_enum qawo_sine[] = { 
    GSL_INTEG_COSINE, GSL_INTEG_SINE };
  return qawo_sine[Int_val(sine)];
}

CAMLprim value ml_gsl_integration_qawo_table_alloc(value omega, value l,
						   value sine, value n)
{
  value res;
  Abstract_ptr(res, gsl_integration_qawo_table_alloc(Double_val(omega),
                                                     Double_val(l),
                                                     qawo_of_val(sine),
                                                     Int_val(n)));
  return res;
}
#define QAWOTABLE_VAL(v) (gsl_integration_qawo_table *)Field((v), 0)

ML4(gsl_integration_qawo_table_set, QAWOTABLE_VAL, Double_val, Double_val, qawo_of_val, Unit)
ML1(gsl_integration_qawo_table_free, QAWOTABLE_VAL, Unit)

CAMLprim value ml_gsl_integration_qawo(value fun, value a,
				       value epsabs, value epsrel,
				       value limit, value ws, value table)
{
  CAMLparam3(fun, ws, table);
  GSLFUN_CLOSURE(gf, fun);
  double result, abserr;
  size_t c_limit;
  gsl_integration_workspace *gslws = GSL_WS(ws);
  
  c_limit = Opt_arg(limit, Int_val, gslws->limit);
  gsl_integration_qawo(&gf, Double_val(a), 
		       Double_val(epsabs), Double_val(epsrel),
		       c_limit, gslws, 
		       QAWOTABLE_VAL(table),
		       &result, &abserr);
  CAMLreturn(copy_two_double_arr(result, abserr));
}  

CAMLprim value ml_gsl_integration_qawo_bc(value *args, int nb)
{
  return ml_gsl_integration_qawo(args[0], args[1], args[2], args[3], 
				 args[4], args[5], args[6]);
}

CAMLprim value ml_gsl_integration_qawf(value fun, value a, value epsabs, 
				       value limit, value ws, value cyclews,
				       value table)
{
  CAMLparam4(fun, ws, cyclews, table);
  GSLFUN_CLOSURE(gf, fun);
  double result, abserr;
  size_t c_limit;
  gsl_integration_workspace *gslws = GSL_WS(ws);
  
  c_limit = Opt_arg(limit, Int_val, gslws->limit);
  gsl_integration_qawf(&gf, Double_val(a), 
		       Double_val(epsabs), c_limit, 
		       gslws, GSL_WS(cyclews),
		       QAWOTABLE_VAL(table),
		       &result, &abserr);
  CAMLreturn(copy_two_double_arr(result, abserr));
}  

CAMLprim value ml_gsl_integration_qawf_bc(value *args, int nb)
{
  return ml_gsl_integration_qawf(args[0], args[1], args[2], args[3], 
				 args[4], args[5], args[6]);
}