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,1051 @@
1
+ # ----------------------------------------------------------------------------
2
+ #
3
+ # carray/base/base.rb
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
+ # ----------------------------------------------------------------------------
12
+
13
+ module CAMath
14
+ include Math
15
+ end
16
+
17
+ def nan
18
+ 0.0/0.0
19
+ end
20
+
21
+ class Array # :nodoc:
22
+ def +@
23
+ return CA_INT32(self)
24
+ end
25
+ def to_ca
26
+ return CA_OBJECT(self)
27
+ end
28
+ end
29
+
30
+ class Range # :nodoc:
31
+ def +@
32
+ return CA_INT32(self)
33
+ end
34
+ def to_ca
35
+ return CA_OBJECT(self)
36
+ end
37
+ end
38
+
39
+ # Create new CArray object from the return value of the block
40
+ # with data type +type+. The dimensional size and the initialization value
41
+ # are guessed from the return value of the block.
42
+ # The block should return one of the following objects.
43
+ #
44
+ # * Numeric
45
+ # * Array
46
+ # * CArray
47
+ # * an object that has either method +to_ca+ or +to_a+ or +map+
48
+ #
49
+ # When the return value of the block is a Numeric or CScalar object,
50
+ # CScalar object is returned.
51
+ #
52
+ def CArray.__new__ (type, *args) # :nodoc:
53
+ case v = args.first
54
+ when CArray
55
+ return ( v.data_type == type ) ? v.to_ca : v.to_type(type)
56
+ when Array
57
+ return CArray.new(type, CArray.guess_array_shape(v)) { v }
58
+ when Range
59
+ return CArray.span(type, *args)
60
+ when String
61
+ if type == CA_OBJECT
62
+ return CScalar.new(CA_OBJECT) { v }
63
+ elsif type == CA_BOOLEAN
64
+ v = v.dup
65
+ v.tr!('^01',"1")
66
+ v.tr!('01',"\x0\x1")
67
+ return CArray.boolean(v.length).load_binary(v)
68
+ else
69
+ case v
70
+ when /;/
71
+ v = v.strip.split(/\s*;\s*/).
72
+ map{|s| s.split(/\s+|\s*,\s*/).map{|x| x=='_' ? UNDEF : x} }
73
+ else
74
+ v = v.strip.split(/\s+|\s*,\s*/).map{|x| x=='_' ? UNDEF : x}
75
+ end
76
+ return CArray.new(type, CArray.guess_array_shape(v)) { v }
77
+ end
78
+ when NilClass
79
+ return CArray.new(type, [0])
80
+ else
81
+ if v.respond_to?(:to_ca)
82
+ ca = v.to_ca
83
+ return ( ca.data_type == type ) ? ca : ca.to_type(type)
84
+ else
85
+ return CScalar.new(type) { v }
86
+ end
87
+ end
88
+ end
89
+
90
+ def CArray.__new_fixlen__ (bytes, v) # :nodoc:
91
+ case v
92
+ when CArray
93
+ return ( v.data_type == :fixlen ) ? v.to_ca : v.to_type(:fixlen, :bytes=>bytes)
94
+ when Array
95
+ unless bytes
96
+ bytes = v.map{|s| s.length}.max
97
+ end
98
+ return CArray.new(:fixlen, CArray.guess_array_shape(v), :bytes=>bytes) { v }
99
+ when NilClass
100
+ return CArray.new(type, [0])
101
+ else
102
+ if v.respond_to?(:to_ca)
103
+ ca = v.to_ca
104
+ return ( ca.data_type == :fixlen ) ? ca : ca.to_type(:fixlen, :bytes=>bytes)
105
+ else
106
+ return CScalar.new(:fixlen, :bytes=>bytes) { v }
107
+ end
108
+ end
109
+ end
110
+
111
+ #
112
+ # CA_INT8(data)
113
+ # :
114
+ # CA_CMPLX256(data)
115
+ #
116
+ # Create new CArray object from +data+ with data type
117
+ # which is guessed from the method name. +data+ should be one of the following
118
+ # objects.
119
+ #
120
+ # * Numeric
121
+ # * Array
122
+ # * CArray
123
+ # * an object that has either method +to_ca+ or +to_a+ or +map+
124
+ #
125
+ # When the block returns a Numeric or CScalar object,
126
+ # the resulted array is a CScalar object.
127
+
128
+ [
129
+ "CA_BOOLEAN",
130
+ "CA_INT8",
131
+ "CA_UINT8",
132
+ "CA_INT16",
133
+ "CA_UINT16",
134
+ "CA_INT32",
135
+ "CA_UINT32",
136
+ "CA_INT64",
137
+ "CA_UINT64",
138
+ "CA_FLOAT32",
139
+ "CA_FLOAT64",
140
+ "CA_FLOAT128",
141
+ "CA_CMPLX64",
142
+ "CA_CMPLX128",
143
+ "CA_CMPLX256",
144
+ "CA_OBJECT",
145
+ "CA_BYTE",
146
+ "CA_SHORT",
147
+ "CA_INT",
148
+ "CA_FLOAT",
149
+ "CA_DOUBLE",
150
+ "CA_COMPLEX",
151
+ "CA_DCOMPLEX",
152
+ ].each do |name|
153
+ eval %{
154
+ def #{name} (*val)
155
+ CArray.__new__(#{name}, *val)
156
+ end
157
+ }
158
+ end
159
+
160
+ def CA_FIXLEN (val, options = {})
161
+ CArray.__new_fixlen__(options[:bytes], val)
162
+ end
163
+
164
+ class CArray
165
+
166
+ def has_attribute?
167
+ if ( not @attribute ) or @attribute.empty?
168
+ return false
169
+ else
170
+ return true
171
+ end
172
+ end
173
+
174
+ def attribute= (obj)
175
+ unless obj.is_a?(Hash)
176
+ raise "attribute should be a hash object"
177
+ end
178
+ @attribute = obj
179
+ end
180
+
181
+ def attribute
182
+ @attribute ||= {}
183
+ return @attribute
184
+ end
185
+
186
+ def first
187
+ self[0]
188
+ end
189
+
190
+ def last
191
+ self[-1]
192
+ end
193
+
194
+ def range
195
+ return (self.min)..(self.max)
196
+ end
197
+
198
+ def asign (*idx)
199
+ self[*idx] = yield
200
+ return self
201
+ end
202
+
203
+ # mask
204
+
205
+ #
206
+ # Returns the number of masked elements.
207
+ #
208
+
209
+ def count_masked (*argv)
210
+ if has_mask?
211
+ return mask.int32.accumulate(*argv)
212
+ else
213
+ return 0
214
+ end
215
+ end
216
+
217
+ #
218
+ # Returns the number of not-masked elements.
219
+ #
220
+ def count_not_masked (*argv)
221
+ if has_mask?
222
+ return mask.not.int32.accumulate(*argv)
223
+ else
224
+ return elements
225
+ end
226
+ end
227
+
228
+ def maskout! (*argv)
229
+ if argv.size == 1
230
+ val = argv.first
231
+ case val
232
+ when CArray, Symbol
233
+ self[val] = UNDEF
234
+ else
235
+ self[:eq, val] = UNDEF
236
+ end
237
+ else
238
+ self[*argv] = UNDEF
239
+ end
240
+ return self
241
+ end
242
+
243
+ def maskout (*argv)
244
+ obj = self.to_ca
245
+ if argv.size == 1
246
+ val = argv.first
247
+ case val
248
+ when CArray, Symbol
249
+ obj[val] = UNDEF
250
+ else
251
+ obj[:eq, val] = UNDEF
252
+ end
253
+ else
254
+ obj[*argv] = UNDEF
255
+ end
256
+ return obj
257
+ end
258
+
259
+ # matchup
260
+
261
+ def matchup (ref)
262
+ ri = ref.sort_addr
263
+ rs = ref[ri].to_ca
264
+ si = rs.bsearch(self)
265
+ return ri.project(si)
266
+ end
267
+
268
+ def matchup_nearest (ref)
269
+ ri = ref.sort_addr
270
+ rs = ref[ri].to_ca
271
+ si = rs.section(self).round.int32
272
+ si.trim!(0,si.size)
273
+ return ri[si].to_ca
274
+ end
275
+
276
+ # replace
277
+ #
278
+ #
279
+ def replace (from, to)
280
+ self[:eq, from] = to
281
+ return self
282
+ end
283
+
284
+ # reshape
285
+
286
+ def reshape (*newdim)
287
+ ifalse = nil
288
+ i = 0
289
+ 0.upto(newdim.size-1) do |i|
290
+ if newdim[i].nil?
291
+ newdim[i] = dim[i]
292
+ elsif newdim[i] == false
293
+ ifalse = i
294
+ break
295
+ end
296
+ end
297
+ k = 0
298
+ (newdim.size-1).downto(i+1) do |j|
299
+ if newdim[j].nil?
300
+ newdim[j] = dim[rank-1-k]
301
+ end
302
+ k += 1
303
+ end
304
+ if ifalse
305
+ newdim[ifalse] =
306
+ elements/newdim.select{|x| x!=false}.inject(1){|s,x| s*x}
307
+ end
308
+ return refer(data_type, newdim, :bytes=>bytes)
309
+ end
310
+
311
+ # flatten
312
+
313
+ def flattened
314
+ return reshape(elements)
315
+ end
316
+
317
+ def flatten
318
+ return reshape(elements).to_ca
319
+ end
320
+
321
+ # reversed
322
+
323
+ def reversed
324
+ return self[*([-1..0]*rank)]
325
+ end
326
+
327
+ # roll / shift
328
+
329
+ def shift! (*argv, &block)
330
+ self[] = self.shifted(*argv, &block)
331
+ return self
332
+ end
333
+
334
+ def shift (*argv, &block)
335
+ return self.shifted(*argv, &block).to_ca
336
+ end
337
+
338
+ def rolled (*argv)
339
+ argv.push({:roll => Array.new(rank){1} })
340
+ return shifted(*argv)
341
+ end
342
+
343
+ def roll! (*argv)
344
+ self[] = self.rolled(*argv)
345
+ return self
346
+ end
347
+
348
+ def roll (*argv)
349
+ return self.rolled(*argv).to_ca
350
+ end
351
+
352
+ def transpose! (*argv)
353
+ self[] = self.transposed(*argv)
354
+ return self
355
+ end
356
+
357
+ def transpose (*argv)
358
+ return self.transposed(*argv).to_ca
359
+ end
360
+
361
+ # Reutrns the reference which rank is reduced
362
+ # by eliminating the dimensions which size == 1
363
+ def compacted
364
+ newdim = dim.reject{|x| x == 1 }
365
+ return ( rank != newdim.size ) ? reshape(*newdim) : self[]
366
+ end
367
+
368
+ # Returns the array which rank is reduced
369
+ # by eliminating the dimensions which size == 1
370
+ def compact
371
+ newdim = dim.reject{|x| x == 1 }
372
+ return ( rank != newdim.size ) ? reshape(*newdim).to_ca : self.to_ca
373
+ end
374
+
375
+ # Returns the reference which elements are sorted by the comparison method
376
+ # given as block
377
+ def sorted_by (type=nil, opt={}, &block)
378
+ type, bytes =
379
+ CArray.guess_type_and_bytes(type||data_type, opt[:bytes]||bytes)
380
+ cmpary = convert(type, :bytes=>bytes, &block)
381
+ return self[cmpary.sort_addr]
382
+ end
383
+
384
+ # Returns the array which elements are sorted by the comparison method
385
+ # given as block
386
+ def sort_by (type=nil, opt={}, &block)
387
+ type, bytes =
388
+ CArray.guess_type_and_bytes(type||data_type, opt[:bytes]||bytes)
389
+ cmpary = convert(type, :bytes=>bytes, &block)
390
+ return self[cmpary.sort_addr].to_ca
391
+ end
392
+
393
+ def max_by (&block)
394
+ if empty?
395
+ return UNDEF
396
+ else
397
+ addr = convert(:object, &block).max_addr
398
+ return self[addr]
399
+ end
400
+ end
401
+
402
+ def min_by (&block)
403
+ if empty?
404
+ return UNDEF
405
+ else
406
+ addr = convert(:object, &block).min_addr
407
+ return self[addr]
408
+ end
409
+ end
410
+
411
+ # Returns (1,n) array from 1-dimensional array
412
+ def to_row
413
+ if rank != 1
414
+ raise "rank should be 1"
415
+ end
416
+ return self[1,:%]
417
+ end
418
+
419
+ # Returns (n,1) array from 1-dimensional array
420
+ def to_column
421
+ if rank != 1
422
+ raise "rank should be 1"
423
+ end
424
+ return self[:%,1]
425
+ end
426
+
427
+ # Returns the array resized to the dimension given as `newdim`.
428
+ # The new area is filled by the value returned by the block.
429
+ def resize (*newdim, &block)
430
+ if newdim.size != rank
431
+ raise "rank mismatch"
432
+ end
433
+ offset = Array.new(rank){0}
434
+ rank.times do |i|
435
+ d = newdim[i]
436
+ case d
437
+ when nil
438
+ newdim[i] = dim[i]
439
+ when Integer
440
+ if d < 0
441
+ newdim[i] *= -1
442
+ offset[i] = newdim[i] - dim[i]
443
+ end
444
+ else
445
+ raise "invalid dimension size"
446
+ end
447
+ end
448
+ out = CArray.new(data_type, newdim, &block)
449
+ if out.has_mask?
450
+ out.mask.paste(offset, self.false)
451
+ end
452
+ out.paste(offset, self)
453
+ return out
454
+ end
455
+
456
+ # insert
457
+ def insert_block (offset, bsize, &block)
458
+ if offset.size != rank or
459
+ bsize.size != rank
460
+ raise "rank mismatch"
461
+ end
462
+ newdim = dim
463
+ grids = dim.map{|d| CArray.int32(d) }
464
+ rank.times do |i|
465
+ if offset[i] < 0
466
+ offset[i] += dim[i]
467
+ end
468
+ if offset[i] < 0 or offset[i] >= dim[i] or bsize[i] < 0
469
+ raise "invalid offset or size"
470
+ end
471
+ if bsize[i] > 0
472
+ newdim[i] += bsize[i]
473
+ end
474
+ grids[i][0...offset[i]].seq!
475
+ grids[i][offset[i]..-1].seq!(offset[i]+bsize[i])
476
+ end
477
+ out = CArray.new(data_type, newdim)
478
+ if block_given?
479
+ sel = out.true
480
+ sel[*grids] = 0
481
+ out[sel] = block.call
482
+ end
483
+ out[*grids] = self
484
+ return out
485
+ end
486
+
487
+ def delete_block (offset, bsize)
488
+ if offset.size != rank or
489
+ bsize.size != rank
490
+ raise "rank mismatch"
491
+ end
492
+ newdim = dim
493
+ grids = []
494
+ rank.times do |i|
495
+ if offset[i] < 0
496
+ offset[i] += dim[i]
497
+ end
498
+ if bsize[i] >= 0
499
+ if offset[i] < 0 or offset[i] >= dim[i]
500
+ raise "invalid offset or size"
501
+ end
502
+ newdim[i] -= bsize[i]
503
+ else
504
+ if offset[i] + bsize[i] + 1 < 0 or offset[i] + bsize[i] > dim[i]
505
+ raise "invalid offset or size"
506
+ end
507
+ newdim[i] += bsize[i]
508
+ end
509
+ grids[i] = CArray.int32(newdim[i])
510
+ if bsize[i] >= 0
511
+ if offset[i] > 0
512
+ grids[i][0...offset[i]].seq!
513
+ end
514
+ if offset[i] + bsize[i] < dim[i]
515
+ grids[i][offset[i]..-1].seq!(offset[i]+bsize[i])
516
+ end
517
+ else
518
+ if offset[i]+bsize[i] > 0
519
+ grids[i][0..offset[i]+bsize[i]].seq!
520
+ end
521
+ if offset[i]+bsize[i]+1 < dim[i]-1
522
+ grids[i][offset[i]+bsize[i]+1..-1].seq!(offset[i]+1)
523
+ end
524
+ end
525
+ end
526
+ return self[*grids].to_ca
527
+ end
528
+
529
+ def where_range
530
+ w = where
531
+ x = (w - w.shifted(1){-2}).sub!(1).where
532
+ y = (w - w.shifted(-1){-2}).add!(1).where
533
+ list = []
534
+ x.each_addr do |i|
535
+ list.push(w[x[i]]..w[y[i]])
536
+ end
537
+ return list
538
+ end
539
+
540
+ def order (dir = 1)
541
+ if dir >= 0 ### ascending order
542
+ if has_mask?
543
+ obj = template(:int32) { UNDEF }
544
+ sel = is_not_masked
545
+ obj[sel][self[sel].sort_addr].seq!
546
+ return obj
547
+ else
548
+ obj = template(:int32)
549
+ obj[sort_addr].seq!
550
+ return obj
551
+ end
552
+ else ### descending order
553
+ if has_mask?
554
+ obj = template(:int32) { UNDEF}
555
+ sel = is_not_masked
556
+ obj[sel][self[sel].sort_addr.reversed].seq!
557
+ return obj
558
+ else
559
+ obj = template(:int32)
560
+ obj[sort_addr.reversed].seq!
561
+ return obj
562
+ end
563
+ end
564
+ end
565
+
566
+ # Returns the array eliminated all the duplicated elements.
567
+ def uniq
568
+ ary = to_a.uniq
569
+ if has_mask?
570
+ ary.delete(UNDEF)
571
+ end
572
+ if has_data_class?
573
+ return CArray.new(data_class, [ary.length]) { ary }
574
+ else
575
+ return CArray.new(data_type, [ary.length], :bytes=>bytes) { ary }
576
+ end
577
+ end
578
+
579
+ # Returns the array eliminated all the duplicated elements.
580
+ def duplicated_values
581
+ if uniq.size == size
582
+ return []
583
+ else
584
+ hash = {}
585
+ list = []
586
+ each_with_addr do |v, addr|
587
+ if v == UNDEF
588
+ next
589
+ elsif hash[v]
590
+ list << [v, addr, hash[v]]
591
+ hash[v] += 1
592
+ else
593
+ hash[v] = 0
594
+ end
595
+ end
596
+ return list
597
+ end
598
+ end
599
+
600
+ #
601
+ # CArray.span(data_type, range[, step])
602
+ # CArray.span(range[, step]) -> data_type guessed by range.first typ
603
+ #
604
+
605
+ def self.span (*argv)
606
+ if argv.first.is_a?(Range)
607
+ type = nil
608
+ else
609
+ type, = *CArray.guess_type_and_bytes(argv.shift, nil)
610
+ end
611
+ range, step = argv[0], argv[1] || 1
612
+ start, stop = range.begin, range.end
613
+ if step == 0
614
+ raise "step should not be 0"
615
+ end
616
+ if not type
617
+ case start
618
+ when Integer
619
+ type = CA_INT32
620
+ when Float
621
+ type = CA_FLOAT64
622
+ else
623
+ type = CA_OBJECT
624
+ end
625
+ end
626
+ if type == CA_OBJECT and not step
627
+ return CA_OBJECT(range.map)
628
+ else
629
+ # start = start.to_s.to_r
630
+ # stop = stop.to_s.to_r
631
+ # step = step.to_s.to_r
632
+ if range.exclude_end?
633
+ n = ((stop - start).abs/step).floor
634
+ else
635
+ n = ((stop - start).abs/step).floor + 1
636
+ end
637
+ if start <= stop
638
+ return CArray.new(type, [n]).seq(start, step)
639
+ else
640
+ return CArray.new(type, [n]).seq(start, -step.abs)
641
+ end
642
+ end
643
+ end
644
+
645
+ def span! (range)
646
+ first = range.begin.to_r
647
+ last = range.end.to_r
648
+ if range.exclude_end?
649
+ seq!(first, (last-first)/elements)
650
+ else
651
+ seq!(first, (last-first)/(elements-1))
652
+ end
653
+ return self
654
+ end
655
+
656
+ def span (range)
657
+ return template.span!(range)
658
+ end
659
+
660
+ #
661
+ #
662
+ #
663
+
664
+ def scale! (xa, xb)
665
+ xa = xa.to_f
666
+ xb = xb.to_f
667
+ seq!(xa, (xb-xa)/(elements-1))
668
+ end
669
+
670
+ def scale (xa, xb)
671
+ template.scale!(xa, xb)
672
+ end
673
+
674
+ # index / indices / axes
675
+
676
+ def address ()
677
+ return CArray.int32(*dim).seq!
678
+ end
679
+
680
+ def index (n)
681
+ unless n.is_a?(Integer)
682
+ raise ArgumentError, "argument should be an integer"
683
+ end
684
+ if n.between?(0, rank-1)
685
+ return CArray.int32(dim[n]).seq!
686
+ else
687
+ raise ArgumentError,
688
+ "invalid dimension specifier #{n} (0..#{self.rank-1})"
689
+ end
690
+ end
691
+
692
+ def indices
693
+ list = Array.new(rank) {|i|
694
+ rpt = self.dim
695
+ rpt[i] = :%
696
+ index(i)[*rpt]
697
+ }
698
+ if block_given?
699
+ return yield(*list)
700
+ else
701
+ return list
702
+ end
703
+ end
704
+
705
+ #
706
+ # Returns the 8-bit integer CArray object filled with 0 which
707
+ # dimension size is same as +self+. The resulted array represents
708
+ # the logical array which has +false+ for its all elements.
709
+ #
710
+ def false ()
711
+ return template(:boolean)
712
+ end
713
+
714
+ #
715
+ # Returns the 8-bit integer CArray object filled with 1 which
716
+ # dimension size is same as +self+. The resulted array represents
717
+ # the logical array which has +true+ for its all elements.
718
+ #
719
+ def true ()
720
+ return template(:boolean) { 1 }
721
+ end
722
+
723
+ # Returns map
724
+ def map (&block)
725
+ return self.convert(CA_OBJECT, &block).to_a
726
+ end
727
+
728
+ # Array#join like method
729
+
730
+ def join (*argv)
731
+ case argv.size
732
+ when 0
733
+ return to_a.join()
734
+ when 1
735
+ return to_a.join(argv[0])
736
+ else
737
+ sep = argv.shift
738
+ return self[:i, false].map { |s|
739
+ s[0, false].join(*argv)
740
+ }.join(sep)
741
+ end
742
+ end
743
+
744
+ #
745
+ def to_bit_string (nb)
746
+ hex = CArray.uint8(((nb*elements)/8.0).ceil)
747
+ hex.bits[nil].paste([0], self.bits[false,[(nb-1)..0]].flatten)
748
+ hex.bits[] = hex.bits[nil,[-1..0]]
749
+ return hex.to_s
750
+ end
751
+
752
+ def from_bit_string (bstr, nb)
753
+ hex = CArray.uint8(bstr.length).load_binary(bstr)
754
+ hex.bits[] = hex.bits[nil,[-1..0]]
755
+ bits = hex.bits.flatten
756
+ self.bits[false,[(nb-1)..0]][nil].paste([0], bits)
757
+ return self
758
+ end
759
+
760
+ def self.from_bit_string (bstr, nb, data_type=CA_INT32, dim=nil)
761
+ if dim
762
+ obj = CArray.new(data_type, dim)
763
+ else
764
+ dim0 = ((bstr.length*8)/nb.to_f).floor
765
+ obj = CArray.new(data_type, [dim0])
766
+ end
767
+ obj.from_bit_string(bstr, nb)
768
+ return obj
769
+ end
770
+
771
+ end
772
+
773
+
774
+ class CArray
775
+
776
+ #
777
+ # ref = CA_INT([[0,1,2],[1,2,0],[2,0,1]])
778
+ # CArray.pickup(CA_OBJECT, ref, ["a","b","c"])
779
+ # => <CArray.object(3,3): elem=9 mem=36b
780
+ # [ [ "a", "b", "c" ],
781
+ # [ "b", "c", "a" ],
782
+ # [ "c", "a", "b" ] ]>
783
+ #
784
+ def self.pickup (data_type, ref, args)
785
+ out = ref.template(data_type)
786
+ args.each_with_index do |v, i|
787
+ s = ref.eq(i)
788
+ case v
789
+ when CArray
790
+ out[s] = v[s]
791
+ else
792
+ out[s] = v
793
+ end
794
+ end
795
+ return out
796
+ end
797
+
798
+ def self.combine (data_type, tdim, list, at = 0)
799
+ has_fill_value = false
800
+ if block_given?
801
+ fill_value = yield
802
+ has_fill_value = true
803
+ end
804
+ if not tdim.is_a?(Array) or tdim.size == 0
805
+ raise "invalid binding dimension"
806
+ end
807
+ if not list.is_a?(Array) or list.size == 0
808
+ raise "invalid list"
809
+ end
810
+ list = list.map{|x| CArray.wrap_readonly(x, data_type) }
811
+ ref = list.detect{|x| x.is_a?(CArray) or not x.scalar? }
812
+ unless ref
813
+ raise "at least one element in list should be a carray"
814
+ end
815
+ dim = ref.dim
816
+ rank = ref.rank
817
+ trank = tdim.size
818
+ if at < 0
819
+ at += rank - trank + 1
820
+ end
821
+ unless at.between?(0, rank - trank)
822
+ raise "concatnating position out of range"
823
+ end
824
+ list.map! do |x|
825
+ if x.scalar?
826
+ rdim = dim.clone
827
+ rdim[at] = :%
828
+ x = x[*rdim] # convert CScalar to CARepeat
829
+ end
830
+ x
831
+ end
832
+ block = CArray.object(*tdim){ list }
833
+ edim = tdim.clone
834
+ idx = Array.new(tdim)
835
+ offset = Array.new(tdim.size) { [] }
836
+ tdim.each_with_index do |td, i|
837
+ edim[i] = 0
838
+ idx.map!{0}
839
+ idx[i] = nil
840
+ block[*idx].each do |e|
841
+ offset[i] << edim[i]
842
+ edim[i] += e.dim[at+i] # extended dimension size
843
+ end
844
+ end
845
+ newdim = dim.clone
846
+ newdim[at,trank] = edim # extended dimension size
847
+ if has_fill_value
848
+ obj = CArray.new(data_type, newdim) { fill_value }
849
+ else
850
+ obj = CArray.new(data_type, newdim)
851
+ end
852
+ idx = newdim.map{0}
853
+ block.each_with_index do |item, tidx|
854
+ (at...at+trank).each_with_index do |d,i|
855
+ idx[d] = offset[i][tidx[i]]
856
+ end
857
+ obj.paste(idx, item)
858
+ end
859
+ obj
860
+ end
861
+
862
+ def self.bind (data_type, list, at = 0)
863
+ return CArray.combine(data_type, [list.size], list, at)
864
+ end
865
+
866
+ def self.composite (data_type, tdim, list, at = 0)
867
+ if not tdim.is_a?(Array) or tdim.size == 0
868
+ raise "invalid tiling dimension"
869
+ end
870
+ if not list.is_a?(Array) or list.size == 0
871
+ raise "invalid carray list"
872
+ end
873
+ list = list.map{|x| CArray.wrap_readonly(x, data_type) }
874
+ ref = list.detect{|x| x.is_a?(CArray) or not x.scalar? }
875
+ unless ref
876
+ raise "at least one element in list should be a carray"
877
+ end
878
+ dim = ref.dim
879
+ rank = ref.rank
880
+ if at < 0
881
+ at += rank + 1 # "+ 1" is needed here
882
+ end
883
+ unless at.between?(0,rank)
884
+ raise "tiling position is out of range"
885
+ end
886
+ trank = tdim.size
887
+ list.map! do |x|
888
+ if x.scalar?
889
+ rdim = dim.clone
890
+ rdim[at] = :%
891
+ x = x[*rdim] # convert CScalar to CARepeat
892
+ end
893
+ x
894
+ end
895
+ newdim = dim.clone
896
+ newdim[at,0] = tdim
897
+ obj = CArray.new(data_type, newdim)
898
+ idx = Array.new(rank+trank) { nil }
899
+ CArray.each_index(*tdim) do |*tidx|
900
+ idx[at,trank] = tidx
901
+ obj[*idx] = list.shift
902
+ end
903
+ obj
904
+ end
905
+
906
+ def self.merge (data_type, list, at = -1)
907
+ return CArray.composite(data_type, [list.size], list, at)
908
+ end
909
+
910
+ def self.join (*argv)
911
+ # get options
912
+ case argv.first
913
+ when Fixnum, Symbol, String
914
+ type, = *CArray.guess_type_and_bytes(argv.shift, 0)
915
+ else
916
+ type = argv.flatten.first.data_type
917
+ end
918
+ # process
919
+ conc = argv.map do |list|
920
+ case list
921
+ when CArray
922
+ if list.rank == 1
923
+ list[:%,1]
924
+ else
925
+ list
926
+ end
927
+ when Array
928
+ x0 = list.first
929
+ if list.size == 1 and
930
+ x0.is_a?(CArray) and
931
+ x0.rank == 1
932
+ list = [x0[:%,1]]
933
+ else
934
+ list = list.map { |x|
935
+ case x
936
+ when CArray
937
+ if x.rank == 1
938
+ x[:%,1]
939
+ else
940
+ x
941
+ end
942
+ when Array
943
+ y = x.first
944
+ if x.size == 1 and
945
+ y.is_a?(CArray) and
946
+ y.rank == 1
947
+ y[1,:%]
948
+ else
949
+ CArray.join(*x)
950
+ end
951
+ else
952
+ x
953
+ end
954
+ }
955
+ end
956
+ if block_given?
957
+ CArray.bind(type, list, 1, &block)
958
+ else
959
+ CArray.bind(type, list, 1)
960
+ end
961
+ else
962
+ list
963
+ end
964
+ end
965
+ if conc.size > 1
966
+ return CArray.bind(type, conc)
967
+ else
968
+ return conc.first
969
+ end
970
+ end
971
+
972
+ # ---
973
+
974
+ def split (*argv)
975
+ odim = dim.values_at(*argv)
976
+ out = CArray.object(*odim)
977
+ idx = [nil] * rank
978
+ attach {
979
+ out.map_with_index! do |o, v|
980
+ argv.each_with_index do |r, i|
981
+ idx[r] = v[i]
982
+ end
983
+ self[*idx].to_ca
984
+ end
985
+ }
986
+ return out
987
+ end
988
+
989
+
990
+ end
991
+
992
+ class CAUnboundRepeat
993
+ def template (*argv, &block)
994
+ return parent.template(*argv,&block)[*spec.map{|x| x != :* ? nil : x}]
995
+ end
996
+ end
997
+
998
+ class Numeric
999
+
1000
+ def eq (other)
1001
+ case other
1002
+ when CArray
1003
+ return other.eq(self)
1004
+ else
1005
+ return send(:eq, *other.coerce(self))
1006
+ end
1007
+ end
1008
+
1009
+ def ne (other)
1010
+ case other
1011
+ when CArray
1012
+ return other.ne(self)
1013
+ else
1014
+ return send(:ne, *other.coerce(self))
1015
+ end
1016
+ end
1017
+
1018
+ [
1019
+ :boolean,
1020
+ :int8,
1021
+ :uint8,
1022
+ :int16,
1023
+ :uint16,
1024
+ :int32,
1025
+ :uint32,
1026
+ :int64,
1027
+ :uint64,
1028
+ :float32,
1029
+ :float64,
1030
+ :float128,
1031
+ :cmplx64,
1032
+ :cmplx128,
1033
+ :cmplx256,
1034
+ :byte,
1035
+ :short,
1036
+ :int,
1037
+ :float,
1038
+ :double,
1039
+ :complex,
1040
+ :dcomplex,
1041
+ :object
1042
+ ].each do |name|
1043
+ class_eval %{
1044
+ def #{name} ()
1045
+ CScalar.new(#{name.inspect}) {self}
1046
+ end
1047
+ }
1048
+ end
1049
+
1050
+ end
1051
+