romanbsd-gsl 1.11.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (156) hide show
  1. data/README.rdoc +29 -0
  2. data/Rakefile +54 -0
  3. data/VERSION +1 -0
  4. data/ext/MANIFEST +116 -0
  5. data/ext/array.c +665 -0
  6. data/ext/array_complex.c +247 -0
  7. data/ext/blas.c +29 -0
  8. data/ext/blas1.c +729 -0
  9. data/ext/blas2.c +1093 -0
  10. data/ext/blas3.c +881 -0
  11. data/ext/block.c +44 -0
  12. data/ext/block_source.c +885 -0
  13. data/ext/bspline.c +116 -0
  14. data/ext/bundle.c +3 -0
  15. data/ext/cdf.c +754 -0
  16. data/ext/cheb.c +538 -0
  17. data/ext/combination.c +283 -0
  18. data/ext/common.c +310 -0
  19. data/ext/complex.c +1005 -0
  20. data/ext/const.c +668 -0
  21. data/ext/const_additional.c +120 -0
  22. data/ext/cqp.c +283 -0
  23. data/ext/deriv.c +194 -0
  24. data/ext/dht.c +360 -0
  25. data/ext/diff.c +165 -0
  26. data/ext/dirac.c +395 -0
  27. data/ext/eigen.c +2373 -0
  28. data/ext/error.c +194 -0
  29. data/ext/extconf.rb +272 -0
  30. data/ext/fcmp.c +66 -0
  31. data/ext/fft.c +1092 -0
  32. data/ext/fit.c +205 -0
  33. data/ext/fresnel.c +312 -0
  34. data/ext/function.c +522 -0
  35. data/ext/graph.c +1634 -0
  36. data/ext/gsl.c +265 -0
  37. data/ext/gsl_narray.c +652 -0
  38. data/ext/histogram.c +1717 -0
  39. data/ext/histogram2d.c +1067 -0
  40. data/ext/histogram3d.c +883 -0
  41. data/ext/histogram3d_source.c +750 -0
  42. data/ext/histogram_find.c +101 -0
  43. data/ext/histogram_oper.c +159 -0
  44. data/ext/ieee.c +98 -0
  45. data/ext/integration.c +1138 -0
  46. data/ext/interp.c +511 -0
  47. data/ext/jacobi.c +737 -0
  48. data/ext/linalg.c +4045 -0
  49. data/ext/linalg_complex.c +743 -0
  50. data/ext/math.c +724 -0
  51. data/ext/matrix.c +39 -0
  52. data/ext/matrix_complex.c +1731 -0
  53. data/ext/matrix_double.c +560 -0
  54. data/ext/matrix_int.c +256 -0
  55. data/ext/matrix_source.c +2678 -0
  56. data/ext/min.c +234 -0
  57. data/ext/monte.c +759 -0
  58. data/ext/multifit.c +1810 -0
  59. data/ext/multimin.c +793 -0
  60. data/ext/multimin_fsdf.c +156 -0
  61. data/ext/multiroots.c +952 -0
  62. data/ext/ndlinear.c +320 -0
  63. data/ext/nmf.c +159 -0
  64. data/ext/nmf_wrap.c +63 -0
  65. data/ext/ntuple.c +469 -0
  66. data/ext/odeiv.c +958 -0
  67. data/ext/ool.c +879 -0
  68. data/ext/oper_complex_source.c +253 -0
  69. data/ext/permutation.c +596 -0
  70. data/ext/poly.c +42 -0
  71. data/ext/poly2.c +265 -0
  72. data/ext/poly_source.c +1799 -0
  73. data/ext/qrng.c +171 -0
  74. data/ext/randist.c +1869 -0
  75. data/ext/rational.c +480 -0
  76. data/ext/rng.c +612 -0
  77. data/ext/root.c +408 -0
  78. data/ext/sf.c +1494 -0
  79. data/ext/sf_airy.c +200 -0
  80. data/ext/sf_bessel.c +867 -0
  81. data/ext/sf_clausen.c +28 -0
  82. data/ext/sf_coulomb.c +206 -0
  83. data/ext/sf_coupling.c +118 -0
  84. data/ext/sf_dawson.c +29 -0
  85. data/ext/sf_debye.c +157 -0
  86. data/ext/sf_dilog.c +42 -0
  87. data/ext/sf_elementary.c +44 -0
  88. data/ext/sf_ellint.c +206 -0
  89. data/ext/sf_elljac.c +29 -0
  90. data/ext/sf_erfc.c +93 -0
  91. data/ext/sf_exp.c +164 -0
  92. data/ext/sf_expint.c +211 -0
  93. data/ext/sf_fermi_dirac.c +148 -0
  94. data/ext/sf_gamma.c +344 -0
  95. data/ext/sf_gegenbauer.c +96 -0
  96. data/ext/sf_hyperg.c +197 -0
  97. data/ext/sf_laguerre.c +112 -0
  98. data/ext/sf_lambert.c +47 -0
  99. data/ext/sf_legendre.c +367 -0
  100. data/ext/sf_log.c +104 -0
  101. data/ext/sf_mathieu.c +238 -0
  102. data/ext/sf_power.c +46 -0
  103. data/ext/sf_psi.c +98 -0
  104. data/ext/sf_synchrotron.c +48 -0
  105. data/ext/sf_transport.c +76 -0
  106. data/ext/sf_trigonometric.c +207 -0
  107. data/ext/sf_zeta.c +119 -0
  108. data/ext/signal.c +310 -0
  109. data/ext/siman.c +718 -0
  110. data/ext/sort.c +208 -0
  111. data/ext/spline.c +393 -0
  112. data/ext/stats.c +799 -0
  113. data/ext/sum.c +168 -0
  114. data/ext/tamu_anova.c +56 -0
  115. data/ext/tensor.c +38 -0
  116. data/ext/tensor_source.c +1121 -0
  117. data/ext/vector.c +38 -0
  118. data/ext/vector_complex.c +2139 -0
  119. data/ext/vector_double.c +1445 -0
  120. data/ext/vector_int.c +204 -0
  121. data/ext/vector_source.c +3325 -0
  122. data/ext/wavelet.c +937 -0
  123. data/include/rb_gsl.h +140 -0
  124. data/include/rb_gsl_array.h +230 -0
  125. data/include/rb_gsl_cheb.h +21 -0
  126. data/include/rb_gsl_common.h +343 -0
  127. data/include/rb_gsl_complex.h +25 -0
  128. data/include/rb_gsl_const.h +29 -0
  129. data/include/rb_gsl_dirac.h +15 -0
  130. data/include/rb_gsl_eigen.h +17 -0
  131. data/include/rb_gsl_fft.h +62 -0
  132. data/include/rb_gsl_fit.h +25 -0
  133. data/include/rb_gsl_function.h +27 -0
  134. data/include/rb_gsl_graph.h +70 -0
  135. data/include/rb_gsl_histogram.h +65 -0
  136. data/include/rb_gsl_histogram3d.h +97 -0
  137. data/include/rb_gsl_integration.h +17 -0
  138. data/include/rb_gsl_interp.h +46 -0
  139. data/include/rb_gsl_linalg.h +25 -0
  140. data/include/rb_gsl_math.h +26 -0
  141. data/include/rb_gsl_odeiv.h +21 -0
  142. data/include/rb_gsl_poly.h +71 -0
  143. data/include/rb_gsl_rational.h +37 -0
  144. data/include/rb_gsl_rng.h +21 -0
  145. data/include/rb_gsl_root.h +22 -0
  146. data/include/rb_gsl_sf.h +119 -0
  147. data/include/rb_gsl_statistics.h +17 -0
  148. data/include/rb_gsl_tensor.h +45 -0
  149. data/include/rb_gsl_with_narray.h +22 -0
  150. data/include/templates_off.h +87 -0
  151. data/include/templates_on.h +241 -0
  152. data/lib/gsl/gnuplot.rb +41 -0
  153. data/lib/gsl/oper.rb +68 -0
  154. data/lib/ool.rb +22 -0
  155. data/lib/ool/conmin.rb +30 -0
  156. metadata +221 -0
