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,253 @@
1
+ /* matrix/oper_complex_source.c
2
+ *
3
+ * Copyright (C) 1996, 1997, 1998, 1999, 2000 Brian Gough
4
+ *
5
+ * This program is free software; you can redistribute it and/or modify
6
+ * it under the terms of the GNU General Public License as published by
7
+ * the Free Software Foundation; either version 2 of the License, or (at
8
+ * your option) any later version.
9
+ *
10
+ * This program is distributed in the hope that it will be useful, but
11
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
+ * General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU General Public License
16
+ * along with this program; if not, write to the Free Software
17
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
+ */
19
+
20
+ #include "rb_gsl_config.h"
21
+
22
+ #ifndef GSL_1_2_LATER
23
+ #include <config.h>
24
+ #include <stdlib.h>
25
+ #include <math.h>
26
+ #include <gsl/gsl_math.h>
27
+ #include <gsl/gsl_matrix.h>
28
+
29
+ #define BASE_GSL_COMPLEX
30
+ #include "templates_on.h"
31
+
32
+ int
33
+ FUNCTION (gsl_matrix, add) (GSL_TYPE (gsl_matrix) * a,
34
+ const GSL_TYPE (gsl_matrix) * b)
35
+ {
36
+ const size_t M = a->size1;
37
+ const size_t N = a->size2;
38
+
39
+ if (b->size1 != M || b->size2 != N)
40
+ {
41
+ GSL_ERROR ("matrices must have same dimensions", GSL_EBADLEN);
42
+ }
43
+ else
44
+ {
45
+ const size_t tda_a = a->tda;
46
+ const size_t tda_b = b->tda;
47
+
48
+ size_t i, j;
49
+
50
+ for (i = 0; i < M; i++)
51
+ {
52
+ for (j = 0; j < N; j++)
53
+ {
54
+ const size_t aij = 2 * (i * tda_a + j);
55
+ const size_t bij = 2 * (i * tda_b + j);
56
+
57
+ a->data[aij] += b->data[bij];
58
+ a->data[aij + 1] += b->data[bij + 1];
59
+ }
60
+ }
61
+
62
+ return GSL_SUCCESS;
63
+ }
64
+ }
65
+
66
+ int
67
+ FUNCTION (gsl_matrix, sub) (GSL_TYPE (gsl_matrix) * a,
68
+ const GSL_TYPE (gsl_matrix) * b)
69
+ {
70
+ const size_t M = a->size1;
71
+ const size_t N = a->size2;
72
+
73
+ if (b->size1 != M || b->size2 != N)
74
+ {
75
+ GSL_ERROR ("matrices must have same dimensions", GSL_EBADLEN);
76
+ }
77
+ else
78
+ {
79
+ const size_t tda_a = a->tda;
80
+ const size_t tda_b = b->tda;
81
+
82
+ size_t i, j;
83
+
84
+ for (i = 0; i < M; i++)
85
+ {
86
+ for (j = 0; j < N; j++)
87
+ {
88
+ const size_t aij = 2 * (i * tda_a + j);
89
+ const size_t bij = 2 * (i * tda_b + j);
90
+
91
+ a->data[aij] -= b->data[bij];
92
+ a->data[aij + 1] -= b->data[bij + 1];
93
+ }
94
+ }
95
+
96
+ return GSL_SUCCESS;
97
+ }
98
+ }
99
+
100
+ int
101
+ FUNCTION (gsl_matrix, mul_elements) (GSL_TYPE (gsl_matrix) * a,
102
+ const GSL_TYPE (gsl_matrix) * b)
103
+ {
104
+ const size_t M = a->size1;
105
+ const size_t N = a->size2;
106
+
107
+ if (b->size1 != M || b->size2 != N)
108
+ {
109
+ GSL_ERROR ("matrices must have same dimensions", GSL_EBADLEN);
110
+ }
111
+ else
112
+ {
113
+ const size_t tda_a = a->tda;
114
+ const size_t tda_b = b->tda;
115
+
116
+ size_t i, j;
117
+
118
+ for (i = 0; i < M; i++)
119
+ {
120
+ for (j = 0; j < N; j++)
121
+ {
122
+ const size_t aij = 2 * (i * tda_a + j);
123
+ const size_t bij = 2 * (i * tda_b + j);
124
+
125
+ ATOMIC ar = a->data[aij];
126
+ ATOMIC ai = a->data[aij + 1];
127
+
128
+ ATOMIC br = b->data[bij];
129
+ ATOMIC bi = b->data[bij + 1];
130
+
131
+ a->data[aij] = ar * br - ai * bi;
132
+ a->data[aij + 1] = ar * bi + ai * br;
133
+ }
134
+ }
135
+
136
+ return GSL_SUCCESS;
137
+ }
138
+ }
139
+
140
+ int
141
+ FUNCTION (gsl_matrix, div_elements) (GSL_TYPE (gsl_matrix) * a,
142
+ const GSL_TYPE (gsl_matrix) * b)
143
+ {
144
+ const size_t M = a->size1;
145
+ const size_t N = a->size2;
146
+
147
+ if (b->size1 != M || b->size2 != N)
148
+ {
149
+ GSL_ERROR ("matrices must have same dimensions", GSL_EBADLEN);
150
+ }
151
+ else
152
+ {
153
+ const size_t tda_a = a->tda;
154
+ const size_t tda_b = b->tda;
155
+
156
+ size_t i, j;
157
+
158
+ for (i = 0; i < M; i++)
159
+ {
160
+ for (j = 0; j < N; j++)
161
+ {
162
+ const size_t aij = 2 * (i * tda_a + j);
163
+ const size_t bij = 2 * (i * tda_b + j);
164
+
165
+ ATOMIC ar = a->data[aij];
166
+ ATOMIC ai = a->data[aij + 1];
167
+
168
+ ATOMIC br = b->data[bij];
169
+ ATOMIC bi = b->data[bij + 1];
170
+
171
+ ATOMIC s = 1.0 / hypot(br, bi);
172
+
173
+ ATOMIC sbr = s * br;
174
+ ATOMIC sbi = s * bi;
175
+
176
+ a->data[aij] = (ar * sbr + ai * sbi) * s;
177
+ a->data[aij + 1] = (ai * sbr - ar * sbi) * s;
178
+ }
179
+ }
180
+
181
+ return GSL_SUCCESS;
182
+ }
183
+ }
184
+
185
+ int FUNCTION (gsl_matrix, scale) (GSL_TYPE (gsl_matrix) * a, const BASE x)
186
+ {
187
+ const size_t M = a->size1;
188
+ const size_t N = a->size2;
189
+ const size_t tda = a->tda;
190
+
191
+ size_t i, j;
192
+
193
+ ATOMIC xr = GSL_REAL(x);
194
+ ATOMIC xi = GSL_IMAG(x);
195
+
196
+ for (i = 0; i < M; i++)
197
+ {
198
+ for (j = 0; j < N; j++)
199
+ {
200
+ const size_t aij = 2 * (i * tda + j);
201
+
202
+ ATOMIC ar = a->data[aij];
203
+ ATOMIC ai = a->data[aij + 1];
204
+
205
+ a->data[aij] = ar * xr - ai * xi;
206
+ a->data[aij + 1] = ar * xi + ai * xr;
207
+ }
208
+ }
209
+
210
+ return GSL_SUCCESS;
211
+ }
212
+
213
+ int FUNCTION (gsl_matrix, add_constant) (GSL_TYPE (gsl_matrix) * a, const BASE x)
214
+ {
215
+ const size_t M = a->size1;
216
+ const size_t N = a->size2;
217
+ const size_t tda = a->tda;
218
+
219
+ size_t i, j;
220
+
221
+ for (i = 0; i < M; i++)
222
+ {
223
+ for (j = 0; j < N; j++)
224
+ {
225
+ a->data[2 * (i * tda + j)] += GSL_REAL (x);
226
+ a->data[2 * (i * tda + j) + 1] += GSL_IMAG (x);
227
+ }
228
+ }
229
+
230
+ return GSL_SUCCESS;
231
+ }
232
+
233
+
234
+ int FUNCTION (gsl_matrix, add_diagonal) (GSL_TYPE (gsl_matrix) * a, const BASE x)
235
+ {
236
+ const size_t M = a->size1;
237
+ const size_t N = a->size2;
238
+ const size_t tda = a->tda;
239
+ const size_t loop_lim = (M < N ? M : N);
240
+ size_t i;
241
+ for (i = 0; i < loop_lim; i++)
242
+ {
243
+ a->data[2 * (i * tda + i)] += GSL_REAL (x);
244
+ a->data[2 * (i * tda + i) + 1] += GSL_IMAG (x);
245
+ }
246
+
247
+ return GSL_SUCCESS;
248
+ }
249
+
250
+
251
+ #include "templates_off.h"
252
+ #undef BASE_GSL_COMPLEX
253
+ #endif
@@ -0,0 +1,596 @@
1
+ /*
2
+ permutation.c
3
+ Ruby/GSL: Ruby extension library for GSL (GNU Scientific Library)
4
+ (C) Copyright 2001-2006 by Yoshiki Tsunesada
5
+
6
+ Ruby/GSL is free software: you can redistribute it and/or modify it
7
+ under the terms of the GNU General Public License.
8
+ This library is distributed in the hope that it will be useful, but
9
+ WITHOUT ANY WARRANTY.
10
+ */
11
+ #include "rb_gsl_config.h"
12
+ #include "rb_gsl_array.h"
13
+ #include <gsl/gsl_permute.h>
14
+ #include <gsl/gsl_permute_vector.h>
15
+
16
+ VALUE rb_gsl_permutation_alloc(VALUE klass, VALUE nn);
17
+
18
+ /*
19
+ * Creates a new permutation of size n. The permutation is not initialized
20
+ * and its elements are undefined. Use the method calloc if you want to create
21
+ * a permutation which is initialized to the identity.
22
+ */
23
+ VALUE rb_gsl_permutation_alloc(VALUE klass, VALUE nn)
24
+ {
25
+ gsl_permutation *p = NULL;
26
+ CHECK_FIXNUM(nn);
27
+ p = gsl_permutation_calloc(FIX2INT(nn));
28
+ return Data_Wrap_Struct(klass, 0, gsl_permutation_free, p);
29
+ }
30
+
31
+ static VALUE rb_gsl_permutation_calloc(VALUE klass, VALUE nn)
32
+ {
33
+ gsl_permutation *p = NULL;
34
+ CHECK_FIXNUM(nn);
35
+ p = gsl_permutation_calloc(FIX2INT(nn));
36
+ return Data_Wrap_Struct(klass, 0, gsl_permutation_free, p);
37
+ }
38
+
39
+ static VALUE rb_gsl_permutation_size(VALUE obj)
40
+ {
41
+ gsl_permutation *p = NULL;
42
+ Data_Get_Struct(obj, gsl_permutation, p);
43
+ return INT2FIX(p->size);
44
+ }
45
+
46
+ static VALUE rb_gsl_permutation_init(VALUE obj)
47
+ {
48
+ gsl_permutation *p = NULL;
49
+ Data_Get_Struct(obj, gsl_permutation, p);
50
+ gsl_permutation_init(p);
51
+ return obj;
52
+ }
53
+
54
+ void get_range_int_beg_en_n(VALUE range, int *beg, int *en, size_t *n, int *step);
55
+ static VALUE rb_gsl_permutation_get(int argc, VALUE *argv, VALUE obj)
56
+ {
57
+ gsl_permutation *b, *bnew;
58
+ gsl_index *p;
59
+ int beg, en, i, step;
60
+ size_t n, j, k;
61
+ Data_Get_Struct(obj, gsl_permutation, b);
62
+ switch (argc) {
63
+ case 0:
64
+ rb_raise(rb_eArgError, "too few arguments (%d for >= 1)", argc);
65
+ break;
66
+ case 1:
67
+ switch (TYPE(argv[0])) {
68
+ case T_FIXNUM:
69
+ i = FIX2INT(argv[0]);
70
+ if (i < 0) j = b->size + i; else j = (size_t) i;
71
+ return INT2FIX((int) b->data[j]);
72
+ break;
73
+ case T_ARRAY:
74
+ // n = RARRAY(argv[0])->len;
75
+ n = RARRAY_LEN(argv[0]);
76
+ bnew = gsl_permutation_alloc(n);
77
+ for (j = 0; j < n; j++) {
78
+ i = FIX2INT(rb_ary_entry(argv[0], j));
79
+ if (i < 0) k = b->size + i; else k = i;
80
+ bnew->data[j] = b->data[k];
81
+ }
82
+ return Data_Wrap_Struct(CLASS_OF(argv[0]), 0, gsl_permutation_free, bnew);
83
+ break;
84
+ default:
85
+ if (PERMUTATION_P(argv[0])) {
86
+ Data_Get_Struct(argv[0], gsl_index, p);
87
+ bnew = gsl_permutation_alloc(p->size);
88
+ for (j = 0; j < p->size; j++) bnew->data[j] = b->data[p->data[j]];
89
+ return Data_Wrap_Struct(CLASS_OF(argv[0]), 0, gsl_permutation_free, bnew);
90
+ } else if (CLASS_OF(argv[0]) == rb_cRange) {
91
+ get_range_int_beg_en_n(argv[0], &beg, &en, &n, &step);
92
+ bnew = gsl_permutation_alloc(n);
93
+ for (j = 0; j < n; j++)
94
+ bnew->data[j] = b->data[beg+j];
95
+ return Data_Wrap_Struct(CLASS_OF(obj), 0, gsl_permutation_free, bnew);
96
+ } else {
97
+ rb_raise(rb_eArgError, "wrong argument type %s (Fixnum, Array, or Range expected)", rb_class2name(CLASS_OF(argv[0])));
98
+ break;
99
+ }
100
+ }
101
+ break;
102
+ default:
103
+ bnew = gsl_permutation_alloc(argc);
104
+ for (j = 0; j < argc; j++) {
105
+ i = FIX2INT(argv[j]);
106
+ if (i < 0) k = b->size + i; else k = i;
107
+ bnew->data[j] = b->data[k];
108
+ }
109
+ return Data_Wrap_Struct(CLASS_OF(argv[0]), 0, gsl_permutation_free, bnew);
110
+ break;
111
+ }
112
+ return Qnil;
113
+ }
114
+
115
+ #ifdef GSL_1_1_LATER
116
+ static VALUE rb_gsl_permutation_clone(VALUE obj)
117
+ {
118
+ gsl_permutation *p, *p2 = NULL;
119
+ Data_Get_Struct(obj, gsl_permutation, p);
120
+ p2 = gsl_permutation_alloc(p->size);
121
+ gsl_permutation_memcpy(p2, p);
122
+ return Data_Wrap_Struct(CLASS_OF(obj), 0, gsl_permutation_free, p2);
123
+ }
124
+
125
+ /* singleton */
126
+ static VALUE rb_gsl_permutation_memcpy(VALUE obj, VALUE pp1, VALUE pp2)
127
+ {
128
+ gsl_permutation *p1, *p2 = NULL;
129
+ CHECK_PERMUTATION(pp1);
130
+ CHECK_PERMUTATION(pp2);
131
+ Data_Get_Struct(pp1, gsl_permutation, p1);
132
+ Data_Get_Struct(pp2, gsl_permutation, p2);
133
+ gsl_permutation_memcpy(p1, p2);
134
+ return pp1;
135
+ }
136
+ #endif
137
+
138
+ static VALUE rb_gsl_permutation_swap(VALUE obj, VALUE i, VALUE j)
139
+ {
140
+ gsl_permutation *p = NULL;
141
+ CHECK_FIXNUM(i); CHECK_FIXNUM(j);
142
+ Data_Get_Struct(obj, gsl_permutation, p);
143
+ gsl_permutation_swap(p, FIX2INT(i), FIX2INT(j));
144
+ return obj;
145
+ }
146
+
147
+ static VALUE rb_gsl_permutation_valid(VALUE obj)
148
+ {
149
+ gsl_permutation *p = NULL;
150
+ Data_Get_Struct(obj, gsl_permutation, p);
151
+ return INT2FIX(gsl_permutation_valid(p));
152
+ }
153
+
154
+ static VALUE rb_gsl_permutation_valid2(VALUE obj)
155
+ {
156
+ gsl_permutation *p = NULL;
157
+ Data_Get_Struct(obj, gsl_permutation, p);
158
+ if(gsl_permutation_valid(p)) return Qtrue;
159
+ else return Qfalse;
160
+ }
161
+
162
+ /* to array */
163
+ static VALUE rb_gsl_permutation_to_a(VALUE obj)
164
+ {
165
+ gsl_permutation *p = NULL;
166
+ size_t i;
167
+ VALUE ary;
168
+ Data_Get_Struct(obj, gsl_permutation, p);
169
+ ary = rb_ary_new2(p->size);
170
+ for (i = 0; i < p->size; i++) {
171
+ rb_ary_store(ary, i, INT2FIX(gsl_permutation_get(p, i)));
172
+ }
173
+ return ary;
174
+ }
175
+
176
+ /* to vector */
177
+ static VALUE rb_gsl_permutation_to_v(VALUE obj)
178
+ {
179
+ gsl_permutation *p = NULL;
180
+ gsl_vector *v;
181
+ size_t size;
182
+ size_t i;
183
+ Data_Get_Struct(obj, gsl_permutation, p);
184
+ size = p->size;
185
+ v = gsl_vector_alloc(size);
186
+ for (i = 0; i < size; i++) {
187
+ gsl_vector_set(v, i, gsl_permutation_get(p, i));
188
+ }
189
+ return Data_Wrap_Struct(cgsl_vector, 0, gsl_vector_free, v);
190
+ }
191
+
192
+ static VALUE rb_gsl_permutation_reverse(VALUE obj)
193
+ {
194
+ gsl_permutation *p = NULL;
195
+ Data_Get_Struct(obj, gsl_permutation, p);
196
+ gsl_permutation_reverse(p);
197
+ return obj;
198
+ }
199
+
200
+ static VALUE rb_gsl_permutation_inverse(VALUE obj)
201
+ {
202
+ gsl_permutation *p, *inv;
203
+ Data_Get_Struct(obj, gsl_permutation, p);
204
+ inv = gsl_permutation_alloc(p->size);
205
+ gsl_permutation_inverse(inv, p);
206
+ return Data_Wrap_Struct(cgsl_permutation, 0, gsl_permutation_free, inv);
207
+ }
208
+
209
+ static VALUE rb_gsl_permutation_next(VALUE obj)
210
+ {
211
+ gsl_permutation *p = NULL;
212
+ Data_Get_Struct(obj, gsl_permutation, p);
213
+ return INT2FIX(gsl_permutation_next(p));
214
+ }
215
+
216
+ static VALUE rb_gsl_permutation_prev(VALUE obj)
217
+ {
218
+ gsl_permutation *p = NULL;
219
+ Data_Get_Struct(obj, gsl_permutation, p);
220
+ return INT2FIX(gsl_permutation_prev(p));
221
+ }
222
+
223
+ static VALUE rb_gsl_permutation_permute_vector(VALUE obj, VALUE vv)
224
+ {
225
+ gsl_permutation *p = NULL;
226
+ gsl_vector *v;
227
+ int status;
228
+ CHECK_VECTOR(vv);
229
+ Data_Get_Struct(obj, gsl_permutation, p);
230
+ Data_Get_Struct(vv, gsl_vector, v);
231
+ status = gsl_permute_vector(p, v);
232
+ return INT2FIX(status);
233
+ }
234
+
235
+ static VALUE rb_gsl_permutation_permute_vector_inverse(VALUE obj, VALUE vv)
236
+ {
237
+ gsl_permutation *p = NULL;
238
+ gsl_vector *v;
239
+ int status;
240
+ CHECK_VECTOR(vv);
241
+ Data_Get_Struct(obj, gsl_permutation, p);
242
+ Data_Get_Struct(vv, gsl_vector, v);
243
+ status = gsl_permute_vector_inverse(p, v);
244
+ return INT2FIX(status);
245
+ }
246
+
247
+ /* singleton */
248
+ static VALUE rb_gsl_permute_vector(VALUE obj, VALUE pp, VALUE vv)
249
+ {
250
+ gsl_permutation *p = NULL;
251
+ gsl_vector *v;
252
+ int status;
253
+ CHECK_VECTOR(vv);
254
+ Data_Get_Struct(pp, gsl_permutation, p);
255
+ Data_Get_Struct(vv, gsl_vector, v);
256
+ status = gsl_permute_vector(p, v);
257
+ return INT2FIX(status);
258
+ }
259
+
260
+ /* singleton */
261
+ static VALUE rb_gsl_permute_vector_inverse(VALUE obj, VALUE pp, VALUE vv)
262
+ {
263
+ gsl_permutation *p = NULL;
264
+ gsl_vector *v;
265
+ int status;
266
+ CHECK_VECTOR(vv);
267
+ Data_Get_Struct(pp, gsl_permutation, p);
268
+ Data_Get_Struct(vv, gsl_vector, v);
269
+ status = gsl_permute_vector_inverse(p, v);
270
+ return INT2FIX(status);
271
+ }
272
+
273
+ /* singleton method */
274
+ #ifdef GSL_1_2_LATER
275
+ static VALUE rb_gsl_permutation_mul(VALUE obj, VALUE ppa, VALUE ppb)
276
+ {
277
+ gsl_permutation *p = NULL;
278
+ gsl_permutation *pa = NULL, *pb = NULL;
279
+ int flag = 0;
280
+ CHECK_PERMUTATION(ppa);
281
+ CHECK_PERMUTATION(ppb);
282
+ Data_Get_Struct(ppa, gsl_permutation, pa);
283
+ Data_Get_Struct(ppb, gsl_permutation, pb);
284
+ if (rb_obj_is_kind_of(obj, cgsl_permutation)) {
285
+ Data_Get_Struct(obj, gsl_permutation, p);
286
+ flag = 1;
287
+ } else {
288
+ p = gsl_permutation_alloc(pa->size);
289
+ }
290
+ gsl_permutation_mul(p, pa, pb);
291
+ if (flag == 1) return obj;
292
+ else return Data_Wrap_Struct(cgsl_permutation, 0, gsl_permutation_free, p);
293
+ }
294
+ #endif
295
+
296
+ static VALUE rb_gsl_permutation_print(VALUE obj);
297
+
298
+ static VALUE rb_gsl_permutation_print(VALUE obj)
299
+ {
300
+ gsl_permutation *p = NULL;
301
+ size_t size, i;
302
+ Data_Get_Struct(obj, gsl_permutation, p);
303
+ size = p->size;
304
+ for (i = 0; i < size; i++) {
305
+ printf("%3d ", (int) gsl_permutation_get(p, i));
306
+ if ((i+1)%10 == 0) printf("\n");
307
+ }
308
+ printf("\n");
309
+ return obj;
310
+ }
311
+
312
+ static VALUE rb_gsl_permutation_to_s(VALUE obj)
313
+ {
314
+ gsl_permutation *v = NULL;
315
+ char buf[16];
316
+ size_t i;
317
+ VALUE str;
318
+ Data_Get_Struct(obj, gsl_permutation, v);
319
+ str = rb_str_new2("[");
320
+ for (i = 0; i < v->size; i++) {
321
+ sprintf(buf, " %d", (int) gsl_permutation_get(v, i));
322
+ rb_str_cat(str, buf, strlen(buf));
323
+ }
324
+ sprintf(buf, " ]");
325
+ rb_str_cat(str, buf, strlen(buf));
326
+ return str;
327
+ }
328
+
329
+ static VALUE rb_gsl_permutation_inspect(VALUE obj)
330
+ {
331
+ VALUE str;
332
+ char buf[64];
333
+ sprintf(buf, "%s\n", rb_class2name(CLASS_OF(obj)));
334
+ str = rb_str_new2(buf);
335
+ return rb_str_concat(str, rb_gsl_permutation_to_s(obj));
336
+ }
337
+
338
+ static VALUE rb_gsl_permutation_fwrite(VALUE obj, VALUE io)
339
+ {
340
+ gsl_permutation *h;
341
+ FILE *f = NULL;
342
+ int status, flag = 0;
343
+
344
+ Data_Get_Struct(obj, gsl_permutation, h);
345
+ f = rb_gsl_open_writefile(io, &flag);
346
+ status = gsl_permutation_fwrite(f, h);
347
+ if (flag == 1) fclose(f);
348
+ return INT2FIX(status);
349
+ }
350
+
351
+ static VALUE rb_gsl_permutation_fread(VALUE obj, VALUE io)
352
+ {
353
+ gsl_permutation *h;
354
+ FILE *f = NULL;
355
+ int status, flag = 0;
356
+
357
+ Data_Get_Struct(obj, gsl_permutation, h);
358
+ f = rb_gsl_open_readfile(io, &flag);
359
+ status = gsl_permutation_fread(f, h);
360
+ if (flag == 1) fclose(f);
361
+ return INT2FIX(status);
362
+ }
363
+
364
+ static VALUE rb_gsl_permutation_fprintf(int argc, VALUE *argv, VALUE obj)
365
+ {
366
+ gsl_permutation *h;
367
+ FILE *fp = NULL;
368
+ int status, flag = 0;
369
+
370
+ if (argc != 1 && argc != 2) rb_raise(rb_eArgError,
371
+ "wrong number of arguments (%d for 1 or 2)", argc);
372
+
373
+ Data_Get_Struct(obj, gsl_permutation, h);
374
+ fp = rb_gsl_open_writefile(argv[0], &flag);
375
+ if (argc == 1) {
376
+ status = gsl_permutation_fprintf(fp, h, "%u\n");
377
+ } else {
378
+ Check_Type(argv[1], T_STRING);
379
+ status = gsl_permutation_fprintf(fp, h, STR2CSTR(argv[1]));
380
+ }
381
+ if (flag == 1) fclose(fp);
382
+ return INT2FIX(status);
383
+ }
384
+
385
+ static VALUE rb_gsl_permutation_printf(int argc, VALUE *argv, VALUE obj)
386
+ {
387
+ gsl_permutation *h;
388
+ int status;
389
+
390
+ Data_Get_Struct(obj, gsl_permutation, h);
391
+ if (argc == 0) {
392
+ status = gsl_permutation_fprintf(stdout, h, "%u\n");
393
+ } else {
394
+ Check_Type(argv[0], T_STRING);
395
+ status = gsl_permutation_fprintf(stdout, h, STR2CSTR(argv[0]));
396
+ }
397
+ return INT2FIX(status);
398
+ }
399
+
400
+ static VALUE rb_gsl_permutation_fscanf(VALUE obj, VALUE io)
401
+ {
402
+ gsl_permutation *h;
403
+ FILE *f = NULL;
404
+ int status, flag = 0;
405
+ Data_Get_Struct(obj, gsl_permutation, h);
406
+ f = rb_gsl_open_readfile(io, &flag);
407
+ status = gsl_permutation_fscanf(f, h);
408
+ if (flag == 1) fclose(f);
409
+ return INT2FIX(status);
410
+ }
411
+
412
+ #ifdef GSL_1_2_LATER
413
+ static VALUE rb_gsl_permutation_linear_to_canonical(int argc, VALUE *argv, VALUE obj)
414
+ {
415
+ gsl_permutation *p, *q;
416
+
417
+ Data_Get_Struct(obj, gsl_permutation, p);
418
+ switch (argc) {
419
+ case 0:
420
+ q = gsl_permutation_alloc(p->size);
421
+ gsl_permutation_linear_to_canonical(q, p);
422
+ return Data_Wrap_Struct(cgsl_permutation, 0, gsl_permutation_free, q);
423
+ break;
424
+ case 1:
425
+ CHECK_PERMUTATION(argv[0]);
426
+ Data_Get_Struct(argv[0], gsl_permutation, q);
427
+ gsl_permutation_linear_to_canonical(q, p);
428
+ return obj;
429
+ break;
430
+ default:
431
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 1)", argc);
432
+ }
433
+ return Qtrue;
434
+ }
435
+
436
+ static VALUE rb_gsl_permutation_canonical_to_linear(int argc, VALUE *argv, VALUE obj)
437
+ {
438
+ gsl_permutation *p, *q;
439
+
440
+ Data_Get_Struct(obj, gsl_permutation, p);
441
+ switch (argc) {
442
+ case 0:
443
+ q = gsl_permutation_alloc(p->size);
444
+ gsl_permutation_canonical_to_linear(q, p);
445
+ return Data_Wrap_Struct(cgsl_permutation, 0, gsl_permutation_free, q);
446
+ break;
447
+ case 1:
448
+ CHECK_PERMUTATION(argv[0]);
449
+ Data_Get_Struct(argv[0], gsl_permutation, q);
450
+ gsl_permutation_canonical_to_linear(q, p);
451
+ return obj;
452
+ break;
453
+ default:
454
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 1)", argc);
455
+ }
456
+ return Qtrue;
457
+ }
458
+
459
+ static VALUE rb_gsl_permutation_inversions(VALUE obj)
460
+ {
461
+ gsl_permutation *p = NULL;
462
+ Data_Get_Struct(obj, gsl_permutation, p);
463
+ return INT2FIX(gsl_permutation_inversions(p));
464
+ }
465
+
466
+ static VALUE rb_gsl_permutation_linear_cycles(VALUE obj)
467
+ {
468
+ gsl_permutation *p = NULL;
469
+ Data_Get_Struct(obj, gsl_permutation, p);
470
+ return INT2FIX(gsl_permutation_linear_cycles(p));
471
+ }
472
+
473
+ static VALUE rb_gsl_permutation_canonical_cycles(VALUE obj)
474
+ {
475
+ gsl_permutation *p = NULL;
476
+ Data_Get_Struct(obj, gsl_permutation, p);
477
+ return INT2FIX(gsl_permutation_canonical_cycles(p));
478
+ }
479
+ #endif
480
+
481
+ static VALUE rb_gsl_vector_permute(VALUE obj, VALUE pp)
482
+ {
483
+ gsl_permutation *p = NULL;
484
+ gsl_vector *v = NULL;
485
+ int status;
486
+ CHECK_PERMUTATION(pp);
487
+ Data_Get_Struct(pp, gsl_permutation, p);
488
+ Data_Get_Struct(obj, gsl_vector, v);
489
+ status = gsl_permute_vector(p, v);
490
+ return INT2FIX(status);
491
+ }
492
+
493
+ static VALUE rb_gsl_vector_permute_inverse(VALUE obj, VALUE pp)
494
+ {
495
+ gsl_permutation *p = NULL;
496
+ gsl_vector *v = NULL;
497
+ int status;
498
+ CHECK_PERMUTATION(pp);
499
+ Data_Get_Struct(pp, gsl_permutation, p);
500
+ Data_Get_Struct(obj, gsl_vector, v);
501
+ status = gsl_permute_vector_inverse(p, v);
502
+ return INT2FIX(status);
503
+ }
504
+
505
+ static VALUE rb_gsl_permutation_set(VALUE obj, VALUE ii, VALUE val)
506
+ {
507
+ gsl_permutation *p = NULL;
508
+ CHECK_FIXNUM(ii);
509
+ CHECK_FIXNUM(val);
510
+ Data_Get_Struct(obj, gsl_permutation, p);
511
+ p->data[FIX2INT(ii)] = FIX2INT(val);
512
+ return obj;
513
+ }
514
+
515
+ static VALUE rb_gsl_permutation_equal(VALUE obj, VALUE other)
516
+ {
517
+ gsl_permutation *p1 = NULL, *p2 = NULL;
518
+ size_t i;
519
+ CHECK_PERMUTATION(other);
520
+ Data_Get_Struct(obj, gsl_permutation, p1);
521
+ Data_Get_Struct(other, gsl_permutation, p2);
522
+ if (p1->size != p2->size) return Qfalse;
523
+ for (i = 0; i < p1->size; i++)
524
+ if (p1->data[i] != p2->data[i]) return Qfalse;
525
+ return Qtrue;
526
+ }
527
+
528
+ void Init_gsl_permutation(VALUE module)
529
+ {
530
+ rb_define_singleton_method(cgsl_permutation, "alloc", rb_gsl_permutation_alloc, 1);
531
+ rb_define_singleton_method(cgsl_permutation, "calloc", rb_gsl_permutation_calloc, 1);
532
+ rb_define_method(cgsl_permutation, "size", rb_gsl_permutation_size, 0);
533
+ rb_define_method(cgsl_permutation, "init", rb_gsl_permutation_init, 0);
534
+ rb_define_method(cgsl_permutation, "inspect", rb_gsl_permutation_inspect, 0);
535
+ rb_define_method(cgsl_permutation, "to_s", rb_gsl_permutation_to_s, 0);
536
+ rb_define_method(cgsl_permutation, "get", rb_gsl_permutation_get, -1);
537
+ rb_define_alias(cgsl_permutation, "[]", "get");
538
+ rb_define_method(cgsl_permutation, "set", rb_gsl_permutation_set, 2);
539
+ rb_define_alias(cgsl_permutation, "[]=", "set");
540
+ #ifdef GSL_1_1_LATER
541
+ rb_define_singleton_method(cgsl_permutation, "memcpy", rb_gsl_permutation_memcpy, 2);
542
+ rb_define_method(cgsl_permutation, "clone", rb_gsl_permutation_clone, 0);
543
+ #endif
544
+ rb_define_method(cgsl_permutation, "swap", rb_gsl_permutation_swap, 2);
545
+ rb_define_method(cgsl_permutation, "valid", rb_gsl_permutation_valid, 0);
546
+ rb_define_method(cgsl_permutation, "valid?", rb_gsl_permutation_valid2, 0);
547
+ rb_define_method(cgsl_permutation, "to_a", rb_gsl_permutation_to_a, 0);
548
+ rb_define_method(cgsl_permutation, "to_v", rb_gsl_permutation_to_v, 0);
549
+ rb_define_method(cgsl_permutation, "reverse", rb_gsl_permutation_reverse, 0);
550
+ rb_define_method(cgsl_permutation, "inverse", rb_gsl_permutation_inverse, 0);
551
+ rb_define_alias(cgsl_permutation, "inv", "inverse");
552
+ rb_define_method(cgsl_permutation, "next", rb_gsl_permutation_next, 0);
553
+ rb_define_method(cgsl_permutation, "prev", rb_gsl_permutation_prev, 0);
554
+
555
+ rb_define_method(cgsl_permutation, "permute_vector", rb_gsl_permutation_permute_vector, 1);
556
+ rb_define_alias(cgsl_permutation, "permute", "permute_vector");
557
+ rb_define_method(cgsl_permutation, "permute_vector_inverse", rb_gsl_permutation_permute_vector_inverse, 1);
558
+ rb_define_alias(cgsl_permutation, "permute_inverse", "permute_vector_inverse");
559
+
560
+ rb_define_singleton_method(cgsl_permutation, "permute_vector", rb_gsl_permute_vector, 2);
561
+ rb_define_singleton_method(cgsl_permutation, "permute_vector_inverse", rb_gsl_permute_vector_inverse, 2);
562
+ rb_define_module_function(module, "permute_vector", rb_gsl_permute_vector, 2);
563
+ rb_define_module_function(module, "permute_vector_inverse", rb_gsl_permute_vector_inverse, 2);
564
+
565
+ rb_define_singleton_method(cgsl_permutation, "permute", rb_gsl_permute_vector, 2);
566
+ rb_define_singleton_method(cgsl_permutation, "permute_inverse", rb_gsl_permute_vector_inverse, 2);
567
+ rb_define_module_function(module, "permute", rb_gsl_permute_vector, 2);
568
+ rb_define_module_function(module, "permute_inverse", rb_gsl_permute_vector_inverse, 2);
569
+
570
+ rb_define_method(cgsl_permutation, "fwrite", rb_gsl_permutation_fwrite, 1);
571
+ rb_define_method(cgsl_permutation, "fread", rb_gsl_permutation_fread, 1);
572
+ rb_define_method(cgsl_permutation, "fprintf", rb_gsl_permutation_fprintf, -1);
573
+ rb_define_method(cgsl_permutation, "printf", rb_gsl_permutation_printf, -1);
574
+ rb_define_method(cgsl_permutation, "fscanf", rb_gsl_permutation_fscanf, 1);
575
+ rb_define_method(cgsl_permutation, "print", rb_gsl_permutation_print, 0);
576
+
577
+ #ifdef GSL_1_2_LATER
578
+ rb_define_singleton_method(cgsl_permutation, "mul", rb_gsl_permutation_mul, 2);
579
+ rb_define_method(cgsl_permutation, "mul", rb_gsl_permutation_mul, 2);
580
+ rb_define_method(cgsl_permutation, "linear_to_canonical", rb_gsl_permutation_linear_to_canonical, -1);
581
+ rb_define_alias(cgsl_permutation, "to_canonical", "linear_to_canonical");
582
+ rb_define_method(cgsl_permutation, "canonical_to_linear", rb_gsl_permutation_canonical_to_linear, -1);
583
+ rb_define_alias(cgsl_permutation, "to_linear", "canonical_to_linear");
584
+
585
+ rb_define_method(cgsl_permutation, "inversions", rb_gsl_permutation_inversions, 0);
586
+ rb_define_method(cgsl_permutation, "linear_cycles", rb_gsl_permutation_linear_cycles, 0);
587
+ rb_define_method(cgsl_permutation, "canonical_cycles", rb_gsl_permutation_canonical_cycles, 0);
588
+ #endif
589
+
590
+ rb_define_method(cgsl_vector, "permute", rb_gsl_vector_permute, 1);
591
+ rb_define_method(cgsl_vector, "permute_inverse", rb_gsl_vector_permute_inverse, 1);
592
+
593
+ rb_define_method(cgsl_permutation, "equal?", rb_gsl_permutation_equal, 1);
594
+ rb_define_alias(cgsl_permutation, "==", "equal?");
595
+
596
+ }