romanbsd-gsl 1.11.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (156) hide show
  1. data/README.rdoc +29 -0
  2. data/Rakefile +54 -0
  3. data/VERSION +1 -0
  4. data/ext/MANIFEST +116 -0
  5. data/ext/array.c +665 -0
  6. data/ext/array_complex.c +247 -0
  7. data/ext/blas.c +29 -0
  8. data/ext/blas1.c +729 -0
  9. data/ext/blas2.c +1093 -0
  10. data/ext/blas3.c +881 -0
  11. data/ext/block.c +44 -0
  12. data/ext/block_source.c +885 -0
  13. data/ext/bspline.c +116 -0
  14. data/ext/bundle.c +3 -0
  15. data/ext/cdf.c +754 -0
  16. data/ext/cheb.c +538 -0
  17. data/ext/combination.c +283 -0
  18. data/ext/common.c +310 -0
  19. data/ext/complex.c +1005 -0
  20. data/ext/const.c +668 -0
  21. data/ext/const_additional.c +120 -0
  22. data/ext/cqp.c +283 -0
  23. data/ext/deriv.c +194 -0
  24. data/ext/dht.c +360 -0
  25. data/ext/diff.c +165 -0
  26. data/ext/dirac.c +395 -0
  27. data/ext/eigen.c +2373 -0
  28. data/ext/error.c +194 -0
  29. data/ext/extconf.rb +272 -0
  30. data/ext/fcmp.c +66 -0
  31. data/ext/fft.c +1092 -0
  32. data/ext/fit.c +205 -0
  33. data/ext/fresnel.c +312 -0
  34. data/ext/function.c +522 -0
  35. data/ext/graph.c +1634 -0
  36. data/ext/gsl.c +265 -0
  37. data/ext/gsl_narray.c +652 -0
  38. data/ext/histogram.c +1717 -0
  39. data/ext/histogram2d.c +1067 -0
  40. data/ext/histogram3d.c +883 -0
  41. data/ext/histogram3d_source.c +750 -0
  42. data/ext/histogram_find.c +101 -0
  43. data/ext/histogram_oper.c +159 -0
  44. data/ext/ieee.c +98 -0
  45. data/ext/integration.c +1138 -0
  46. data/ext/interp.c +511 -0
  47. data/ext/jacobi.c +737 -0
  48. data/ext/linalg.c +4045 -0
  49. data/ext/linalg_complex.c +743 -0
  50. data/ext/math.c +724 -0
  51. data/ext/matrix.c +39 -0
  52. data/ext/matrix_complex.c +1731 -0
  53. data/ext/matrix_double.c +560 -0
  54. data/ext/matrix_int.c +256 -0
  55. data/ext/matrix_source.c +2678 -0
  56. data/ext/min.c +234 -0
  57. data/ext/monte.c +759 -0
  58. data/ext/multifit.c +1810 -0
  59. data/ext/multimin.c +793 -0
  60. data/ext/multimin_fsdf.c +156 -0
  61. data/ext/multiroots.c +952 -0
  62. data/ext/ndlinear.c +320 -0
  63. data/ext/nmf.c +159 -0
  64. data/ext/nmf_wrap.c +63 -0
  65. data/ext/ntuple.c +469 -0
  66. data/ext/odeiv.c +958 -0
  67. data/ext/ool.c +879 -0
  68. data/ext/oper_complex_source.c +253 -0
  69. data/ext/permutation.c +596 -0
  70. data/ext/poly.c +42 -0
  71. data/ext/poly2.c +265 -0
  72. data/ext/poly_source.c +1799 -0
  73. data/ext/qrng.c +171 -0
  74. data/ext/randist.c +1869 -0
  75. data/ext/rational.c +480 -0
  76. data/ext/rng.c +612 -0
  77. data/ext/root.c +408 -0
  78. data/ext/sf.c +1494 -0
  79. data/ext/sf_airy.c +200 -0
  80. data/ext/sf_bessel.c +867 -0
  81. data/ext/sf_clausen.c +28 -0
  82. data/ext/sf_coulomb.c +206 -0
  83. data/ext/sf_coupling.c +118 -0
  84. data/ext/sf_dawson.c +29 -0
  85. data/ext/sf_debye.c +157 -0
  86. data/ext/sf_dilog.c +42 -0
  87. data/ext/sf_elementary.c +44 -0
  88. data/ext/sf_ellint.c +206 -0
  89. data/ext/sf_elljac.c +29 -0
  90. data/ext/sf_erfc.c +93 -0
  91. data/ext/sf_exp.c +164 -0
  92. data/ext/sf_expint.c +211 -0
  93. data/ext/sf_fermi_dirac.c +148 -0
  94. data/ext/sf_gamma.c +344 -0
  95. data/ext/sf_gegenbauer.c +96 -0
  96. data/ext/sf_hyperg.c +197 -0
  97. data/ext/sf_laguerre.c +112 -0
  98. data/ext/sf_lambert.c +47 -0
  99. data/ext/sf_legendre.c +367 -0
  100. data/ext/sf_log.c +104 -0
  101. data/ext/sf_mathieu.c +238 -0
  102. data/ext/sf_power.c +46 -0
  103. data/ext/sf_psi.c +98 -0
  104. data/ext/sf_synchrotron.c +48 -0
  105. data/ext/sf_transport.c +76 -0
  106. data/ext/sf_trigonometric.c +207 -0
  107. data/ext/sf_zeta.c +119 -0
  108. data/ext/signal.c +310 -0
  109. data/ext/siman.c +718 -0
  110. data/ext/sort.c +208 -0
  111. data/ext/spline.c +393 -0
  112. data/ext/stats.c +799 -0
  113. data/ext/sum.c +168 -0
  114. data/ext/tamu_anova.c +56 -0
  115. data/ext/tensor.c +38 -0
  116. data/ext/tensor_source.c +1121 -0
  117. data/ext/vector.c +38 -0
  118. data/ext/vector_complex.c +2139 -0
  119. data/ext/vector_double.c +1445 -0
  120. data/ext/vector_int.c +204 -0
  121. data/ext/vector_source.c +3325 -0
  122. data/ext/wavelet.c +937 -0
  123. data/include/rb_gsl.h +140 -0
  124. data/include/rb_gsl_array.h +230 -0
  125. data/include/rb_gsl_cheb.h +21 -0
  126. data/include/rb_gsl_common.h +343 -0
  127. data/include/rb_gsl_complex.h +25 -0
  128. data/include/rb_gsl_const.h +29 -0
  129. data/include/rb_gsl_dirac.h +15 -0
  130. data/include/rb_gsl_eigen.h +17 -0
  131. data/include/rb_gsl_fft.h +62 -0
  132. data/include/rb_gsl_fit.h +25 -0
  133. data/include/rb_gsl_function.h +27 -0
  134. data/include/rb_gsl_graph.h +70 -0
  135. data/include/rb_gsl_histogram.h +65 -0
  136. data/include/rb_gsl_histogram3d.h +97 -0
  137. data/include/rb_gsl_integration.h +17 -0
  138. data/include/rb_gsl_interp.h +46 -0
  139. data/include/rb_gsl_linalg.h +25 -0
  140. data/include/rb_gsl_math.h +26 -0
  141. data/include/rb_gsl_odeiv.h +21 -0
  142. data/include/rb_gsl_poly.h +71 -0
  143. data/include/rb_gsl_rational.h +37 -0
  144. data/include/rb_gsl_rng.h +21 -0
  145. data/include/rb_gsl_root.h +22 -0
  146. data/include/rb_gsl_sf.h +119 -0
  147. data/include/rb_gsl_statistics.h +17 -0
  148. data/include/rb_gsl_tensor.h +45 -0
  149. data/include/rb_gsl_with_narray.h +22 -0
  150. data/include/templates_off.h +87 -0
  151. data/include/templates_on.h +241 -0
  152. data/lib/gsl/gnuplot.rb +41 -0
  153. data/lib/gsl/oper.rb +68 -0
  154. data/lib/ool.rb +22 -0
  155. data/lib/ool/conmin.rb +30 -0
  156. metadata +221 -0
