carray 3.0.7 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.yardopts +1 -1
- data/CHANGELOG.md +7 -1940
- data/README.md +38 -32
- data/carray.gemspec +16 -4
- data/docs/changelogs/CHANGELOG-3.0.md +2464 -0
- data/ext/ca_array_pool.c +4 -5
- data/ext/ca_axis_descriptor.h +45 -30
- data/ext/ca_axis_dispatch.c +7 -4
- data/ext/ca_axis_group.c +6 -9
- data/ext/ca_bincmp_dispatch.h +16 -38
- data/ext/ca_binop_dispatch.c +27 -0
- data/ext/ca_binop_dispatch.h +22 -47
- data/ext/ca_categorical_iterator.c +36 -34
- data/ext/ca_compare.c +0 -1
- data/ext/ca_compare.h +8 -1
- data/ext/ca_composite_dispatch.h +6 -0
- data/ext/ca_for_buffer.h +6 -1
- data/ext/ca_group_iter.c +17 -18
- data/ext/ca_iter_state_fields.h +196 -0
- data/ext/ca_iter_substrate.h +84 -103
- data/ext/ca_kernel_iterator.c +125 -89
- data/ext/ca_kernel_iterator.h +229 -1506
- data/ext/ca_kernel_iterator_internal.h +1167 -0
- data/ext/ca_moncmp_dispatch.h +17 -24
- data/ext/ca_monop_dispatch.h +25 -74
- data/ext/ca_obj_array.c +90 -163
- data/ext/ca_obj_bincmp.c +47 -46
- data/ext/ca_obj_binop.c +38 -38
- data/ext/ca_obj_bitarray.c +5 -17
- data/ext/ca_obj_bitfield.c +15 -30
- data/ext/ca_obj_block.c +11 -12
- data/ext/ca_obj_byte_swap.c +32 -30
- data/ext/ca_obj_const_string.c +57 -62
- data/ext/ca_obj_face.c +99 -91
- data/ext/ca_obj_face.h +32 -170
- data/ext/ca_obj_fake.c +15 -21
- data/ext/ca_obj_farray.c +7 -9
- data/ext/ca_obj_field.c +17 -13
- data/ext/ca_obj_fixlen_string.c +8 -19
- data/ext/ca_obj_grid.c +137 -106
- data/ext/ca_obj_meld.c +23 -21
- data/ext/ca_obj_moncmp.c +10 -21
- data/ext/ca_obj_monop.c +24 -35
- data/ext/ca_obj_object.c +51 -37
- data/ext/ca_obj_record.c +74 -22
- data/ext/ca_obj_reduce.c +8 -10
- data/ext/ca_obj_refer.c +42 -29
- data/ext/ca_obj_remap.c +11 -29
- data/ext/ca_obj_repeat.c +41 -23
- data/ext/ca_obj_roll.c +18 -24
- data/ext/ca_obj_select.c +10 -22
- data/ext/ca_obj_select_axis.c +27 -36
- data/ext/ca_obj_shift.c +22 -42
- data/ext/ca_obj_source.c +1 -5
- data/ext/ca_obj_stack.c +28 -24
- data/ext/ca_obj_stride.c +53 -54
- data/ext/ca_obj_string.c +6 -18
- data/ext/ca_obj_tile.c +12 -12
- data/ext/ca_obj_time.c +12 -23
- data/ext/ca_obj_timedelta.c +12 -23
- data/ext/ca_obj_transpose.c +8 -10
- data/ext/ca_obj_triop.c +35 -39
- data/ext/ca_obj_window.c +67 -86
- data/ext/ca_op_cmplx64.h +4 -0
- data/ext/ca_op_ipower.c +34 -12
- data/ext/ca_op_powi.h +4 -0
- data/ext/ca_pow5_table.h +2 -1
- data/ext/ca_rng_normal.h +4 -0
- data/ext/ca_rng_xoshiro256pp.h +3 -0
- data/ext/ca_sort_kernels.h +20 -26
- data/ext/ca_sweep_engine.c +2 -3
- data/ext/ca_sweep_engine.h +17 -9
- data/ext/ca_transform_common.c +3 -8
- data/ext/ca_triop_dispatch.h +20 -32
- data/ext/caframe_csv_reader.c +11 -11
- data/ext/carray.h +300 -1011
- data/ext/carray_access.c +234 -180
- data/ext/carray_address_basis.c +33 -38
- data/ext/carray_attribute.c +81 -104
- data/ext/carray_bincount.c +12 -14
- data/ext/carray_broadcast.c +30 -26
- data/ext/carray_call_cfunc.c +169 -277
- data/ext/carray_call_cfunc.h +36 -28
- data/ext/carray_cast.c +912 -346
- data/ext/carray_cast_func.rb +31 -3
- data/ext/carray_class.c +6 -11
- data/ext/carray_conversion.c +43 -38
- data/ext/carray_copy.c +28 -26
- data/ext/carray_core.c +217 -126
- data/ext/carray_count.c +27 -15
- data/ext/carray_element.c +40 -25
- data/ext/carray_factorize.c +53 -52
- data/ext/carray_generate.c +26 -19
- data/ext/carray_histogram.c +8 -10
- data/ext/carray_hold.c +7 -9
- data/ext/carray_index_classifier.c +123 -228
- data/ext/carray_index_classifier.h +11 -10
- data/ext/carray_integer_total.c +523 -0
- data/ext/carray_internal.h +1274 -160
- data/ext/carray_key_windows.c +3 -3
- data/ext/carray_lazy.c +21 -36
- data/ext/carray_loop.c +19 -22
- data/ext/carray_mask.c +123 -145
- data/ext/carray_math_kernel.h +23 -47
- data/ext/carray_mathfunc.c +10 -3
- data/ext/carray_median_percentile.c +95 -94
- data/ext/carray_memory_view.c +42 -47
- data/ext/carray_operator.c +423 -122
- data/ext/carray_order.c +121 -85
- data/ext/carray_partition.c +13 -15
- data/ext/carray_random.c +47 -41
- data/ext/carray_round.c +9 -4
- data/ext/carray_scatter.c +54 -40
- data/ext/carray_segment.c +41 -45
- data/ext/carray_slab.c +64 -47
- data/ext/carray_slab.h +44 -33
- data/ext/carray_sort.c +32 -29
- data/ext/carray_sort_kernel.c +12 -10
- data/ext/carray_struct.c +38 -43
- data/ext/carray_test.c +47 -43
- data/ext/carray_time_text.c +24 -31
- data/ext/carray_undef.c +50 -7
- data/ext/carray_utils.c +258 -94
- data/ext/extconf.rb +24 -17
- data/ext/mk_call_cfunc.rb +19 -20
- data/ext/mk_pow5_table.rb +2 -1
- data/ext/mkkernel.rb +782 -461
- data/ext/ruby_carray.c +47 -42
- data/ext/version.h +7 -5
- data/lib/carray/attribute.rb +19 -18
- data/lib/carray/autoload_carray.rb +20 -15
- data/lib/carray/autoload_method_extension.rb +64 -57
- data/lib/carray/axis_group.rb +264 -180
- data/lib/carray/basics.rb +272 -48
- data/lib/carray/bincount_nd.rb +33 -16
- data/lib/carray/block_iterator.rb +123 -102
- data/lib/carray/boolean_reduce.rb +7 -4
- data/lib/carray/categorical.rb +63 -51
- data/lib/carray/categorical_iterator.rb +89 -67
- data/lib/carray/complex.rb +61 -41
- data/lib/carray/conditional.rb +49 -22
- data/lib/carray/const_string.rb +54 -62
- data/lib/carray/core_extensions.rb +1 -1
- data/lib/carray/distance_limit.rb +7 -5
- data/lib/carray/factories.rb +269 -0
- data/lib/carray/fixlen_string.rb +25 -24
- data/lib/carray/frame/column_verbs.rb +20 -8
- data/lib/carray/frame/concat.rb +3 -3
- data/lib/carray/frame/convert.rb +2 -1
- data/lib/carray/frame/csv_parser.rb +7 -1
- data/lib/carray/frame/frame.rb +35 -25
- data/lib/carray/frame/group.rb +183 -150
- data/lib/carray/frame/io.rb +26 -7
- data/lib/carray/frame/join.rb +37 -12
- data/lib/carray/frame/records.rb +1 -1
- data/lib/carray/frame/reshape.rb +3 -3
- data/lib/carray/frame/sort.rb +10 -8
- data/lib/carray/frame/verbs.rb +148 -118
- data/lib/carray/frame/windows.rb +232 -21
- data/lib/carray/frame.rb +1 -2
- data/lib/carray/fuse_source.rb +24 -21
- data/lib/carray/fusion.rb +30 -30
- data/lib/carray/histogram.rb +35 -16
- data/lib/carray/inspect.rb +7 -7
- data/lib/carray/iterator.rb +97 -24
- data/lib/carray/key_windows.rb +83 -57
- data/lib/carray/lazy.rb +196 -94
- data/lib/carray/limits.rb +80 -0
- data/lib/carray/mask_gap_fill.rb +21 -16
- data/lib/carray/math.rb +87 -50
- data/lib/carray/meld_reduce.rb +30 -8
- data/lib/carray/methods/align_addr.rb +29 -17
- data/lib/carray/methods/bin.rb +5 -1
- data/lib/carray/methods/bincount.rb +4 -2
- data/lib/carray/methods/bit_string.rb +19 -31
- data/lib/carray/methods/broadcast.rb +8 -3
- data/lib/carray/methods/choose.rb +5 -5
- data/lib/carray/methods/composition.rb +72 -71
- data/lib/carray/methods/{index.rb → coordinate.rb} +13 -13
- data/lib/carray/methods/discovery_along.rb +3 -3
- data/lib/carray/methods/gather_nd.rb +106 -106
- data/lib/carray/methods/insert_block.rb +2 -2
- data/lib/carray/methods/is_between.rb +6 -4
- data/lib/carray/methods/is_in.rb +165 -145
- data/lib/carray/methods/join.rb +2 -1
- data/lib/carray/methods/lag.rb +6 -2
- data/lib/carray/methods/locate_addr.rb +150 -138
- data/lib/carray/methods/mask_duplicates.rb +1 -1
- data/lib/carray/methods/meshgrid.rb +28 -16
- data/lib/carray/methods/mode.rb +67 -67
- data/lib/carray/methods/nunique.rb +9 -7
- data/lib/carray/methods/repeat.rb +12 -12
- data/lib/carray/methods/resize.rb +1 -1
- data/lib/carray/methods/snap.rb +24 -20
- data/lib/carray/methods/string_format.rb +2 -2
- data/lib/carray/methods/unique.rb +4 -2
- data/lib/carray/methods/value_counts.rb +1 -1
- data/lib/carray/mkmf.rb +25 -28
- data/lib/carray/percentile_arguments.rb +82 -0
- data/lib/carray/record_order.rb +18 -17
- data/lib/carray/rng.rb +1 -0
- data/lib/carray/runtime.rb +190 -22
- data/lib/carray/segment_iterator.rb +251 -129
- data/lib/carray/serialize.rb +618 -571
- data/lib/carray/slab_iterator.rb +114 -114
- data/lib/carray/stack.rb +11 -12
- data/lib/carray/string.rb +16 -22
- data/lib/carray/string_operation_extension.rb +76 -52
- data/lib/carray/struct.rb +122 -164
- data/lib/carray/struct_builder.rb +243 -72
- data/lib/carray/time.rb +496 -342
- data/lib/carray/window_iterator.rb +191 -150
- data/lib/carray.rb +6 -11
- data/yard-stubs/ca_obj_array.rb +20 -135
- data/yard-stubs/ca_obj_bitarray.rb +1 -8
- data/yard-stubs/ca_obj_bitfield.rb +4 -3
- data/yard-stubs/ca_obj_byte_swap.rb +21 -7
- data/yard-stubs/ca_obj_fake.rb +3 -1
- data/yard-stubs/ca_obj_farray.rb +1 -1
- data/yard-stubs/ca_obj_field.rb +8 -6
- data/yard-stubs/ca_obj_refer.rb +7 -5
- data/yard-stubs/ca_obj_roll.rb +7 -6
- data/yard-stubs/ca_obj_shift.rb +2 -3
- data/yard-stubs/ca_obj_stride.rb +5 -7
- data/yard-stubs/ca_obj_tile.rb +1 -1
- data/yard-stubs/ca_obj_time.rb +43 -0
- data/yard-stubs/ca_obj_transpose.rb +1 -1
- data/yard-stubs/ca_obj_window.rb +26 -15
- data/yard-stubs/carray_access.rb +51 -4
- data/yard-stubs/carray_attribute.rb +26 -38
- data/yard-stubs/carray_cast.rb +118 -165
- data/yard-stubs/carray_class.rb +3 -4
- data/yard-stubs/carray_conversion.rb +6 -7
- data/yard-stubs/carray_copy.rb +2 -1
- data/yard-stubs/carray_count.rb +5 -3
- data/yard-stubs/carray_element.rb +4 -3
- data/yard-stubs/carray_limits.rb +100 -0
- data/yard-stubs/carray_loop.rb +0 -20
- data/yard-stubs/carray_mask.rb +59 -41
- data/yard-stubs/carray_math.rb +157 -100
- data/yard-stubs/carray_mathfunc.rb +415 -3
- data/yard-stubs/carray_median_percentile.rb +29 -11
- data/yard-stubs/carray_memory_view.rb +2 -2
- data/yard-stubs/carray_order.rb +67 -34
- data/yard-stubs/carray_random.rb +6 -6
- data/yard-stubs/carray_reduce.rb +353 -0
- data/yard-stubs/carray_rng.rb +92 -0
- data/yard-stubs/carray_scatter.rb +10 -2
- data/yard-stubs/carray_slab.rb +7 -4
- data/yard-stubs/carray_sort.rb +7 -6
- data/yard-stubs/carray_undef.rb +44 -5
- data/yard-stubs/ruby_carray.rb +75 -40
- metadata +62 -24
- data/CHANGELOG.v1.md +0 -126
- data/ext/carray_build_flags.h +0 -3
- data/ext/carray_config.h +0 -28
- data/ext/carray_data_type.c +0 -90
- data/ext/carray_kernels_bincmp.c +0 -6457
- data/ext/carray_kernels_binop.c +0 -31534
- data/ext/carray_kernels_init.c +0 -1734
- data/ext/carray_kernels_moncmp.c +0 -2104
- data/ext/carray_kernels_monop.c +0 -25337
- data/ext/carray_kernels_reduce_aggregate.c +0 -27545
- data/ext/carray_kernels_reduce_boolean.c +0 -1924
- data/ext/carray_kernels_reduce_cumulative.c +0 -11646
- data/ext/carray_kernels_reduce_extreme.c +0 -21631
- data/ext/carray_kernels_reduce_variance.c +0 -5981
- data/ext/carray_kernels_scan.c +0 -4302
- data/ext/carray_kernels_search.c +0 -37776
- data/ext/carray_kernels_sort.c +0 -10814
- data/ext/carray_kernels_triop.c +0 -1864
- data/lib/carray/construct.rb +0 -360
- data/lib/carray/data_type_extension.rb +0 -262
- data/lib/carray/data_type_limits.rb +0 -91
data/lib/carray/fusion.rb
CHANGED
|
@@ -117,7 +117,7 @@ class CArray
|
|
|
117
117
|
THRESHOLD = 10_000
|
|
118
118
|
|
|
119
119
|
# Returns the array, or nil where nothing computed it.
|
|
120
|
-
def self.evaluate
|
|
120
|
+
def self.evaluate(view)
|
|
121
121
|
plan = worth_asking(view) or return nil
|
|
122
122
|
out = CArray.__alloc_uninit__(view.data_type, view.shape)
|
|
123
123
|
ask(plan, out) ? out : nil
|
|
@@ -126,7 +126,7 @@ class CArray
|
|
|
126
126
|
# Fills an array the caller already has. Called from the store as well,
|
|
127
127
|
# where making one and copying it over would be most of the work.
|
|
128
128
|
# Returns true when something computed it.
|
|
129
|
-
def self.evaluate_into
|
|
129
|
+
def self.evaluate_into(view, out)
|
|
130
130
|
plan = worth_asking(view) or return false
|
|
131
131
|
ask(plan, out)
|
|
132
132
|
end
|
|
@@ -136,7 +136,7 @@ class CArray
|
|
|
136
136
|
# is the evaluator's doing, so nothing here retires it: an array that
|
|
137
137
|
# raises when it is looked at (a CAObject whose backing is gone) raises
|
|
138
138
|
# again in the walk, where the caller sees it.
|
|
139
|
-
def self.worth_asking
|
|
139
|
+
def self.worth_asking(view)
|
|
140
140
|
return nil unless askable?(view)
|
|
141
141
|
plan = plan(view) or return nil
|
|
142
142
|
plan.nodes.any? { |n| n.is_a?(Op) } ? plan : nil
|
|
@@ -144,7 +144,7 @@ class CArray
|
|
|
144
144
|
nil
|
|
145
145
|
end
|
|
146
146
|
|
|
147
|
-
def self.ask
|
|
147
|
+
def self.ask(plan, out)
|
|
148
148
|
evaluator = CArray.expression_evaluator or return false
|
|
149
149
|
return false if overlaps?(plan, out)
|
|
150
150
|
return false unless ready_mask(plan, out)
|
|
@@ -164,7 +164,7 @@ class CArray
|
|
|
164
164
|
# destination that arrives masked loses its mask to an expression that
|
|
165
165
|
# has none. A destination that cannot hold a mask (a value array) is
|
|
166
166
|
# left to the walk, which says so.
|
|
167
|
-
def self.ready_mask
|
|
167
|
+
def self.ready_mask(plan, out)
|
|
168
168
|
if plan.masked
|
|
169
169
|
out.mask = 0 unless out.has_mask?
|
|
170
170
|
elsif out.has_mask?
|
|
@@ -181,7 +181,7 @@ class CArray
|
|
|
181
181
|
# reads what it has just written. The walk reads every operand before
|
|
182
182
|
# it writes. The destination itself as a leaf is fine as long as it is
|
|
183
183
|
# read at the cell being written, not shifted.
|
|
184
|
-
def self.overlaps?
|
|
184
|
+
def self.overlaps?(plan, out)
|
|
185
185
|
mine = storages(out)
|
|
186
186
|
shifted = plan.nodes.grep(Shifted).map(&:index)
|
|
187
187
|
plan.leaves.each_with_index.any? do |leaf, index|
|
|
@@ -197,7 +197,7 @@ class CArray
|
|
|
197
197
|
# where a view stands over several. Two of them share storage when
|
|
198
198
|
# they are one array, or entities whose memory overlaps (an array and a
|
|
199
199
|
# wrap_memory_view of it).
|
|
200
|
-
def self.storages
|
|
200
|
+
def self.storages(array, found = [])
|
|
201
201
|
root = array.root_array
|
|
202
202
|
if ! root.entity? && root.respond_to?(:parents)
|
|
203
203
|
root.parents.each { |parent| storages(parent, found) }
|
|
@@ -207,26 +207,26 @@ class CArray
|
|
|
207
207
|
found
|
|
208
208
|
end
|
|
209
209
|
|
|
210
|
-
def self.retire
|
|
210
|
+
def self.retire(error)
|
|
211
211
|
CArray.expression_evaluator = nil
|
|
212
212
|
warn "CArray: the registered expression evaluator raised " \
|
|
213
213
|
"(#{error.class}: #{error.message}); expressions will be walked " \
|
|
214
214
|
"from here on"
|
|
215
215
|
end
|
|
216
216
|
|
|
217
|
-
def self.askable?
|
|
217
|
+
def self.askable?(view)
|
|
218
218
|
! CArray.expression_evaluator.nil? && view.elements >= THRESHOLD
|
|
219
219
|
end
|
|
220
220
|
|
|
221
221
|
# Returns a Plan, or nil where the expression holds something a plan
|
|
222
222
|
# cannot describe. Refusing is ordinary: the caller walks instead.
|
|
223
|
-
def self.plan
|
|
223
|
+
def self.plan(view)
|
|
224
224
|
build(view)
|
|
225
225
|
rescue Refused
|
|
226
226
|
nil
|
|
227
227
|
end
|
|
228
228
|
|
|
229
|
-
def self.build
|
|
229
|
+
def self.build(view)
|
|
230
230
|
raise Refused, "not a lazy expression" unless lazy?(view)
|
|
231
231
|
w = Walk.new
|
|
232
232
|
w.visit(view)
|
|
@@ -235,7 +235,7 @@ class CArray
|
|
|
235
235
|
Plan.new(w.nodes, w.leaves, view.data_type, view.shape, masked, w.signature)
|
|
236
236
|
end
|
|
237
237
|
|
|
238
|
-
def self.lazy?
|
|
238
|
+
def self.lazy?(x)
|
|
239
239
|
LAZY_CLASSES.any? { |k| x.is_a?(k) }
|
|
240
240
|
end
|
|
241
241
|
|
|
@@ -252,13 +252,13 @@ class CArray
|
|
|
252
252
|
@signature = +""
|
|
253
253
|
end
|
|
254
254
|
|
|
255
|
-
def visit
|
|
255
|
+
def visit(n)
|
|
256
256
|
@seen[n.object_id] ||= build(n)
|
|
257
257
|
end
|
|
258
258
|
|
|
259
259
|
private
|
|
260
260
|
|
|
261
|
-
def build
|
|
261
|
+
def build(n)
|
|
262
262
|
case n
|
|
263
263
|
when CALazyMarker then visit(n.parent)
|
|
264
264
|
when CAMonOp then unary(n)
|
|
@@ -273,7 +273,7 @@ class CArray
|
|
|
273
273
|
end
|
|
274
274
|
end
|
|
275
275
|
|
|
276
|
-
def unary
|
|
276
|
+
def unary(n)
|
|
277
277
|
return conversion(n) if n.__op_id__ >= CAMonOp::CAST_BASE
|
|
278
278
|
name = spell(MONOP_BY_ID, n.__op_id__, {})
|
|
279
279
|
args = [visit(n.parent)]
|
|
@@ -284,7 +284,7 @@ class CArray
|
|
|
284
284
|
|
|
285
285
|
# A cast node is named by the type it converts to, cast_<data_type>,
|
|
286
286
|
# and its body is keyed by the type it converts from.
|
|
287
|
-
def conversion
|
|
287
|
+
def conversion(n)
|
|
288
288
|
from = n.parent.data_type
|
|
289
289
|
name = :"cast_#{n.data_type}"
|
|
290
290
|
body = CArray.__kernel_body__(:monop, name, from) or
|
|
@@ -294,7 +294,7 @@ class CArray
|
|
|
294
294
|
push Op.new(:monop, name, n.data_type, args, body, :pass, n.__trapping__)
|
|
295
295
|
end
|
|
296
296
|
|
|
297
|
-
def binary
|
|
297
|
+
def binary(n)
|
|
298
298
|
name = spell(BINOP_BY_ID, n.__op_id__, BINOP_NAMES)
|
|
299
299
|
args = [visit(n.parent), visit(n.__binop_right__)]
|
|
300
300
|
# Boolean `&` and `|` are three-valued: a masked cell whose known
|
|
@@ -306,7 +306,7 @@ class CArray
|
|
|
306
306
|
op(:binop, name, n.data_type, args, rule, n.__trapping__)
|
|
307
307
|
end
|
|
308
308
|
|
|
309
|
-
def ternary
|
|
309
|
+
def ternary(n)
|
|
310
310
|
name = spell(TRIOP_BY_ID, n.__op_id__, TRIOP_NAMES)
|
|
311
311
|
args = [visit(n.parent), visit(n.__triop_op2__), visit(n.__triop_op3__)]
|
|
312
312
|
# select is masked where its condition is, or where the branch the
|
|
@@ -317,14 +317,14 @@ class CArray
|
|
|
317
317
|
|
|
318
318
|
# A comparison is masked where its operands are (ca_obj_moncmp.c,
|
|
319
319
|
# ca_obj_bincmp.c): there is no three-valued comparison.
|
|
320
|
-
def unary_comparison
|
|
320
|
+
def unary_comparison(n)
|
|
321
321
|
name = spell(MONCMP_BY_ID, n.__op_id__, {})
|
|
322
322
|
operand = n.parent
|
|
323
323
|
args = [visit(operand)]
|
|
324
324
|
comparison(:moncmp, name, operand.data_type, args, :pass, n.__trapping__)
|
|
325
325
|
end
|
|
326
326
|
|
|
327
|
-
def binary_comparison
|
|
327
|
+
def binary_comparison(n)
|
|
328
328
|
name = spell(BINCMP_BY_ID, n.__op_id__, {})
|
|
329
329
|
right = n.__bincmp_right__
|
|
330
330
|
# Both sides are already of the type compared (CABinCmp casts one
|
|
@@ -339,7 +339,7 @@ class CArray
|
|
|
339
339
|
# `trapping`: the node skips the cells its operands mask rather than
|
|
340
340
|
# computing them -- one that can raise on a cell or calls Ruby for it.
|
|
341
341
|
# Each lazy view answers it from the rule its own kernel follows.
|
|
342
|
-
def comparison
|
|
342
|
+
def comparison(kind, name, compared, args, mask, trapping)
|
|
343
343
|
body = CArray.__kernel_body__(kind, name, compared) or
|
|
344
344
|
raise Refused, "#{kind} #{name} has no body at #{compared}"
|
|
345
345
|
# A body written against its compared type or a tolerance (`feq`)
|
|
@@ -351,14 +351,14 @@ class CArray
|
|
|
351
351
|
push Op.new(kind, name, :boolean, args, body, mask, trapping)
|
|
352
352
|
end
|
|
353
353
|
|
|
354
|
-
def op
|
|
354
|
+
def op(kind, name, type, args, mask, trapping)
|
|
355
355
|
body = CArray.__kernel_body__(kind, name, type) or
|
|
356
356
|
raise Refused, "#{kind} #{name} has no body at #{type}"
|
|
357
357
|
note(kind.to_s[0], name, type, args.join(","))
|
|
358
358
|
push Op.new(kind, name, type, args, body, mask, trapping)
|
|
359
359
|
end
|
|
360
360
|
|
|
361
|
-
def leaf
|
|
361
|
+
def leaf(n)
|
|
362
362
|
index = leaf_index(n)
|
|
363
363
|
note("a", index, n.data_type, n.has_mask? ? 1 : 0)
|
|
364
364
|
push Leaf.new(index, n.data_type, n.has_mask?)
|
|
@@ -367,7 +367,7 @@ class CArray
|
|
|
367
367
|
# A shift of an array is that array read elsewhere, so it is the
|
|
368
368
|
# array that becomes a leaf. A shift of anything else -- an
|
|
369
369
|
# expression, say -- has no array to read and is taken as a whole.
|
|
370
|
-
def shifted
|
|
370
|
+
def shifted(n)
|
|
371
371
|
array = n.parent
|
|
372
372
|
bounds = n.__boundary_rule_of_each_axis__
|
|
373
373
|
if Fusion.lazy?(array) || array.shape != n.shape ||
|
|
@@ -387,7 +387,7 @@ class CArray
|
|
|
387
387
|
|
|
388
388
|
# A leaf the address bases would refuse to lend cannot be read by an
|
|
389
389
|
# evaluator, so the expression is not described.
|
|
390
|
-
def leaf_index
|
|
390
|
+
def leaf_index(array)
|
|
391
391
|
@leaf_index.fetch(array.object_id) do
|
|
392
392
|
if (reason = AddressBasis.classify(array)[:refusal])
|
|
393
393
|
raise Refused, "a leaf: #{reason}"
|
|
@@ -398,25 +398,25 @@ class CArray
|
|
|
398
398
|
end
|
|
399
399
|
|
|
400
400
|
# A masked scalar has no value to write into the expression.
|
|
401
|
-
def constant
|
|
401
|
+
def constant(n)
|
|
402
402
|
raise Refused, "a masked scalar" if n.has_mask? && n.is_masked[0]
|
|
403
403
|
value = n[0]
|
|
404
404
|
note("k", value.inspect, n.data_type)
|
|
405
405
|
push Const.new(value, n.data_type)
|
|
406
406
|
end
|
|
407
407
|
|
|
408
|
-
def as_cell
|
|
408
|
+
def as_cell(value, data_type)
|
|
409
409
|
cell = CScalar.new(data_type)
|
|
410
410
|
cell[0] = value
|
|
411
411
|
cell[0]
|
|
412
412
|
end
|
|
413
413
|
|
|
414
|
-
def push
|
|
414
|
+
def push(node)
|
|
415
415
|
@nodes << node
|
|
416
416
|
@nodes.size - 1
|
|
417
417
|
end
|
|
418
418
|
|
|
419
|
-
def spell
|
|
419
|
+
def spell(table, id, renames)
|
|
420
420
|
ruby = table[id] or raise Refused, "operation id #{id}"
|
|
421
421
|
renames.fetch(ruby, ruby)
|
|
422
422
|
end
|
|
@@ -425,7 +425,7 @@ class CArray
|
|
|
425
425
|
# they are over, so a consumer can keep one compiled kernel for both.
|
|
426
426
|
# The shape includes which node each operation reads and which leaf
|
|
427
427
|
# each read is: `(a - b) - b` and `(a - b) - a` differ only there.
|
|
428
|
-
def note
|
|
428
|
+
def note(*parts)
|
|
429
429
|
@signature << parts.join(":") << ";"
|
|
430
430
|
end
|
|
431
431
|
end
|
data/lib/carray/histogram.rb
CHANGED
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
# closed, right-open). See `include_max:` for the upper-edge variant.
|
|
44
44
|
# * **bin** — half-open interval between two consecutive edges. Bin index
|
|
45
45
|
# k in 0..N-1.
|
|
46
|
-
# * **N**
|
|
46
|
+
# * **N** — number of bins along dimension k (= edges.size - 1).
|
|
47
47
|
# * **midpoints** — bin center points: `(edges[0..-2] + edges[1..-1]) / 2`,
|
|
48
48
|
# shape (N,). Returned per-dim (single CArray for M=1, Array of M for M>=2).
|
|
49
49
|
#
|
|
@@ -144,12 +144,13 @@ class CArray
|
|
|
144
144
|
# (non-sample, non-channel) axes.
|
|
145
145
|
# @param include_max [Boolean, Array<Boolean>] whether values
|
|
146
146
|
# equal to the last edge fold into the last bin; a scalar
|
|
147
|
-
# broadcasts across dimensions.
|
|
147
|
+
# broadcasts across dimensions. Off by default: the edges are bin
|
|
148
|
+
# bounds, each bin half-open.
|
|
148
149
|
# @param weights_data_type [Symbol, nil] `data_type` of the
|
|
149
150
|
# accumulator when weighted; `nil` for a count-only
|
|
150
151
|
# accumulator (int64 counts).
|
|
151
152
|
# @return [Histogram]
|
|
152
|
-
def initialize
|
|
153
|
+
def initialize(edges:, fiber_shape: [], include_max: false, weights_data_type: nil)
|
|
153
154
|
@edges_list = edges.map { |e| CArray.wrap_readonly(e, :float64) }
|
|
154
155
|
raise ArgumentError, "edges must be a non-empty list" if @edges_list.empty?
|
|
155
156
|
@edges_list.each_with_index do |e, k|
|
|
@@ -162,9 +163,9 @@ class CArray
|
|
|
162
163
|
@include_max = case include_max
|
|
163
164
|
when Array
|
|
164
165
|
raise ArgumentError, "include_max length mismatch" unless include_max.size == @m
|
|
165
|
-
include_max.map { |v|
|
|
166
|
+
include_max.map { |v| CArray.send(:bool_arg, v, "include_max", "histogram") }
|
|
166
167
|
else
|
|
167
|
-
[
|
|
168
|
+
[CArray.send(:bool_arg, include_max, "include_max", "histogram")] * @m
|
|
168
169
|
end
|
|
169
170
|
@weighted = !weights_data_type.nil?
|
|
170
171
|
@counts_data_type = @weighted ? weights_data_type : :int64
|
|
@@ -176,7 +177,12 @@ class CArray
|
|
|
176
177
|
end
|
|
177
178
|
private_class_method :new
|
|
178
179
|
|
|
179
|
-
attr_reader :edges_list, :fiber_shape, :include_max, :
|
|
180
|
+
attr_reader :edges_list, :fiber_shape, :include_max, :full_counts
|
|
181
|
+
|
|
182
|
+
# Read by #+ from the other accumulator. The number of bins on each
|
|
183
|
+
# dimension is counts.shape after fiber_shape.
|
|
184
|
+
attr_reader :m
|
|
185
|
+
protected :m
|
|
180
186
|
|
|
181
187
|
# @overload edges
|
|
182
188
|
# Returns the bin edges: a single CArray when the accumulator
|
|
@@ -203,7 +209,7 @@ class CArray
|
|
|
203
209
|
# @param axis [Integer, nil] bin dimension to marginalise.
|
|
204
210
|
# @return [CArray]
|
|
205
211
|
# @raise [ArgumentError] when `axis` is required but omitted.
|
|
206
|
-
def under
|
|
212
|
+
def under(axis: nil)
|
|
207
213
|
raise ArgumentError, "axis: keyword required (M=#{@m})" if axis.nil? && @m > 1
|
|
208
214
|
ax = axis.nil? ? 0 : CArray.normalize_axis(axis, @m, "under")
|
|
209
215
|
outlier_marginal(ax, 0)
|
|
@@ -215,7 +221,7 @@ class CArray
|
|
|
215
221
|
# @param axis [Integer, nil] bin dimension to marginalise.
|
|
216
222
|
# @return [CArray]
|
|
217
223
|
# @raise [ArgumentError] when `axis` is required but omitted.
|
|
218
|
-
def over
|
|
224
|
+
def over(axis: nil)
|
|
219
225
|
raise ArgumentError, "axis: keyword required (M=#{@m})" if axis.nil? && @m > 1
|
|
220
226
|
ax = axis.nil? ? 0 : CArray.normalize_axis(axis, @m, "over")
|
|
221
227
|
outlier_marginal(ax, -1)
|
|
@@ -231,6 +237,17 @@ class CArray
|
|
|
231
237
|
@m == 1 ? arr[0] : arr
|
|
232
238
|
end
|
|
233
239
|
|
|
240
|
+
# @overload inspect
|
|
241
|
+
# Returns the class, the data type and shape of {#counts}, and
|
|
242
|
+
# either {#fiber_shape} or, with no fiber axes, the sample {#total}.
|
|
243
|
+
# The counts themselves are not shown.
|
|
244
|
+
# @return [String]
|
|
245
|
+
def inspect
|
|
246
|
+
c = counts
|
|
247
|
+
tail = @fiber_shape.empty? ? "total=#{total}" : "fiber_shape=#{@fiber_shape}"
|
|
248
|
+
"#<#{self.class} counts=#{c.data_type_name}#{c.shape} #{tail}>"
|
|
249
|
+
end
|
|
250
|
+
|
|
234
251
|
# @overload total
|
|
235
252
|
# Returns the per-fiber sample total, including outliers, with
|
|
236
253
|
# shape `fiber_shape` (or a scalar when `fiber_shape` is
|
|
@@ -263,7 +280,7 @@ class CArray
|
|
|
263
280
|
# @return [self]
|
|
264
281
|
# @raise [ArgumentError] on shape / axis / weighted-state
|
|
265
282
|
# mismatch.
|
|
266
|
-
def add
|
|
283
|
+
def add(chunk, axis: nil, weights: nil)
|
|
267
284
|
chunk = CArray.wrap_readonly(chunk, :float64)
|
|
268
285
|
|
|
269
286
|
# For M=1, accept chunks without the trailing channel axis (= the 1-D
|
|
@@ -321,6 +338,7 @@ class CArray
|
|
|
321
338
|
|
|
322
339
|
if weights
|
|
323
340
|
raise ArgumentError, "weights given but accumulator is unweighted" unless @weighted
|
|
341
|
+
CArray.send(:refuse_ruby_array, "histogram", "a weight array") if weights.is_a?(Array)
|
|
324
342
|
# coerce: the accumulator's type, declared by weights_data_type:
|
|
325
343
|
weights = CArray.wrap_readonly(weights, @counts_data_type)
|
|
326
344
|
expected_w_shape = chunk.shape.dup
|
|
@@ -365,7 +383,7 @@ class CArray
|
|
|
365
383
|
# @param other [Histogram] compatible accumulator.
|
|
366
384
|
# @return [Histogram]
|
|
367
385
|
# @raise [ArgumentError] when the structure does not match.
|
|
368
|
-
def +
|
|
386
|
+
def +(other)
|
|
369
387
|
# --- semantic guards: structure must match exactly ---------------
|
|
370
388
|
raise ArgumentError, "type mismatch" unless other.is_a?(Histogram)
|
|
371
389
|
raise ArgumentError, "M mismatch" unless @m == other.m
|
|
@@ -406,7 +424,7 @@ class CArray
|
|
|
406
424
|
# unlike `sum` which always lifts to float64. Caveat: int64 overflows at
|
|
407
425
|
# ~9.2e18 (silent wrap); weighted float64 loses precision past 2^53 but
|
|
408
426
|
# does not overflow. Realistic histograms do not hit these limits.
|
|
409
|
-
def sum_along_bin_axes
|
|
427
|
+
def sum_along_bin_axes(arr)
|
|
410
428
|
out = arr
|
|
411
429
|
@m.times { out = out.accumulate(axis: out.ndim - 1) }
|
|
412
430
|
out
|
|
@@ -415,7 +433,7 @@ class CArray
|
|
|
415
433
|
# axis k of the bin dims (= which channel's outlier to look at).
|
|
416
434
|
# offset = 0 (under) or -1 (over). Other bin axes are marginalised away
|
|
417
435
|
# (= including their outlier positions).
|
|
418
|
-
def outlier_marginal
|
|
436
|
+
def outlier_marginal(axis, offset)
|
|
419
437
|
base = [nil] * @fiber_shape.size
|
|
420
438
|
bin_idx = (0...@m).map { |k|
|
|
421
439
|
if k == axis
|
|
@@ -444,10 +462,11 @@ class CArray
|
|
|
444
462
|
# @param edges [CArray, Array<Numeric>] 1-D ascending bin edges.
|
|
445
463
|
# @param axis [Integer] sample axis.
|
|
446
464
|
# @param include_max [Boolean] fold last-edge equality into the
|
|
447
|
-
# last bin.
|
|
465
|
+
# last bin. Off by default: each bin is half-open.
|
|
448
466
|
# @param weights [CArray, nil] optional per-sample weights.
|
|
449
467
|
# @return [Histogram] 1-D accumulator (`m == 1`).
|
|
450
|
-
def histogram1d
|
|
468
|
+
def histogram1d(edges:, axis: -1, include_max: false, weights: nil)
|
|
469
|
+
include_max = CArray.send(:bool_arg, include_max, "include_max", "histogram1d")
|
|
451
470
|
ax = normalize_axis(axis.nil? ? -1 : axis, "histogram1d")
|
|
452
471
|
|
|
453
472
|
new_shape = shape + [1]
|
|
@@ -472,7 +491,7 @@ class CArray
|
|
|
472
491
|
# @param weights [CArray, nil] optional per-sample weights.
|
|
473
492
|
# @return [Histogram] 2-D accumulator (`m == 2`).
|
|
474
493
|
# @raise [ArgumentError] when `edges` is not a length-2 Array.
|
|
475
|
-
def histogram2d
|
|
494
|
+
def histogram2d(edges:, axis: [-2, -1], include_max: false, weights: nil)
|
|
476
495
|
raise ArgumentError, "edges must be a list of 2" unless edges.is_a?(Array) && edges.size == 2
|
|
477
496
|
histogram(edges: edges, axis: axis, include_max: include_max, weights: weights)
|
|
478
497
|
end
|
|
@@ -489,7 +508,7 @@ class CArray
|
|
|
489
508
|
# @param weights [CArray, nil] optional per-sample weights.
|
|
490
509
|
# @return [Histogram]
|
|
491
510
|
# @raise [ArgumentError] when `edges` is not an Array.
|
|
492
|
-
def histogram
|
|
511
|
+
def histogram(edges:, axis: [-2, -1], include_max: false, weights: nil)
|
|
493
512
|
raise ArgumentError, "edges must be an Array of edges arrays" unless edges.is_a?(Array)
|
|
494
513
|
arr = self
|
|
495
514
|
axis = [-2, -1] if axis.nil?
|
data/lib/carray/inspect.rb
CHANGED
|
@@ -2,7 +2,7 @@ require "pp" # CArray#source_code uses Array#pretty_inspect
|
|
|
2
2
|
|
|
3
3
|
class CArray::Inspector # :nodoc:
|
|
4
4
|
|
|
5
|
-
def initialize
|
|
5
|
+
def initialize(carray)
|
|
6
6
|
@carray = carray
|
|
7
7
|
end
|
|
8
8
|
|
|
@@ -12,7 +12,7 @@ class CArray::Inspector # :nodoc:
|
|
|
12
12
|
# It is the one difference between #inspect and #inspect_full: the
|
|
13
13
|
# header and the layout are the same, so there is one renderer rather
|
|
14
14
|
# than two that could drift apart.
|
|
15
|
-
def inspect_string
|
|
15
|
+
def inspect_string(abbrev: true)
|
|
16
16
|
if @carray.ndim == 0
|
|
17
17
|
raise "can't inspect CArray of ndim == 0"
|
|
18
18
|
end
|
|
@@ -53,11 +53,11 @@ class CArray::Inspector # :nodoc:
|
|
|
53
53
|
# Face by its name (face_label), any other array by its data type name.
|
|
54
54
|
# Every message and listing that names a type goes through here, so a
|
|
55
55
|
# Face is never named by its storage (:fixlen for a time).
|
|
56
|
-
def self.type_label
|
|
56
|
+
def self.type_label(ca)
|
|
57
57
|
ca.face? ? face_label(ca) : CArray.data_type_name(ca.data_type).to_s
|
|
58
58
|
end
|
|
59
59
|
|
|
60
|
-
def self.face_label
|
|
60
|
+
def self.face_label(ca)
|
|
61
61
|
return ca.send(:inspect_label) if ca.respond_to?(:inspect_label, true)
|
|
62
62
|
klass = ca.class
|
|
63
63
|
klass = klass.superclass while klass.name.nil?
|
|
@@ -130,7 +130,7 @@ class CArray::Inspector # :nodoc:
|
|
|
130
130
|
list << "ro"
|
|
131
131
|
end
|
|
132
132
|
# ---
|
|
133
|
-
if
|
|
133
|
+
if view? and attached?
|
|
134
134
|
list << "attached"
|
|
135
135
|
end
|
|
136
136
|
# ---
|
|
@@ -152,7 +152,7 @@ class CArray::Inspector # :nodoc:
|
|
|
152
152
|
# to do with the storage data_type, so the formatter must follow the decoded
|
|
153
153
|
# value, not the storage. Faces without the hook (CAString) hand back the
|
|
154
154
|
# stored value itself and fall through to the storage formatters below.
|
|
155
|
-
if @carray.face? and @carray.respond_to?(:storage_to_element)
|
|
155
|
+
if @carray.face? and @carray.respond_to?(:storage_to_element, true)
|
|
156
156
|
return lambda { |x| x.inspect }
|
|
157
157
|
end
|
|
158
158
|
data_type = @carray.data_type
|
|
@@ -192,7 +192,7 @@ class CArray::Inspector # :nodoc:
|
|
|
192
192
|
end
|
|
193
193
|
end
|
|
194
194
|
|
|
195
|
-
def get_data_spec
|
|
195
|
+
def get_data_spec(level, idx, formatter, abbrev = true)
|
|
196
196
|
io = +"[ " # mutable buffer; `<<` below appends into it
|
|
197
197
|
ndim = @carray.ndim
|
|
198
198
|
dim = @carray.shape
|
data/lib/carray/iterator.rb
CHANGED
|
@@ -8,11 +8,11 @@
|
|
|
8
8
|
# implement (surface uniformity is the value: "this is an iterator, so it must
|
|
9
9
|
# answer these").
|
|
10
10
|
#
|
|
11
|
-
#
|
|
12
|
-
#
|
|
13
|
-
#
|
|
14
|
-
#
|
|
15
|
-
#
|
|
11
|
+
# A member exposes what it actually holds (source / reference / labels) and
|
|
12
|
+
# adds its own methods (min_index / max_index / map / correlate / convolve /
|
|
13
|
+
# sort_addr / ...); those are NOT part of the common contract because some
|
|
14
|
+
# members legitimately omit them (a window has no map, a group has no
|
|
15
|
+
# within-piece min_index).
|
|
16
16
|
#
|
|
17
17
|
# The 2.0 generic dispatch (calculate / filter / evaluate over a
|
|
18
18
|
# kernel_at_addr slot) was retired in 3.0.
|
|
@@ -27,22 +27,27 @@ class CAIterator
|
|
|
27
27
|
# that a member does not define is a clean NoMethodError, not a wrong answer.
|
|
28
28
|
|
|
29
29
|
attr_reader :ndim, :shape
|
|
30
|
-
|
|
30
|
+
|
|
31
|
+
# @overload source
|
|
32
|
+
# Returns the array the iterator was made from: the array being
|
|
33
|
+
# sliced, tiled, rolled over, grouped or split into segments.
|
|
34
|
+
# @return [CArray]
|
|
35
|
+
attr_reader :source
|
|
31
36
|
|
|
32
37
|
# The keywords a member passes on to a core reduction over its own axes.
|
|
33
38
|
# The member sets `axis:`, so one from the caller is refused; the rest go
|
|
34
39
|
# to the core, which checks them.
|
|
35
|
-
# Whether a fill method carries values backward: :forward
|
|
36
|
-
#
|
|
40
|
+
# Whether a fill method carries values backward: :forward carries them
|
|
41
|
+
# forward, :backward back. A member fills within its own
|
|
37
42
|
# pieces by holding the nearest present value; :linear, which needs a
|
|
38
43
|
# coordinate, is the core's alone.
|
|
39
|
-
def self.hold_backward?
|
|
44
|
+
private_class_method def self.hold_backward?(method) # :nodoc:
|
|
40
45
|
case method
|
|
41
|
-
when :forward
|
|
42
|
-
when :backward
|
|
46
|
+
when :forward then false
|
|
47
|
+
when :backward then true
|
|
43
48
|
else
|
|
44
49
|
raise ArgumentError,
|
|
45
|
-
"strip_mask: method must be :forward
|
|
50
|
+
"strip_mask: method must be :forward or :backward " \
|
|
46
51
|
"within a piece (got #{method.inspect})"
|
|
47
52
|
end
|
|
48
53
|
end
|
|
@@ -58,12 +63,12 @@ class CAIterator
|
|
|
58
63
|
# is asked with the blank cell its resize gives, so a reduction over no
|
|
59
64
|
# groups answers in the same kind as over some; this is nil only when the
|
|
60
65
|
# Face has no blank cell.
|
|
61
|
-
def self.core_probe
|
|
66
|
+
private_class_method def self.core_probe(value) # :nodoc:
|
|
62
67
|
unless value.face?
|
|
63
68
|
return CArray.new(value.data_type, [1, 1], bytes: value.bytes)
|
|
64
69
|
end
|
|
65
70
|
flat = value.reshape(value.elements)
|
|
66
|
-
return flat[
|
|
71
|
+
return flat[0..0].reshape(1, 1) if value.elements > 0
|
|
67
72
|
begin
|
|
68
73
|
flat.resize(1).reshape(1, 1)
|
|
69
74
|
rescue StandardError
|
|
@@ -71,13 +76,44 @@ class CAIterator
|
|
|
71
76
|
end
|
|
72
77
|
end
|
|
73
78
|
|
|
79
|
+
# Below 2**53 every integer is a float64, so a float64 running sum or
|
|
80
|
+
# product of integers is exact while every running value stays below it.
|
|
81
|
+
EXACT_RUNNING_IN_FLOAT64 = 2.0 ** 53
|
|
82
|
+
private_constant :EXACT_RUNNING_IN_FLOAT64
|
|
83
|
+
|
|
84
|
+
# Whether +op+ is a running sum or product of an integer value, which the
|
|
85
|
+
# core answers in int64 / uint64, or float64 where a running value is out
|
|
86
|
+
# of range (+answer+ is the core's scan of one cell).
|
|
87
|
+
private_class_method def self.integer_running?(op, value, answer) # :nodoc:
|
|
88
|
+
(op == :cumsum || op == :cumprod) && value.integer? && !value.face? && answer.integer?
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# A fused scan kernel's float64 running values +out+ of an integer value,
|
|
92
|
+
# in the core's type when every one is below 2**53 (so exact); otherwise
|
|
93
|
+
# the block answers, piece by piece through the core.
|
|
94
|
+
private_class_method def self.integer_running(out, answer) # :nodoc:
|
|
95
|
+
# coerce: the core's type for the answer, which the exact float64 values fit
|
|
96
|
+
out.abs.lt(EXACT_RUNNING_IN_FLOAT64).all ? out.to_type(answer.data_type) : yield
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# +out+, widened to float64 when +answer+ (a piece's running values from
|
|
100
|
+
# the core) is float64 and +out+ is an integer array: one running value out
|
|
101
|
+
# of range makes the whole answer float64, as it does for the core.
|
|
102
|
+
private_class_method def self.widen_for_running(out, answer) # :nodoc:
|
|
103
|
+
if answer.is_a?(CArray) && answer.float? && out.integer?
|
|
104
|
+
out.to_type(CA_FLOAT64)
|
|
105
|
+
else
|
|
106
|
+
out
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
|
|
74
110
|
# A member that keeps what it built from its source's values or mask (a
|
|
75
111
|
# padded copy, the views over it, counts of the cells present) names those
|
|
76
112
|
# instance variables here. They are dropped when a public method of the
|
|
77
113
|
# member is entered from outside, so every call reads the source as it is
|
|
78
114
|
# then; within one call the helpers it runs share them. What follows from
|
|
79
115
|
# the source's shape alone is kept for good.
|
|
80
|
-
def self.read_source_per_call
|
|
116
|
+
def self.read_source_per_call(*state)
|
|
81
117
|
names = public_instance_methods(false)
|
|
82
118
|
prepend(Module.new do
|
|
83
119
|
module_eval(<<~RUBY, __FILE__, __LINE__ + 1)
|
|
@@ -87,7 +123,7 @@ class CAIterator
|
|
|
87
123
|
RUBY
|
|
88
124
|
names.each do |name|
|
|
89
125
|
module_eval(<<~RUBY, __FILE__, __LINE__ + 1)
|
|
90
|
-
def #{name}
|
|
126
|
+
def #{name}(...)
|
|
91
127
|
return super if @reading_source
|
|
92
128
|
forget_source_state
|
|
93
129
|
@reading_source = true
|
|
@@ -103,7 +139,27 @@ class CAIterator
|
|
|
103
139
|
end
|
|
104
140
|
private_class_method :read_source_per_call
|
|
105
141
|
|
|
106
|
-
|
|
142
|
+
# percentile with every form of p CArray#percentile takes: `answer` gives
|
|
143
|
+
# the answer for one p, and the answers come back in the form p was
|
|
144
|
+
# written in, as CArray#percentile returns them.
|
|
145
|
+
private_class_method def self.percentile_in_form(pers) # :nodoc:
|
|
146
|
+
list, form = CArray.send(:percentile_arguments, pers)
|
|
147
|
+
CArray.send(:percentile_answer, list.map { |p| yield(p) }, form)
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
# quantile takes no p, as CArray#quantile takes none.
|
|
151
|
+
private_class_method def self.refuse_quantile_arguments(args, kw = {}) # :nodoc:
|
|
152
|
+
unless kw.empty?
|
|
153
|
+
raise ArgumentError,
|
|
154
|
+
"unknown keyword#{kw.size > 1 ? "s" : ""}: #{kw.keys.map(&:inspect).join(", ")}"
|
|
155
|
+
end
|
|
156
|
+
return if args.empty?
|
|
157
|
+
raise ArgumentError,
|
|
158
|
+
"quantile: takes no positional arguments (got #{args.size}); " \
|
|
159
|
+
"for the value at p percent, use percentile(p)"
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
private_class_method def self.reduce_keywords(op, kw) # :nodoc:
|
|
107
163
|
if kw.key?(:axis)
|
|
108
164
|
raise ArgumentError, "#{op}: unknown keyword: :axis (the iterator sets the axes)"
|
|
109
165
|
end
|
|
@@ -259,7 +315,24 @@ class CAIterator
|
|
|
259
315
|
# CArray#count over a view: CABlock / CAWindow shadow #count with a geometry
|
|
260
316
|
# accessor, so it is dispatched explicitly, while a Face keeps a #count of
|
|
261
317
|
# its own (CAConstString reads its cells as Strings).
|
|
262
|
-
|
|
318
|
+
# The three-valued fold of `all` / `any` (skip_masked: false) from the
|
|
319
|
+
# two-valued answer `r` and the count of masked cells `cm`, piece by
|
|
320
|
+
# piece, as CArray#all / #any compose it: a masked cell leaves a piece
|
|
321
|
+
# UNDEF unless a present cell settles it (a false for all, a true for
|
|
322
|
+
# any).
|
|
323
|
+
private def kleene_settle(kind, r, cm)
|
|
324
|
+
unless r.is_a?(CArray)
|
|
325
|
+
settled = ( kind == :any ) ? r : !r
|
|
326
|
+
return ( settled || cm == 0 ) ? r : UNDEF
|
|
327
|
+
end
|
|
328
|
+
settled = ( kind == :any ) ? r : r.not
|
|
329
|
+
out = r.copy
|
|
330
|
+
unsettled = settled.not & cm.gt(0)
|
|
331
|
+
out[unsettled] = UNDEF if unsettled.any
|
|
332
|
+
out
|
|
333
|
+
end
|
|
334
|
+
|
|
335
|
+
private def count_cells(view, *args, **kw)
|
|
263
336
|
return view.count(*args, **kw) if view.face?
|
|
264
337
|
CArray.instance_method(:count).bind_call(view, *args, **kw)
|
|
265
338
|
end
|
|
@@ -326,19 +399,19 @@ class CAIterator
|
|
|
326
399
|
# cell has no single position within a piece.
|
|
327
400
|
# @!method strip_mask(method:)
|
|
328
401
|
# Returns a source-shaped array with each masked cell filled from its own
|
|
329
|
-
# piece, as `CArray#strip_mask(method:)` fills: `:forward`
|
|
330
|
-
# carries the last present value forward, `:backward`
|
|
402
|
+
# piece, as `CArray#strip_mask(method:)` fills: `:forward`
|
|
403
|
+
# carries the last present value forward, `:backward` the next
|
|
331
404
|
# one back, never across into another piece.
|
|
332
|
-
# @param method [Symbol] `:forward
|
|
405
|
+
# @param method [Symbol] `:forward` or `:backward`.
|
|
333
406
|
# @return [CArray] shaped like the source.
|
|
334
407
|
# @raise [NotImplementedError] for a member whose pieces overlap.
|
|
335
|
-
# @!method
|
|
408
|
+
# @!method rank_index(descending: false, method: :ordinal, kind: :quick)
|
|
336
409
|
# Returns a source-shaped int64 array of each cell's rank among the cells
|
|
337
|
-
# of its piece, as `CArray#
|
|
410
|
+
# of its piece, as `CArray#rank_index` ranks them (`0` the smallest).
|
|
338
411
|
# @return [CArray] shaped like the source.
|
|
339
412
|
# @raise [NotImplementedError] for a member whose pieces overlap, where a
|
|
340
413
|
# cell has no single rank.
|
|
341
|
-
[:map, :sort_addr, :
|
|
414
|
+
[:map, :sort_addr, :rank_index, :strip_mask,
|
|
342
415
|
:cumsum, :cumprod, :cummax, :cummin, :cumcount, :lag].each do |name|
|
|
343
416
|
# define_method: a stub that names itself in its own message
|
|
344
417
|
define_method(name, &Ractor.make_shareable(proc do |*, **, &_blk|
|