carray 1.1.4 → 1.1.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (231) hide show
  1. checksums.yaml +4 -4
  2. data/COPYING +56 -0
  3. data/GPL +340 -0
  4. data/Gemfile +9 -0
  5. data/Gemfile.lock +33 -0
  6. data/LEGAL +50 -0
  7. data/NOTE +73 -0
  8. data/Rakefile +20 -0
  9. data/TODO +5 -0
  10. data/ca_iter_block.c +242 -0
  11. data/ca_iter_dimension.c +287 -0
  12. data/ca_iter_window.c +202 -0
  13. data/ca_obj_array.c +1189 -0
  14. data/ca_obj_bitarray.c +523 -0
  15. data/ca_obj_bitfield.c +636 -0
  16. data/ca_obj_block.c +885 -0
  17. data/ca_obj_fake.c +405 -0
  18. data/ca_obj_farray.c +482 -0
  19. data/ca_obj_field.c +625 -0
  20. data/ca_obj_grid.c +738 -0
  21. data/ca_obj_mapping.c +614 -0
  22. data/ca_obj_object.c +777 -0
  23. data/ca_obj_reduce.c +299 -0
  24. data/ca_obj_refer.c +627 -0
  25. data/ca_obj_repeat.c +640 -0
  26. data/ca_obj_select.c +558 -0
  27. data/ca_obj_shift.c +952 -0
  28. data/ca_obj_transpose.c +582 -0
  29. data/ca_obj_unbound_repeat.c +557 -0
  30. data/ca_obj_window.c +1023 -0
  31. data/carray.h +1381 -0
  32. data/carray_access.c +1798 -0
  33. data/carray_attribute.c +903 -0
  34. data/carray_call_cfunc.c +1107 -0
  35. data/carray_cast.c +1155 -0
  36. data/carray_cast_func.rb +498 -0
  37. data/carray_class.c +132 -0
  38. data/carray_conversion.c +518 -0
  39. data/carray_copy.c +453 -0
  40. data/carray_core.c +1307 -0
  41. data/carray_element.c +572 -0
  42. data/carray_generate.c +681 -0
  43. data/carray_iterator.c +630 -0
  44. data/carray_loop.c +462 -0
  45. data/carray_mask.c +1174 -0
  46. data/carray_math.rb +834 -0
  47. data/carray_numeric.c +257 -0
  48. data/carray_operator.c +582 -0
  49. data/carray_order.c +1040 -0
  50. data/carray_random.c +529 -0
  51. data/carray_sort_addr.c +261 -0
  52. data/carray_stat.c +2102 -0
  53. data/carray_stat_proc.rb +1990 -0
  54. data/carray_test.c +602 -0
  55. data/carray_undef.c +69 -0
  56. data/carray_utils.c +740 -0
  57. data/ext/calculus/carray_calculus.c +792 -0
  58. data/ext/calculus/carray_interp.c +355 -0
  59. data/ext/calculus/extconf.rb +12 -0
  60. data/ext/calculus/lib/autoload/autoload_math_calculus.rb +2 -0
  61. data/ext/calculus/lib/math/calculus.rb +119 -0
  62. data/ext/calculus/lib/math/interp/adapter_interp1d.rb +31 -0
  63. data/ext/dataframe/API.txt +11 -0
  64. data/ext/dataframe/extconf.rb +3 -0
  65. data/ext/dataframe/lib/carray/autoload/autoload_dataframe_dataframe.rb +14 -0
  66. data/ext/dataframe/lib/carray/dataframe/dataframe.rb +1104 -0
  67. data/ext/dataframe/sample/test_uniq_sort.rb +5 -0
  68. data/ext/fortio/extconf.rb +3 -0
  69. data/ext/fortio/lib/carray/autoload/autoload_fortran_format.rb +5 -0
  70. data/ext/fortio/lib/carray/io/fortran_format.rb +43 -0
  71. data/ext/fortio/lib/fortio.rb +3 -0
  72. data/ext/fortio/lib/fortio/fortran_format.rb +603 -0
  73. data/ext/fortio/lib/fortio/fortran_format.tab.rb +536 -0
  74. data/ext/fortio/lib/fortio/fortran_format.y +215 -0
  75. data/ext/fortio/lib/fortio/fortran_namelist.rb +151 -0
  76. data/ext/fortio/lib/fortio/fortran_namelist.tab.rb +470 -0
  77. data/ext/fortio/lib/fortio/fortran_namelist.y +213 -0
  78. data/ext/fortio/lib/fortio/fortran_sequential.rb +345 -0
  79. data/ext/fortio/ruby_fortio.c +182 -0
  80. data/ext/fortio/test/test_H.rb +5 -0
  81. data/ext/fortio/test/test_T.rb +7 -0
  82. data/ext/fortio/test/test_fortran_format.rb +86 -0
  83. data/ext/fortio/test/test_namelist.rb +25 -0
  84. data/ext/fortio/test/test_sequential.rb +13 -0
  85. data/ext/fortio/test/test_sequential2.rb +13 -0
  86. data/ext/fortio/work/test.rb +10 -0
  87. data/ext/fortio/work/test_e.rb +19 -0
  88. data/ext/fortio/work/test_ep.rb +10 -0
  89. data/ext/fortio/work/test_parse.rb +12 -0
  90. data/ext/imagemap/carray_imagemap.c +495 -0
  91. data/ext/imagemap/doc/call_graph.dot +28 -0
  92. data/ext/imagemap/draw.c +567 -0
  93. data/ext/imagemap/extconf.rb +13 -0
  94. data/ext/imagemap/lib/autoload/autoload_graphics_imagemap.rb +1 -0
  95. data/ext/imagemap/lib/graphics/imagemap.rb +273 -0
  96. data/ext/imagemap/lib/image_map.rb +4 -0
  97. data/ext/imagemap/test/swath_index.rb +83 -0
  98. data/ext/imagemap/test/swath_warp.rb +99 -0
  99. data/ext/imagemap/test/test.rb +23 -0
  100. data/ext/imagemap/test/test_image.rb +42 -0
  101. data/ext/imagemap/test/test_line.rb +14 -0
  102. data/ext/imagemap/test/test_rotate.rb +17 -0
  103. data/ext/imagemap/test/test_triangle.rb +20 -0
  104. data/ext/imagemap/test/test_warp.rb +26 -0
  105. data/ext/mathfunc/carray_mathfunc.c +321 -0
  106. data/ext/mathfunc/extconf.rb +18 -0
  107. data/ext/mathfunc/lib/autoload/autoload_math_mathfunc.rb +1 -0
  108. data/ext/mathfunc/lib/math/mathfunc.rb +15 -0
  109. data/ext/mathfunc/test/test_hypot.rb +5 -0
  110. data/ext/mathfunc/test/test_j0.rb +22 -0
  111. data/ext/mathfunc/test/test_jn.rb +8 -0
  112. data/ext/mathfunc/test/test_sph.rb +9 -0
  113. data/ext/narray/README +22 -0
  114. data/ext/narray/ca_wrap_narray.c +491 -0
  115. data/ext/narray/carray_narray.c +21 -0
  116. data/ext/narray/extconf.rb +57 -0
  117. data/ext/narray/lib/autoload/autoload_math_narray.rb +1 -0
  118. data/ext/narray/lib/autoload/autoload_math_narray_miss.rb +11 -0
  119. data/ext/narray/lib/math/narray.rb +17 -0
  120. data/ext/narray/lib/math/narray_miss.rb +45 -0
  121. data/extconf.rb +3 -25
  122. data/lib/carray.rb +28 -0
  123. data/lib/carray/autoload/autoload_base.rb +23 -0
  124. data/lib/carray/autoload/autoload_graphics_gnuplot.rb +2 -0
  125. data/lib/carray/autoload/autoload_io_csv.rb +14 -0
  126. data/lib/carray/autoload/autoload_io_excel.rb +5 -0
  127. data/lib/carray/autoload/autoload_io_imagemagick.rb +6 -0
  128. data/lib/carray/autoload/autoload_io_pg.rb +6 -0
  129. data/lib/carray/autoload/autoload_io_sqlite3.rb +12 -0
  130. data/lib/carray/autoload/autoload_io_table.rb +1 -0
  131. data/lib/carray/autoload/autoload_math_histogram.rb +5 -0
  132. data/lib/carray/autoload/autoload_math_interp.rb +4 -0
  133. data/lib/carray/autoload/autoload_math_recurrence.rb +6 -0
  134. data/lib/carray/autoload/autoload_object_iterator.rb +1 -0
  135. data/lib/carray/autoload/autoload_object_link.rb +1 -0
  136. data/lib/carray/autoload/autoload_object_pack.rb +2 -0
  137. data/lib/carray/base/autoload.rb +94 -0
  138. data/lib/carray/base/basic.rb +1051 -0
  139. data/lib/carray/base/inspect.rb +252 -0
  140. data/lib/carray/base/iterator.rb +367 -0
  141. data/lib/carray/base/math.rb +403 -0
  142. data/lib/carray/base/obsolete.rb +93 -0
  143. data/lib/carray/base/serialize.rb +260 -0
  144. data/lib/carray/base/struct.rb +634 -0
  145. data/lib/carray/graphics/gnuplot.rb +2116 -0
  146. data/lib/carray/info.rb +112 -0
  147. data/lib/carray/io/csv.rb +560 -0
  148. data/lib/carray/io/excel.rb +26 -0
  149. data/lib/carray/io/imagemagick.rb +231 -0
  150. data/lib/carray/io/pg.rb +101 -0
  151. data/lib/carray/io/sqlite3.rb +202 -0
  152. data/lib/carray/io/table.rb +77 -0
  153. data/lib/carray/math/histogram.rb +179 -0
  154. data/lib/carray/math/interp.rb +57 -0
  155. data/lib/carray/math/interp/adapter_gsl_spline.rb +47 -0
  156. data/lib/carray/math/recurrence.rb +95 -0
  157. data/lib/carray/mkmf.rb +145 -0
  158. data/lib/carray/object/ca_obj_iterator.rb +52 -0
  159. data/lib/carray/object/ca_obj_link.rb +52 -0
  160. data/lib/carray/object/ca_obj_pack.rb +101 -0
  161. data/mkmath.rb +731 -0
  162. data/mt19937ar.c +182 -0
  163. data/mt19937ar.h +86 -0
  164. data/rdoc_main.rb +27 -0
  165. data/rdoc_math.rb +5 -0
  166. data/rdoc_stat.rb +31 -0
  167. data/ruby_carray.c +242 -0
  168. data/ruby_ccomplex.c +497 -0
  169. data/ruby_float_func.c +83 -0
  170. data/spec/CABlockIterator/CABlockIterator_spec.rb +113 -0
  171. data/spec/CArray/bug/store_spec.rb +27 -0
  172. data/spec/CArray/index/repeat_spec.rb +10 -0
  173. data/spec/CArray/method/eq_spec.rb +80 -0
  174. data/spec/CArray/method/is_nan_spec.rb +12 -0
  175. data/spec/CArray/method/ne_spec.rb +18 -0
  176. data/spec/CArray/method/round_spec.rb +11 -0
  177. data/spec/CArray/object/_attribute_spec.rb +32 -0
  178. data/spec/CArray/object/s_new_spec.rb +31 -0
  179. data/spec/CArray/serialize/Serialization_spec.rb +89 -0
  180. data/spec/spec_all.rb +11 -0
  181. data/test/test_ALL.rb +50 -0
  182. data/test/test_CABitfield.rb +59 -0
  183. data/test/test_CABlock.rb +208 -0
  184. data/test/test_CAField.rb +40 -0
  185. data/test/test_CAGrid.rb +76 -0
  186. data/test/test_CAMapping.rb +106 -0
  187. data/test/test_CAMmap.rb +11 -0
  188. data/test/test_CARefer.rb +94 -0
  189. data/test/test_CARepeat.rb +66 -0
  190. data/test/test_CASelect.rb +23 -0
  191. data/test/test_CAShift.rb +17 -0
  192. data/test/test_CATranspose.rb +61 -0
  193. data/test/test_CAVirtual.rb +214 -0
  194. data/test/test_CAWindow.rb +55 -0
  195. data/test/test_CAWrap.rb +9 -0
  196. data/test/test_CArray.rb +228 -0
  197. data/test/test_CComplex.rb +83 -0
  198. data/test/test_CScalar.rb +91 -0
  199. data/test/test_attribute.rb +281 -0
  200. data/test/test_block_iterator.rb +17 -0
  201. data/test/test_boolean.rb +99 -0
  202. data/test/test_cast.rb +33 -0
  203. data/test/test_class.rb +85 -0
  204. data/test/test_complex.rb +43 -0
  205. data/test/test_composite.rb +125 -0
  206. data/test/test_convert.rb +79 -0
  207. data/test/test_copy.rb +141 -0
  208. data/test/test_creation.rb +85 -0
  209. data/test/test_element.rb +146 -0
  210. data/test/test_extream.rb +55 -0
  211. data/test/test_generate.rb +75 -0
  212. data/test/test_index.rb +71 -0
  213. data/test/test_mask.rb +578 -0
  214. data/test/test_math.rb +98 -0
  215. data/test/test_narray.rb +64 -0
  216. data/test/test_order.rb +147 -0
  217. data/test/test_random.rb +15 -0
  218. data/test/test_ref_store.rb +211 -0
  219. data/test/test_stat.rb +414 -0
  220. data/test/test_struct.rb +72 -0
  221. data/test/test_virtual.rb +49 -0
  222. data/utils/ca_ase.rb +21 -0
  223. data/utils/ca_methods.rb +15 -0
  224. data/utils/cast_checker.rb +30 -0
  225. data/utils/create_rdoc.sh +9 -0
  226. data/utils/diff_method.rb +52 -0
  227. data/utils/extract_rdoc.rb +27 -0
  228. data/utils/make_tgz.sh +3 -0
  229. data/utils/remove_resource_fork.sh +5 -0
  230. data/version.h +3 -3
  231. metadata +266 -1
