carray 1.4.0 → 1.5.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.yardopts +32 -0
- data/LICENSE +22 -0
- data/NEWS.md +83 -0
- data/README.md +38 -18
- data/Rakefile +2 -2
- data/TODO.md +17 -0
- data/carray.gemspec +13 -18
- data/{ca_iter_block.c → ext/ca_iter_block.c} +16 -18
- data/{ca_iter_dimension.c → ext/ca_iter_dimension.c} +20 -21
- data/{ca_iter_window.c → ext/ca_iter_window.c} +12 -14
- data/{ca_obj_array.c → ext/ca_obj_array.c} +451 -176
- data/{ca_obj_bitarray.c → ext/ca_obj_bitarray.c} +18 -23
- data/{ca_obj_bitfield.c → ext/ca_obj_bitfield.c} +12 -16
- data/{ca_obj_block.c → ext/ca_obj_block.c} +47 -54
- data/{ca_obj_fake.c → ext/ca_obj_fake.c} +10 -12
- data/{ca_obj_farray.c → ext/ca_obj_farray.c} +21 -23
- data/{ca_obj_field.c → ext/ca_obj_field.c} +30 -32
- data/{ca_obj_grid.c → ext/ca_obj_grid.c} +32 -33
- data/{ca_obj_mapping.c → ext/ca_obj_mapping.c} +11 -13
- data/{ca_obj_object.c → ext/ca_obj_object.c} +40 -42
- data/{ca_obj_reduce.c → ext/ca_obj_reduce.c} +3 -5
- data/{ca_obj_refer.c → ext/ca_obj_refer.c} +44 -48
- data/{ca_obj_repeat.c → ext/ca_obj_repeat.c} +45 -47
- data/{ca_obj_select.c → ext/ca_obj_select.c} +4 -6
- data/{ca_obj_shift.c → ext/ca_obj_shift.c} +26 -28
- data/{ca_obj_transpose.c → ext/ca_obj_transpose.c} +26 -28
- data/{ca_obj_unbound_repeat.c → ext/ca_obj_unbound_repeat.c} +106 -160
- data/{ca_obj_window.c → ext/ca_obj_window.c} +33 -35
- data/{carray.h → ext/carray.h} +86 -59
- data/{carray_access.c → ext/carray_access.c} +194 -101
- data/{carray_attribute.c → ext/carray_attribute.c} +161 -207
- data/{carray_call_cfunc.c → ext/carray_call_cfunc.c} +1 -3
- data/{carray_cast.c → ext/carray_cast.c} +351 -150
- data/{carray_cast_func.rb → ext/carray_cast_func.rb} +1 -2
- data/{carray_class.c → ext/carray_class.c} +28 -36
- data/{carray_conversion.c → ext/carray_conversion.c} +63 -68
- data/{carray_copy.c → ext/carray_copy.c} +34 -50
- data/{carray_core.c → ext/carray_core.c} +75 -62
- data/ext/carray_data_type.c +66 -0
- data/{carray_element.c → ext/carray_element.c} +34 -53
- data/{carray_generate.c → ext/carray_generate.c} +71 -50
- data/{carray_iterator.c → ext/carray_iterator.c} +53 -53
- data/{carray_loop.c → ext/carray_loop.c} +77 -106
- data/{carray_mask.c → ext/carray_mask.c} +105 -114
- data/{carray_math.rb → ext/carray_math.rb} +29 -13
- data/ext/{mathfunc/carray_mathfunc.c → carray_mathfunc.c} +1 -3
- data/{carray_numeric.c → ext/carray_numeric.c} +43 -46
- data/{carray_operator.c → ext/carray_operator.c} +49 -36
- data/{carray_order.c → ext/carray_order.c} +232 -217
- data/{carray_sort_addr.c → ext/carray_sort_addr.c} +14 -21
- data/{carray_stat.c → ext/carray_stat.c} +6 -8
- data/{carray_stat_proc.rb → ext/carray_stat_proc.rb} +25 -27
- data/{carray_test.c → ext/carray_test.c} +63 -51
- data/{carray_undef.c → ext/carray_undef.c} +1 -11
- data/{carray_utils.c → ext/carray_utils.c} +12 -4
- data/{extconf.rb → ext/extconf.rb} +10 -7
- data/{mkmath.rb → ext/mkmath.rb} +2 -2
- data/{ruby_carray.c → ext/ruby_carray.c} +20 -8
- data/{ruby_ccomplex.c → ext/ruby_ccomplex.c} +2 -4
- data/{ruby_float_func.c → ext/ruby_float_func.c} +1 -3
- data/ext/version.h +16 -0
- data/{version.rb → ext/version.rb} +0 -0
- data/lib/carray.rb +51 -40
- data/lib/carray/{base/autoload.rb → autoload.rb} +1 -3
- data/lib/carray/autoload/autoload_base.rb +1 -1
- data/lib/carray/autoload/autoload_gem_numo_narray.rb +7 -6
- data/lib/carray/autoload/autoload_gem_random.rb +8 -0
- data/lib/carray/basic.rb +191 -0
- data/lib/carray/broadcast.rb +101 -0
- data/lib/carray/compose.rb +315 -0
- data/lib/carray/construct.rb +484 -0
- data/lib/carray/convert.rb +115 -0
- data/lib/carray/info.rb +1 -3
- data/lib/carray/{base/inspect.rb → inspect.rb} +9 -11
- data/lib/carray/io/imagemagick.rb +2 -4
- data/lib/carray/{base/iterator.rb → iterator.rb} +6 -6
- data/lib/carray/mask.rb +102 -0
- data/lib/carray/{base/math.rb → math.rb} +20 -52
- data/lib/carray/math/histogram.rb +8 -10
- data/lib/carray/math/recurrence.rb +1 -3
- data/lib/carray/mkmf.rb +9 -3
- data/lib/carray/object/ca_obj_iterator.rb +1 -3
- data/lib/carray/object/ca_obj_link.rb +1 -3
- data/lib/carray/object/ca_obj_pack.rb +9 -11
- data/lib/carray/obsolete.rb +256 -0
- data/lib/carray/ordering.rb +181 -0
- data/lib/carray/{base/serialize.rb → serialize.rb} +60 -76
- data/lib/carray/{base/string.rb → string.rb} +10 -64
- data/lib/carray/{base/struct.rb → struct.rb} +19 -21
- data/lib/carray/{io/table.rb → table.rb} +1 -10
- data/lib/carray/testing.rb +51 -0
- data/lib/carray/time.rb +76 -0
- data/lib/carray/transform.rb +109 -0
- data/misc/Methods.ja.md +182 -0
- data/{NOTE → misc/NOTE} +16 -38
- data/spec/Classes/CABitfield_spec.rb +58 -0
- data/spec/Classes/CABlockIterator_spec.rb +114 -0
- data/spec/Classes/CABlock_spec.rb +205 -0
- data/spec/Classes/CAField_spec.rb +39 -0
- data/spec/Classes/CAGrid_spec.rb +75 -0
- data/spec/Classes/CAMap_spec.rb +0 -0
- data/{test/test_CAMapping.rb → spec/Classes/CAMapping_spec.rb} +35 -36
- data/spec/Classes/CAObject_attribute_spec.rb +33 -0
- data/spec/Classes/CAObject_spec.rb +33 -0
- data/spec/Classes/CARefer_spec.rb +93 -0
- data/spec/Classes/CARepeat_spec.rb +65 -0
- data/spec/Classes/CASelect_spec.rb +22 -0
- data/spec/Classes/CAShift_spec.rb +16 -0
- data/spec/Classes/CAStruct_spec.rb +71 -0
- data/{test/test_CATranspose.rb → spec/Classes/CATranspose_spec.rb} +20 -21
- data/spec/Classes/CAUnboudRepeat_spec.rb +102 -0
- data/spec/Classes/CAWindow_spec.rb +54 -0
- data/spec/Classes/CAWrap_spec.rb +8 -0
- data/{test/test_CArray.rb → spec/Classes/CArray_spec.rb} +48 -92
- data/spec/Classes/CScalar_spec.rb +55 -0
- data/spec/Features/feature_130_spec.rb +19 -0
- data/spec/Features/feature_attributes_spec.rb +280 -0
- data/spec/Features/feature_boolean_spec.rb +98 -0
- data/spec/Features/feature_broadcast.rb +116 -0
- data/spec/Features/feature_cast_function.rb +19 -0
- data/spec/Features/feature_cast_spec.rb +33 -0
- data/spec/Features/feature_class_spec.rb +84 -0
- data/spec/Features/feature_complex_spec.rb +42 -0
- data/{test/test_composite.rb → spec/Features/feature_composite_spec.rb} +17 -18
- data/spec/Features/feature_convert_spec.rb +46 -0
- data/spec/Features/feature_copy_spec.rb +123 -0
- data/spec/Features/feature_creation_spec.rb +84 -0
- data/spec/Features/feature_element_spec.rb +144 -0
- data/spec/Features/feature_extream_spec.rb +54 -0
- data/spec/Features/feature_generate_spec.rb +74 -0
- data/spec/Features/feature_index_spec.rb +69 -0
- data/spec/Features/feature_mask_spec.rb +580 -0
- data/spec/Features/feature_math_spec.rb +97 -0
- data/spec/Features/feature_order_spec.rb +146 -0
- data/spec/Features/feature_ref_store_spec.rb +209 -0
- data/spec/Features/feature_serialization_spec.rb +125 -0
- data/spec/Features/feature_stat_spec.rb +397 -0
- data/spec/Features/feature_virtual_spec.rb +48 -0
- data/spec/Features/method_eq_spec.rb +81 -0
- data/spec/Features/method_is_nan_spec.rb +12 -0
- data/spec/Features/method_map_spec.rb +54 -0
- data/spec/Features/method_max_with.rb +20 -0
- data/spec/Features/method_min_with.rb +19 -0
- data/spec/Features/method_ne_spec.rb +18 -0
- data/spec/Features/method_project_spec.rb +188 -0
- data/spec/Features/method_ref_spec.rb +27 -0
- data/spec/Features/method_round_spec.rb +11 -0
- data/spec/Features/method_s_linspace_spec.rb +48 -0
- data/spec/Features/method_s_span_spec.rb +14 -0
- data/spec/Features/method_seq_spec.rb +47 -0
- data/spec/Features/method_sort_with.rb +43 -0
- data/spec/Features/method_sorted_with.rb +29 -0
- data/spec/Features/method_span_spec.rb +42 -0
- data/spec/Features/method_wrap_readonly_spec.rb +43 -0
- data/{test → spec/UnitTest}/test_CAVirtual.rb +0 -0
- data/spec/spec_all.rb +0 -1
- data/utils/convert_test.rb +73 -0
- data/utils/{extract_rdoc.rb → extract_yard.rb} +7 -12
- data/{devel → utils}/guess_shape.rb +0 -0
- data/utils/{diff_method.rb → monkey_patch_methods.rb} +17 -7
- metadata +159 -206
- 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/spec/CABlockIterator/CABlockIterator_spec.rb +0 -113
- data/spec/CArray/bug/store_spec.rb +0 -27
- data/spec/CArray/index/repeat_spec.rb +0 -10
- data/spec/CArray/method/eq_spec.rb +0 -80
- data/spec/CArray/method/is_nan_spec.rb +0 -12
- data/spec/CArray/method/ne_spec.rb +0 -18
- data/spec/CArray/method/round_spec.rb +0 -11
- data/spec/CArray/object/_attribute_spec.rb +0 -32
- data/spec/CArray/object/s_new_spec.rb +0 -31
- data/spec/CArray/serialize/Serialization_spec.rb +0 -89
- data/test/test_130.rb +0 -23
- data/test/test_ALL.rb +0 -51
- data/test/test_CABitfield.rb +0 -59
- data/test/test_CABlock.rb +0 -208
- data/test/test_CAField.rb +0 -40
- data/test/test_CAGrid.rb +0 -76
- data/test/test_CAMmap.rb +0 -11
- data/test/test_CARefer.rb +0 -94
- data/test/test_CARepeat.rb +0 -66
- data/test/test_CASelect.rb +0 -23
- data/test/test_CAShift.rb +0 -17
- data/test/test_CAWindow.rb +0 -55
- data/test/test_CAWrap.rb +0 -9
- data/test/test_CComplex.rb +0 -83
- data/test/test_CScalar.rb +0 -91
- data/test/test_attribute.rb +0 -281
- data/test/test_block_iterator.rb +0 -17
- data/test/test_boolean.rb +0 -99
- data/test/test_cast.rb +0 -33
- data/test/test_class.rb +0 -85
- data/test/test_complex.rb +0 -43
- data/test/test_convert.rb +0 -79
- data/test/test_copy.rb +0 -141
- data/test/test_creation.rb +0 -85
- data/test/test_element.rb +0 -146
- data/test/test_extream.rb +0 -55
- data/test/test_generate.rb +0 -75
- data/test/test_index.rb +0 -71
- data/test/test_mask.rb +0 -578
- data/test/test_math.rb +0 -98
- data/test/test_narray.rb +0 -64
- data/test/test_order.rb +0 -147
- data/test/test_random.rb +0 -15
- data/test/test_ref_store.rb +0 -211
- data/test/test_stat.rb +0 -414
- data/test/test_struct.rb +0 -72
- data/test/test_virtual.rb +0 -49
- data/utils/create_rdoc.sh +0 -9
- data/utils/make_tgz.sh +0 -3
- data/version.h +0 -18
data/lib/carray/basic.rb
ADDED
@@ -0,0 +1,191 @@
|
|
1
|
+
# ----------------------------------------------------------------------------
|
2
|
+
#
|
3
|
+
# carray/basic.rb
|
4
|
+
#
|
5
|
+
# This file is part of Ruby/CArray extension library.
|
6
|
+
#
|
7
|
+
# Copyright (C) 2005-2020 Hiroki Motoyoshi
|
8
|
+
#
|
9
|
+
# ----------------------------------------------------------------------------
|
10
|
+
|
11
|
+
module CAMath
|
12
|
+
include Math
|
13
|
+
end
|
14
|
+
|
15
|
+
#
|
16
|
+
# monkey patch
|
17
|
+
#
|
18
|
+
|
19
|
+
def nan
|
20
|
+
0.0/0.0
|
21
|
+
end
|
22
|
+
|
23
|
+
class Array # :nodoc:
|
24
|
+
def +@
|
25
|
+
return CA_SIZE(self)
|
26
|
+
end
|
27
|
+
def to_ca
|
28
|
+
return CA_OBJECT(self)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
class Range # :nodoc:
|
33
|
+
def +@
|
34
|
+
return CA_SIZE(self)
|
35
|
+
end
|
36
|
+
def to_ca
|
37
|
+
return CA_OBJECT(self)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
class Numeric
|
42
|
+
|
43
|
+
def eq (other)
|
44
|
+
case other
|
45
|
+
when CArray
|
46
|
+
return other.eq(self)
|
47
|
+
else
|
48
|
+
return send(:eq, *other.coerce(self))
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def ne (other)
|
53
|
+
case other
|
54
|
+
when CArray
|
55
|
+
return other.ne(self)
|
56
|
+
else
|
57
|
+
return send(:ne, *other.coerce(self))
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
class CArray
|
64
|
+
|
65
|
+
def has_attribute?
|
66
|
+
if ( not @attribute ) or @attribute.empty?
|
67
|
+
return false
|
68
|
+
else
|
69
|
+
return true
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def attribute= (obj)
|
74
|
+
unless obj.is_a?(Hash)
|
75
|
+
raise "attribute should be a hash object"
|
76
|
+
end
|
77
|
+
@attribute = obj
|
78
|
+
end
|
79
|
+
|
80
|
+
def attribute
|
81
|
+
@attribute ||= {}
|
82
|
+
return @attribute
|
83
|
+
end
|
84
|
+
|
85
|
+
def first
|
86
|
+
self[0]
|
87
|
+
end
|
88
|
+
|
89
|
+
def last
|
90
|
+
self[-1]
|
91
|
+
end
|
92
|
+
|
93
|
+
# matchup
|
94
|
+
|
95
|
+
def matchup (ref)
|
96
|
+
ri = ref.sort_addr
|
97
|
+
rs = ref[ri].to_ca
|
98
|
+
si = rs.bsearch(self)
|
99
|
+
return ri.project(si)
|
100
|
+
end
|
101
|
+
|
102
|
+
def matchup_nearest (ref, direction: "round")
|
103
|
+
ri = ref.sort_addr
|
104
|
+
rs = ref[ri].to_ca
|
105
|
+
si = rs.section(self).send(direction.intern).int64
|
106
|
+
si.trim!(0,si.size)
|
107
|
+
return ri[si].to_ca
|
108
|
+
end
|
109
|
+
|
110
|
+
|
111
|
+
# index / indices / axes
|
112
|
+
|
113
|
+
def address ()
|
114
|
+
return CArray.int32(*dim).seq!
|
115
|
+
end
|
116
|
+
|
117
|
+
def index (n = 0)
|
118
|
+
unless n.is_a?(Integer)
|
119
|
+
raise ArgumentError, "argument should be an integer"
|
120
|
+
end
|
121
|
+
if n.between?(0, ndim-1)
|
122
|
+
return CArray.int32(dim[n]).seq!
|
123
|
+
else
|
124
|
+
raise ArgumentError,
|
125
|
+
"invalid dimension specifier #{n} (0..#{self.ndim-1})"
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
#
|
130
|
+
#
|
131
|
+
#
|
132
|
+
|
133
|
+
def indices
|
134
|
+
list = Array.new(ndim) {|i|
|
135
|
+
rpt = self.dim
|
136
|
+
rpt[i] = :%
|
137
|
+
index(i)[*rpt]
|
138
|
+
}
|
139
|
+
if block_given?
|
140
|
+
return yield(*list)
|
141
|
+
else
|
142
|
+
return list
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
|
147
|
+
|
148
|
+
end
|
149
|
+
|
150
|
+
class CArray
|
151
|
+
|
152
|
+
# Returns object carray has elements of splitted carray at dimensions
|
153
|
+
# which is given by arguments
|
154
|
+
#
|
155
|
+
# a = CA_INT([[1,2,3], [4,5,6], [7,8,9]])
|
156
|
+
#
|
157
|
+
# a.split(0)
|
158
|
+
# [1,2,3], [4,5,6], [7,8,9]
|
159
|
+
#
|
160
|
+
# a.split(1)
|
161
|
+
# [1,4,7], [2,5,8], [3,6,9]
|
162
|
+
#
|
163
|
+
|
164
|
+
def split (*argv)
|
165
|
+
odim = dim.values_at(*argv)
|
166
|
+
out = CArray.object(*odim)
|
167
|
+
idx = [nil] * ndim
|
168
|
+
attach {
|
169
|
+
out.map_with_index! do |o, v|
|
170
|
+
argv.each_with_index do |r, i|
|
171
|
+
idx[r] = v[i]
|
172
|
+
end
|
173
|
+
self[*idx].to_ca
|
174
|
+
end
|
175
|
+
}
|
176
|
+
return out
|
177
|
+
end
|
178
|
+
|
179
|
+
end
|
180
|
+
|
181
|
+
class CAUnboundRepeat
|
182
|
+
|
183
|
+
def template (*argv, &block)
|
184
|
+
return parent.template(*argv,&block)[*spec.map{|x| x != :* ? nil : x}]
|
185
|
+
end
|
186
|
+
|
187
|
+
end
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
|
@@ -0,0 +1,101 @@
|
|
1
|
+
# ----------------------------------------------------------------------------
|
2
|
+
#
|
3
|
+
# carray/broadcast.rb
|
4
|
+
#
|
5
|
+
# This file is part of Ruby/CArray extension library.
|
6
|
+
#
|
7
|
+
# Copyright (C) 2005-2020 Hiroki Motoyoshi
|
8
|
+
#
|
9
|
+
# ----------------------------------------------------------------------------
|
10
|
+
|
11
|
+
class CArray
|
12
|
+
|
13
|
+
def broadcast_to (*newdim)
|
14
|
+
|
15
|
+
if newdim.size < ndim
|
16
|
+
raise "(Broadcasting) can't broadcast to #{newdim.inspect} because too small rank is specified"
|
17
|
+
end
|
18
|
+
|
19
|
+
#
|
20
|
+
# Try to build unbound repeat index (includes :*)
|
21
|
+
# with broadcasting rule in Numpy.
|
22
|
+
#
|
23
|
+
repdim = []
|
24
|
+
shape = []
|
25
|
+
|
26
|
+
srcdim = dim.dup
|
27
|
+
dstdim = newdim.dup
|
28
|
+
sd = srcdim.pop
|
29
|
+
dd = dstdim.pop
|
30
|
+
while dd
|
31
|
+
if sd == dd
|
32
|
+
repdim.unshift nil
|
33
|
+
shape.unshift(dd)
|
34
|
+
sd = srcdim.pop
|
35
|
+
elsif dd == 1
|
36
|
+
repdim.unshift :*
|
37
|
+
elsif sd == 1
|
38
|
+
repdim.unshift :*
|
39
|
+
sd = srcdim.pop
|
40
|
+
else
|
41
|
+
raise "(Broadcasting) can't broadcast to #{newdim.inspect} "
|
42
|
+
end
|
43
|
+
dd = dstdim.pop
|
44
|
+
end
|
45
|
+
|
46
|
+
#
|
47
|
+
# Call Unbound repeat's bind
|
48
|
+
#
|
49
|
+
return self.reshape(*shape)[*repdim].bind(*newdim) if repdim.include?(:*)
|
50
|
+
|
51
|
+
self
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
class CScalar
|
57
|
+
|
58
|
+
def broadcast_to (*newdim)
|
59
|
+
out = CArray.new(data_type, newdim, bytes: bytes)
|
60
|
+
out[] = self
|
61
|
+
out
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
class CAUnboundRepeat
|
67
|
+
|
68
|
+
alias broadcast_to bind
|
69
|
+
|
70
|
+
end
|
71
|
+
|
72
|
+
def CArray.broadcast (*argv, expand_scalar: false, &block)
|
73
|
+
|
74
|
+
sel = argv.select { |arg| arg.is_a?(CArray) }
|
75
|
+
return argv if sel.empty?
|
76
|
+
|
77
|
+
dim = []
|
78
|
+
ndim = sel.map(&:ndim).max
|
79
|
+
ndim.times do |k|
|
80
|
+
dim[k] = sel.map { |arg| arg.dim[k] || 1 }.max
|
81
|
+
end
|
82
|
+
|
83
|
+
if not expand_scalar
|
84
|
+
list = argv.map { |arg|
|
85
|
+
case arg
|
86
|
+
when CScalar
|
87
|
+
arg
|
88
|
+
when CArray
|
89
|
+
arg.broadcast_to(*dim)
|
90
|
+
else
|
91
|
+
arg
|
92
|
+
end
|
93
|
+
}
|
94
|
+
else
|
95
|
+
list = argv.map { |arg| arg.is_a?(CArray) ? arg.broadcast_to(*dim) : arg }
|
96
|
+
end
|
97
|
+
|
98
|
+
return block.call(*list) if block
|
99
|
+
return list
|
100
|
+
end
|
101
|
+
|
@@ -0,0 +1,315 @@
|
|
1
|
+
# ----------------------------------------------------------------------------
|
2
|
+
#
|
3
|
+
# carray/composition.rb
|
4
|
+
#
|
5
|
+
# This file is part of Ruby/CArray extension library.
|
6
|
+
#
|
7
|
+
# Copyright (C) 2005-2020 Hiroki Motoyoshi
|
8
|
+
#
|
9
|
+
# ----------------------------------------------------------------------------
|
10
|
+
|
11
|
+
class CArray
|
12
|
+
|
13
|
+
# Returns the array resized to the dimension given as `newdim`.
|
14
|
+
# The new area is filled by the value returned by the block.
|
15
|
+
def resize (*newdim, &block)
|
16
|
+
if newdim.size != ndim
|
17
|
+
raise "ndim mismatch"
|
18
|
+
end
|
19
|
+
offset = Array.new(ndim){0}
|
20
|
+
ndim.times do |i|
|
21
|
+
d = newdim[i]
|
22
|
+
case d
|
23
|
+
when nil
|
24
|
+
newdim[i] = dim[i]
|
25
|
+
when Integer
|
26
|
+
if d < 0
|
27
|
+
newdim[i] *= -1
|
28
|
+
offset[i] = newdim[i] - dim[i]
|
29
|
+
end
|
30
|
+
else
|
31
|
+
raise "invalid dimension size"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
out = CArray.new(data_type, newdim, &block)
|
35
|
+
out.data_class = data_class if has_data_class?
|
36
|
+
if out.has_mask?
|
37
|
+
out.mask.paste(offset, self.false)
|
38
|
+
end
|
39
|
+
out.paste(offset, self)
|
40
|
+
return out
|
41
|
+
end
|
42
|
+
|
43
|
+
# insert
|
44
|
+
def insert_block (offset, bsize, &block)
|
45
|
+
if offset.size != ndim or
|
46
|
+
bsize.size != ndim
|
47
|
+
raise "ndim mismatch"
|
48
|
+
end
|
49
|
+
newdim = dim
|
50
|
+
grids = dim.map{|d| CArray.int32(d) }
|
51
|
+
ndim.times do |i|
|
52
|
+
if offset[i] < 0
|
53
|
+
offset[i] += dim[i]
|
54
|
+
end
|
55
|
+
if offset[i] < 0 or offset[i] >= dim[i] or bsize[i] < 0
|
56
|
+
raise "invalid offset or size"
|
57
|
+
end
|
58
|
+
if bsize[i] > 0
|
59
|
+
newdim[i] += bsize[i]
|
60
|
+
end
|
61
|
+
grids[i][0...offset[i]].seq!
|
62
|
+
grids[i][offset[i]..-1].seq!(offset[i]+bsize[i])
|
63
|
+
end
|
64
|
+
out = CArray.new(data_type, newdim)
|
65
|
+
out.data_class = data_class if has_data_class?
|
66
|
+
if block_given?
|
67
|
+
sel = out.true
|
68
|
+
sel[*grids] = 0
|
69
|
+
out[sel] = block.call
|
70
|
+
end
|
71
|
+
out[*grids] = self
|
72
|
+
return out
|
73
|
+
end
|
74
|
+
|
75
|
+
def delete_block (offset, bsize)
|
76
|
+
if offset.size != ndim or
|
77
|
+
bsize.size != ndim
|
78
|
+
raise "ndim mismatch"
|
79
|
+
end
|
80
|
+
newdim = dim
|
81
|
+
grids = []
|
82
|
+
ndim.times do |i|
|
83
|
+
if offset[i] < 0
|
84
|
+
offset[i] += dim[i]
|
85
|
+
end
|
86
|
+
if bsize[i] >= 0
|
87
|
+
if offset[i] < 0 or offset[i] >= dim[i]
|
88
|
+
raise "invalid offset or size"
|
89
|
+
end
|
90
|
+
newdim[i] -= bsize[i]
|
91
|
+
else
|
92
|
+
if offset[i] + bsize[i] + 1 < 0 or offset[i] + bsize[i] > dim[i]
|
93
|
+
raise "invalid offset or size"
|
94
|
+
end
|
95
|
+
newdim[i] += bsize[i]
|
96
|
+
end
|
97
|
+
grids[i] = CArray.int32(newdim[i])
|
98
|
+
if bsize[i] >= 0
|
99
|
+
if offset[i] > 0
|
100
|
+
grids[i][0...offset[i]].seq!
|
101
|
+
end
|
102
|
+
if offset[i] + bsize[i] < dim[i]
|
103
|
+
grids[i][offset[i]..-1].seq!(offset[i]+bsize[i])
|
104
|
+
end
|
105
|
+
else
|
106
|
+
if offset[i]+bsize[i] > 0
|
107
|
+
grids[i][0..offset[i]+bsize[i]].seq!
|
108
|
+
end
|
109
|
+
if offset[i]+bsize[i]+1 < dim[i]-1
|
110
|
+
grids[i][offset[i]+bsize[i]+1..-1].seq!(offset[i]+1)
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
return self[*grids].to_ca
|
115
|
+
end
|
116
|
+
|
117
|
+
def self.combine (data_type, tdim, list, at = 0, bytes: nil)
|
118
|
+
if CArray.data_class?(data_type)
|
119
|
+
data_class = data_type
|
120
|
+
data_type = :fixlen
|
121
|
+
bytes = data_class::DATA_SIZE
|
122
|
+
else
|
123
|
+
data_class = nil
|
124
|
+
end
|
125
|
+
has_fill_value = false
|
126
|
+
if block_given?
|
127
|
+
fill_value = yield
|
128
|
+
has_fill_value = true
|
129
|
+
end
|
130
|
+
if not tdim.is_a?(Array) or tdim.size == 0
|
131
|
+
raise "invalid binding dimension"
|
132
|
+
end
|
133
|
+
if not list.is_a?(Array) or list.size == 0
|
134
|
+
raise "invalid list"
|
135
|
+
end
|
136
|
+
list = list.map{|x| CArray.wrap_readonly(x, data_type) }
|
137
|
+
ref = list.detect{|x| x.is_a?(CArray) or not x.scalar? }
|
138
|
+
unless ref
|
139
|
+
raise "at least one element in list should be a carray"
|
140
|
+
end
|
141
|
+
dim = ref.dim
|
142
|
+
ndim = ref.ndim
|
143
|
+
tndim = tdim.size
|
144
|
+
if at < 0
|
145
|
+
at += ndim - tndim + 1
|
146
|
+
end
|
147
|
+
unless at.between?(0, ndim - tndim)
|
148
|
+
raise "concatnating position out of range"
|
149
|
+
end
|
150
|
+
list.map! do |x|
|
151
|
+
if x.scalar?
|
152
|
+
rdim = dim.clone
|
153
|
+
rdim[at] = :%
|
154
|
+
x = x[*rdim] # convert CScalar to CARepeat
|
155
|
+
end
|
156
|
+
x
|
157
|
+
end
|
158
|
+
block = CArray.object(*tdim){ list }
|
159
|
+
edim = tdim.clone
|
160
|
+
idx = Array.new(tdim)
|
161
|
+
offset = Array.new(tdim.size) { [] }
|
162
|
+
tdim.each_with_index do |td, i|
|
163
|
+
edim[i] = 0
|
164
|
+
idx.map!{0}
|
165
|
+
idx[i] = nil
|
166
|
+
block[*idx].each do |e|
|
167
|
+
offset[i] << edim[i]
|
168
|
+
edim[i] += e.dim[at+i] # extended dimension size
|
169
|
+
end
|
170
|
+
end
|
171
|
+
newdim = dim.clone
|
172
|
+
newdim[at,tndim] = edim # extended dimension size
|
173
|
+
if has_fill_value
|
174
|
+
obj = CArray.new(data_type, newdim) { fill_value }
|
175
|
+
else
|
176
|
+
obj = CArray.new(data_type, newdim)
|
177
|
+
end
|
178
|
+
out.data_class = data_class if data_class
|
179
|
+
idx = newdim.map{0}
|
180
|
+
block.each_with_index do |item, tidx|
|
181
|
+
(at...at+tndim).each_with_index do |d,i|
|
182
|
+
idx[d] = offset[i][tidx[i]]
|
183
|
+
end
|
184
|
+
obj.paste(idx, item)
|
185
|
+
end
|
186
|
+
obj
|
187
|
+
end
|
188
|
+
|
189
|
+
def self.bind (data_type, list, at = 0, bytes: nil)
|
190
|
+
return CArray.combine(data_type, [list.size], list, at, bytes: bytes)
|
191
|
+
end
|
192
|
+
|
193
|
+
def self.composite (data_type, tdim, list, at = 0, bytes: nil)
|
194
|
+
if CArray.data_class?(data_type)
|
195
|
+
data_class = data_type
|
196
|
+
data_type = :fixlen
|
197
|
+
bytes = data_class::DATA_SIZE
|
198
|
+
else
|
199
|
+
data_class = nil
|
200
|
+
end
|
201
|
+
if not tdim.is_a?(Array) or tdim.size == 0
|
202
|
+
raise "invalid tiling dimension"
|
203
|
+
end
|
204
|
+
if not list.is_a?(Array) or list.size == 0
|
205
|
+
raise "invalid carray list"
|
206
|
+
end
|
207
|
+
list = list.map{|x| CArray.wrap_readonly(x, data_type) }
|
208
|
+
ref = list.detect{|x| x.is_a?(CArray) or not x.scalar? }
|
209
|
+
unless ref
|
210
|
+
raise "at least one element in list should be a carray"
|
211
|
+
end
|
212
|
+
dim = ref.dim
|
213
|
+
ndim = ref.ndim
|
214
|
+
if at < 0
|
215
|
+
at += ndim + 1 # "+ 1" is needed here
|
216
|
+
end
|
217
|
+
unless at.between?(0,ndim)
|
218
|
+
raise "tiling position is out of range"
|
219
|
+
end
|
220
|
+
tndim = tdim.size
|
221
|
+
list.map! do |x|
|
222
|
+
if x.scalar?
|
223
|
+
rdim = dim.clone
|
224
|
+
rdim[at] = :%
|
225
|
+
x = x[*rdim] # convert CScalar to CARepeat
|
226
|
+
end
|
227
|
+
x
|
228
|
+
end
|
229
|
+
newdim = dim.clone
|
230
|
+
newdim[at,0] = tdim
|
231
|
+
obj = CArray.new(data_type, newdim, bytes: bytes)
|
232
|
+
out.data_class = data_class if data_class
|
233
|
+
idx = Array.new(ndim+tndim) { nil }
|
234
|
+
CArray.each_index(*tdim) do |*tidx|
|
235
|
+
idx[at,tndim] = tidx
|
236
|
+
obj[*idx] = list.shift
|
237
|
+
end
|
238
|
+
obj
|
239
|
+
end
|
240
|
+
|
241
|
+
def self.merge (data_type, list, at = -1, bytes: nil)
|
242
|
+
return CArray.composite(data_type, [list.size], list, at, bytes: bytes)
|
243
|
+
end
|
244
|
+
|
245
|
+
def self.join (*argv)
|
246
|
+
# get options
|
247
|
+
case argv.first
|
248
|
+
when Class
|
249
|
+
if CArray.data_class?(argv.first)
|
250
|
+
data_class = argv.shift
|
251
|
+
data_type = "fixlen"
|
252
|
+
bytes = data_class::DATA_SIZE
|
253
|
+
else
|
254
|
+
raise "#{argv.first} can not to be a data_class for CArray"
|
255
|
+
end
|
256
|
+
when Integer, Symbol, String
|
257
|
+
type, = *CArray.guess_type_and_bytes(argv.shift, 0)
|
258
|
+
else
|
259
|
+
type = argv.flatten.first.data_type
|
260
|
+
end
|
261
|
+
# process
|
262
|
+
conc = argv.map do |list|
|
263
|
+
case list
|
264
|
+
when CArray
|
265
|
+
if list.ndim == 1
|
266
|
+
list[:%,1]
|
267
|
+
else
|
268
|
+
list
|
269
|
+
end
|
270
|
+
when Array
|
271
|
+
x0 = list.first
|
272
|
+
if list.size == 1 and
|
273
|
+
x0.is_a?(CArray) and
|
274
|
+
x0.ndim == 1
|
275
|
+
list = [x0[:%,1]]
|
276
|
+
else
|
277
|
+
list = list.map { |x|
|
278
|
+
case x
|
279
|
+
when CArray
|
280
|
+
if x.ndim == 1
|
281
|
+
x[:%,1]
|
282
|
+
else
|
283
|
+
x
|
284
|
+
end
|
285
|
+
when Array
|
286
|
+
y = x.first
|
287
|
+
if x.size == 1 and
|
288
|
+
y.is_a?(CArray) and
|
289
|
+
y.ndim == 1
|
290
|
+
y[1,:%]
|
291
|
+
else
|
292
|
+
CArray.join(*x)
|
293
|
+
end
|
294
|
+
else
|
295
|
+
x
|
296
|
+
end
|
297
|
+
}
|
298
|
+
end
|
299
|
+
if block_given?
|
300
|
+
CArray.bind(type, list, 1, &block)
|
301
|
+
else
|
302
|
+
CArray.bind(type, list, 1)
|
303
|
+
end
|
304
|
+
else
|
305
|
+
list
|
306
|
+
end
|
307
|
+
end
|
308
|
+
if conc.size > 1
|
309
|
+
return CArray.bind(type, conc)
|
310
|
+
else
|
311
|
+
return conc.first
|
312
|
+
end
|
313
|
+
end
|
314
|
+
|
315
|
+
end
|