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/histogram.c ADDED
@@ -0,0 +1,1717 @@
1
+ /*
2
+ histogram.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
+
12
+ #include "rb_gsl_config.h"
13
+ #include "rb_gsl_histogram.h"
14
+ #include "rb_gsl_array.h"
15
+ #include <gsl/gsl_fit.h>
16
+ #include <gsl/gsl_multifit_nlin.h>
17
+ #include <gsl/gsl_blas.h>
18
+
19
+ VALUE cgsl_histogram;
20
+ VALUE cgsl_histogram_range;
21
+ VALUE cgsl_histogram_bin;
22
+ static VALUE cgsl_histogram_integ;
23
+
24
+ static VALUE rb_gsl_histogram_alloc_from_file(VALUE klass, VALUE name);
25
+ #ifdef GSL_0_9_4_LATER
26
+ static VALUE rb_gsl_histogram_alloc(int argc, VALUE *argv, VALUE klass)
27
+ {
28
+ gsl_histogram *h = NULL;
29
+ gsl_vector *v;
30
+ double min, max;
31
+ size_t n, size;
32
+ switch (argc) {
33
+ case 1:
34
+ switch (TYPE(argv[0])) {
35
+ case T_FIXNUM:
36
+ n = FIX2INT(argv[0]);
37
+ h = gsl_histogram_alloc(n);
38
+ break;
39
+ case T_ARRAY:
40
+ v = make_cvector_from_rarray(argv[0]);
41
+ h = gsl_histogram_alloc(v->size-1);
42
+ gsl_histogram_set_ranges(h, v->data, v->size);
43
+ gsl_vector_free(v);
44
+ break;
45
+ case T_STRING:
46
+ return rb_gsl_histogram_alloc_from_file(klass, argv[0]);
47
+ break;
48
+ default:
49
+ CHECK_VECTOR(argv[0]);
50
+ Data_Get_Struct(argv[0], gsl_vector, v);
51
+ h = gsl_histogram_alloc(v->size-1);
52
+ gsl_histogram_set_ranges(h, v->data, v->size);
53
+ break;
54
+ }
55
+ return Data_Wrap_Struct(klass, 0, gsl_histogram_free, h);
56
+ break;
57
+ case 3:
58
+ CHECK_FIXNUM(argv[0]);
59
+ Need_Float(argv[1]); Need_Float(argv[2]);
60
+ n = FIX2INT(argv[0]);
61
+ min = NUM2DBL(argv[1]);
62
+ max = NUM2DBL(argv[2]);
63
+ h = gsl_histogram_calloc(n);
64
+ gsl_histogram_set_ranges_uniform(h, min, max);
65
+ return Data_Wrap_Struct(klass, 0, gsl_histogram_free, h);
66
+ break;
67
+ case 2:
68
+ switch (TYPE(argv[0])) {
69
+ case T_FIXNUM:
70
+ CHECK_FIXNUM(argv[0]);
71
+ if (TYPE(argv[1]) != T_ARRAY) {
72
+ rb_raise(rb_eTypeError, "wrong argument type %s (Array expected)",
73
+ rb_class2name(CLASS_OF(argv[1])));
74
+ }
75
+ n = FIX2INT(argv[0]);
76
+ min = NUM2DBL(rb_ary_entry(argv[1], 0));
77
+ max = NUM2DBL(rb_ary_entry(argv[1], 1));
78
+ h = gsl_histogram_calloc(n);
79
+ gsl_histogram_set_ranges_uniform(h, min, max);
80
+ return Data_Wrap_Struct(klass, 0, gsl_histogram_free, h);
81
+ break;
82
+ case T_ARRAY:
83
+ CHECK_FIXNUM(argv[1]);
84
+ v = make_cvector_from_rarray(argv[0]);
85
+ size = FIX2INT(argv[1]);
86
+ h = gsl_histogram_calloc(size-1);
87
+ gsl_histogram_set_ranges(h, v->data, size);
88
+ gsl_vector_free(v);
89
+ return Data_Wrap_Struct(klass, 0, gsl_histogram_free, h);
90
+ break;
91
+ default:
92
+ CHECK_VECTOR(argv[0]);
93
+ CHECK_FIXNUM(argv[1]);
94
+ Data_Get_Struct(argv[0], gsl_vector, v);
95
+ size = FIX2INT(argv[1]);
96
+ h = gsl_histogram_calloc(size-1);
97
+ gsl_histogram_set_ranges(h, v->data, size);
98
+ return Data_Wrap_Struct(klass, 0, gsl_histogram_free, h);
99
+ break;
100
+ }
101
+ break;
102
+ default:
103
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 1, 2, 3)", argc);
104
+ break;
105
+ }
106
+
107
+ }
108
+
109
+ static VALUE rb_gsl_histogram_alloc_from_file(VALUE klass, VALUE name)
110
+ {
111
+ char filename[1024], buf[1024];
112
+ gsl_histogram *h;
113
+ int nn;
114
+ size_t n, i;
115
+ FILE *fp = NULL;
116
+ double upper;
117
+ strcpy(filename, STR2CHARPTR(name));
118
+ sprintf(buf, "wc %s", filename);
119
+ fp = popen(buf, "r");
120
+ if (fp == NULL) rb_raise(rb_eIOError, "popen failed.");
121
+ fgets(buf, 1024, fp);
122
+ pclose(fp);
123
+ sscanf(buf, "%d", &nn);
124
+ n = (size_t) nn; /* vector length */
125
+ fp = fopen(filename, "r");
126
+ if (fp == NULL) rb_raise(rb_eIOError, "cannot open file %s.", filename);
127
+ h = gsl_histogram_alloc(n);
128
+ i = 0;
129
+ while (fgets(buf, 1024, fp)) {
130
+ sscanf(buf, "%lg %lg %lg", h->range+i, &upper, h->bin+i);
131
+ i++;
132
+ }
133
+ h->range[n] = upper;
134
+ fclose(fp);
135
+ return Data_Wrap_Struct(klass, 0, gsl_histogram_free, h);
136
+
137
+ }
138
+
139
+ /* initialization + set uniform ranges (equal spacing from min to max) */
140
+ static VALUE rb_gsl_histogram_alloc_uniform(int argc, VALUE *argv, VALUE klass)
141
+ {
142
+ gsl_histogram *h = NULL;
143
+ double min, max, tmp;
144
+ size_t n;
145
+ switch (argc) {
146
+ case 3:
147
+ CHECK_FIXNUM(argv[0]);
148
+ Need_Float(argv[1]); Need_Float(argv[2]);
149
+ n = FIX2INT(argv[0]);
150
+ min = NUM2DBL(argv[1]);
151
+ max = NUM2DBL(argv[2]);
152
+ break;
153
+ case 2:
154
+ CHECK_FIXNUM(argv[0]);
155
+ n = FIX2INT(argv[0]);
156
+ Check_Type(argv[1], T_ARRAY);
157
+ min = NUM2DBL(rb_ary_entry(argv[1], 0));
158
+ max = NUM2DBL(rb_ary_entry(argv[1], 1));
159
+ break;
160
+ default:
161
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 2 or 3)", argc);
162
+ break;
163
+ }
164
+ if (min > max) {
165
+ tmp = min;
166
+ min = max;
167
+ max = tmp;
168
+ }
169
+ h = gsl_histogram_alloc(n);
170
+ gsl_histogram_set_ranges_uniform(h, min, max);
171
+ return Data_Wrap_Struct(klass, 0, gsl_histogram_free, h);
172
+ }
173
+
174
+ /* initialization + set ranges with a given spacing from min to max */
175
+ static VALUE rb_gsl_histogram_alloc_with_min_max_step(VALUE klass, VALUE vmin,
176
+ VALUE vmax, VALUE ss)
177
+ {
178
+ gsl_histogram *h = NULL;
179
+ gsl_vector *v = NULL;
180
+ double min, max, tmp, step;
181
+ size_t i, n;
182
+ Need_Float(vmin); Need_Float(vmax); Need_Float(ss);
183
+ min = NUM2DBL(vmin);
184
+ max = NUM2DBL(vmax);
185
+ step = NUM2DBL(ss);
186
+ if (min > max) {
187
+ tmp = min;
188
+ min = max;
189
+ max = tmp;
190
+ }
191
+ n = (int) ((max - min)/step);
192
+ h = gsl_histogram_alloc(n);
193
+ v = gsl_vector_alloc(n + 1);
194
+ for (i = 0; i < n + 1; i++) gsl_vector_set(v, i, min + step*i);
195
+ gsl_histogram_set_ranges(h, v->data, v->size);
196
+ gsl_vector_free(v);
197
+ return Data_Wrap_Struct(klass, 0, gsl_histogram_free, h);
198
+ }
199
+ #endif
200
+
201
+ static VALUE rb_gsl_histogram_calloc(VALUE klass, VALUE nn)
202
+ {
203
+ gsl_histogram *h = NULL;
204
+ CHECK_FIXNUM(nn);
205
+ h = gsl_histogram_calloc(FIX2INT(nn));
206
+ return Data_Wrap_Struct(klass, 0, gsl_histogram_free, h);
207
+ }
208
+
209
+ static VALUE rb_gsl_histogram_calloc_range(int argc, VALUE *argv, VALUE klass)
210
+ {
211
+ gsl_histogram *h = NULL;
212
+ gsl_vector *v = NULL;
213
+ size_t n;
214
+ switch (argc) {
215
+ case 1:
216
+ CHECK_VECTOR(argv[0]);
217
+ Data_Get_Struct(argv[0], gsl_vector, v);
218
+ n = v->size;
219
+ break;
220
+ case 2:
221
+ CHECK_FIXNUM(argv[0]);
222
+ CHECK_VECTOR(argv[1]);
223
+ n = FIX2INT(argv[0]);
224
+ Data_Get_Struct(argv[1], gsl_vector, v);
225
+ break;
226
+ default:
227
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 1 or 2)", argc);
228
+ break;
229
+ }
230
+ h = gsl_histogram_calloc_range(n, v->data);
231
+ return Data_Wrap_Struct(klass, 0, gsl_histogram_free, h);
232
+ }
233
+
234
+ static VALUE rb_gsl_histogram_bins(VALUE obj)
235
+ {
236
+ gsl_histogram *h = NULL;
237
+ Data_Get_Struct(obj, gsl_histogram, h);
238
+ return INT2FIX(gsl_histogram_bins(h));
239
+ }
240
+
241
+ static VALUE rb_gsl_histogram_set_ranges(int argc, VALUE *argv, VALUE obj)
242
+ {
243
+ gsl_histogram *h = NULL;
244
+ gsl_vector *v = NULL;
245
+ size_t size;
246
+ Data_Get_Struct(obj, gsl_histogram, h);
247
+ if (argc != 1 && argc != 2)
248
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 1 or 2)", argc);
249
+ if (TYPE(argv[0]) == T_ARRAY) {
250
+ v = make_cvector_from_rarray(argv[0]);
251
+ if (argc == 1) size = v->size;
252
+ else size = FIX2INT(argv[1]);
253
+ gsl_histogram_set_ranges(h, v->data, size);
254
+ gsl_vector_free(v);
255
+ } else {
256
+ CHECK_VECTOR(argv[0]);
257
+ Data_Get_Struct(argv[0], gsl_vector, v);
258
+ if (argc == 1) size = v->size;
259
+ else size = FIX2INT(argv[1]);
260
+ gsl_histogram_set_ranges(h, v->data, size);
261
+ }
262
+ return obj;
263
+ }
264
+
265
+ static VALUE rb_gsl_histogram_range(VALUE obj)
266
+ {
267
+ gsl_histogram *h = NULL;
268
+ gsl_vector_view *v = NULL;
269
+ Data_Get_Struct(obj, gsl_histogram, h);
270
+ v = gsl_vector_view_alloc();
271
+ v->vector.data = h->range;
272
+ v->vector.size = h->n + 1;
273
+ v->vector.stride = 1;
274
+ return Data_Wrap_Struct(cgsl_histogram_range, 0, gsl_vector_view_free, v);
275
+ }
276
+
277
+ static VALUE rb_gsl_histogram_bin(VALUE obj)
278
+ {
279
+ gsl_histogram *h = NULL;
280
+ gsl_vector_view *v = NULL;
281
+ Data_Get_Struct(obj, gsl_histogram, h);
282
+ v = gsl_vector_view_alloc();
283
+ v->vector.data = h->bin;
284
+ v->vector.size = h->n;
285
+ v->vector.stride = 1;
286
+ return Data_Wrap_Struct(cgsl_histogram_bin, 0, gsl_vector_view_free, v);
287
+ }
288
+
289
+ static VALUE rb_gsl_histogram_set_ranges_uniform(int argc, VALUE *argv, VALUE obj)
290
+ {
291
+ gsl_histogram *h = NULL;
292
+ double xmin, xmax;
293
+ switch (argc) {
294
+ case 1:
295
+ Check_Type(argv[0], T_ARRAY);
296
+ xmin = NUM2DBL(rb_ary_entry(argv[0], 0));
297
+ xmax = NUM2DBL(rb_ary_entry(argv[0], 1));
298
+ break;
299
+ case 2:
300
+ xmin = NUM2DBL(argv[0]);
301
+ xmax = NUM2DBL(argv[1]);
302
+ break;
303
+ default:
304
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 1 or 2)", argc);
305
+ break;
306
+ }
307
+ Data_Get_Struct(obj, gsl_histogram, h);
308
+ gsl_histogram_set_ranges_uniform(h, xmin, xmax);
309
+ return obj;
310
+ }
311
+
312
+ /* singleton */
313
+ static VALUE rb_gsl_histogram_memcpy(VALUE obj, VALUE vhdest, VALUE vhsrc)
314
+ {
315
+ gsl_histogram *hdest = NULL, *hsrc = NULL;
316
+ CHECK_HISTOGRAM(vhdest);
317
+ CHECK_HISTOGRAM(vhsrc);
318
+ Data_Get_Struct(vhdest, gsl_histogram, hdest);
319
+ Data_Get_Struct(vhsrc, gsl_histogram, hsrc);
320
+ gsl_histogram_memcpy(hdest, hsrc);
321
+ return vhdest;
322
+ }
323
+
324
+ static VALUE rb_gsl_histogram_clone(VALUE obj)
325
+ {
326
+ gsl_histogram *hsrc = NULL, *hnew = NULL;
327
+ Data_Get_Struct(obj, gsl_histogram, hsrc);
328
+ hnew = gsl_histogram_clone(hsrc);
329
+ return Data_Wrap_Struct(CLASS_OF(obj), 0, gsl_histogram_free, hnew);
330
+ }
331
+
332
+ static VALUE rb_gsl_histogram_accumulate(int argc, VALUE *argv, VALUE obj)
333
+ {
334
+ gsl_histogram *h = NULL;
335
+ gsl_vector *v;
336
+ gsl_vector_int *vi;
337
+ size_t i;
338
+ double weight = 1;
339
+ #ifdef HAVE_NARRAY_H
340
+ double *ptr;
341
+ size_t size, stride;
342
+ #endif
343
+ switch (argc) {
344
+ case 2:
345
+ Need_Float(argv[1]);
346
+ weight = NUM2DBL(argv[1]);
347
+ break;
348
+ case 1:
349
+ weight = 1;
350
+ break;
351
+ default:
352
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 1 or 2)", argc);
353
+ break;
354
+ }
355
+ Data_Get_Struct(obj, gsl_histogram, h);
356
+ if (TYPE(argv[0]) == T_ARRAY) {
357
+ for (i = 0; i < RARRAY(argv[0])->len; i++)
358
+ gsl_histogram_accumulate(h, NUM2DBL(rb_ary_entry(argv[0], i)), weight);
359
+ } else if (VECTOR_P(argv[0])) {
360
+ Data_Get_Struct(argv[0], gsl_vector, v);
361
+ for (i = 0; i < v->size; i++)
362
+ gsl_histogram_accumulate(h, gsl_vector_get(v, i), weight);
363
+ } else if (VECTOR_INT_P(argv[0])) {
364
+ Data_Get_Struct(argv[0], gsl_vector_int, vi);
365
+ for (i = 0; i < vi->size; i++)
366
+ gsl_histogram_accumulate(h, (double)gsl_vector_int_get(vi, i), weight);
367
+ #ifdef HAVE_NARRAY_H
368
+ } else if (NA_IsNArray(argv[0])) {
369
+ ptr = get_vector_ptr(argv[0], &stride, &size);
370
+ for (i = 0; i < size; i++)
371
+ gsl_histogram_accumulate(h, ptr[i], weight);
372
+ #endif
373
+ } else {
374
+ gsl_histogram_accumulate(h, NUM2DBL(argv[0]), weight);
375
+ }
376
+ return argv[0];
377
+ }
378
+
379
+ static VALUE rb_gsl_histogram_accumulate2(int argc, VALUE *argv, VALUE obj)
380
+ {
381
+ gsl_histogram *h = NULL;
382
+ double weight = 1;
383
+ double x;
384
+ switch (argc) {
385
+ case 2:
386
+ Need_Float(argv[1]);
387
+ weight = NUM2DBL(argv[1]);
388
+ /* no break; */
389
+ case 1:
390
+ Need_Float(argv[0]);
391
+ x = NUM2DBL(argv[0]);
392
+ break;
393
+ default:
394
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 1 or 2)", argc);
395
+ break;
396
+ }
397
+ Data_Get_Struct(obj, gsl_histogram, h);
398
+ if (x < h->range[0]) x = h->range[0] + 4*GSL_DBL_EPSILON;
399
+ if (x > h->range[h->n]) x = h->range[h->n] - 4*GSL_DBL_EPSILON;
400
+ gsl_histogram_accumulate(h, x, weight);
401
+ return argv[0];
402
+ }
403
+
404
+ static VALUE rb_gsl_histogram_get(VALUE obj, VALUE i)
405
+ {
406
+ gsl_histogram *h = NULL;
407
+ CHECK_FIXNUM(i);
408
+ Data_Get_Struct(obj, gsl_histogram, h);
409
+ return rb_float_new(gsl_histogram_get(h, FIX2INT(i)));
410
+ }
411
+
412
+ static VALUE rb_gsl_histogram_get_range(VALUE obj, VALUE i)
413
+ {
414
+ gsl_histogram *h = NULL;
415
+ double lower, upper;
416
+ CHECK_FIXNUM(i);
417
+ Data_Get_Struct(obj, gsl_histogram, h);
418
+ gsl_histogram_get_range(h, FIX2INT(i), &lower, &upper);
419
+ return rb_ary_new3(2, rb_float_new(lower), rb_float_new(upper));
420
+ }
421
+
422
+ static VALUE rb_gsl_histogram_max(VALUE obj)
423
+ {
424
+ gsl_histogram *h = NULL;
425
+ Data_Get_Struct(obj, gsl_histogram, h);
426
+ return rb_float_new(gsl_histogram_max(h));
427
+ }
428
+
429
+ static VALUE rb_gsl_histogram_min(VALUE obj)
430
+ {
431
+ gsl_histogram *h = NULL;
432
+ Data_Get_Struct(obj, gsl_histogram, h);
433
+ return rb_float_new(gsl_histogram_min(h));
434
+ }
435
+
436
+ static VALUE rb_gsl_histogram_reset(VALUE obj)
437
+ {
438
+ gsl_histogram *h = NULL;
439
+ Data_Get_Struct(obj, gsl_histogram, h);
440
+ gsl_histogram_reset(h);
441
+ return obj;
442
+ }
443
+
444
+ static VALUE rb_gsl_histogram_find(VALUE obj, VALUE x)
445
+ {
446
+ gsl_histogram *h = NULL;
447
+ size_t i;
448
+ Need_Float(x);
449
+ Data_Get_Struct(obj, gsl_histogram, h);
450
+ gsl_histogram_find(h, NUM2DBL(x), &i);
451
+ return INT2FIX(i);
452
+ }
453
+
454
+ static VALUE rb_gsl_histogram_max_val(VALUE obj)
455
+ {
456
+ gsl_histogram *h = NULL;
457
+ Data_Get_Struct(obj, gsl_histogram, h);
458
+ return rb_float_new(gsl_histogram_max_val(h));
459
+ }
460
+
461
+ static VALUE rb_gsl_histogram_max_bin(VALUE obj)
462
+ {
463
+ gsl_histogram *h = NULL;
464
+ Data_Get_Struct(obj, gsl_histogram, h);
465
+ return INT2FIX(gsl_histogram_max_bin(h));
466
+ }
467
+
468
+ static VALUE rb_gsl_histogram_min_val(VALUE obj)
469
+ {
470
+ gsl_histogram *h = NULL;
471
+ Data_Get_Struct(obj, gsl_histogram, h);
472
+ return rb_float_new(gsl_histogram_min_val(h));
473
+ }
474
+
475
+ static VALUE rb_gsl_histogram_min_bin(VALUE obj)
476
+ {
477
+ gsl_histogram *h = NULL;
478
+ Data_Get_Struct(obj, gsl_histogram, h);
479
+ return INT2FIX(gsl_histogram_min_bin(h));
480
+ }
481
+
482
+ static VALUE rb_gsl_histogram_mean(VALUE obj)
483
+ {
484
+ gsl_histogram *h = NULL;
485
+ Data_Get_Struct(obj, gsl_histogram, h);
486
+ return rb_float_new(gsl_histogram_mean(h));
487
+ }
488
+
489
+ static VALUE rb_gsl_histogram_sigma(VALUE obj)
490
+ {
491
+ gsl_histogram *h = NULL;
492
+ Data_Get_Struct(obj, gsl_histogram, h);
493
+ return rb_float_new(gsl_histogram_sigma(h));
494
+ }
495
+
496
+ #ifndef GSL_1_1_LATER
497
+ double gsl_histogram_sum(const gsl_histogram * h)
498
+ {
499
+ double sum=0;
500
+ size_t i=0, n;
501
+ n=h->n;
502
+ while(i < n) sum += h->bin[i++];
503
+ return sum;
504
+ }
505
+ #endif
506
+
507
+ static VALUE rb_gsl_histogram_sum(VALUE obj)
508
+ {
509
+ gsl_histogram *h = NULL;
510
+ Data_Get_Struct(obj, gsl_histogram, h);
511
+ if (CLASS_OF(obj) == cgsl_histogram_integ)
512
+ return rb_float_new(gsl_histogram_get(h, h->n-1));
513
+ else
514
+ return rb_float_new(gsl_histogram_sum(h));
515
+ }
516
+
517
+ static VALUE rb_gsl_histogram_normalize_bang(VALUE obj)
518
+ {
519
+ gsl_histogram *h = NULL;
520
+ double scale;
521
+ Data_Get_Struct(obj, gsl_histogram, h);
522
+ if (CLASS_OF(obj) == cgsl_histogram_integ)
523
+ scale = 1.0/gsl_histogram_get(h, h->n-1);
524
+ else
525
+ scale = 1.0/gsl_histogram_sum(h);
526
+ gsl_histogram_scale(h, scale);
527
+ return obj;
528
+ }
529
+
530
+ static VALUE rb_gsl_histogram_normalize(VALUE obj)
531
+ {
532
+ gsl_histogram *h = NULL, *hnew = NULL;
533
+ Data_Get_Struct(obj, gsl_histogram, h);
534
+ hnew = gsl_histogram_clone(h);
535
+ return rb_gsl_histogram_normalize_bang(Data_Wrap_Struct(CLASS_OF(obj), 0, gsl_histogram_free, hnew));
536
+ }
537
+
538
+ static VALUE rb_gsl_histogram_integral(int argc, VALUE *argv, VALUE obj)
539
+ {
540
+ gsl_histogram *h = NULL;
541
+ size_t istart = 0, iend, i = 0;
542
+ double sum = 0.0;
543
+ Data_Get_Struct(obj, gsl_histogram, h);
544
+ switch (argc) {
545
+ case 0:
546
+ return rb_gsl_histogram_sum(obj);
547
+ break;
548
+ case 1:
549
+ CHECK_FIXNUM(argv[0]);
550
+ istart = 0; iend = FIX2INT(argv[0]);
551
+ break;
552
+ case 2:
553
+ CHECK_FIXNUM(argv[0]); CHECK_FIXNUM(argv[1]);
554
+ istart = FIX2INT(argv[0]);
555
+ iend = FIX2INT(argv[1]);
556
+ break;
557
+ default:
558
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 0-2)", argc);
559
+ break;
560
+ }
561
+ if (iend >= h->n) iend = h->n - 1;
562
+ i = istart;
563
+ while (i <= iend) sum += h->bin[i++];
564
+ return rb_float_new(sum);
565
+ }
566
+
567
+ static VALUE rb_gsl_histogram_equal_bins_p(int argc, VALUE *argv, VALUE obj)
568
+ {
569
+ gsl_histogram *h1 = NULL, *h2 = NULL;
570
+ switch (TYPE(obj)) {
571
+ case T_MODULE:
572
+ case T_CLASS:
573
+ case T_OBJECT:
574
+ if (argc != 2) rb_raise(rb_eArgError, "wrong number of arguments (%d for 2)",
575
+ argc);
576
+ CHECK_HISTOGRAM(argv[0]);
577
+ CHECK_HISTOGRAM(argv[1]);
578
+ Data_Get_Struct(argv[0], gsl_histogram, h1);
579
+ Data_Get_Struct(argv[1], gsl_histogram, h2);
580
+ break;
581
+ default:
582
+ if (argc != 1) rb_raise(rb_eArgError, "wrong number of arguments (%d for 2)",
583
+ argc);
584
+ Data_Get_Struct(obj, gsl_histogram, h1);
585
+ CHECK_HISTOGRAM(argv[0]);
586
+ Data_Get_Struct(argv[0], gsl_histogram, h2);
587
+ break;
588
+ }
589
+ return INT2FIX(gsl_histogram_equal_bins_p(h1, h2));
590
+ }
591
+
592
+ static VALUE rb_gsl_histogram_equal_bins_p2(int argc, VALUE *argv, VALUE obj)
593
+ {
594
+ gsl_histogram *h1 = NULL, *h2 = NULL;
595
+ switch (TYPE(obj)) {
596
+ case T_MODULE:
597
+ case T_CLASS:
598
+ case T_OBJECT:
599
+ if (argc != 2) rb_raise(rb_eArgError, "wrong number of arguments (%d for 2)",
600
+ argc);
601
+ CHECK_HISTOGRAM(argv[0]);
602
+ CHECK_HISTOGRAM(argv[1]);
603
+ Data_Get_Struct(argv[0], gsl_histogram, h1);
604
+ Data_Get_Struct(argv[1], gsl_histogram, h2);
605
+ break;
606
+ default:
607
+ if (argc != 1) rb_raise(rb_eArgError, "wrong number of arguments (%d for 2)",
608
+ argc);
609
+ Data_Get_Struct(obj, gsl_histogram, h1);
610
+ CHECK_HISTOGRAM(argv[0]);
611
+ Data_Get_Struct(argv[0], gsl_histogram, h2);
612
+ break;
613
+ }
614
+ if (gsl_histogram_equal_bins_p(h1, h2)) return Qtrue;
615
+ else return Qfalse;
616
+ }
617
+
618
+ static VALUE rb_gsl_histogram_add(VALUE obj, VALUE hh2)
619
+ {
620
+ gsl_histogram *h1 = NULL, *h2 = NULL, *hnew = NULL;
621
+ Data_Get_Struct(obj, gsl_histogram, h1);
622
+ hnew = gsl_histogram_clone(h1);
623
+ if (HISTOGRAM_P(hh2)) {
624
+ Data_Get_Struct(hh2, gsl_histogram, h2);
625
+ mygsl_histogram_add(hnew, h2);
626
+ } else {
627
+ Need_Float(hh2);
628
+ gsl_histogram_shift(hnew, NUM2DBL(hh2));
629
+ }
630
+ return Data_Wrap_Struct(CLASS_OF(obj), 0, gsl_histogram_free, hnew);
631
+ }
632
+
633
+ static VALUE rb_gsl_histogram_add2(VALUE obj, VALUE hh2)
634
+ {
635
+ gsl_histogram *h1 = NULL, *h2 = NULL;
636
+ Data_Get_Struct(obj, gsl_histogram, h1);
637
+ if (HISTOGRAM_P(hh2)) {
638
+ Data_Get_Struct(hh2, gsl_histogram, h2);
639
+ mygsl_histogram_add(h1, h2);
640
+ } else {
641
+ Need_Float(hh2);
642
+ gsl_histogram_shift(h1, NUM2DBL(hh2));
643
+ }
644
+ return obj;
645
+ }
646
+
647
+ static VALUE rb_gsl_histogram_sub(VALUE obj, VALUE hh2)
648
+ {
649
+ gsl_histogram *h1 = NULL, *h2 = NULL, *hnew = NULL;
650
+ Data_Get_Struct(obj, gsl_histogram, h1);
651
+ hnew = gsl_histogram_clone(h1);
652
+ if (HISTOGRAM_P(hh2)) {
653
+ Data_Get_Struct(hh2, gsl_histogram, h2);
654
+ mygsl_histogram_sub(hnew, h2);
655
+ } else {
656
+ Need_Float(hh2);
657
+ gsl_histogram_shift(hnew, -NUM2DBL(hh2));
658
+ }
659
+ return Data_Wrap_Struct(CLASS_OF(obj), 0, gsl_histogram_free, hnew);
660
+ }
661
+
662
+ static VALUE rb_gsl_histogram_sub2(VALUE obj, VALUE hh2)
663
+ {
664
+ gsl_histogram *h1 = NULL, *h2 = NULL;
665
+ Data_Get_Struct(obj, gsl_histogram, h1);
666
+ if (HISTOGRAM_P(hh2)) {
667
+ Data_Get_Struct(hh2, gsl_histogram, h2);
668
+ mygsl_histogram_sub(h1, h2);
669
+ } else {
670
+ Need_Float(hh2);
671
+ gsl_histogram_shift(h1, -NUM2DBL(hh2));
672
+ }
673
+ return obj;
674
+ }
675
+
676
+ static VALUE rb_gsl_histogram_mul(VALUE obj, VALUE hh2)
677
+ {
678
+ gsl_histogram *h1 = NULL, *h2 = NULL, *hnew = NULL;
679
+ Data_Get_Struct(obj, gsl_histogram, h1);
680
+ hnew = gsl_histogram_clone(h1);
681
+ if (HISTOGRAM_P(hh2)) {
682
+ Data_Get_Struct(hh2, gsl_histogram, h2);
683
+ mygsl_histogram_mul(hnew, h2);
684
+ } else {
685
+ Need_Float(hh2);
686
+ gsl_histogram_scale(hnew, NUM2DBL(hh2));
687
+ }
688
+ return Data_Wrap_Struct(CLASS_OF(obj), 0, gsl_histogram_free, hnew);
689
+ }
690
+
691
+ static VALUE rb_gsl_histogram_mul2(VALUE obj, VALUE hh2)
692
+ {
693
+ gsl_histogram *h1 = NULL, *h2 = NULL;
694
+ Data_Get_Struct(obj, gsl_histogram, h1);
695
+ if (HISTOGRAM_P(hh2)) {
696
+ Data_Get_Struct(hh2, gsl_histogram, h2);
697
+ mygsl_histogram_mul(h1, h2);
698
+ } else {
699
+ Need_Float(hh2);
700
+ gsl_histogram_scale(h1, NUM2DBL(hh2));
701
+ }
702
+ return obj;
703
+ }
704
+
705
+ static VALUE rb_gsl_histogram_div(VALUE obj, VALUE hh2)
706
+ {
707
+ gsl_histogram *h1 = NULL, *h2 = NULL, *hnew = NULL;
708
+ Data_Get_Struct(obj, gsl_histogram, h1);
709
+ hnew = gsl_histogram_clone(h1);
710
+ if (HISTOGRAM_P(hh2)) {
711
+ Data_Get_Struct(hh2, gsl_histogram, h2);
712
+ mygsl_histogram_div(hnew, h2);
713
+ } else {
714
+ Need_Float(hh2);
715
+ gsl_histogram_scale(hnew, 1.0/NUM2DBL(hh2));
716
+ }
717
+ return Data_Wrap_Struct(CLASS_OF(obj), 0, gsl_histogram_free, hnew);
718
+ }
719
+
720
+ static VALUE rb_gsl_histogram_div2(VALUE obj, VALUE hh2)
721
+ {
722
+ gsl_histogram *h1 = NULL, *h2 = NULL;
723
+ Data_Get_Struct(obj, gsl_histogram, h1);
724
+ if (HISTOGRAM_P(hh2)) {
725
+ Data_Get_Struct(hh2, gsl_histogram, h2);
726
+ mygsl_histogram_div(h1, h2);
727
+ } else {
728
+ Need_Float(hh2);
729
+ gsl_histogram_scale(h1, 1.0/NUM2DBL(hh2));
730
+ }
731
+ return obj;
732
+ }
733
+
734
+ static VALUE rb_gsl_histogram_scale_bang(int argc, VALUE *argv, VALUE obj)
735
+ {
736
+ gsl_histogram *h = NULL;
737
+ double scale;
738
+ Data_Get_Struct(obj, gsl_histogram, h);
739
+ switch (argc) {
740
+ case 0:
741
+ if (CLASS_OF(obj) == cgsl_histogram_integ)
742
+ scale = 1.0/h->bin[h->n-1];
743
+ else
744
+ scale = 1.0/gsl_histogram_sum(h);
745
+ break;
746
+ case 1:
747
+ scale = NUM2DBL(argv[0]);
748
+ break;
749
+ default:
750
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 1 or 2)", argc);
751
+ break;
752
+ }
753
+ gsl_histogram_scale(h, scale);
754
+ return obj;
755
+ }
756
+
757
+ static VALUE rb_gsl_histogram_scale(int argc, VALUE *argv, VALUE obj)
758
+ {
759
+ gsl_histogram *h = NULL, *hnew = NULL;
760
+ double scale;
761
+ Data_Get_Struct(obj, gsl_histogram, h);
762
+ switch (argc) {
763
+ case 0:
764
+ if (CLASS_OF(obj) == cgsl_histogram_integ)
765
+ scale = 1.0/h->bin[h->n-1];
766
+ else
767
+ scale = 1.0/gsl_histogram_sum(h);
768
+ break;
769
+ case 1:
770
+ scale = NUM2DBL(argv[0]);
771
+ break;
772
+ default:
773
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 1 or 2)", argc);
774
+ break;
775
+ }
776
+ hnew = gsl_histogram_clone(h);
777
+ gsl_histogram_scale(hnew, scale);
778
+ return Data_Wrap_Struct(CLASS_OF(obj), 0, gsl_histogram_free, hnew);
779
+ }
780
+
781
+ static VALUE rb_gsl_histogram_shift(VALUE obj, VALUE shift)
782
+ {
783
+ gsl_histogram *h = NULL;
784
+ Need_Float(shift);
785
+ Data_Get_Struct(obj, gsl_histogram, h);
786
+ gsl_histogram_shift(h, NUM2DBL(shift));
787
+ return obj;
788
+ }
789
+
790
+ static VALUE rb_gsl_histogram_shift2(VALUE obj, VALUE shift)
791
+ {
792
+ gsl_histogram *h = NULL, *hnew = NULL;
793
+ Need_Float(shift);
794
+ Data_Get_Struct(obj, gsl_histogram, h);
795
+ hnew = gsl_histogram_clone(h);
796
+ gsl_histogram_shift(hnew, NUM2DBL(shift));
797
+ return Data_Wrap_Struct(CLASS_OF(obj), 0, gsl_histogram_free, hnew);
798
+ }
799
+
800
+ static VALUE rb_gsl_histogram_fwrite(VALUE obj, VALUE io)
801
+ {
802
+ gsl_histogram *h = NULL;
803
+ FILE *f;
804
+ int status, flag = 0;
805
+ Data_Get_Struct(obj, gsl_histogram, h);
806
+ f = rb_gsl_open_writefile(io, &flag);
807
+ status = gsl_histogram_fwrite(f, h);
808
+ if (flag == 1) fclose(f);
809
+ return INT2FIX(status);
810
+ }
811
+
812
+ static VALUE rb_gsl_histogram_fread(VALUE obj, VALUE io)
813
+ {
814
+ gsl_histogram *h = NULL;
815
+ FILE *f;
816
+ int status, flag = 0;
817
+ Data_Get_Struct(obj, gsl_histogram, h);
818
+ f = rb_gsl_open_readfile(io, &flag);
819
+ status = gsl_histogram_fread(f, h);
820
+ if (flag == 1) fclose(f);
821
+ return INT2FIX(status);
822
+ }
823
+
824
+ static VALUE rb_gsl_histogram_fprintf(int argc, VALUE *argv, VALUE obj)
825
+ {
826
+ gsl_histogram *h = NULL;
827
+ FILE *fp;
828
+ int status, flag = 0;
829
+
830
+ if (argc != 1 && argc != 3) {
831
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 1 or 3)", argc);
832
+ }
833
+ Data_Get_Struct(obj, gsl_histogram, h);
834
+ fp = rb_gsl_open_writefile(argv[0], &flag);
835
+ if (argc == 3) {
836
+ Check_Type(argv[1], T_STRING);
837
+ Check_Type(argv[2], T_STRING);
838
+ status = gsl_histogram_fprintf(fp, h, STR2CSTR(argv[1]), STR2CSTR(argv[2]));
839
+ } else {
840
+ status = gsl_histogram_fprintf(fp, h, "%g", "%g");
841
+ }
842
+ if (flag == 1) fclose(fp);
843
+ return INT2FIX(status);
844
+ }
845
+
846
+ static VALUE rb_gsl_histogram_printf(int argc, VALUE *argv, VALUE obj)
847
+ {
848
+ gsl_histogram *h = NULL;
849
+ int status;
850
+ Data_Get_Struct(obj, gsl_histogram, h);
851
+ if (argc == 2) {
852
+ Check_Type(argv[0], T_STRING);
853
+ Check_Type(argv[1], T_STRING);
854
+ status = gsl_histogram_fprintf(stdout, h, STR2CSTR(argv[0]), STR2CSTR(argv[1]));
855
+ } else {
856
+ status = gsl_histogram_fprintf(stdout, h, "%g", "%g");
857
+ }
858
+ return INT2FIX(status);
859
+ }
860
+
861
+ static VALUE rb_gsl_histogram_fscanf(VALUE obj, VALUE io)
862
+ {
863
+ gsl_histogram *h = NULL;
864
+ FILE *fp;
865
+ int status, flag = 0;
866
+ Data_Get_Struct(obj, gsl_histogram, h);
867
+ fp = rb_gsl_open_readfile(io, &flag);
868
+ status = gsl_histogram_fscanf(fp, h);
869
+ if (flag == 1) fclose(fp);
870
+ return INT2FIX(status);
871
+ }
872
+
873
+ static VALUE rb_gsl_histogram_print(VALUE obj)
874
+ {
875
+ gsl_histogram *h = NULL;
876
+ int status;
877
+ Data_Get_Struct(obj, gsl_histogram, h);
878
+ status = gsl_histogram_fprintf(stdout, h, "%g", "%g");
879
+ return INT2FIX(status);
880
+ }
881
+
882
+ static VALUE rb_gsl_histogram_pdf_alloc(VALUE klass, VALUE nn)
883
+ {
884
+ gsl_histogram_pdf *h = NULL;
885
+ gsl_histogram *h0 = NULL;
886
+ #ifdef GSL_0_9_4_LATER
887
+ if (rb_obj_is_kind_of(nn, cgsl_histogram)) {
888
+ Data_Get_Struct(nn, gsl_histogram, h0);
889
+ h = gsl_histogram_pdf_alloc(h0->n);
890
+ gsl_histogram_pdf_init(h, h0);
891
+ } else {
892
+ CHECK_FIXNUM(nn);
893
+ h = gsl_histogram_pdf_alloc(FIX2INT(nn));
894
+ }
895
+ #else
896
+ gsl_histogram *hh = NULL;
897
+ Data_Get_Struct(nn, gsl_histogram, hh);
898
+ h = gsl_histogram_pdf_alloc(hh);
899
+ #endif
900
+ return Data_Wrap_Struct(klass, 0, gsl_histogram_pdf_free, h);
901
+ }
902
+
903
+ #ifdef GSL_0_9_4_LATER
904
+ static VALUE rb_gsl_histogram_pdf_init(VALUE obj, VALUE hh)
905
+ {
906
+ gsl_histogram_pdf *p = NULL;
907
+ gsl_histogram *h = NULL;
908
+ CHECK_HISTOGRAM(hh);
909
+ Data_Get_Struct(obj, gsl_histogram_pdf, p);
910
+ Data_Get_Struct(hh, gsl_histogram, h);
911
+ gsl_histogram_pdf_init(p, h);
912
+ return obj;
913
+ }
914
+ #endif
915
+
916
+ static VALUE rb_gsl_histogram_pdf_sample(VALUE obj, VALUE r)
917
+ {
918
+ gsl_histogram_pdf *p = NULL;
919
+ Need_Float(r);
920
+ Data_Get_Struct(obj, gsl_histogram_pdf, p);
921
+ return rb_float_new(gsl_histogram_pdf_sample(p, NUM2DBL(r)));
922
+ }
923
+
924
+ static VALUE rb_gsl_histogram_pdf_range(VALUE obj)
925
+ {
926
+ gsl_histogram_pdf *h = NULL;
927
+ gsl_vector_view *v = NULL;
928
+ Data_Get_Struct(obj, gsl_histogram_pdf, h);
929
+ v = gsl_vector_view_alloc(h->n);
930
+ v->vector.data = h->range;
931
+ v->vector.size = h->n + 1;
932
+ v->vector.stride = 1;
933
+ return Data_Wrap_Struct(cgsl_histogram_range, 0, gsl_vector_view_free, v);
934
+ }
935
+
936
+ static VALUE rb_gsl_histogram_pdf_sum(VALUE obj)
937
+ {
938
+ gsl_histogram_pdf *h = NULL;
939
+ gsl_vector_view *v = NULL;
940
+ Data_Get_Struct(obj, gsl_histogram_pdf, h);
941
+ v = gsl_vector_view_alloc(h->n);
942
+ v->vector.data = h->sum;
943
+ v->vector.size = h->n + 1;
944
+ v->vector.stride = 1;
945
+ return Data_Wrap_Struct(cgsl_vector_view_ro, 0, gsl_vector_view_free, v);
946
+ }
947
+
948
+ static VALUE rb_gsl_histogram_graph(int argc, VALUE *argv, VALUE obj)
949
+ {
950
+ #ifdef HAVE_GNU_GRAPH
951
+ gsl_histogram *v = NULL;
952
+ FILE *fp = NULL;
953
+ size_t i;
954
+ char command[1024];
955
+ Data_Get_Struct(obj, gsl_histogram, v);
956
+ switch (argc) {
957
+ case 0:
958
+ strcpy(command, "graph -T X -g 3");
959
+ break;
960
+ case 1:
961
+ make_graphcommand(command, argv[0]);
962
+ break;
963
+ default:
964
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 0 or 1)", argc);
965
+ break;
966
+ }
967
+ fp = popen(command, "w");
968
+ if (fp == NULL) rb_raise(rb_eIOError, "GNU graph not found.");
969
+ for (i = 0; i < v->n; i++) {
970
+ fprintf(fp, "%e %e\n%e %e\n", v->range[i], v->bin[i], v->range[i+1], v->bin[i]);
971
+ }
972
+ fflush(fp);
973
+ pclose(fp);
974
+ fp = NULL;
975
+ return Qtrue;
976
+ #else
977
+ rb_raise(rb_eNoMethodError, "not implemented");
978
+ return Qfalse;
979
+ #endif
980
+ }
981
+
982
+ static VALUE rb_gsl_histogram_plot(int argc, VALUE *argv, VALUE obj)
983
+ {
984
+ #ifdef HAVE_GNU_GRAPH
985
+ gsl_histogram *v = NULL;
986
+ FILE *fp = NULL;
987
+ size_t i;
988
+ Data_Get_Struct(obj, gsl_histogram, v);
989
+ switch (argc) {
990
+ case 0:
991
+ fp = popen("gnuplot -persist", "w");
992
+ if (fp == NULL) rb_raise(rb_eIOError, "GNU graph not found.");
993
+ fprintf(fp, "plot '-' with fsteps\n");
994
+ break;
995
+ case 1:
996
+ fp = popen("gnuplot -persist", "w");
997
+ if (fp == NULL) rb_raise(rb_eIOError, "GNU graph not found.");
998
+ if (TYPE(argv[0]) == T_STRING)
999
+ fprintf(fp, "plot '-' %s\n", STR2CSTR(argv[0]));
1000
+ else
1001
+ fprintf(fp, "plot '-' with fsteps\n");
1002
+ break;
1003
+ default:
1004
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 0 or 1)", argc);
1005
+ break;
1006
+ }
1007
+ for (i = 0; i < v->n; i++) {
1008
+ fprintf(fp, "%e %e\n", v->range[i], v->bin[i]);
1009
+ }
1010
+ fprintf(fp, "e\n");
1011
+ fflush(fp);
1012
+ pclose(fp);
1013
+ fp = NULL;
1014
+ return Qtrue;
1015
+ #else
1016
+ rb_raise(rb_eNoMethodError, "not implemented");
1017
+ return Qfalse;
1018
+ #endif
1019
+ }
1020
+
1021
+ struct fit_histogram {
1022
+ gsl_histogram *h;
1023
+ size_t binstart, binend;
1024
+ };
1025
+
1026
+ static VALUE rb_gsl_histogram_fit_exponential(int argc, VALUE *argv, VALUE obj)
1027
+ {
1028
+ struct fit_histogram hh;
1029
+ gsl_histogram *h;
1030
+ gsl_vector *x, *lny, *w;
1031
+ size_t binstart = 0, binend, n, p = 2, dof, i;
1032
+ double c0, c1, cov00, cov01, cov11, sumsq, xl, xh;
1033
+ Data_Get_Struct(obj, gsl_histogram, h);
1034
+ binstart = 0;
1035
+ binend = h->n - 1;
1036
+ switch (argc) {
1037
+ case 2:
1038
+ CHECK_FIXNUM(argv[0]); CHECK_FIXNUM(argv[1]);
1039
+ binstart = FIX2INT(argv[0]);
1040
+ binend = FIX2INT(argv[1]);
1041
+ if (binend >= h->n) binend = h->n - 1;
1042
+ break;
1043
+ case 0:
1044
+ break;
1045
+ default:
1046
+ rb_raise(rb_eArgError, "too many arguments (%d for 0 or 2)", argc);
1047
+ break;
1048
+ }
1049
+ hh.h = h;
1050
+ hh.binstart = binstart;
1051
+ hh.binend = binend;
1052
+ n = binend - binstart + 1;
1053
+ dof = n - p;
1054
+
1055
+ x = gsl_vector_alloc(n);
1056
+ w = gsl_vector_alloc(n);
1057
+ lny = gsl_vector_alloc(n);
1058
+ for (i = 0; i < n; i++) {
1059
+ if (gsl_histogram_get_range(h, i+binstart, &xl, &xh))
1060
+ rb_raise(rb_eIndexError, "wrong index");
1061
+ gsl_vector_set(x, i, (xl+xh)/2.0);
1062
+ gsl_vector_set(lny, i, log(h->bin[i+binstart]));
1063
+ gsl_vector_set(w, i, h->bin[i+binstart]);
1064
+ }
1065
+ gsl_fit_wlinear(x->data, 1, w->data, 1, lny->data, 1, n,
1066
+ &c0, &c1, &cov00, &cov01, &cov11, &sumsq);
1067
+ gsl_vector_free(lny);
1068
+ gsl_vector_free(w);
1069
+ gsl_vector_free(x);
1070
+ c0 = exp(c0);
1071
+ return rb_ary_new3(6, rb_float_new(c0), rb_float_new(c1),
1072
+ rb_float_new(c0*sqrt(cov00)), rb_float_new(sqrt(cov11)),
1073
+ rb_float_new(sumsq), INT2FIX(dof));
1074
+ }
1075
+
1076
+ static VALUE rb_gsl_histogram_fit_power(int argc, VALUE *argv, VALUE obj)
1077
+ {
1078
+ struct fit_histogram hh;
1079
+ gsl_histogram *h;
1080
+ gsl_vector *lnx, *lny, *w;
1081
+ size_t binstart = 0, binend, n, p = 2, dof, i;
1082
+ double c0, c1, cov00, cov01, cov11, sumsq, xl, xh;
1083
+ Data_Get_Struct(obj, gsl_histogram, h);
1084
+ binstart = 0;
1085
+ binend = h->n - 1;
1086
+ switch (argc) {
1087
+ case 2:
1088
+ CHECK_FIXNUM(argv[0]); CHECK_FIXNUM(argv[1]);
1089
+ binstart = FIX2INT(argv[0]);
1090
+ binend = FIX2INT(argv[1]);
1091
+ if (binend >= h->n) binend = h->n - 1;
1092
+ break;
1093
+ case 0:
1094
+ break;
1095
+ default:
1096
+ rb_raise(rb_eArgError, "too many arguments (%d for 0 or 2)", argc);
1097
+ break;
1098
+ }
1099
+ hh.h = h;
1100
+ hh.binstart = binstart;
1101
+ hh.binend = binend;
1102
+ n = binend - binstart + 1;
1103
+ dof = n - p;
1104
+
1105
+ lnx = gsl_vector_alloc(n);
1106
+ w = gsl_vector_alloc(n);
1107
+ lny = gsl_vector_alloc(n);
1108
+ for (i = 0; i < n; i++) {
1109
+ if (gsl_histogram_get_range(h, i+binstart, &xl, &xh))
1110
+ rb_raise(rb_eIndexError, "wrong index");
1111
+ gsl_vector_set(lnx, i, (log(xl)+log(xh))/2.0);
1112
+ gsl_vector_set(lny, i, log(h->bin[i+binstart]));
1113
+ gsl_vector_set(w, i, h->bin[i+binstart]);
1114
+ }
1115
+ gsl_fit_wlinear(lnx->data, 1, w->data, 1, lny->data, 1, n,
1116
+ &c0, &c1, &cov00, &cov01, &cov11, &sumsq);
1117
+ gsl_vector_free(lny);
1118
+ gsl_vector_free(w);
1119
+ gsl_vector_free(lnx);
1120
+ c0 = exp(c0);
1121
+ return rb_ary_new3(6, rb_float_new(c0), rb_float_new(c1),
1122
+ rb_float_new(c0*sqrt(cov00)), rb_float_new(sqrt(cov11)),
1123
+ rb_float_new(sumsq), INT2FIX(dof));
1124
+ }
1125
+
1126
+ static int Gaussian_f(const gsl_vector *v, void *params, gsl_vector *f);
1127
+ static int Gaussian_df(const gsl_vector *v, void *params, gsl_matrix * J);
1128
+ static int Gaussian_f(const gsl_vector *v, void *params, gsl_vector *f)
1129
+ {
1130
+ struct fit_histogram *hh;
1131
+ gsl_histogram *h = NULL;
1132
+ double amp, mu, var, xl, xh, xi, yi, sqw;
1133
+ size_t i, binstart, binend;
1134
+ hh = (struct fit_histogram *) params;
1135
+ h = hh->h;
1136
+ binstart = hh->binstart;
1137
+ binend = hh->binend;
1138
+ var = gsl_vector_get(v, 0);
1139
+ mu = gsl_vector_get(v, 1);
1140
+ amp = gsl_vector_get(v, 2);
1141
+ for (i = binstart; i <= binend; i++) {
1142
+ if (gsl_histogram_get_range(h, i, &xl, &xh))
1143
+ rb_raise(rb_eIndexError, "wrong index");
1144
+ xi = (xl + xh)/2.0;
1145
+ yi = h->bin[i];
1146
+ sqw = sqrt(yi);
1147
+ gsl_vector_set(f, i-binstart, (amp*exp(-(xi - mu)*(xi - mu)/var/2.0) - yi)*sqw);
1148
+ }
1149
+ return GSL_SUCCESS;
1150
+ }
1151
+
1152
+ static int Gaussian_df(const gsl_vector *v, void *params, gsl_matrix *J)
1153
+ {
1154
+ struct fit_histogram *hh;
1155
+ gsl_histogram *h = NULL;
1156
+ double amp, mu, var, xl, xh, xi, yi, y, sqw;
1157
+ size_t i, binstart, binend;
1158
+ hh = (struct fit_histogram *) params;
1159
+ h = hh->h;
1160
+ binstart = hh->binstart;
1161
+ binend = hh->binend;
1162
+ var = gsl_vector_get(v, 0);
1163
+ mu = gsl_vector_get(v, 1);
1164
+ amp = gsl_vector_get(v, 2);
1165
+ for (i = binstart; i <= binend; i++) {
1166
+ if (gsl_histogram_get_range(h, i, &xl, &xh))
1167
+ rb_raise(rb_eIndexError, "wrong index");
1168
+ xi = (xl + xh)/2.0;
1169
+ yi = h->bin[i];
1170
+ sqw = sqrt(yi);
1171
+ y = exp(-(xi - mu)*(xi - mu)/var/2.0);
1172
+ gsl_matrix_set(J, i-binstart, 0, amp*y*(xi - mu)*(xi - mu)/2/var/var*sqw);
1173
+ gsl_matrix_set(J, i-binstart, 1, amp*y*(xi - mu)/var*sqw);
1174
+ gsl_matrix_set(J, i-binstart, 2, y*sqw);
1175
+ }
1176
+ return GSL_SUCCESS;
1177
+ }
1178
+
1179
+ static int Gaussian_fdf(const gsl_vector *v, void *params, gsl_vector *f,
1180
+ gsl_matrix *J)
1181
+ {
1182
+ Gaussian_f(v, params, f);
1183
+ Gaussian_df(v, params, J);
1184
+ return GSL_SUCCESS;
1185
+ }
1186
+
1187
+ static VALUE rb_gsl_histogram_fit_gaussian(int argc, VALUE *argv, VALUE obj)
1188
+ {
1189
+ gsl_histogram *h = NULL;
1190
+ struct fit_histogram hh;
1191
+ const gsl_multifit_fdfsolver_type *T;
1192
+ gsl_multifit_fdfsolver *s;
1193
+ int status;
1194
+ size_t iter = 0, binstart, binend;
1195
+ size_t n, dof; /* # of data points */
1196
+ size_t p = 3; /* # of fitting parameters */
1197
+ gsl_multifit_function_fdf f;
1198
+ gsl_matrix *covar = NULL;
1199
+ gsl_vector *x = NULL;
1200
+ double sigma, mean, height, errs, errm, errh, chi2;
1201
+ Data_Get_Struct(obj, gsl_histogram, h);
1202
+ binstart = 0;
1203
+ binend = h->n - 1;
1204
+ switch (argc) {
1205
+ case 2:
1206
+ CHECK_FIXNUM(argv[0]); CHECK_FIXNUM(argv[1]);
1207
+ binstart = FIX2INT(argv[0]);
1208
+ binend = FIX2INT(argv[1]);
1209
+ if (binend >= h->n) binend = h->n - 1;
1210
+ break;
1211
+ case 0:
1212
+ break;
1213
+ default:
1214
+ rb_raise(rb_eArgError, "too many arguments (%d for 0 or 2)", argc);
1215
+ break;
1216
+ }
1217
+ x = gsl_vector_alloc(p);
1218
+ gsl_vector_set(x, 0, 1); /* initial values, var = 1 */
1219
+ gsl_vector_set(x, 1, 0.0); /* mu = 0 */
1220
+ gsl_vector_set(x, 2, 1); /* amp = 1 */
1221
+ hh.h = h;
1222
+ hh.binstart = binstart;
1223
+ hh.binend = binend;
1224
+ n = binend - binstart + 1;
1225
+
1226
+ covar = gsl_matrix_alloc(p, p);
1227
+
1228
+ f.f = Gaussian_f;
1229
+ f.df = Gaussian_df;
1230
+ f.fdf = Gaussian_fdf;
1231
+ f.n = n;
1232
+ f.p = p;
1233
+ f.params = &hh;
1234
+
1235
+ T = gsl_multifit_fdfsolver_lmsder;
1236
+ s = gsl_multifit_fdfsolver_alloc(T, n, p);
1237
+ gsl_multifit_fdfsolver_set(s, &f, x);
1238
+
1239
+ do {
1240
+ iter++;
1241
+ status = gsl_multifit_fdfsolver_iterate(s);
1242
+ if (status) break;
1243
+ status = gsl_multifit_test_delta(s->dx, s->x, 1e-4, 1e-4);
1244
+ } while (status == GSL_CONTINUE);
1245
+ sigma = sqrt(gsl_vector_get(s->x, 0));
1246
+ mean = gsl_vector_get(s->x, 1);
1247
+ height = gsl_vector_get(s->x, 2)*sigma*sqrt(2*M_PI);
1248
+ gsl_multifit_covar(s->J, 0.0, covar);
1249
+ chi2 = gsl_pow_2(gsl_blas_dnrm2(s->f)); /* not reduced chi-square */
1250
+ dof = n - p;
1251
+ errs = sqrt(chi2/dof*gsl_matrix_get(covar, 0, 0))/sigma/2;
1252
+ errm = sqrt(chi2/dof*gsl_matrix_get(covar, 1, 1));
1253
+ errh = sqrt(chi2/dof*gsl_matrix_get(covar, 2, 2));
1254
+
1255
+ gsl_multifit_fdfsolver_free(s);
1256
+ gsl_vector_free(x);
1257
+ gsl_matrix_free(covar);
1258
+ return rb_ary_new3(8, rb_float_new(sigma), rb_float_new(mean),
1259
+ rb_float_new(height), rb_float_new(errs),
1260
+ rb_float_new(errm), rb_float_new(errh),
1261
+ rb_float_new(chi2), INT2FIX(dof));
1262
+ }
1263
+
1264
+ static VALUE rb_gsl_histogram_fit(int argc, VALUE *argv, VALUE obj)
1265
+ {
1266
+ char fittype[32];
1267
+ if (argc < 1) rb_raise(rb_eArgError, "too few arguments");
1268
+ Check_Type(argv[0], T_STRING);
1269
+ strcpy(fittype, STR2CSTR(argv[0]));
1270
+ if (str_head_grep(fittype, "exp") == 0) {
1271
+ return rb_gsl_histogram_fit_exponential(argc-1, argv+1, obj);
1272
+ } else if (str_head_grep(fittype, "power") == 0) {
1273
+ return rb_gsl_histogram_fit_power(argc-1, argv+1, obj);
1274
+ } else if (str_head_grep(fittype, "gaus") == 0) {
1275
+ return rb_gsl_histogram_fit_gaussian(argc-1, argv+1, obj);
1276
+ } else {
1277
+ rb_raise(rb_eRuntimeError,
1278
+ "unknown fitting type %s (exp, power, gaus expected)", fittype);
1279
+ }
1280
+ return Qnil;
1281
+ }
1282
+
1283
+ /* Integrate histogram: the two histograms must have the same range and bins. */
1284
+ void mygsl_histogram_integrate(const gsl_histogram *h, gsl_histogram *hi,
1285
+ size_t istart, size_t iend)
1286
+ {
1287
+ size_t i;
1288
+ if (iend >= istart) {
1289
+ if (istart < 0) istart = 0;
1290
+ if (iend >= h->n) iend = h->n-1;
1291
+ hi->bin[istart] = h->bin[istart];
1292
+ for (i = istart+1; i <= iend; i++) hi->bin[i] = hi->bin[i-1] + h->bin[i];
1293
+ } else {
1294
+ if (istart >= h->n) istart = h->n-1;
1295
+ if (iend < 0) iend = 0;
1296
+ hi->bin[istart] = h->bin[istart];
1297
+ for (i = istart-1; i >= iend; i--) {
1298
+ hi->bin[i] = hi->bin[i+1] + h->bin[i];
1299
+ if (i == 0) break;
1300
+ }
1301
+ }
1302
+ }
1303
+
1304
+ void mygsl_histogram_differentiate(const gsl_histogram *hi, gsl_histogram *h)
1305
+ {
1306
+ size_t i;
1307
+ h->bin[0] = hi->bin[0];
1308
+ for (i = 1; i < hi->n; i++) h->bin[i] = hi->bin[i] - hi->bin[i-1];
1309
+ }
1310
+
1311
+ /* Create a histogram integrating the given histogram h */
1312
+ gsl_histogram* mygsl_histogram_calloc_integrate(const gsl_histogram *h,
1313
+ size_t istart, size_t iend)
1314
+ {
1315
+ gsl_histogram *hi = NULL;
1316
+ hi = gsl_histogram_calloc_range(h->n, h->range);
1317
+ mygsl_histogram_integrate(h, hi, istart, iend);
1318
+ return hi;
1319
+ }
1320
+
1321
+ gsl_histogram* mygsl_histogram_calloc_differentiate(const gsl_histogram *hi)
1322
+ {
1323
+ gsl_histogram *h = NULL;
1324
+ h = gsl_histogram_calloc_range(hi->n, hi->range);
1325
+ mygsl_histogram_differentiate(hi, h);
1326
+ return h;
1327
+ }
1328
+
1329
+ static VALUE rb_gsl_histogram_integrate(int argc, VALUE *argv, VALUE obj)
1330
+ {
1331
+ gsl_histogram *h, *hi;
1332
+ size_t istart, iend;
1333
+ int itmp;
1334
+ Data_Get_Struct(obj, gsl_histogram, h);
1335
+ switch (argc) {
1336
+ case 2:
1337
+ istart = FIX2INT(argv[0]);
1338
+ iend = FIX2INT(argv[1]);
1339
+ break;
1340
+ case 1:
1341
+ switch (TYPE(argv[0])) {
1342
+ case T_ARRAY:
1343
+ istart = FIX2INT(rb_ary_entry(argv[0], 0));
1344
+ iend = FIX2INT(rb_ary_entry(argv[0], 1));
1345
+ break;
1346
+ case T_FIXNUM:
1347
+ itmp = FIX2INT(argv[0]);
1348
+ if (itmp == -1) {
1349
+ istart = h->n - 1;
1350
+ iend = 0;
1351
+ } else {
1352
+ istart = 0;
1353
+ iend = h->n - 1;
1354
+ }
1355
+ break;
1356
+ default:
1357
+ rb_raise(rb_eArgError, "wrong argument type %s (Arran or Fixnum expected)",
1358
+ rb_class2name(CLASS_OF(argv[0])));
1359
+ break;
1360
+ }
1361
+ break;
1362
+ case 0:
1363
+ istart = 0;
1364
+ iend = h->n - 1;
1365
+ break;
1366
+ default:
1367
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 0-2)", argc);
1368
+ break;
1369
+ }
1370
+ hi = mygsl_histogram_calloc_integrate(h, istart, iend);
1371
+ return Data_Wrap_Struct(cgsl_histogram_integ, 0, gsl_histogram_free, hi);
1372
+ }
1373
+
1374
+ static VALUE rb_gsl_histogram_differentiate(VALUE obj)
1375
+ {
1376
+ gsl_histogram *h, *hi;
1377
+ Data_Get_Struct(obj, gsl_histogram, hi);
1378
+ h = mygsl_histogram_calloc_differentiate(hi);
1379
+ return Data_Wrap_Struct(cgsl_histogram, 0, gsl_histogram_free, h);
1380
+ }
1381
+
1382
+ static gsl_histogram* mygsl_histogram_rebin(const gsl_histogram *h, size_t m)
1383
+ {
1384
+ gsl_histogram *hnew;
1385
+ double w;
1386
+ size_t n, i, j, k;
1387
+ if (m > h->n) m = h->n;
1388
+ n = (size_t) h->n/m;
1389
+ if (n*m != h->n) n += 1;
1390
+ w = (h->range[h->n] - h->range[0])/h->n;
1391
+ hnew = gsl_histogram_alloc(n);
1392
+ for (i = 0, j = 0; i <= n; i++) {
1393
+ if (i*m <= h->n) hnew->range[i] = h->range[i*m];
1394
+ else hnew->range[i] = w*m*i;
1395
+ }
1396
+ for (i = 0, j = 0; i < n; i++) {
1397
+ hnew->bin[i] = 0;
1398
+ for (k = 0; k < m && j < h->n; k++) hnew->bin[i] += h->bin[j++];
1399
+ }
1400
+ return hnew;
1401
+ }
1402
+
1403
+ static VALUE rb_gsl_histogram_rebin(int argc, VALUE *argv, VALUE obj)
1404
+ {
1405
+ gsl_histogram *h, *hnew;
1406
+ size_t m = 2;
1407
+ switch (argc) {
1408
+ case 1:
1409
+ CHECK_FIXNUM(argv[0]);
1410
+ m = (size_t) FIX2INT(argv[0]);
1411
+ break;
1412
+ case 0:
1413
+ m = 2;
1414
+ break;
1415
+ default:
1416
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 0 or 1)", argc);
1417
+ break;
1418
+ }
1419
+ Data_Get_Struct(obj, gsl_histogram, h);
1420
+ hnew = mygsl_histogram_rebin(h, m);
1421
+ return Data_Wrap_Struct(cgsl_histogram, 0, gsl_histogram_free, hnew);
1422
+ }
1423
+
1424
+ static int mygsl_histogram_fread2(FILE * stream, gsl_histogram * h)
1425
+ {
1426
+ double min, max;
1427
+ int status;
1428
+ status = gsl_block_raw_fread(stream, &min, 1, 1);
1429
+ if (status) return status;
1430
+ status = gsl_block_raw_fread(stream, &max, 1, 1);
1431
+ if (status) return status;
1432
+ gsl_histogram_set_ranges_uniform(h, min, max);
1433
+ status = gsl_block_raw_fread (stream, h->bin, h->n, 1);
1434
+ if (status) return status;
1435
+ return status;
1436
+ }
1437
+
1438
+ static int mygsl_histogram_fwrite2(FILE * stream, const gsl_histogram * h)
1439
+ {
1440
+ int status;
1441
+ status = gsl_block_raw_fwrite (stream, h->range, 1, 1);
1442
+ if (status) return status;
1443
+ status = gsl_block_raw_fwrite (stream, h->range+h->n, 1, 1);
1444
+ if (status) return status;
1445
+ status = gsl_block_raw_fwrite (stream, h->bin, h->n, 1);
1446
+ return status;
1447
+ }
1448
+
1449
+ static VALUE rb_gsl_histogram_fwrite2(VALUE obj, VALUE io)
1450
+ {
1451
+ gsl_histogram *h = NULL;
1452
+ FILE *f;
1453
+ int status, flag = 0;
1454
+ Data_Get_Struct(obj, gsl_histogram, h);
1455
+ f = rb_gsl_open_writefile(io, &flag);
1456
+ status = mygsl_histogram_fwrite2(f, h);
1457
+ if (flag == 1) fclose(f);
1458
+ return INT2FIX(status);
1459
+ }
1460
+
1461
+ static VALUE rb_gsl_histogram_fread2(VALUE obj, VALUE io)
1462
+ {
1463
+ gsl_histogram *h = NULL;
1464
+ FILE *f;
1465
+ int status, flag = 0;
1466
+ Data_Get_Struct(obj, gsl_histogram, h);
1467
+ f = rb_gsl_open_readfile(io, &flag);
1468
+ status = mygsl_histogram_fread2(f, h);
1469
+ if (flag == 1) fclose(f);
1470
+ return INT2FIX(status);
1471
+ }
1472
+
1473
+ static gsl_histogram* mygsl_histogram_calloc_reverse(const gsl_histogram *h)
1474
+ {
1475
+ gsl_histogram *hnew;
1476
+ size_t i, n;
1477
+ hnew = gsl_histogram_alloc(h->n);
1478
+ n = h->n;
1479
+ for (i = 0; i <= n; i++) hnew->range[i] = h->range[n-i];
1480
+ for (i = 0; i < n; i++) hnew->bin[i] = h->bin[n-1-i];
1481
+ return hnew;
1482
+ }
1483
+
1484
+ static VALUE rb_gsl_histogram_reverse(VALUE obj)
1485
+ {
1486
+ gsl_histogram *h, *hnew;
1487
+ Data_Get_Struct(obj, gsl_histogram, h);
1488
+ hnew = mygsl_histogram_calloc_reverse(h);
1489
+ return Data_Wrap_Struct(cgsl_histogram, 0, gsl_histogram_free, hnew);
1490
+ }
1491
+
1492
+ /* The functions below are not included in GSL */
1493
+ /*
1494
+ * Returns an x value at which the histogram integration
1495
+ * reaches the given percentile. The x value is calculated
1496
+ * by an interpolation between the ranges in which the percentile
1497
+ * is found.
1498
+ */
1499
+ static double histogram_percentile(const gsl_histogram *h, double f)
1500
+ {
1501
+ double sum = gsl_histogram_sum(h), sf;
1502
+ double val, s = 0, x;
1503
+ double ri, ri1;
1504
+ size_t i;
1505
+ sf = sum * f;
1506
+ for (i = 0; i < h->n; i++) {
1507
+ val = gsl_histogram_get(h, i);
1508
+ if ((s+val) > sf) break;
1509
+ s += val;
1510
+ }
1511
+ ri = h->range[i];
1512
+ ri1 = h->range[i+1];
1513
+ x = (sf - s)*(ri1 - ri)/val + ri;
1514
+ return x;
1515
+ }
1516
+
1517
+ static double histogram_median(const gsl_histogram *h)
1518
+ {
1519
+ return histogram_percentile(h, 0.5);
1520
+ }
1521
+
1522
+ static VALUE rb_gsl_histogram_percentile(VALUE obj, VALUE f)
1523
+ {
1524
+ gsl_histogram *h;
1525
+ Data_Get_Struct(obj, gsl_histogram, h);
1526
+ return rb_float_new(histogram_percentile(h, NUM2DBL(f)));
1527
+ }
1528
+
1529
+ static VALUE rb_gsl_histogram_median(VALUE obj)
1530
+ {
1531
+ gsl_histogram *h;
1532
+ Data_Get_Struct(obj, gsl_histogram, h);
1533
+ return rb_float_new(histogram_median(h));
1534
+ }
1535
+
1536
+ static double histogram_percentile_inv(const gsl_histogram *h, double x)
1537
+ {
1538
+ double sum = gsl_histogram_sum(h);
1539
+ double val, s = 0;
1540
+ double ri, ri1, q;
1541
+ size_t i;
1542
+
1543
+ for (i = 0; i < h->n; i++) {
1544
+ val = gsl_histogram_get(h, i);
1545
+ if (h->range[i+1] > x) break;
1546
+ s += val;
1547
+ }
1548
+ ri = h->range[i];
1549
+ ri1 = h->range[i+1];
1550
+ q = s + val/(ri1 - ri)*(x - ri);
1551
+ return q/sum;
1552
+ }
1553
+
1554
+ static VALUE rb_gsl_histogram_percentile_inv(VALUE obj, VALUE x)
1555
+ {
1556
+ gsl_histogram *h;
1557
+ Data_Get_Struct(obj, gsl_histogram, h);
1558
+ return rb_float_new(histogram_percentile_inv(h, NUM2DBL(x)));
1559
+ }
1560
+
1561
+ void Init_gsl_histogram(VALUE module)
1562
+ {
1563
+ VALUE cgsl_histogram_pdf;
1564
+
1565
+ cgsl_histogram = rb_define_class_under(module, "Histogram", cGSL_Object);
1566
+ cgsl_histogram_range = rb_define_class_under(cgsl_histogram, "Range",
1567
+ cgsl_vector_view_ro);
1568
+ cgsl_histogram_bin = rb_define_class_under(cgsl_histogram, "Bin",
1569
+ cgsl_vector_view);
1570
+ cgsl_histogram_integ = rb_define_class_under(cgsl_histogram, "Integral",
1571
+ cgsl_histogram);
1572
+
1573
+ #ifdef GSL_0_9_4_LATER
1574
+ rb_define_singleton_method(cgsl_histogram, "alloc", rb_gsl_histogram_alloc, -1);
1575
+ /* rb_define_singleton_method(cgsl_histogram, "new", rb_gsl_histogram_alloc, -1);*/
1576
+ rb_define_singleton_method(cgsl_histogram, "[]", rb_gsl_histogram_alloc, -1);
1577
+
1578
+ rb_define_singleton_method(cgsl_histogram, "alloc_uniform",
1579
+ rb_gsl_histogram_alloc_uniform, -1);
1580
+ rb_define_singleton_method(cgsl_histogram, "new_uniform",
1581
+ rb_gsl_histogram_alloc_uniform, -1);
1582
+
1583
+ rb_define_singleton_method(cgsl_histogram, "alloc_with_min_max_step",
1584
+ rb_gsl_histogram_alloc_with_min_max_step, 3);
1585
+ rb_define_singleton_method(cgsl_histogram, "new_with_min_max_step",
1586
+ rb_gsl_histogram_alloc_with_min_max_step, 3);
1587
+ #endif
1588
+
1589
+ rb_define_singleton_method(cgsl_histogram, "calloc",
1590
+ rb_gsl_histogram_calloc, 1);
1591
+ rb_define_singleton_method(cgsl_histogram, "calloc_range",
1592
+ rb_gsl_histogram_calloc_range, -1);
1593
+
1594
+ rb_define_method(cgsl_histogram, "bins", rb_gsl_histogram_bins, 0);
1595
+ rb_define_alias(cgsl_histogram, "n", "bins");
1596
+ rb_define_alias(cgsl_histogram, "size", "bins");
1597
+ rb_define_method(cgsl_histogram, "set_ranges", rb_gsl_histogram_set_ranges, -1);
1598
+ rb_define_method(cgsl_histogram, "range", rb_gsl_histogram_range, 0);
1599
+ rb_define_method(cgsl_histogram, "bin", rb_gsl_histogram_bin, 0);
1600
+ rb_define_method(cgsl_histogram, "set_ranges_uniform",
1601
+ rb_gsl_histogram_set_ranges_uniform, -1);
1602
+ rb_define_singleton_method(cgsl_histogram, "memcpy", rb_gsl_histogram_memcpy, 2);
1603
+ rb_define_method(cgsl_histogram, "clone", rb_gsl_histogram_clone, 0);
1604
+ rb_define_alias(cgsl_histogram, "duplicate", "clone");
1605
+ rb_define_method(cgsl_histogram, "increment", rb_gsl_histogram_accumulate, -1);
1606
+ rb_define_alias(cgsl_histogram, "fill", "increment");
1607
+ rb_define_alias(cgsl_histogram, "accumulate", "increment");
1608
+ rb_define_method(cgsl_histogram, "increment2", rb_gsl_histogram_accumulate2, -1);
1609
+ rb_define_alias(cgsl_histogram, "accumulate2", "increment2");
1610
+ rb_define_alias(cgsl_histogram, "fill2", "increment2");
1611
+
1612
+ rb_define_method(cgsl_histogram, "get", rb_gsl_histogram_get, 1);
1613
+ rb_define_alias(cgsl_histogram, "[]", "get");
1614
+ rb_define_method(cgsl_histogram, "get_range", rb_gsl_histogram_get_range, 1);
1615
+ rb_define_method(cgsl_histogram, "max", rb_gsl_histogram_max, 0);
1616
+ rb_define_method(cgsl_histogram, "min", rb_gsl_histogram_min, 0);
1617
+ rb_define_method(cgsl_histogram, "reset", rb_gsl_histogram_reset, 0);
1618
+ rb_define_method(cgsl_histogram, "find", rb_gsl_histogram_find, 1);
1619
+ rb_define_method(cgsl_histogram, "max_val", rb_gsl_histogram_max_val, 0);
1620
+ rb_define_method(cgsl_histogram, "max_bin", rb_gsl_histogram_max_bin, 0);
1621
+ rb_define_method(cgsl_histogram, "min_val", rb_gsl_histogram_min_val, 0);
1622
+ rb_define_method(cgsl_histogram, "min_bin", rb_gsl_histogram_min_bin, 0);
1623
+ rb_define_method(cgsl_histogram, "mean", rb_gsl_histogram_mean, 0);
1624
+ rb_define_method(cgsl_histogram, "sigma", rb_gsl_histogram_sigma, 0);
1625
+
1626
+ rb_define_method(cgsl_histogram, "sum", rb_gsl_histogram_integral, -1);
1627
+ rb_define_alias(cgsl_histogram, "integral", "sum");
1628
+
1629
+ rb_define_method(cgsl_histogram, "equal_bins_p",
1630
+ rb_gsl_histogram_equal_bins_p, -1);
1631
+ rb_define_alias(cgsl_histogram, "equal_bins", "equal_bins_p");
1632
+ rb_define_singleton_method(cgsl_histogram, "equal_bins_p",
1633
+ rb_gsl_histogram_equal_bins_p, -1);
1634
+ rb_define_singleton_method(cgsl_histogram, "equal_bins",
1635
+ rb_gsl_histogram_equal_bins_p, -1);
1636
+ rb_define_method(cgsl_histogram, "equal_bins_p?",
1637
+ rb_gsl_histogram_equal_bins_p2, -1);
1638
+ rb_define_alias(cgsl_histogram, "equal_bins?", "equal_bins_p?");
1639
+ rb_define_singleton_method(cgsl_histogram, "equal_bins_p?",
1640
+ rb_gsl_histogram_equal_bins_p2, -1);
1641
+ rb_define_singleton_method(cgsl_histogram, "equal_bins?",
1642
+ rb_gsl_histogram_equal_bins_p2, -1);
1643
+
1644
+ rb_define_method(cgsl_histogram, "add", rb_gsl_histogram_add, 1);
1645
+ rb_define_alias(cgsl_histogram, "+", "add");
1646
+ rb_define_method(cgsl_histogram, "sub", rb_gsl_histogram_sub, 1);
1647
+ rb_define_alias(cgsl_histogram, "-", "sub");
1648
+ rb_define_method(cgsl_histogram, "mul", rb_gsl_histogram_mul, 1);
1649
+ rb_define_alias(cgsl_histogram, "*", "mul");
1650
+ rb_define_method(cgsl_histogram, "div", rb_gsl_histogram_div, 1);
1651
+ rb_define_alias(cgsl_histogram, "/", "div");
1652
+
1653
+ rb_define_method(cgsl_histogram, "add!", rb_gsl_histogram_add2, 1);
1654
+ rb_define_method(cgsl_histogram, "sub!", rb_gsl_histogram_sub2, 1);
1655
+ rb_define_method(cgsl_histogram, "mul!", rb_gsl_histogram_mul2, 1);
1656
+ rb_define_method(cgsl_histogram, "div!", rb_gsl_histogram_div2, 1);
1657
+
1658
+ rb_define_method(cgsl_histogram, "scale!", rb_gsl_histogram_scale_bang, -1);
1659
+ rb_define_method(cgsl_histogram, "scale", rb_gsl_histogram_scale, -1);
1660
+ rb_define_method(cgsl_histogram, "shift!", rb_gsl_histogram_shift, 1);
1661
+ rb_define_method(cgsl_histogram, "shift", rb_gsl_histogram_shift2, 1);
1662
+
1663
+ rb_define_method(cgsl_histogram, "fwrite", rb_gsl_histogram_fwrite, 1);
1664
+ rb_define_method(cgsl_histogram, "fread", rb_gsl_histogram_fread, 1);
1665
+ rb_define_method(cgsl_histogram, "fwrite2", rb_gsl_histogram_fwrite2, 1);
1666
+ rb_define_method(cgsl_histogram, "fread2", rb_gsl_histogram_fread2, 1);
1667
+ rb_define_method(cgsl_histogram, "fprintf", rb_gsl_histogram_fprintf, -1);
1668
+ rb_define_method(cgsl_histogram, "printf", rb_gsl_histogram_printf, -1);
1669
+ rb_define_method(cgsl_histogram, "fscanf", rb_gsl_histogram_fscanf, 1);
1670
+ rb_define_method(cgsl_histogram, "print", rb_gsl_histogram_print, 0);
1671
+
1672
+ cgsl_histogram_pdf = rb_define_class_under(cgsl_histogram, "Pdf", cGSL_Object);
1673
+ rb_define_singleton_method(cgsl_histogram_pdf, "alloc",
1674
+ rb_gsl_histogram_pdf_alloc, 1);
1675
+ /* rb_define_singleton_method(cgsl_histogram_pdf, "new",
1676
+ rb_gsl_histogram_pdf_alloc, 1);*/
1677
+ #ifdef GSL_0_9_4_LATER
1678
+ rb_define_method(cgsl_histogram_pdf, "init", rb_gsl_histogram_pdf_init, 1);
1679
+ #endif
1680
+ rb_define_method(cgsl_histogram_pdf, "sample", rb_gsl_histogram_pdf_sample, 1);
1681
+
1682
+ rb_define_method(cgsl_histogram_pdf, "range", rb_gsl_histogram_pdf_range, 0);
1683
+ rb_define_method(cgsl_histogram_pdf, "sum", rb_gsl_histogram_pdf_sum, 0);
1684
+
1685
+ /*****/
1686
+ rb_define_method(cgsl_histogram, "graph", rb_gsl_histogram_graph, -1);
1687
+ rb_define_alias(cgsl_histogram, "draw", "graph");
1688
+
1689
+ rb_define_method(cgsl_histogram, "plot", rb_gsl_histogram_plot, -1);
1690
+
1691
+ rb_define_method(cgsl_histogram, "fit_gaussian",
1692
+ rb_gsl_histogram_fit_gaussian, -1);
1693
+ rb_define_method(cgsl_histogram, "fit_exponential",
1694
+ rb_gsl_histogram_fit_exponential, -1);
1695
+ rb_define_method(cgsl_histogram, "fit_power",
1696
+ rb_gsl_histogram_fit_power, -1);
1697
+ rb_define_method(cgsl_histogram, "fit",
1698
+ rb_gsl_histogram_fit, -1);
1699
+
1700
+ rb_define_method(cgsl_histogram, "integrate", rb_gsl_histogram_integrate, -1);
1701
+ rb_undef_method(cgsl_histogram_integ, "integrate");
1702
+ rb_define_method(cgsl_histogram_integ, "differentiate",
1703
+ rb_gsl_histogram_differentiate, 0);
1704
+ rb_define_alias(cgsl_histogram_integ, "diff", "differentiate");
1705
+
1706
+ rb_define_method(cgsl_histogram, "normalize", rb_gsl_histogram_normalize, 0);
1707
+ rb_define_method(cgsl_histogram, "normalize!", rb_gsl_histogram_normalize_bang, 0);
1708
+
1709
+ rb_define_method(cgsl_histogram, "rebin", rb_gsl_histogram_rebin, -1);
1710
+ rb_define_alias(cgsl_histogram, "mergebin", "rebin");
1711
+
1712
+ rb_define_method(cgsl_histogram, "reverse", rb_gsl_histogram_reverse, 0);
1713
+
1714
+ rb_define_method(cgsl_histogram, "percentile", rb_gsl_histogram_percentile, 1);
1715
+ rb_define_method(cgsl_histogram, "median", rb_gsl_histogram_median, 0);
1716
+ rb_define_method(cgsl_histogram, "percentile_inv", rb_gsl_histogram_percentile_inv, 1);
1717
+ }