romanbsd-gsl 1.11.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (156) hide show
  1. data/README.rdoc +29 -0
  2. data/Rakefile +54 -0
  3. data/VERSION +1 -0
  4. data/ext/MANIFEST +116 -0
  5. data/ext/array.c +665 -0
  6. data/ext/array_complex.c +247 -0
  7. data/ext/blas.c +29 -0
  8. data/ext/blas1.c +729 -0
  9. data/ext/blas2.c +1093 -0
  10. data/ext/blas3.c +881 -0
  11. data/ext/block.c +44 -0
  12. data/ext/block_source.c +885 -0
  13. data/ext/bspline.c +116 -0
  14. data/ext/bundle.c +3 -0
  15. data/ext/cdf.c +754 -0
  16. data/ext/cheb.c +538 -0
  17. data/ext/combination.c +283 -0
  18. data/ext/common.c +310 -0
  19. data/ext/complex.c +1005 -0
  20. data/ext/const.c +668 -0
  21. data/ext/const_additional.c +120 -0
  22. data/ext/cqp.c +283 -0
  23. data/ext/deriv.c +194 -0
  24. data/ext/dht.c +360 -0
  25. data/ext/diff.c +165 -0
  26. data/ext/dirac.c +395 -0
  27. data/ext/eigen.c +2373 -0
  28. data/ext/error.c +194 -0
  29. data/ext/extconf.rb +272 -0
  30. data/ext/fcmp.c +66 -0
  31. data/ext/fft.c +1092 -0
  32. data/ext/fit.c +205 -0
  33. data/ext/fresnel.c +312 -0
  34. data/ext/function.c +522 -0
  35. data/ext/graph.c +1634 -0
  36. data/ext/gsl.c +265 -0
  37. data/ext/gsl_narray.c +652 -0
  38. data/ext/histogram.c +1717 -0
  39. data/ext/histogram2d.c +1067 -0
  40. data/ext/histogram3d.c +883 -0
  41. data/ext/histogram3d_source.c +750 -0
  42. data/ext/histogram_find.c +101 -0
  43. data/ext/histogram_oper.c +159 -0
  44. data/ext/ieee.c +98 -0
  45. data/ext/integration.c +1138 -0
  46. data/ext/interp.c +511 -0
  47. data/ext/jacobi.c +737 -0
  48. data/ext/linalg.c +4045 -0
  49. data/ext/linalg_complex.c +743 -0
  50. data/ext/math.c +724 -0
  51. data/ext/matrix.c +39 -0
  52. data/ext/matrix_complex.c +1731 -0
  53. data/ext/matrix_double.c +560 -0
  54. data/ext/matrix_int.c +256 -0
  55. data/ext/matrix_source.c +2678 -0
  56. data/ext/min.c +234 -0
  57. data/ext/monte.c +759 -0
  58. data/ext/multifit.c +1810 -0
  59. data/ext/multimin.c +793 -0
  60. data/ext/multimin_fsdf.c +156 -0
  61. data/ext/multiroots.c +952 -0
  62. data/ext/ndlinear.c +320 -0
  63. data/ext/nmf.c +159 -0
  64. data/ext/nmf_wrap.c +63 -0
  65. data/ext/ntuple.c +469 -0
  66. data/ext/odeiv.c +958 -0
  67. data/ext/ool.c +879 -0
  68. data/ext/oper_complex_source.c +253 -0
  69. data/ext/permutation.c +596 -0
  70. data/ext/poly.c +42 -0
  71. data/ext/poly2.c +265 -0
  72. data/ext/poly_source.c +1799 -0
  73. data/ext/qrng.c +171 -0
  74. data/ext/randist.c +1869 -0
  75. data/ext/rational.c +480 -0
  76. data/ext/rng.c +612 -0
  77. data/ext/root.c +408 -0
  78. data/ext/sf.c +1494 -0
  79. data/ext/sf_airy.c +200 -0
  80. data/ext/sf_bessel.c +867 -0
  81. data/ext/sf_clausen.c +28 -0
  82. data/ext/sf_coulomb.c +206 -0
  83. data/ext/sf_coupling.c +118 -0
  84. data/ext/sf_dawson.c +29 -0
  85. data/ext/sf_debye.c +157 -0
  86. data/ext/sf_dilog.c +42 -0
  87. data/ext/sf_elementary.c +44 -0
  88. data/ext/sf_ellint.c +206 -0
  89. data/ext/sf_elljac.c +29 -0
  90. data/ext/sf_erfc.c +93 -0
  91. data/ext/sf_exp.c +164 -0
  92. data/ext/sf_expint.c +211 -0
  93. data/ext/sf_fermi_dirac.c +148 -0
  94. data/ext/sf_gamma.c +344 -0
  95. data/ext/sf_gegenbauer.c +96 -0
  96. data/ext/sf_hyperg.c +197 -0
  97. data/ext/sf_laguerre.c +112 -0
  98. data/ext/sf_lambert.c +47 -0
  99. data/ext/sf_legendre.c +367 -0
  100. data/ext/sf_log.c +104 -0
  101. data/ext/sf_mathieu.c +238 -0
  102. data/ext/sf_power.c +46 -0
  103. data/ext/sf_psi.c +98 -0
  104. data/ext/sf_synchrotron.c +48 -0
  105. data/ext/sf_transport.c +76 -0
  106. data/ext/sf_trigonometric.c +207 -0
  107. data/ext/sf_zeta.c +119 -0
  108. data/ext/signal.c +310 -0
  109. data/ext/siman.c +718 -0
  110. data/ext/sort.c +208 -0
  111. data/ext/spline.c +393 -0
  112. data/ext/stats.c +799 -0
  113. data/ext/sum.c +168 -0
  114. data/ext/tamu_anova.c +56 -0
  115. data/ext/tensor.c +38 -0
  116. data/ext/tensor_source.c +1121 -0
  117. data/ext/vector.c +38 -0
  118. data/ext/vector_complex.c +2139 -0
  119. data/ext/vector_double.c +1445 -0
  120. data/ext/vector_int.c +204 -0
  121. data/ext/vector_source.c +3325 -0
  122. data/ext/wavelet.c +937 -0
  123. data/include/rb_gsl.h +140 -0
  124. data/include/rb_gsl_array.h +230 -0
  125. data/include/rb_gsl_cheb.h +21 -0
  126. data/include/rb_gsl_common.h +343 -0
  127. data/include/rb_gsl_complex.h +25 -0
  128. data/include/rb_gsl_const.h +29 -0
  129. data/include/rb_gsl_dirac.h +15 -0
  130. data/include/rb_gsl_eigen.h +17 -0
  131. data/include/rb_gsl_fft.h +62 -0
  132. data/include/rb_gsl_fit.h +25 -0
  133. data/include/rb_gsl_function.h +27 -0
  134. data/include/rb_gsl_graph.h +70 -0
  135. data/include/rb_gsl_histogram.h +65 -0
  136. data/include/rb_gsl_histogram3d.h +97 -0
  137. data/include/rb_gsl_integration.h +17 -0
  138. data/include/rb_gsl_interp.h +46 -0
  139. data/include/rb_gsl_linalg.h +25 -0
  140. data/include/rb_gsl_math.h +26 -0
  141. data/include/rb_gsl_odeiv.h +21 -0
  142. data/include/rb_gsl_poly.h +71 -0
  143. data/include/rb_gsl_rational.h +37 -0
  144. data/include/rb_gsl_rng.h +21 -0
  145. data/include/rb_gsl_root.h +22 -0
  146. data/include/rb_gsl_sf.h +119 -0
  147. data/include/rb_gsl_statistics.h +17 -0
  148. data/include/rb_gsl_tensor.h +45 -0
  149. data/include/rb_gsl_with_narray.h +22 -0
  150. data/include/templates_off.h +87 -0
  151. data/include/templates_on.h +241 -0
  152. data/lib/gsl/gnuplot.rb +41 -0
  153. data/lib/gsl/oper.rb +68 -0
  154. data/lib/ool.rb +22 -0
  155. data/lib/ool/conmin.rb +30 -0
  156. metadata +221 -0
