gsl 1.12.108

Sign up to get free protection for your applications and to get access to all the features.
Files changed (158) hide show
  1. data/README.rdoc +29 -0
  2. data/Rakefile +54 -0
  3. data/VERSION +2 -0
  4. data/ext/MANIFEST +119 -0
  5. data/ext/alf.c +206 -0
  6. data/ext/array.c +666 -0
  7. data/ext/array_complex.c +247 -0
  8. data/ext/blas.c +29 -0
  9. data/ext/blas1.c +731 -0
  10. data/ext/blas2.c +1093 -0
  11. data/ext/blas3.c +881 -0
  12. data/ext/block.c +44 -0
  13. data/ext/block_source.c +886 -0
  14. data/ext/bspline.c +130 -0
  15. data/ext/bundle.c +3 -0
  16. data/ext/cdf.c +754 -0
  17. data/ext/cheb.c +542 -0
  18. data/ext/combination.c +283 -0
  19. data/ext/common.c +325 -0
  20. data/ext/complex.c +1004 -0
  21. data/ext/const.c +673 -0
  22. data/ext/const_additional.c +120 -0
  23. data/ext/cqp.c +283 -0
  24. data/ext/deriv.c +195 -0
  25. data/ext/dht.c +361 -0
  26. data/ext/diff.c +166 -0
  27. data/ext/dirac.c +395 -0
  28. data/ext/eigen.c +2373 -0
  29. data/ext/error.c +194 -0
  30. data/ext/extconf.rb +281 -0
  31. data/ext/fcmp.c +66 -0
  32. data/ext/fft.c +1092 -0
  33. data/ext/fit.c +205 -0
  34. data/ext/fresnel.c +312 -0
  35. data/ext/function.c +524 -0
  36. data/ext/geometry.c +139 -0
  37. data/ext/graph.c +1638 -0
  38. data/ext/gsl.c +271 -0
  39. data/ext/gsl_narray.c +653 -0
  40. data/ext/histogram.c +1995 -0
  41. data/ext/histogram2d.c +1068 -0
  42. data/ext/histogram3d.c +884 -0
  43. data/ext/histogram3d_source.c +750 -0
  44. data/ext/histogram_find.c +101 -0
  45. data/ext/histogram_oper.c +159 -0
  46. data/ext/ieee.c +98 -0
  47. data/ext/integration.c +1138 -0
  48. data/ext/interp.c +512 -0
  49. data/ext/jacobi.c +739 -0
  50. data/ext/linalg.c +4047 -0
  51. data/ext/linalg_complex.c +741 -0
  52. data/ext/math.c +725 -0
  53. data/ext/matrix.c +39 -0
  54. data/ext/matrix_complex.c +1732 -0
  55. data/ext/matrix_double.c +560 -0
  56. data/ext/matrix_int.c +256 -0
  57. data/ext/matrix_source.c +2733 -0
  58. data/ext/min.c +250 -0
  59. data/ext/monte.c +992 -0
  60. data/ext/multifit.c +1879 -0
  61. data/ext/multimin.c +808 -0
  62. data/ext/multimin_fsdf.c +156 -0
  63. data/ext/multiroots.c +955 -0
  64. data/ext/ndlinear.c +321 -0
  65. data/ext/nmf.c +167 -0
  66. data/ext/nmf_wrap.c +72 -0
  67. data/ext/ntuple.c +469 -0
  68. data/ext/odeiv.c +959 -0
  69. data/ext/ool.c +879 -0
  70. data/ext/oper_complex_source.c +253 -0
  71. data/ext/permutation.c +596 -0
  72. data/ext/poly.c +42 -0
  73. data/ext/poly2.c +265 -0
  74. data/ext/poly_source.c +1885 -0
  75. data/ext/qrng.c +171 -0
  76. data/ext/randist.c +1873 -0
  77. data/ext/rational.c +480 -0
  78. data/ext/rng.c +612 -0
  79. data/ext/root.c +408 -0
  80. data/ext/sf.c +1494 -0
  81. data/ext/sf_airy.c +200 -0
  82. data/ext/sf_bessel.c +867 -0
  83. data/ext/sf_clausen.c +28 -0
  84. data/ext/sf_coulomb.c +206 -0
  85. data/ext/sf_coupling.c +118 -0
  86. data/ext/sf_dawson.c +29 -0
  87. data/ext/sf_debye.c +157 -0
  88. data/ext/sf_dilog.c +42 -0
  89. data/ext/sf_elementary.c +44 -0
  90. data/ext/sf_ellint.c +206 -0
  91. data/ext/sf_elljac.c +29 -0
  92. data/ext/sf_erfc.c +93 -0
  93. data/ext/sf_exp.c +164 -0
  94. data/ext/sf_expint.c +211 -0
  95. data/ext/sf_fermi_dirac.c +148 -0
  96. data/ext/sf_gamma.c +344 -0
  97. data/ext/sf_gegenbauer.c +96 -0
  98. data/ext/sf_hyperg.c +197 -0
  99. data/ext/sf_laguerre.c +112 -0
  100. data/ext/sf_lambert.c +47 -0
  101. data/ext/sf_legendre.c +367 -0
  102. data/ext/sf_log.c +104 -0
  103. data/ext/sf_mathieu.c +238 -0
  104. data/ext/sf_power.c +46 -0
  105. data/ext/sf_psi.c +98 -0
  106. data/ext/sf_synchrotron.c +48 -0
  107. data/ext/sf_transport.c +76 -0
  108. data/ext/sf_trigonometric.c +207 -0
  109. data/ext/sf_zeta.c +119 -0
  110. data/ext/signal.c +310 -0
  111. data/ext/siman.c +718 -0
  112. data/ext/sort.c +208 -0
  113. data/ext/spline.c +395 -0
  114. data/ext/stats.c +799 -0
  115. data/ext/sum.c +168 -0
  116. data/ext/tamu_anova.c +56 -0
  117. data/ext/tensor.c +38 -0
  118. data/ext/tensor_source.c +1123 -0
  119. data/ext/vector.c +38 -0
  120. data/ext/vector_complex.c +2236 -0
  121. data/ext/vector_double.c +1433 -0
  122. data/ext/vector_int.c +204 -0
  123. data/ext/vector_source.c +3329 -0
  124. data/ext/wavelet.c +937 -0
  125. data/include/rb_gsl.h +151 -0
  126. data/include/rb_gsl_array.h +238 -0
  127. data/include/rb_gsl_cheb.h +21 -0
  128. data/include/rb_gsl_common.h +343 -0
  129. data/include/rb_gsl_complex.h +25 -0
  130. data/include/rb_gsl_const.h +29 -0
  131. data/include/rb_gsl_dirac.h +13 -0
  132. data/include/rb_gsl_eigen.h +17 -0
  133. data/include/rb_gsl_fft.h +62 -0
  134. data/include/rb_gsl_fit.h +25 -0
  135. data/include/rb_gsl_function.h +27 -0
  136. data/include/rb_gsl_graph.h +70 -0
  137. data/include/rb_gsl_histogram.h +63 -0
  138. data/include/rb_gsl_histogram3d.h +97 -0
  139. data/include/rb_gsl_integration.h +17 -0
  140. data/include/rb_gsl_interp.h +46 -0
  141. data/include/rb_gsl_linalg.h +25 -0
  142. data/include/rb_gsl_math.h +26 -0
  143. data/include/rb_gsl_odeiv.h +21 -0
  144. data/include/rb_gsl_poly.h +71 -0
  145. data/include/rb_gsl_rational.h +37 -0
  146. data/include/rb_gsl_rng.h +21 -0
  147. data/include/rb_gsl_root.h +22 -0
  148. data/include/rb_gsl_sf.h +119 -0
  149. data/include/rb_gsl_statistics.h +17 -0
  150. data/include/rb_gsl_tensor.h +45 -0
  151. data/include/rb_gsl_with_narray.h +22 -0
  152. data/include/templates_off.h +87 -0
  153. data/include/templates_on.h +241 -0
  154. data/lib/gsl/gnuplot.rb +41 -0
  155. data/lib/gsl/oper.rb +68 -0
  156. data/lib/ool.rb +22 -0
  157. data/lib/ool/conmin.rb +30 -0
  158. metadata +224 -0