@@ -0,0 +1,529 @@
1
+ /* ---------------------------------------------------------------------------
2
+
3
+ carray_random.c
4
+
5
+ This file is part of Ruby/CArray extension library.
6
+ You can redistribute it and/or modify it under the terms of
7
+ the Ruby Licence.
8
+
9
+ Copyright (C) 2005 Hiroki Motoyoshi
10
+
11
+ This file is partially based on the codes in the following two files:
12
+
13
+ * random.c in Ruby distribution ( ruby-1.8.6 )
14
+ Copyright (C) 1993-2003 Yukihiro Matsumoto
15
+
16
+ * na_random.c in Ruby/NArray distribution ( narray-0.5.9 )
17
+ (C) Copyright 2003 by Masahiro TANAKA
18
+
19
+ ---------------------------------------------------------------------------- */
20
+
21
+ #include "carray.h"
22
+ #include <math.h>
23
+ #include "mt19937ar.h"
24
+
25
+ static int first = 1;
26
+
27
+ static VALUE
28
+ rb_ca_s_srand(int argc, VALUE *argv, VALUE self)
29
+ {
30
+ volatile VALUE seeds;
31
+ unsigned long *init_key;
32
+ int key_length;
33
+ int i;
34
+
35
+ if ( argc == 0 ) {
36
+ volatile VALUE limit = ULONG2NUM(0xffffffff), val;
37
+ seeds = rb_ary_new2(4);
38
+ for (i=0; i<4; i++) {
39
+ val = rb_funcall(rb_mKernel, rb_intern("rand"), 1, limit);
40
+ rb_ary_store(seeds, i, val);
41
+ }
42
+ }
43
+ else {
44
+ seeds = rb_ary_new4(argc, argv);
45
+ }
46
+
47
+ key_length = RARRAY_LEN(seeds);
48
+ init_key = malloc_with_check(sizeof(unsigned long)*key_length);
49
+
50
+ for (i=0; i<key_length; i++) {
51
+ init_key[i] = NUM2ULONG(rb_ary_entry(seeds, i));
52
+ }
53
+
54
+ init_by_array(init_key, key_length);
55
+
56
+ free(init_key);
57
+
58
+ first = 0;
59
+
60
+ return Qnil;
61
+ }
62
+
63
+ /* -------------------------------------------------------------------- */
64
+
65
+ static int
66
+ bit_width (uint32_t max)
67
+ {
68
+ static uint32_t bits[32] = {
69
+ 1, 2, 4, 8, 16, 32, 64, 128, 256,
70
+ 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536,
71
+ 131072, 262144, 524288, 1048576, 2097152, 4194304,
72
+ 8388608, 16777216, 33554432, 67108864, 134217728,
73
+ 268435456, 536870912, 1073741824, 2147483648UL
74
+ };
75
+ int i;
76
+ if ( max == 0 ) {
77
+ return 0;
78
+ }
79
+ for (i=0; i<32; i++) {
80
+ if ( max <= bits[i] ) {
81
+ return i+1;
82
+ }
83
+ }
84
+ return 32;
85
+ }
86
+
87
+ static void
88
+ ca_monop_random_boolean8_t (int n, char *ptr1, int i1, uint32_t max)
89
+ {
90
+ boolean8_t *q1 = (boolean8_t *) ptr1, *p1;
91
+ uint32_t val;
92
+ int32_t k;
93
+
94
+ if ( max > 1 ) {
95
+ rb_raise(rb_eArgError, "given maximum number is out of range");
96
+ }
97
+
98
+ #ifdef _OPENMP
99
+ #pragma omp parallel for private(p1)
100
+ #endif
101
+ for (k=0; k<n; k++) {
102
+ p1 = q1 + k*i1;
103
+ val = genrand_int32();
104
+ val >>= 31;
105
+ *(int8_t*)p1 = (int8_t) val;
106
+ }
107
+ }
108
+
109
+ static void
110
+ ca_monop_random_int8_t (int n, char *ptr1, int i1, uint32_t max)
111
+ {
112
+ int8_t *q1 = (int8_t *) ptr1, *p1;
113
+ uint32_t val;
114
+ int shift;
115
+ int32_t k;
116
+
117
+ if ( max > 0x80 ) {
118
+ rb_raise(rb_eArgError, "given maximum number is out of range");
119
+ }
120
+
121
+ shift = 32 - bit_width(max);
122
+
123
+ #ifdef _OPENMP
124
+ #pragma omp parallel for private(p1)
125
+ #endif
126
+ for (k=0; k<n; k++) {
127
+ p1 = q1 + k*i1;
128
+ do {
129
+ val = genrand_int32();
130
+ val >>= shift;
131
+ } while ( val >= max );
132
+ *(int8_t*)p1 = (int8_t) val;
133
+ }
134
+ }
135
+
136
+ static void
137
+ ca_monop_random_uint8_t (int n, char *ptr1, int i1, uint32_t max)
138
+ {
139
+ uint8_t *q1 = (uint8_t *) ptr1, *p1;
140
+ uint32_t val;
141
+ int shift;
142
+ int32_t k;
143
+
144
+ if ( max > 0x100 ) {
145
+ rb_raise(rb_eArgError, "given maximum number is out of range");
146
+ }
147
+
148
+ shift = 32 - bit_width(max);
149
+
150
+ #ifdef _OPENMP
151
+ #pragma omp parallel for private(p1)
152
+ #endif
153
+ for (k=0; k<n; k++) {
154
+ p1 = q1 + k*i1;
155
+ do {
156
+ val = genrand_int32();
157
+ val >>= shift;
158
+ } while ( val >= max );
159
+ *(uint8_t*)p1 = (uint8_t) val;
160
+ }
161
+ }
162
+
163
+ static void
164
+ ca_monop_random_int16_t (int n, char *ptr1, int i1, uint32_t max)
165
+ {
166
+ int16_t *q1 = (int16_t *) ptr1, *p1;
167
+ uint32_t val;
168
+ int shift;
169
+ int32_t k;
170
+
171
+ if ( max > 0x8000 ) {
172
+ rb_raise(rb_eArgError, "given maximum number is out of range");
173
+ }
174
+
175
+ shift = 32 - bit_width(max);
176
+
177
+ #ifdef _OPENMP
178
+ #pragma omp parallel for private(p1)
179
+ #endif
180
+ for (k=0; k<n; k++) {
181
+ p1 = q1 + k*i1;
182
+ do {
183
+ val = genrand_int32();
184
+ val >>= shift;
185
+ } while ( val >= max );
186
+ *(int16_t*)p1 = (int16_t) val;
187
+ }
188
+ }
189
+
190
+ static void
191
+ ca_monop_random_uint16_t (int n, char *ptr1, int i1, uint32_t max)
192
+ {
193
+ uint16_t *q1 = (uint16_t *) ptr1, *p1;
194
+ uint32_t val;
195
+ int shift;
196
+ int32_t k;
197
+
198
+ if ( max > 0x10000 ) {
199
+ rb_raise(rb_eArgError, "given maximum number is out of range");
200
+ }
201
+
202
+ shift = 32 - bit_width(max);
203
+
204
+ #ifdef _OPENMP
205
+ #pragma omp parallel for private(p1)
206
+ #endif
207
+ for (k=0; k<n; k++) {
208
+ p1 = q1 + k*i1;
209
+ do {
210
+ val = genrand_int32();
211
+ val >>= shift;
212
+ } while ( val >= max );
213
+ *(uint16_t*)p1 = (uint16_t) val;
214
+ }
215
+ }
216
+
217
+
218
+ static void
219
+ ca_monop_random_int32_t (int n, char *ptr1, int i1, uint32_t max)
220
+ {
221
+ int32_t *q1 = (int32_t *) ptr1, *p1;
222
+ uint32_t val;
223
+ int shift;
224
+ int32_t k;
225
+
226
+ if ( max > 0x80000000 ) {
227
+ rb_raise(rb_eArgError, "given maximum number is out of range");
228
+ }
229
+
230
+ shift = 32 - bit_width(max);
231
+
232
+ #ifdef _OPENMP
233
+ #pragma omp parallel for private(p1)
234
+ #endif
235
+ for (k=0; k<n; k++) {
236
+ p1 = q1 + k*i1;
237
+ do {
238
+ val = genrand_int32();
239
+ val >>= shift;
240
+ } while ( val >= max );
241
+ *(int32_t*)p1 = (int32_t) val;
242
+ }
243
+ }
244
+
245
+ static void
246
+ ca_monop_random_uint32_t (int n, char *ptr1, int i1, double max)
247
+ {
248
+ uint32_t *q1 = (uint32_t *) ptr1, *p1;
249
+ uint32_t val;
250
+ int shift;
251
+ int32_t k;
252
+
253
+ if ( max > 4294967296.0 ) { /* 0x100000000 */
254
+ rb_raise(rb_eArgError, "given maximum number is out of range");
255
+ }
256
+
257
+ if ( max > 4294967295.0 ) { /* 0xffffffff */
258
+ shift = 0;
259
+ }
260
+ else {
261
+ shift = 32 - bit_width((uint32_t) max);
262
+ }
263
+
264
+ #ifdef _OPENMP
265
+ #pragma omp parallel for private(p1)
266
+ #endif
267
+ for (k=0; k<n; k++) {
268
+ p1 = q1 + k*i1;
269
+ do {
270
+ val = genrand_int32();
271
+ val >>= shift;
272
+ } while ( val >= max );
273
+ *(uint32_t*)p1 = (uint32_t) val;
274
+ }
275
+ }
276
+
277
+ static void
278
+ ca_monop_random_float32_t(int n, char *ptr1, int i1, double rmax)
279
+ {
280
+ float32_t *q1 = (float32_t *) ptr1, *p1;
281
+ int32_t k;
282
+ #ifdef _OPENMP
283
+ #pragma omp parallel for private(p1)
284
+ #endif
285
+ for (k=0; k<n; k++) {
286
+ p1 = q1 + k*i1;
287
+ *(float32_t*)p1 = genrand_real2() * rmax;
288
+ }
289
+ }
290
+
291
+ static void
292
+ ca_monop_random_float64_t(int n, char *ptr1, int i1, double rmax)
293
+ {
294
+ float64_t *q1 = (float64_t *) ptr1, *p1;
295
+ int32_t k;
296
+ #ifdef _OPENMP
297
+ #pragma omp parallel for private(p1)
298
+ #endif
299
+ for (k=0; k<n; k++) {
300
+ p1 = q1 + k*i1;
301
+ *(float64_t*)p1 = genrand_res53() * rmax;
302
+ }
303
+ }
304
+
305
+ #ifdef HAVE_COMPLEX_H
306
+
307
+ static void
308
+ ca_monop_random_cmplx64_t(int n, char *ptr1, int i1, double rmax, double imax)
309
+ {
310
+ cmplx64_t *q1 = (cmplx64_t *) ptr1, *p1;
311
+ int32_t k;
312
+ #ifdef _OPENMP
313
+ #pragma omp parallel for private(p1)
314
+ #endif
315
+ for (k=0; k<n; k++) {
316
+ p1 = q1 + k*i1;
317
+ *(cmplx64_t*)p1 = genrand_real2() * rmax + I * (genrand_real2() * rmax);
318
+ }
319
+ }
320
+
321
+ static void
322
+ ca_monop_random_cmplx128_t(int n, char *ptr1, int i1, double rmax, double imax)
323
+ {
324
+ cmplx128_t *q1 = (cmplx128_t *) ptr1, *p1;
325
+ int32_t k;
326
+ #ifdef _OPENMP
327
+ #pragma omp parallel for private(p1)
328
+ #endif
329
+ for (k=0; k<n; k++) {
330
+ p1 = q1 + k*i1;
331
+ *(cmplx128_t*)p1 = genrand_res53() * rmax + I * (genrand_res53() * rmax);
332
+ }
333
+ }
334
+
335
+ #endif
336
+
337
+ ca_monop_func_t
338
+ ca_monop_random[CA_NTYPE] = {
339
+ ca_monop_not_implement,
340
+ ca_monop_random_boolean8_t,
341
+ ca_monop_random_int8_t,
342
+ ca_monop_random_uint8_t,
343
+ ca_monop_random_int16_t,
344
+ ca_monop_random_uint16_t,
345
+ ca_monop_random_int32_t,
346
+ ca_monop_random_uint32_t,
347
+ ca_monop_not_implement, /* ca_monop_random_int64_t, */
348
+ ca_monop_not_implement, /* ca_monop_random_uint64_t, */
349
+ ca_monop_random_float32_t,
350
+ ca_monop_random_float64_t,
351
+ ca_monop_not_implement, /* ca_monop_random_float128_t, */
352
+ #ifdef HAVE_COMPLEX_H
353
+ ca_monop_random_cmplx64_t,
354
+ ca_monop_random_cmplx128_t,
355
+ ca_monop_not_implement, /* ca_monop_random_cmplx256_t, */
356
+ #else
357
+ ca_monop_not_implement,
358
+ ca_monop_not_implement,
359
+ ca_monop_not_implement,
360
+ #endif
361
+ ca_monop_not_implement,
362
+ };
363
+
364
+ /* rdoc:
365
+ class CArray
366
+ def random! (max=1.0)
367
+ end
368
+ end
369
+ */
370
+
371
+ static VALUE
372
+ rb_ca_random (int argc, VALUE *argv, VALUE self)
373
+ {
374
+ VALUE rrmax, rimax = Qnil;
375
+ CArray *ca;
376
+ double rmax, imax = 1;
377
+
378
+ rb_ca_modify(self);
379
+
380
+ Data_Get_Struct(self, CArray, ca);
381
+
382
+ if ( ca_is_object_type(ca) ) {
383
+ CArray *dmy;
384
+ VALUE *p;
385
+ int i;
386
+
387
+ rb_scan_args(argc, argv, "01", &rrmax);
388
+ if ( NIL_P(rrmax) ) {
389
+ rrmax = rb_float_new(1.0);
390
+ }
391
+
392
+ ca_clear_mask(ca);
393
+
394
+ ca_attach(ca);
395
+
396
+ if ( rb_obj_is_kind_of(rrmax, rb_cInteger) ) {
397
+ int32_t *q;
398
+ rmax = (double)NUM2INT(rrmax);
399
+ dmy = carray_new(CA_INT32, ca->rank, ca->dim, ca->bytes, NULL);
400
+ (*ca_monop_random[dmy->data_type])(dmy->elements,
401
+ dmy->ptr, 1, (uint32_t)rmax);
402
+ p = (VALUE *)ca->ptr;
403
+ q = (int32_t *)dmy->ptr;
404
+ for (i=0; i<ca->elements; i++) {
405
+ *p = INT2NUM(*q);
406
+ p++; q++;
407
+ }
408
+ }
409
+ else if ( rb_obj_is_kind_of(rrmax, rb_cFloat) ) {
410
+ float64_t *q;
411
+ rmax = NUM2DBL(rrmax);
412
+ dmy = carray_new(CA_FLOAT64, ca->rank, ca->dim, ca->bytes, NULL);
413
+ (*ca_monop_random[dmy->data_type])(dmy->elements,
414
+ dmy->ptr, 1, rmax);
415
+ p = (VALUE *)ca->ptr;
416
+ q = (float64_t *)dmy->ptr;
417
+ for (i=0; i<ca->elements; i++) {
418
+ *p = rb_float_new(*q);
419
+ p++; q++;
420
+ }
421
+ }
422
+ else {
423
+ rb_raise(rb_eArgError,
424
+ "maximum number should be an integer or a float number");
425
+ }
426
+
427
+ ca_sync(ca);
428
+ ca_detach(ca);
429
+
430
+ ca_free(dmy);
431
+
432
+ return self;
433
+ }
434
+
435
+ rb_scan_args(argc, argv, "02", &rrmax, &rimax);
436
+
437
+ if ( first ) {
438
+ rb_ca_s_srand(0, NULL, rb_cCArray);
439
+ }
440
+
441
+ rmax = ( NIL_P(rrmax) ) ? 1.0 : NUM2DBL(rrmax);
442
+ imax = ( NIL_P(rimax) ) ? rmax : NUM2DBL(rimax);
443
+
444
+ if ( isinf(rmax) || isnan(rmax) || isinf(imax) || isnan(imax) ) {
445
+ rb_raise(rb_eArgError,
446
+ "maximum number should be finite");
447
+ }
448
+
449
+ if ( rmax < 0 || imax < 0 ) {
450
+ rb_raise(rb_eArgError,
451
+ "maximum number should be positive");
452
+ }
453
+
454
+ ca_clear_mask(ca);
455
+ ca_attach(ca);
456
+
457
+ switch ( ca->data_type ) {
458
+ case CA_CMPLX64:
459
+ case CA_CMPLX128:
460
+ if ( rmax < 0 || imax < 0 ) {
461
+ rb_raise(rb_eArgError,
462
+ "maximum number should be positive");
463
+ }
464
+ (*ca_monop_random[ca->data_type])(ca->elements,
465
+ ca->ptr, 1, rmax, imax);
466
+ break;
467
+ case CA_UINT32:
468
+ case CA_FLOAT32:
469
+ case CA_FLOAT64:
470
+ if ( rmax < 0 ) {
471
+ rb_raise(rb_eArgError,
472
+ "maximum number should be positive");
473
+ }
474
+ (*ca_monop_random[ca->data_type])(ca->elements,
475
+ ca->ptr, 1, rmax);
476
+ break;
477
+ default:
478
+ if ( rmax < 1 ) {
479
+ rb_raise(rb_eArgError,
480
+ "maximum number should be positive");
481
+ }
482
+ (*ca_monop_random[ca->data_type])(ca->elements,
483
+ ca->ptr, 1, (uint32_t)rmax);
484
+ }
485
+
486
+ ca_sync(ca);
487
+ ca_detach(ca);
488
+
489
+ return self;
490
+ }
491
+
492
+ void
493
+ ca_check_rand_init ()
494
+ {
495
+ if ( first ) {
496
+ rb_ca_s_srand(0, NULL, rb_cCArray);
497
+ }
498
+ }
499
+
500
+ int32_t
501
+ ca_rand (double rmax)
502
+ {
503
+ uint32_t val;
504
+ int shift;
505
+
506
+ ca_check_rand_init();
507
+
508
+ if ( rmax < 0 || rmax > 0x7fffffff ) {
509
+ rb_raise(rb_eArgError,
510
+ "given maximum number is out of range");
511
+ }
512
+
513
+ shift = 32 - bit_width((uint32_t)rmax);
514
+
515
+ do {
516
+ val = genrand_int32();
517
+ val >>= shift;
518
+ } while ( val >= (uint32_t) rmax );
519
+
520
+ return (int32_t) val;
521
+ }
522
+
523
+ void
524
+ Init_carray_random()
525
+ {
526
+ rb_define_singleton_method(rb_cCArray,"srand", rb_ca_s_srand,-1);
527
+ rb_define_method(rb_cCArray, "random!", rb_ca_random, -1);
528
+ }
529
+