carray 3.0.6 → 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 -1830
- data/README.md +62 -35
- 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 +12 -15
- data/ext/ca_bincmp_dispatch.h +16 -38
- data/ext/ca_binop_dispatch.c +35 -0
- data/ext/ca_binop_dispatch.h +22 -46
- 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 -1504
- 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 +92 -165
- data/ext/ca_obj_bincmp.c +65 -48
- data/ext/ca_obj_binop.c +74 -49
- data/ext/ca_obj_bitarray.c +7 -21
- data/ext/ca_obj_bitfield.c +93 -26
- data/ext/ca_obj_block.c +11 -12
- data/ext/ca_obj_byte_swap.c +34 -34
- data/ext/ca_obj_const_string.c +464 -304
- data/ext/ca_obj_face.c +167 -121
- data/ext/ca_obj_face.h +32 -170
- data/ext/ca_obj_fake.c +127 -34
- data/ext/ca_obj_farray.c +13 -9
- data/ext/ca_obj_field.c +17 -13
- data/ext/ca_obj_fixlen_string.c +57 -34
- data/ext/ca_obj_grid.c +149 -113
- data/ext/ca_obj_meld.c +28 -26
- data/ext/ca_obj_moncmp.c +10 -21
- data/ext/ca_obj_monop.c +88 -73
- data/ext/ca_obj_object.c +60 -46
- data/ext/ca_obj_record.c +74 -22
- data/ext/ca_obj_reduce.c +10 -14
- data/ext/ca_obj_refer.c +146 -39
- data/ext/ca_obj_remap.c +11 -29
- data/ext/ca_obj_repeat.c +41 -23
- data/ext/ca_obj_roll.c +20 -28
- data/ext/ca_obj_select.c +17 -27
- data/ext/ca_obj_select_axis.c +35 -42
- data/ext/ca_obj_shift.c +22 -42
- data/ext/ca_obj_source.c +1 -5
- data/ext/ca_obj_stack.c +33 -29
- data/ext/ca_obj_stride.c +99 -62
- data/ext/ca_obj_string.c +8 -20
- data/ext/ca_obj_tile.c +14 -16
- 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 +48 -49
- data/ext/ca_obj_window.c +87 -99
- data/ext/ca_op_cmplx64.h +4 -0
- data/ext/ca_op_ipower.c +97 -27
- data/ext/ca_op_powi.h +9 -28
- 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 +23 -26
- data/ext/ca_sweep_engine.c +92 -26
- data/ext/ca_sweep_engine.h +22 -15
- data/ext/ca_transform_common.c +3 -8
- data/ext/ca_triop_dispatch.h +20 -32
- data/ext/caframe_csv_reader.c +12 -12
- data/ext/carray.h +300 -1009
- data/ext/carray_access.c +320 -207
- data/ext/carray_address_basis.c +128 -59
- data/ext/carray_attribute.c +81 -104
- data/ext/carray_bincount.c +12 -14
- data/ext/carray_broadcast.c +31 -27
- data/ext/carray_call_cfunc.c +174 -285
- data/ext/carray_call_cfunc.h +36 -28
- data/ext/carray_cast.c +1191 -351
- data/ext/carray_cast_func.rb +68 -8
- data/ext/carray_class.c +6 -11
- data/ext/carray_conversion.c +98 -39
- data/ext/carray_copy.c +52 -26
- data/ext/carray_core.c +286 -132
- data/ext/carray_count.c +53 -22
- data/ext/carray_element.c +49 -28
- data/ext/carray_factorize.c +64 -63
- data/ext/carray_generate.c +26 -19
- data/ext/carray_histogram.c +9 -11
- data/ext/carray_hold.c +7 -9
- data/ext/carray_index_classifier.c +127 -230
- data/ext/carray_index_classifier.h +11 -10
- data/ext/carray_integer_total.c +523 -0
- data/ext/carray_internal.h +1364 -156
- data/ext/carray_key_windows.c +3 -3
- data/ext/carray_lazy.c +37 -37
- data/ext/carray_loop.c +27 -23
- data/ext/carray_mask.c +235 -150
- data/ext/carray_math_kernel.h +23 -47
- data/ext/carray_mathfunc.c +10 -3
- data/ext/carray_median_percentile.c +106 -100
- data/ext/carray_memory_view.c +83 -69
- data/ext/carray_operator.c +490 -128
- data/ext/carray_order.c +163 -84
- data/ext/carray_partition.c +14 -16
- data/ext/carray_random.c +48 -42
- data/ext/carray_round.c +33 -5
- data/ext/carray_scatter.c +57 -39
- data/ext/carray_segment.c +42 -46
- data/ext/carray_slab.c +77 -51
- data/ext/carray_slab.h +44 -33
- data/ext/carray_sort.c +37 -31
- data/ext/carray_sort_kernel.c +36 -10
- data/ext/carray_struct.c +44 -34
- data/ext/carray_test.c +131 -81
- data/ext/carray_time_text.c +30 -31
- data/ext/carray_undef.c +50 -7
- data/ext/carray_utils.c +373 -139
- data/ext/extconf.rb +24 -17
- data/ext/mk_call_cfunc.rb +25 -31
- data/ext/mk_pow5_table.rb +2 -1
- data/ext/mkkernel.rb +819 -488
- 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 +272 -185
- data/lib/carray/basics.rb +298 -56
- data/lib/carray/bincount_nd.rb +41 -18
- data/lib/carray/block_iterator.rb +167 -119
- data/lib/carray/boolean_reduce.rb +7 -4
- data/lib/carray/categorical.rb +84 -59
- data/lib/carray/categorical_iterator.rb +90 -67
- data/lib/carray/complex.rb +61 -41
- data/lib/carray/conditional.rb +98 -23
- data/lib/carray/const_string.rb +112 -46
- data/lib/carray/core_extensions.rb +20 -69
- data/lib/carray/distance_limit.rb +7 -5
- data/lib/carray/factories.rb +269 -0
- data/lib/carray/fixlen_string.rb +95 -12
- data/lib/carray/frame/column_verbs.rb +76 -9
- data/lib/carray/frame/concat.rb +51 -7
- data/lib/carray/frame/convert.rb +2 -1
- data/lib/carray/frame/csv_parser.rb +20 -5
- data/lib/carray/frame/frame.rb +73 -31
- data/lib/carray/frame/group.rb +247 -187
- data/lib/carray/frame/io.rb +36 -12
- data/lib/carray/frame/join.rb +56 -17
- data/lib/carray/frame/records.rb +7 -5
- data/lib/carray/frame/reshape.rb +27 -7
- data/lib/carray/frame/sort.rb +10 -8
- data/lib/carray/frame/verbs.rb +217 -134
- data/lib/carray/frame/windows.rb +254 -19
- data/lib/carray/frame.rb +1 -2
- data/lib/carray/fuse_source.rb +28 -23
- 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 +104 -23
- data/lib/carray/key_windows.rb +93 -61
- data/lib/carray/lazy.rb +208 -111
- 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 +8 -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 +73 -72
- data/lib/carray/methods/{index.rb → coordinate.rb} +13 -13
- data/lib/carray/methods/discovery_along.rb +3 -3
- data/lib/carray/methods/factorize.rb +2 -0
- data/lib/carray/methods/gather_nd.rb +106 -103
- 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 +183 -143
- data/lib/carray/methods/join.rb +2 -1
- data/lib/carray/methods/lag.rb +6 -2
- data/lib/carray/methods/locate_addr.rb +157 -94
- 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 +23 -13
- 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 +50 -20
- data/lib/carray/rng.rb +1 -0
- data/lib/carray/runtime.rb +195 -17
- data/lib/carray/segment_iterator.rb +262 -137
- data/lib/carray/serialize.rb +635 -566
- data/lib/carray/slab_iterator.rb +114 -114
- data/lib/carray/stack.rb +14 -15
- data/lib/carray/string.rb +16 -22
- data/lib/carray/string_operation_extension.rb +97 -52
- data/lib/carray/struct.rb +122 -164
- data/lib/carray/struct_builder.rb +247 -73
- data/lib/carray/time.rb +663 -365
- data/lib/carray/window_iterator.rb +232 -155
- 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 +6 -2
- 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 +10 -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 +28 -16
- data/yard-stubs/carray_access.rb +55 -6
- data/yard-stubs/carray_attribute.rb +26 -38
- data/yard-stubs/carray_cast.rb +121 -162
- 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 +10 -5
- 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 +162 -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 +72 -33
- 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 +12 -3
- data/yard-stubs/carray_slab.rb +7 -4
- data/yard-stubs/carray_sort.rb +7 -6
- data/yard-stubs/carray_test.rb +18 -5
- data/yard-stubs/carray_undef.rb +44 -5
- data/yard-stubs/ruby_carray.rb +75 -40
- metadata +63 -25
- 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 -25387
- 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 -261
- data/lib/carray/data_type_limits.rb +0 -91
|
@@ -37,7 +37,7 @@ class CASegmentIterator < CAIterator
|
|
|
37
37
|
# Takes a copy of the cells the segments cover, so the reductions answer
|
|
38
38
|
# about the values as they were at construction. Cells before offsets[0]
|
|
39
39
|
# or from offsets[-1] on belong to no segment.
|
|
40
|
-
def initialize
|
|
40
|
+
def initialize(value, offsets: nil, lengths: nil)
|
|
41
41
|
kw = {}
|
|
42
42
|
kw[:offsets] = offsets unless offsets.nil?
|
|
43
43
|
kw[:lengths] = lengths unless lengths.nil?
|
|
@@ -48,7 +48,7 @@ class CASegmentIterator < CAIterator
|
|
|
48
48
|
"segments: the segments span #{origin}...#{finish}, " \
|
|
49
49
|
"outside the #{value.elements} elements of the value"
|
|
50
50
|
end
|
|
51
|
-
@
|
|
51
|
+
@source = value
|
|
52
52
|
@src_shape = value.shape
|
|
53
53
|
@k = bounds.elements - 1
|
|
54
54
|
@ndim = 1
|
|
@@ -112,7 +112,7 @@ class CASegmentIterator < CAIterator
|
|
|
112
112
|
# returns {#count_not_masked} (present cells); `count(UNDEF)` returns
|
|
113
113
|
# {#count_masked}; `count(v)` counts cells whose value equals `v`.
|
|
114
114
|
# @return [CArray] length-k int64
|
|
115
|
-
def count
|
|
115
|
+
def count(*args, axis: nil)
|
|
116
116
|
if axis
|
|
117
117
|
return count_not_masked(axis: axis) if args.empty?
|
|
118
118
|
raise NotImplementedError,
|
|
@@ -125,8 +125,7 @@ class CASegmentIterator < CAIterator
|
|
|
125
125
|
# CABlock, whose own #count is the block geometry accessor, so dispatch
|
|
126
126
|
# CArray#count explicitly. (Not fused: a value-equality reduceat would have
|
|
127
127
|
# to reproduce core's cross-type / out-of-range equality exactly.)
|
|
128
|
-
|
|
129
|
-
per_segment(CA_INT64) { |s| cnt.bind_call(s, *args) }
|
|
128
|
+
per_segment(CA_INT64) { |s| count_cells(s, *args) }
|
|
130
129
|
end
|
|
131
130
|
|
|
132
131
|
# @overload count_masked
|
|
@@ -144,15 +143,23 @@ class CASegmentIterator < CAIterator
|
|
|
144
143
|
end
|
|
145
144
|
|
|
146
145
|
# @overload sum
|
|
147
|
-
# Returns per-segment sums in the data type `CArray#sum`
|
|
148
|
-
#
|
|
149
|
-
#
|
|
150
|
-
#
|
|
151
|
-
#
|
|
146
|
+
# Returns per-segment sums in the data type `CArray#sum` answers in: int64
|
|
147
|
+
# for a signed integer value (uint64 for an unsigned one), float64 when a
|
|
148
|
+
# sum is out of that range, and float64 for a float value. `accumulate`
|
|
149
|
+
# is the same fold kept in the value's own type. An empty or fully-masked
|
|
150
|
+
# segment sums the empty set, which is the additive identity `0`
|
|
151
|
+
# (unmasked) — the same contract as `CArray#sum` on an empty / all-masked
|
|
152
|
+
# array.
|
|
152
153
|
# @return [CArray]
|
|
153
154
|
def sum(axis: nil)
|
|
154
155
|
return axis_sum(axis) if axis
|
|
155
156
|
m = moments
|
|
157
|
+
if integer_payload?
|
|
158
|
+
return per_segment_total(:sum) unless m
|
|
159
|
+
return integer_total(:sum, m[:sum], min: m[:min], max: m[:max], count: m[:count]) {
|
|
160
|
+
per_segment_total(:sum)
|
|
161
|
+
}
|
|
162
|
+
end
|
|
156
163
|
return per_segment(core_reduce_kind(:sum)) { |s| s.sum } unless m
|
|
157
164
|
m[:sum].copy # the moments sum IS the core fold (empty -> 0.0)
|
|
158
165
|
end
|
|
@@ -216,13 +223,21 @@ class CASegmentIterator < CAIterator
|
|
|
216
223
|
# `:linear` interpolation, matching `CArray#percentile`). Empty segments
|
|
217
224
|
# are MASKED. Order statistics need every value of a segment held together —
|
|
218
225
|
# this is the reduceat that only the eager grouped copy can serve.
|
|
226
|
+
# Several p are taken in every form `CArray#percentile` takes, and
|
|
227
|
+
# answered in the form p was written in (ReductionCountAndFill reads
|
|
228
|
+
# them, one p at a time).
|
|
219
229
|
# @param p [Numeric] percentile in 0..100.
|
|
220
|
-
# @return [CArray]
|
|
221
|
-
def percentile
|
|
230
|
+
# @return [CArray, Array<CArray>]
|
|
231
|
+
def percentile(p, axis: nil)
|
|
222
232
|
return axis_order_stat(axis, :percentile, p) if axis
|
|
223
233
|
unless kernel_reads?
|
|
224
234
|
return per_segment(core_reduce_kind(:percentile, p)) { |s| s.percentile(p) }
|
|
225
235
|
end
|
|
236
|
+
# The kernel takes a Float; read p as CArray#percentile does, rather
|
|
237
|
+
# than let to_f turn nil or a String into 0.
|
|
238
|
+
unless p.is_a?(Numeric) or p.is_a?(CScalar)
|
|
239
|
+
raise ArgumentError, "percentile: p must be Numeric in [0,100] (got #{p.inspect})"
|
|
240
|
+
end
|
|
226
241
|
out = CArray.float64(@k)
|
|
227
242
|
grouped.send(:__reduceat_percentile__, @offsets, p.to_f, out)
|
|
228
243
|
out
|
|
@@ -233,8 +248,13 @@ class CASegmentIterator < CAIterator
|
|
|
233
248
|
# as five length-k float64 CArrays (matching `CArray#quantile`): the
|
|
234
249
|
# percentiles at 0 / 25 / 50 / 75 / 100. Empty / all-masked segments are
|
|
235
250
|
# MASKED. For a single fraction q in 0..1 use `percentile(q * 100)`.
|
|
251
|
+
# @param axis [Integer, nil] as for {#percentile}.
|
|
236
252
|
# @return [Array<CArray>]
|
|
237
|
-
def quantile
|
|
253
|
+
def quantile(*args, axis: nil, **kw)
|
|
254
|
+
CAIterator.send(:refuse_quantile_arguments, args)
|
|
255
|
+
# min_count: / fill_value: are those of percentile, which takes them.
|
|
256
|
+
return percentile([0, 25, 50, 75, 100], axis: axis, **kw) unless kw.empty?
|
|
257
|
+
return CArray.__name_errors_after_method__(:quantile) { [0, 25, 50, 75, 100].map { |p| percentile(p, axis: axis) } } if axis
|
|
238
258
|
unless kernel_reads?
|
|
239
259
|
return CArray.__name_errors_after_method__(:quantile) { [0, 25, 50, 75, 100].map { |p| percentile(p) } }
|
|
240
260
|
end
|
|
@@ -258,7 +278,7 @@ class CASegmentIterator < CAIterator
|
|
|
258
278
|
# The kernel divides the centred sum of squares by count - ddof once, as
|
|
259
279
|
# the core reduction does; scaling the sample variance by (n-1)/n instead
|
|
260
280
|
# rounds twice and lands an ulp away.
|
|
261
|
-
def centred_variance
|
|
281
|
+
def centred_variance(m, ddof)
|
|
262
282
|
cnt = m[:count]
|
|
263
283
|
means = m[:sum] / cnt.float64 # per-segment mean (garbage where the count
|
|
264
284
|
out = CArray.float64(@k) # is too small; the kernel masks those)
|
|
@@ -280,36 +300,62 @@ class CASegmentIterator < CAIterator
|
|
|
280
300
|
end
|
|
281
301
|
|
|
282
302
|
# @overload prod
|
|
283
|
-
# Returns per-segment products
|
|
284
|
-
#
|
|
303
|
+
# Returns per-segment products in the data type `CArray#prod` answers in:
|
|
304
|
+
# int64 for a signed integer value (uint64 for an unsigned one), float64
|
|
305
|
+
# when a product is out of that range, and float64 for a float value. An
|
|
306
|
+
# empty / fully-masked segment is `1` (the multiplicative identity).
|
|
285
307
|
# Single-pass reduceat for numeric values; per-segment fallback otherwise.
|
|
286
308
|
# @return [CArray]
|
|
287
309
|
def prod(axis: nil)
|
|
288
310
|
return axis_prod(axis) if axis
|
|
311
|
+
if integer_payload?
|
|
312
|
+
return per_segment_total(:prod) unless kernel_reads?
|
|
313
|
+
out = CArray.float64(@k)
|
|
314
|
+
grouped.send(:__reduceat_prod__, @offsets, out)
|
|
315
|
+
return integer_total(:prod, out) { per_segment_total(:prod) }
|
|
316
|
+
end
|
|
289
317
|
return per_segment(core_reduce_kind(:prod)) { |s| s.prod } unless kernel_reads?
|
|
290
318
|
out = CArray.float64(@k)
|
|
291
319
|
grouped.send(:__reduceat_prod__, @offsets, out)
|
|
292
320
|
out
|
|
293
321
|
end
|
|
294
322
|
|
|
295
|
-
# @overload all
|
|
323
|
+
# @overload all(skip_masked: true)
|
|
296
324
|
# Returns the per-segment `all` as boolean (matching `CArray#all`): true
|
|
297
325
|
# iff every present value is truthy (empty segment -> true, vacuously).
|
|
298
|
-
# The value data type must be boolean, as for `CArray#all`.
|
|
326
|
+
# The value data type must be boolean, as for `CArray#all`. With
|
|
327
|
+
# `skip_masked: false` the fold is three-valued, as `CArray#all` folds
|
|
328
|
+
# it: a segment is `UNDEF` when a masked cell could change it.
|
|
329
|
+
# @param skip_masked [Boolean]
|
|
299
330
|
# @return [CArray]
|
|
300
|
-
def all
|
|
301
|
-
|
|
302
|
-
aa ? aa[:all] : per_segment(CA_BOOLEAN) { |s| s.all }
|
|
331
|
+
def all(axis: nil, skip_masked: true)
|
|
332
|
+
fold_all_any(:all, axis, skip_masked)
|
|
303
333
|
end
|
|
304
334
|
|
|
305
|
-
# @overload any
|
|
335
|
+
# @overload any(skip_masked: true)
|
|
306
336
|
# Returns the per-segment `any` as boolean (matching `CArray#any`): true
|
|
307
337
|
# iff some present value is truthy (empty segment -> false). The value
|
|
308
|
-
# data type must be boolean, as for `CArray#any`.
|
|
338
|
+
# data type must be boolean, as for `CArray#any`. With
|
|
339
|
+
# `skip_masked: false` the fold is three-valued, as for {#all}.
|
|
340
|
+
# @param skip_masked [Boolean]
|
|
309
341
|
# @return [CArray]
|
|
310
|
-
def any
|
|
342
|
+
def any(axis: nil, skip_masked: true)
|
|
343
|
+
fold_all_any(:any, axis, skip_masked)
|
|
344
|
+
end
|
|
345
|
+
|
|
346
|
+
private def fold_all_any(op, axis, skip_masked)
|
|
347
|
+
skip_masked = CArray.send(:bool_arg, skip_masked, "skip_masked", op.to_s)
|
|
348
|
+
if axis
|
|
349
|
+
unless skip_masked
|
|
350
|
+
raise NotImplementedError,
|
|
351
|
+
"#{self.class}##{op}(axis:, skip_masked: false) is not implemented " \
|
|
352
|
+
"-- call it without axis:."
|
|
353
|
+
end
|
|
354
|
+
return axis_by_core(axis, op)
|
|
355
|
+
end
|
|
311
356
|
aa = all_any
|
|
312
|
-
aa ? aa[
|
|
357
|
+
r = aa ? aa[op] : per_segment(CA_BOOLEAN) { |s| s.public_send(op) }
|
|
358
|
+
skip_masked ? r : kleene_settle(op, r, count_masked)
|
|
313
359
|
end
|
|
314
360
|
|
|
315
361
|
# @overload first
|
|
@@ -317,7 +363,7 @@ class CASegmentIterator < CAIterator
|
|
|
317
363
|
# type (matching `CArray#first`). A segment with no present value is
|
|
318
364
|
# UNDEF.
|
|
319
365
|
# @return [CArray]
|
|
320
|
-
def first
|
|
366
|
+
def first(axis: nil)
|
|
321
367
|
return axis_by_core(axis, :first) if axis
|
|
322
368
|
edge_values(:first)
|
|
323
369
|
end
|
|
@@ -327,7 +373,7 @@ class CASegmentIterator < CAIterator
|
|
|
327
373
|
# type (matching `CArray#last`). A segment with no present value is
|
|
328
374
|
# UNDEF.
|
|
329
375
|
# @return [CArray]
|
|
330
|
-
def last
|
|
376
|
+
def last(axis: nil)
|
|
331
377
|
return axis_by_core(axis, :last) if axis
|
|
332
378
|
edge_values(:last)
|
|
333
379
|
end
|
|
@@ -342,7 +388,7 @@ class CASegmentIterator < CAIterator
|
|
|
342
388
|
# by one: from an average of about 100 cells (1M cells on an M2 Max).
|
|
343
389
|
NUNIQUE_KERNEL_MAX_LENGTH = 100
|
|
344
390
|
|
|
345
|
-
def nunique
|
|
391
|
+
def nunique(axis: nil)
|
|
346
392
|
return axis_by_core(axis, :nunique) if axis
|
|
347
393
|
if ! CArray.__fast_paths_enabled__(:segment_nunique) ||
|
|
348
394
|
grouped.elements >= NUNIQUE_KERNEL_MAX_LENGTH * @k
|
|
@@ -455,28 +501,41 @@ class CASegmentIterator < CAIterator
|
|
|
455
501
|
# Unlike {#min_index} / {#max_index} (positions within a segment), this
|
|
456
502
|
# indexes back into the original array; only the source-address form is
|
|
457
503
|
# offered, mirroring {#min_addr}.
|
|
504
|
+
# With `masked_position: :first` the masked cells come first instead.
|
|
505
|
+
# @param masked_position [Symbol] `:last` (default) or `:first`.
|
|
458
506
|
# @return [CArray] length-nvalid int64
|
|
459
|
-
def sort_addr
|
|
507
|
+
def sort_addr(masked_position: :last)
|
|
460
508
|
out = CArray.int64(grouped.elements)
|
|
461
509
|
@k.times do |c|
|
|
462
510
|
lo, hi = @bounds[c], @bounds[c + 1]
|
|
463
511
|
next unless hi > lo
|
|
464
512
|
# View-local sort order of the segment (0..size-1), lifted to grouped
|
|
465
513
|
# slots, then mapped back to source addresses via perm.
|
|
466
|
-
out[lo...hi] = perm[grouped[lo...hi].sort_addr + lo]
|
|
514
|
+
out[lo...hi] = perm[grouped[lo...hi].sort_addr(masked_position: masked_position) + lo]
|
|
467
515
|
end
|
|
468
516
|
out
|
|
469
517
|
end
|
|
470
518
|
|
|
471
519
|
# @overload wsum(weights)
|
|
472
|
-
# Per-segment weighted sum
|
|
520
|
+
# Per-segment weighted sum, matching `CArray#wsum`: an integer value with
|
|
521
|
+
# integer weights gives an integer total, others float64. `weights` is
|
|
473
522
|
# a per-cell weight CArray in the source order (same elements as the value).
|
|
474
|
-
# Empty / all-masked segment -> 0
|
|
523
|
+
# Empty / all-masked segment -> 0 (the additive identity). A cell is
|
|
475
524
|
# skipped iff its value OR its weight is masked (core's contract).
|
|
476
525
|
# @param weights [CArray]
|
|
477
526
|
# @return [CArray]
|
|
478
|
-
|
|
527
|
+
# @raise [TypeError] when `weights` is a number (one weight on every
|
|
528
|
+
# cell is `w * sum`).
|
|
529
|
+
def wsum(weights, axis: nil)
|
|
530
|
+
CArray.send(:refuse_scalar_weights, "wsum", "the value") if weights.is_a?(Numeric)
|
|
479
531
|
return axis_wsum_wmean(weights, axis, :wsum)[0] if axis
|
|
532
|
+
if weights.is_a?(CArray)
|
|
533
|
+
products, unsigned = @source.send(:integer_weighted_products, weights)
|
|
534
|
+
if products
|
|
535
|
+
total = scatter_weights(products, false).segments(offsets: @bounds).sum
|
|
536
|
+
return CArray.send(:integer_weighted_total, total, unsigned)
|
|
537
|
+
end
|
|
538
|
+
end
|
|
480
539
|
kernel = kernel_reads?
|
|
481
540
|
wg = scatter_weights(weights, kernel)
|
|
482
541
|
return kernel_weighted(wg)[0] if kernel
|
|
@@ -488,7 +547,10 @@ class CASegmentIterator < CAIterator
|
|
|
488
547
|
# segment, and one whose weights sum to zero, is masked.
|
|
489
548
|
# @param weights [CArray]
|
|
490
549
|
# @return [CArray]
|
|
491
|
-
|
|
550
|
+
# @raise [TypeError] when `weights` is a number (one weight on every
|
|
551
|
+
# cell gives {#mean}).
|
|
552
|
+
def wmean(weights, axis: nil)
|
|
553
|
+
CArray.send(:refuse_scalar_weights, "wmean", "the value") if weights.is_a?(Numeric)
|
|
492
554
|
return axis_wsum_wmean(weights, axis, :wmean)[1] if axis
|
|
493
555
|
kernel = kernel_reads?
|
|
494
556
|
wg = scatter_weights(weights, kernel)
|
|
@@ -503,11 +565,15 @@ class CASegmentIterator < CAIterator
|
|
|
503
565
|
# @yieldparam members [CArray]
|
|
504
566
|
# @return [CArray] length-k
|
|
505
567
|
# @overload reduce(init) { |acc, elem| ... }
|
|
506
|
-
# Per-segment fold: each segment's members are folded element
|
|
507
|
-
# element
|
|
568
|
+
# Per-segment fold: each segment's present members are folded element
|
|
569
|
+
# by element from a copy of `init` (`init.dup`). A masked member is not
|
|
570
|
+
# passed; a segment with none present answers `init`.
|
|
508
571
|
# @param init [Object] initial accumulator.
|
|
509
572
|
# @return [CArray] length-k
|
|
510
|
-
def reduce
|
|
573
|
+
def reduce(*args, data_type: nil, &blk)
|
|
574
|
+
if args.size > 1
|
|
575
|
+
raise ArgumentError, "reduce: wrong number of arguments (given #{args.size}, expected 0..1)"
|
|
576
|
+
end
|
|
511
577
|
raise LocalJumpError, "no block given (yield)" unless blk
|
|
512
578
|
dt = data_type || CA_OBJECT
|
|
513
579
|
if args.empty?
|
|
@@ -515,8 +581,8 @@ class CASegmentIterator < CAIterator
|
|
|
515
581
|
else
|
|
516
582
|
init = args[0]
|
|
517
583
|
per_segment(dt) { |s|
|
|
518
|
-
acc = init
|
|
519
|
-
s.each { |e| acc = blk.call(acc, e) }
|
|
584
|
+
acc = init.dup
|
|
585
|
+
s.each { |e| acc = blk.call(acc, e) unless UNDEF.equal?(e) }
|
|
520
586
|
acc
|
|
521
587
|
}
|
|
522
588
|
end
|
|
@@ -540,7 +606,7 @@ class CASegmentIterator < CAIterator
|
|
|
540
606
|
# along it, every other axis kept. A segment iterator has no axis: form.
|
|
541
607
|
# @yieldparam members [CArray]
|
|
542
608
|
# @return [CArray] shaped like the source value
|
|
543
|
-
def map
|
|
609
|
+
def map(data_type: nil, axis: nil, &block)
|
|
544
610
|
raise LocalJumpError, "no block given (yield)" unless block
|
|
545
611
|
return axis_map(axis, data_type, &block) if axis
|
|
546
612
|
# Apply the block per segment, assembled in grouped (segment-contiguous)
|
|
@@ -585,7 +651,7 @@ class CASegmentIterator < CAIterator
|
|
|
585
651
|
CArray.send(:define_named_methods, self,
|
|
586
652
|
[:cumsum, :cumprod, :cummax, :cummin, :cumcount], :segment_scan)
|
|
587
653
|
|
|
588
|
-
private def segment_scan
|
|
654
|
+
private def segment_scan(op, axis: nil)
|
|
589
655
|
axis ? axis_scan(op, axis) : scan(op)
|
|
590
656
|
end
|
|
591
657
|
|
|
@@ -596,12 +662,12 @@ class CASegmentIterator < CAIterator
|
|
|
596
662
|
# a cell in no segment is UNDEF. A negative `n` looks ahead.
|
|
597
663
|
# @param n [Integer]
|
|
598
664
|
# @return [CArray]
|
|
599
|
-
def lag
|
|
665
|
+
def lag(n = 1, axis: nil)
|
|
600
666
|
unless n.is_a?(Integer)
|
|
601
667
|
raise TypeError, "lag: n must be an Integer (got #{n.class})"
|
|
602
668
|
end
|
|
603
669
|
return axis_lag(n, axis) if axis
|
|
604
|
-
out = blank_like(@
|
|
670
|
+
out = blank_like(@source, @src_shape)
|
|
605
671
|
out[] = UNDEF
|
|
606
672
|
m = grouped.elements
|
|
607
673
|
return out if m.zero?
|
|
@@ -616,26 +682,26 @@ class CASegmentIterator < CAIterator
|
|
|
616
682
|
pos = CArray.int64(m).seq! - start
|
|
617
683
|
lagged[n > 0 ? pos.lt(n) : pos.ge(@bounds[seg + 1] - start + n)] = UNDEF
|
|
618
684
|
end
|
|
619
|
-
out.reshape(@
|
|
685
|
+
out.reshape(@source.elements)[perm] = lagged
|
|
620
686
|
out
|
|
621
687
|
end
|
|
622
688
|
|
|
623
689
|
# @overload strip_mask(method:)
|
|
624
690
|
# Returns the values with each masked cell filled from its segment, as
|
|
625
|
-
# `CArray#strip_mask(method:)` fills a 1-D array: `:forward`
|
|
691
|
+
# `CArray#strip_mask(method:)` fills a 1-D array: `:forward`
|
|
626
692
|
# carries the segment's last present value forward, `:backward`
|
|
627
|
-
# (`:
|
|
693
|
+
# (`:backward`) its next one back. A fill never reaches across into another
|
|
628
694
|
# segment, so a masked cell with nothing to carry in its segment stays
|
|
629
695
|
# masked; a cell in no segment is UNDEF. The value is not modified.
|
|
630
|
-
# @param method [Symbol] `:forward
|
|
696
|
+
# @param method [Symbol] `:forward` or `:backward`.
|
|
631
697
|
# @return [CArray] source-shaped
|
|
632
|
-
def strip_mask
|
|
633
|
-
backward = CAIterator.hold_backward
|
|
698
|
+
def strip_mask(method:, axis: nil)
|
|
699
|
+
backward = CAIterator.send(:hold_backward?, method)
|
|
634
700
|
return axis_hold(method, axis) if axis
|
|
635
|
-
if @
|
|
636
|
-
return scatter_segments(->(shape) { blank_like(@
|
|
701
|
+
if @source.face? && ! face_output?
|
|
702
|
+
return scatter_segments(->(shape) { blank_like(@source, shape) }) { |s| s.strip_mask(method: method) }
|
|
637
703
|
end
|
|
638
|
-
out = blank_like(@
|
|
704
|
+
out = blank_like(@source, @src_shape)
|
|
639
705
|
out[] = UNDEF
|
|
640
706
|
m = grouped.elements
|
|
641
707
|
return out if m.zero?
|
|
@@ -646,7 +712,7 @@ class CASegmentIterator < CAIterator
|
|
|
646
712
|
slot[grouped.is_masked] = backward ? m : -1 if grouped.has_mask?
|
|
647
713
|
seg = CArray.segment_index(offsets: @bounds)
|
|
648
714
|
if backward
|
|
649
|
-
source = slot.
|
|
715
|
+
source = slot.flip.cummin.flip
|
|
650
716
|
found = source.lt(@bounds[seg + 1])
|
|
651
717
|
else
|
|
652
718
|
source = slot.cummax
|
|
@@ -654,27 +720,28 @@ class CASegmentIterator < CAIterator
|
|
|
654
720
|
end
|
|
655
721
|
filled = grouped[source.pmin(m - 1).pmax(0)].copy
|
|
656
722
|
filled[found.not] = UNDEF
|
|
657
|
-
out.reshape(@
|
|
723
|
+
out.reshape(@source.elements)[perm] = filled
|
|
658
724
|
out
|
|
659
725
|
end
|
|
660
726
|
|
|
661
|
-
# @overload
|
|
727
|
+
# @overload rank_index(descending: false, method: :ordinal, kind: :quick)
|
|
662
728
|
# Returns each cell's rank among the members of its segment, source-shaped
|
|
663
|
-
# int64, as `CArray#
|
|
729
|
+
# int64, as `CArray#rank_index` ranks a 1-D array: `0` is the smallest, ties
|
|
664
730
|
# are broken by position (`:ordinal`) or share a rank (`:dense`), and
|
|
665
731
|
# `descending:` reverses the order. A masked cell, and a cell in no
|
|
666
732
|
# segment, is UNDEF.
|
|
667
733
|
# @return [CArray]
|
|
668
|
-
def
|
|
669
|
-
|
|
734
|
+
def rank_index(axis: nil, descending: false, method: :ordinal, kind: :quick)
|
|
735
|
+
descending = CArray.send(:bool_arg, descending, "descending", "rank_index")
|
|
736
|
+
return axis_rank_index(axis, descending: descending, method: method, kind: kind) if axis
|
|
670
737
|
out = CArray.int64(*@src_shape)
|
|
671
738
|
out[] = UNDEF
|
|
672
739
|
# The keywords are checked by the core, on no cells, so a segment
|
|
673
|
-
# iterator refuses what CArray#
|
|
674
|
-
CArray.int64(0).
|
|
740
|
+
# iterator refuses what CArray#rank_index refuses.
|
|
741
|
+
CArray.int64(0).rank_index(descending: descending, method: method, kind: kind)
|
|
675
742
|
# The core ranks every value once, so the order and the ties are its own.
|
|
676
743
|
# Ranked in the asked direction, so tied cells keep their order either way.
|
|
677
|
-
ranks = grouped.
|
|
744
|
+
ranks = grouped.rank_index(method: :dense, kind: kind, descending: descending)
|
|
678
745
|
m = grouped.elements
|
|
679
746
|
return out if m.zero?
|
|
680
747
|
seg = CArray.segment_index(offsets: @bounds)
|
|
@@ -696,7 +763,7 @@ class CASegmentIterator < CAIterator
|
|
|
696
763
|
local[sorted] = CArray.int64(m).seq! - first
|
|
697
764
|
end
|
|
698
765
|
local[present.not] = UNDEF
|
|
699
|
-
out.reshape(@
|
|
766
|
+
out.reshape(@source.elements)[perm] = local
|
|
700
767
|
out
|
|
701
768
|
end
|
|
702
769
|
|
|
@@ -725,28 +792,32 @@ class CASegmentIterator < CAIterator
|
|
|
725
792
|
# With no segment at all the kernel has no group to size, so it is given
|
|
726
793
|
# one that no cell belongs to: every cell comes back UNDEF, as it does when
|
|
727
794
|
# there are segments and a cell is in none of them.
|
|
728
|
-
def scan
|
|
795
|
+
def scan(op)
|
|
729
796
|
CArray.__name_errors_after_method__(op) { scan_answered(op) }
|
|
730
797
|
end
|
|
731
798
|
|
|
732
|
-
def scan_answered
|
|
799
|
+
def scan_answered(op)
|
|
733
800
|
# An empty Face has no cell to ask the core about, and nothing to scan.
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
unless kernel_scans?(op, answer)
|
|
737
|
-
return scatter_segments(->(shape) { blank_like(answer, shape) }) { |s| s.public_send(op) }
|
|
801
|
+
if @source.face? && @source.elements.zero?
|
|
802
|
+
return op == :cumcount ? CArray.int64(*@src_shape) : blank_output(@src_shape)
|
|
738
803
|
end
|
|
804
|
+
answer = core_scan_answer(op)
|
|
805
|
+
by_core = -> { scatter_segments(->(shape) { blank_like(answer, shape) }, widen: true) { |s| s.public_send(op) } }
|
|
806
|
+
integer = CArray.__fast_paths_enabled__(:segment_scan_kernel) &&
|
|
807
|
+
CAIterator.send(:integer_running?, op, @source, answer)
|
|
808
|
+
return by_core.call unless integer || kernel_scans?(op, answer)
|
|
739
809
|
bundle = @k > 0 ? [codes, @k, [0]]
|
|
740
|
-
: [CArray.int64(@
|
|
741
|
-
scan_source.reshape(@
|
|
742
|
-
|
|
743
|
-
|
|
810
|
+
: [CArray.int64(@source.elements).fill(-1), 1, [0]]
|
|
811
|
+
out = scan_source.reshape(@source.elements)
|
|
812
|
+
.__scan_by_axis_groups__([0], [bundle], op)
|
|
813
|
+
.reshape(*@src_shape)
|
|
814
|
+
integer ? CAIterator.send(:integer_running, out, answer, &by_core) : out
|
|
744
815
|
end
|
|
745
816
|
|
|
746
817
|
# The core's scan of one cell of the value: the scan answers in its data
|
|
747
818
|
# type and Face. A scan the core refuses raises here, in the core's words.
|
|
748
|
-
def core_scan_answer
|
|
749
|
-
(@core_scan_answer ||= {})[op] ||= CAIterator.core_probe
|
|
819
|
+
def core_scan_answer(op)
|
|
820
|
+
(@core_scan_answer ||= {})[op] ||= CAIterator.send(:core_probe, @source).public_send(op)
|
|
750
821
|
end
|
|
751
822
|
|
|
752
823
|
# Whether the axis-group scan kernel gives the core's answer. It reads
|
|
@@ -755,14 +826,14 @@ class CASegmentIterator < CAIterator
|
|
|
755
826
|
# object stays an object), cummax / cummin in the value's data type and
|
|
756
827
|
# cumcount in int64 -- which is not the core's answer for every data type
|
|
757
828
|
# (the core counts a boolean's running sum in uint64).
|
|
758
|
-
def kernel_scans?
|
|
829
|
+
def kernel_scans?(op, answer)
|
|
759
830
|
return false unless CArray.__fast_paths_enabled__(:segment_scan_kernel)
|
|
760
|
-
return false if @
|
|
831
|
+
return false if @source.face? || answer.face? || @source.complex? || @source.data_type == CA_FIXLEN
|
|
761
832
|
kernel_type =
|
|
762
833
|
case op
|
|
763
834
|
when :cumcount then CA_INT64
|
|
764
|
-
when :cumsum, :cumprod then @
|
|
765
|
-
else @
|
|
835
|
+
when :cumsum, :cumprod then @source.data_type == CA_OBJECT ? CA_OBJECT : CA_FLOAT64
|
|
836
|
+
else @source.data_type
|
|
766
837
|
end
|
|
767
838
|
answer.data_type == kernel_type
|
|
768
839
|
end
|
|
@@ -770,38 +841,44 @@ class CASegmentIterator < CAIterator
|
|
|
770
841
|
# Applies the block to each segment's members in grouped order and scatters
|
|
771
842
|
# the answers back to the source cells; a cell in no segment is UNDEF.
|
|
772
843
|
# +blank+ builds an empty array of a given shape that can hold the answers.
|
|
773
|
-
|
|
844
|
+
# +widen+ is for the scans: an integer running total out of range answers
|
|
845
|
+
# in float64, and the result follows it. A map keeps the type it was given.
|
|
846
|
+
def scatter_segments(blank, widen: false)
|
|
774
847
|
out = blank.call(@src_shape)
|
|
775
848
|
out[] = UNDEF
|
|
776
849
|
return out if grouped.elements.zero?
|
|
777
850
|
transformed = blank.call([grouped.elements])
|
|
778
851
|
@k.times do |c|
|
|
779
852
|
lo, hi = @bounds[c], @bounds[c + 1]
|
|
780
|
-
|
|
853
|
+
next unless hi > lo
|
|
854
|
+
answer = yield(grouped[lo...hi])
|
|
855
|
+
transformed = CAIterator.send(:widen_for_running, transformed, answer) if widen
|
|
856
|
+
transformed[lo...hi] = surface_for(transformed, answer)
|
|
781
857
|
end
|
|
782
|
-
out.
|
|
858
|
+
out = CAIterator.send(:widen_for_running, out, transformed) if widen
|
|
859
|
+
out.reshape(@source.elements)[perm] = transformed
|
|
783
860
|
out
|
|
784
861
|
end
|
|
785
862
|
|
|
786
863
|
# A blank array for #map: of +data_type+ when one is given, otherwise of the
|
|
787
864
|
# same kind as the value.
|
|
788
|
-
def blank_output
|
|
789
|
-
data_type ? CArray.new(data_type, shape) : blank_like(@
|
|
865
|
+
def blank_output(shape, data_type = nil)
|
|
866
|
+
data_type ? CArray.new(data_type, shape) : blank_like(@source, shape)
|
|
790
867
|
end
|
|
791
868
|
|
|
792
869
|
# An answer as +out+ can hold it. A Face goes into an object array as its
|
|
793
870
|
# surface values; stored as it is, the object array would receive its
|
|
794
871
|
# storage bytes.
|
|
795
|
-
def surface_for
|
|
872
|
+
def surface_for(out, answer)
|
|
796
873
|
return answer unless out.data_type == CA_OBJECT && answer.is_a?(CArray) && answer.face?
|
|
797
874
|
answer.to_a
|
|
798
875
|
end
|
|
799
876
|
|
|
800
877
|
# A blank array of +shape+ of the same kind as +model+: its data type, or its
|
|
801
|
-
# Face. A
|
|
802
|
-
# held as the surface objects they are.
|
|
803
|
-
def blank_like
|
|
804
|
-
if model.face? && model.
|
|
878
|
+
# Face. A Face that holds no new value cannot be written cell by cell, so its
|
|
879
|
+
# values are held as the surface objects they are.
|
|
880
|
+
def blank_like(model, shape)
|
|
881
|
+
if model.face? && !model.__send__(:holds_new_values?)
|
|
805
882
|
CArray.new(CA_OBJECT, shape)
|
|
806
883
|
else
|
|
807
884
|
model.__send__(:blank_like, shape)
|
|
@@ -810,7 +887,7 @@ class CASegmentIterator < CAIterator
|
|
|
810
887
|
|
|
811
888
|
# The values as they were when the iterator was built, in source order.
|
|
812
889
|
# Every no-axis reduction works from the contiguous copy taken then; the
|
|
813
|
-
# scans read @
|
|
890
|
+
# scans read @source, so a write through the source between two calls used to
|
|
814
891
|
# be visible to a cumsum and not to a sum, off one iterator.
|
|
815
892
|
#
|
|
816
893
|
# Rebuilt rather than copied a second time: @perm is exactly the classified
|
|
@@ -821,7 +898,7 @@ class CASegmentIterator < CAIterator
|
|
|
821
898
|
def scan_source
|
|
822
899
|
@scan_source ||=
|
|
823
900
|
begin
|
|
824
|
-
snap = @
|
|
901
|
+
snap = @source.template
|
|
825
902
|
snap.reshape(snap.elements)[perm] = grouped
|
|
826
903
|
snap
|
|
827
904
|
end
|
|
@@ -842,7 +919,7 @@ class CASegmentIterator < CAIterator
|
|
|
842
919
|
# first time one of them asks, since the reductions do not need it.
|
|
843
920
|
def codes
|
|
844
921
|
@codes ||= begin
|
|
845
|
-
c = CArray.int64(@
|
|
922
|
+
c = CArray.int64(@source.elements).fill(-1)
|
|
846
923
|
if grouped.elements > 0
|
|
847
924
|
c[@origin...(@origin + grouped.elements)] =
|
|
848
925
|
CArray.segment_index(offsets: @bounds)
|
|
@@ -858,7 +935,9 @@ class CASegmentIterator < CAIterator
|
|
|
858
935
|
# cells, so wg lines up with @grouped.
|
|
859
936
|
# The weighted kernel reads float64 weights; the per-segment fallback hands
|
|
860
937
|
# them to the core as they are, so an object weight stays exact.
|
|
861
|
-
def scatter_weights
|
|
938
|
+
def scatter_weights(weights, as_float64 = true)
|
|
939
|
+
CArray.send(:refuse_ruby_array, "wsum/wmean", "a weight array") if weights.is_a?(Array)
|
|
940
|
+
layout = grouped # raises where only the axis: form answers
|
|
862
941
|
unless weights.elements == codes.elements
|
|
863
942
|
raise ArgumentError,
|
|
864
943
|
"wsum/wmean: weights.elements (#{weights.elements}) != " \
|
|
@@ -868,14 +947,14 @@ class CASegmentIterator < CAIterator
|
|
|
868
947
|
wf = wf.reshape(wf.elements)
|
|
869
948
|
# An empty index into an empty source is out of range, so take the empty
|
|
870
949
|
# layout directly.
|
|
871
|
-
|
|
950
|
+
layout.elements > 0 ? wf[perm].copy : wf[0...0].copy
|
|
872
951
|
end
|
|
873
952
|
|
|
874
953
|
# Map a per-segment index to the flat source address via the
|
|
875
954
|
# permutation (grouped slot -> source index). The min/max sits at grouped slot
|
|
876
955
|
# offsets[c] + local_index[c]; perm carries it back to the source. Empty
|
|
877
956
|
# segments (masked local index) stay masked.
|
|
878
|
-
def segment_addr
|
|
957
|
+
def segment_addr(local_index)
|
|
879
958
|
out = CArray.int64(@k)
|
|
880
959
|
@k.times do |c|
|
|
881
960
|
out[c] = local_index.is_masked[c] ? UNDEF
|
|
@@ -887,7 +966,7 @@ class CASegmentIterator < CAIterator
|
|
|
887
966
|
# Fused per-segment weighted sum + weighted mean (one C pass over the grouped
|
|
888
967
|
# copy, weights in segment order). Returns [wsum, wmean]; wmean is masked where a
|
|
889
968
|
# segment has no present (value AND weight) pair. Numeric value data types only.
|
|
890
|
-
def kernel_weighted
|
|
969
|
+
def kernel_weighted(wg)
|
|
891
970
|
ws = CArray.float64(@k)
|
|
892
971
|
wm = CArray.float64(@k)
|
|
893
972
|
grouped.send(:__reduceat_wsum_wmean__, @offsets, wg, ws, wm)
|
|
@@ -897,7 +976,7 @@ class CASegmentIterator < CAIterator
|
|
|
897
976
|
# Per-segment weighted fallback for the value data types the kernel does not
|
|
898
977
|
# read (object): delegate each segment to CArray#wsum / #wmean, answering in
|
|
899
978
|
# the data type the core answers in. Empty segments take the given identity.
|
|
900
|
-
def fold_weighted
|
|
979
|
+
def fold_weighted(op, wg, empty)
|
|
901
980
|
out = CArray.new(core_reduce_type(op, CA_FLOAT64([[1.0]])), [@k])
|
|
902
981
|
@k.times do |c|
|
|
903
982
|
lo, hi = @bounds[c], @bounds[c + 1]
|
|
@@ -920,9 +999,9 @@ class CASegmentIterator < CAIterator
|
|
|
920
999
|
# and 2000 for :last (1M cells on an M2 Max).
|
|
921
1000
|
EDGE_KERNEL_MAX_LENGTH = { first: 300, last: 2000 }.freeze
|
|
922
1001
|
|
|
923
|
-
def edge_values
|
|
1002
|
+
def edge_values(op)
|
|
924
1003
|
n = grouped.elements
|
|
925
|
-
if n.zero? || (@
|
|
1004
|
+
if n.zero? || (@source.face? && ! face_output?) || ! CArray.__fast_paths_enabled__(:segment_edge_kernel) ||
|
|
926
1005
|
n >= EDGE_KERNEL_MAX_LENGTH.fetch(op) * @k
|
|
927
1006
|
return per_segment(core_reduce_kind(op)) { |s| s.public_send(op) }
|
|
928
1007
|
end
|
|
@@ -932,7 +1011,7 @@ class CASegmentIterator < CAIterator
|
|
|
932
1011
|
end
|
|
933
1012
|
lo, hi = @bounds[0...-1], @bounds[1..-1]
|
|
934
1013
|
if op == :first
|
|
935
|
-
nearest = slot.
|
|
1014
|
+
nearest = slot.flip.cummin.flip
|
|
936
1015
|
at = nearest[lo.pmin(n - 1)]
|
|
937
1016
|
else
|
|
938
1017
|
nearest = slot.cummax
|
|
@@ -949,9 +1028,10 @@ class CASegmentIterator < CAIterator
|
|
|
949
1028
|
# segment (zero-width segment) yields the shared empty array — a zero-length
|
|
950
1029
|
# slice cannot be taken directly, and an empty array carries the same reduction
|
|
951
1030
|
# contract we want (identity for sum, UNDEF for ratios).
|
|
952
|
-
def segment_slice
|
|
1031
|
+
def segment_slice(c)
|
|
1032
|
+
g = grouped # raises for a per-fiber-only iterator
|
|
953
1033
|
lo, hi = @bounds[c], @bounds[c + 1]
|
|
954
|
-
hi > lo ?
|
|
1034
|
+
hi > lo ? g[lo...hi] : @empty
|
|
955
1035
|
end
|
|
956
1036
|
|
|
957
1037
|
# Single-pass reduceat moments (count / sum / min / max per segment), computed
|
|
@@ -969,7 +1049,7 @@ class CASegmentIterator < CAIterator
|
|
|
969
1049
|
# core's answer for each group; a development build run with
|
|
970
1050
|
# CARRAY_GENERAL_PATH set reduces every group by the core instead, which
|
|
971
1051
|
# is how the two are compared.
|
|
972
|
-
def kernel_reads?
|
|
1052
|
+
def kernel_reads?(data_type = grouped.data_type)
|
|
973
1053
|
MONOID_TYPES.include?(data_type) && CArray.__fast_paths_enabled__(:segment_reduce_kernel)
|
|
974
1054
|
end
|
|
975
1055
|
|
|
@@ -1021,11 +1101,11 @@ class CASegmentIterator < CAIterator
|
|
|
1021
1101
|
# type but a boolean widens, `prod` on an object stays an object). A payload
|
|
1022
1102
|
# the core refuses to fold this way raises here, with the core's own error.
|
|
1023
1103
|
# Probes the core with a one-cell array of the value's data type and takes
|
|
1024
|
-
# the answer's. Asks @
|
|
1104
|
+
# the answer's. Asks @source rather than the grouped copy, which has the same
|
|
1025
1105
|
# data type but does not exist on an iterator that answers only the
|
|
1026
1106
|
# per-fiber form -- and `accumulate(axis:)`, the one axis: member that needs
|
|
1027
1107
|
# this, is exactly the case that would have found it missing.
|
|
1028
|
-
def core_reduce_type
|
|
1108
|
+
def core_reduce_type(op, *args)
|
|
1029
1109
|
kind = core_reduce_kind(op, *args)
|
|
1030
1110
|
kind ? kind.data_type : CA_OBJECT
|
|
1031
1111
|
end
|
|
@@ -1033,19 +1113,57 @@ class CASegmentIterator < CAIterator
|
|
|
1033
1113
|
# The core's answer itself for the one-cell probe, or nil for a Face with no
|
|
1034
1114
|
# cell to probe. Its class is the one the per-segment answers come in, which
|
|
1035
1115
|
# for a Face need not be the values' own (a CATime's spread is a CATimedelta).
|
|
1036
|
-
def core_reduce_kind
|
|
1116
|
+
def core_reduce_kind(op, *args)
|
|
1037
1117
|
memo = (@core_reduce_kind ||= {})
|
|
1038
1118
|
return memo[[op, args]] if memo.key?([op, args])
|
|
1039
|
-
probe = CAIterator.core_probe
|
|
1119
|
+
probe = CAIterator.send(:core_probe, @source)
|
|
1040
1120
|
memo[[op, args]] = probe && probe.public_send(op, *args, axis: 1)
|
|
1041
1121
|
end
|
|
1042
1122
|
|
|
1043
1123
|
# Whether an output can be built by lifting one. A Face is filled by writing
|
|
1044
1124
|
# surface values into storage, so this needs a Face that can be written
|
|
1045
|
-
# into;
|
|
1125
|
+
# into; another -- a const string's records index a shared pool, a
|
|
1046
1126
|
# categorical's codes index a vocabulary -- has no blank form to fill.
|
|
1047
1127
|
def face_output?
|
|
1048
|
-
@
|
|
1128
|
+
@source.face? && @source.__send__(:holds_new_values?)
|
|
1129
|
+
end
|
|
1130
|
+
|
|
1131
|
+
# Whether the value is an integer array, whose sum and product the core
|
|
1132
|
+
# answers in int64 / uint64 (float64 past that range).
|
|
1133
|
+
def integer_payload?
|
|
1134
|
+
@source.integer? && ! @source.face?
|
|
1135
|
+
end
|
|
1136
|
+
|
|
1137
|
+
# Below 2**53 every integer is a float64, and a float64 total of integers
|
|
1138
|
+
# whose partial totals stay below it is exact.
|
|
1139
|
+
EXACT_IN_FLOAT64 = 2.0 ** 53
|
|
1140
|
+
|
|
1141
|
+
# The core's sum or product of an integer value, from a fused kernel's
|
|
1142
|
+
# float64 answer +out+. The kernel's answer is exact while every partial
|
|
1143
|
+
# total stays below 2**53. For a sum, the largest magnitude in a piece
|
|
1144
|
+
# times its count bounds the partial totals; for a product of integers, the
|
|
1145
|
+
# partial products only grow in magnitude (until a factor 0 makes them 0),
|
|
1146
|
+
# so the answer bounds them. Within the bound the answer is read back in
|
|
1147
|
+
# the core's integer type; beyond it, the block answers piece by piece
|
|
1148
|
+
# through the core.
|
|
1149
|
+
def integer_total(op, out, min: nil, max: nil, count: nil)
|
|
1150
|
+
exact = if op == :sum
|
|
1151
|
+
(max.float64.abs.pmax(min.float64.abs) * count.float64).lt(EXACT_IN_FLOAT64).all
|
|
1152
|
+
else
|
|
1153
|
+
out.abs.lt(EXACT_IN_FLOAT64).all
|
|
1154
|
+
end
|
|
1155
|
+
exact ? out.to_type(core_reduce_type(op)) : yield
|
|
1156
|
+
end
|
|
1157
|
+
|
|
1158
|
+
# An integer value's sum or product, each segment folded by the core: the
|
|
1159
|
+
# answers are integers, or floats where one is out of range, and the output
|
|
1160
|
+
# is float64 if any of them is.
|
|
1161
|
+
def per_segment_total(op)
|
|
1162
|
+
answers = Array.new(@k) { |c| segment_slice(c).public_send(op) }
|
|
1163
|
+
type = answers.all?(Integer) ? core_reduce_type(op) : CA_FLOAT64
|
|
1164
|
+
out = CArray.new(type, [@k])
|
|
1165
|
+
answers.each_with_index { |v, c| out[c] = v }
|
|
1166
|
+
out
|
|
1049
1167
|
end
|
|
1050
1168
|
|
|
1051
1169
|
# Build a length-k typed output by folding each segment's members with the
|
|
@@ -1053,26 +1171,24 @@ class CASegmentIterator < CAIterator
|
|
|
1053
1171
|
# non-numeric value data type): each segment is delegated to the same CArray
|
|
1054
1172
|
# reduction, so the per-segment result matches `CArray#<reduction>` over that
|
|
1055
1173
|
# segment's members — the mask carries the "insufficient present data" contract
|
|
1056
|
-
#
|
|
1174
|
+
# with no extra code (an all-masked segment reduces like an empty one; identity-bearing
|
|
1057
1175
|
# reductions return their identity, ratios return UNDEF).
|
|
1058
|
-
def per_segment
|
|
1059
|
-
# a data type the values' own Face answers in names that Face
|
|
1060
|
-
kind = @value if ! kind.is_a?(CArray) && @value.face? && kind == @value.data_type
|
|
1176
|
+
def per_segment(kind)
|
|
1061
1177
|
data_type = kind.is_a?(CArray) ? kind.data_type : kind
|
|
1062
1178
|
out = if kind.nil?
|
|
1063
1179
|
CArray.new(CA_OBJECT, [@k])
|
|
1064
1180
|
elsif ! (kind.is_a?(CArray) && kind.face?)
|
|
1065
1181
|
bytes = if kind.is_a?(CArray) then kind.bytes
|
|
1066
|
-
elsif data_type == @
|
|
1182
|
+
elsif data_type == @source.data_type then @source.bytes
|
|
1067
1183
|
else 0 end
|
|
1068
1184
|
CArray.new(data_type, [@k], bytes: bytes)
|
|
1069
|
-
elsif
|
|
1185
|
+
elsif kind.__send__(:holds_new_values?)
|
|
1070
1186
|
# the core answered in a Face, so the output is one too:
|
|
1071
1187
|
# CATime#min hands back a CATime::Element, which only a CATime
|
|
1072
1188
|
# has anywhere to put
|
|
1073
1189
|
kind.__send__(:blank_like, [@k])
|
|
1074
1190
|
else
|
|
1075
|
-
# a
|
|
1191
|
+
# a Face that holds no new value cannot be filled, so its answers are collected
|
|
1076
1192
|
# as the surface objects they already are
|
|
1077
1193
|
CArray.new(CA_OBJECT, [@k])
|
|
1078
1194
|
end
|
|
@@ -1084,67 +1200,67 @@ class CASegmentIterator < CAIterator
|
|
|
1084
1200
|
|
|
1085
1201
|
# ---- no axis: form -------------------------------------------------------
|
|
1086
1202
|
|
|
1087
|
-
def axis_by_core
|
|
1203
|
+
def axis_by_core(*)
|
|
1088
1204
|
raise NotImplementedError, "#{self.class} has no axis: form"
|
|
1089
1205
|
end
|
|
1090
1206
|
|
|
1091
|
-
def axis_by_masked_copy
|
|
1207
|
+
def axis_by_masked_copy(*)
|
|
1092
1208
|
raise NotImplementedError, "#{self.class} has no axis: form"
|
|
1093
1209
|
end
|
|
1094
1210
|
|
|
1095
|
-
def axis_counts
|
|
1211
|
+
def axis_counts(*)
|
|
1096
1212
|
raise NotImplementedError, "#{self.class} has no axis: form"
|
|
1097
1213
|
end
|
|
1098
1214
|
|
|
1099
|
-
def axis_mean
|
|
1215
|
+
def axis_mean(*)
|
|
1100
1216
|
raise NotImplementedError, "#{self.class} has no axis: form"
|
|
1101
1217
|
end
|
|
1102
1218
|
|
|
1103
|
-
def axis_moments
|
|
1219
|
+
def axis_moments(*)
|
|
1104
1220
|
raise NotImplementedError, "#{self.class} has no axis: form"
|
|
1105
1221
|
end
|
|
1106
1222
|
|
|
1107
|
-
def axis_order_stat
|
|
1223
|
+
def axis_order_stat(*)
|
|
1108
1224
|
raise NotImplementedError, "#{self.class} has no axis: form"
|
|
1109
1225
|
end
|
|
1110
1226
|
|
|
1111
|
-
def axis_scan
|
|
1227
|
+
def axis_scan(*)
|
|
1112
1228
|
raise NotImplementedError, "#{self.class} has no axis: form"
|
|
1113
1229
|
end
|
|
1114
1230
|
|
|
1115
|
-
def axis_lag
|
|
1231
|
+
def axis_lag(*)
|
|
1116
1232
|
raise NotImplementedError, "#{self.class} has no axis: form"
|
|
1117
1233
|
end
|
|
1118
1234
|
|
|
1119
|
-
def
|
|
1235
|
+
def axis_rank_index(*)
|
|
1120
1236
|
raise NotImplementedError, "#{self.class} has no axis: form"
|
|
1121
1237
|
end
|
|
1122
1238
|
|
|
1123
|
-
def axis_hold
|
|
1239
|
+
def axis_hold(*)
|
|
1124
1240
|
raise NotImplementedError, "#{self.class} has no axis: form"
|
|
1125
1241
|
end
|
|
1126
1242
|
|
|
1127
|
-
def axis_map
|
|
1243
|
+
def axis_map(*)
|
|
1128
1244
|
raise NotImplementedError, "#{self.class} has no axis: form"
|
|
1129
1245
|
end
|
|
1130
1246
|
|
|
1131
|
-
def axis_prod
|
|
1247
|
+
def axis_prod(*)
|
|
1132
1248
|
raise NotImplementedError, "#{self.class} has no axis: form"
|
|
1133
1249
|
end
|
|
1134
1250
|
|
|
1135
|
-
def axis_sum
|
|
1251
|
+
def axis_sum(*)
|
|
1136
1252
|
raise NotImplementedError, "#{self.class} has no axis: form"
|
|
1137
1253
|
end
|
|
1138
1254
|
|
|
1139
|
-
def axis_wsum_wmean
|
|
1255
|
+
def axis_wsum_wmean(*)
|
|
1140
1256
|
raise NotImplementedError, "#{self.class} has no axis: form"
|
|
1141
1257
|
end
|
|
1142
1258
|
|
|
1143
1259
|
# The present cells per segment a weighted reduction counts: those whose
|
|
1144
1260
|
# value and weight are both present, as CArray#wsum / #wmean count them.
|
|
1145
|
-
def present_pair_count
|
|
1261
|
+
def present_pair_count(weights, axis)
|
|
1146
1262
|
if axis
|
|
1147
|
-
paired = @
|
|
1263
|
+
paired = @source.copy
|
|
1148
1264
|
paired[weights.is_masked] = UNDEF if weights.has_mask?
|
|
1149
1265
|
return paired.group_by_category(@cat).count_not_masked(axis: axis)
|
|
1150
1266
|
end
|
|
@@ -1166,7 +1282,7 @@ class CASegmentIterator < CAIterator
|
|
|
1166
1282
|
count count_not_masked count_masked].freeze
|
|
1167
1283
|
|
|
1168
1284
|
# A count of the mask reads every cell of the segment, as the core's does.
|
|
1169
|
-
def self.mask_count?
|
|
1285
|
+
def self.mask_count?(op, args)
|
|
1170
1286
|
op == :count_not_masked || op == :count_masked ||
|
|
1171
1287
|
(op == :count && (args.empty? || args[0].equal?(UNDEF)))
|
|
1172
1288
|
end
|
|
@@ -1176,7 +1292,16 @@ class CASegmentIterator < CAIterator
|
|
|
1176
1292
|
# The segment reduction already leaves an empty segment as the core does,
|
|
1177
1293
|
# so only a min_count asking for more is applied here. The reduction the
|
|
1178
1294
|
# member is made of is the block (super).
|
|
1179
|
-
private def reduce_counted
|
|
1295
|
+
private def reduce_counted(op, args, kw, &block)
|
|
1296
|
+
if op == :percentile
|
|
1297
|
+
return CAIterator.send(:percentile_in_form, args) { |p|
|
|
1298
|
+
reduce_counted_one(op, [p], kw.dup, &block)
|
|
1299
|
+
}
|
|
1300
|
+
end
|
|
1301
|
+
reduce_counted_one(op, args, kw, &block)
|
|
1302
|
+
end
|
|
1303
|
+
|
|
1304
|
+
private def reduce_counted_one(op, args, kw)
|
|
1180
1305
|
min_count = kw.delete(:min_count)
|
|
1181
1306
|
fill_value = kw.delete(:fill_value)
|
|
1182
1307
|
need = CArray.__present_cells_needed_per_result_cell__(op, min_count, 0)
|
|
@@ -1199,7 +1324,7 @@ class CASegmentIterator < CAIterator
|
|
|
1199
1324
|
if short
|
|
1200
1325
|
(result.is_a?(Array) ? result : [result]).each { |r| r[short] = UNDEF }
|
|
1201
1326
|
end
|
|
1202
|
-
CArray.__fill_undefined_result_cells__(result, fill_value)
|
|
1327
|
+
CArray.__fill_undefined_result_cells__(result, fill_value, op.to_sym)
|
|
1203
1328
|
end
|
|
1204
1329
|
end
|
|
1205
1330
|
prepend ReductionCountAndFill
|
|
@@ -1209,8 +1334,8 @@ class CASegmentIterator < CAIterator
|
|
|
1209
1334
|
# place, so the result shows the source's attributes, as CArray#lag and
|
|
1210
1335
|
# CArray#strip_mask(method:) do.
|
|
1211
1336
|
prepend(Module.new do
|
|
1212
|
-
def lag
|
|
1213
|
-
def strip_mask
|
|
1337
|
+
def lag(*args, **keywords) = @source.__send__(:carry_attrs, super)
|
|
1338
|
+
def strip_mask(**keywords) = @source.__send__(:carry_attrs, super)
|
|
1214
1339
|
end)
|
|
1215
1340
|
end
|
|
1216
1341
|
|
|
@@ -1234,7 +1359,7 @@ class CArray
|
|
|
1234
1359
|
# @raise [ArgumentError] for decreasing or empty offsets, a negative or
|
|
1235
1360
|
# masked length, segments reaching outside `self`, or unless exactly one
|
|
1236
1361
|
# of `offsets:` and `lengths:` is given.
|
|
1237
|
-
def segments
|
|
1362
|
+
def segments(offsets: nil, lengths: nil)
|
|
1238
1363
|
CASegmentIterator.new(self, offsets: offsets, lengths: lengths)
|
|
1239
1364
|
end
|
|
1240
1365
|
end
|