data/ext/function.c ADDED
@@ -0,0 +1,522 @@
1
+ /*
2
+ function.c
3
+ Ruby/GSL: Ruby extension library for GSL (GNU Scientific Library)
4
+ (C) Copyright 2001-2006 by Yoshiki Tsunesada
5
+
6
+ Ruby/GSL is free software: you can redistribute it and/or modify it
7
+ under the terms of the GNU General Public License.
8
+ This library is distributed in the hope that it will be useful, but
9
+ WITHOUT ANY WARRANTY.
10
+ */
11
+ #include "rb_gsl_config.h"
12
+ #include "rb_gsl_function.h"
13
+ #ifdef HAVE_NARRAY_H
14
+ #include "narray.h"
15
+ #endif
16
+
17
+ VALUE cgsl_function;
18
+ VALUE cgsl_function_fdf;
19
+
20
+ void gsl_function_free(gsl_function *f);
21
+ double rb_gsl_function_f(double x, void *p);
22
+ ID RBGSL_ID_call, RBGSL_ID_arity;
23
+
24
+ static VALUE rb_gsl_function_set_f(int argc, VALUE *argv, VALUE obj)
25
+ {
26
+ gsl_function *F = NULL;
27
+ VALUE ary, ary2;
28
+ size_t i;
29
+ Data_Get_Struct(obj, gsl_function, F);
30
+ if (F->params == NULL) {
31
+ ary = rb_ary_new2(2);
32
+ /* (VALUE) F->params = ary;*/
33
+ F->params = (void *) ary;
34
+ } else {
35
+ ary = (VALUE) F->params;
36
+ }
37
+ rb_ary_store(ary, 1, Qnil);
38
+
39
+ switch (argc) {
40
+ case 0:
41
+ break;
42
+ case 1:
43
+ CHECK_PROC(argv[0]);
44
+ rb_ary_store(ary, 0, argv[0]);
45
+ break;
46
+ case 2:
47
+ CHECK_PROC(argv[0]);
48
+ rb_ary_store(ary, 0, argv[0]);
49
+ rb_ary_store(ary, 1, argv[1]);
50
+ break;
51
+ default:
52
+ CHECK_PROC(argv[0]);
53
+ rb_ary_store(ary, 0, argv[0]);
54
+ ary2 = rb_ary_new2(argc-1);
55
+ for (i = 1; i < argc; i++) rb_ary_store(ary2, i-1, argv[i]);
56
+ rb_ary_store(ary, 1, ary2);
57
+ break;
58
+ }
59
+ if (rb_block_given_p()) rb_ary_store(ary, 0, RB_GSL_MAKE_PROC);
60
+ return obj;
61
+ }
62
+
63
+ void gsl_function_free(gsl_function *f)
64
+ {
65
+ if (f) free((gsl_function *) f);
66
+ }
67
+
68
+ void gsl_function_mark(gsl_function *f)
69
+ {
70
+ rb_gc_mark((VALUE) f->params);
71
+ }
72
+
73
+ /*
74
+ * Create a Function object
75
+ */
76
+ static VALUE rb_gsl_function_alloc(int argc, VALUE *argv, VALUE klass)
77
+ {
78
+ gsl_function *f = NULL;
79
+ VALUE obj;
80
+ f = ALLOC(gsl_function);
81
+ f->function = &rb_gsl_function_f;
82
+ /* (VALUE) f->params = rb_ary_new2(2);*/
83
+ f->params = (void *) rb_ary_new2(2);
84
+ rb_ary_store((VALUE) f->params, 1, Qnil);
85
+ obj = Data_Wrap_Struct(klass, gsl_function_mark, gsl_function_free, f);
86
+ rb_gsl_function_set_f(argc, argv, obj);
87
+ return obj;
88
+ }
89
+
90
+ double rb_gsl_function_f(double x, void *p)
91
+ {
92
+ VALUE result, ary, proc, params;
93
+ ary = (VALUE) p;
94
+ proc = rb_ary_entry(ary, 0);
95
+ params = rb_ary_entry(ary, 1);
96
+ if (NIL_P(params)) result = rb_funcall(proc, RBGSL_ID_call, 1, rb_float_new(x));
97
+ else result = rb_funcall(proc, RBGSL_ID_call, 2, rb_float_new(x), params);
98
+ return NUM2DBL(result);
99
+ }
100
+
101
+ /*
102
+ * Calculates a function at x, and returns the rusult.
103
+ */
104
+ static VALUE rb_gsl_function_eval(VALUE obj, VALUE x)
105
+ {
106
+ gsl_function *F = NULL;
107
+ VALUE ary, proc, params, result, arynew, x2;
108
+ gsl_vector *v = NULL, *vnew = NULL;
109
+ gsl_matrix *m = NULL, *mnew = NULL;
110
+ size_t i, j, n;
111
+ #ifdef HAVE_NARRAY_H
112
+ double *ptr1, *ptr2;
113
+ struct NARRAY *na;
114
+ #endif
115
+ Data_Get_Struct(obj, gsl_function, F);
116
+ ary = (VALUE) F->params;
117
+ proc = rb_ary_entry(ary, 0);
118
+ params = rb_ary_entry(ary, 1);
119
+ if (CLASS_OF(x) == rb_cRange) x = rb_gsl_range2ary(x);
120
+ switch (TYPE(x)) {
121
+ case T_FIXNUM:
122
+ case T_BIGNUM:
123
+ case T_FLOAT:
124
+ if (NIL_P(params)) result = rb_funcall(proc, RBGSL_ID_call, 1, x);
125
+ else result = rb_funcall(proc, RBGSL_ID_call, 2, x, params);
126
+ return result;
127
+ break;
128
+ case T_ARRAY:
129
+ n = RARRAY(x)->len;
130
+ arynew = rb_ary_new2(n);
131
+ for (i = 0; i < n; i++) {
132
+ x2 = rb_ary_entry(x, i);
133
+ Need_Float(x2);
134
+ if (NIL_P(params)) result = rb_funcall(proc, RBGSL_ID_call, 1, x2);
135
+ else result = rb_funcall(proc, RBGSL_ID_call, 2, x2, params);
136
+ rb_ary_store(arynew, i, result);
137
+ }
138
+ return arynew;
139
+ break;
140
+ default:
141
+ #ifdef HAVE_NARRAY_H
142
+ if (NA_IsNArray(x)) {
143
+ GetNArray(x, na);
144
+ ptr1 = (double *) na->ptr;
145
+ n = na->total;
146
+ ary = na_make_object(NA_DFLOAT, na->rank, na->shape, CLASS_OF(x));
147
+ ptr2 = NA_PTR_TYPE(ary, double*);
148
+ for (i = 0; i < n; i++) {
149
+ x2 = rb_float_new(ptr1[i]);
150
+ if (NIL_P(params)) result = rb_funcall(proc, RBGSL_ID_call, 1, x2);
151
+ else result = rb_funcall(proc, RBGSL_ID_call, 2, x2, params);
152
+ ptr2[i] = NUM2DBL(result);
153
+ }
154
+ return ary;
155
+ }
156
+ #endif
157
+ if (VECTOR_P(x)) {
158
+ Data_Get_Struct(x, gsl_vector, v);
159
+ vnew = gsl_vector_alloc(v->size);
160
+ for (i = 0; i < v->size; i++) {
161
+ x2 = rb_float_new(gsl_vector_get(v, i));
162
+ if (NIL_P(params)) result = rb_funcall(proc, RBGSL_ID_call, 1, x2);
163
+ else result = rb_funcall(proc, RBGSL_ID_call, 2, x2, params);
164
+ gsl_vector_set(vnew, i, NUM2DBL(result));
165
+ }
166
+ return Data_Wrap_Struct(cgsl_vector, 0, gsl_vector_free, vnew);
167
+ } else if (MATRIX_P(x)) {
168
+ Data_Get_Struct(x, gsl_matrix, m);
169
+ mnew = gsl_matrix_alloc(m->size1, m->size2);
170
+ for (i = 0; i < m->size1; i++) {
171
+ for (j = 0; j < m->size2; j++) {
172
+ x2 = rb_float_new(gsl_matrix_get(m, i, j));
173
+ if (NIL_P(params)) result = rb_funcall(proc, RBGSL_ID_call, 1, x2);
174
+ else result = rb_funcall(proc, RBGSL_ID_call, 2, x2, params);
175
+ gsl_matrix_set(mnew, i, j, NUM2DBL(result));
176
+ }
177
+ }
178
+ return Data_Wrap_Struct(cgsl_matrix, 0, gsl_matrix_free, mnew);
179
+ } else {
180
+ rb_raise(rb_eTypeError, "wrong argument type");
181
+ }
182
+ break;
183
+ }
184
+ /* never reach here */
185
+ return Qnil;
186
+ }
187
+
188
+ static VALUE rb_gsl_function_arity(VALUE obj)
189
+ {
190
+ gsl_function *F = NULL;
191
+ VALUE proc;
192
+ Data_Get_Struct(obj, gsl_function, F);
193
+ proc = rb_ary_entry((VALUE) F->params, 0);
194
+ return INT2FIX(rb_funcall(proc, RBGSL_ID_arity, 0));
195
+ }
196
+
197
+ static VALUE rb_gsl_function_proc(VALUE obj)
198
+ {
199
+ gsl_function *F = NULL;
200
+ Data_Get_Struct(obj, gsl_function, F);
201
+ return rb_ary_entry((VALUE) F->params, 0);
202
+ }
203
+
204
+ static VALUE rb_gsl_function_params(VALUE obj)
205
+ {
206
+ gsl_function *F = NULL;
207
+ Data_Get_Struct(obj, gsl_function, F);
208
+ return rb_ary_entry((VALUE) F->params, 1);
209
+ }
210
+
211
+ static VALUE rb_gsl_function_set_params(int argc, VALUE *argv, VALUE obj)
212
+ {
213
+ gsl_function *F = NULL;
214
+ VALUE ary, ary2;
215
+ size_t i;
216
+ if (argc == 0) return obj;
217
+ Data_Get_Struct(obj, gsl_function, F);
218
+ ary = (VALUE) F->params;
219
+ if (argc == 1) {
220
+ rb_ary_store(ary, 1, argv[0]);
221
+ } else {
222
+ ary2 = rb_ary_new2(argc);
223
+ for (i = 0; i < argc; i++) rb_ary_store(ary2, i, argv[i]);
224
+ rb_ary_store(ary, 1, ary2);
225
+ }
226
+ return obj;
227
+ }
228
+
229
+ static VALUE rb_gsl_function_graph(int argc, VALUE *argv, VALUE obj)
230
+ {
231
+ #ifdef HAVE_GNU_GRAPH
232
+ gsl_function *F = NULL;
233
+ gsl_vector *v = NULL;
234
+ double x, y;
235
+ char opt[256] = "", command[1024];
236
+ size_t i, n;
237
+ int flag = 0;
238
+ FILE *fp = NULL;
239
+ VALUE ary, params, proc;
240
+ switch (argc) {
241
+ case 2:
242
+ Check_Type(argv[1], T_STRING);
243
+ strcpy(opt, STR2CSTR(argv[1]));
244
+ /* no break, do next */
245
+ case 1:
246
+ if (CLASS_OF(argv[0]) == rb_cRange) argv[0] = rb_gsl_range2ary(argv[0]);
247
+ if (TYPE(argv[0]) == T_ARRAY) {
248
+ n = RARRAY(argv[0])->len;
249
+ v = gsl_vector_alloc(n);
250
+ flag = 1;
251
+ for (i = 0; i < n; i++)
252
+ gsl_vector_set(v, i, NUM2DBL(rb_ary_entry(argv[0], i)));
253
+ } else if (rb_obj_is_kind_of(argv[0], cgsl_vector)) {
254
+ Data_Get_Struct(argv[0], gsl_vector, v);
255
+ n = v->size;
256
+ flag = 0;
257
+ } else {
258
+ rb_raise(rb_eTypeError,
259
+ "wrong argument type %s (Array or GSL::Vector expected)",
260
+ rb_class2name(CLASS_OF(argv[0])));
261
+ }
262
+ break;
263
+ default:
264
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 1 or 2)", argc);
265
+ break;
266
+ }
267
+ Data_Get_Struct(obj, gsl_function, F);
268
+ ary = (VALUE) F->params;
269
+ proc = rb_ary_entry(ary, 0);
270
+ params = rb_ary_entry(ary, 1);
271
+ sprintf(command, "graph -T X -g 3 %s", opt);
272
+ fp = popen(command, "w");
273
+ if (fp == NULL)
274
+ rb_raise(rb_eIOError, "GNU graph not found.");
275
+ for (i = 0; i < n; i++) {
276
+ x = gsl_vector_get(v, i);
277
+ if (NIL_P(params)) y = NUM2DBL(rb_funcall(proc, RBGSL_ID_call, 1, rb_float_new(x)));
278
+ else y = NUM2DBL(rb_funcall(proc, RBGSL_ID_call, 2, rb_float_new(x), params));
279
+ fprintf(fp, "%e %e\n", x, y);
280
+ }
281
+ fflush(fp);
282
+ pclose(fp);
283
+ fp = NULL;
284
+ if (flag == 1) gsl_vector_free(v);
285
+ return Qtrue;
286
+ #else
287
+ rb_raise(rb_eNoMethodError, "not implemented");
288
+ return Qfalse;
289
+ #endif
290
+ }
291
+
292
+
293
+ static double rb_gsl_function_fdf_f(double x, void *p);
294
+ static void gsl_function_fdf_free(gsl_function_fdf *f);
295
+
296
+ static double rb_gsl_function_fdf_f(double x, void *p);
297
+ static double rb_gsl_function_fdf_df(double x, void *p);
298
+ static void rb_gsl_function_fdf_fdf(double x, void *p, double *f, double *df);
299
+
300
+ static void setfunc(int i, VALUE *argv, gsl_function_fdf *F);
301
+ static void setfunc(int i, VALUE *argv, gsl_function_fdf *F)
302
+ {
303
+ VALUE ary;
304
+ if (F->params == NULL) {
305
+ ary = rb_ary_new2(4);
306
+ /* (VALUE) F->params = ary;*/
307
+ F->params = (void *) ary;
308
+ } else {
309
+ ary = (VALUE) F->params;
310
+ }
311
+
312
+ if (rb_obj_is_kind_of(argv[i], rb_cProc)) {
313
+ rb_ary_store(ary, i, argv[i]);
314
+ } else if (TYPE(argv[i]) == T_ARRAY || rb_obj_is_kind_of(argv[i], cgsl_vector)
315
+ || TYPE(argv[i]) == T_FIXNUM || TYPE(argv[i]) == T_FLOAT) {
316
+ rb_ary_store(ary, 3, argv[i]);
317
+ } else {
318
+ rb_raise(rb_eArgError,
319
+ "wrong type argument (Proc, Array, GSL::Vector or a number)");
320
+ }
321
+ }
322
+
323
+ static void gsl_function_fdf_mark(gsl_function_fdf *f);
324
+ static VALUE rb_gsl_function_fdf_new(int argc, VALUE *argv, VALUE klass)
325
+ {
326
+ gsl_function_fdf *F = NULL;
327
+ VALUE ary;
328
+ size_t i;
329
+ F = ALLOC(gsl_function_fdf);
330
+ F->f = &rb_gsl_function_fdf_f;
331
+ F->df = &rb_gsl_function_fdf_df;
332
+ F->fdf = &rb_gsl_function_fdf_fdf;
333
+ ary = rb_ary_new2(4);
334
+ /* (VALUE) F->params = ary;*/
335
+ F->params = (void *) ary;
336
+ rb_ary_store(ary, 2, Qnil);
337
+ rb_ary_store(ary, 3, Qnil);
338
+ for (i = 0; i < argc; i++) setfunc(i, argv, F);
339
+ return Data_Wrap_Struct(klass, gsl_function_fdf_mark, gsl_function_fdf_free, F);
340
+ }
341
+
342
+ static void gsl_function_fdf_free(gsl_function_fdf *f)
343
+ {
344
+ free((gsl_function_fdf *) f);
345
+ }
346
+
347
+ static void gsl_function_fdf_mark(gsl_function_fdf *f)
348
+ {
349
+ rb_gc_mark((VALUE) f->params);
350
+ }
351
+
352
+ static VALUE rb_gsl_function_fdf_set(int argc, VALUE *argv, VALUE obj)
353
+ {
354
+ gsl_function_fdf *F = NULL;
355
+ VALUE ary;
356
+ size_t i;
357
+ Data_Get_Struct(obj, gsl_function_fdf, F);
358
+ ary = (VALUE) F->params;
359
+ rb_ary_store(ary, 2, Qnil);
360
+ rb_ary_store(ary, 3, Qnil);
361
+ for (i = 0; i < argc; i++) setfunc(i, argv, F);
362
+ return obj;
363
+ }
364
+
365
+ static VALUE rb_gsl_function_fdf_set_f(VALUE obj, VALUE procf)
366
+ {
367
+ gsl_function_fdf *F = NULL;
368
+ VALUE ary;
369
+ CHECK_PROC(procf);
370
+ Data_Get_Struct(obj, gsl_function_fdf, F);
371
+ if (F->params == NULL) {
372
+ ary = rb_ary_new2(4);
373
+ /* (VALUE) F->params = ary;*/
374
+ F->params = (void *) ary;
375
+ } else {
376
+ ary = (VALUE) F->params;
377
+ }
378
+ rb_ary_store(ary, 0, procf);
379
+ return obj;
380
+ }
381
+
382
+ static VALUE rb_gsl_function_fdf_set_df(VALUE obj, VALUE procdf)
383
+ {
384
+ gsl_function_fdf *F = NULL;
385
+ VALUE ary;
386
+ CHECK_PROC(procdf);
387
+ Data_Get_Struct(obj, gsl_function_fdf, F);
388
+ if (F->params == NULL) {
389
+ ary = rb_ary_new2(4);
390
+ /* (VALUE) F->params = ary;*/
391
+ F->params = (void *) ary;
392
+ } else {
393
+ ary = (VALUE) F->params;
394
+ }
395
+ rb_ary_store(ary, 1, procdf);
396
+ return obj;
397
+ }
398
+
399
+ static VALUE rb_gsl_function_fdf_set_fdf(VALUE obj, VALUE procfdf)
400
+ {
401
+ gsl_function_fdf *F = NULL;
402
+ VALUE ary;
403
+ CHECK_PROC(procfdf);
404
+ Data_Get_Struct(obj, gsl_function_fdf, F);
405
+ if (F->params == NULL) {
406
+ ary = rb_ary_new2(4);
407
+ /* (VALUE) F->params = ary;*/
408
+ F->params = (void *) ary;
409
+ } else {
410
+ ary = (VALUE) F->params;
411
+ }
412
+ rb_ary_store(ary, 2, procfdf);
413
+ return obj;
414
+ }
415
+
416
+ static VALUE rb_gsl_function_fdf_set_params(int argc, VALUE *argv, VALUE obj)
417
+ {
418
+ gsl_function_fdf *F = NULL;
419
+ VALUE ary, ary2;
420
+ size_t i;
421
+ Data_Get_Struct(obj, gsl_function_fdf, F);
422
+ ary = (VALUE) F->params;
423
+ if (argc == 0) return obj;
424
+ if (argc == 1) {
425
+ rb_ary_store(ary, 3, argv[0]);
426
+ } else {
427
+ ary2 = rb_ary_new2(argc);
428
+ for (i = 0; i < argc; i++) rb_ary_store(ary2, i, argv[i]);
429
+ rb_ary_store(ary, 3, ary2);
430
+ }
431
+ return obj;
432
+ }
433
+
434
+ static double rb_gsl_function_fdf_f(double x, void *p)
435
+ {
436
+ VALUE result, params, proc, ary;
437
+ ary = (VALUE) p;
438
+ proc = rb_ary_entry(ary, 0);
439
+ params = rb_ary_entry(ary, 3);
440
+ if (NIL_P(params)) result = rb_funcall(proc, RBGSL_ID_call, 1, rb_float_new(x));
441
+ else result = rb_funcall(proc, RBGSL_ID_call, 2, rb_float_new(x), params);
442
+ return NUM2DBL(result);
443
+ }
444
+
445
+ static double rb_gsl_function_fdf_df(double x, void *p)
446
+ {
447
+ VALUE result, params, proc, ary;
448
+ ary = (VALUE) p;
449
+ proc = rb_ary_entry(ary, 1);
450
+ params = rb_ary_entry(ary, 3);
451
+ if (NIL_P(params)) result = rb_funcall(proc, RBGSL_ID_call, 1, rb_float_new(x));
452
+ else result = rb_funcall(proc, RBGSL_ID_call, 2, rb_float_new(x), params);
453
+ return NUM2DBL(result);
454
+ }
455
+
456
+ static void rb_gsl_function_fdf_fdf(double x, void *p, double *f, double *df)
457
+ {
458
+ VALUE result, params, proc_f, proc_df, proc_fdf, ary;
459
+ ary = (VALUE) p;
460
+ proc_f = rb_ary_entry(ary, 0);
461
+ proc_df = rb_ary_entry(ary, 1);
462
+ proc_fdf = rb_ary_entry(ary, 2);
463
+ params = rb_ary_entry(ary, 3);
464
+ if (NIL_P(proc_fdf)) {
465
+ if (NIL_P(params)) {
466
+ result = rb_funcall(proc_f, RBGSL_ID_call, 1, rb_float_new(x));
467
+ *f = NUM2DBL(result);
468
+ result = rb_funcall(proc_df, RBGSL_ID_call, 1, rb_float_new(x));
469
+ *df = NUM2DBL(result);
470
+ } else {
471
+ result = rb_funcall(proc_f, RBGSL_ID_call, 2, rb_float_new(x), params);
472
+ *f = NUM2DBL(result);
473
+ result = rb_funcall(proc_df, RBGSL_ID_call, 2, rb_float_new(x), params);
474
+ *df = NUM2DBL(result);
475
+ }
476
+ } else {
477
+ if (NIL_P(params)) result = rb_funcall(proc_fdf, RBGSL_ID_call, 1, rb_float_new(x));
478
+ else result = rb_funcall(proc_fdf, RBGSL_ID_call, 2, rb_float_new(x), params);
479
+ *f = NUM2DBL(rb_ary_entry(result, 0));
480
+ *df = NUM2DBL(rb_ary_entry(result, 1));
481
+ }
482
+ }
483
+
484
+ void Init_gsl_function(VALUE module)
485
+ {
486
+ VALUE cgsl_function_fdf2;
487
+ RBGSL_ID_call = rb_intern("call");
488
+ RBGSL_ID_arity = rb_intern("arity");
489
+
490
+ cgsl_function = rb_define_class_under(module, "Function", cGSL_Object);
491
+ cgsl_function_fdf = rb_define_class_under(module, "Function_fdf", cGSL_Object);
492
+ cgsl_function_fdf2 = rb_define_class_under(cgsl_function_fdf, "Fdf", cgsl_function_fdf);
493
+
494
+ /* rb_define_singleton_method(cgsl_function, "new", rb_gsl_function_new, -1);*/
495
+ rb_define_singleton_method(cgsl_function, "alloc", rb_gsl_function_alloc, -1);
496
+
497
+ rb_define_method(cgsl_function, "eval", rb_gsl_function_eval, 1);
498
+ rb_define_alias(cgsl_function, "call", "eval");
499
+ rb_define_alias(cgsl_function, "[]", "eval");
500
+ rb_define_alias(cgsl_function, "at", "eval");
501
+ rb_define_method(cgsl_function, "arity", rb_gsl_function_arity, 0);
502
+ rb_define_method(cgsl_function, "proc", rb_gsl_function_proc, 0);
503
+ rb_define_alias(cgsl_function, "f", "proc");
504
+ rb_define_method(cgsl_function, "params", rb_gsl_function_params, 0);
505
+ rb_define_alias(cgsl_function, "param", "params");
506
+ rb_define_method(cgsl_function, "set", rb_gsl_function_set_f, -1);
507
+ rb_define_method(cgsl_function, "set_params", rb_gsl_function_set_params, -1);
508
+ rb_define_alias(cgsl_function, "set_param", "set_params");
509
+ rb_define_alias(cgsl_function, "params=", "set_params");
510
+ rb_define_alias(cgsl_function, "param=", "set_params");
511
+
512
+ rb_define_method(cgsl_function, "graph", rb_gsl_function_graph, -1);
513
+ /*****/
514
+ rb_define_singleton_method(cgsl_function_fdf, "new", rb_gsl_function_fdf_new, -1);
515
+ rb_define_singleton_method(cgsl_function_fdf, "alloc", rb_gsl_function_fdf_new, -1);
516
+ rb_define_method(cgsl_function_fdf, "set", rb_gsl_function_fdf_set, -1);
517
+ rb_define_method(cgsl_function_fdf, "set_f", rb_gsl_function_fdf_set_f, 1);
518
+ rb_define_method(cgsl_function_fdf, "set_df", rb_gsl_function_fdf_set_df, 1);
519
+ rb_define_method(cgsl_function_fdf, "set_fdf", rb_gsl_function_fdf_set_fdf, 1);
520
+ rb_define_method(cgsl_function_fdf, "set_params", rb_gsl_function_fdf_set_params, -1);
521
+
522
+ }