@@ -0,0 +1,1995 @@
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
+ for (i = 0; i < RARRAY_LEN(argv[0]); i++)
359
+ gsl_histogram_accumulate(h, NUM2DBL(rb_ary_entry(argv[0], i)), weight);
360
+ } else if (VECTOR_P(argv[0])) {
361
+ Data_Get_Struct(argv[0], gsl_vector, v);
362
+ for (i = 0; i < v->size; i++)
363
+ gsl_histogram_accumulate(h, gsl_vector_get(v, i), weight);
364
+ } else if (VECTOR_INT_P(argv[0])) {
365
+ Data_Get_Struct(argv[0], gsl_vector_int, vi);
366
+ for (i = 0; i < vi->size; i++)
367
+ gsl_histogram_accumulate(h, (double)gsl_vector_int_get(vi, i), weight);
368
+ #ifdef HAVE_NARRAY_H
369
+ } else if (NA_IsNArray(argv[0])) {
370
+ ptr = get_vector_ptr(argv[0], &stride, &size);
371
+ for (i = 0; i < size; i++)
372
+ gsl_histogram_accumulate(h, ptr[i], weight);
373
+ #endif
374
+ } else {
375
+ gsl_histogram_accumulate(h, NUM2DBL(argv[0]), weight);
376
+ }
377
+ return argv[0];
378
+ }
379
+
380
+ static VALUE rb_gsl_histogram_accumulate2(int argc, VALUE *argv, VALUE obj)
381
+ {
382
+ gsl_histogram *h = NULL;
383
+ double weight = 1;
384
+ double x;
385
+ switch (argc) {
386
+ case 2:
387
+ Need_Float(argv[1]);
388
+ weight = NUM2DBL(argv[1]);
389
+ /* no break; */
390
+ case 1:
391
+ Need_Float(argv[0]);
392
+ x = NUM2DBL(argv[0]);
393
+ break;
394
+ default:
395
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 1 or 2)", argc);
396
+ break;
397
+ }
398
+ Data_Get_Struct(obj, gsl_histogram, h);
399
+ if (x < h->range[0]) x = h->range[0] + 4*GSL_DBL_EPSILON;
400
+ if (x > h->range[h->n]) x = h->range[h->n] - 4*GSL_DBL_EPSILON;
401
+ gsl_histogram_accumulate(h, x, weight);
402
+ return argv[0];
403
+ }
404
+
405
+ static VALUE rb_gsl_histogram_get(VALUE obj, VALUE i)
406
+ {
407
+ gsl_histogram *h = NULL;
408
+ CHECK_FIXNUM(i);
409
+ Data_Get_Struct(obj, gsl_histogram, h);
410
+ return rb_float_new(gsl_histogram_get(h, FIX2INT(i)));
411
+ }
412
+
413
+ static VALUE rb_gsl_histogram_get_range(VALUE obj, VALUE i)
414
+ {
415
+ gsl_histogram *h = NULL;
416
+ double lower, upper;
417
+ CHECK_FIXNUM(i);
418
+ Data_Get_Struct(obj, gsl_histogram, h);
419
+ gsl_histogram_get_range(h, FIX2INT(i), &lower, &upper);
420
+ return rb_ary_new3(2, rb_float_new(lower), rb_float_new(upper));
421
+ }
422
+
423
+ static VALUE rb_gsl_histogram_max(VALUE obj)
424
+ {
425
+ gsl_histogram *h = NULL;
426
+ Data_Get_Struct(obj, gsl_histogram, h);
427
+ return rb_float_new(gsl_histogram_max(h));
428
+ }
429
+
430
+ static VALUE rb_gsl_histogram_min(VALUE obj)
431
+ {
432
+ gsl_histogram *h = NULL;
433
+ Data_Get_Struct(obj, gsl_histogram, h);
434
+ return rb_float_new(gsl_histogram_min(h));
435
+ }
436
+
437
+ static VALUE rb_gsl_histogram_reset(VALUE obj)
438
+ {
439
+ gsl_histogram *h = NULL;
440
+ Data_Get_Struct(obj, gsl_histogram, h);
441
+ gsl_histogram_reset(h);
442
+ return obj;
443
+ }
444
+
445
+ static VALUE rb_gsl_histogram_find(VALUE obj, VALUE x)
446
+ {
447
+ gsl_histogram *h = NULL;
448
+ size_t i;
449
+ Need_Float(x);
450
+ Data_Get_Struct(obj, gsl_histogram, h);
451
+ gsl_histogram_find(h, NUM2DBL(x), &i);
452
+ return INT2FIX(i);
453
+ }
454
+
455
+ static VALUE rb_gsl_histogram_max_val(VALUE obj)
456
+ {
457
+ gsl_histogram *h = NULL;
458
+ Data_Get_Struct(obj, gsl_histogram, h);
459
+ return rb_float_new(gsl_histogram_max_val(h));
460
+ }
461
+
462
+ static VALUE rb_gsl_histogram_max_bin(VALUE obj)
463
+ {
464
+ gsl_histogram *h = NULL;
465
+ Data_Get_Struct(obj, gsl_histogram, h);
466
+ return INT2FIX(gsl_histogram_max_bin(h));
467
+ }
468
+
469
+ static VALUE rb_gsl_histogram_min_val(VALUE obj)
470
+ {
471
+ gsl_histogram *h = NULL;
472
+ Data_Get_Struct(obj, gsl_histogram, h);
473
+ return rb_float_new(gsl_histogram_min_val(h));
474
+ }
475
+
476
+ static VALUE rb_gsl_histogram_min_bin(VALUE obj)
477
+ {
478
+ gsl_histogram *h = NULL;
479
+ Data_Get_Struct(obj, gsl_histogram, h);
480
+ return INT2FIX(gsl_histogram_min_bin(h));
481
+ }
482
+
483
+ static VALUE rb_gsl_histogram_mean(VALUE obj)
484
+ {
485
+ gsl_histogram *h = NULL;
486
+ Data_Get_Struct(obj, gsl_histogram, h);
487
+ return rb_float_new(gsl_histogram_mean(h));
488
+ }
489
+
490
+ static VALUE rb_gsl_histogram_sigma(VALUE obj)
491
+ {
492
+ gsl_histogram *h = NULL;
493
+ Data_Get_Struct(obj, gsl_histogram, h);
494
+ return rb_float_new(gsl_histogram_sigma(h));
495
+ }
496
+
497
+ #ifndef GSL_1_1_LATER
498
+ double gsl_histogram_sum(const gsl_histogram * h)
499
+ {
500
+ double sum=0;
501
+ size_t i=0, n;
502
+ n=h->n;
503
+ while(i < n) sum += h->bin[i++];
504
+ return sum;
505
+ }
506
+ #endif
507
+
508
+ static VALUE rb_gsl_histogram_sum(VALUE obj)
509
+ {
510
+ gsl_histogram *h = NULL;
511
+ Data_Get_Struct(obj, gsl_histogram, h);
512
+ if (CLASS_OF(obj) == cgsl_histogram_integ)
513
+ return rb_float_new(gsl_histogram_get(h, h->n-1));
514
+ else
515
+ return rb_float_new(gsl_histogram_sum(h));
516
+ }
517
+
518
+ static VALUE rb_gsl_histogram_normalize_bang(VALUE obj)
519
+ {
520
+ gsl_histogram *h = NULL;
521
+ double scale;
522
+ Data_Get_Struct(obj, gsl_histogram, h);
523
+ if (CLASS_OF(obj) == cgsl_histogram_integ)
524
+ scale = 1.0/gsl_histogram_get(h, h->n-1);
525
+ else
526
+ scale = 1.0/gsl_histogram_sum(h);
527
+ gsl_histogram_scale(h, scale);
528
+ return obj;
529
+ }
530
+
531
+ static VALUE rb_gsl_histogram_normalize(VALUE obj)
532
+ {
533
+ gsl_histogram *h = NULL, *hnew = NULL;
534
+ Data_Get_Struct(obj, gsl_histogram, h);
535
+ hnew = gsl_histogram_clone(h);
536
+ return rb_gsl_histogram_normalize_bang(Data_Wrap_Struct(CLASS_OF(obj), 0, gsl_histogram_free, hnew));
537
+ }
538
+
539
+ static VALUE rb_gsl_histogram_integral(int argc, VALUE *argv, VALUE obj)
540
+ {
541
+ gsl_histogram *h = NULL;
542
+ size_t istart = 0, iend, i = 0;
543
+ double sum = 0.0;
544
+ Data_Get_Struct(obj, gsl_histogram, h);
545
+ switch (argc) {
546
+ case 0:
547
+ return rb_gsl_histogram_sum(obj);
548
+ break;
549
+ case 1:
550
+ CHECK_FIXNUM(argv[0]);
551
+ istart = 0; iend = FIX2INT(argv[0]);
552
+ break;
553
+ case 2:
554
+ CHECK_FIXNUM(argv[0]); CHECK_FIXNUM(argv[1]);
555
+ istart = FIX2INT(argv[0]);
556
+ iend = FIX2INT(argv[1]);
557
+ break;
558
+ default:
559
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 0-2)", argc);
560
+ break;
561
+ }
562
+ if (iend >= h->n) iend = h->n - 1;
563
+ i = istart;
564
+ while (i <= iend) sum += h->bin[i++];
565
+ return rb_float_new(sum);
566
+ }
567
+
568
+ static VALUE rb_gsl_histogram_equal_bins_p(int argc, VALUE *argv, VALUE obj)
569
+ {
570
+ gsl_histogram *h1 = NULL, *h2 = NULL;
571
+ switch (TYPE(obj)) {
572
+ case T_MODULE:
573
+ case T_CLASS:
574
+ case T_OBJECT:
575
+ if (argc != 2) rb_raise(rb_eArgError, "wrong number of arguments (%d for 2)",
576
+ argc);
577
+ CHECK_HISTOGRAM(argv[0]);
578
+ CHECK_HISTOGRAM(argv[1]);
579
+ Data_Get_Struct(argv[0], gsl_histogram, h1);
580
+ Data_Get_Struct(argv[1], gsl_histogram, h2);
581
+ break;
582
+ default:
583
+ if (argc != 1) rb_raise(rb_eArgError, "wrong number of arguments (%d for 2)",
584
+ argc);
585
+ Data_Get_Struct(obj, gsl_histogram, h1);
586
+ CHECK_HISTOGRAM(argv[0]);
587
+ Data_Get_Struct(argv[0], gsl_histogram, h2);
588
+ break;
589
+ }
590
+ return INT2FIX(gsl_histogram_equal_bins_p(h1, h2));
591
+ }
592
+
593
+ static VALUE rb_gsl_histogram_equal_bins_p2(int argc, VALUE *argv, VALUE obj)
594
+ {
595
+ gsl_histogram *h1 = NULL, *h2 = NULL;
596
+ switch (TYPE(obj)) {
597
+ case T_MODULE:
598
+ case T_CLASS:
599
+ case T_OBJECT:
600
+ if (argc != 2) rb_raise(rb_eArgError, "wrong number of arguments (%d for 2)",
601
+ argc);
602
+ CHECK_HISTOGRAM(argv[0]);
603
+ CHECK_HISTOGRAM(argv[1]);
604
+ Data_Get_Struct(argv[0], gsl_histogram, h1);
605
+ Data_Get_Struct(argv[1], gsl_histogram, h2);
606
+ break;
607
+ default:
608
+ if (argc != 1) rb_raise(rb_eArgError, "wrong number of arguments (%d for 2)",
609
+ argc);
610
+ Data_Get_Struct(obj, gsl_histogram, h1);
611
+ CHECK_HISTOGRAM(argv[0]);
612
+ Data_Get_Struct(argv[0], gsl_histogram, h2);
613
+ break;
614
+ }
615
+ if (gsl_histogram_equal_bins_p(h1, h2)) return Qtrue;
616
+ else return Qfalse;
617
+ }
618
+
619
+ static VALUE rb_gsl_histogram_add(VALUE obj, VALUE hh2)
620
+ {
621
+ gsl_histogram *h1 = NULL, *h2 = NULL, *hnew = NULL;
622
+ Data_Get_Struct(obj, gsl_histogram, h1);
623
+ hnew = gsl_histogram_clone(h1);
624
+ if (HISTOGRAM_P(hh2)) {
625
+ Data_Get_Struct(hh2, gsl_histogram, h2);
626
+ mygsl_histogram_add(hnew, h2);
627
+ } else {
628
+ Need_Float(hh2);
629
+ gsl_histogram_shift(hnew, NUM2DBL(hh2));
630
+ }
631
+ return Data_Wrap_Struct(CLASS_OF(obj), 0, gsl_histogram_free, hnew);
632
+ }
633
+
634
+ static VALUE rb_gsl_histogram_add2(VALUE obj, VALUE hh2)
635
+ {
636
+ gsl_histogram *h1 = NULL, *h2 = NULL;
637
+ Data_Get_Struct(obj, gsl_histogram, h1);
638
+ if (HISTOGRAM_P(hh2)) {
639
+ Data_Get_Struct(hh2, gsl_histogram, h2);
640
+ mygsl_histogram_add(h1, h2);
641
+ } else {
642
+ Need_Float(hh2);
643
+ gsl_histogram_shift(h1, NUM2DBL(hh2));
644
+ }
645
+ return obj;
646
+ }
647
+
648
+ static VALUE rb_gsl_histogram_sub(VALUE obj, VALUE hh2)
649
+ {
650
+ gsl_histogram *h1 = NULL, *h2 = NULL, *hnew = NULL;
651
+ Data_Get_Struct(obj, gsl_histogram, h1);
652
+ hnew = gsl_histogram_clone(h1);
653
+ if (HISTOGRAM_P(hh2)) {
654
+ Data_Get_Struct(hh2, gsl_histogram, h2);
655
+ mygsl_histogram_sub(hnew, h2);
656
+ } else {
657
+ Need_Float(hh2);
658
+ gsl_histogram_shift(hnew, -NUM2DBL(hh2));
659
+ }
660
+ return Data_Wrap_Struct(CLASS_OF(obj), 0, gsl_histogram_free, hnew);
661
+ }
662
+
663
+ static VALUE rb_gsl_histogram_sub2(VALUE obj, VALUE hh2)
664
+ {
665
+ gsl_histogram *h1 = NULL, *h2 = NULL;
666
+ Data_Get_Struct(obj, gsl_histogram, h1);
667
+ if (HISTOGRAM_P(hh2)) {
668
+ Data_Get_Struct(hh2, gsl_histogram, h2);
669
+ mygsl_histogram_sub(h1, h2);
670
+ } else {
671
+ Need_Float(hh2);
672
+ gsl_histogram_shift(h1, -NUM2DBL(hh2));
673
+ }
674
+ return obj;
675
+ }
676
+
677
+ static VALUE rb_gsl_histogram_mul(VALUE obj, VALUE hh2)
678
+ {
679
+ gsl_histogram *h1 = NULL, *h2 = NULL, *hnew = NULL;
680
+ Data_Get_Struct(obj, gsl_histogram, h1);
681
+ hnew = gsl_histogram_clone(h1);
682
+ if (HISTOGRAM_P(hh2)) {
683
+ Data_Get_Struct(hh2, gsl_histogram, h2);
684
+ mygsl_histogram_mul(hnew, h2);
685
+ } else {
686
+ Need_Float(hh2);
687
+ gsl_histogram_scale(hnew, NUM2DBL(hh2));
688
+ }
689
+ return Data_Wrap_Struct(CLASS_OF(obj), 0, gsl_histogram_free, hnew);
690
+ }
691
+
692
+ static VALUE rb_gsl_histogram_mul2(VALUE obj, VALUE hh2)
693
+ {
694
+ gsl_histogram *h1 = NULL, *h2 = NULL;
695
+ Data_Get_Struct(obj, gsl_histogram, h1);
696
+ if (HISTOGRAM_P(hh2)) {
697
+ Data_Get_Struct(hh2, gsl_histogram, h2);
698
+ mygsl_histogram_mul(h1, h2);
699
+ } else {
700
+ Need_Float(hh2);
701
+ gsl_histogram_scale(h1, NUM2DBL(hh2));
702
+ }
703
+ return obj;
704
+ }
705
+
706
+ static VALUE rb_gsl_histogram_div(VALUE obj, VALUE hh2)
707
+ {
708
+ gsl_histogram *h1 = NULL, *h2 = NULL, *hnew = NULL;
709
+ Data_Get_Struct(obj, gsl_histogram, h1);
710
+ hnew = gsl_histogram_clone(h1);
711
+ if (HISTOGRAM_P(hh2)) {
712
+ Data_Get_Struct(hh2, gsl_histogram, h2);
713
+ mygsl_histogram_div(hnew, h2);
714
+ } else {
715
+ Need_Float(hh2);
716
+ gsl_histogram_scale(hnew, 1.0/NUM2DBL(hh2));
717
+ }
718
+ return Data_Wrap_Struct(CLASS_OF(obj), 0, gsl_histogram_free, hnew);
719
+ }
720
+
721
+ static VALUE rb_gsl_histogram_div2(VALUE obj, VALUE hh2)
722
+ {
723
+ gsl_histogram *h1 = NULL, *h2 = NULL;
724
+ Data_Get_Struct(obj, gsl_histogram, h1);
725
+ if (HISTOGRAM_P(hh2)) {
726
+ Data_Get_Struct(hh2, gsl_histogram, h2);
727
+ mygsl_histogram_div(h1, h2);
728
+ } else {
729
+ Need_Float(hh2);
730
+ gsl_histogram_scale(h1, 1.0/NUM2DBL(hh2));
731
+ }
732
+ return obj;
733
+ }
734
+
735
+ static VALUE rb_gsl_histogram_scale_bang(int argc, VALUE *argv, VALUE obj)
736
+ {
737
+ gsl_histogram *h = NULL;
738
+ double scale;
739
+ Data_Get_Struct(obj, gsl_histogram, h);
740
+ switch (argc) {
741
+ case 0:
742
+ if (CLASS_OF(obj) == cgsl_histogram_integ)
743
+ scale = 1.0/h->bin[h->n-1];
744
+ else
745
+ scale = 1.0/gsl_histogram_sum(h);
746
+ break;
747
+ case 1:
748
+ scale = NUM2DBL(argv[0]);
749
+ break;
750
+ default:
751
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 1 or 2)", argc);
752
+ break;
753
+ }
754
+ gsl_histogram_scale(h, scale);
755
+ return obj;
756
+ }
757
+
758
+ static VALUE rb_gsl_histogram_scale(int argc, VALUE *argv, VALUE obj)
759
+ {
760
+ gsl_histogram *h = NULL, *hnew = NULL;
761
+ double scale;
762
+ Data_Get_Struct(obj, gsl_histogram, h);
763
+ switch (argc) {
764
+ case 0:
765
+ if (CLASS_OF(obj) == cgsl_histogram_integ)
766
+ scale = 1.0/h->bin[h->n-1];
767
+ else
768
+ scale = 1.0/gsl_histogram_sum(h);
769
+ break;
770
+ case 1:
771
+ scale = NUM2DBL(argv[0]);
772
+ break;
773
+ default:
774
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 1 or 2)", argc);
775
+ break;
776
+ }
777
+ hnew = gsl_histogram_clone(h);
778
+ gsl_histogram_scale(hnew, scale);
779
+ return Data_Wrap_Struct(CLASS_OF(obj), 0, gsl_histogram_free, hnew);
780
+ }
781
+
782
+ static VALUE rb_gsl_histogram_shift(VALUE obj, VALUE shift)
783
+ {
784
+ gsl_histogram *h = NULL;
785
+ Need_Float(shift);
786
+ Data_Get_Struct(obj, gsl_histogram, h);
787
+ gsl_histogram_shift(h, NUM2DBL(shift));
788
+ return obj;
789
+ }
790
+
791
+ static VALUE rb_gsl_histogram_shift2(VALUE obj, VALUE shift)
792
+ {
793
+ gsl_histogram *h = NULL, *hnew = NULL;
794
+ Need_Float(shift);
795
+ Data_Get_Struct(obj, gsl_histogram, h);
796
+ hnew = gsl_histogram_clone(h);
797
+ gsl_histogram_shift(hnew, NUM2DBL(shift));
798
+ return Data_Wrap_Struct(CLASS_OF(obj), 0, gsl_histogram_free, hnew);
799
+ }
800
+
801
+ static VALUE rb_gsl_histogram_fwrite(VALUE obj, VALUE io)
802
+ {
803
+ gsl_histogram *h = NULL;
804
+ FILE *f;
805
+ int status, flag = 0;
806
+ Data_Get_Struct(obj, gsl_histogram, h);
807
+ f = rb_gsl_open_writefile(io, &flag);
808
+ status = gsl_histogram_fwrite(f, h);
809
+ if (flag == 1) fclose(f);
810
+ return INT2FIX(status);
811
+ }
812
+
813
+ static VALUE rb_gsl_histogram_fread(VALUE obj, VALUE io)
814
+ {
815
+ gsl_histogram *h = NULL;
816
+ FILE *f;
817
+ int status, flag = 0;
818
+ Data_Get_Struct(obj, gsl_histogram, h);
819
+ f = rb_gsl_open_readfile(io, &flag);
820
+ status = gsl_histogram_fread(f, h);
821
+ if (flag == 1) fclose(f);
822
+ return INT2FIX(status);
823
+ }
824
+
825
+ static VALUE rb_gsl_histogram_fprintf(int argc, VALUE *argv, VALUE obj)
826
+ {
827
+ gsl_histogram *h = NULL;
828
+ FILE *fp;
829
+ int status, flag = 0;
830
+
831
+ if (argc != 1 && argc != 3) {
832
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 1 or 3)", argc);
833
+ }
834
+ Data_Get_Struct(obj, gsl_histogram, h);
835
+ fp = rb_gsl_open_writefile(argv[0], &flag);
836
+ if (argc == 3) {
837
+ Check_Type(argv[1], T_STRING);
838
+ Check_Type(argv[2], T_STRING);
839
+ status = gsl_histogram_fprintf(fp, h, STR2CSTR(argv[1]), STR2CSTR(argv[2]));
840
+ } else {
841
+ status = gsl_histogram_fprintf(fp, h, "%g", "%g");
842
+ }
843
+ if (flag == 1) fclose(fp);
844
+ return INT2FIX(status);
845
+ }
846
+
847
+ static VALUE rb_gsl_histogram_printf(int argc, VALUE *argv, VALUE obj)
848
+ {
849
+ gsl_histogram *h = NULL;
850
+ int status;
851
+ Data_Get_Struct(obj, gsl_histogram, h);
852
+ if (argc == 2) {
853
+ Check_Type(argv[0], T_STRING);
854
+ Check_Type(argv[1], T_STRING);
855
+ status = gsl_histogram_fprintf(stdout, h, STR2CSTR(argv[0]), STR2CSTR(argv[1]));
856
+ } else {
857
+ status = gsl_histogram_fprintf(stdout, h, "%g", "%g");
858
+ }
859
+ return INT2FIX(status);
860
+ }
861
+
862
+ static VALUE rb_gsl_histogram_fscanf(VALUE obj, VALUE io)
863
+ {
864
+ gsl_histogram *h = NULL;
865
+ FILE *fp;
866
+ int status, flag = 0;
867
+ Data_Get_Struct(obj, gsl_histogram, h);
868
+ fp = rb_gsl_open_readfile(io, &flag);
869
+ status = gsl_histogram_fscanf(fp, h);
870
+ if (flag == 1) fclose(fp);
871
+ return INT2FIX(status);
872
+ }
873
+
874
+ static VALUE rb_gsl_histogram_print(VALUE obj)
875
+ {
876
+ gsl_histogram *h = NULL;
877
+ int status;
878
+ Data_Get_Struct(obj, gsl_histogram, h);
879
+ status = gsl_histogram_fprintf(stdout, h, "%g", "%g");
880
+ return INT2FIX(status);
881
+ }
882
+
883
+ static VALUE rb_gsl_histogram_pdf_alloc(VALUE klass, VALUE nn)
884
+ {
885
+ gsl_histogram_pdf *h = NULL;
886
+ gsl_histogram *h0 = NULL;
887
+ #ifdef GSL_0_9_4_LATER
888
+ if (rb_obj_is_kind_of(nn, cgsl_histogram)) {
889
+ Data_Get_Struct(nn, gsl_histogram, h0);
890
+ h = gsl_histogram_pdf_alloc(h0->n);
891
+ gsl_histogram_pdf_init(h, h0);
892
+ } else {
893
+ CHECK_FIXNUM(nn);
894
+ h = gsl_histogram_pdf_alloc(FIX2INT(nn));
895
+ }
896
+ #else
897
+ gsl_histogram *hh = NULL;
898
+ Data_Get_Struct(nn, gsl_histogram, hh);
899
+ h = gsl_histogram_pdf_alloc(hh);
900
+ #endif
901
+ return Data_Wrap_Struct(klass, 0, gsl_histogram_pdf_free, h);
902
+ }
903
+
904
+ #ifdef GSL_0_9_4_LATER
905
+ static VALUE rb_gsl_histogram_pdf_init(VALUE obj, VALUE hh)
906
+ {
907
+ gsl_histogram_pdf *p = NULL;
908
+ gsl_histogram *h = NULL;
909
+ CHECK_HISTOGRAM(hh);
910
+ Data_Get_Struct(obj, gsl_histogram_pdf, p);
911
+ Data_Get_Struct(hh, gsl_histogram, h);
912
+ gsl_histogram_pdf_init(p, h);
913
+ return obj;
914
+ }
915
+ #endif
916
+
917
+ static VALUE rb_gsl_histogram_pdf_sample(VALUE obj, VALUE r)
918
+ {
919
+ gsl_histogram_pdf *p = NULL;
920
+ Need_Float(r);
921
+ Data_Get_Struct(obj, gsl_histogram_pdf, p);
922
+ return rb_float_new(gsl_histogram_pdf_sample(p, NUM2DBL(r)));
923
+ }
924
+
925
+ static VALUE rb_gsl_histogram_pdf_range(VALUE obj)
926
+ {
927
+ gsl_histogram_pdf *h = NULL;
928
+ gsl_vector_view *v = NULL;
929
+ Data_Get_Struct(obj, gsl_histogram_pdf, h);
930
+ v = gsl_vector_view_alloc(h->n);
931
+ v->vector.data = h->range;
932
+ v->vector.size = h->n + 1;
933
+ v->vector.stride = 1;
934
+ return Data_Wrap_Struct(cgsl_histogram_range, 0, gsl_vector_view_free, v);
935
+ }
936
+
937
+ static VALUE rb_gsl_histogram_pdf_sum(VALUE obj)
938
+ {
939
+ gsl_histogram_pdf *h = NULL;
940
+ gsl_vector_view *v = NULL;
941
+ Data_Get_Struct(obj, gsl_histogram_pdf, h);
942
+ v = gsl_vector_view_alloc(h->n);
943
+ v->vector.data = h->sum;
944
+ v->vector.size = h->n + 1;
945
+ v->vector.stride = 1;
946
+ return Data_Wrap_Struct(cgsl_vector_view_ro, 0, gsl_vector_view_free, v);
947
+ }
948
+
949
+ static VALUE rb_gsl_histogram_graph(int argc, VALUE *argv, VALUE obj)
950
+ {
951
+ #ifdef HAVE_GNU_GRAPH
952
+ gsl_histogram *v = NULL;
953
+ FILE *fp = NULL;
954
+ size_t i;
955
+ char command[1024];
956
+ Data_Get_Struct(obj, gsl_histogram, v);
957
+ switch (argc) {
958
+ case 0:
959
+ strcpy(command, "graph -T X -g 3");
960
+ break;
961
+ case 1:
962
+ make_graphcommand(command, argv[0]);
963
+ break;
964
+ default:
965
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 0 or 1)", argc);
966
+ break;
967
+ }
968
+ fp = popen(command, "w");
969
+ if (fp == NULL) rb_raise(rb_eIOError, "GNU graph not found.");
970
+ for (i = 0; i < v->n; i++) {
971
+ fprintf(fp, "%e %e\n%e %e\n", v->range[i], v->bin[i], v->range[i+1], v->bin[i]);
972
+ }
973
+ fflush(fp);
974
+ pclose(fp);
975
+ fp = NULL;
976
+ return Qtrue;
977
+ #else
978
+ rb_raise(rb_eNoMethodError, "not implemented");
979
+ return Qfalse;
980
+ #endif
981
+ }
982
+
983
+ static VALUE rb_gsl_histogram_plot(int argc, VALUE *argv, VALUE obj)
984
+ {
985
+ #ifdef HAVE_GNU_GRAPH
986
+ gsl_histogram *v = NULL;
987
+ FILE *fp = NULL;
988
+ size_t i;
989
+ Data_Get_Struct(obj, gsl_histogram, v);
990
+ switch (argc) {
991
+ case 0:
992
+ fp = popen("gnuplot -persist", "w");
993
+ if (fp == NULL) rb_raise(rb_eIOError, "GNU graph not found.");
994
+ fprintf(fp, "plot '-' with fsteps\n");
995
+ break;
996
+ case 1:
997
+ fp = popen("gnuplot -persist", "w");
998
+ if (fp == NULL) rb_raise(rb_eIOError, "GNU graph not found.");
999
+ if (TYPE(argv[0]) == T_STRING)
1000
+ fprintf(fp, "plot '-' %s\n", STR2CSTR(argv[0]));
1001
+ else
1002
+ fprintf(fp, "plot '-' with fsteps\n");
1003
+ break;
1004
+ default:
1005
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 0 or 1)", argc);
1006
+ break;
1007
+ }
1008
+ for (i = 0; i < v->n; i++) {
1009
+ fprintf(fp, "%e %e\n", v->range[i], v->bin[i]);
1010
+ }
1011
+ fprintf(fp, "e\n");
1012
+ fflush(fp);
1013
+ pclose(fp);
1014
+ fp = NULL;
1015
+ return Qtrue;
1016
+ #else
1017
+ rb_raise(rb_eNoMethodError, "not implemented");
1018
+ return Qfalse;
1019
+ #endif
1020
+ }
1021
+
1022
+ struct fit_histogram {
1023
+ gsl_histogram *h;
1024
+ size_t binstart, binend;
1025
+ };
1026
+
1027
+ static VALUE rb_gsl_histogram_fit_exponential(int argc, VALUE *argv, VALUE obj)
1028
+ {
1029
+ struct fit_histogram hh;
1030
+ gsl_histogram *h;
1031
+ gsl_vector *x, *lny, *w;
1032
+ size_t binstart = 0, binend, n, p = 2, dof, i;
1033
+ double c0, c1, cov00, cov01, cov11, sumsq, xl, xh;
1034
+ Data_Get_Struct(obj, gsl_histogram, h);
1035
+ binstart = 0;
1036
+ binend = h->n - 1;
1037
+ switch (argc) {
1038
+ case 2:
1039
+ CHECK_FIXNUM(argv[0]); CHECK_FIXNUM(argv[1]);
1040
+ binstart = FIX2INT(argv[0]);
1041
+ binend = FIX2INT(argv[1]);
1042
+ if (binend >= h->n) binend = h->n - 1;
1043
+ break;
1044
+ case 0:
1045
+ break;
1046
+ default:
1047
+ rb_raise(rb_eArgError, "too many arguments (%d for 0 or 2)", argc);
1048
+ break;
1049
+ }
1050
+ hh.h = h;
1051
+ hh.binstart = binstart;
1052
+ hh.binend = binend;
1053
+ n = binend - binstart + 1;
1054
+ dof = n - p;
1055
+
1056
+ x = gsl_vector_alloc(n);
1057
+ w = gsl_vector_alloc(n);
1058
+ lny = gsl_vector_alloc(n);
1059
+ for (i = 0; i < n; i++) {
1060
+ if (gsl_histogram_get_range(h, i+binstart, &xl, &xh))
1061
+ rb_raise(rb_eIndexError, "wrong index");
1062
+ gsl_vector_set(x, i, (xl+xh)/2.0);
1063
+ gsl_vector_set(lny, i, log(h->bin[i+binstart]));
1064
+ gsl_vector_set(w, i, h->bin[i+binstart]);
1065
+ }
1066
+ gsl_fit_wlinear(x->data, 1, w->data, 1, lny->data, 1, n,
1067
+ &c0, &c1, &cov00, &cov01, &cov11, &sumsq);
1068
+ gsl_vector_free(lny);
1069
+ gsl_vector_free(w);
1070
+ gsl_vector_free(x);
1071
+ c0 = exp(c0);
1072
+ return rb_ary_new3(6, rb_float_new(c0), rb_float_new(c1),
1073
+ rb_float_new(c0*sqrt(cov00)), rb_float_new(sqrt(cov11)),
1074
+ rb_float_new(sumsq), INT2FIX(dof));
1075
+ }
1076
+
1077
+ static VALUE rb_gsl_histogram_fit_power(int argc, VALUE *argv, VALUE obj)
1078
+ {
1079
+ struct fit_histogram hh;
1080
+ gsl_histogram *h;
1081
+ gsl_vector *lnx, *lny, *w;
1082
+ size_t binstart = 0, binend, n, p = 2, dof, i;
1083
+ double c0, c1, cov00, cov01, cov11, sumsq, xl, xh;
1084
+ Data_Get_Struct(obj, gsl_histogram, h);
1085
+ binstart = 0;
1086
+ binend = h->n - 1;
1087
+ switch (argc) {
1088
+ case 2:
1089
+ CHECK_FIXNUM(argv[0]); CHECK_FIXNUM(argv[1]);
1090
+ binstart = FIX2INT(argv[0]);
1091
+ binend = FIX2INT(argv[1]);
1092
+ if (binend >= h->n) binend = h->n - 1;
1093
+ break;
1094
+ case 0:
1095
+ break;
1096
+ default:
1097
+ rb_raise(rb_eArgError, "too many arguments (%d for 0 or 2)", argc);
1098
+ break;
1099
+ }
1100
+ hh.h = h;
1101
+ hh.binstart = binstart;
1102
+ hh.binend = binend;
1103
+ n = binend - binstart + 1;
1104
+ dof = n - p;
1105
+
1106
+ lnx = gsl_vector_alloc(n);
1107
+ w = gsl_vector_alloc(n);
1108
+ lny = gsl_vector_alloc(n);
1109
+ for (i = 0; i < n; i++) {
1110
+ if (gsl_histogram_get_range(h, i+binstart, &xl, &xh))
1111
+ rb_raise(rb_eIndexError, "wrong index");
1112
+ gsl_vector_set(lnx, i, (log(xl)+log(xh))/2.0);
1113
+ gsl_vector_set(lny, i, log(h->bin[i+binstart]));
1114
+ gsl_vector_set(w, i, h->bin[i+binstart]);
1115
+ }
1116
+ gsl_fit_wlinear(lnx->data, 1, w->data, 1, lny->data, 1, n,
1117
+ &c0, &c1, &cov00, &cov01, &cov11, &sumsq);
1118
+ gsl_vector_free(lny);
1119
+ gsl_vector_free(w);
1120
+ gsl_vector_free(lnx);
1121
+ c0 = exp(c0);
1122
+ return rb_ary_new3(6, rb_float_new(c0), rb_float_new(c1),
1123
+ rb_float_new(c0*sqrt(cov00)), rb_float_new(sqrt(cov11)),
1124
+ rb_float_new(sumsq), INT2FIX(dof));
1125
+ }
1126
+
1127
+ static int Gaussian_f(const gsl_vector *v, void *params, gsl_vector *f);
1128
+ static int Gaussian_df(const gsl_vector *v, void *params, gsl_matrix * J);
1129
+ static int Gaussian_f(const gsl_vector *v, void *params, gsl_vector *f)
1130
+ {
1131
+ struct fit_histogram *hh;
1132
+ gsl_histogram *h = NULL;
1133
+ double amp, mu, var, xl, xh, xi, yi, sqw;
1134
+ size_t i, binstart, binend;
1135
+ hh = (struct fit_histogram *) params;
1136
+ h = hh->h;
1137
+ binstart = hh->binstart;
1138
+ binend = hh->binend;
1139
+ var = gsl_vector_get(v, 0);
1140
+ mu = gsl_vector_get(v, 1);
1141
+ amp = gsl_vector_get(v, 2);
1142
+ for (i = binstart; i <= binend; i++) {
1143
+ if (gsl_histogram_get_range(h, i, &xl, &xh))
1144
+ rb_raise(rb_eIndexError, "wrong index");
1145
+ xi = (xl + xh)/2.0;
1146
+ yi = h->bin[i];
1147
+ sqw = sqrt(yi);
1148
+ gsl_vector_set(f, i-binstart, (amp*exp(-(xi - mu)*(xi - mu)/var/2.0) - yi)*sqw);
1149
+ }
1150
+ return GSL_SUCCESS;
1151
+ }
1152
+
1153
+ static int Gaussian_df(const gsl_vector *v, void *params, gsl_matrix *J)
1154
+ {
1155
+ struct fit_histogram *hh;
1156
+ gsl_histogram *h = NULL;
1157
+ double amp, mu, var, xl, xh, xi, yi, y, sqw;
1158
+ size_t i, binstart, binend;
1159
+ hh = (struct fit_histogram *) params;
1160
+ h = hh->h;
1161
+ binstart = hh->binstart;
1162
+ binend = hh->binend;
1163
+ var = gsl_vector_get(v, 0);
1164
+ mu = gsl_vector_get(v, 1);
1165
+ amp = gsl_vector_get(v, 2);
1166
+ for (i = binstart; i <= binend; i++) {
1167
+ if (gsl_histogram_get_range(h, i, &xl, &xh))
1168
+ rb_raise(rb_eIndexError, "wrong index");
1169
+ xi = (xl + xh)/2.0;
1170
+ yi = h->bin[i];
1171
+ sqw = sqrt(yi);
1172
+ y = exp(-(xi - mu)*(xi - mu)/var/2.0);
1173
+ gsl_matrix_set(J, i-binstart, 0, amp*y*(xi - mu)*(xi - mu)/2/var/var*sqw);
1174
+ gsl_matrix_set(J, i-binstart, 1, amp*y*(xi - mu)/var*sqw);
1175
+ gsl_matrix_set(J, i-binstart, 2, y*sqw);
1176
+ }
1177
+ return GSL_SUCCESS;
1178
+ }
1179
+
1180
+ static int Gaussian_fdf(const gsl_vector *v, void *params, gsl_vector *f,
1181
+ gsl_matrix *J)
1182
+ {
1183
+ Gaussian_f(v, params, f);
1184
+ Gaussian_df(v, params, J);
1185
+ return GSL_SUCCESS;
1186
+ }
1187
+
1188
+ static VALUE rb_gsl_histogram_fit_gaussian(int argc, VALUE *argv, VALUE obj)
1189
+ {
1190
+ gsl_histogram *h = NULL;
1191
+ struct fit_histogram hh;
1192
+ const gsl_multifit_fdfsolver_type *T;
1193
+ gsl_multifit_fdfsolver *s;
1194
+ int status;
1195
+ size_t iter = 0, binstart, binend;
1196
+ size_t n, dof; /* # of data points */
1197
+ size_t p = 3; /* # of fitting parameters */
1198
+ gsl_multifit_function_fdf f;
1199
+ gsl_matrix *covar = NULL;
1200
+ gsl_vector *x = NULL;
1201
+ double sigma, mean, height, errs, errm, errh, chi2;
1202
+ Data_Get_Struct(obj, gsl_histogram, h);
1203
+ binstart = 0;
1204
+ binend = h->n - 1;
1205
+ switch (argc) {
1206
+ case 2:
1207
+ CHECK_FIXNUM(argv[0]); CHECK_FIXNUM(argv[1]);
1208
+ binstart = FIX2INT(argv[0]);
1209
+ binend = FIX2INT(argv[1]);
1210
+ if (binend >= h->n) binend = h->n - 1;
1211
+ break;
1212
+ case 0:
1213
+ break;
1214
+ default:
1215
+ rb_raise(rb_eArgError, "too many arguments (%d for 0 or 2)", argc);
1216
+ break;
1217
+ }
1218
+ x = gsl_vector_alloc(p);
1219
+ gsl_vector_set(x, 0, gsl_pow_2(gsl_histogram_sigma(h))); /* initial values, var = 1 */
1220
+ gsl_vector_set(x, 1, gsl_histogram_mean(h)); /* mu = 0 */
1221
+ gsl_vector_set(x, 2, gsl_histogram_max_val(h)); /* amp = 1 */
1222
+ hh.h = h;
1223
+ hh.binstart = binstart;
1224
+ hh.binend = binend;
1225
+ n = binend - binstart + 1;
1226
+
1227
+ covar = gsl_matrix_alloc(p, p);
1228
+
1229
+ f.f = Gaussian_f;
1230
+ f.df = Gaussian_df;
1231
+ f.fdf = Gaussian_fdf;
1232
+ f.n = n;
1233
+ f.p = p;
1234
+ f.params = &hh;
1235
+
1236
+ T = gsl_multifit_fdfsolver_lmsder;
1237
+ s = gsl_multifit_fdfsolver_alloc(T, n, p);
1238
+ gsl_multifit_fdfsolver_set(s, &f, x);
1239
+
1240
+ do {
1241
+ iter++;
1242
+ status = gsl_multifit_fdfsolver_iterate(s);
1243
+ if (status) break;
1244
+ status = gsl_multifit_test_delta(s->dx, s->x, 1e-4, 1e-4);
1245
+ } while (status == GSL_CONTINUE);
1246
+ sigma = sqrt(gsl_vector_get(s->x, 0));
1247
+ mean = gsl_vector_get(s->x, 1);
1248
+ height = gsl_vector_get(s->x, 2)*sigma*sqrt(2*M_PI);
1249
+ gsl_multifit_covar(s->J, 0.0, covar);
1250
+ chi2 = gsl_pow_2(gsl_blas_dnrm2(s->f)); /* not reduced chi-square */
1251
+ dof = n - p;
1252
+ errs = sqrt(chi2/dof*gsl_matrix_get(covar, 0, 0))/sigma/2;
1253
+ errm = sqrt(chi2/dof*gsl_matrix_get(covar, 1, 1));
1254
+ errh = sqrt(chi2/dof*gsl_matrix_get(covar, 2, 2));
1255
+
1256
+ gsl_multifit_fdfsolver_free(s);
1257
+ gsl_vector_free(x);
1258
+ gsl_matrix_free(covar);
1259
+ return rb_ary_new3(8, rb_float_new(sigma), rb_float_new(mean),
1260
+ rb_float_new(height), rb_float_new(errs),
1261
+ rb_float_new(errm), rb_float_new(errh),
1262
+ rb_float_new(chi2), INT2FIX(dof));
1263
+ }
1264
+
1265
+ static int Rayleigh_f(const gsl_vector *v, void *params, gsl_vector *f);
1266
+ static int Rayleigh_df(const gsl_vector *v, void *params, gsl_matrix * J);
1267
+ static int Rayleigh_f(const gsl_vector *v, void *params, gsl_vector *f)
1268
+ {
1269
+ struct fit_histogram *hh;
1270
+ gsl_histogram *h = NULL;
1271
+ double amp, var, xl, xh, xi, yi, sqw;
1272
+ size_t i, binstart, binend;
1273
+ hh = (struct fit_histogram *) params;
1274
+ h = hh->h;
1275
+ binstart = hh->binstart;
1276
+ binend = hh->binend;
1277
+ var = gsl_vector_get(v, 0);
1278
+ amp = gsl_vector_get(v, 1);
1279
+ for (i = binstart; i <= binend; i++) {
1280
+ if (gsl_histogram_get_range(h, i, &xl, &xh))
1281
+ rb_raise(rb_eIndexError, "wrong index");
1282
+ xi = (xl + xh)/2.0;
1283
+ yi = h->bin[i];
1284
+ sqw = sqrt(yi);
1285
+ gsl_vector_set(f, i-binstart, (amp*xi*exp(-xi*xi/var/2.0) - yi)*sqw);
1286
+ }
1287
+ return GSL_SUCCESS;
1288
+ }
1289
+
1290
+ static int Rayleigh_df(const gsl_vector *v, void *params, gsl_matrix *J)
1291
+ {
1292
+ struct fit_histogram *hh;
1293
+ gsl_histogram *h = NULL;
1294
+ double amp, var, xl, xh, xi, yi, y, sqw;
1295
+ size_t i, binstart, binend;
1296
+ hh = (struct fit_histogram *) params;
1297
+ h = hh->h;
1298
+ binstart = hh->binstart;
1299
+ binend = hh->binend;
1300
+ var = gsl_vector_get(v, 0);
1301
+ amp = gsl_vector_get(v, 1);
1302
+ for (i = binstart; i <= binend; i++) {
1303
+ if (gsl_histogram_get_range(h, i, &xl, &xh))
1304
+ rb_raise(rb_eIndexError, "wrong index");
1305
+ xi = (xl + xh)/2.0;
1306
+ yi = h->bin[i];
1307
+ sqw = sqrt(yi);
1308
+ y = xi*exp(-xi*xi/var/2.0);
1309
+ gsl_matrix_set(J, i-binstart, 0, amp*y*xi*xi/2/var/var*sqw);
1310
+ gsl_matrix_set(J, i-binstart, 1, y*sqw);
1311
+ }
1312
+ return GSL_SUCCESS;
1313
+ }
1314
+
1315
+ static int Rayleigh_fdf(const gsl_vector *v, void *params, gsl_vector *f,
1316
+ gsl_matrix *J)
1317
+ {
1318
+ Rayleigh_f(v, params, f);
1319
+ Rayleigh_df(v, params, J);
1320
+ return GSL_SUCCESS;
1321
+ }
1322
+
1323
+
1324
+ static VALUE rb_gsl_histogram_fit_rayleigh(int argc, VALUE *argv, VALUE obj)
1325
+ {
1326
+ gsl_histogram *h = NULL;
1327
+ struct fit_histogram hh;
1328
+ const gsl_multifit_fdfsolver_type *T;
1329
+ gsl_multifit_fdfsolver *s;
1330
+ int status;
1331
+ size_t iter = 0, binstart, binend;
1332
+ size_t n, dof; /* # of data points */
1333
+ size_t p = 2; /* # of fitting parameters */
1334
+ gsl_multifit_function_fdf f;
1335
+ gsl_matrix *covar = NULL;
1336
+ gsl_vector *x = NULL;
1337
+ double sigma, height, errs, errh, chi2;
1338
+ Data_Get_Struct(obj, gsl_histogram, h);
1339
+ binstart = 0;
1340
+ binend = h->n - 1;
1341
+ switch (argc) {
1342
+ case 2:
1343
+ CHECK_FIXNUM(argv[0]); CHECK_FIXNUM(argv[1]);
1344
+ binstart = FIX2INT(argv[0]);
1345
+ binend = FIX2INT(argv[1]);
1346
+ if (binend >= h->n) binend = h->n - 1;
1347
+ break;
1348
+ case 0:
1349
+ break;
1350
+ default:
1351
+ rb_raise(rb_eArgError, "too many arguments (%d for 0 or 2)", argc);
1352
+ break;
1353
+ }
1354
+ x = gsl_vector_alloc(p);
1355
+ gsl_vector_set(x, 0, gsl_pow_2(gsl_histogram_sigma(h))); /* initial values, var = 1 */
1356
+ gsl_vector_set(x, 1, gsl_histogram_max_val(h));
1357
+ hh.h = h;
1358
+ hh.binstart = binstart;
1359
+ hh.binend = binend;
1360
+ n = binend - binstart + 1;
1361
+
1362
+ covar = gsl_matrix_alloc(p, p);
1363
+
1364
+ f.f = Rayleigh_f;
1365
+ f.df = Rayleigh_df;
1366
+ f.fdf = Rayleigh_fdf;
1367
+ f.n = n;
1368
+ f.p = p;
1369
+ f.params = &hh;
1370
+
1371
+ T = gsl_multifit_fdfsolver_lmsder;
1372
+ s = gsl_multifit_fdfsolver_alloc(T, n, p);
1373
+ gsl_multifit_fdfsolver_set(s, &f, x);
1374
+
1375
+ do {
1376
+ iter++;
1377
+ status = gsl_multifit_fdfsolver_iterate(s);
1378
+ if (status) break;
1379
+ status = gsl_multifit_test_delta(s->dx, s->x, 1e-4, 1e-4);
1380
+ } while (status == GSL_CONTINUE);
1381
+ sigma = sqrt(gsl_vector_get(s->x, 0));
1382
+ height = gsl_vector_get(s->x, 1)*sigma*sigma;
1383
+ gsl_multifit_covar(s->J, 0.0, covar);
1384
+ chi2 = gsl_pow_2(gsl_blas_dnrm2(s->f)); /* not reduced chi-square */
1385
+ dof = n - p;
1386
+ errs = sqrt(chi2/dof*gsl_matrix_get(covar, 0, 0))/sigma/2;
1387
+ errh = sqrt(chi2/dof*gsl_matrix_get(covar, 1, 1));
1388
+
1389
+ gsl_multifit_fdfsolver_free(s);
1390
+ gsl_vector_free(x);
1391
+ gsl_matrix_free(covar);
1392
+ return rb_ary_new3(6, rb_float_new(sigma),
1393
+ rb_float_new(height), rb_float_new(errs),
1394
+ rb_float_new(errh),
1395
+ rb_float_new(chi2), INT2FIX(dof));
1396
+ }
1397
+
1398
+ /*
1399
+ * y(x) = Amp*exp(-a*x)
1400
+ */
1401
+ static int xExponential_f(const gsl_vector *v, void *params, gsl_vector *f);
1402
+ static int xExponential_df(const gsl_vector *v, void *params, gsl_matrix * J);
1403
+ static int xExponential_f(const gsl_vector *v, void *params, gsl_vector *f)
1404
+ {
1405
+ struct fit_histogram *hh;
1406
+ gsl_histogram *h = NULL;
1407
+ double amp, b, xl, xh, xi, yi, sqw;
1408
+ size_t i, binstart, binend;
1409
+ hh = (struct fit_histogram *) params;
1410
+ h = hh->h;
1411
+ binstart = hh->binstart;
1412
+ binend = hh->binend;
1413
+ b = gsl_vector_get(v, 0);
1414
+ amp = gsl_vector_get(v, 1);
1415
+ for (i = binstart; i <= binend; i++) {
1416
+ if (gsl_histogram_get_range(h, i, &xl, &xh))
1417
+ rb_raise(rb_eIndexError, "wrong index");
1418
+ xi = (xl + xh)/2.0;
1419
+ yi = h->bin[i];
1420
+ sqw = sqrt(yi);
1421
+ gsl_vector_set(f, i-binstart, (amp*xi*exp(-b*xi) - yi)*sqw);
1422
+ }
1423
+ return GSL_SUCCESS;
1424
+ }
1425
+
1426
+ static int xExponential_df(const gsl_vector *v, void *params, gsl_matrix *J)
1427
+ {
1428
+ struct fit_histogram *hh;
1429
+ gsl_histogram *h = NULL;
1430
+ double amp, b, xl, xh, xi, yi, y, sqw;
1431
+ size_t i, binstart, binend;
1432
+ hh = (struct fit_histogram *) params;
1433
+ h = hh->h;
1434
+ binstart = hh->binstart;
1435
+ binend = hh->binend;
1436
+ b = gsl_vector_get(v, 0);
1437
+ amp = gsl_vector_get(v, 1);
1438
+ for (i = binstart; i <= binend; i++) {
1439
+ if (gsl_histogram_get_range(h, i, &xl, &xh))
1440
+ rb_raise(rb_eIndexError, "wrong index");
1441
+ xi = (xl + xh)/2.0;
1442
+ yi = h->bin[i];
1443
+ sqw = sqrt(yi);
1444
+ y = xi*exp(-b*xi);
1445
+ gsl_matrix_set(J, i-binstart, 0, -amp*y*xi*sqw);
1446
+ gsl_matrix_set(J, i-binstart, 1, y*sqw);
1447
+ }
1448
+ return GSL_SUCCESS;
1449
+ }
1450
+
1451
+ static int xExponential_fdf(const gsl_vector *v, void *params, gsl_vector *f,
1452
+ gsl_matrix *J)
1453
+ {
1454
+ xExponential_f(v, params, f);
1455
+ xExponential_df(v, params, J);
1456
+ return GSL_SUCCESS;
1457
+ }
1458
+
1459
+
1460
+ static VALUE rb_gsl_histogram_fit_xexponential(int argc, VALUE *argv, VALUE obj)
1461
+ {
1462
+ gsl_histogram *h = NULL;
1463
+ struct fit_histogram hh;
1464
+ const gsl_multifit_fdfsolver_type *T;
1465
+ gsl_multifit_fdfsolver *s;
1466
+ int status;
1467
+ size_t iter = 0, binstart, binend;
1468
+ size_t n, dof; /* # of data points */
1469
+ size_t p = 2; /* # of fitting parameters */
1470
+ gsl_multifit_function_fdf f;
1471
+ gsl_matrix *covar = NULL;
1472
+ gsl_vector *x = NULL;
1473
+ double b, height, errs, errh, chi2;
1474
+ Data_Get_Struct(obj, gsl_histogram, h);
1475
+ binstart = 0;
1476
+ binend = h->n - 1;
1477
+ switch (argc) {
1478
+ case 2:
1479
+ CHECK_FIXNUM(argv[0]); CHECK_FIXNUM(argv[1]);
1480
+ binstart = FIX2INT(argv[0]);
1481
+ binend = FIX2INT(argv[1]);
1482
+ if (binend >= h->n) binend = h->n - 1;
1483
+ break;
1484
+ case 0:
1485
+ break;
1486
+ default:
1487
+ rb_raise(rb_eArgError, "too many arguments (%d for 0 or 2)", argc);
1488
+ break;
1489
+ }
1490
+ x = gsl_vector_alloc(p);
1491
+ gsl_vector_set(x, 0, gsl_histogram_sigma(h)); /* initial values, var = 1 */
1492
+ gsl_vector_set(x, 1, gsl_histogram_max_val(h));
1493
+ hh.h = h;
1494
+ hh.binstart = binstart;
1495
+ hh.binend = binend;
1496
+ n = binend - binstart + 1;
1497
+
1498
+ covar = gsl_matrix_alloc(p, p);
1499
+
1500
+ f.f = xExponential_f;
1501
+ f.df = xExponential_df;
1502
+ f.fdf = xExponential_fdf;
1503
+ f.n = n;
1504
+ f.p = p;
1505
+ f.params = &hh;
1506
+
1507
+ T = gsl_multifit_fdfsolver_lmsder;
1508
+ s = gsl_multifit_fdfsolver_alloc(T, n, p);
1509
+ gsl_multifit_fdfsolver_set(s, &f, x);
1510
+
1511
+ do {
1512
+ iter++;
1513
+ status = gsl_multifit_fdfsolver_iterate(s);
1514
+ if (status) break;
1515
+ status = gsl_multifit_test_delta(s->dx, s->x, 1e-4, 1e-4);
1516
+ } while (status == GSL_CONTINUE);
1517
+ b = gsl_vector_get(s->x, 0);
1518
+ height = gsl_vector_get(s->x, 1);
1519
+ gsl_multifit_covar(s->J, 0.0, covar);
1520
+ chi2 = gsl_pow_2(gsl_blas_dnrm2(s->f)); /* not reduced chi-square */
1521
+ dof = n - p;
1522
+ errs = sqrt(chi2/dof*gsl_matrix_get(covar, 0, 0));
1523
+ errh = sqrt(chi2/dof*gsl_matrix_get(covar, 1, 1));
1524
+
1525
+ gsl_multifit_fdfsolver_free(s);
1526
+ gsl_vector_free(x);
1527
+ gsl_matrix_free(covar);
1528
+ return rb_ary_new3(6, rb_float_new(b),
1529
+ rb_float_new(height), rb_float_new(errs),
1530
+ rb_float_new(errh),
1531
+ rb_float_new(chi2), INT2FIX(dof));
1532
+ }
1533
+
1534
+ static VALUE rb_gsl_histogram_fit(int argc, VALUE *argv, VALUE obj)
1535
+ {
1536
+ char fittype[32];
1537
+ if (argc < 1) rb_raise(rb_eArgError, "too few arguments");
1538
+ Check_Type(argv[0], T_STRING);
1539
+ strcpy(fittype, STR2CSTR(argv[0]));
1540
+ if (str_head_grep(fittype, "exp") == 0) {
1541
+ return rb_gsl_histogram_fit_exponential(argc-1, argv+1, obj);
1542
+ } else if (str_head_grep(fittype, "power") == 0) {
1543
+ return rb_gsl_histogram_fit_power(argc-1, argv+1, obj);
1544
+ } else if (str_head_grep(fittype, "gaus") == 0) {
1545
+ return rb_gsl_histogram_fit_gaussian(argc-1, argv+1, obj);
1546
+ } else if (str_head_grep(fittype, "rayleigh") == 0) {
1547
+ return rb_gsl_histogram_fit_rayleigh(argc-1, argv+1, obj);
1548
+ } else if (str_head_grep(fittype, "xexp") == 0) {
1549
+ return rb_gsl_histogram_fit_xexponential(argc-1, argv+1, obj);
1550
+ } else {
1551
+ rb_raise(rb_eRuntimeError,
1552
+ "unknown fitting type %s (exp, power, gaus expected)", fittype);
1553
+ }
1554
+ return Qnil;
1555
+ }
1556
+
1557
+ /* Integrate histogram: the two histograms must have the same range and bins. */
1558
+ void mygsl_histogram_integrate(const gsl_histogram *h, gsl_histogram *hi,
1559
+ size_t istart, size_t iend)
1560
+ {
1561
+ size_t i;
1562
+ if (iend >= istart) {
1563
+ if (istart < 0) istart = 0;
1564
+ if (iend >= h->n) iend = h->n-1;
1565
+ hi->bin[istart] = h->bin[istart];
1566
+ for (i = istart+1; i <= iend; i++) hi->bin[i] = hi->bin[i-1] + h->bin[i];
1567
+ } else {
1568
+ if (istart >= h->n) istart = h->n-1;
1569
+ if (iend < 0) iend = 0;
1570
+ hi->bin[istart] = h->bin[istart];
1571
+ for (i = istart-1; i >= iend; i--) {
1572
+ hi->bin[i] = hi->bin[i+1] + h->bin[i];
1573
+ if (i == 0) break;
1574
+ }
1575
+ }
1576
+ }
1577
+
1578
+ void mygsl_histogram_differentiate(const gsl_histogram *hi, gsl_histogram *h)
1579
+ {
1580
+ size_t i;
1581
+ h->bin[0] = hi->bin[0];
1582
+ for (i = 1; i < hi->n; i++) h->bin[i] = hi->bin[i] - hi->bin[i-1];
1583
+ }
1584
+
1585
+ /* Create a histogram integrating the given histogram h */
1586
+ gsl_histogram* mygsl_histogram_calloc_integrate(const gsl_histogram *h,
1587
+ size_t istart, size_t iend)
1588
+ {
1589
+ gsl_histogram *hi = NULL;
1590
+ hi = gsl_histogram_calloc_range(h->n, h->range);
1591
+ mygsl_histogram_integrate(h, hi, istart, iend);
1592
+ return hi;
1593
+ }
1594
+
1595
+ gsl_histogram* mygsl_histogram_calloc_differentiate(const gsl_histogram *hi)
1596
+ {
1597
+ gsl_histogram *h = NULL;
1598
+ h = gsl_histogram_calloc_range(hi->n, hi->range);
1599
+ mygsl_histogram_differentiate(hi, h);
1600
+ return h;
1601
+ }
1602
+
1603
+ static VALUE rb_gsl_histogram_integrate(int argc, VALUE *argv, VALUE obj)
1604
+ {
1605
+ gsl_histogram *h, *hi;
1606
+ size_t istart, iend;
1607
+ int itmp;
1608
+ Data_Get_Struct(obj, gsl_histogram, h);
1609
+ switch (argc) {
1610
+ case 2:
1611
+ istart = FIX2INT(argv[0]);
1612
+ iend = FIX2INT(argv[1]);
1613
+ break;
1614
+ case 1:
1615
+ switch (TYPE(argv[0])) {
1616
+ case T_ARRAY:
1617
+ istart = FIX2INT(rb_ary_entry(argv[0], 0));
1618
+ iend = FIX2INT(rb_ary_entry(argv[0], 1));
1619
+ break;
1620
+ case T_FIXNUM:
1621
+ itmp = FIX2INT(argv[0]);
1622
+ if (itmp == -1) {
1623
+ istart = h->n - 1;
1624
+ iend = 0;
1625
+ } else {
1626
+ istart = 0;
1627
+ iend = h->n - 1;
1628
+ }
1629
+ break;
1630
+ default:
1631
+ rb_raise(rb_eArgError, "wrong argument type %s (Arran or Fixnum expected)",
1632
+ rb_class2name(CLASS_OF(argv[0])));
1633
+ break;
1634
+ }
1635
+ break;
1636
+ case 0:
1637
+ istart = 0;
1638
+ iend = h->n - 1;
1639
+ break;
1640
+ default:
1641
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 0-2)", argc);
1642
+ break;
1643
+ }
1644
+ hi = mygsl_histogram_calloc_integrate(h, istart, iend);
1645
+ return Data_Wrap_Struct(cgsl_histogram_integ, 0, gsl_histogram_free, hi);
1646
+ }
1647
+
1648
+ static VALUE rb_gsl_histogram_differentiate(VALUE obj)
1649
+ {
1650
+ gsl_histogram *h, *hi;
1651
+ Data_Get_Struct(obj, gsl_histogram, hi);
1652
+ h = mygsl_histogram_calloc_differentiate(hi);
1653
+ return Data_Wrap_Struct(cgsl_histogram, 0, gsl_histogram_free, h);
1654
+ }
1655
+
1656
+ static gsl_histogram* mygsl_histogram_rebin(const gsl_histogram *h, size_t m)
1657
+ {
1658
+ gsl_histogram *hnew;
1659
+ double w;
1660
+ size_t n, i, j, k;
1661
+ if (m > h->n) m = h->n;
1662
+ n = (size_t) h->n/m;
1663
+ if (n*m != h->n) n += 1;
1664
+ w = (h->range[h->n] - h->range[0])/h->n;
1665
+ hnew = gsl_histogram_alloc(n);
1666
+ for (i = 0, j = 0; i <= n; i++) {
1667
+ if (i*m <= h->n) hnew->range[i] = h->range[i*m];
1668
+ else hnew->range[i] = w*m*i;
1669
+ }
1670
+ for (i = 0, j = 0; i < n; i++) {
1671
+ hnew->bin[i] = 0;
1672
+ for (k = 0; k < m && j < h->n; k++) hnew->bin[i] += h->bin[j++];
1673
+ }
1674
+ return hnew;
1675
+ }
1676
+
1677
+ static VALUE rb_gsl_histogram_rebin(int argc, VALUE *argv, VALUE obj)
1678
+ {
1679
+ gsl_histogram *h, *hnew;
1680
+ size_t m = 2;
1681
+ switch (argc) {
1682
+ case 1:
1683
+ CHECK_FIXNUM(argv[0]);
1684
+ m = (size_t) FIX2INT(argv[0]);
1685
+ break;
1686
+ case 0:
1687
+ m = 2;
1688
+ break;
1689
+ default:
1690
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 0 or 1)", argc);
1691
+ break;
1692
+ }
1693
+ Data_Get_Struct(obj, gsl_histogram, h);
1694
+ hnew = mygsl_histogram_rebin(h, m);
1695
+ return Data_Wrap_Struct(cgsl_histogram, 0, gsl_histogram_free, hnew);
1696
+ }
1697
+
1698
+ static int mygsl_histogram_fread2(FILE * stream, gsl_histogram * h)
1699
+ {
1700
+ double min, max;
1701
+ int status;
1702
+ status = gsl_block_raw_fread(stream, &min, 1, 1);
1703
+ if (status) return status;
1704
+ status = gsl_block_raw_fread(stream, &max, 1, 1);
1705
+ if (status) return status;
1706
+ gsl_histogram_set_ranges_uniform(h, min, max);
1707
+ status = gsl_block_raw_fread (stream, h->bin, h->n, 1);
1708
+ if (status) return status;
1709
+ return status;
1710
+ }
1711
+
1712
+ static int mygsl_histogram_fwrite2(FILE * stream, const gsl_histogram * h)
1713
+ {
1714
+ int status;
1715
+ status = gsl_block_raw_fwrite (stream, h->range, 1, 1);
1716
+ if (status) return status;
1717
+ status = gsl_block_raw_fwrite (stream, h->range+h->n, 1, 1);
1718
+ if (status) return status;
1719
+ status = gsl_block_raw_fwrite (stream, h->bin, h->n, 1);
1720
+ return status;
1721
+ }
1722
+
1723
+ static VALUE rb_gsl_histogram_fwrite2(VALUE obj, VALUE io)
1724
+ {
1725
+ gsl_histogram *h = NULL;
1726
+ FILE *f;
1727
+ int status, flag = 0;
1728
+ Data_Get_Struct(obj, gsl_histogram, h);
1729
+ f = rb_gsl_open_writefile(io, &flag);
1730
+ status = mygsl_histogram_fwrite2(f, h);
1731
+ if (flag == 1) fclose(f);
1732
+ return INT2FIX(status);
1733
+ }
1734
+
1735
+ static VALUE rb_gsl_histogram_fread2(VALUE obj, VALUE io)
1736
+ {
1737
+ gsl_histogram *h = NULL;
1738
+ FILE *f;
1739
+ int status, flag = 0;
1740
+ Data_Get_Struct(obj, gsl_histogram, h);
1741
+ f = rb_gsl_open_readfile(io, &flag);
1742
+ status = mygsl_histogram_fread2(f, h);
1743
+ if (flag == 1) fclose(f);
1744
+ return INT2FIX(status);
1745
+ }
1746
+
1747
+ static gsl_histogram* mygsl_histogram_calloc_reverse(const gsl_histogram *h)
1748
+ {
1749
+ gsl_histogram *hnew;
1750
+ size_t i, n;
1751
+ hnew = gsl_histogram_alloc(h->n);
1752
+ n = h->n;
1753
+ for (i = 0; i <= n; i++) hnew->range[i] = h->range[n-i];
1754
+ for (i = 0; i < n; i++) hnew->bin[i] = h->bin[n-1-i];
1755
+ return hnew;
1756
+ }
1757
+
1758
+ static VALUE rb_gsl_histogram_reverse(VALUE obj)
1759
+ {
1760
+ gsl_histogram *h, *hnew;
1761
+ Data_Get_Struct(obj, gsl_histogram, h);
1762
+ hnew = mygsl_histogram_calloc_reverse(h);
1763
+ return Data_Wrap_Struct(cgsl_histogram, 0, gsl_histogram_free, hnew);
1764
+ }
1765
+
1766
+ /* The functions below are not included in GSL */
1767
+ /*
1768
+ * Returns an x value at which the histogram integration
1769
+ * reaches the given percentile. The x value is calculated
1770
+ * by an interpolation between the ranges in which the percentile
1771
+ * is found.
1772
+ */
1773
+ static double histogram_percentile(const gsl_histogram *h, double f)
1774
+ {
1775
+ double sum = gsl_histogram_sum(h), sf;
1776
+ double val, s = 0, x;
1777
+ double ri, ri1;
1778
+ size_t i;
1779
+ sf = sum * f;
1780
+ for (i = 0; i < h->n; i++) {
1781
+ val = gsl_histogram_get(h, i);
1782
+ if ((s+val) > sf) break;
1783
+ s += val;
1784
+ }
1785
+ ri = h->range[i];
1786
+ ri1 = h->range[i+1];
1787
+ x = (sf - s)*(ri1 - ri)/val + ri;
1788
+ return x;
1789
+ }
1790
+
1791
+ static double histogram_median(const gsl_histogram *h)
1792
+ {
1793
+ return histogram_percentile(h, 0.5);
1794
+ }
1795
+
1796
+ static VALUE rb_gsl_histogram_percentile(VALUE obj, VALUE f)
1797
+ {
1798
+ gsl_histogram *h;
1799
+ Data_Get_Struct(obj, gsl_histogram, h);
1800
+ return rb_float_new(histogram_percentile(h, NUM2DBL(f)));
1801
+ }
1802
+
1803
+ static VALUE rb_gsl_histogram_median(VALUE obj)
1804
+ {
1805
+ gsl_histogram *h;
1806
+ Data_Get_Struct(obj, gsl_histogram, h);
1807
+ return rb_float_new(histogram_median(h));
1808
+ }
1809
+
1810
+ static double histogram_percentile_inv(const gsl_histogram *h, double x)
1811
+ {
1812
+ double sum = gsl_histogram_sum(h);
1813
+ double val, s = 0;
1814
+ double ri, ri1, q;
1815
+ size_t i;
1816
+
1817
+ for (i = 0; i < h->n; i++) {
1818
+ val = gsl_histogram_get(h, i);
1819
+ if (h->range[i+1] > x) break;
1820
+ s += val;
1821
+ }
1822
+ ri = h->range[i];
1823
+ ri1 = h->range[i+1];
1824
+ q = s + val/(ri1 - ri)*(x - ri);
1825
+ return q/sum;
1826
+ }
1827
+
1828
+ static VALUE rb_gsl_histogram_percentile_inv(VALUE obj, VALUE x)
1829
+ {
1830
+ gsl_histogram *h;
1831
+ Data_Get_Struct(obj, gsl_histogram, h);
1832
+ return rb_float_new(histogram_percentile_inv(h, NUM2DBL(x)));
1833
+ }
1834
+
1835
+ void Init_gsl_histogram(VALUE module)
1836
+ {
1837
+ VALUE cgsl_histogram_pdf;
1838
+
1839
+ cgsl_histogram = rb_define_class_under(module, "Histogram", cGSL_Object);
1840
+ cgsl_histogram_range = rb_define_class_under(cgsl_histogram, "Range",
1841
+ cgsl_vector_view_ro);
1842
+ cgsl_histogram_bin = rb_define_class_under(cgsl_histogram, "Bin",
1843
+ cgsl_vector_view);
1844
+ cgsl_histogram_integ = rb_define_class_under(cgsl_histogram, "Integral",
1845
+ cgsl_histogram);
1846
+
1847
+ #ifdef GSL_0_9_4_LATER
1848
+ rb_define_singleton_method(cgsl_histogram, "alloc", rb_gsl_histogram_alloc, -1);
1849
+ /* rb_define_singleton_method(cgsl_histogram, "new", rb_gsl_histogram_alloc, -1);*/
1850
+ rb_define_singleton_method(cgsl_histogram, "[]", rb_gsl_histogram_alloc, -1);
1851
+
1852
+ rb_define_singleton_method(cgsl_histogram, "alloc_uniform",
1853
+ rb_gsl_histogram_alloc_uniform, -1);
1854
+ rb_define_singleton_method(cgsl_histogram, "new_uniform",
1855
+ rb_gsl_histogram_alloc_uniform, -1);
1856
+
1857
+ rb_define_singleton_method(cgsl_histogram, "alloc_with_min_max_step",
1858
+ rb_gsl_histogram_alloc_with_min_max_step, 3);
1859
+ rb_define_singleton_method(cgsl_histogram, "new_with_min_max_step",
1860
+ rb_gsl_histogram_alloc_with_min_max_step, 3);
1861
+ #endif
1862
+
1863
+ rb_define_singleton_method(cgsl_histogram, "calloc",
1864
+ rb_gsl_histogram_calloc, 1);
1865
+ rb_define_singleton_method(cgsl_histogram, "calloc_range",
1866
+ rb_gsl_histogram_calloc_range, -1);
1867
+
1868
+ rb_define_method(cgsl_histogram, "bins", rb_gsl_histogram_bins, 0);
1869
+ rb_define_alias(cgsl_histogram, "n", "bins");
1870
+ rb_define_alias(cgsl_histogram, "size", "bins");
1871
+ rb_define_method(cgsl_histogram, "set_ranges", rb_gsl_histogram_set_ranges, -1);
1872
+ rb_define_method(cgsl_histogram, "range", rb_gsl_histogram_range, 0);
1873
+ rb_define_method(cgsl_histogram, "bin", rb_gsl_histogram_bin, 0);
1874
+ rb_define_method(cgsl_histogram, "set_ranges_uniform",
1875
+ rb_gsl_histogram_set_ranges_uniform, -1);
1876
+ rb_define_singleton_method(cgsl_histogram, "memcpy", rb_gsl_histogram_memcpy, 2);
1877
+ rb_define_method(cgsl_histogram, "clone", rb_gsl_histogram_clone, 0);
1878
+ rb_define_alias(cgsl_histogram, "duplicate", "clone");
1879
+ rb_define_method(cgsl_histogram, "increment", rb_gsl_histogram_accumulate, -1);
1880
+ rb_define_alias(cgsl_histogram, "fill", "increment");
1881
+ rb_define_alias(cgsl_histogram, "accumulate", "increment");
1882
+ rb_define_method(cgsl_histogram, "increment2", rb_gsl_histogram_accumulate2, -1);
1883
+ rb_define_alias(cgsl_histogram, "accumulate2", "increment2");
1884
+ rb_define_alias(cgsl_histogram, "fill2", "increment2");
1885
+
1886
+ rb_define_method(cgsl_histogram, "get", rb_gsl_histogram_get, 1);
1887
+ rb_define_alias(cgsl_histogram, "[]", "get");
1888
+ rb_define_method(cgsl_histogram, "get_range", rb_gsl_histogram_get_range, 1);
1889
+ rb_define_method(cgsl_histogram, "max", rb_gsl_histogram_max, 0);
1890
+ rb_define_method(cgsl_histogram, "min", rb_gsl_histogram_min, 0);
1891
+ rb_define_method(cgsl_histogram, "reset", rb_gsl_histogram_reset, 0);
1892
+ rb_define_method(cgsl_histogram, "find", rb_gsl_histogram_find, 1);
1893
+ rb_define_method(cgsl_histogram, "max_val", rb_gsl_histogram_max_val, 0);
1894
+ rb_define_method(cgsl_histogram, "max_bin", rb_gsl_histogram_max_bin, 0);
1895
+ rb_define_method(cgsl_histogram, "min_val", rb_gsl_histogram_min_val, 0);
1896
+ rb_define_method(cgsl_histogram, "min_bin", rb_gsl_histogram_min_bin, 0);
1897
+ rb_define_method(cgsl_histogram, "mean", rb_gsl_histogram_mean, 0);
1898
+ rb_define_method(cgsl_histogram, "sigma", rb_gsl_histogram_sigma, 0);
1899
+
1900
+ rb_define_method(cgsl_histogram, "sum", rb_gsl_histogram_integral, -1);
1901
+ rb_define_alias(cgsl_histogram, "integral", "sum");
1902
+
1903
+ rb_define_method(cgsl_histogram, "equal_bins_p",
1904
+ rb_gsl_histogram_equal_bins_p, -1);
1905
+ rb_define_alias(cgsl_histogram, "equal_bins", "equal_bins_p");
1906
+ rb_define_singleton_method(cgsl_histogram, "equal_bins_p",
1907
+ rb_gsl_histogram_equal_bins_p, -1);
1908
+ rb_define_singleton_method(cgsl_histogram, "equal_bins",
1909
+ rb_gsl_histogram_equal_bins_p, -1);
1910
+ rb_define_method(cgsl_histogram, "equal_bins_p?",
1911
+ rb_gsl_histogram_equal_bins_p2, -1);
1912
+ rb_define_alias(cgsl_histogram, "equal_bins?", "equal_bins_p?");
1913
+ rb_define_singleton_method(cgsl_histogram, "equal_bins_p?",
1914
+ rb_gsl_histogram_equal_bins_p2, -1);
1915
+ rb_define_singleton_method(cgsl_histogram, "equal_bins?",
1916
+ rb_gsl_histogram_equal_bins_p2, -1);
1917
+
1918
+ rb_define_method(cgsl_histogram, "add", rb_gsl_histogram_add, 1);
1919
+ rb_define_alias(cgsl_histogram, "+", "add");
1920
+ rb_define_method(cgsl_histogram, "sub", rb_gsl_histogram_sub, 1);
1921
+ rb_define_alias(cgsl_histogram, "-", "sub");
1922
+ rb_define_method(cgsl_histogram, "mul", rb_gsl_histogram_mul, 1);
1923
+ rb_define_alias(cgsl_histogram, "*", "mul");
1924
+ rb_define_method(cgsl_histogram, "div", rb_gsl_histogram_div, 1);
1925
+ rb_define_alias(cgsl_histogram, "/", "div");
1926
+
1927
+ rb_define_method(cgsl_histogram, "add!", rb_gsl_histogram_add2, 1);
1928
+ rb_define_method(cgsl_histogram, "sub!", rb_gsl_histogram_sub2, 1);
1929
+ rb_define_method(cgsl_histogram, "mul!", rb_gsl_histogram_mul2, 1);
1930
+ rb_define_method(cgsl_histogram, "div!", rb_gsl_histogram_div2, 1);
1931
+
1932
+ rb_define_method(cgsl_histogram, "scale!", rb_gsl_histogram_scale_bang, -1);
1933
+ rb_define_method(cgsl_histogram, "scale", rb_gsl_histogram_scale, -1);
1934
+ rb_define_method(cgsl_histogram, "shift!", rb_gsl_histogram_shift, 1);
1935
+ rb_define_method(cgsl_histogram, "shift", rb_gsl_histogram_shift2, 1);
1936
+
1937
+ rb_define_method(cgsl_histogram, "fwrite", rb_gsl_histogram_fwrite, 1);
1938
+ rb_define_method(cgsl_histogram, "fread", rb_gsl_histogram_fread, 1);
1939
+ rb_define_method(cgsl_histogram, "fwrite2", rb_gsl_histogram_fwrite2, 1);
1940
+ rb_define_method(cgsl_histogram, "fread2", rb_gsl_histogram_fread2, 1);
1941
+ rb_define_method(cgsl_histogram, "fprintf", rb_gsl_histogram_fprintf, -1);
1942
+ rb_define_method(cgsl_histogram, "printf", rb_gsl_histogram_printf, -1);
1943
+ rb_define_method(cgsl_histogram, "fscanf", rb_gsl_histogram_fscanf, 1);
1944
+ rb_define_method(cgsl_histogram, "print", rb_gsl_histogram_print, 0);
1945
+
1946
+ cgsl_histogram_pdf = rb_define_class_under(cgsl_histogram, "Pdf", cGSL_Object);
1947
+ rb_define_singleton_method(cgsl_histogram_pdf, "alloc",
1948
+ rb_gsl_histogram_pdf_alloc, 1);
1949
+ /* rb_define_singleton_method(cgsl_histogram_pdf, "new",
1950
+ rb_gsl_histogram_pdf_alloc, 1);*/
1951
+ #ifdef GSL_0_9_4_LATER
1952
+ rb_define_method(cgsl_histogram_pdf, "init", rb_gsl_histogram_pdf_init, 1);
1953
+ #endif
1954
+ rb_define_method(cgsl_histogram_pdf, "sample", rb_gsl_histogram_pdf_sample, 1);
1955
+
1956
+ rb_define_method(cgsl_histogram_pdf, "range", rb_gsl_histogram_pdf_range, 0);
1957
+ rb_define_method(cgsl_histogram_pdf, "sum", rb_gsl_histogram_pdf_sum, 0);
1958
+
1959
+ /*****/
1960
+ rb_define_method(cgsl_histogram, "graph", rb_gsl_histogram_graph, -1);
1961
+ rb_define_alias(cgsl_histogram, "draw", "graph");
1962
+
1963
+ rb_define_method(cgsl_histogram, "plot", rb_gsl_histogram_plot, -1);
1964
+
1965
+ rb_define_method(cgsl_histogram, "fit_gaussian",
1966
+ rb_gsl_histogram_fit_gaussian, -1);
1967
+ rb_define_method(cgsl_histogram, "fit_exponential",
1968
+ rb_gsl_histogram_fit_exponential, -1);
1969
+ rb_define_method(cgsl_histogram, "fit_xexponential",
1970
+ rb_gsl_histogram_fit_xexponential, -1);
1971
+ rb_define_method(cgsl_histogram, "fit_power",
1972
+ rb_gsl_histogram_fit_power, -1);
1973
+ rb_define_method(cgsl_histogram, "fit_rayleigh",
1974
+ rb_gsl_histogram_fit_rayleigh, -1);
1975
+ rb_define_method(cgsl_histogram, "fit",
1976
+ rb_gsl_histogram_fit, -1);
1977
+
1978
+ rb_define_method(cgsl_histogram, "integrate", rb_gsl_histogram_integrate, -1);
1979
+ rb_undef_method(cgsl_histogram_integ, "integrate");
1980
+ rb_define_method(cgsl_histogram_integ, "differentiate",
1981
+ rb_gsl_histogram_differentiate, 0);
1982
+ rb_define_alias(cgsl_histogram_integ, "diff", "differentiate");
1983
+
1984
+ rb_define_method(cgsl_histogram, "normalize", rb_gsl_histogram_normalize, 0);
1985
+ rb_define_method(cgsl_histogram, "normalize!", rb_gsl_histogram_normalize_bang, 0);
1986
+
1987
+ rb_define_method(cgsl_histogram, "rebin", rb_gsl_histogram_rebin, -1);
1988
+ rb_define_alias(cgsl_histogram, "mergebin", "rebin");
1989
+
1990
+ rb_define_method(cgsl_histogram, "reverse", rb_gsl_histogram_reverse, 0);
1991
+
1992
+ rb_define_method(cgsl_histogram, "percentile", rb_gsl_histogram_percentile, 1);
1993
+ rb_define_method(cgsl_histogram, "median", rb_gsl_histogram_median, 0);
1994
+ rb_define_method(cgsl_histogram, "percentile_inv", rb_gsl_histogram_percentile_inv, 1);
1995
+ }