gsl 1.12.108
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +29 -0
- data/Rakefile +54 -0
- data/VERSION +2 -0
- data/ext/MANIFEST +119 -0
- data/ext/alf.c +206 -0
- data/ext/array.c +666 -0
- data/ext/array_complex.c +247 -0
- data/ext/blas.c +29 -0
- data/ext/blas1.c +731 -0
- data/ext/blas2.c +1093 -0
- data/ext/blas3.c +881 -0
- data/ext/block.c +44 -0
- data/ext/block_source.c +886 -0
- data/ext/bspline.c +130 -0
- data/ext/bundle.c +3 -0
- data/ext/cdf.c +754 -0
- data/ext/cheb.c +542 -0
- data/ext/combination.c +283 -0
- data/ext/common.c +325 -0
- data/ext/complex.c +1004 -0
- data/ext/const.c +673 -0
- data/ext/const_additional.c +120 -0
- data/ext/cqp.c +283 -0
- data/ext/deriv.c +195 -0
- data/ext/dht.c +361 -0
- data/ext/diff.c +166 -0
- data/ext/dirac.c +395 -0
- data/ext/eigen.c +2373 -0
- data/ext/error.c +194 -0
- data/ext/extconf.rb +281 -0
- data/ext/fcmp.c +66 -0
- data/ext/fft.c +1092 -0
- data/ext/fit.c +205 -0
- data/ext/fresnel.c +312 -0
- data/ext/function.c +524 -0
- data/ext/geometry.c +139 -0
- data/ext/graph.c +1638 -0
- data/ext/gsl.c +271 -0
- data/ext/gsl_narray.c +653 -0
- data/ext/histogram.c +1995 -0
- data/ext/histogram2d.c +1068 -0
- data/ext/histogram3d.c +884 -0
- data/ext/histogram3d_source.c +750 -0
- data/ext/histogram_find.c +101 -0
- data/ext/histogram_oper.c +159 -0
- data/ext/ieee.c +98 -0
- data/ext/integration.c +1138 -0
- data/ext/interp.c +512 -0
- data/ext/jacobi.c +739 -0
- data/ext/linalg.c +4047 -0
- data/ext/linalg_complex.c +741 -0
- data/ext/math.c +725 -0
- data/ext/matrix.c +39 -0
- data/ext/matrix_complex.c +1732 -0
- data/ext/matrix_double.c +560 -0
- data/ext/matrix_int.c +256 -0
- data/ext/matrix_source.c +2733 -0
- data/ext/min.c +250 -0
- data/ext/monte.c +992 -0
- data/ext/multifit.c +1879 -0
- data/ext/multimin.c +808 -0
- data/ext/multimin_fsdf.c +156 -0
- data/ext/multiroots.c +955 -0
- data/ext/ndlinear.c +321 -0
- data/ext/nmf.c +167 -0
- data/ext/nmf_wrap.c +72 -0
- data/ext/ntuple.c +469 -0
- data/ext/odeiv.c +959 -0
- data/ext/ool.c +879 -0
- data/ext/oper_complex_source.c +253 -0
- data/ext/permutation.c +596 -0
- data/ext/poly.c +42 -0
- data/ext/poly2.c +265 -0
- data/ext/poly_source.c +1885 -0
- data/ext/qrng.c +171 -0
- data/ext/randist.c +1873 -0
- data/ext/rational.c +480 -0
- data/ext/rng.c +612 -0
- data/ext/root.c +408 -0
- data/ext/sf.c +1494 -0
- data/ext/sf_airy.c +200 -0
- data/ext/sf_bessel.c +867 -0
- data/ext/sf_clausen.c +28 -0
- data/ext/sf_coulomb.c +206 -0
- data/ext/sf_coupling.c +118 -0
- data/ext/sf_dawson.c +29 -0
- data/ext/sf_debye.c +157 -0
- data/ext/sf_dilog.c +42 -0
- data/ext/sf_elementary.c +44 -0
- data/ext/sf_ellint.c +206 -0
- data/ext/sf_elljac.c +29 -0
- data/ext/sf_erfc.c +93 -0
- data/ext/sf_exp.c +164 -0
- data/ext/sf_expint.c +211 -0
- data/ext/sf_fermi_dirac.c +148 -0
- data/ext/sf_gamma.c +344 -0
- data/ext/sf_gegenbauer.c +96 -0
- data/ext/sf_hyperg.c +197 -0
- data/ext/sf_laguerre.c +112 -0
- data/ext/sf_lambert.c +47 -0
- data/ext/sf_legendre.c +367 -0
- data/ext/sf_log.c +104 -0
- data/ext/sf_mathieu.c +238 -0
- data/ext/sf_power.c +46 -0
- data/ext/sf_psi.c +98 -0
- data/ext/sf_synchrotron.c +48 -0
- data/ext/sf_transport.c +76 -0
- data/ext/sf_trigonometric.c +207 -0
- data/ext/sf_zeta.c +119 -0
- data/ext/signal.c +310 -0
- data/ext/siman.c +718 -0
- data/ext/sort.c +208 -0
- data/ext/spline.c +395 -0
- data/ext/stats.c +799 -0
- data/ext/sum.c +168 -0
- data/ext/tamu_anova.c +56 -0
- data/ext/tensor.c +38 -0
- data/ext/tensor_source.c +1123 -0
- data/ext/vector.c +38 -0
- data/ext/vector_complex.c +2236 -0
- data/ext/vector_double.c +1433 -0
- data/ext/vector_int.c +204 -0
- data/ext/vector_source.c +3329 -0
- data/ext/wavelet.c +937 -0
- data/include/rb_gsl.h +151 -0
- data/include/rb_gsl_array.h +238 -0
- data/include/rb_gsl_cheb.h +21 -0
- data/include/rb_gsl_common.h +343 -0
- data/include/rb_gsl_complex.h +25 -0
- data/include/rb_gsl_const.h +29 -0
- data/include/rb_gsl_dirac.h +13 -0
- data/include/rb_gsl_eigen.h +17 -0
- data/include/rb_gsl_fft.h +62 -0
- data/include/rb_gsl_fit.h +25 -0
- data/include/rb_gsl_function.h +27 -0
- data/include/rb_gsl_graph.h +70 -0
- data/include/rb_gsl_histogram.h +63 -0
- data/include/rb_gsl_histogram3d.h +97 -0
- data/include/rb_gsl_integration.h +17 -0
- data/include/rb_gsl_interp.h +46 -0
- data/include/rb_gsl_linalg.h +25 -0
- data/include/rb_gsl_math.h +26 -0
- data/include/rb_gsl_odeiv.h +21 -0
- data/include/rb_gsl_poly.h +71 -0
- data/include/rb_gsl_rational.h +37 -0
- data/include/rb_gsl_rng.h +21 -0
- data/include/rb_gsl_root.h +22 -0
- data/include/rb_gsl_sf.h +119 -0
- data/include/rb_gsl_statistics.h +17 -0
- data/include/rb_gsl_tensor.h +45 -0
- data/include/rb_gsl_with_narray.h +22 -0
- data/include/templates_off.h +87 -0
- data/include/templates_on.h +241 -0
- data/lib/gsl/gnuplot.rb +41 -0
- data/lib/gsl/oper.rb +68 -0
- data/lib/ool.rb +22 -0
- data/lib/ool/conmin.rb +30 -0
- metadata +224 -0
data/ext/combination.c
ADDED
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
/*
|
|
2
|
+
combination.c
|
|
3
|
+
Ruby/GSL: Ruby extension library for GSL (GNU Scientific Library)
|
|
4
|
+
(C) Copyright 2004 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
|
+
#ifdef GSL_1_1_LATER
|
|
14
|
+
#include "rb_gsl_common.h"
|
|
15
|
+
#include "rb_gsl_array.h"
|
|
16
|
+
|
|
17
|
+
static VALUE cgsl_combination_data;
|
|
18
|
+
|
|
19
|
+
static VALUE rb_gsl_combination_new(VALUE klass, VALUE n, VALUE k)
|
|
20
|
+
{
|
|
21
|
+
gsl_combination *c = NULL;
|
|
22
|
+
CHECK_FIXNUM(n);CHECK_FIXNUM(k);
|
|
23
|
+
c = gsl_combination_alloc(FIX2INT(n), FIX2INT(k));
|
|
24
|
+
return Data_Wrap_Struct(klass, 0, gsl_combination_free, c);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
static VALUE rb_gsl_combination_calloc(VALUE klass, VALUE n, VALUE k)
|
|
28
|
+
{
|
|
29
|
+
gsl_combination *c = NULL;
|
|
30
|
+
CHECK_FIXNUM(n);CHECK_FIXNUM(k);
|
|
31
|
+
c = gsl_combination_calloc(FIX2INT(n), FIX2INT(k));
|
|
32
|
+
return Data_Wrap_Struct(klass, 0, gsl_combination_free, c);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
static VALUE rb_gsl_combination_init_first(VALUE obj)
|
|
36
|
+
{
|
|
37
|
+
gsl_combination *c = NULL;
|
|
38
|
+
Data_Get_Struct(obj, gsl_combination, c);
|
|
39
|
+
gsl_combination_init_first(c);
|
|
40
|
+
return obj;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
static VALUE rb_gsl_combination_init_last(VALUE obj)
|
|
44
|
+
{
|
|
45
|
+
gsl_combination *c = NULL;
|
|
46
|
+
Data_Get_Struct(obj, gsl_combination, c);
|
|
47
|
+
gsl_combination_init_last(c);
|
|
48
|
+
return obj;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
#ifdef GSL_1_4_LATER
|
|
52
|
+
/* singleton */
|
|
53
|
+
static VALUE rb_gsl_combination_memcpy(VALUE klass, VALUE dst, VALUE src)
|
|
54
|
+
{
|
|
55
|
+
gsl_combination *c, *c2;
|
|
56
|
+
if (!rb_obj_is_kind_of(dst, klass))
|
|
57
|
+
rb_raise(rb_eTypeError, "wrong argument type %s (Combination expected)",
|
|
58
|
+
rb_class2name(CLASS_OF(dst)));
|
|
59
|
+
if (!rb_obj_is_kind_of(src, klass))
|
|
60
|
+
rb_raise(rb_eTypeError, "wrong argument type %s (Combination expected)",
|
|
61
|
+
rb_class2name(CLASS_OF(src)));
|
|
62
|
+
|
|
63
|
+
Data_Get_Struct(dst, gsl_combination, c2);
|
|
64
|
+
Data_Get_Struct(src, gsl_combination, c);
|
|
65
|
+
gsl_combination_memcpy(c2, c);
|
|
66
|
+
return dst;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
static VALUE rb_gsl_combination_clone(VALUE obj)
|
|
70
|
+
{
|
|
71
|
+
gsl_combination *c, *c2;
|
|
72
|
+
Data_Get_Struct(obj, gsl_combination, c);
|
|
73
|
+
c2 = gsl_combination_alloc(c->n, c->k);
|
|
74
|
+
gsl_combination_memcpy(c2, c);
|
|
75
|
+
return Data_Wrap_Struct(CLASS_OF(obj), 0, gsl_combination_free, c2);
|
|
76
|
+
}
|
|
77
|
+
#endif
|
|
78
|
+
|
|
79
|
+
static VALUE rb_gsl_combination_get(VALUE obj, VALUE ii)
|
|
80
|
+
{
|
|
81
|
+
gsl_combination *c = NULL;
|
|
82
|
+
size_t i;
|
|
83
|
+
CHECK_FIXNUM(ii);
|
|
84
|
+
Data_Get_Struct(obj, gsl_combination, c);
|
|
85
|
+
i = FIX2INT(ii);
|
|
86
|
+
if (i > c->n) rb_raise(rb_eIndexError, "index out of range");
|
|
87
|
+
return INT2FIX(gsl_combination_get(c, i));
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
static VALUE rb_gsl_combination_set(VALUE obj, VALUE ii, VALUE val)
|
|
91
|
+
{
|
|
92
|
+
gsl_combination *c = NULL;
|
|
93
|
+
size_t i;
|
|
94
|
+
CHECK_FIXNUM(ii);
|
|
95
|
+
CHECK_FIXNUM(val);
|
|
96
|
+
Data_Get_Struct(obj, gsl_combination, c);
|
|
97
|
+
i = FIX2INT(ii);
|
|
98
|
+
c->data[i] = FIX2INT(val);
|
|
99
|
+
return obj;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
static VALUE rb_gsl_combination_n(VALUE obj)
|
|
103
|
+
{
|
|
104
|
+
gsl_combination *c = NULL;
|
|
105
|
+
Data_Get_Struct(obj, gsl_combination, c);
|
|
106
|
+
return INT2FIX(gsl_combination_n(c));
|
|
107
|
+
|
|
108
|
+
}
|
|
109
|
+
static VALUE rb_gsl_combination_k(VALUE obj)
|
|
110
|
+
{
|
|
111
|
+
gsl_combination *c = NULL;
|
|
112
|
+
Data_Get_Struct(obj, gsl_combination, c);
|
|
113
|
+
return INT2FIX(gsl_combination_k(c));
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
static VALUE rb_gsl_combination_data(VALUE obj)
|
|
117
|
+
{
|
|
118
|
+
gsl_combination *c = NULL;
|
|
119
|
+
gsl_permutation *p = NULL;
|
|
120
|
+
Data_Get_Struct(obj, gsl_combination, c);
|
|
121
|
+
p = ALLOC(gsl_permutation);
|
|
122
|
+
p->size = c->k;
|
|
123
|
+
p->data = c->data;
|
|
124
|
+
return Data_Wrap_Struct(cgsl_combination_data, 0, free, p);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
static VALUE rb_gsl_combination_valid(VALUE obj)
|
|
128
|
+
{
|
|
129
|
+
gsl_combination *c = NULL;
|
|
130
|
+
Data_Get_Struct(obj, gsl_combination, c);
|
|
131
|
+
return INT2FIX(gsl_combination_valid(c));
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
static VALUE rb_gsl_combination_valid2(VALUE obj)
|
|
135
|
+
{
|
|
136
|
+
gsl_combination *c = NULL;
|
|
137
|
+
Data_Get_Struct(obj, gsl_combination, c);
|
|
138
|
+
if(gsl_combination_valid(c)) return Qtrue;
|
|
139
|
+
else return Qfalse;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
static VALUE rb_gsl_combination_next(VALUE obj)
|
|
143
|
+
{
|
|
144
|
+
gsl_combination *c = NULL;
|
|
145
|
+
Data_Get_Struct(obj, gsl_combination, c);
|
|
146
|
+
return INT2FIX(gsl_combination_next(c));
|
|
147
|
+
}
|
|
148
|
+
static VALUE rb_gsl_combination_prev(VALUE obj)
|
|
149
|
+
{
|
|
150
|
+
gsl_combination *c = NULL;
|
|
151
|
+
Data_Get_Struct(obj, gsl_combination, c);
|
|
152
|
+
return INT2FIX(gsl_combination_prev(c));
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
static VALUE rb_gsl_combination_fwrite(VALUE obj, VALUE io)
|
|
156
|
+
{
|
|
157
|
+
gsl_combination *h = NULL;
|
|
158
|
+
FILE *f = NULL;
|
|
159
|
+
int status, flag = 0;
|
|
160
|
+
Data_Get_Struct(obj, gsl_combination, h);
|
|
161
|
+
f = rb_gsl_open_writefile(io, &flag);
|
|
162
|
+
status = gsl_combination_fwrite(f, h);
|
|
163
|
+
if (flag == 1) fclose(f);
|
|
164
|
+
return INT2FIX(status);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
static VALUE rb_gsl_combination_fread(VALUE obj, VALUE io)
|
|
168
|
+
{
|
|
169
|
+
gsl_combination *h = NULL;
|
|
170
|
+
FILE *f = NULL;
|
|
171
|
+
int status, flag = 0;
|
|
172
|
+
|
|
173
|
+
Data_Get_Struct(obj, gsl_combination, h);
|
|
174
|
+
f = rb_gsl_open_readfile(io, &flag);
|
|
175
|
+
status = gsl_combination_fread(f, h);
|
|
176
|
+
if (flag == 1) fclose(f);
|
|
177
|
+
return INT2FIX(status);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
static VALUE rb_gsl_combination_fprintf(int argc, VALUE *argv, VALUE obj)
|
|
181
|
+
{
|
|
182
|
+
gsl_combination *h = NULL;
|
|
183
|
+
FILE *fp = NULL;
|
|
184
|
+
int status, flag = 0;
|
|
185
|
+
|
|
186
|
+
if (argc != 1 && argc != 2) rb_raise(rb_eArgError,
|
|
187
|
+
"wrong number of arguments (%d for 1 or 2)", argc);
|
|
188
|
+
|
|
189
|
+
Data_Get_Struct(obj, gsl_combination, h);
|
|
190
|
+
fp = rb_gsl_open_writefile(argv[0], &flag);
|
|
191
|
+
switch (argc) {
|
|
192
|
+
case 1:
|
|
193
|
+
status = gsl_combination_fprintf(fp, h, "%u\n");
|
|
194
|
+
break;
|
|
195
|
+
default:
|
|
196
|
+
Check_Type(argv[1], T_STRING);
|
|
197
|
+
status = gsl_combination_fprintf(fp, h, STR2CSTR(argv[1]));
|
|
198
|
+
break;
|
|
199
|
+
}
|
|
200
|
+
if (flag == 1) fclose(fp);
|
|
201
|
+
return INT2FIX(status);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
static VALUE rb_gsl_combination_printf(int argc, VALUE *argv, VALUE obj)
|
|
205
|
+
{
|
|
206
|
+
gsl_combination *h = NULL;
|
|
207
|
+
int status;
|
|
208
|
+
Data_Get_Struct(obj, gsl_combination, h);
|
|
209
|
+
switch (argc) {
|
|
210
|
+
case 0:
|
|
211
|
+
status = gsl_combination_fprintf(stdout, h, "%u\n");
|
|
212
|
+
break;
|
|
213
|
+
default:
|
|
214
|
+
Check_Type(argv[0], T_STRING);
|
|
215
|
+
status = gsl_combination_fprintf(stdout, h, STR2CSTR(argv[0]));
|
|
216
|
+
break;
|
|
217
|
+
}
|
|
218
|
+
return INT2FIX(status);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
static VALUE rb_gsl_combination_fscanf(VALUE obj, VALUE io)
|
|
223
|
+
{
|
|
224
|
+
gsl_combination *h = NULL;
|
|
225
|
+
FILE *f = NULL;
|
|
226
|
+
int status, flag = 0;
|
|
227
|
+
Data_Get_Struct(obj, gsl_combination, h);
|
|
228
|
+
f = rb_gsl_open_readfile(io, &flag);
|
|
229
|
+
status = gsl_combination_fscanf(f, h);
|
|
230
|
+
if (flag == 1) fclose(f);
|
|
231
|
+
return INT2FIX(status);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
static VALUE rb_gsl_combination_equal(VALUE obj, VALUE other)
|
|
235
|
+
{
|
|
236
|
+
gsl_combination *p1 = NULL, *p2 = NULL;
|
|
237
|
+
size_t i;
|
|
238
|
+
Data_Get_Struct(obj, gsl_combination, p1);
|
|
239
|
+
Data_Get_Struct(other, gsl_combination, p2);
|
|
240
|
+
if (p1->k != p2->k) return Qfalse;
|
|
241
|
+
for (i = 0; i < p1->k; i++)
|
|
242
|
+
if (p1->data[i] != p2->data[i]) return Qfalse;
|
|
243
|
+
return Qtrue;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
void Init_gsl_combination(VALUE module)
|
|
247
|
+
{
|
|
248
|
+
VALUE cgsl_combination;
|
|
249
|
+
cgsl_combination = rb_define_class_under(module, "Combination", cGSL_Object);
|
|
250
|
+
cgsl_combination_data = rb_define_class_under(cgsl_combination, "Data",
|
|
251
|
+
cgsl_permutation);
|
|
252
|
+
rb_define_singleton_method(cgsl_combination, "new", rb_gsl_combination_new, 2);
|
|
253
|
+
rb_define_singleton_method(cgsl_combination, "alloc", rb_gsl_combination_new, 2);
|
|
254
|
+
rb_define_singleton_method(cgsl_combination, "calloc", rb_gsl_combination_calloc, 2);
|
|
255
|
+
rb_define_method(cgsl_combination, "init_first", rb_gsl_combination_init_first, 0);
|
|
256
|
+
rb_define_method(cgsl_combination, "init_last", rb_gsl_combination_init_last, 0);
|
|
257
|
+
#ifdef GSL_1_4_LATER
|
|
258
|
+
rb_define_singleton_method(cgsl_combination, "memcpy", rb_gsl_combination_memcpy, 2);
|
|
259
|
+
rb_define_method(cgsl_combination, "clone", rb_gsl_combination_clone, 0);
|
|
260
|
+
#endif
|
|
261
|
+
rb_define_method(cgsl_combination, "get", rb_gsl_combination_get, 1);
|
|
262
|
+
rb_define_alias(cgsl_combination, "[]", "get");
|
|
263
|
+
rb_define_method(cgsl_combination, "set", rb_gsl_combination_set, 2);
|
|
264
|
+
rb_define_alias(cgsl_combination, "[]=", "set");
|
|
265
|
+
|
|
266
|
+
rb_define_method(cgsl_combination, "n", rb_gsl_combination_n, 0);
|
|
267
|
+
rb_define_method(cgsl_combination, "k", rb_gsl_combination_k, 0);
|
|
268
|
+
rb_define_method(cgsl_combination, "data", rb_gsl_combination_data, 0);
|
|
269
|
+
rb_define_method(cgsl_combination, "valid", rb_gsl_combination_valid, 0);
|
|
270
|
+
rb_define_method(cgsl_combination, "valid?", rb_gsl_combination_valid2, 0);
|
|
271
|
+
rb_define_method(cgsl_combination, "next", rb_gsl_combination_next, 0);
|
|
272
|
+
rb_define_method(cgsl_combination, "prev", rb_gsl_combination_prev, 0);
|
|
273
|
+
|
|
274
|
+
rb_define_method(cgsl_combination, "fwrite", rb_gsl_combination_fwrite, 1);
|
|
275
|
+
rb_define_method(cgsl_combination, "fread", rb_gsl_combination_fread, 1);
|
|
276
|
+
rb_define_method(cgsl_combination, "fprintf", rb_gsl_combination_fprintf, -1);
|
|
277
|
+
rb_define_method(cgsl_combination, "printf", rb_gsl_combination_printf, -1);
|
|
278
|
+
rb_define_method(cgsl_combination, "fscanf", rb_gsl_combination_fscanf, 1);
|
|
279
|
+
|
|
280
|
+
rb_define_method(cgsl_combination, "equal?", rb_gsl_combination_equal, 1);
|
|
281
|
+
rb_define_alias(cgsl_combination, "==", "equal?");
|
|
282
|
+
}
|
|
283
|
+
#endif
|
data/ext/common.c
ADDED
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
/*
|
|
2
|
+
common.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
|
+
|
|
14
|
+
#include "rb_gsl_array.h"
|
|
15
|
+
#include "rb_gsl_histogram.h"
|
|
16
|
+
#include <string.h>
|
|
17
|
+
#include <ctype.h>
|
|
18
|
+
|
|
19
|
+
FILE* rb_gsl_open_writefile(VALUE io, int *flag)
|
|
20
|
+
{
|
|
21
|
+
#ifdef RUBY_1_9_LATER
|
|
22
|
+
rb_io_t *fptr = NULL;
|
|
23
|
+
#else
|
|
24
|
+
OpenFile *fptr = NULL;
|
|
25
|
+
#endif
|
|
26
|
+
FILE *fp = NULL;
|
|
27
|
+
char *name;
|
|
28
|
+
switch (TYPE(io)) {
|
|
29
|
+
case T_STRING:
|
|
30
|
+
name = RSTRING_PTR(io);
|
|
31
|
+
fp = fopen(name, "w");
|
|
32
|
+
*flag = 1;
|
|
33
|
+
break;
|
|
34
|
+
case T_FILE:
|
|
35
|
+
GetOpenFile(io, fptr);
|
|
36
|
+
/*
|
|
37
|
+
#ifdef RUBY_1_9_LATER
|
|
38
|
+
name = STR2CSTR(fptr->pathv);
|
|
39
|
+
#else
|
|
40
|
+
name = fptr->path;
|
|
41
|
+
#endif
|
|
42
|
+
*/
|
|
43
|
+
rb_io_check_writable(fptr);
|
|
44
|
+
#ifdef RUBY_1_9_LATER
|
|
45
|
+
fp = rb_io_stdio_file(fptr);
|
|
46
|
+
#else
|
|
47
|
+
fp = GetWriteFile(fptr);
|
|
48
|
+
#endif
|
|
49
|
+
*flag = 0;
|
|
50
|
+
break;
|
|
51
|
+
default:
|
|
52
|
+
rb_raise(rb_eTypeError, "argv 1 String or File expected");
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
// if (fp == NULL) rb_raise(rb_eIOError, "Cannot open file %s.", name);
|
|
56
|
+
if (fp == NULL) rb_raise(rb_eIOError, "Cannot open file.");
|
|
57
|
+
return fp;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
FILE* rb_gsl_open_readfile(VALUE io, int *flag)
|
|
61
|
+
{
|
|
62
|
+
#ifdef RUBY_1_9_LATER
|
|
63
|
+
rb_io_t *fptr = NULL;
|
|
64
|
+
#else
|
|
65
|
+
OpenFile *fptr = NULL;
|
|
66
|
+
#endif
|
|
67
|
+
FILE *fp = NULL;
|
|
68
|
+
char *name;
|
|
69
|
+
switch (TYPE(io)) {
|
|
70
|
+
case T_STRING:
|
|
71
|
+
name = RSTRING_PTR(io);
|
|
72
|
+
fp = fopen(name, "r");
|
|
73
|
+
*flag = 1;
|
|
74
|
+
break;
|
|
75
|
+
case T_FILE:
|
|
76
|
+
GetOpenFile(io, fptr);
|
|
77
|
+
/*
|
|
78
|
+
#ifdef RUBY_1_9_LATER
|
|
79
|
+
name = STR2CSTR(fptr->pathv);
|
|
80
|
+
#else
|
|
81
|
+
name = fptr->path;
|
|
82
|
+
#endif
|
|
83
|
+
*/
|
|
84
|
+
rb_io_check_readable(fptr);
|
|
85
|
+
#ifdef RUBY_1_9_LATER
|
|
86
|
+
fp = rb_io_stdio_file(fptr);
|
|
87
|
+
#else
|
|
88
|
+
fp = fptr->f;
|
|
89
|
+
#endif
|
|
90
|
+
*flag = 0;
|
|
91
|
+
break;
|
|
92
|
+
default:
|
|
93
|
+
rb_raise(rb_eTypeError, "argv 1 String or File expected");
|
|
94
|
+
break;
|
|
95
|
+
}
|
|
96
|
+
// if (fp == NULL) rb_raise(rb_eIOError, "Cannot open file %s.", name);
|
|
97
|
+
if (fp == NULL) rb_raise(rb_eIOError, "Cannot open file");
|
|
98
|
+
return fp;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
VALUE rb_gsl_obj_read_only(int argc, VALUE *argv, VALUE obj)
|
|
102
|
+
{
|
|
103
|
+
rb_raise(rb_eRuntimeError, "Read only object.");
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
int str_tail_grep(const char *s0, const char *s1)
|
|
107
|
+
{
|
|
108
|
+
int len0, len1;
|
|
109
|
+
char *p = NULL;
|
|
110
|
+
len0 = strlen(s0);
|
|
111
|
+
len1 = strlen(s1);
|
|
112
|
+
p = (char *) s0 + len0 - len1;
|
|
113
|
+
return strcmp(p, s1);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
int str_head_grep(const char *s0, const char *s1)
|
|
117
|
+
{
|
|
118
|
+
int len0, len1;
|
|
119
|
+
size_t i, len;
|
|
120
|
+
char *p0, *p1;
|
|
121
|
+
len0 = strlen(s0);
|
|
122
|
+
len1 = strlen(s1);
|
|
123
|
+
len = (size_t) GSL_MIN_INT(len0, len1);
|
|
124
|
+
p0 = (char *) s0;
|
|
125
|
+
p1 = (char *) s1;
|
|
126
|
+
for (i = 0; i < len; i++) if (*p0++ != *p1++) return 1;
|
|
127
|
+
return 0;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
size_t count_columns(const char *str)
|
|
131
|
+
{
|
|
132
|
+
size_t n = 0;
|
|
133
|
+
int flag = 1;
|
|
134
|
+
char *p;
|
|
135
|
+
p = (char *) str;
|
|
136
|
+
do {
|
|
137
|
+
if (isspace(*p)) {
|
|
138
|
+
flag = 1;
|
|
139
|
+
} else {
|
|
140
|
+
if (flag == 1) {
|
|
141
|
+
flag = 0;
|
|
142
|
+
n++;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
p++;
|
|
146
|
+
} while (*p != '\0' && *p != '\n');
|
|
147
|
+
return n;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
char* str_scan_double(const char *str, double *val)
|
|
151
|
+
{
|
|
152
|
+
char buf[256];
|
|
153
|
+
char *p, *q;
|
|
154
|
+
double x;
|
|
155
|
+
int flag = 0;
|
|
156
|
+
p = (char *) str;
|
|
157
|
+
q = buf;
|
|
158
|
+
do {
|
|
159
|
+
if (isspace(*p)) {
|
|
160
|
+
if (flag == 0) {
|
|
161
|
+
/* do nothing */
|
|
162
|
+
} else {
|
|
163
|
+
break;
|
|
164
|
+
}
|
|
165
|
+
} else {
|
|
166
|
+
*q++ = *p;
|
|
167
|
+
flag = 1;
|
|
168
|
+
}
|
|
169
|
+
p++;
|
|
170
|
+
} while (*p != '\0' && *p != '\n');
|
|
171
|
+
if (flag == 0) {
|
|
172
|
+
*val = 0;
|
|
173
|
+
return NULL;
|
|
174
|
+
}
|
|
175
|
+
*q = '\0';
|
|
176
|
+
flag = sscanf(buf, "%lf", &x);
|
|
177
|
+
if (flag == 1) {
|
|
178
|
+
*val = x;
|
|
179
|
+
return p;
|
|
180
|
+
} else {
|
|
181
|
+
*val = 0;
|
|
182
|
+
return NULL;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
char* str_scan_int(const char *str, int *val)
|
|
187
|
+
{
|
|
188
|
+
char buf[256];
|
|
189
|
+
char *p, *q;
|
|
190
|
+
int x;
|
|
191
|
+
int flag = 0;
|
|
192
|
+
p = (char *) str;
|
|
193
|
+
q = buf;
|
|
194
|
+
do {
|
|
195
|
+
if (isspace(*p)) {
|
|
196
|
+
if (flag == 0) {
|
|
197
|
+
/* do nothing */
|
|
198
|
+
} else {
|
|
199
|
+
break;
|
|
200
|
+
}
|
|
201
|
+
} else {
|
|
202
|
+
*q++ = *p;
|
|
203
|
+
flag = 1;
|
|
204
|
+
}
|
|
205
|
+
p++;
|
|
206
|
+
} while (*p != '\0' && *p != '\n');
|
|
207
|
+
if (flag == 0) {
|
|
208
|
+
*val = 0;
|
|
209
|
+
return NULL;
|
|
210
|
+
}
|
|
211
|
+
*q = '\0';
|
|
212
|
+
flag = sscanf(buf, "%d", &x);
|
|
213
|
+
if (flag == 1) {
|
|
214
|
+
*val = x;
|
|
215
|
+
return p;
|
|
216
|
+
} else {
|
|
217
|
+
*val = 0;
|
|
218
|
+
return NULL;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
double* get_ptr_double3(VALUE obj, size_t *size, size_t *stride, int *flag)
|
|
223
|
+
{
|
|
224
|
+
gsl_vector *v;
|
|
225
|
+
#ifdef HAVE_NARRAY_H
|
|
226
|
+
double *ptr;
|
|
227
|
+
struct NARRAY *na;
|
|
228
|
+
if (NA_IsNArray(obj)) {
|
|
229
|
+
obj = na_change_type(obj, NA_DFLOAT);
|
|
230
|
+
GetNArray(obj, na);
|
|
231
|
+
ptr = (double *) na->ptr;
|
|
232
|
+
*size = na->total;
|
|
233
|
+
*stride = 1;
|
|
234
|
+
*flag = 1;
|
|
235
|
+
return ptr;
|
|
236
|
+
}
|
|
237
|
+
#endif
|
|
238
|
+
CHECK_VECTOR(obj);
|
|
239
|
+
Data_Get_Struct(obj, gsl_vector, v);
|
|
240
|
+
*size = v->size;
|
|
241
|
+
*stride = v->stride;
|
|
242
|
+
*flag = 0;
|
|
243
|
+
return v->data;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
gsl_complex ary2complex(VALUE obj)
|
|
247
|
+
{
|
|
248
|
+
gsl_complex *z, c;
|
|
249
|
+
switch (TYPE(obj)) {
|
|
250
|
+
case T_ARRAY:
|
|
251
|
+
GSL_SET_REAL(&c, NUM2DBL(rb_ary_entry(obj, 0)));
|
|
252
|
+
GSL_SET_IMAG(&c, NUM2DBL(rb_ary_entry(obj, 1)));
|
|
253
|
+
break;
|
|
254
|
+
default:
|
|
255
|
+
if (COMPLEX_P(obj)) {
|
|
256
|
+
Data_Get_Struct(obj, gsl_complex, z);
|
|
257
|
+
c = *z;
|
|
258
|
+
} else {
|
|
259
|
+
rb_raise(rb_eTypeError, "wrong argument type %s (Array or Complex expected)",
|
|
260
|
+
rb_class2name(CLASS_OF(obj)));
|
|
261
|
+
}
|
|
262
|
+
break;
|
|
263
|
+
}
|
|
264
|
+
return c;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
VALUE vector_eval_create(VALUE obj, double (*func)(double))
|
|
268
|
+
{
|
|
269
|
+
gsl_vector *vnew;
|
|
270
|
+
size_t i, size, stride;
|
|
271
|
+
double *ptr;
|
|
272
|
+
ptr = get_vector_ptr(obj, &stride, &size);
|
|
273
|
+
vnew = gsl_vector_alloc(size);
|
|
274
|
+
for (i = 0; i < size; i++) {
|
|
275
|
+
gsl_vector_set(vnew, i, (*func)(ptr[i*stride]));
|
|
276
|
+
}
|
|
277
|
+
return Data_Wrap_Struct(cgsl_vector, 0, gsl_vector_free, vnew);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
VALUE matrix_eval_create(VALUE obj, double (*func)(double))
|
|
281
|
+
{
|
|
282
|
+
gsl_matrix *m, *mnew;
|
|
283
|
+
size_t i, j;
|
|
284
|
+
Data_Get_Struct(obj, gsl_matrix, m);
|
|
285
|
+
mnew = gsl_matrix_alloc(m->size1, m->size2);
|
|
286
|
+
for (i = 0; i < m->size1; i++) {
|
|
287
|
+
for (j = 0; j < m->size2; j++) {
|
|
288
|
+
gsl_matrix_set(mnew, i, j, (*func)(gsl_matrix_get(m, i, j)));
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
return Data_Wrap_Struct(cgsl_matrix, 0, gsl_matrix_free, mnew);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
VALUE rb_gsl_ary_eval1(VALUE ary, double (*f)(double))
|
|
295
|
+
{
|
|
296
|
+
VALUE ary2;
|
|
297
|
+
size_t i, n;
|
|
298
|
+
double val;
|
|
299
|
+
// n = RARRAY(ary)->len;
|
|
300
|
+
n = RARRAY_LEN(ary);
|
|
301
|
+
ary2 = rb_ary_new2(n);
|
|
302
|
+
for (i = 0; i < n; i++) {
|
|
303
|
+
val = (*f)(NUM2DBL(rb_ary_entry(ary, i)));
|
|
304
|
+
rb_ary_store(ary2, i, rb_float_new(val));
|
|
305
|
+
}
|
|
306
|
+
return ary2;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
#ifdef HAVE_NARRAY_H
|
|
310
|
+
VALUE rb_gsl_nary_eval1(VALUE ary, double (*f)(double))
|
|
311
|
+
{
|
|
312
|
+
VALUE ary2;
|
|
313
|
+
struct NARRAY *na;
|
|
314
|
+
double *ptr1, *ptr2;
|
|
315
|
+
size_t i, n;
|
|
316
|
+
ary = na_change_type(ary, NA_DFLOAT);
|
|
317
|
+
GetNArray(ary, na);
|
|
318
|
+
ptr1 = (double *) na->ptr;
|
|
319
|
+
n = na->total;
|
|
320
|
+
ary2 = na_make_object(NA_DFLOAT, na->rank, na->shape, CLASS_OF(ary));
|
|
321
|
+
ptr2 = NA_PTR_TYPE(ary2, double*);
|
|
322
|
+
for (i = 0; i < n; i++) ptr2[i] = (*f)(ptr1[i]);
|
|
323
|
+
return ary2;
|
|
324
|
+
}
|
|
325
|
+
#endif
|