data/ext/vector.c ADDED
@@ -0,0 +1,38 @@
1
+ /*
2
+ vector.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
+ /*
13
+ This code uses "templates_on.h" and "templates_off.h",
14
+ which are provided by the GSL source.
15
+ */
16
+
17
+ #include "rb_gsl_config.h"
18
+ #include "rb_gsl_array.h"
19
+ #include "rb_gsl_histogram.h"
20
+ #include "rb_gsl_complex.h"
21
+ #include "rb_gsl_poly.h"
22
+ #ifdef HAVE_NARRAY_H
23
+ #include "rb_gsl_with_narray.h"
24
+ #endif
25
+
26
+ #define BASE_DOUBLE
27
+ #include "templates_on.h"
28
+ #include "vector_source.c"
29
+ #include "templates_off.h"
30
+ #undef BASE_DOUBLE
31
+
32
+ #define BASE_INT
33
+ #include "templates_on.h"
34
+ #include "vector_source.c"
35
+ #include "templates_off.h"
36
+ #undef BASE_INT
37
+
38
+
@@ -0,0 +1,2139 @@
1
+ /*
2
+ vector_complex.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 "rb_gsl_complex.h"
14
+
15
+ EXTERN VALUE cgsl_complex;
16
+ static VALUE rb_gsl_vector_complex_inner_product(int argc, VALUE *argv, VALUE obj);
17
+ static VALUE rb_gsl_vector_complex_product_to_m(int argc, VALUE *argv, VALUE obj);
18
+
19
+ // From vector_source.c
20
+ void get_range_beg_en_n(VALUE range, double *beg, double *en, size_t *n, int *step);
21
+ //
22
+ // From vector_source.c
23
+ void parse_subvector_args(int argc, VALUE *argv, size_t size,
24
+ size_t *offset, size_t *stride, size_t *n);
25
+
26
+ // From complex.c
27
+ gsl_complex rb_gsl_obj_to_gsl_complex(VALUE obj, gsl_complex *z);
28
+
29
+ static VALUE rb_gsl_vector_complex_new(int argc, VALUE *argv, VALUE klass)
30
+ {
31
+ VALUE tmp;
32
+ gsl_vector_complex *v = NULL;
33
+ gsl_vector *x, *y;
34
+ gsl_complex z, *z2 = NULL;
35
+ size_t n, i;
36
+ switch (argc) {
37
+ case 1:
38
+ switch (TYPE(argv[0])) {
39
+ case T_FIXNUM:
40
+ n = FIX2INT(argv[0]);
41
+ v = gsl_vector_complex_calloc(n);
42
+ if (v == NULL) rb_raise(rb_eNoMemError, "gsl_vector_complex_alloc failed");
43
+ break;
44
+ case T_ARRAY:
45
+ n = RARRAY(argv[0])->len;
46
+ v = gsl_vector_complex_alloc(n);
47
+ if (v == NULL) rb_raise(rb_eNoMemError, "gsl_vector_complex_alloc failed");
48
+ for (i = 0; i < n; i++) {
49
+ z2 = &z;
50
+ tmp = rb_ary_entry(argv[0], i);
51
+ if (TYPE(tmp) == T_ARRAY) {
52
+ GSL_SET_REAL(z2, NUM2DBL(rb_ary_entry(tmp, 0)));
53
+ GSL_SET_IMAG(z2, NUM2DBL(rb_ary_entry(tmp, 1)));
54
+ } else if (COMPLEX_P(tmp)) {
55
+ Data_Get_Struct(tmp, gsl_complex, z2);
56
+ } else {
57
+ rb_raise(rb_eTypeError,
58
+ "wrong argument type %s (Array or Complex expected)",
59
+ rb_class2name(CLASS_OF(tmp)));
60
+
61
+ }
62
+ gsl_vector_complex_set(v, i, *z2);
63
+ }
64
+ break;
65
+ default:
66
+ rb_raise(rb_eTypeError,
67
+ "wrong argument type %s", rb_class2name(CLASS_OF(argv[0])));
68
+ break;
69
+ }
70
+ break;
71
+ default:
72
+ if (argc == 2 && (VECTOR_P(argv[0]) && VECTOR_P(argv[1]))) {
73
+ Data_Get_Struct(argv[0], gsl_vector, x);
74
+ Data_Get_Struct(argv[1], gsl_vector, y);
75
+ n = GSL_MIN_INT(x->size, y->size);
76
+ v = gsl_vector_complex_alloc(n);
77
+ for (i = 0; i < n; i++) {
78
+ z.dat[0] = gsl_vector_get(x, i);
79
+ z.dat[1] = gsl_vector_get(y, i);
80
+ gsl_vector_complex_set(v, i, z);
81
+ }
82
+ break;
83
+ }
84
+ n = argc;
85
+ v = gsl_vector_complex_alloc(n);
86
+ if (v == NULL) rb_raise(rb_eNoMemError, "gsl_vector_complex_alloc failed");
87
+ for (i = 0; i < n; i++) {
88
+ if (TYPE(argv[i]) == T_ARRAY) {
89
+ GSL_SET_REAL(&z, NUM2DBL(rb_ary_entry(argv[i], 0)));
90
+ GSL_SET_IMAG(&z, NUM2DBL(rb_ary_entry(argv[i], 1)));
91
+ z2 = &z;
92
+ } else if (COMPLEX_P(argv[i])) {
93
+ Data_Get_Struct(argv[i], gsl_complex, z2);
94
+ } else {
95
+ rb_raise(rb_eTypeError,
96
+ "wrong argument type %s (Array or Complex expected)",
97
+ rb_class2name(CLASS_OF(argv[i])));
98
+ }
99
+ gsl_vector_complex_set(v, i, *z2);
100
+ }
101
+ break;
102
+ }
103
+ return Data_Wrap_Struct(klass, 0, gsl_vector_complex_free, v);
104
+ }
105
+
106
+ static VALUE rb_gsl_vector_complex_row_new(int argc, VALUE *argv, VALUE klass)
107
+ {
108
+ return rb_gsl_vector_complex_new(argc, argv, klass);
109
+ }
110
+
111
+ static VALUE rb_gsl_vector_complex_calloc(VALUE klass, VALUE nn)
112
+ {
113
+ gsl_vector_complex *vc = NULL;
114
+ CHECK_FIXNUM(nn);
115
+ vc = gsl_vector_complex_calloc(FIX2INT(nn));
116
+ if (vc == NULL) rb_raise(rb_eNoMemError, "gsl_vector_complex_alloc failed");
117
+ return Data_Wrap_Struct(klass, 0, gsl_vector_complex_free, vc);
118
+ }
119
+
120
+ static VALUE rb_gsl_vector_complex_size(VALUE obj)
121
+ {
122
+ gsl_vector_complex *v = NULL;
123
+ Data_Get_Struct(obj, gsl_vector_complex, v);
124
+ return INT2FIX(v->size);
125
+ }
126
+
127
+ static VALUE rb_gsl_vector_complex_stride(VALUE obj)
128
+ {
129
+ gsl_vector_complex *v = NULL;
130
+ Data_Get_Struct(obj, gsl_vector_complex, v);
131
+ return INT2FIX(v->stride);
132
+ }
133
+
134
+ static VALUE rb_gsl_vector_complex_owner(VALUE obj)
135
+ {
136
+ gsl_vector_complex *v = NULL;
137
+ Data_Get_Struct(obj, gsl_vector_complex, v);
138
+ return INT2FIX(v->owner);
139
+ }
140
+
141
+ static VALUE rb_gsl_vector_complex_ptr(VALUE obj, VALUE i)
142
+ {
143
+ gsl_vector_complex *v = NULL;
144
+ Data_Get_Struct(obj, gsl_vector_complex, v);
145
+ return Data_Wrap_Struct(cgsl_complex, 0, NULL, gsl_vector_complex_ptr(v, FIX2INT(i)));
146
+ }
147
+
148
+ // TODO return Data_Wrap_Struct(VECTOR_COMPLEX_ROW_COL(obj),..." where appropriate
149
+ static VALUE rb_gsl_vector_complex_subvector(int argc, VALUE *argv, VALUE obj);
150
+ static VALUE rb_gsl_vector_complex_get(int argc, VALUE *argv, VALUE obj)
151
+ {
152
+ VALUE retval = Qnil;
153
+ gsl_vector_complex *v = NULL, *vnew;
154
+ gsl_complex *c = NULL;
155
+ gsl_index *p;
156
+ int i, k;
157
+ size_t index, j;
158
+ // If argc is not 1 or argv[0] is a Range
159
+ if( argc != 1 || rb_obj_is_kind_of(argv[0], rb_cRange)) {
160
+ // Treat as call to subvector
161
+ retval = rb_gsl_vector_complex_subvector(argc, argv, obj);
162
+ } else {
163
+ Data_Get_Struct(obj, gsl_vector_complex, v);
164
+
165
+ switch (TYPE(argv[0])) {
166
+ case T_FIXNUM:
167
+ CHECK_FIXNUM(argv[0]);
168
+ i = FIX2INT(argv[0]);
169
+ if (i < 0) index = (size_t) (v->size + i);
170
+ else index = (size_t) i;
171
+ c = ALLOC(gsl_complex);
172
+ *c = gsl_vector_complex_get(v, index);
173
+ retval = Data_Wrap_Struct(cgsl_complex, 0, free, c);
174
+ break;
175
+ case T_ARRAY:
176
+ vnew = gsl_vector_complex_alloc(RARRAY(argv[0])->len);
177
+ for (j = 0; j < vnew->size; j++) {
178
+ i = FIX2INT(rb_ary_entry(argv[0], j));
179
+ if (i < 0) i = v->size + i;
180
+ gsl_vector_complex_set(vnew, j, gsl_vector_complex_get(v, i));
181
+ }
182
+ retval = Data_Wrap_Struct(cgsl_vector_complex, 0, gsl_vector_complex_free, vnew);
183
+ break;
184
+ default:
185
+ if (PERMUTATION_P(argv[0])) {
186
+ Data_Get_Struct(argv[0], gsl_index, p);
187
+ vnew = gsl_vector_complex_alloc(p->size);
188
+ for (j = 0; j < p->size; j++) {
189
+ k = p->data[j];
190
+ if (k < 0) k = p->size + j;
191
+ gsl_vector_complex_set(vnew, j, gsl_vector_complex_get(v, k));
192
+ }
193
+ retval = Data_Wrap_Struct(cgsl_vector_complex, 0, gsl_vector_complex_free, vnew);
194
+ } else {
195
+ // TODO Support Vector::Int (and even Vector?)
196
+ rb_raise(rb_eTypeError, "wrong argument type %s (Array, Range, GSL::Permutation, or Fixnum expected)", rb_class2name(CLASS_OF(argv[0])));
197
+ }
198
+ break;
199
+ }
200
+ }
201
+ return retval;
202
+ }
203
+
204
+ static VALUE rb_gsl_vector_complex_set_all(int argc, VALUE *argv, VALUE obj)
205
+ {
206
+ gsl_vector_complex *v = NULL;
207
+ gsl_complex tmp;
208
+
209
+ if (argc < 1) rb_raise(rb_eArgError, "wrong number of arguments");
210
+
211
+ Data_Get_Struct(obj, gsl_vector_complex, v);
212
+
213
+ switch (argc) {
214
+ case 1:
215
+ tmp = rb_gsl_obj_to_gsl_complex(argv[0], NULL);
216
+ break;
217
+ case 2:
218
+ GSL_SET_COMPLEX(&tmp, NUM2DBL(argv[0]), NUM2DBL(argv[1]));
219
+ break;
220
+ default:
221
+ rb_raise(rb_eArgError, "wrong number of arguments");
222
+ break;
223
+ }
224
+
225
+ gsl_vector_complex_set_all(v, tmp);
226
+
227
+ return obj;
228
+ }
229
+
230
+ void rb_gsl_vector_complex_set_subvector(int argc, VALUE *argv, gsl_vector_complex *v, VALUE other)
231
+ {
232
+ gsl_vector_complex *vother;
233
+ gsl_vector_complex_view vv;
234
+ gsl_complex tmp;
235
+ int step;
236
+ size_t i, offset, stride, n, nother;
237
+ double beg, end;
238
+
239
+ // assignment to v.subvector(...)
240
+ parse_subvector_args(argc, argv, v->size, &offset, &stride, &n);
241
+ vv = gsl_vector_complex_subvector_with_stride(v, offset, stride, n);
242
+ if(rb_obj_is_kind_of(other, cgsl_vector_complex)) {
243
+ Data_Get_Struct(other, gsl_vector_complex, vother);
244
+ if(n != vother->size) {
245
+ rb_raise(rb_eRangeError, "lengths do not match (%d != %d)", n, vother->size);
246
+ }
247
+ // TODO Change to gsl_vector_complex_memmove if/when GSL has such a
248
+ // function because gsl_vector_memcpy does not handle overlapping regions
249
+ // (e.g. Views) well.
250
+ gsl_vector_complex_memcpy(&vv.vector, vother);
251
+ } else if(rb_obj_is_kind_of(other, rb_cArray)) {
252
+ // TODO Support other forms of Array contents as well
253
+ if(n != RARRAY_LEN(other)) {
254
+ rb_raise(rb_eRangeError, "lengths do not match (%d != %d)", n, RARRAY_LEN(other));
255
+ }
256
+ for(i = 0; i < n; i++) {
257
+ tmp = rb_gsl_obj_to_gsl_complex(rb_ary_entry(other, i), NULL);
258
+ gsl_vector_complex_set(&vv.vector, i, tmp);
259
+ }
260
+ } else if(rb_obj_is_kind_of(other, rb_cRange)) {
261
+ get_range_beg_en_n(other, &beg, &end, &nother, &step);
262
+ if(n != nother) {
263
+ rb_raise(rb_eRangeError, "lengths do not match (%d != %d)", n, nother);
264
+ }
265
+ GSL_SET_IMAG(&tmp, 0.0);
266
+ for(i = 0; i < n; i++) {
267
+ GSL_SET_REAL(&tmp, beg);
268
+ gsl_vector_complex_set(&vv.vector, i, tmp);
269
+ beg += step;
270
+ }
271
+ } else {
272
+ tmp = rb_gsl_obj_to_gsl_complex(argv[1], NULL);
273
+ gsl_vector_complex_set_all(&vv.vector, tmp);
274
+ }
275
+ }
276
+
277
+ static VALUE rb_gsl_vector_complex_set(int argc, VALUE *argv, VALUE obj)
278
+ {
279
+ gsl_vector_complex *v = NULL;
280
+ gsl_complex tmp;
281
+ VALUE other;
282
+ int ii;
283
+
284
+ if(argc < 1 || argc > 4) {
285
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 1-4)", argc);
286
+ }
287
+
288
+ Data_Get_Struct(obj, gsl_vector_complex, v);
289
+ other = argv[argc-1];
290
+
291
+ if(argc == 1) {
292
+ rb_gsl_vector_complex_set_all(argc, argv, obj);
293
+ } else if(argc == 2 && TYPE(argv[0]) == T_FIXNUM) {
294
+ // v[i] = x
295
+ ii = FIX2INT(argv[0]);
296
+ if(ii < 0) ii += v->size;
297
+ // Get/make GSL::Complex from second arg
298
+ tmp = gsl_vector_complex_get(v, ii);
299
+ rb_gsl_obj_to_gsl_complex(argv[1], &tmp);
300
+ gsl_vector_complex_set(v, (size_t)ii, tmp);
301
+ } else {
302
+ // assignment to v.subvector(...)
303
+ rb_gsl_vector_complex_set_subvector(argc-1, argv, v, other);
304
+ }
305
+
306
+ return obj;
307
+ }
308
+
309
+ static VALUE rb_gsl_vector_complex_each(VALUE obj)
310
+ {
311
+ gsl_vector_complex *v = NULL;
312
+ VALUE vz;
313
+ gsl_complex * zp;
314
+ size_t i;
315
+ Data_Get_Struct(obj, gsl_vector_complex, v);
316
+ for (i = 0; i < v->size; i++) {
317
+ vz = Data_Make_Struct(cgsl_complex, gsl_complex, 0, free, zp);
318
+ *zp = gsl_vector_complex_get(v, i);
319
+ rb_yield(vz);
320
+ }
321
+ return obj;
322
+ }
323
+
324
+ static VALUE rb_gsl_vector_complex_reverse_each(VALUE obj)
325
+ {
326
+ gsl_vector_complex *v = NULL;
327
+ VALUE vz;
328
+ gsl_complex * zp;
329
+ size_t i;
330
+ Data_Get_Struct(obj, gsl_vector_complex, v);
331
+ for (i = v->size-1; i >= 0; i--) {
332
+ vz = Data_Make_Struct(cgsl_complex, gsl_complex, 0, free, zp);
333
+ *zp = gsl_vector_complex_get(v, i);
334
+ rb_yield(vz);
335
+ }
336
+ return obj;
337
+ }
338
+
339
+ static VALUE rb_gsl_vector_complex_each_index(VALUE obj)
340
+ {
341
+ gsl_vector_complex *v = NULL;
342
+ size_t i;
343
+ Data_Get_Struct(obj, gsl_vector_complex, v);
344
+ for (i = 0; i < v->size; i++) rb_yield(INT2FIX(i));
345
+ return obj;
346
+ }
347
+
348
+ static VALUE rb_gsl_vector_complex_reverse_each_index(VALUE obj)
349
+ {
350
+ gsl_vector_complex *v = NULL;
351
+ size_t i;
352
+ Data_Get_Struct(obj, gsl_vector_complex, v);
353
+ for (i = v->size-1; i >= 0; i--) {
354
+ rb_yield(INT2FIX(i));
355
+ if (i == 0) break;
356
+ }
357
+ return obj;
358
+ }
359
+
360
+ static void rb_gsl_vector_complex_collect_native(gsl_vector_complex *src, gsl_vector_complex *dst)
361
+ {
362
+ VALUE vz;
363
+ gsl_complex * zp;
364
+ size_t i;
365
+ for (i = 0; i < src->size; i++) {
366
+ vz = Data_Make_Struct(cgsl_complex, gsl_complex, 0, free, zp);
367
+ *zp = gsl_vector_complex_get(src, i);
368
+ vz = rb_yield(vz);
369
+ CHECK_COMPLEX(vz);
370
+ Data_Get_Struct(vz, gsl_complex, zp);
371
+ gsl_vector_complex_set(dst, i, *zp);
372
+ }
373
+ }
374
+
375
+ static VALUE rb_gsl_vector_complex_collect(VALUE obj)
376
+ {
377
+ gsl_vector_complex *v = NULL, *vnew;
378
+ Data_Get_Struct(obj, gsl_vector_complex, v);
379
+ vnew = gsl_vector_complex_alloc(v->size);
380
+ rb_gsl_vector_complex_collect_native(v, vnew);
381
+ return Data_Wrap_Struct(cgsl_vector_complex, 0, gsl_vector_complex_free, vnew);
382
+ }
383
+
384
+ static VALUE rb_gsl_vector_complex_collect_bang(VALUE obj)
385
+ {
386
+ gsl_vector_complex *v = NULL;
387
+ Data_Get_Struct(obj, gsl_vector_complex, v);
388
+ rb_gsl_vector_complex_collect_native(v, v);
389
+ return obj;
390
+ }
391
+
392
+ static VALUE rb_gsl_vector_complex_set_zero(VALUE obj)
393
+ {
394
+ gsl_vector_complex *v = NULL;
395
+ Data_Get_Struct(obj, gsl_vector_complex, v);
396
+ gsl_vector_complex_set_zero(v);
397
+ return obj;
398
+ }
399
+
400
+ static VALUE rb_gsl_vector_complex_set_basis(VALUE obj, VALUE ii)
401
+ {
402
+ gsl_vector_complex *v = NULL;
403
+ CHECK_FIXNUM(ii);
404
+ Data_Get_Struct(obj, gsl_vector_complex, v);
405
+ gsl_vector_complex_set_basis(v, FIX2INT(ii));
406
+ return obj;
407
+ }
408
+
409
+ static VALUE rb_gsl_vector_complex_to_s(VALUE obj)
410
+ {
411
+ gsl_vector_complex *v = NULL;
412
+ char buf[64];
413
+ size_t i;
414
+ VALUE str;
415
+ gsl_complex * z;
416
+
417
+ Data_Get_Struct(obj, gsl_vector_complex, v);
418
+ if (v->size == 0) return rb_str_new2("[ ]");
419
+ str = rb_str_new2("[ ");
420
+ if (VECTOR_COMPLEX_COL_P(obj)) {
421
+ for (i = 0; i < v->size; i++) {
422
+ if (i != 0) {
423
+ rb_str_cat(str, " ", 2);
424
+ }
425
+ z = GSL_COMPLEX_AT(v, i);
426
+ sprintf(buf, "[%4.3e %4.3e]", GSL_REAL(*z), GSL_IMAG(*z));
427
+ if (i != v->size-1) strcat(buf, "\n");
428
+ rb_str_cat(str, buf, strlen(buf));
429
+ if (i >= 10 && i != v->size-1) {
430
+ rb_str_cat(str, " ...", 5);
431
+ break;
432
+ }
433
+ }
434
+ } else {
435
+ z = GSL_COMPLEX_AT(v, 0);
436
+ sprintf(buf, "[%4.3e %4.3e]", GSL_REAL(*z), GSL_IMAG(*z));
437
+ rb_str_cat(str, buf, strlen(buf));
438
+ for (i = 1; i < v->size; i++) {
439
+ z = GSL_COMPLEX_AT(v, i);
440
+ sprintf(buf, " [%4.3e %4.3e]", GSL_REAL(*z), GSL_IMAG(*z));
441
+ rb_str_cat(str, buf, strlen(buf));
442
+ if (i >= 10 && i != v->size-1) {
443
+ rb_str_cat(str, " ...", 4);
444
+ break;
445
+ }
446
+ }
447
+ }
448
+ rb_str_cat(str, " ]", 2);
449
+ return str;
450
+ }
451
+
452
+ static VALUE rb_gsl_vector_complex_inspect(VALUE obj)
453
+ {
454
+ VALUE str;
455
+ char buf[128];
456
+ gsl_vector_complex *v;
457
+
458
+ Data_Get_Struct(obj, gsl_vector_complex, v);
459
+ sprintf(buf, "#<%s[%lu]:%#x>\n", rb_class2name(CLASS_OF(obj)), v->size, NUM2UINT(rb_obj_id(obj)));
460
+ str = rb_str_new2(buf);
461
+ return rb_str_concat(str, rb_gsl_vector_complex_to_s(obj));
462
+ }
463
+
464
+ /*static VALUE rb_gsl_vector_complex_fprintf(VALUE obj, VALUE io, VALUE format)*/
465
+ static VALUE rb_gsl_vector_complex_fprintf(int argc, VALUE *argv, VALUE obj)
466
+ {
467
+ gsl_vector_complex *h = NULL;
468
+ FILE *fp = NULL;
469
+ int status, flag = 0;
470
+ if (argc != 1 && argc != 2) {
471
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 1 or 2)", argc);
472
+ }
473
+ Data_Get_Struct(obj, gsl_vector_complex, h);
474
+ fp = rb_gsl_open_writefile(argv[0], &flag);
475
+ if (argc == 2) {
476
+ Check_Type(argv[1], T_STRING);
477
+ status = gsl_vector_complex_fprintf(fp, h, STR2CSTR(argv[1]));
478
+ } else {
479
+ status = gsl_vector_complex_fprintf(fp, h, "%4.3e");
480
+ }
481
+ if (flag == 1) fclose(fp);
482
+ return INT2FIX(status);
483
+ }
484
+
485
+ /*static VALUE rb_gsl_vector_complex_printf(VALUE obj, VALUE format)*/
486
+ static VALUE rb_gsl_vector_complex_printf(int argc, VALUE *argv, VALUE obj)
487
+ {
488
+ gsl_vector_complex *h = NULL;
489
+ int status;
490
+ Data_Get_Struct(obj, gsl_vector_complex, h);
491
+ if (argc == 1) {
492
+ Check_Type(argv[0], T_STRING);
493
+ status = gsl_vector_complex_fprintf(stdout, h, STR2CSTR(argv[0]));
494
+ } else {
495
+ status = gsl_vector_complex_fprintf(stdout, h, "%4.3e");
496
+ }
497
+ return INT2FIX(status);
498
+ }
499
+ /*
500
+ static VALUE rb_gsl_vector_complex_print(VALUE obj);
501
+ static VALUE rb_gsl_vector_complex_inspect(VALUE obj)
502
+ {
503
+ gsl_vector_complex *h = NULL;
504
+ Data_Get_Struct(obj, gsl_vector_complex, h);
505
+ printf("%s\n", rb_class2name(CLASS_OF(obj)));
506
+ rb_gsl_vector_complex_print(obj);
507
+ return Qtrue;
508
+ }
509
+ */
510
+ static VALUE rb_gsl_vector_complex_print(VALUE obj)
511
+ {
512
+ gsl_vector_complex *h = NULL;
513
+ gsl_complex *z = NULL;
514
+ size_t i;
515
+ Data_Get_Struct(obj, gsl_vector_complex, h);
516
+ printf("[ ");
517
+ if (VECTOR_COMPLEX_COL_P(obj)) {
518
+ printf("\n");
519
+ for (i = 0; i < h->size; i++) {
520
+ z = GSL_COMPLEX_AT(h, i);
521
+ printf(" [%4.3e %4.3e]\n", GSL_REAL(*z), GSL_IMAG(*z));
522
+ }
523
+ } else {
524
+ for (i = 0; i < h->size; i++) {
525
+ z = GSL_COMPLEX_AT(h, i);
526
+ printf("[%4.3e %4.3e] ", GSL_REAL(*z), GSL_IMAG(*z));
527
+ }
528
+ }
529
+ printf("]\n");
530
+ return obj;
531
+ }
532
+
533
+ static VALUE rb_gsl_vector_complex_fwrite(VALUE obj, VALUE io)
534
+ {
535
+ gsl_vector_complex *h = NULL;
536
+ FILE *fp = NULL;
537
+ int status, flag = 0;
538
+ Data_Get_Struct(obj, gsl_vector_complex, h);
539
+ fp = rb_gsl_open_writefile(io, &flag);
540
+ status = gsl_vector_complex_fwrite(fp, h);
541
+ if (flag == 1) fclose(fp);
542
+ return INT2FIX(status);
543
+ }
544
+
545
+ static VALUE rb_gsl_vector_complex_fread(VALUE obj, VALUE io)
546
+ {
547
+ gsl_vector_complex *h = NULL;
548
+ FILE *f = NULL;
549
+ int status, flag = 0;
550
+ Data_Get_Struct(obj, gsl_vector_complex, h);
551
+ f = rb_gsl_open_writefile(io, &flag);
552
+ status = gsl_vector_complex_fread(f, h);
553
+ if (flag == 1) fclose(f);
554
+ return INT2FIX(status);
555
+ }
556
+
557
+ static VALUE rb_gsl_vector_complex_fscanf(VALUE obj, VALUE io)
558
+ {
559
+ gsl_vector_complex *h = NULL;
560
+ FILE *f = NULL;
561
+ int status, flag = 0;
562
+ Data_Get_Struct(obj, gsl_vector_complex, h);
563
+ f = rb_gsl_open_writefile(io, &flag);
564
+ status = gsl_vector_complex_fscanf(f, h);
565
+ if (flag == 1) fclose(f);
566
+ return INT2FIX(status);
567
+ }
568
+
569
+ static VALUE rb_gsl_vector_complex_real(VALUE obj)
570
+ {
571
+ gsl_vector_complex *c = NULL;
572
+ gsl_vector_view *vv = NULL;
573
+ Data_Get_Struct(obj, gsl_vector_complex, c);
574
+ vv = gsl_vector_view_alloc();
575
+ *vv = gsl_vector_complex_real(c);
576
+ if (VECTOR_COMPLEX_ROW_P(obj))
577
+ return Data_Wrap_Struct(cgsl_vector_view, 0, gsl_vector_view_free, vv);
578
+ else
579
+ return Data_Wrap_Struct(cgsl_vector_col_view, 0, gsl_vector_view_free, vv);
580
+ }
581
+
582
+ static VALUE rb_gsl_vector_complex_imag(VALUE obj)
583
+ {
584
+ gsl_vector_complex *c = NULL;
585
+ gsl_vector_view *vv = NULL;
586
+ Data_Get_Struct(obj, gsl_vector_complex, c);
587
+ vv = gsl_vector_view_alloc();
588
+ *vv = gsl_vector_complex_imag(c);
589
+ if (VECTOR_COMPLEX_ROW_P(obj))
590
+ return Data_Wrap_Struct(cgsl_vector_view, 0, gsl_vector_view_free, vv);
591
+ else
592
+ return Data_Wrap_Struct(cgsl_vector_col_view, 0, gsl_vector_view_free, vv);
593
+ }
594
+
595
+ static VALUE rb_gsl_vector_complex_set_real(VALUE obj, VALUE val)
596
+ {
597
+ gsl_vector_complex *v = NULL;
598
+ gsl_vector_view vv;
599
+ double d = NUM2DBL(val);
600
+ Data_Get_Struct(obj, gsl_vector_complex, v);
601
+ vv = gsl_vector_complex_real(v);
602
+ gsl_vector_set_all(&vv.vector, d);
603
+ return obj;
604
+ }
605
+
606
+ static VALUE rb_gsl_vector_complex_set_imag(VALUE obj, VALUE val)
607
+ {
608
+ gsl_vector_complex *v = NULL;
609
+ gsl_vector_view vv;
610
+ double d = NUM2DBL(val);
611
+ Data_Get_Struct(obj, gsl_vector_complex, v);
612
+ vv = gsl_vector_complex_imag(v);
613
+ gsl_vector_set_all(&vv.vector, d);
614
+ return obj;
615
+ }
616
+
617
+ static VALUE rb_gsl_vector_complex_conj(VALUE obj)
618
+ {
619
+ size_t i;
620
+ gsl_vector_complex *vin = NULL;
621
+ gsl_vector_complex *vout = NULL;
622
+ Data_Get_Struct(obj, gsl_vector_complex, vin);
623
+ vout = gsl_vector_complex_alloc(vin->size);
624
+ for(i=0; i<vin->size; i++) {
625
+ gsl_vector_complex_set(vout, i,
626
+ gsl_complex_conjugate(
627
+ gsl_vector_complex_get(vin, i)));
628
+ }
629
+ return Data_Wrap_Struct(VECTOR_COMPLEX_ROW_COL(obj), 0, gsl_vector_complex_free, vout);
630
+ }
631
+
632
+ static VALUE rb_gsl_vector_complex_conj_bang(VALUE obj)
633
+ {
634
+ size_t i;
635
+ gsl_vector_complex *v = NULL;
636
+ Data_Get_Struct(obj, gsl_vector_complex, v);
637
+ for(i=0; i<v->size; i++) {
638
+ gsl_vector_complex_set(v, i,
639
+ gsl_complex_conjugate(
640
+ gsl_vector_complex_get(v, i)));
641
+ }
642
+ return obj;
643
+ }
644
+
645
+ static VALUE rb_gsl_vector_complex_to_a(VALUE obj)
646
+ {
647
+ gsl_vector_complex *c = NULL;
648
+ gsl_complex *z = NULL;
649
+ size_t i, j;
650
+ VALUE ary;
651
+ Data_Get_Struct(obj, gsl_vector_complex, c);
652
+ ary = rb_ary_new2(c->size*2);
653
+ for (i = 0, j = 0; i < c->size; i++, j+=2) {
654
+ z = GSL_COMPLEX_AT(c, i);
655
+ rb_ary_store(ary, j, rb_float_new(GSL_REAL(*z)));
656
+ rb_ary_store(ary, j+1, rb_float_new(GSL_IMAG(*z)));
657
+ }
658
+ return ary;
659
+ }
660
+
661
+ static VALUE rb_gsl_vector_complex_to_a2(VALUE obj)
662
+ {
663
+ gsl_vector_complex *c = NULL;
664
+ gsl_complex *znew = NULL, *z = NULL;
665
+ size_t i;
666
+ VALUE ary, vz;
667
+ Data_Get_Struct(obj, gsl_vector_complex, c);
668
+ ary = rb_ary_new2(c->size);
669
+ for (i = 0; i < c->size; i++) {
670
+ z = GSL_COMPLEX_AT(c, i);
671
+ znew = make_complex(z->dat[0], z->dat[1]);
672
+ vz = Data_Wrap_Struct(cgsl_complex, 0, free, znew);
673
+ rb_ary_store(ary, i, vz);
674
+ }
675
+ return ary;
676
+ }
677
+
678
+ gsl_vector_complex_view* gsl_vector_complex_view_alloc()
679
+ {
680
+ gsl_vector_complex_view *vv = NULL;
681
+ vv = ALLOC(gsl_vector_complex_view);
682
+ if (vv == NULL) rb_raise(rb_eRuntimeError, "malloc failed");
683
+ return vv;
684
+ }
685
+
686
+ void gsl_vector_complex_view_free(gsl_vector_view * vv)
687
+ {
688
+ free((gsl_vector_complex_view *) vv);
689
+ }
690
+
691
+ static VALUE rb_gsl_vector_complex_subvector(int argc, VALUE *argv, VALUE obj)
692
+ {
693
+ gsl_vector_complex *v = NULL;
694
+ gsl_vector_complex_view *vv = NULL;
695
+ size_t offset, stride, n;
696
+ Data_Get_Struct(obj, gsl_vector_complex, v);
697
+ parse_subvector_args(argc, argv, v->size, &offset, &stride, &n);
698
+ vv = gsl_vector_complex_view_alloc();
699
+ *vv = gsl_vector_complex_subvector_with_stride(v, offset, stride, n);
700
+ if (VECTOR_COMPLEX_ROW_P(obj))
701
+ return Data_Wrap_Struct(cgsl_vector_complex_view, 0, gsl_vector_complex_view_free, vv);
702
+ else
703
+ return Data_Wrap_Struct(cgsl_vector_complex_col_view, 0, gsl_vector_complex_view_free, vv);
704
+ }
705
+
706
+ static VALUE rb_gsl_vector_complex_subvector_with_stride(VALUE obj, VALUE o, VALUE s, VALUE nn)
707
+ {
708
+ gsl_vector_complex *v = NULL;
709
+ gsl_vector_complex_view *vv = NULL;
710
+ int offset;
711
+ CHECK_FIXNUM(o); CHECK_FIXNUM(nn); CHECK_FIXNUM(s);
712
+ offset = NUM2INT(o);
713
+ Data_Get_Struct(obj, gsl_vector_complex, v);
714
+ if(offset < 0) {
715
+ offset += v->size;
716
+ }
717
+ vv = gsl_vector_complex_view_alloc();
718
+ *vv = gsl_vector_complex_subvector_with_stride(v, (size_t)offset, FIX2INT(s), FIX2INT(nn));
719
+ if (VECTOR_COMPLEX_ROW_P(obj))
720
+ return Data_Wrap_Struct(cgsl_vector_complex_view, 0, gsl_vector_complex_view_free, vv);
721
+ else
722
+ return Data_Wrap_Struct(cgsl_vector_complex_col_view, 0, gsl_vector_complex_view_free, vv);
723
+ }
724
+
725
+ /* singleton */
726
+ static VALUE rb_gsl_vector_complex_memcpy(VALUE obj, VALUE dst, VALUE src)
727
+ {
728
+ gsl_vector_complex *v = NULL, *dest = NULL;
729
+ CHECK_VECTOR_COMPLEX(dst);
730
+ CHECK_VECTOR_COMPLEX(src);
731
+ Data_Get_Struct(dst, gsl_vector_complex, dest);
732
+ Data_Get_Struct(src, gsl_vector_complex, v);
733
+ gsl_vector_complex_memcpy(dest, v);
734
+ return dst;
735
+ }
736
+
737
+ static VALUE rb_gsl_vector_complex_clone(VALUE obj)
738
+ {
739
+ gsl_vector_complex *v = NULL, *vnew = NULL;
740
+ Data_Get_Struct(obj, gsl_vector_complex, v);
741
+ vnew = gsl_vector_complex_alloc(v->size);
742
+ if (vnew == NULL) rb_raise(rb_eNoMemError, "gsl_vector_complex_alloc failed");
743
+ gsl_vector_complex_memcpy(vnew, v);
744
+ if (VECTOR_COMPLEX_ROW_P(obj))
745
+ return Data_Wrap_Struct(cgsl_vector_complex, 0, gsl_vector_complex_free, vnew);
746
+ else
747
+ return Data_Wrap_Struct(cgsl_vector_complex_col, 0, gsl_vector_complex_free, vnew);
748
+ }
749
+
750
+ static VALUE rb_gsl_vector_complex_reverse(VALUE obj)
751
+ {
752
+ gsl_vector_complex *v = NULL;
753
+ Data_Get_Struct(obj, gsl_vector_complex, v);
754
+ gsl_vector_complex_reverse(v);
755
+ return obj;
756
+ }
757
+
758
+ static VALUE rb_gsl_vector_complex_reverse2(VALUE obj)
759
+ {
760
+ gsl_vector_complex *v = NULL, *vnew = NULL;
761
+ Data_Get_Struct(obj, gsl_vector_complex, v);
762
+ vnew = gsl_vector_complex_alloc(v->size);
763
+ gsl_vector_complex_memcpy(vnew, v);
764
+ gsl_vector_complex_reverse(vnew);
765
+ if (VECTOR_COMPLEX_ROW_P(obj))
766
+ return Data_Wrap_Struct(cgsl_vector_complex, 0, gsl_vector_complex_free, vnew);
767
+ else
768
+ return Data_Wrap_Struct(cgsl_vector_complex_col, 0, gsl_vector_complex_free, vnew);
769
+ }
770
+
771
+ static VALUE rb_gsl_vector_complex_swap_elements(VALUE obj, VALUE i, VALUE j)
772
+ {
773
+ gsl_vector_complex *v = NULL;
774
+ CHECK_FIXNUM(i); CHECK_FIXNUM(j);
775
+ Data_Get_Struct(obj, gsl_vector_complex, v);
776
+ gsl_vector_complex_swap_elements(v, FIX2INT(i), FIX2INT(j));
777
+ return obj;
778
+ }
779
+
780
+ static VALUE rb_gsl_vector_complex_fftshift_bang(VALUE obj)
781
+ {
782
+ gsl_vector_complex *v = NULL;
783
+ gsl_complex tmp;
784
+ size_t i, n;
785
+
786
+ Data_Get_Struct(obj, gsl_vector_complex, v);
787
+ n = v->size;
788
+ if(n & 1) {
789
+ // length is odd
790
+ tmp = gsl_vector_complex_get(v,0);
791
+ for(i = 0; i < n/2; i++) {
792
+ gsl_vector_complex_set(v, i, gsl_vector_complex_get(v, i+n/2+1));
793
+ gsl_vector_complex_set(v, i+n/2+1, gsl_vector_complex_get(v, i+1));
794
+ }
795
+ gsl_vector_complex_set(v, n/2, tmp);
796
+ } else {
797
+ // length is even
798
+ for(i = 0; i < n/2; i++) {
799
+ gsl_vector_complex_swap_elements(v, i, i+n/2);
800
+ }
801
+ }
802
+
803
+ return obj;
804
+ }
805
+
806
+ static VALUE rb_gsl_vector_complex_fftshift(VALUE obj)
807
+ {
808
+ gsl_vector_complex *v, *vnew;
809
+ gsl_vector_complex_view vv, vvnew;
810
+ size_t n;
811
+
812
+ Data_Get_Struct(obj, gsl_vector_complex, v);
813
+ n = v->size;
814
+ vnew = gsl_vector_complex_alloc(n);
815
+ // Copy low to high
816
+ vv = gsl_vector_complex_subvector(v, 0, (n+1)/2);
817
+ vvnew = gsl_vector_complex_subvector(vnew, n/2, (n+1)/2);
818
+ gsl_vector_complex_memcpy(&vvnew.vector, &vv.vector);
819
+ // Copy high to low
820
+ vv = gsl_vector_complex_subvector(v, (n+1)/2, n/2);
821
+ vvnew = gsl_vector_complex_subvector(vnew, 0, n/2);
822
+ gsl_vector_complex_memcpy(&vvnew.vector, &vv.vector);
823
+
824
+ return Data_Wrap_Struct(VECTOR_COMPLEX_ROW_COL(obj), 0, gsl_vector_complex_free, vnew);
825
+ }
826
+
827
+ static VALUE rb_gsl_vector_complex_ifftshift_bang(VALUE obj)
828
+ {
829
+ gsl_vector_complex *v = NULL;
830
+ gsl_complex tmp;
831
+ size_t i, n;
832
+
833
+ Data_Get_Struct(obj, gsl_vector_complex, v);
834
+ n = v->size;
835
+ if(n & 1) {
836
+ // length is odd
837
+ tmp = gsl_vector_complex_get(v,n/2);
838
+ for(i = n/2; i > 0; i--) {
839
+ gsl_vector_complex_set(v, i, gsl_vector_complex_get(v, i+n/2));
840
+ gsl_vector_complex_set(v, i+n/2, gsl_vector_complex_get(v, i-1));
841
+ }
842
+ gsl_vector_complex_set(v, 0, tmp);
843
+ } else {
844
+ // length is even
845
+ for(i = 0; i < n/2; i++) {
846
+ gsl_vector_complex_swap_elements(v, i, i+n/2);
847
+ }
848
+ }
849
+
850
+ return obj;
851
+ }
852
+
853
+ static VALUE rb_gsl_vector_complex_ifftshift(VALUE obj)
854
+ {
855
+ return rb_gsl_vector_complex_ifftshift_bang(rb_gsl_vector_complex_clone(obj));
856
+ gsl_vector_complex *v, *vnew;
857
+ gsl_vector_complex_view vv, vvnew;
858
+ size_t n;
859
+
860
+ Data_Get_Struct(obj, gsl_vector_complex, v);
861
+ n = v->size;
862
+ vnew = gsl_vector_complex_alloc(n);
863
+ // Copy high to low
864
+ vv = gsl_vector_complex_subvector(vnew, n/2, (n+1)/2);
865
+ vvnew = gsl_vector_complex_subvector(v, 0, (n+1)/2);
866
+ gsl_vector_complex_memcpy(&vvnew.vector, &vv.vector);
867
+ // Copy low to high
868
+ vv = gsl_vector_complex_subvector(vnew, 0, n/2);
869
+ vvnew = gsl_vector_complex_subvector(v, (n+1)/2, n/2);
870
+ gsl_vector_complex_memcpy(&vvnew.vector, &vv.vector);
871
+
872
+ return Data_Wrap_Struct(VECTOR_COMPLEX_ROW_COL(obj), 0, gsl_vector_complex_free, vnew);
873
+ }
874
+
875
+ static VALUE rb_gsl_vector_complex_isnull(VALUE obj)
876
+ {
877
+ gsl_vector_complex *v = NULL;
878
+ Data_Get_Struct(obj, gsl_vector_complex, v);
879
+ if (gsl_vector_complex_isnull(v)) return Qtrue;
880
+ else return Qfalse;
881
+ }
882
+
883
+ static VALUE rb_gsl_vector_complex_matrix_view(int argc, VALUE *argv, VALUE obj)
884
+ {
885
+ gsl_vector_complex *v = NULL;
886
+ gsl_matrix_complex_view *mv = NULL;
887
+ Data_Get_Struct(obj, gsl_vector_complex, v);
888
+ switch (argc) {
889
+ case 2:
890
+ mv = gsl_matrix_complex_view_alloc();
891
+ *mv = gsl_matrix_complex_view_vector(v, FIX2INT(argv[0]), FIX2INT(argv[1]));
892
+ break;
893
+ case 3:
894
+ mv = gsl_matrix_complex_view_alloc();
895
+ *mv = gsl_matrix_complex_view_vector_with_tda(v, FIX2INT(argv[0]), FIX2INT(argv[1]),
896
+ FIX2INT(argv[2]));
897
+ break;
898
+ default:
899
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 2 or 3)", argc);
900
+ break;
901
+ }
902
+ return Data_Wrap_Struct(cgsl_matrix_complex_view, 0, gsl_matrix_complex_view_free, mv);
903
+ }
904
+
905
+ static VALUE rb_gsl_vector_complex_matrix_view_with_tda(VALUE obj, VALUE nn1, VALUE nn2,
906
+ VALUE tda)
907
+ {
908
+ gsl_vector_complex *v = NULL;
909
+ gsl_matrix_complex_view *mv = NULL;
910
+ CHECK_FIXNUM(nn1); CHECK_FIXNUM(nn2); CHECK_FIXNUM(tda);
911
+ Data_Get_Struct(obj, gsl_vector_complex, v);
912
+ mv = gsl_matrix_complex_view_alloc();
913
+ if (mv == NULL) rb_raise(rb_eNoMemError, "gsl_matrix_complex_alloc failed");
914
+ *mv = gsl_matrix_complex_view_vector_with_tda(v, FIX2INT(nn1), FIX2INT(nn2), FIX2INT(tda));
915
+ return Data_Wrap_Struct(cgsl_matrix_complex_view, 0, gsl_matrix_complex_view_free, mv);
916
+ }
917
+
918
+ static VALUE rb_gsl_vector_complex_trans(VALUE obj)
919
+ {
920
+ gsl_vector_complex *v = NULL, *vnew = NULL;
921
+ Data_Get_Struct(obj, gsl_vector_complex, v);
922
+ vnew = make_vector_complex_clone(v);
923
+ if (VECTOR_COMPLEX_ROW_P(obj))
924
+ return Data_Wrap_Struct(cgsl_vector_complex_col, 0, gsl_vector_complex_free, vnew);
925
+ else if (VECTOR_COMPLEX_COL_P(obj))
926
+ return Data_Wrap_Struct(cgsl_vector_complex, 0, gsl_vector_complex_free, vnew);
927
+ else {
928
+ rb_raise(rb_eTypeError, "wrong type");
929
+ }
930
+ }
931
+
932
+ static VALUE rb_gsl_vector_complex_trans2(VALUE obj)
933
+ {
934
+ if (CLASS_OF(obj) == cgsl_vector_complex)
935
+ RBASIC(obj)->klass = cgsl_vector_complex_col;
936
+ else if (CLASS_OF(obj) == cgsl_vector_complex_col)
937
+ RBASIC(obj)->klass = cgsl_vector_complex;
938
+ else {
939
+ rb_raise(rb_eRuntimeError, "method trans! for %s is forbidden",
940
+ rb_class2name(CLASS_OF(obj)));
941
+ }
942
+ return obj;
943
+ }
944
+
945
+ static VALUE rb_gsl_vector_complex_to_real(VALUE obj)
946
+ {
947
+ gsl_vector_complex *cv = NULL;
948
+ gsl_vector *v = NULL;
949
+ gsl_complex z;
950
+ size_t i;
951
+ Data_Get_Struct(obj, gsl_vector_complex, cv);
952
+ v = gsl_vector_alloc(cv->size);
953
+ if (v == NULL) rb_raise(rb_eNoMemError, "gsl_vector_alloc failed");
954
+ for (i = 0; i < cv->size; i++) {
955
+ z = gsl_vector_complex_get(cv, i);
956
+ gsl_vector_set(v, i, GSL_REAL(z));
957
+ }
958
+ if (VECTOR_COMPLEX_ROW_P(obj))
959
+ return Data_Wrap_Struct(cgsl_vector, 0, gsl_vector_free, v);
960
+ else
961
+ return Data_Wrap_Struct(cgsl_vector_col, 0, gsl_vector_free, v);
962
+ }
963
+
964
+ enum {
965
+ GSL_VECTOR_COMPLEX_ADD,
966
+ GSL_VECTOR_COMPLEX_SUB,
967
+ GSL_VECTOR_COMPLEX_MUL,
968
+ GSL_VECTOR_COMPLEX_DIV,
969
+ GSL_VECTOR_COMPLEX_ADD_BANG,
970
+ GSL_VECTOR_COMPLEX_SUB_BANG,
971
+ GSL_VECTOR_COMPLEX_MUL_BANG,
972
+ GSL_VECTOR_COMPLEX_DIV_BANG,
973
+ };
974
+
975
+ static VALUE rb_gsl_vector_complex_arithmetics(int flag, VALUE obj, VALUE bb);
976
+
977
+ static VALUE rb_gsl_vector_complex_arithmetics(int flag, VALUE obj, VALUE bb)
978
+ {
979
+ gsl_vector *b = NULL;
980
+ gsl_vector_complex *cv = NULL, *cvnew = NULL, *cb = NULL;
981
+ gsl_complex *c = NULL, z;
982
+ Data_Get_Struct(obj, gsl_vector_complex, cv);
983
+ switch (flag) {
984
+ case GSL_VECTOR_COMPLEX_ADD:
985
+ case GSL_VECTOR_COMPLEX_SUB:
986
+ case GSL_VECTOR_COMPLEX_MUL:
987
+ case GSL_VECTOR_COMPLEX_DIV:
988
+ cvnew = make_vector_complex_clone(cv);
989
+ obj = Data_Wrap_Struct(VECTOR_COMPLEX_ROW_COL(obj), 0, gsl_vector_complex_free, cvnew);
990
+ break;
991
+ case GSL_VECTOR_COMPLEX_ADD_BANG:
992
+ case GSL_VECTOR_COMPLEX_SUB_BANG:
993
+ case GSL_VECTOR_COMPLEX_MUL_BANG:
994
+ case GSL_VECTOR_COMPLEX_DIV_BANG:
995
+ cvnew = cv;
996
+ break;
997
+ default:
998
+ rb_raise(rb_eRuntimeError, "unknown operation");
999
+ break;
1000
+ }
1001
+ switch (TYPE(bb)) {
1002
+ case T_FLOAT:
1003
+ case T_FIXNUM:
1004
+ case T_BIGNUM:
1005
+ z = gsl_complex_rect(NUM2DBL(bb), 0.0);
1006
+ switch (flag) {
1007
+ case GSL_VECTOR_COMPLEX_ADD:
1008
+ case GSL_VECTOR_COMPLEX_ADD_BANG:
1009
+ gsl_vector_complex_add_constant(cvnew, z);
1010
+ break;
1011
+ case GSL_VECTOR_COMPLEX_SUB:
1012
+ case GSL_VECTOR_COMPLEX_SUB_BANG:
1013
+ gsl_vector_complex_add_constant(cvnew, gsl_complex_negative(z));
1014
+ break;
1015
+ case GSL_VECTOR_COMPLEX_MUL:
1016
+ case GSL_VECTOR_COMPLEX_MUL_BANG:
1017
+ gsl_vector_complex_scale(cvnew, z);
1018
+ break;
1019
+ case GSL_VECTOR_COMPLEX_DIV:
1020
+ case GSL_VECTOR_COMPLEX_DIV_BANG:
1021
+ gsl_vector_complex_scale(cvnew, gsl_complex_inverse(z));
1022
+ break;
1023
+ }
1024
+ break;
1025
+ default:
1026
+ if (VECTOR_P(bb)) {
1027
+ Data_Get_Struct(bb, gsl_vector, b);
1028
+ cb = vector_to_complex(b);
1029
+ switch (flag) {
1030
+ case GSL_VECTOR_COMPLEX_ADD:
1031
+ case GSL_VECTOR_COMPLEX_ADD_BANG:
1032
+ gsl_vector_complex_add(cvnew, cb);
1033
+ break;
1034
+ case GSL_VECTOR_COMPLEX_SUB:
1035
+ case GSL_VECTOR_COMPLEX_SUB_BANG:
1036
+ gsl_vector_complex_sub(cvnew, cb);
1037
+ break;
1038
+ case GSL_VECTOR_COMPLEX_MUL:
1039
+ case GSL_VECTOR_COMPLEX_MUL_BANG:
1040
+ gsl_vector_complex_mul(cvnew, cb);
1041
+ break;
1042
+ case GSL_VECTOR_COMPLEX_DIV:
1043
+ case GSL_VECTOR_COMPLEX_DIV_BANG:
1044
+ gsl_vector_complex_div(cvnew, cb);
1045
+ break;
1046
+ }
1047
+ gsl_vector_complex_free(cb);
1048
+ } else if (VECTOR_COMPLEX_P(bb)) {
1049
+ Data_Get_Struct(bb, gsl_vector_complex, cb);
1050
+ switch (flag) {
1051
+ case GSL_VECTOR_COMPLEX_ADD:
1052
+ case GSL_VECTOR_COMPLEX_ADD_BANG:
1053
+ gsl_vector_complex_add(cvnew, cb);
1054
+ break;
1055
+ case GSL_VECTOR_COMPLEX_SUB:
1056
+ case GSL_VECTOR_COMPLEX_SUB_BANG:
1057
+ gsl_vector_complex_sub(cvnew, cb);
1058
+ break;
1059
+ case GSL_VECTOR_COMPLEX_MUL:
1060
+ case GSL_VECTOR_COMPLEX_MUL_BANG:
1061
+ gsl_vector_complex_mul(cvnew, cb);
1062
+ break;
1063
+ case GSL_VECTOR_COMPLEX_DIV:
1064
+ case GSL_VECTOR_COMPLEX_DIV_BANG:
1065
+ gsl_vector_complex_div(cvnew, cb);
1066
+ break;
1067
+ }
1068
+ } else if (COMPLEX_P(bb)) {
1069
+ Data_Get_Struct(bb, gsl_complex, c);
1070
+ switch (flag) {
1071
+ case GSL_VECTOR_COMPLEX_ADD:
1072
+ case GSL_VECTOR_COMPLEX_ADD_BANG:
1073
+ gsl_vector_complex_add_constant(cvnew, *c);
1074
+ break;
1075
+ case GSL_VECTOR_COMPLEX_SUB:
1076
+ case GSL_VECTOR_COMPLEX_SUB_BANG:
1077
+ gsl_vector_complex_add_constant(cvnew, gsl_complex_negative(*c));
1078
+ break;
1079
+ case GSL_VECTOR_COMPLEX_MUL:
1080
+ case GSL_VECTOR_COMPLEX_MUL_BANG:
1081
+ gsl_vector_complex_scale(cvnew, *c);
1082
+ break;
1083
+ case GSL_VECTOR_COMPLEX_DIV:
1084
+ case GSL_VECTOR_COMPLEX_DIV_BANG:
1085
+ gsl_vector_complex_scale(cvnew, gsl_complex_inverse(*c));
1086
+ break;
1087
+ }
1088
+ } else {
1089
+ rb_raise(rb_eTypeError, "wrong type argument %s", rb_class2name(CLASS_OF(bb)));
1090
+ }
1091
+ break;
1092
+ }
1093
+ return obj;
1094
+ }
1095
+
1096
+ static VALUE rb_gsl_vector_complex_add(VALUE obj, VALUE bb)
1097
+ {
1098
+ return rb_gsl_vector_complex_arithmetics(GSL_VECTOR_COMPLEX_ADD, obj, bb);
1099
+ }
1100
+
1101
+ static VALUE rb_gsl_vector_complex_sub(VALUE obj, VALUE bb)
1102
+ {
1103
+ return rb_gsl_vector_complex_arithmetics(GSL_VECTOR_COMPLEX_SUB, obj, bb);
1104
+ }
1105
+
1106
+ static VALUE rb_gsl_vector_complex_mul(VALUE obj, VALUE bb)
1107
+ {
1108
+ VALUE argv[2];
1109
+ if (VECTOR_COMPLEX_ROW_P(obj) && VECTOR_COMPLEX_COL_P(bb)) {
1110
+ argv[0] = obj;
1111
+ argv[1] = bb;
1112
+ return rb_gsl_vector_complex_inner_product(2, argv, CLASS_OF(obj));
1113
+ }
1114
+ if (VECTOR_COMPLEX_COL_P(obj) && VECTOR_COMPLEX_ROW_P(bb)) {
1115
+ argv[0] = obj;
1116
+ argv[1] = bb;
1117
+ return rb_gsl_vector_complex_product_to_m(2, argv, CLASS_OF(obj));
1118
+ }
1119
+ return rb_gsl_vector_complex_arithmetics(GSL_VECTOR_COMPLEX_MUL, obj, bb);
1120
+ }
1121
+
1122
+ static VALUE rb_gsl_vector_complex_div(VALUE obj, VALUE bb)
1123
+ {
1124
+ return rb_gsl_vector_complex_arithmetics(GSL_VECTOR_COMPLEX_DIV, obj, bb);
1125
+ }
1126
+
1127
+ static VALUE rb_gsl_vector_complex_add_bang(VALUE obj, VALUE bb)
1128
+ {
1129
+ return rb_gsl_vector_complex_arithmetics(GSL_VECTOR_COMPLEX_ADD_BANG, obj, bb);
1130
+ }
1131
+
1132
+ static VALUE rb_gsl_vector_complex_sub_bang(VALUE obj, VALUE bb)
1133
+ {
1134
+ return rb_gsl_vector_complex_arithmetics(GSL_VECTOR_COMPLEX_SUB_BANG, obj, bb);
1135
+ }
1136
+
1137
+ static VALUE rb_gsl_vector_complex_mul_bang(VALUE obj, VALUE bb)
1138
+ {
1139
+ return rb_gsl_vector_complex_arithmetics(GSL_VECTOR_COMPLEX_MUL_BANG, obj, bb);
1140
+ }
1141
+
1142
+ static VALUE rb_gsl_vector_complex_div_bang(VALUE obj, VALUE bb)
1143
+ {
1144
+ return rb_gsl_vector_complex_arithmetics(GSL_VECTOR_COMPLEX_DIV_BANG, obj, bb);
1145
+ }
1146
+
1147
+ static VALUE rb_gsl_vector_complex_coerce(VALUE obj, VALUE other)
1148
+ {
1149
+ gsl_vector_complex *cv = NULL, *cb = NULL;
1150
+ gsl_complex z;
1151
+ VALUE vv;
1152
+ Data_Get_Struct(obj, gsl_vector_complex, cv);
1153
+ switch (TYPE(other)) {
1154
+ case T_FLOAT:
1155
+ case T_FIXNUM:
1156
+ case T_BIGNUM:
1157
+ z = gsl_complex_rect(NUM2DBL(other), 0.0);
1158
+ cb = gsl_vector_complex_alloc(cv->size);
1159
+ if (cb == NULL) rb_raise(rb_eNoMemError, "gsl_vector_complex_alloc failed");
1160
+ gsl_vector_complex_set_all(cb, z);
1161
+ vv = Data_Wrap_Struct(VECTOR_COMPLEX_ROW_COL(obj), 0, gsl_vector_complex_free, cb);
1162
+ return rb_ary_new3(2, vv, obj);
1163
+ break;
1164
+ default:
1165
+ rb_raise(rb_eTypeError, "GSL::Vector::Complex, operation not defined");
1166
+ break;
1167
+ }
1168
+ }
1169
+
1170
+ /* 2.Aug.2004 */
1171
+ static VALUE rb_gsl_vector_complex_inner_product(int argc, VALUE *argv, VALUE obj)
1172
+ {
1173
+ gsl_vector_complex *v = NULL, *v2 = NULL;
1174
+ gsl_complex prod, a, b, *z = NULL;
1175
+ size_t i;
1176
+ switch (TYPE(obj)) {
1177
+ case T_MODULE:
1178
+ case T_CLASS:
1179
+ case T_OBJECT:
1180
+ if (argc != 2) rb_raise(rb_eArgError, "wrong number of arguments (%d for 2)",
1181
+ argc);
1182
+ if (!VECTOR_COMPLEX_ROW_P(argv[0]))
1183
+ rb_raise(rb_eTypeError, "wrong argument type %s (GSL::Vector::Complex expected)",
1184
+ rb_class2name(CLASS_OF(argv[0])));
1185
+ if (!VECTOR_COMPLEX_COL_P(argv[1]))
1186
+ rb_raise(rb_eTypeError, "wrong argument type %s (GSL::Vector::Complex::Col expected)",
1187
+ rb_class2name(CLASS_OF(argv[1])));
1188
+ Data_Get_Struct(argv[0], gsl_vector_complex, v);
1189
+ Data_Get_Struct(argv[1], gsl_vector_complex, v2);
1190
+ break;
1191
+ default:
1192
+ if (argc != 1) rb_raise(rb_eArgError, "wrong number of arguments (%d for 1)",
1193
+ argc);
1194
+ if (!VECTOR_COMPLEX_COL_P(argv[0]))
1195
+ rb_raise(rb_eTypeError, "wrong argument type %s (GSL::Vector::Complex::Col expected)",
1196
+ rb_class2name(CLASS_OF(argv[0])));
1197
+ Data_Get_Struct(obj, gsl_vector_complex, v);
1198
+ Data_Get_Struct(argv[0], gsl_vector_complex, v2);
1199
+ break;
1200
+ }
1201
+ if (v->size != v2->size) rb_raise(rb_eRangeError, "vector lengths are different.");
1202
+ prod = gsl_complex_rect(0.0, 0.0);
1203
+ for (i = 0; i < v->size; i++) {
1204
+ a = gsl_vector_complex_get(v, i);
1205
+ b = gsl_vector_complex_get(v2, i);
1206
+ prod = gsl_complex_add(prod, gsl_complex_mul(a, b));
1207
+ }
1208
+ z = ALLOC(gsl_complex);
1209
+ *z = prod;
1210
+ return Data_Wrap_Struct(cgsl_complex, 0, free, z);
1211
+ }
1212
+
1213
+ static VALUE rb_gsl_vector_complex_product_to_m(int argc, VALUE *argv, VALUE obj)
1214
+ {
1215
+ gsl_vector_complex *v = NULL, *v2 = NULL;
1216
+ gsl_matrix_complex *m = NULL;
1217
+ gsl_complex a, b;
1218
+ size_t i, j;
1219
+ switch (TYPE(obj)) {
1220
+ case T_MODULE:
1221
+ case T_CLASS:
1222
+ case T_OBJECT:
1223
+ if (argc != 2) rb_raise(rb_eArgError, "wrong number of arguments (%d for 2)",
1224
+ argc);
1225
+ if (!VECTOR_COMPLEX_COL_P(argv[0]))
1226
+ rb_raise(rb_eTypeError,
1227
+ "wrong argument type %s (GSL::Vector::Complex::Col expected)",
1228
+ rb_class2name(CLASS_OF(argv[0])));
1229
+ if (!VECTOR_COMPLEX_ROW_P(argv[1]))
1230
+ rb_raise(rb_eTypeError,
1231
+ "wrong argument type %s (GSL::Vector::Complex expected)",
1232
+ rb_class2name(CLASS_OF(argv[1])));
1233
+ Data_Get_Struct(argv[0], gsl_vector_complex, v);
1234
+ Data_Get_Struct(argv[1], gsl_vector_complex, v2);
1235
+ break;
1236
+ default:
1237
+ if (argc != 1) rb_raise(rb_eArgError, "wrong number of arguments (%d for 1)",
1238
+ argc);
1239
+ if (!VECTOR_COMPLEX_ROW_P(argv[0]))
1240
+ rb_raise(rb_eTypeError,
1241
+ "wrong argument type %s (GSL::Vector::Complex expected)",
1242
+ rb_class2name(CLASS_OF(argv[0])));
1243
+ Data_Get_Struct(obj, gsl_vector_complex, v);
1244
+ Data_Get_Struct(argv[0], gsl_vector_complex, v2);
1245
+ break;
1246
+ }
1247
+ m = gsl_matrix_complex_alloc(v->size, v2->size);
1248
+ for (i = 0; i < v->size; i++) {
1249
+ for (j = 0; j < v2->size; j++) {
1250
+ a = gsl_vector_complex_get(v, i);
1251
+ b = gsl_vector_complex_get(v2, j);
1252
+ gsl_matrix_complex_set(m, i, j, gsl_complex_mul(a, b));
1253
+ }
1254
+ }
1255
+ return Data_Wrap_Struct(cgsl_matrix_complex, 0, gsl_matrix_complex_free, m);
1256
+ }
1257
+
1258
+ static VALUE rb_gsl_vector_complex_uplus(VALUE obj)
1259
+ {
1260
+ return obj;
1261
+ }
1262
+
1263
+ static VALUE rb_gsl_vector_complex_uminus(VALUE obj)
1264
+ {
1265
+ gsl_vector_complex *v = NULL, *vnew;
1266
+ size_t i;
1267
+ Data_Get_Struct(obj, gsl_vector_complex, v);
1268
+ vnew = gsl_vector_complex_alloc(v->size);
1269
+ for (i = 0; i < v->size; i++) {
1270
+ gsl_vector_complex_set(vnew, i, gsl_complex_negative(gsl_vector_complex_get(v, i)));
1271
+ }
1272
+ return Data_Wrap_Struct(VECTOR_COMPLEX_ROW_COL(obj), 0, gsl_vector_complex_free, vnew);
1273
+ }
1274
+
1275
+ /*****/
1276
+ static VALUE rb_gsl_vector_complex_XXX(VALUE obj, double (*f)(gsl_complex))
1277
+ {
1278
+ gsl_vector_complex *m;
1279
+ gsl_vector *v;
1280
+ gsl_complex c;
1281
+ size_t i;
1282
+ Data_Get_Struct(obj, gsl_vector_complex, m);
1283
+ v = gsl_vector_alloc(m->size);
1284
+ for (i = 0; i < m->size; i++) {
1285
+ c = gsl_vector_complex_get(m, i);
1286
+ gsl_vector_set(v, i, (*f)(c));
1287
+ }
1288
+ return Data_Wrap_Struct(cgsl_vector, 0, gsl_vector_free, v);
1289
+ }
1290
+
1291
+ static VALUE rb_gsl_vector_complex_XXXz(VALUE obj, gsl_complex (*f)(gsl_complex))
1292
+ {
1293
+ gsl_vector_complex *m, *v;
1294
+ gsl_complex c;
1295
+ size_t i;
1296
+ Data_Get_Struct(obj, gsl_vector_complex, m);
1297
+ v = gsl_vector_complex_alloc(m->size);
1298
+ for (i = 0; i < m->size; i++) {
1299
+ c = gsl_vector_complex_get(m, i);
1300
+ gsl_vector_complex_set(v, i, (*f)(c));
1301
+ }
1302
+ return Data_Wrap_Struct(cgsl_vector_complex, 0, gsl_vector_complex_free, v);
1303
+ }
1304
+
1305
+ static VALUE rb_gsl_vector_complex_XXXz2(VALUE obj, VALUE a,
1306
+ gsl_complex (*f)(gsl_complex, gsl_complex))
1307
+ {
1308
+ gsl_vector_complex *m, *v;
1309
+ gsl_complex c, *z;
1310
+ size_t i;
1311
+ CHECK_COMPLEX(a);
1312
+ Data_Get_Struct(obj, gsl_vector_complex, m);
1313
+ Data_Get_Struct(a, gsl_complex, z);
1314
+ v = gsl_vector_complex_alloc(m->size);
1315
+ for (i = 0; i < m->size; i++) {
1316
+ c = gsl_vector_complex_get(m, i);
1317
+ gsl_vector_complex_set(v, i, (*f)(c, *z));
1318
+ }
1319
+ return Data_Wrap_Struct(cgsl_vector_complex, 0, gsl_vector_complex_free, v);
1320
+ }
1321
+
1322
+ static VALUE rb_gsl_vector_complex_abs2(VALUE obj)
1323
+ {
1324
+ return rb_gsl_vector_complex_XXX(obj, gsl_complex_abs2);
1325
+ }
1326
+
1327
+ static VALUE rb_gsl_vector_complex_abs(VALUE obj)
1328
+ {
1329
+ return rb_gsl_vector_complex_XXX(obj, gsl_complex_abs);
1330
+ }
1331
+
1332
+ static VALUE rb_gsl_vector_complex_logabs(VALUE obj)
1333
+ {
1334
+ return rb_gsl_vector_complex_XXX(obj, gsl_complex_logabs);
1335
+ }
1336
+
1337
+ static VALUE rb_gsl_vector_complex_arg(VALUE obj)
1338
+ {
1339
+ return rb_gsl_vector_complex_XXX(obj, gsl_complex_arg);
1340
+ }
1341
+
1342
+ static VALUE rb_gsl_vector_complex_sqrt(VALUE obj)
1343
+ {
1344
+ return rb_gsl_vector_complex_XXXz(obj, gsl_complex_sqrt);
1345
+ }
1346
+
1347
+ static VALUE rb_gsl_vector_complex_exp(VALUE obj)
1348
+ {
1349
+ return rb_gsl_vector_complex_XXXz(obj, gsl_complex_exp);
1350
+ }
1351
+
1352
+ static VALUE rb_gsl_vector_complex_pow(VALUE obj, VALUE a)
1353
+ {
1354
+ return rb_gsl_vector_complex_XXXz2(obj, a, gsl_complex_pow);
1355
+ }
1356
+
1357
+ static VALUE rb_gsl_vector_complex_log(VALUE obj)
1358
+ {
1359
+ return rb_gsl_vector_complex_XXXz(obj, gsl_complex_log);
1360
+ }
1361
+
1362
+ static VALUE rb_gsl_vector_complex_log10(VALUE obj)
1363
+ {
1364
+ return rb_gsl_vector_complex_XXXz(obj, gsl_complex_log10);
1365
+ }
1366
+
1367
+ static VALUE rb_gsl_vector_complex_log_b(VALUE obj, VALUE a)
1368
+ {
1369
+ return rb_gsl_vector_complex_XXXz2(obj, a, gsl_complex_log_b);
1370
+ }
1371
+
1372
+ /* gsl_vector_complex_sum */
1373
+ static gsl_complex rb_gsl_vector_complex_sum_gsl(gsl_vector_complex * v)
1374
+ {
1375
+ size_t i;
1376
+ gsl_complex z = gsl_complex_rect(0.0,0.0);
1377
+
1378
+ for(i=0; i<v->size; i++) {
1379
+ z = gsl_complex_add(z, gsl_vector_complex_get(v,i));
1380
+ }
1381
+ return z;
1382
+ }
1383
+
1384
+ /* gsl_vector_complex_mean */
1385
+ static gsl_complex rb_gsl_vector_complex_mean_gsl(gsl_vector_complex * v)
1386
+ {
1387
+ gsl_complex z = rb_gsl_vector_complex_sum_gsl(v);
1388
+ return gsl_complex_div_real(z, (double)v->size);
1389
+ }
1390
+
1391
+ /* gsl_vector_complex_tss_m */
1392
+ static double rb_gsl_vector_complex_tss_m_gsl(gsl_vector_complex * v, gsl_complex mean)
1393
+ {
1394
+ size_t i;
1395
+ double tss = 0.0;
1396
+
1397
+ for(i=0; i<v->size; i++) {
1398
+ tss += gsl_complex_abs2(gsl_complex_sub(gsl_vector_complex_get(v,i), mean));
1399
+ }
1400
+ return tss;
1401
+ }
1402
+
1403
+ /* gsl_vector_complex_variance_m */
1404
+ static double rb_gsl_vector_complex_variance_m_gsl(gsl_vector_complex * v, gsl_complex mean)
1405
+ {
1406
+ double tss = rb_gsl_vector_complex_tss_m_gsl(v, mean);
1407
+ return tss / (double)(v->size - 1);
1408
+ }
1409
+
1410
+ /* gsl_vector_complex_variance_with_fixed_mean */
1411
+ static double rb_gsl_vector_complex_variance_fm_gsl(gsl_vector_complex * v, gsl_complex mean)
1412
+ {
1413
+ double tss = rb_gsl_vector_complex_tss_m_gsl(v, mean);
1414
+ return tss / (double)(v->size);
1415
+ }
1416
+
1417
+ /* gsl_vector_complex_sd_m */
1418
+ static double rb_gsl_vector_complex_sd_m_gsl(gsl_vector_complex * v, gsl_complex mean)
1419
+ {
1420
+ double var = rb_gsl_vector_complex_variance_m_gsl(v, mean);
1421
+ return sqrt(var);
1422
+ }
1423
+
1424
+ /* gsl_vector_complex_sd_with_fixed_mean */
1425
+ static double rb_gsl_vector_complex_sd_fm_gsl(gsl_vector_complex * v, gsl_complex mean)
1426
+ {
1427
+ double var = rb_gsl_vector_complex_variance_fm_gsl(v, mean);
1428
+ return sqrt(var);
1429
+ }
1430
+
1431
+ /* gsl_vector_complex_tss */
1432
+ static double rb_gsl_vector_complex_tss_gsl(gsl_vector_complex * v)
1433
+ {
1434
+ gsl_complex mean = rb_gsl_vector_complex_mean_gsl(v);
1435
+ return rb_gsl_vector_complex_tss_m_gsl(v, mean);
1436
+ }
1437
+
1438
+ /* gsl_vector_complex_variance */
1439
+ static double rb_gsl_vector_complex_variance_gsl(gsl_vector_complex * v)
1440
+ {
1441
+ double tss = rb_gsl_vector_complex_tss_gsl(v);
1442
+ return tss / (double)(v->size - 1);
1443
+ }
1444
+
1445
+ /* gsl_vector_complex_sd */
1446
+ static double rb_gsl_vector_complex_sd_gsl(gsl_vector_complex * v)
1447
+ {
1448
+ double var = rb_gsl_vector_complex_variance_gsl(v);
1449
+ return sqrt(var);
1450
+ }
1451
+
1452
+ /* Wrapper around stats funcs with prototype like
1453
+ * "gsl_complex func(gsl_vector_complex *v)"
1454
+ * (e.g. sum and mean)
1455
+ */
1456
+ static VALUE rb_gsl_vector_complex_z_stats_v(VALUE obj,
1457
+ gsl_complex (*func)(gsl_vector_complex*))
1458
+ {
1459
+ gsl_vector_complex * v;
1460
+ gsl_complex * zp;
1461
+ VALUE zv;
1462
+
1463
+ CHECK_VECTOR_COMPLEX(obj);
1464
+ Data_Get_Struct(obj, gsl_vector_complex, v);
1465
+
1466
+ zv = Data_Make_Struct(cgsl_complex, gsl_complex, 0, free, zp);
1467
+ *zp = func(v);
1468
+
1469
+ return zv;
1470
+ }
1471
+
1472
+ /* Wrapper around stats funcs with prototype like
1473
+ * "double func(gsl_vector_complex *v)"
1474
+ * (e.g. tss, variance, sd)
1475
+ */
1476
+ static VALUE rb_gsl_vector_complex_d_stats_v(VALUE obj,
1477
+ double (*func)(gsl_vector_complex*))
1478
+ {
1479
+ gsl_vector_complex * v;
1480
+ double d;
1481
+
1482
+ CHECK_VECTOR_COMPLEX(obj);
1483
+ Data_Get_Struct(obj, gsl_vector_complex, v);
1484
+
1485
+ d = func(v);
1486
+
1487
+ return rb_float_new(d);
1488
+ }
1489
+
1490
+ /* Wrapper around stats funcs with prototype like
1491
+ * "double func(gsl_vector_complex *v, gsl_complex z)"
1492
+ * (e.g. tss_m, variance_m, sd_m, variance_fm, sd_fm)
1493
+ */
1494
+ static VALUE rb_gsl_vector_complex_d_stats_v_z(VALUE obj, VALUE arg,
1495
+ double (*func)(gsl_vector_complex*, gsl_complex))
1496
+ {
1497
+ gsl_vector_complex * v;
1498
+ gsl_complex z;
1499
+ gsl_complex * zp;
1500
+ double d;
1501
+
1502
+ CHECK_VECTOR_COMPLEX(obj);
1503
+ Data_Get_Struct(obj, gsl_vector_complex, v);
1504
+
1505
+
1506
+ switch (TYPE(arg)) {
1507
+ case T_FLOAT:
1508
+ case T_FIXNUM:
1509
+ case T_BIGNUM:
1510
+ z = gsl_complex_rect(NUM2DBL(arg), 0.0);
1511
+ zp = &z;
1512
+ break;
1513
+ default:
1514
+ CHECK_COMPLEX(arg);
1515
+ Data_Get_Struct(arg, gsl_complex, zp);
1516
+ break;
1517
+ }
1518
+
1519
+ d = func(v,*zp);
1520
+
1521
+ return rb_float_new(d);
1522
+ }
1523
+
1524
+ static VALUE rb_gsl_vector_complex_sum(VALUE obj)
1525
+ {
1526
+ return rb_gsl_vector_complex_z_stats_v(obj, rb_gsl_vector_complex_sum_gsl);
1527
+ }
1528
+
1529
+ static VALUE rb_gsl_vector_complex_mean(VALUE obj)
1530
+ {
1531
+ return rb_gsl_vector_complex_z_stats_v(obj, rb_gsl_vector_complex_mean_gsl);
1532
+ }
1533
+
1534
+ static VALUE rb_gsl_vector_complex_tss_m(VALUE obj, VALUE arg)
1535
+ {
1536
+ return rb_gsl_vector_complex_d_stats_v_z(obj, arg, rb_gsl_vector_complex_tss_m_gsl);
1537
+ }
1538
+
1539
+ static VALUE rb_gsl_vector_complex_variance_m(VALUE obj, VALUE arg)
1540
+ {
1541
+ return rb_gsl_vector_complex_d_stats_v_z(obj, arg, rb_gsl_vector_complex_variance_m_gsl);
1542
+ }
1543
+
1544
+ static VALUE rb_gsl_vector_complex_variance_fm(VALUE obj, VALUE arg)
1545
+ {
1546
+ return rb_gsl_vector_complex_d_stats_v_z(obj, arg, rb_gsl_vector_complex_variance_fm_gsl);
1547
+ }
1548
+
1549
+ static VALUE rb_gsl_vector_complex_sd_m(VALUE obj, VALUE arg)
1550
+ {
1551
+ return rb_gsl_vector_complex_d_stats_v_z(obj, arg, rb_gsl_vector_complex_sd_m_gsl);
1552
+ }
1553
+
1554
+ static VALUE rb_gsl_vector_complex_sd_fm(VALUE obj, VALUE arg)
1555
+ {
1556
+ return rb_gsl_vector_complex_d_stats_v_z(obj, arg, rb_gsl_vector_complex_sd_fm_gsl);
1557
+ }
1558
+
1559
+ static VALUE rb_gsl_vector_complex_tss(VALUE obj)
1560
+ {
1561
+ return rb_gsl_vector_complex_d_stats_v(obj, rb_gsl_vector_complex_tss_gsl);
1562
+ }
1563
+
1564
+ static VALUE rb_gsl_vector_complex_variance(VALUE obj)
1565
+ {
1566
+ return rb_gsl_vector_complex_d_stats_v(obj, rb_gsl_vector_complex_variance_gsl);
1567
+ }
1568
+
1569
+ static VALUE rb_gsl_vector_complex_sd(VALUE obj)
1570
+ {
1571
+ return rb_gsl_vector_complex_d_stats_v(obj, rb_gsl_vector_complex_sd_gsl);
1572
+ }
1573
+
1574
+ /*****/
1575
+
1576
+ static VALUE rb_gsl_vector_complex_sin(VALUE obj)
1577
+ {
1578
+ return rb_gsl_vector_complex_XXXz(obj, gsl_complex_sin);
1579
+ }
1580
+
1581
+ static VALUE rb_gsl_vector_complex_cos(VALUE obj)
1582
+ {
1583
+ return rb_gsl_vector_complex_XXXz(obj, gsl_complex_cos);
1584
+ }
1585
+
1586
+ static VALUE rb_gsl_vector_complex_tan(VALUE obj)
1587
+ {
1588
+ return rb_gsl_vector_complex_XXXz(obj, gsl_complex_tan);
1589
+ }
1590
+
1591
+ static VALUE rb_gsl_vector_complex_sec(VALUE obj)
1592
+ {
1593
+ return rb_gsl_vector_complex_XXXz(obj, gsl_complex_sec);
1594
+ }
1595
+
1596
+ static VALUE rb_gsl_vector_complex_csc(VALUE obj)
1597
+ {
1598
+ return rb_gsl_vector_complex_XXXz(obj, gsl_complex_csc);
1599
+ }
1600
+ static VALUE rb_gsl_vector_complex_cot(VALUE obj)
1601
+ {
1602
+ return rb_gsl_vector_complex_XXXz(obj, gsl_complex_cot);
1603
+ }
1604
+
1605
+ static VALUE rb_gsl_vector_complex_arcsin(VALUE obj)
1606
+ {
1607
+ return rb_gsl_vector_complex_XXXz(obj, gsl_complex_arcsin);
1608
+ }
1609
+
1610
+ static VALUE rb_gsl_vector_complex_arccos(VALUE obj)
1611
+ {
1612
+ return rb_gsl_vector_complex_XXXz(obj, gsl_complex_arccos);
1613
+ }
1614
+
1615
+ static VALUE rb_gsl_vector_complex_arctan(VALUE obj)
1616
+ {
1617
+ return rb_gsl_vector_complex_XXXz(obj, gsl_complex_arctan);
1618
+ }
1619
+
1620
+ static VALUE rb_gsl_vector_complex_arcsec(VALUE obj)
1621
+ {
1622
+ return rb_gsl_vector_complex_XXXz(obj, gsl_complex_arcsec);
1623
+ }
1624
+
1625
+ static VALUE rb_gsl_vector_complex_arccsc(VALUE obj)
1626
+ {
1627
+ return rb_gsl_vector_complex_XXXz(obj, gsl_complex_arccsc);
1628
+ }
1629
+
1630
+ static VALUE rb_gsl_vector_complex_arccot(VALUE obj)
1631
+ {
1632
+ return rb_gsl_vector_complex_XXXz(obj, gsl_complex_arccot);
1633
+ }
1634
+
1635
+ static VALUE rb_gsl_vector_complex_sinh(VALUE obj)
1636
+ {
1637
+ return rb_gsl_vector_complex_XXXz(obj, gsl_complex_sinh);
1638
+ }
1639
+
1640
+ static VALUE rb_gsl_vector_complex_cosh(VALUE obj)
1641
+ {
1642
+ return rb_gsl_vector_complex_XXXz(obj, gsl_complex_cosh);
1643
+ }
1644
+
1645
+ static VALUE rb_gsl_vector_complex_tanh(VALUE obj)
1646
+ {
1647
+ return rb_gsl_vector_complex_XXXz(obj, gsl_complex_tanh);
1648
+ }
1649
+
1650
+ static VALUE rb_gsl_vector_complex_sech(VALUE obj)
1651
+ {
1652
+ return rb_gsl_vector_complex_XXXz(obj, gsl_complex_sech);
1653
+ }
1654
+
1655
+ static VALUE rb_gsl_vector_complex_csch(VALUE obj)
1656
+ {
1657
+ return rb_gsl_vector_complex_XXXz(obj, gsl_complex_csch);
1658
+ }
1659
+
1660
+ static VALUE rb_gsl_vector_complex_coth(VALUE obj)
1661
+ {
1662
+ return rb_gsl_vector_complex_XXXz(obj, gsl_complex_coth);
1663
+ }
1664
+
1665
+ static VALUE rb_gsl_vector_complex_arcsinh(VALUE obj)
1666
+ {
1667
+ return rb_gsl_vector_complex_XXXz(obj, gsl_complex_arcsinh);
1668
+ }
1669
+
1670
+ static VALUE rb_gsl_vector_complex_arccosh(VALUE obj)
1671
+ {
1672
+ return rb_gsl_vector_complex_XXXz(obj, gsl_complex_arccosh);
1673
+ }
1674
+
1675
+ static VALUE rb_gsl_vector_complex_arctanh(VALUE obj)
1676
+ {
1677
+ return rb_gsl_vector_complex_XXXz(obj, gsl_complex_arctanh);
1678
+ }
1679
+
1680
+ static VALUE rb_gsl_vector_complex_arcsech(VALUE obj)
1681
+ {
1682
+ return rb_gsl_vector_complex_XXXz(obj, gsl_complex_arcsech);
1683
+ }
1684
+
1685
+ static VALUE rb_gsl_vector_complex_arccsch(VALUE obj)
1686
+ {
1687
+ return rb_gsl_vector_complex_XXXz(obj, gsl_complex_arccsch);
1688
+ }
1689
+
1690
+ static VALUE rb_gsl_vector_complex_arccoth(VALUE obj)
1691
+ {
1692
+ return rb_gsl_vector_complex_XXXz(obj, gsl_complex_arccoth);
1693
+ }
1694
+
1695
+ static VALUE rb_gsl_vector_complex_concat(VALUE obj, VALUE other)
1696
+ {
1697
+ gsl_vector_complex *v = NULL, *v2 = NULL, *vnew = NULL;
1698
+ gsl_vector_complex_view vv;
1699
+ gsl_complex tmp;
1700
+ VALUE x;
1701
+ double beg, end;
1702
+ int step;
1703
+ size_t i, size2;
1704
+
1705
+ Data_Get_Struct(obj, gsl_vector_complex, v);
1706
+
1707
+ switch(TYPE(other)) {
1708
+ case T_FIXNUM:
1709
+ case T_BIGNUM:
1710
+ case T_FLOAT:
1711
+ vnew = gsl_vector_complex_alloc(v->size + 1);
1712
+ vv = gsl_vector_complex_subvector(vnew, 0, v->size);
1713
+ gsl_vector_complex_memcpy(&vv.vector, v);
1714
+ gsl_vector_complex_set(vnew, v->size, rb_gsl_obj_to_gsl_complex(other, NULL));
1715
+ break;
1716
+
1717
+ case T_ARRAY:
1718
+ size2 = RARRAY(other)->len;
1719
+ vnew = gsl_vector_complex_alloc(v->size + size2);
1720
+ vv = gsl_vector_complex_subvector(vnew, 0, v->size);
1721
+ gsl_vector_complex_memcpy(&vv.vector, v);
1722
+ for (i = 0; i < size2; i++) {
1723
+ x = rb_ary_entry(other, i);
1724
+ gsl_vector_complex_set(vnew, v->size + i, rb_gsl_obj_to_gsl_complex(x, NULL));
1725
+ }
1726
+ break;
1727
+
1728
+ default:
1729
+ if(rb_obj_is_kind_of(other, cgsl_complex)) {
1730
+ vnew = gsl_vector_complex_alloc(v->size + 1);
1731
+ vv = gsl_vector_complex_subvector(vnew, 0, v->size);
1732
+ gsl_vector_complex_memcpy(&vv.vector, v);
1733
+ gsl_vector_complex_set(vnew, v->size, rb_gsl_obj_to_gsl_complex(other, NULL));
1734
+ } else if(rb_obj_is_kind_of(other, rb_cRange)) {
1735
+ get_range_beg_en_n(other, &beg, &end, &size2, &step);
1736
+ vnew = gsl_vector_complex_alloc(v->size + size2);
1737
+ vv = gsl_vector_complex_subvector(vnew, 0, v->size);
1738
+ gsl_vector_complex_memcpy(&vv.vector, v);
1739
+ GSL_SET_COMPLEX(&tmp, beg, 0.0);
1740
+ for (i = 0; i < size2; i++) {
1741
+ gsl_vector_complex_set(vnew, v->size + i, tmp);
1742
+ GSL_SET_REAL(&tmp, GSL_REAL(tmp) + step);
1743
+ }
1744
+ } else if (rb_obj_is_kind_of(other, cgsl_vector_complex)) {
1745
+ Data_Get_Struct(other, gsl_vector_complex, v2);
1746
+ size2 = v2->size;
1747
+ vnew = gsl_vector_complex_alloc(v->size + size2);
1748
+ vv = gsl_vector_complex_subvector(vnew, 0, v->size);
1749
+ gsl_vector_complex_memcpy(&vv.vector, v);
1750
+ vv = gsl_vector_complex_subvector(vnew, v->size, size2);
1751
+ gsl_vector_complex_memcpy(&vv.vector, v2);
1752
+ } else {
1753
+ rb_raise(rb_eTypeError, "wrong argument type %s (Array, Numeric, Range, GSL::Complex, or %s expected)",
1754
+ rb_class2name(CLASS_OF(other)), rb_class2name(cgsl_vector_complex));
1755
+ }
1756
+ break;
1757
+ }
1758
+
1759
+ return Data_Wrap_Struct(VECTOR_COMPLEX_ROW_COL(obj), 0, gsl_vector_complex_free, vnew);
1760
+ }
1761
+
1762
+ static VALUE rb_gsl_vector_complex_block(VALUE obj)
1763
+ {
1764
+ gsl_vector_complex *v = NULL;
1765
+ Data_Get_Struct(obj, gsl_vector_complex, v);
1766
+ return Data_Wrap_Struct(cgsl_block_complex, 0, NULL, v->block);
1767
+ }
1768
+
1769
+ static VALUE rb_gsl_vector_complex_indgen_bang(int argc, VALUE *argv[], VALUE obj)
1770
+ {
1771
+ gsl_vector_complex *v = NULL;
1772
+ double start = 0.0, step = 1.0, x;
1773
+ size_t i;
1774
+ switch (argc) {
1775
+ case 0:
1776
+ break;
1777
+ case 1:
1778
+ start = NUM2DBL(argv[0]);
1779
+ break;
1780
+ case 2:
1781
+ start = NUM2DBL(argv[0]);
1782
+ step = NUM2DBL(argv[1]);
1783
+ break;
1784
+ default:
1785
+ rb_raise(rb_eArgError, "Wrong number of arguments (%d for 0-2)", argc);
1786
+ }
1787
+ Data_Get_Struct(obj, gsl_vector_complex, v);
1788
+ for (i = 0, x = start; i < v->size; i++, x += step) {
1789
+ gsl_vector_complex_set(v, i, gsl_complex_rect(x,0));
1790
+ }
1791
+ return obj;
1792
+ }
1793
+
1794
+ static VALUE rb_gsl_vector_complex_indgen(int argc, VALUE *argv, VALUE obj)
1795
+ {
1796
+ gsl_vector_complex *v = NULL, *vnew;
1797
+ double start = 0, step = 1, x;
1798
+ size_t i;
1799
+ switch (argc) {
1800
+ case 0:
1801
+ break;
1802
+ case 1:
1803
+ start = FIX2INT(argv[0]);
1804
+ break;
1805
+ case 2:
1806
+ start = FIX2INT(argv[0]);
1807
+ step = FIX2INT(argv[1]);
1808
+ break;
1809
+ default:
1810
+ rb_raise(rb_eArgError, "Wrong number of arguments (%d for 0-2)", argc);
1811
+ }
1812
+ Data_Get_Struct(obj, gsl_vector_complex, v);
1813
+ vnew = gsl_vector_complex_calloc(v->size);
1814
+ for (i = 0, x = start; i < vnew->size; i++, x += step) {
1815
+ gsl_vector_complex_set(vnew, i, gsl_complex_rect(x,0));
1816
+ }
1817
+ return Data_Wrap_Struct(cgsl_vector_complex, 0, gsl_vector_complex_free, vnew);
1818
+ }
1819
+
1820
+ static VALUE rb_gsl_vector_complex_indgen_singleton(int argc, VALUE *argv, VALUE obj)
1821
+ {
1822
+ gsl_vector_complex *vnew;
1823
+ size_t n, start = 0, step = 1;
1824
+ size_t i, j;
1825
+ switch (argc) {
1826
+ case 1:
1827
+ n = (size_t) FIX2INT(argv[0]);
1828
+ break;
1829
+ case 2:
1830
+ n = (size_t) FIX2INT(argv[0]);
1831
+ start = FIX2INT(argv[1]);
1832
+ break;
1833
+ case 3:
1834
+ n = (size_t) FIX2INT(argv[0]);
1835
+ start = FIX2INT(argv[1]);
1836
+ step = FIX2INT(argv[2]);
1837
+ break;
1838
+ default:
1839
+ rb_raise(rb_eArgError, "Wrong number of arguments (%d for 0-3)",argc);
1840
+ }
1841
+ vnew = gsl_vector_complex_calloc(n);
1842
+ for (i = 0, j = start; i < vnew->size; i++, j += step) {
1843
+ gsl_vector_complex_set(vnew, i, gsl_complex_rect((double)j,0));
1844
+ }
1845
+ return Data_Wrap_Struct(cgsl_vector_complex, 0, gsl_vector_complex_free, vnew);
1846
+ }
1847
+
1848
+
1849
+ static VALUE rb_gsl_vector_complex_zip(int argc, VALUE *argv, VALUE obj)
1850
+ {
1851
+ gsl_vector_complex *v0, **vp, *vnew;
1852
+ VALUE ary;
1853
+ size_t i, j;
1854
+ int argc2;
1855
+ VALUE *argv2;
1856
+ gsl_complex zzero = gsl_complex_rect(0, 0);
1857
+ if (VECTOR_COMPLEX_P(obj)) {
1858
+ Data_Get_Struct(obj, gsl_vector_complex, v0);
1859
+ argc2 = argc;
1860
+ argv2 = argv;
1861
+ } else {
1862
+ if (argc < 1) rb_raise(rb_eArgError, "Too few arguments.");
1863
+ Data_Get_Struct(argv[0], gsl_vector_complex, v0);
1864
+ argc2 = argc - 1;
1865
+ argv2 = argv + 1;
1866
+ }
1867
+ for (i = 0; i < argc2; i++) {
1868
+ CHECK_VECTOR_COMPLEX(argv2[i]);
1869
+ }
1870
+ vp = (gsl_vector_complex**) malloc(sizeof(gsl_vector_complex**));
1871
+ for (i = 0; i < argc2; i++) {
1872
+ Data_Get_Struct(argv2[i], gsl_vector_complex, vp[i]);
1873
+ }
1874
+ ary = rb_ary_new2(v0->size);
1875
+ for (i = 0; i < v0->size; i++) {
1876
+ vnew = gsl_vector_complex_alloc(argc2 + 1);
1877
+ gsl_vector_complex_set(vnew, 0, gsl_vector_complex_get(v0, i));
1878
+ for (j = 0; j < argc2; j++) {
1879
+ if (i < vp[j]->size) {
1880
+ gsl_vector_complex_set(vnew, j+1, gsl_vector_complex_get(vp[j], i));
1881
+ } else {
1882
+ gsl_vector_complex_set(vnew, j+1, zzero);
1883
+ }
1884
+ }
1885
+ rb_ary_store(ary, i, Data_Wrap_Struct(cgsl_vector_complex, 0, gsl_vector_complex_free, vnew));
1886
+ }
1887
+
1888
+ free((gsl_vector_complex**) vp);
1889
+ return ary;
1890
+ }
1891
+
1892
+ static int gsl_vector_complex_equal(const gsl_vector_complex *v1,
1893
+ const gsl_vector_complex *v2, double eps)
1894
+ {
1895
+ gsl_complex z1, z2;
1896
+ size_t i;
1897
+ if (v1->size != v2->size) return 0;
1898
+ for (i = 0; i < v1->size; i++) {
1899
+ z1 = gsl_vector_complex_get(v1, i);
1900
+ z2 = gsl_vector_complex_get(v2, i);
1901
+ if (!rbgsl_complex_equal(&z1, &z2, eps)) return 0;
1902
+ }
1903
+ return 1;
1904
+
1905
+ }
1906
+
1907
+ static VALUE rb_gsl_vector_complex_equal(int argc, VALUE *argv, VALUE obj)
1908
+ {
1909
+ gsl_vector_complex *v1, *v2;
1910
+ double eps = 1e-8;
1911
+ int ret;
1912
+ switch (argc) {
1913
+ case 1:
1914
+ eps = 1e-8;
1915
+ break;
1916
+ case 2:
1917
+ eps = NUM2DBL(argv[1]);
1918
+ break;
1919
+ default:
1920
+ rb_raise(rb_eArgError, "Wrong number of arguments (%d for 1 or 2)\n", argc);
1921
+ }
1922
+ Data_Get_Struct(obj, gsl_vector_complex, v1);
1923
+ CHECK_VECTOR_COMPLEX(argv[0]);
1924
+ Data_Get_Struct(argv[0], gsl_vector_complex, v2);
1925
+ ret = gsl_vector_complex_equal(v1, v2, eps);
1926
+ if (ret == 1) return Qtrue;
1927
+ else return Qfalse;
1928
+ }
1929
+
1930
+ static VALUE rb_gsl_vector_complex_not_equal(int argc, VALUE *argv, VALUE obj)
1931
+ {
1932
+ VALUE ret;
1933
+ ret = rb_gsl_vector_complex_equal(argc, argv, obj);
1934
+ if (ret == Qtrue) return Qfalse;
1935
+ else return Qtrue;
1936
+ }
1937
+
1938
+ void Init_gsl_vector_complex(VALUE module)
1939
+ {
1940
+ rb_define_singleton_method(cgsl_vector_complex, "new", rb_gsl_vector_complex_new, -1);
1941
+ rb_define_singleton_method(cgsl_vector_complex, "[]", rb_gsl_vector_complex_new, -1);
1942
+ rb_define_singleton_method(cgsl_vector_complex, "alloc", rb_gsl_vector_complex_new, -1);
1943
+ rb_define_singleton_method(cgsl_vector_complex, "calloc", rb_gsl_vector_complex_calloc, 1);
1944
+
1945
+ rb_define_singleton_method(cgsl_vector_complex_col, "new",
1946
+ rb_gsl_vector_complex_row_new, -1);
1947
+
1948
+ rb_define_method(cgsl_vector_complex, "size", rb_gsl_vector_complex_size, 0);
1949
+ rb_define_alias(cgsl_vector_complex, "len", "size");
1950
+ rb_define_alias(cgsl_vector_complex, "length", "size");
1951
+ rb_define_method(cgsl_vector_complex, "stride", rb_gsl_vector_complex_stride, 0);
1952
+ rb_define_method(cgsl_vector_complex, "owner", rb_gsl_vector_complex_owner, 0);
1953
+ rb_define_method(cgsl_vector_complex, "get", rb_gsl_vector_complex_get, -1);
1954
+ rb_define_alias(cgsl_vector_complex, "[]", "get");
1955
+ rb_define_method(cgsl_vector_complex, "ptr", rb_gsl_vector_complex_ptr, 1);
1956
+
1957
+ rb_define_method(cgsl_vector_complex, "set", rb_gsl_vector_complex_set, -1);
1958
+ rb_define_alias(cgsl_vector_complex, "[]=", "set");
1959
+ rb_define_method(cgsl_vector_complex, "set_all", rb_gsl_vector_complex_set_all, -1);
1960
+
1961
+ rb_define_method(cgsl_vector_complex, "each", rb_gsl_vector_complex_each, 0);
1962
+ rb_define_method(cgsl_vector_complex, "reverse_each", rb_gsl_vector_complex_reverse_each, 0);
1963
+ rb_define_method(cgsl_vector_complex, "each_index", rb_gsl_vector_complex_each_index, 0);
1964
+ rb_define_method(cgsl_vector_complex, "reverse_each_index", rb_gsl_vector_complex_reverse_each_index, 0);
1965
+ rb_define_method(cgsl_vector_complex, "collect", rb_gsl_vector_complex_collect, 0);
1966
+ rb_define_method(cgsl_vector_complex, "collect!", rb_gsl_vector_complex_collect_bang, 0);
1967
+ rb_define_alias(cgsl_vector_complex, "map", "collect");
1968
+ rb_define_alias(cgsl_vector_complex, "map!", "collect!");
1969
+
1970
+ rb_define_method(cgsl_vector_complex, "set_zero", rb_gsl_vector_complex_set_zero, 0);
1971
+ rb_define_method(cgsl_vector_complex, "set_basis", rb_gsl_vector_complex_set_basis, 1);
1972
+
1973
+ rb_define_method(cgsl_vector_complex, "to_s", rb_gsl_vector_complex_to_s, 0);
1974
+ rb_define_method(cgsl_vector_complex, "fprintf", rb_gsl_vector_complex_fprintf, -1);
1975
+ rb_define_method(cgsl_vector_complex, "printf", rb_gsl_vector_complex_printf, -1);
1976
+ rb_define_method(cgsl_vector_complex, "print", rb_gsl_vector_complex_print, 0);
1977
+ rb_define_method(cgsl_vector_complex, "inspect", rb_gsl_vector_complex_inspect, 0);
1978
+ rb_define_method(cgsl_vector_complex, "fwrite", rb_gsl_vector_complex_fwrite, 1);
1979
+ rb_define_method(cgsl_vector_complex, "fread", rb_gsl_vector_complex_fread, 1);
1980
+ rb_define_method(cgsl_vector_complex, "fscanf", rb_gsl_vector_complex_fscanf, 1);
1981
+
1982
+ rb_define_method(cgsl_vector_complex, "real", rb_gsl_vector_complex_real, 0);
1983
+ rb_define_alias(cgsl_vector_complex, "re", "real");
1984
+ rb_define_method(cgsl_vector_complex, "imag", rb_gsl_vector_complex_imag, 0);
1985
+ rb_define_alias(cgsl_vector_complex, "im", "imag");
1986
+
1987
+ rb_define_method(cgsl_vector_complex, "set_real", rb_gsl_vector_complex_set_real, 1);
1988
+ rb_define_alias(cgsl_vector_complex, "real=", "set_real");
1989
+ rb_define_alias(cgsl_vector_complex, "re=", "set_real");
1990
+ rb_define_method(cgsl_vector_complex, "set_imag", rb_gsl_vector_complex_set_imag, 1);
1991
+ rb_define_alias(cgsl_vector_complex, "imag=", "set_imag");
1992
+ rb_define_alias(cgsl_vector_complex, "im=", "set_imag");
1993
+
1994
+ rb_define_method(cgsl_vector_complex, "conj", rb_gsl_vector_complex_conj, 0);
1995
+ rb_define_alias(cgsl_vector_complex, "conjugate", "conj");
1996
+ rb_define_method(cgsl_vector_complex, "conj!", rb_gsl_vector_complex_conj_bang, 0);
1997
+ rb_define_alias(cgsl_vector_complex, "conjugate!", "conj!");
1998
+
1999
+ rb_define_method(cgsl_vector_complex, "to_a", rb_gsl_vector_complex_to_a, 0);
2000
+ rb_define_method(cgsl_vector_complex, "to_a2", rb_gsl_vector_complex_to_a2, 0);
2001
+
2002
+ rb_define_method(cgsl_vector_complex, "subvector", rb_gsl_vector_complex_subvector, -1);
2003
+ rb_define_alias(cgsl_vector_complex, "view", "subvector");
2004
+ rb_define_method(cgsl_vector_complex, "subvector_with_stride", rb_gsl_vector_complex_subvector_with_stride, 3);
2005
+
2006
+ rb_define_singleton_method(cgsl_vector_complex, "memcpy", rb_gsl_vector_complex_memcpy, 2);
2007
+ rb_define_method(cgsl_vector_complex, "clone", rb_gsl_vector_complex_clone, 0);
2008
+ rb_define_alias(cgsl_vector_complex, "duplicate", "clone");
2009
+ rb_define_alias(cgsl_vector_complex, "dup", "clone");
2010
+ rb_define_method(cgsl_vector_complex, "reverse!", rb_gsl_vector_complex_reverse, 0);
2011
+ rb_define_method(cgsl_vector_complex, "reverse", rb_gsl_vector_complex_reverse2, 0);
2012
+ rb_define_method(cgsl_vector_complex, "swap_elements", rb_gsl_vector_complex_swap_elements, 2);
2013
+ rb_define_method(cgsl_vector_complex, "fftshift!", rb_gsl_vector_complex_fftshift_bang, 0);
2014
+ rb_define_method(cgsl_vector_complex, "fftshift", rb_gsl_vector_complex_fftshift, 0);
2015
+ rb_define_method(cgsl_vector_complex, "ifftshift!", rb_gsl_vector_complex_ifftshift_bang, 0);
2016
+ rb_define_method(cgsl_vector_complex, "ifftshift", rb_gsl_vector_complex_ifftshift, 0);
2017
+ rb_define_method(cgsl_vector_complex, "isnull", rb_gsl_vector_complex_isnull, 0);
2018
+
2019
+ rb_define_method(cgsl_vector_complex, "matrix_view", rb_gsl_vector_complex_matrix_view, -1);
2020
+ rb_define_method(cgsl_vector_complex, "matrix_view_with_tda", rb_gsl_vector_complex_matrix_view_with_tda, -1);
2021
+
2022
+ rb_define_method(cgsl_vector_complex, "trans", rb_gsl_vector_complex_trans, 0);
2023
+ rb_define_alias(cgsl_vector_complex, "transpose", "trans");
2024
+ rb_define_method(cgsl_vector_complex, "trans!", rb_gsl_vector_complex_trans2, 0);
2025
+ rb_define_alias(cgsl_vector_complex, "transpose!", "trans!");
2026
+
2027
+ /*****/
2028
+ rb_define_alias(cgsl_vector_complex, "col", "trans");
2029
+ rb_define_alias(cgsl_vector_complex, "col!", "trans!");
2030
+
2031
+ rb_define_alias(cgsl_vector_complex_col, "row", "trans");
2032
+ rb_define_alias(cgsl_vector_complex_col, "row!", "trans!");
2033
+
2034
+ /*****/
2035
+ rb_define_method(cgsl_vector_complex, "to_real", rb_gsl_vector_complex_to_real, 0);
2036
+
2037
+ rb_define_method(cgsl_vector_complex, "add", rb_gsl_vector_complex_add, 1);
2038
+ rb_define_method(cgsl_vector_complex, "sub", rb_gsl_vector_complex_sub, 1);
2039
+ rb_define_method(cgsl_vector_complex, "mul", rb_gsl_vector_complex_mul, 1);
2040
+ rb_define_method(cgsl_vector_complex, "div", rb_gsl_vector_complex_div, 1);
2041
+ rb_define_method(cgsl_vector_complex, "add!", rb_gsl_vector_complex_add_bang, 1);
2042
+ rb_define_method(cgsl_vector_complex, "sub!", rb_gsl_vector_complex_sub_bang, 1);
2043
+ rb_define_method(cgsl_vector_complex, "mul!", rb_gsl_vector_complex_mul_bang, 1);
2044
+ rb_define_method(cgsl_vector_complex, "div!", rb_gsl_vector_complex_div_bang, 1);
2045
+
2046
+ rb_define_alias(cgsl_vector_complex, "+", "add");
2047
+ rb_define_alias(cgsl_vector_complex, "add_constant", "add");
2048
+ rb_define_alias(cgsl_vector_complex, "add_constant!", "add!");
2049
+ rb_define_alias(cgsl_vector_complex, "-", "sub");
2050
+ rb_define_alias(cgsl_vector_complex, "*", "mul");
2051
+ rb_define_alias(cgsl_vector_complex, "scale", "mul");
2052
+ rb_define_alias(cgsl_vector_complex, "scale!", "mul!");
2053
+ rb_define_alias(cgsl_vector_complex, "/", "div");
2054
+
2055
+ rb_define_method(cgsl_vector_complex, "coerce", rb_gsl_vector_complex_coerce, 1);
2056
+
2057
+ /* 2.Aug.2004 */
2058
+ rb_define_singleton_method(cgsl_vector, "inner_product", rb_gsl_vector_complex_inner_product, -1);
2059
+ rb_define_singleton_method(cgsl_vector_complex, "dot", rb_gsl_vector_complex_inner_product, -1);
2060
+ rb_define_method(cgsl_vector_complex, "inner_product", rb_gsl_vector_complex_inner_product, -1);
2061
+ /* rb_define_alias(cgsl_vector_complex, "dot", "inner_product");*/
2062
+
2063
+ /*****/
2064
+ rb_define_method(cgsl_vector_complex, "-@", rb_gsl_vector_complex_uminus, 0);
2065
+ rb_define_method(cgsl_vector_complex, "+@", rb_gsl_vector_complex_uplus, 0);
2066
+
2067
+ rb_define_method(cgsl_vector_complex, "abs2", rb_gsl_vector_complex_abs2, 0);
2068
+ rb_define_alias(cgsl_vector_complex, "square", "abs2");
2069
+ rb_define_method(cgsl_vector_complex, "abs", rb_gsl_vector_complex_abs, 0);
2070
+ rb_define_alias(cgsl_vector_complex, "amp", "abs");
2071
+ rb_define_alias(cgsl_vector_complex, "mag", "abs");
2072
+ rb_define_method(cgsl_vector_complex, "arg", rb_gsl_vector_complex_arg, 0);
2073
+ rb_define_alias(cgsl_vector_complex, "angle", "arg");
2074
+ rb_define_alias(cgsl_vector_complex, "phase", "arg");
2075
+ rb_define_method(cgsl_vector_complex, "logabs", rb_gsl_vector_complex_logabs, 0);
2076
+
2077
+ rb_define_method(cgsl_vector_complex, "sqrt", rb_gsl_vector_complex_sqrt, 0);
2078
+ rb_define_method(cgsl_vector_complex, "exp", rb_gsl_vector_complex_exp, 0);
2079
+ rb_define_method(cgsl_vector_complex, "pow", rb_gsl_vector_complex_pow, 1);
2080
+ rb_define_method(cgsl_vector_complex, "log", rb_gsl_vector_complex_log, 0);
2081
+ rb_define_method(cgsl_vector_complex, "log10", rb_gsl_vector_complex_log10, 0);
2082
+ rb_define_method(cgsl_vector_complex, "log_b", rb_gsl_vector_complex_log_b, 1);
2083
+
2084
+ rb_define_method(cgsl_vector_complex, "sum", rb_gsl_vector_complex_sum, 0);
2085
+ rb_define_method(cgsl_vector_complex, "mean", rb_gsl_vector_complex_mean, 0);
2086
+ rb_define_method(cgsl_vector_complex, "tss", rb_gsl_vector_complex_tss, 0);
2087
+ rb_define_method(cgsl_vector_complex, "tss_m", rb_gsl_vector_complex_tss_m, 1);
2088
+ rb_define_method(cgsl_vector_complex, "variance", rb_gsl_vector_complex_variance, 0);
2089
+ rb_define_method(cgsl_vector_complex, "variance_m", rb_gsl_vector_complex_variance_m, 1);
2090
+ rb_define_method(cgsl_vector_complex, "variance_fm", rb_gsl_vector_complex_variance_fm, 1);
2091
+ rb_define_method(cgsl_vector_complex, "sd", rb_gsl_vector_complex_sd, 0);
2092
+ rb_define_method(cgsl_vector_complex, "sd_m", rb_gsl_vector_complex_sd_m, 1);
2093
+ rb_define_method(cgsl_vector_complex, "sd_fm", rb_gsl_vector_complex_sd_fm, 1);
2094
+
2095
+ rb_define_method(cgsl_vector_complex, "sin", rb_gsl_vector_complex_sin, 0);
2096
+ rb_define_method(cgsl_vector_complex, "cos", rb_gsl_vector_complex_cos, 0);
2097
+ rb_define_method(cgsl_vector_complex, "tan", rb_gsl_vector_complex_tan, 0);
2098
+ rb_define_method(cgsl_vector_complex, "sec", rb_gsl_vector_complex_sec, 0);
2099
+ rb_define_method(cgsl_vector_complex, "csc", rb_gsl_vector_complex_csc, 0);
2100
+ rb_define_method(cgsl_vector_complex, "cot", rb_gsl_vector_complex_cot, 0);
2101
+
2102
+ rb_define_method(cgsl_vector_complex, "arcsin", rb_gsl_vector_complex_arcsin, 0);
2103
+ rb_define_method(cgsl_vector_complex, "arccos", rb_gsl_vector_complex_arccos, 0);
2104
+ rb_define_method(cgsl_vector_complex, "arctan", rb_gsl_vector_complex_arctan, 0);
2105
+ rb_define_method(cgsl_vector_complex, "arcsec", rb_gsl_vector_complex_arcsec, 0);
2106
+ rb_define_method(cgsl_vector_complex, "arccsc", rb_gsl_vector_complex_arccsc, 0);
2107
+ rb_define_method(cgsl_vector_complex, "arccot", rb_gsl_vector_complex_arccot, 0);
2108
+
2109
+ rb_define_method(cgsl_vector_complex, "sinh", rb_gsl_vector_complex_sinh, 0);
2110
+ rb_define_method(cgsl_vector_complex, "cosh", rb_gsl_vector_complex_cosh, 0);
2111
+ rb_define_method(cgsl_vector_complex, "tanh", rb_gsl_vector_complex_tanh, 0);
2112
+ rb_define_method(cgsl_vector_complex, "sech", rb_gsl_vector_complex_sech, 0);
2113
+ rb_define_method(cgsl_vector_complex, "csch", rb_gsl_vector_complex_csch, 0);
2114
+ rb_define_method(cgsl_vector_complex, "coth", rb_gsl_vector_complex_coth, 0);
2115
+
2116
+ rb_define_method(cgsl_vector_complex, "arcsinh", rb_gsl_vector_complex_arcsinh, 0);
2117
+ rb_define_method(cgsl_vector_complex, "arccosh", rb_gsl_vector_complex_arccosh, 0);
2118
+ rb_define_method(cgsl_vector_complex, "arctanh", rb_gsl_vector_complex_arctanh, 0);
2119
+ rb_define_method(cgsl_vector_complex, "arcsech", rb_gsl_vector_complex_arcsech, 0);
2120
+ rb_define_method(cgsl_vector_complex, "arccsch", rb_gsl_vector_complex_arccsch, 0);
2121
+ rb_define_method(cgsl_vector_complex, "arccoth", rb_gsl_vector_complex_arccoth, 0);
2122
+
2123
+ /*****/
2124
+ rb_define_method(cgsl_vector_complex, "concat", rb_gsl_vector_complex_concat, 1);
2125
+ rb_define_method(cgsl_vector_complex, "block", rb_gsl_vector_complex_block, 0);
2126
+
2127
+ rb_define_method(cgsl_vector_complex, "indgen", rb_gsl_vector_complex_indgen, -1);
2128
+ rb_define_method(cgsl_vector_complex, "indgen!", rb_gsl_vector_complex_indgen_bang, -1);
2129
+ rb_define_singleton_method(cgsl_vector_complex, "indgen", rb_gsl_vector_complex_indgen_singleton, -1);
2130
+
2131
+ rb_define_method(cgsl_vector_complex, "zip", rb_gsl_vector_complex_zip, -1);
2132
+ rb_define_singleton_method(cgsl_vector_complex, "zip", rb_gsl_vector_complex_zip, -1);
2133
+
2134
+ rb_define_method(cgsl_vector_complex, "equal?", rb_gsl_vector_complex_equal, -1);
2135
+ rb_define_alias(cgsl_vector_complex, "==", "equal?");
2136
+ rb_define_method(cgsl_vector_complex, "not_equal?", rb_gsl_vector_complex_not_equal, -1);
2137
+ rb_define_alias(cgsl_vector_complex, "!=", "not_equal?");
2138
+ }
2139
+