@@ -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
data/ext/permutation.c ADDED
@@ -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
+ bnew = gsl_permutation_alloc(n);
76
+ for (j = 0; j < n; j++) {
77
+ i = FIX2INT(rb_ary_entry(argv[0], j));
78
+ if (i < 0) k = b->size + i; else k = i;
79
+ bnew->data[j] = b->data[k];
80
+ }
81
+ return Data_Wrap_Struct(CLASS_OF(argv[0]), 0, gsl_permutation_free, bnew);
82
+ break;
83
+ default:
84
+ if (PERMUTATION_P(argv[0])) {
85
+ Data_Get_Struct(argv[0], gsl_index, p);
86
+ bnew = gsl_permutation_alloc(p->size);
87
+ for (j = 0; j < p->size; j++) bnew->data[j] = b->data[p->data[j]];
88
+ return Data_Wrap_Struct(CLASS_OF(argv[0]), 0, gsl_permutation_free, bnew);
89
+ } else if (CLASS_OF(argv[0]) == rb_cRange) {
90
+ get_range_int_beg_en_n(argv[0], &beg, &en, &n, &step);
91
+ bnew = gsl_permutation_alloc(n);
92
+ for (j = 0; j < n; j++)
93
+ bnew->data[j] = b->data[beg+j];
94
+ return Data_Wrap_Struct(CLASS_OF(obj), 0, gsl_permutation_free, bnew);
95
+ } else {
96
+ rb_raise(rb_eArgError, "wrong argument type %s (Fixnum, Array, or Range expected)", rb_class2name(CLASS_OF(argv[0])));
97
+ break;
98
+ }
99
+ }
100
+ break;
101
+ default:
102
+ bnew = gsl_permutation_alloc(argc);
103
+ for (j = 0; j < argc; j++) {
104
+ i = FIX2INT(argv[j]);
105
+ if (i < 0) k = b->size + i; else k = i;
106
+ bnew->data[j] = b->data[k];
107
+ }
108
+ return Data_Wrap_Struct(CLASS_OF(argv[0]), 0, gsl_permutation_free, bnew);
109
+ break;
110
+ }
111
+ return Qnil;
112
+ }
113
+
114
+ #ifdef GSL_1_1_LATER
115
+ static VALUE rb_gsl_permutation_clone(VALUE obj)
116
+ {
117
+ gsl_permutation *p, *p2 = NULL;
118
+ Data_Get_Struct(obj, gsl_permutation, p);
119
+ p2 = gsl_permutation_alloc(p->size);
120
+ gsl_permutation_memcpy(p2, p);
121
+ return Data_Wrap_Struct(CLASS_OF(obj), 0, gsl_permutation_free, p2);
122
+ }
123
+
124
+ /* singleton */
125
+ static VALUE rb_gsl_permutation_memcpy(VALUE obj, VALUE pp1, VALUE pp2)
126
+ {
127
+ gsl_permutation *p1, *p2 = NULL;
128
+ CHECK_PERMUTATION(pp1);
129
+ CHECK_PERMUTATION(pp2);
130
+ Data_Get_Struct(pp1, gsl_permutation, p1);
131
+ Data_Get_Struct(pp2, gsl_permutation, p2);
132
+ gsl_permutation_memcpy(p1, p2);
133
+ return pp1;
134
+ }
135
+ #endif
136
+
137
+ static VALUE rb_gsl_permutation_swap(VALUE obj, VALUE i, VALUE j)
138
+ {
139
+ gsl_permutation *p = NULL;
140
+ CHECK_FIXNUM(i); CHECK_FIXNUM(j);
141
+ Data_Get_Struct(obj, gsl_permutation, p);
142
+ gsl_permutation_swap(p, FIX2INT(i), FIX2INT(j));
143
+ return obj;
144
+ }
145
+
146
+ static VALUE rb_gsl_permutation_valid(VALUE obj)
147
+ {
148
+ gsl_permutation *p = NULL;
149
+ Data_Get_Struct(obj, gsl_permutation, p);
150
+ return INT2FIX(gsl_permutation_valid(p));
151
+ }
152
+
153
+ static VALUE rb_gsl_permutation_valid2(VALUE obj)
154
+ {
155
+ gsl_permutation *p = NULL;
156
+ Data_Get_Struct(obj, gsl_permutation, p);
157
+ if(gsl_permutation_valid(p)) return Qtrue;
158
+ else return Qfalse;
159
+ }
160
+
161
+ /* to array */
162
+ static VALUE rb_gsl_permutation_to_a(VALUE obj)
163
+ {
164
+ gsl_permutation *p = NULL;
165
+ size_t i;
166
+ VALUE ary;
167
+ Data_Get_Struct(obj, gsl_permutation, p);
168
+ ary = rb_ary_new2(p->size);
169
+ for (i = 0; i < p->size; i++) {
170
+ rb_ary_store(ary, i, INT2FIX(gsl_permutation_get(p, i)));
171
+ }
172
+ return ary;
173
+ }
174
+
175
+ /* to vector */
176
+ static VALUE rb_gsl_permutation_to_v(VALUE obj)
177
+ {
178
+ gsl_permutation *p = NULL;
179
+ gsl_vector *v;
180
+ size_t size;
181
+ size_t i;
182
+ Data_Get_Struct(obj, gsl_permutation, p);
183
+ size = p->size;
184
+ v = gsl_vector_alloc(size);
185
+ for (i = 0; i < size; i++) {
186
+ gsl_vector_set(v, i, gsl_permutation_get(p, i));
187
+ }
188
+ return Data_Wrap_Struct(cgsl_vector, 0, gsl_vector_free, v);
189
+ }
190
+
191
+ static VALUE rb_gsl_permutation_reverse(VALUE obj)
192
+ {
193
+ gsl_permutation *p = NULL;
194
+ Data_Get_Struct(obj, gsl_permutation, p);
195
+ gsl_permutation_reverse(p);
196
+ return obj;
197
+ }
198
+
199
+ static VALUE rb_gsl_permutation_inverse(VALUE obj)
200
+ {
201
+ gsl_permutation *p, *inv;
202
+ Data_Get_Struct(obj, gsl_permutation, p);
203
+ inv = gsl_permutation_alloc(p->size);
204
+ gsl_permutation_inverse(inv, p);
205
+ return Data_Wrap_Struct(cgsl_permutation, 0, gsl_permutation_free, inv);
206
+ }
207
+
208
+ static VALUE rb_gsl_permutation_next(VALUE obj)
209
+ {
210
+ gsl_permutation *p = NULL;
211
+ Data_Get_Struct(obj, gsl_permutation, p);
212
+ return INT2FIX(gsl_permutation_next(p));
213
+ }
214
+
215
+ static VALUE rb_gsl_permutation_prev(VALUE obj)
216
+ {
217
+ gsl_permutation *p = NULL;
218
+ Data_Get_Struct(obj, gsl_permutation, p);
219
+ return INT2FIX(gsl_permutation_prev(p));
220
+ }
221
+
222
+ static VALUE rb_gsl_permutation_permute_vector(VALUE obj, VALUE vv)
223
+ {
224
+ gsl_permutation *p = NULL;
225
+ gsl_vector *v;
226
+ int status;
227
+ CHECK_VECTOR(vv);
228
+ Data_Get_Struct(obj, gsl_permutation, p);
229
+ Data_Get_Struct(vv, gsl_vector, v);
230
+ status = gsl_permute_vector(p, v);
231
+ return INT2FIX(status);
232
+ }
233
+
234
+ static VALUE rb_gsl_permutation_permute_vector_inverse(VALUE obj, VALUE vv)
235
+ {
236
+ gsl_permutation *p = NULL;
237
+ gsl_vector *v;
238
+ int status;
239
+ CHECK_VECTOR(vv);
240
+ Data_Get_Struct(obj, gsl_permutation, p);
241
+ Data_Get_Struct(vv, gsl_vector, v);
242
+ status = gsl_permute_vector_inverse(p, v);
243
+ return INT2FIX(status);
244
+ }
245
+
246
+ /* singleton */
247
+ static VALUE rb_gsl_permute_vector(VALUE obj, VALUE pp, VALUE vv)
248
+ {
249
+ gsl_permutation *p = NULL;
250
+ gsl_vector *v;
251
+ int status;
252
+ CHECK_VECTOR(vv);
253
+ Data_Get_Struct(pp, gsl_permutation, p);
254
+ Data_Get_Struct(vv, gsl_vector, v);
255
+ status = gsl_permute_vector(p, v);
256
+ return INT2FIX(status);
257
+ }
258
+
259
+ /* singleton */
260
+ static VALUE rb_gsl_permute_vector_inverse(VALUE obj, VALUE pp, VALUE vv)
261
+ {
262
+ gsl_permutation *p = NULL;
263
+ gsl_vector *v;
264
+ int status;
265
+ CHECK_VECTOR(vv);
266
+ Data_Get_Struct(pp, gsl_permutation, p);
267
+ Data_Get_Struct(vv, gsl_vector, v);
268
+ status = gsl_permute_vector_inverse(p, v);
269
+ return INT2FIX(status);
270
+ }
271
+
272
+ /* singleton method */
273
+ #ifdef GSL_1_2_LATER
274
+ static VALUE rb_gsl_permutation_mul(VALUE obj, VALUE ppa, VALUE ppb)
275
+ {
276
+ gsl_permutation *p = NULL;
277
+ gsl_permutation *pa = NULL, *pb = NULL;
278
+ int flag = 0;
279
+ CHECK_PERMUTATION(ppa);
280
+ CHECK_PERMUTATION(ppb);
281
+ Data_Get_Struct(ppa, gsl_permutation, pa);
282
+ Data_Get_Struct(ppb, gsl_permutation, pb);
283
+ if (rb_obj_is_kind_of(obj, cgsl_permutation)) {
284
+ Data_Get_Struct(obj, gsl_permutation, p);
285
+ flag = 1;
286
+ } else {
287
+ p = gsl_permutation_alloc(pa->size);
288
+ }
289
+ gsl_permutation_mul(p, pa, pb);
290
+ if (flag == 1) return obj;
291
+ else return Data_Wrap_Struct(cgsl_permutation, 0, gsl_permutation_free, p);
292
+ }
293
+ #endif
294
+
295
+ static VALUE rb_gsl_permutation_print(VALUE obj);
296
+
297
+ static VALUE rb_gsl_permutation_print(VALUE obj)
298
+ {
299
+ gsl_permutation *p = NULL;
300
+ size_t size, i;
301
+ Data_Get_Struct(obj, gsl_permutation, p);
302
+ size = p->size;
303
+ for (i = 0; i < size; i++) {
304
+ printf("%3d ", (int) gsl_permutation_get(p, i));
305
+ if ((i+1)%10 == 0) printf("\n");
306
+ }
307
+ printf("\n");
308
+ return obj;
309
+ }
310
+
311
+ static VALUE rb_gsl_permutation_to_s(VALUE obj)
312
+ {
313
+ gsl_permutation *v = NULL;
314
+ char buf[16];
315
+ size_t i;
316
+ VALUE str;
317
+ Data_Get_Struct(obj, gsl_permutation, v);
318
+ str = rb_str_new2("[");
319
+ for (i = 0; i < v->size; i++) {
320
+ sprintf(buf, " %d", (int) gsl_permutation_get(v, i));
321
+ rb_str_cat(str, buf, strlen(buf));
322
+ }
323
+ sprintf(buf, " ]");
324
+ rb_str_cat(str, buf, strlen(buf));
325
+ return str;
326
+ }
327
+
328
+ static VALUE rb_gsl_permutation_inspect(VALUE obj)
329
+ {
330
+ VALUE str;
331
+ char buf[64];
332
+ sprintf(buf, "%s\n", rb_class2name(CLASS_OF(obj)));
333
+ str = rb_str_new2(buf);
334
+ return rb_str_concat(str, rb_gsl_permutation_to_s(obj));
335
+ }
336
+
337
+ static VALUE rb_gsl_permutation_fwrite(VALUE obj, VALUE io)
338
+ {
339
+ gsl_permutation *h;
340
+ FILE *f = NULL;
341
+ int status, flag = 0;
342
+
343
+ Data_Get_Struct(obj, gsl_permutation, h);
344
+ f = rb_gsl_open_writefile(io, &flag);
345
+ status = gsl_permutation_fwrite(f, h);
346
+ if (flag == 1) fclose(f);
347
+ return INT2FIX(status);
348
+ }
349
+
350
+ static VALUE rb_gsl_permutation_fread(VALUE obj, VALUE io)
351
+ {
352
+ gsl_permutation *h;
353
+ FILE *f = NULL;
354
+ int status, flag = 0;
355
+
356
+ Data_Get_Struct(obj, gsl_permutation, h);
357
+ f = rb_gsl_open_readfile(io, &flag);
358
+ status = gsl_permutation_fread(f, h);
359
+ if (flag == 1) fclose(f);
360
+ return INT2FIX(status);
361
+ }
362
+
363
+ static VALUE rb_gsl_permutation_fprintf(int argc, VALUE *argv, VALUE obj)
364
+ {
365
+ gsl_permutation *h;
366
+ FILE *fp = NULL;
367
+ int status, flag = 0;
368
+
369
+ if (argc != 1 && argc != 2) rb_raise(rb_eArgError,
370
+ "wrong number of arguments (%d for 1 or 2)", argc);
371
+
372
+ Data_Get_Struct(obj, gsl_permutation, h);
373
+ fp = rb_gsl_open_writefile(argv[0], &flag);
374
+ if (argc == 1) {
375
+ status = gsl_permutation_fprintf(fp, h, "%u\n");
376
+ } else {
377
+ Check_Type(argv[1], T_STRING);
378
+ status = gsl_permutation_fprintf(fp, h, STR2CSTR(argv[1]));
379
+ }
380
+ if (flag == 1) fclose(fp);
381
+ return INT2FIX(status);
382
+ }
383
+
384
+ static VALUE rb_gsl_permutation_printf(int argc, VALUE *argv, VALUE obj)
385
+ {
386
+ gsl_permutation *h;
387
+ int status;
388
+
389
+ Data_Get_Struct(obj, gsl_permutation, h);
390
+ if (argc == 0) {
391
+ status = gsl_permutation_fprintf(stdout, h, "%u\n");
392
+ } else {
393
+ Check_Type(argv[0], T_STRING);
394
+ status = gsl_permutation_fprintf(stdout, h, STR2CSTR(argv[0]));
395
+ }
396
+ return INT2FIX(status);
397
+ }
398
+
399
+ static VALUE rb_gsl_permutation_fscanf(VALUE obj, VALUE io)
400
+ {
401
+ gsl_permutation *h;
402
+ FILE *f = NULL;
403
+ int status, flag = 0;
404
+ Data_Get_Struct(obj, gsl_permutation, h);
405
+ f = rb_gsl_open_readfile(io, &flag);
406
+ status = gsl_permutation_fscanf(f, h);
407
+ if (flag == 1) fclose(f);
408
+ return INT2FIX(status);
409
+ }
410
+
411
+ #ifdef GSL_1_2_LATER
412
+ static VALUE rb_gsl_permutation_linear_to_canonical(int argc, VALUE *argv, VALUE obj)
413
+ {
414
+ gsl_permutation *p, *q;
415
+
416
+ Data_Get_Struct(obj, gsl_permutation, p);
417
+ switch (argc) {
418
+ case 0:
419
+ q = gsl_permutation_alloc(p->size);
420
+ gsl_permutation_linear_to_canonical(q, p);
421
+ return Data_Wrap_Struct(cgsl_permutation, 0, gsl_permutation_free, q);
422
+ break;
423
+ case 1:
424
+ CHECK_PERMUTATION(argv[0]);
425
+ Data_Get_Struct(argv[0], gsl_permutation, q);
426
+ gsl_permutation_linear_to_canonical(q, p);
427
+ return obj;
428
+ break;
429
+ default:
430
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 1)", argc);
431
+ }
432
+ return Qtrue;
433
+ }
434
+
435
+ static VALUE rb_gsl_permutation_canonical_to_linear(int argc, VALUE *argv, VALUE obj)
436
+ {
437
+ gsl_permutation *p, *q;
438
+
439
+ Data_Get_Struct(obj, gsl_permutation, p);
440
+ switch (argc) {
441
+ case 0:
442
+ q = gsl_permutation_alloc(p->size);
443
+ gsl_permutation_canonical_to_linear(q, p);
444
+ return Data_Wrap_Struct(cgsl_permutation, 0, gsl_permutation_free, q);
445
+ break;
446
+ case 1:
447
+ CHECK_PERMUTATION(argv[0]);
448
+ Data_Get_Struct(argv[0], gsl_permutation, q);
449
+ gsl_permutation_canonical_to_linear(q, p);
450
+ return obj;
451
+ break;
452
+ default:
453
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 1)", argc);
454
+ }
455
+ return Qtrue;
456
+ }
457
+
458
+ static VALUE rb_gsl_permutation_inversions(VALUE obj)
459
+ {
460
+ gsl_permutation *p = NULL;
461
+ Data_Get_Struct(obj, gsl_permutation, p);
462
+ return INT2FIX(gsl_permutation_inversions(p));
463
+ }
464
+
465
+ static VALUE rb_gsl_permutation_linear_cycles(VALUE obj)
466
+ {
467
+ gsl_permutation *p = NULL;
468
+ Data_Get_Struct(obj, gsl_permutation, p);
469
+ return INT2FIX(gsl_permutation_linear_cycles(p));
470
+ }
471
+
472
+ static VALUE rb_gsl_permutation_canonical_cycles(VALUE obj)
473
+ {
474
+ gsl_permutation *p = NULL;
475
+ Data_Get_Struct(obj, gsl_permutation, p);
476
+ return INT2FIX(gsl_permutation_canonical_cycles(p));
477
+ }
478
+ #endif
479
+
480
+ static VALUE rb_gsl_vector_permute(VALUE obj, VALUE pp)
481
+ {
482
+ gsl_permutation *p = NULL;
483
+ gsl_vector *v = NULL;
484
+ int status;
485
+ CHECK_PERMUTATION(pp);
486
+ Data_Get_Struct(pp, gsl_permutation, p);
487
+ Data_Get_Struct(obj, gsl_vector, v);
488
+ status = gsl_permute_vector(p, v);
489
+ return INT2FIX(status);
490
+ }
491
+
492
+ static VALUE rb_gsl_vector_permute_inverse(VALUE obj, VALUE pp)
493
+ {
494
+ gsl_permutation *p = NULL;
495
+ gsl_vector *v = NULL;
496
+ int status;
497
+ CHECK_PERMUTATION(pp);
498
+ Data_Get_Struct(pp, gsl_permutation, p);
499
+ Data_Get_Struct(obj, gsl_vector, v);
500
+ status = gsl_permute_vector_inverse(p, v);
501
+ return INT2FIX(status);
502
+ }
503
+
504
+ static VALUE rb_gsl_permutation_set(VALUE obj, VALUE ii, VALUE val)
505
+ {
506
+ gsl_permutation *p = NULL;
507
+ CHECK_FIXNUM(ii);
508
+ CHECK_FIXNUM(val);
509
+ Data_Get_Struct(obj, gsl_permutation, p);
510
+ p->data[FIX2INT(ii)] = FIX2INT(val);
511
+ return obj;
512
+ }
513
+
514
+ static VALUE rb_gsl_permutation_equal(VALUE obj, VALUE other)
515
+ {
516
+ gsl_permutation *p1 = NULL, *p2 = NULL;
517
+ size_t i;
518
+ CHECK_PERMUTATION(other);
519
+ Data_Get_Struct(obj, gsl_permutation, p1);
520
+ Data_Get_Struct(other, gsl_permutation, p2);
521
+ if (p1->size != p2->size) return Qfalse;
522
+ for (i = 0; i < p1->size; i++)
523
+ if (p1->data[i] != p2->data[i]) return Qfalse;
524
+ return Qtrue;
525
+ }
526
+
527
+ void Init_gsl_permutation(VALUE module)
528
+ {
529
+ rb_define_singleton_method(cgsl_permutation, "alloc", rb_gsl_permutation_alloc, 1);
530
+ rb_define_singleton_method(cgsl_permutation, "calloc", rb_gsl_permutation_calloc, 1);
531
+ rb_define_method(cgsl_permutation, "size", rb_gsl_permutation_size, 0);
532
+ rb_define_method(cgsl_permutation, "init", rb_gsl_permutation_init, 0);
533
+ rb_define_method(cgsl_permutation, "print", rb_gsl_permutation_print, 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
+ }