carray 1.4.0 → 1.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSES +22 -0
- data/README.md +18 -14
- data/Rakefile +1 -1
- data/carray.gemspec +5 -13
- data/{ca_iter_block.c → ext/ca_iter_block.c} +13 -13
- data/{ca_iter_dimension.c → ext/ca_iter_dimension.c} +16 -16
- data/{ca_iter_window.c → ext/ca_iter_window.c} +10 -10
- data/{ca_obj_array.c → ext/ca_obj_array.c} +60 -55
- data/{ca_obj_bitarray.c → ext/ca_obj_bitarray.c} +12 -12
- data/{ca_obj_bitfield.c → ext/ca_obj_bitfield.c} +7 -7
- data/{ca_obj_block.c → ext/ca_obj_block.c} +42 -42
- data/{ca_obj_fake.c → ext/ca_obj_fake.c} +7 -7
- data/{ca_obj_farray.c → ext/ca_obj_farray.c} +18 -18
- data/{ca_obj_field.c → ext/ca_obj_field.c} +15 -15
- data/{ca_obj_grid.c → ext/ca_obj_grid.c} +27 -27
- data/{ca_obj_mapping.c → ext/ca_obj_mapping.c} +9 -9
- data/{ca_obj_object.c → ext/ca_obj_object.c} +37 -37
- data/{ca_obj_reduce.c → ext/ca_obj_reduce.c} +1 -1
- data/{ca_obj_refer.c → ext/ca_obj_refer.c} +33 -33
- data/{ca_obj_repeat.c → ext/ca_obj_repeat.c} +43 -43
- data/{ca_obj_select.c → ext/ca_obj_select.c} +2 -2
- data/{ca_obj_shift.c → ext/ca_obj_shift.c} +23 -23
- data/{ca_obj_transpose.c → ext/ca_obj_transpose.c} +23 -23
- data/{ca_obj_unbound_repeat.c → ext/ca_obj_unbound_repeat.c} +55 -55
- data/{ca_obj_window.c → ext/ca_obj_window.c} +26 -26
- data/{carray.h → ext/carray.h} +73 -51
- data/{carray_access.c → ext/carray_access.c} +83 -83
- data/{carray_attribute.c → ext/carray_attribute.c} +12 -12
- data/{carray_call_cfunc.c → ext/carray_call_cfunc.c} +0 -0
- data/{carray_cast.c → ext/carray_cast.c} +6 -6
- data/{carray_cast_func.rb → ext/carray_cast_func.rb} +0 -0
- data/{carray_class.c → ext/carray_class.c} +0 -0
- data/{carray_conversion.c → ext/carray_conversion.c} +8 -10
- data/{carray_copy.c → ext/carray_copy.c} +19 -19
- data/{carray_core.c → ext/carray_core.c} +2 -2
- data/ext/carray_data_type.c +66 -0
- data/{carray_element.c → ext/carray_element.c} +9 -9
- data/{carray_generate.c → ext/carray_generate.c} +0 -0
- data/{carray_iterator.c → ext/carray_iterator.c} +40 -38
- data/{carray_loop.c → ext/carray_loop.c} +24 -24
- data/{carray_mask.c → ext/carray_mask.c} +17 -6
- data/{carray_math.rb → ext/carray_math.rb} +3 -3
- data/ext/{mathfunc/carray_mathfunc.c → carray_mathfunc.c} +0 -0
- data/{carray_numeric.c → ext/carray_numeric.c} +1 -1
- data/{carray_operator.c → ext/carray_operator.c} +9 -9
- data/{carray_order.c → ext/carray_order.c} +2 -153
- data/{carray_sort_addr.c → ext/carray_sort_addr.c} +0 -0
- data/{carray_stat.c → ext/carray_stat.c} +5 -5
- data/{carray_stat_proc.rb → ext/carray_stat_proc.rb} +23 -23
- data/{carray_test.c → ext/carray_test.c} +22 -21
- data/{carray_undef.c → ext/carray_undef.c} +0 -0
- data/{carray_utils.c → ext/carray_utils.c} +0 -0
- data/{extconf.rb → ext/extconf.rb} +0 -5
- data/{mkmath.rb → ext/mkmath.rb} +0 -0
- data/{ruby_carray.c → ext/ruby_carray.c} +9 -2
- data/{ruby_ccomplex.c → ext/ruby_ccomplex.c} +0 -0
- data/{ruby_float_func.c → ext/ruby_float_func.c} +0 -0
- data/{version.h → ext/version.h} +5 -5
- data/{version.rb → ext/version.rb} +0 -0
- data/lib/carray.rb +48 -37
- data/lib/carray/{base/autoload.rb → autoload.rb} +1 -1
- data/lib/carray/autoload/autoload_base.rb +1 -1
- data/lib/carray/autoload/autoload_gem_numo_narray.rb +4 -3
- data/lib/carray/autoload/autoload_gem_random.rb +8 -0
- data/lib/carray/basic.rb +193 -0
- data/lib/carray/compose.rb +291 -0
- data/lib/carray/construct.rb +445 -0
- data/lib/carray/convert.rb +115 -0
- data/lib/carray/{base/inspect.rb → inspect.rb} +6 -6
- data/lib/carray/io/imagemagick.rb +1 -1
- data/lib/carray/{base/iterator.rb → iterator.rb} +3 -3
- data/lib/carray/mask.rb +91 -0
- data/lib/carray/{base/math.rb → math.rb} +16 -46
- data/lib/carray/math/histogram.rb +7 -7
- data/lib/carray/mkmf.rb +8 -0
- data/lib/carray/object/ca_obj_pack.rb +8 -8
- data/lib/carray/obsolete.rb +272 -0
- data/lib/carray/ordering.rb +157 -0
- data/lib/carray/{base/serialize.rb → serialize.rb} +28 -53
- data/lib/carray/{base/string.rb → string.rb} +12 -64
- data/lib/carray/{base/struct.rb → struct.rb} +16 -16
- data/lib/carray/{io/table.rb → table.rb} +1 -10
- data/lib/carray/testing.rb +56 -0
- data/lib/carray/time.rb +78 -0
- data/lib/carray/transform.rb +100 -0
- data/misc/Methods.ja.md +182 -0
- data/{NOTE → misc/NOTE} +0 -0
- data/test/test_ALL.rb +0 -2
- data/test/test_order.rb +7 -7
- data/test/test_ref_store.rb +13 -13
- data/test/test_stat.rb +7 -15
- data/{devel → utils}/guess_shape.rb +0 -0
- data/utils/{diff_method.rb → monkey_patch_methods.rb} +17 -7
- metadata +88 -151
- data/COPYING +0 -56
- data/GPL +0 -340
- data/LEGAL +0 -50
- data/TODO +0 -5
- data/carray_random.c +0 -531
- data/devel/im2col.rb +0 -17
- data/ext/calculus/carray_calculus.c +0 -931
- data/ext/calculus/carray_interp.c +0 -358
- data/ext/calculus/extconf.rb +0 -12
- data/ext/calculus/lib/math/calculus.rb +0 -119
- data/ext/calculus/lib/math/interp/adapter_interp1d.rb +0 -31
- data/ext/mathfunc/extconf.rb +0 -18
- data/ext/mathfunc/test/test_hypot.rb +0 -5
- data/ext/mathfunc/test/test_j0.rb +0 -22
- data/ext/mathfunc/test/test_jn.rb +0 -8
- data/ext/mathfunc/test/test_sph.rb +0 -9
- data/lib/carray/autoload/autoload_io_table.rb +0 -1
- data/lib/carray/autoload/autoload_math_interp.rb +0 -4
- data/lib/carray/base/basic.rb +0 -1146
- data/lib/carray/base/obsolete.rb +0 -131
- data/lib/carray/math/interp.rb +0 -57
- data/lib/carray/math/interp/adapter_gsl_spline.rb +0 -47
- data/mt19937ar.c +0 -182
- data/mt19937ar.h +0 -86
- data/rdoc_main.rb +0 -27
- data/rdoc_math.rb +0 -5
- data/rdoc_stat.rb +0 -31
- data/test/test_narray.rb +0 -64
- data/test/test_random.rb +0 -15
- data/utils/create_rdoc.sh +0 -9
- data/utils/make_tgz.sh +0 -3
@@ -0,0 +1,445 @@
|
|
1
|
+
# ----------------------------------------------------------------------------
|
2
|
+
#
|
3
|
+
# carray/constructor.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
|
+
class CArray
|
14
|
+
|
15
|
+
#
|
16
|
+
# CArray.span(data_type, range[, step])
|
17
|
+
# CArray.span(range[, step]) -> data_type guessed by range.first type
|
18
|
+
#
|
19
|
+
|
20
|
+
def self.span (*argv)
|
21
|
+
if argv.first.is_a?(Range)
|
22
|
+
type = nil
|
23
|
+
else
|
24
|
+
type, = *CArray.guess_type_and_bytes(argv.shift, nil)
|
25
|
+
end
|
26
|
+
range, step = argv[0], argv[1]
|
27
|
+
start, stop = range.begin, range.end
|
28
|
+
if step == 0
|
29
|
+
raise "step should not be 0"
|
30
|
+
end
|
31
|
+
if not type
|
32
|
+
case start
|
33
|
+
when Integer
|
34
|
+
type = CA_INT32
|
35
|
+
when Float
|
36
|
+
type = CA_FLOAT64
|
37
|
+
else
|
38
|
+
type = CA_OBJECT
|
39
|
+
end
|
40
|
+
end
|
41
|
+
if type == CA_OBJECT and not step
|
42
|
+
return CA_OBJECT(range.to_a)
|
43
|
+
else
|
44
|
+
step ||= 1
|
45
|
+
if range.exclude_end?
|
46
|
+
n = ((stop - start).abs/step).floor
|
47
|
+
else
|
48
|
+
n = ((stop - start).abs/step).floor + 1
|
49
|
+
end
|
50
|
+
if start <= stop
|
51
|
+
return CArray.new(type, [n]).seq(start, step)
|
52
|
+
else
|
53
|
+
return CArray.new(type, [n]).seq(start, -step.abs)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def span! (range)
|
59
|
+
first = range.begin.to_r
|
60
|
+
last = range.end.to_r
|
61
|
+
if range.exclude_end?
|
62
|
+
seq!(first, (last-first)/elements)
|
63
|
+
else
|
64
|
+
seq!(first, (last-first)/(elements-1))
|
65
|
+
end
|
66
|
+
return self
|
67
|
+
end
|
68
|
+
|
69
|
+
def span (range)
|
70
|
+
return template.span!(range)
|
71
|
+
end
|
72
|
+
|
73
|
+
#
|
74
|
+
#
|
75
|
+
#
|
76
|
+
|
77
|
+
def scale! (xa, xb)
|
78
|
+
xa = xa.to_f
|
79
|
+
xb = xb.to_f
|
80
|
+
seq!(xa, (xb-xa)/(elements-1))
|
81
|
+
end
|
82
|
+
|
83
|
+
def scale (xa, xb)
|
84
|
+
template.scale!(xa, xb)
|
85
|
+
end
|
86
|
+
|
87
|
+
# @private
|
88
|
+
# Create new CArray object from the return value of the block
|
89
|
+
# with data type +type+. The dimensional size and the initialization value
|
90
|
+
# are guessed from the return value of the block.
|
91
|
+
# The block should return one of the following objects.
|
92
|
+
#
|
93
|
+
# * Numeric
|
94
|
+
# * Array
|
95
|
+
# * CArray
|
96
|
+
# * an object that has either method +to_ca+ or +to_a+ or +map+
|
97
|
+
#
|
98
|
+
# When the return value of the block is a Numeric or CScalar object,
|
99
|
+
# CScalar object is returned.
|
100
|
+
#
|
101
|
+
def self.__new__ (type, *args) # :nodoc:
|
102
|
+
case v = args.first
|
103
|
+
when CArray
|
104
|
+
return ( v.data_type == type ) ? v.to_ca : v.to_type(type)
|
105
|
+
when Array
|
106
|
+
return CArray.new(type, CArray.guess_array_shape(v)) { v }
|
107
|
+
when Range
|
108
|
+
return CArray.span(type, *args)
|
109
|
+
when String
|
110
|
+
if type == CA_OBJECT
|
111
|
+
return CScalar.new(CA_OBJECT) { v }
|
112
|
+
elsif type == CA_BOOLEAN
|
113
|
+
v = v.dup
|
114
|
+
v.tr!('^01',"1")
|
115
|
+
v.tr!('01',"\x0\x1")
|
116
|
+
return CArray.boolean(v.length).load_binary(v)
|
117
|
+
else
|
118
|
+
case v
|
119
|
+
when /;/
|
120
|
+
v = v.strip.split(/\s*;\s*/).
|
121
|
+
map{|s| s.split(/\s+|\s*,\s*/).map{|x| x=='_' ? UNDEF : x} }
|
122
|
+
else
|
123
|
+
v = v.strip.split(/\s+|\s*,\s*/).map{|x| x=='_' ? UNDEF : x}
|
124
|
+
end
|
125
|
+
return CArray.new(type, CArray.guess_array_shape(v)) { v }
|
126
|
+
end
|
127
|
+
when NilClass
|
128
|
+
return CArray.new(type, [0])
|
129
|
+
else
|
130
|
+
if v.respond_to?(:to_ca)
|
131
|
+
ca = v.to_ca
|
132
|
+
return ( ca.data_type == type ) ? ca : ca.to_type(type)
|
133
|
+
else
|
134
|
+
return CScalar.new(type) { v }
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
# @private
|
140
|
+
def self.__new_fixlen__ (bytes, v) # :nodoc:
|
141
|
+
case v
|
142
|
+
when CArray
|
143
|
+
return ( v.data_type == :fixlen ) ? v.to_ca : v.to_type(:fixlen, :bytes=>bytes)
|
144
|
+
when Array
|
145
|
+
unless bytes
|
146
|
+
bytes = v.map{|s| s.length}.max
|
147
|
+
end
|
148
|
+
return CArray.new(:fixlen, CArray.guess_array_shape(v), :bytes=>bytes) { v }
|
149
|
+
when NilClass
|
150
|
+
return CArray.new(type, [0])
|
151
|
+
else
|
152
|
+
if v.respond_to?(:to_ca)
|
153
|
+
ca = v.to_ca
|
154
|
+
return ( ca.data_type == :fixlen ) ? ca : ca.to_type(:fixlen, :bytes=>bytes)
|
155
|
+
else
|
156
|
+
return CScalar.new(:fixlen, :bytes=>bytes) { v }
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
end
|
162
|
+
|
163
|
+
#
|
164
|
+
# CA_INT8(data)
|
165
|
+
# :
|
166
|
+
# CA_CMPLX256(data)
|
167
|
+
#
|
168
|
+
# Create new CArray object from +data+ with data type
|
169
|
+
# which is guessed from the method name. +data+ should be one of the following
|
170
|
+
# objects.
|
171
|
+
#
|
172
|
+
# * Numeric
|
173
|
+
# * Array
|
174
|
+
# * CArray
|
175
|
+
# * an object that has either method +to_ca+ or +to_a+ or +map+
|
176
|
+
#
|
177
|
+
# When the block returns a Numeric or CScalar object,
|
178
|
+
# the resulted array is a CScalar object.
|
179
|
+
|
180
|
+
[
|
181
|
+
"CA_BOOLEAN",
|
182
|
+
"CA_INT8",
|
183
|
+
"CA_UINT8",
|
184
|
+
"CA_INT16",
|
185
|
+
"CA_UINT16",
|
186
|
+
"CA_INT32",
|
187
|
+
"CA_UINT32",
|
188
|
+
"CA_INT64",
|
189
|
+
"CA_UINT64",
|
190
|
+
"CA_FLOAT32",
|
191
|
+
"CA_FLOAT64",
|
192
|
+
"CA_FLOAT128",
|
193
|
+
"CA_CMPLX64",
|
194
|
+
"CA_CMPLX128",
|
195
|
+
"CA_CMPLX256",
|
196
|
+
"CA_OBJECT",
|
197
|
+
"CA_BYTE",
|
198
|
+
"CA_SHORT",
|
199
|
+
"CA_INT",
|
200
|
+
"CA_FLOAT",
|
201
|
+
"CA_DOUBLE",
|
202
|
+
"CA_COMPLEX",
|
203
|
+
"CA_DCOMPLEX",
|
204
|
+
"CA_SIZE",
|
205
|
+
].each do |name|
|
206
|
+
eval %{
|
207
|
+
def #{name} (*val)
|
208
|
+
CArray.__new__(#{name}, *val)
|
209
|
+
end
|
210
|
+
}
|
211
|
+
end
|
212
|
+
|
213
|
+
def CA_FIXLEN (val, options = {})
|
214
|
+
CArray.__new_fixlen__(options[:bytes], val)
|
215
|
+
end
|
216
|
+
|
217
|
+
class CArray
|
218
|
+
|
219
|
+
module DataTypeNewConstructor
|
220
|
+
end
|
221
|
+
|
222
|
+
module DataTypeExtension
|
223
|
+
end
|
224
|
+
|
225
|
+
class Boolean
|
226
|
+
extend DataTypeNewConstructor
|
227
|
+
extend DataTypeExtension
|
228
|
+
TypeSymbol = :boolean
|
229
|
+
DataType = CA_BOOLEAN
|
230
|
+
end
|
231
|
+
|
232
|
+
class UInt8
|
233
|
+
extend DataTypeNewConstructor
|
234
|
+
extend DataTypeExtension
|
235
|
+
TypeSymbol = :uint8
|
236
|
+
DataType = CA_UINT8
|
237
|
+
end
|
238
|
+
|
239
|
+
class UInt16
|
240
|
+
extend DataTypeNewConstructor
|
241
|
+
extend DataTypeExtension
|
242
|
+
TypeSymbol = :uint16
|
243
|
+
DataType = CA_UINT16
|
244
|
+
end
|
245
|
+
|
246
|
+
class UInt32
|
247
|
+
extend DataTypeNewConstructor
|
248
|
+
extend DataTypeExtension
|
249
|
+
TypeSymbol = :uint32
|
250
|
+
DataType = CA_UINT32
|
251
|
+
end
|
252
|
+
|
253
|
+
class UInt64
|
254
|
+
extend DataTypeNewConstructor
|
255
|
+
extend DataTypeExtension
|
256
|
+
TypeSymbol = :uint64
|
257
|
+
DataType = CA_UINT64
|
258
|
+
end
|
259
|
+
|
260
|
+
class Int8
|
261
|
+
extend DataTypeNewConstructor
|
262
|
+
extend DataTypeExtension
|
263
|
+
TypeSymbol = :int8
|
264
|
+
DataType = CA_INT8
|
265
|
+
end
|
266
|
+
|
267
|
+
class Int16
|
268
|
+
extend DataTypeNewConstructor
|
269
|
+
extend DataTypeExtension
|
270
|
+
TypeSymbol = :int16
|
271
|
+
DataType = CA_INT16
|
272
|
+
end
|
273
|
+
|
274
|
+
class Int32
|
275
|
+
extend DataTypeNewConstructor
|
276
|
+
extend DataTypeExtension
|
277
|
+
TypeSymbol = :int32
|
278
|
+
DataType = CA_INT32
|
279
|
+
end
|
280
|
+
|
281
|
+
class Int64
|
282
|
+
extend DataTypeNewConstructor
|
283
|
+
extend DataTypeExtension
|
284
|
+
TypeSymbol = :int64
|
285
|
+
DataType = CA_INT64
|
286
|
+
end
|
287
|
+
|
288
|
+
class Float32
|
289
|
+
extend DataTypeNewConstructor
|
290
|
+
extend DataTypeExtension
|
291
|
+
TypeSymbol = :float32
|
292
|
+
DataType = CA_FLOAT32
|
293
|
+
end
|
294
|
+
|
295
|
+
SFloat = Float32
|
296
|
+
|
297
|
+
class Float64
|
298
|
+
extend DataTypeNewConstructor
|
299
|
+
extend DataTypeExtension
|
300
|
+
TypeSymbol = :float64
|
301
|
+
DataType = CA_FLOAT64
|
302
|
+
end
|
303
|
+
|
304
|
+
DFloat = Float64
|
305
|
+
|
306
|
+
class Complex64
|
307
|
+
extend DataTypeNewConstructor
|
308
|
+
extend DataTypeExtension
|
309
|
+
TypeSymbol = :complex64
|
310
|
+
DataType = CA_CMPLX64
|
311
|
+
end
|
312
|
+
|
313
|
+
SComplex = Complex64
|
314
|
+
|
315
|
+
class Complex128
|
316
|
+
extend DataTypeNewConstructor
|
317
|
+
extend DataTypeExtension
|
318
|
+
TypeSymbol = :complex128
|
319
|
+
DataType = CA_CMPLX128
|
320
|
+
end
|
321
|
+
|
322
|
+
DComplex = Complex128
|
323
|
+
|
324
|
+
class Object
|
325
|
+
extend DataTypeNewConstructor
|
326
|
+
extend DataTypeExtension
|
327
|
+
TypeSymbol = :object
|
328
|
+
DataType = CA_OBJECT
|
329
|
+
end
|
330
|
+
|
331
|
+
RObject = Object
|
332
|
+
|
333
|
+
class Fixlen
|
334
|
+
extend DataTypeNewConstructor
|
335
|
+
extend DataTypeExtension
|
336
|
+
TypeSymbol = :fixlen
|
337
|
+
DataType = CA_FIXLEN
|
338
|
+
end
|
339
|
+
|
340
|
+
end
|
341
|
+
|
342
|
+
class CArray
|
343
|
+
extend DataTypeExtension
|
344
|
+
TypeSymbol = nil
|
345
|
+
DataType = nil
|
346
|
+
end
|
347
|
+
|
348
|
+
class CArray
|
349
|
+
|
350
|
+
module DataTypeNewConstructor
|
351
|
+
|
352
|
+
def new (*shape)
|
353
|
+
CArray.new(self::DataType, shape)
|
354
|
+
end
|
355
|
+
|
356
|
+
end
|
357
|
+
|
358
|
+
module DataTypeExtension
|
359
|
+
|
360
|
+
def guess_data_type_from_values (*values)
|
361
|
+
if values.all? {|v| v == true || v == false }
|
362
|
+
CA_BOOLEAN
|
363
|
+
elsif values.all? { |v| v.is_a?(Integer) }
|
364
|
+
CA_INT64
|
365
|
+
elsif values.all? { |v| v.is_a?(Float) }
|
366
|
+
CA_FLOAT64
|
367
|
+
elsif values.all? { |v| v.is_a?(Complex) }
|
368
|
+
CA_CMPLX128
|
369
|
+
else
|
370
|
+
CA_OBJECT
|
371
|
+
end
|
372
|
+
end
|
373
|
+
|
374
|
+
private :guess_data_type_from_values
|
375
|
+
|
376
|
+
def zeros (*shape)
|
377
|
+
CArray.new(self::DataType || CA_FLOAT64, shape).zero
|
378
|
+
end
|
379
|
+
|
380
|
+
def ones (*shape)
|
381
|
+
CArray.new(self::DataType || CA_FLOAT64 , shape).one
|
382
|
+
end
|
383
|
+
|
384
|
+
def eye (n, m = nil, k = 0)
|
385
|
+
m ||= n
|
386
|
+
mat = CArray.new(self::DataType || CA_FLOAT64, [n, m])
|
387
|
+
start = k > 0 ? k : m - k - 1
|
388
|
+
mat[[start..-1,m+1]] = 1
|
389
|
+
mat
|
390
|
+
end
|
391
|
+
|
392
|
+
def identity (n)
|
393
|
+
mat = CArray.new(self::DataType || CA_FLOAT64, [n, n])
|
394
|
+
mat[[nil,n+1]] = 1
|
395
|
+
mat
|
396
|
+
end
|
397
|
+
|
398
|
+
def linspace (x1, x2, n = 100)
|
399
|
+
data_type = self::DataType
|
400
|
+
data_type ||= guess_data_type_from_values(x1, x2)
|
401
|
+
CArray.new(data_type, [n]).span(x1..x2)
|
402
|
+
end
|
403
|
+
|
404
|
+
def arange (*args)
|
405
|
+
case args.size
|
406
|
+
when 3
|
407
|
+
start, stop, step = *args
|
408
|
+
when 2
|
409
|
+
start, stop = *args
|
410
|
+
step = 1
|
411
|
+
when 1
|
412
|
+
start = 0
|
413
|
+
stop, = *args
|
414
|
+
step = 1
|
415
|
+
end
|
416
|
+
data_type = self::DataType
|
417
|
+
data_type ||= guess_data_type_from_values(start, stop, step)
|
418
|
+
CArray.__new__(data_type, start..stop, step)
|
419
|
+
end
|
420
|
+
|
421
|
+
def full (shape, fill_value)
|
422
|
+
data_type = self::DataType
|
423
|
+
data_type ||= guess_data_type_from_values(fill_value)
|
424
|
+
shape = [shape] unless shape.is_a?(Array)
|
425
|
+
CArray.new(data_type, shape).fill(fill_value)
|
426
|
+
end
|
427
|
+
|
428
|
+
end
|
429
|
+
|
430
|
+
end
|
431
|
+
|
432
|
+
class CArray
|
433
|
+
|
434
|
+
def self.meshgrid (*args)
|
435
|
+
dim = args.map(&:size)
|
436
|
+
out = []
|
437
|
+
args.each_with_index do |arg, i|
|
438
|
+
newdim = dim.dup
|
439
|
+
newdim[i] = :%
|
440
|
+
out[i] = arg[*newdim].to_ca
|
441
|
+
end
|
442
|
+
return *out
|
443
|
+
end
|
444
|
+
|
445
|
+
end
|
@@ -0,0 +1,115 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
|
4
|
+
class CArray
|
5
|
+
|
6
|
+
#
|
7
|
+
# Returns the 8-bit integer CArray object filled with 0 which
|
8
|
+
# dimension size is same as +self+. The resulted array represents
|
9
|
+
# the logical array which has +false+ for its all elements.
|
10
|
+
#
|
11
|
+
def false ()
|
12
|
+
return template(:boolean)
|
13
|
+
end
|
14
|
+
|
15
|
+
#
|
16
|
+
# Returns the 8-bit integer CArray object filled with 1 which
|
17
|
+
# dimension size is same as +self+. The resulted array represents
|
18
|
+
# the logical array which has +true+ for its all elements.
|
19
|
+
#
|
20
|
+
def true ()
|
21
|
+
return template(:boolean) { 1 }
|
22
|
+
end
|
23
|
+
|
24
|
+
# Returns map
|
25
|
+
def map (&block)
|
26
|
+
return self.convert(CA_OBJECT, &block).to_a
|
27
|
+
end
|
28
|
+
|
29
|
+
#
|
30
|
+
# ref = CA_INT([[0,1,2],[1,2,0],[2,0,1]])
|
31
|
+
# a = CArray.int(3,3).seq(1)
|
32
|
+
# b = CArray.int(3,3).seq(11)
|
33
|
+
# c = CArray.int(3,3).seq(21)
|
34
|
+
#
|
35
|
+
# CArray.pickup(CA_OBJECT, ref, [a,b,c])
|
36
|
+
# => <CArray.object(3,3): elem=9 mem=72b
|
37
|
+
# [ [ 1, 12, 23 ],
|
38
|
+
# [ 14, 25, 6 ],
|
39
|
+
# [ 27, 8, 19 ] ]>
|
40
|
+
#
|
41
|
+
# CArray.pickup(CA_OBJECT, ref, ["a","b","c"])
|
42
|
+
# => <CArray.object(3,3): elem=9 mem=36b
|
43
|
+
# [ [ "a", "b", "c" ],
|
44
|
+
# [ "b", "c", "a" ],
|
45
|
+
# [ "c", "a", "b" ] ]>
|
46
|
+
#
|
47
|
+
def self.pickup (data_type, ref, args)
|
48
|
+
out = ref.template(data_type)
|
49
|
+
args.each_with_index do |v, i|
|
50
|
+
s = ref.eq(i)
|
51
|
+
case v
|
52
|
+
when CArray
|
53
|
+
out[s] = v[s]
|
54
|
+
else
|
55
|
+
out[s] = v
|
56
|
+
end
|
57
|
+
end
|
58
|
+
return out
|
59
|
+
end
|
60
|
+
|
61
|
+
# Array#join like method
|
62
|
+
#
|
63
|
+
# > a = CArray.object(3,3).seq("a",:succ)
|
64
|
+
# => <CArray.object(3,3): elem=9 mem=72b
|
65
|
+
# [ [ "a", "b", "c" ],
|
66
|
+
# [ "d", "e", "f" ],
|
67
|
+
# [ "g", "h", "i" ] ]>
|
68
|
+
#
|
69
|
+
# > a.join("\n",",")
|
70
|
+
# => "a,b,c\nd,e,f\ng,h,i"
|
71
|
+
#
|
72
|
+
|
73
|
+
def join (*argv)
|
74
|
+
case argv.size
|
75
|
+
when 0
|
76
|
+
return to_a.join()
|
77
|
+
when 1
|
78
|
+
sep = argv.shift
|
79
|
+
return to_a.join(sep)
|
80
|
+
else
|
81
|
+
sep = argv.shift
|
82
|
+
return self[:i, false].map { |s|
|
83
|
+
s[0, false].join(*argv)
|
84
|
+
}.join(sep)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
#
|
89
|
+
def to_bit_string (nb)
|
90
|
+
hex = CArray.uint8(((nb*elements)/8.0).ceil)
|
91
|
+
hex.bits[nil].paste([0], self.bits[false,[(nb-1)..0]].flatten)
|
92
|
+
hex.bits[] = hex.bits[nil,[-1..0]]
|
93
|
+
return hex.to_s
|
94
|
+
end
|
95
|
+
|
96
|
+
def from_bit_string (bstr, nb)
|
97
|
+
hex = CArray.uint8(bstr.length).load_binary(bstr)
|
98
|
+
hex.bits[] = hex.bits[nil,[-1..0]]
|
99
|
+
bits = hex.bits.flatten
|
100
|
+
self.bits[false,[(nb-1)..0]][nil].paste([0], bits)
|
101
|
+
return self
|
102
|
+
end
|
103
|
+
|
104
|
+
def self.from_bit_string (bstr, nb, data_type=CA_INT32, dim=nil)
|
105
|
+
if dim
|
106
|
+
obj = CArray.new(data_type, dim)
|
107
|
+
else
|
108
|
+
dim0 = ((bstr.length*8)/nb.to_f).floor
|
109
|
+
obj = CArray.new(data_type, [dim0])
|
110
|
+
end
|
111
|
+
obj.from_bit_string(bstr, nb)
|
112
|
+
return obj
|
113
|
+
end
|
114
|
+
|
115
|
+
end
|