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
|
@@ -86,11 +86,11 @@ class CACategoricalIterator < CASegmentIterator
|
|
|
86
86
|
# through perm is the only per-column work. Excluded cells (masked or
|
|
87
87
|
# out-of-vocabulary code) are absent from perm, so they never join a group;
|
|
88
88
|
# the value mask rides the gather into the grouped copy.
|
|
89
|
-
def initialize
|
|
89
|
+
def initialize(value, cat)
|
|
90
90
|
@cat = cat
|
|
91
91
|
@labels = cat.labels
|
|
92
92
|
@k = cat.labels.size
|
|
93
|
-
@
|
|
93
|
+
@source = value # source, kept for #cumsum etc.
|
|
94
94
|
@src_shape = value.shape # output shape for #map
|
|
95
95
|
@ndim = 1 # 1-D iterator over k categories
|
|
96
96
|
@shape = [@k]
|
|
@@ -106,7 +106,7 @@ class CACategoricalIterator < CASegmentIterator
|
|
|
106
106
|
# plan, so the counting sort is not repeated here.
|
|
107
107
|
@elements = cat.category_sizes.int64
|
|
108
108
|
nvalid = counts.sum
|
|
109
|
-
@offsets = cat.reduceat_index # cached segment STARTS (int64[k])
|
|
109
|
+
@offsets = cat.send(:reduceat_index) # cached segment STARTS (int64[k])
|
|
110
110
|
@bounds = CArray.segment_offsets(lengths: @elements) # every boundary (int64[k+1])
|
|
111
111
|
# The group-major source indices (#perm) are taken from the cached
|
|
112
112
|
# group_addr when first asked for.
|
|
@@ -339,7 +339,7 @@ class CACategoricalIterator < CASegmentIterator
|
|
|
339
339
|
super
|
|
340
340
|
end
|
|
341
341
|
|
|
342
|
-
def kernel_reads?
|
|
342
|
+
def kernel_reads?(data_type = (@as_built || grouped).data_type)
|
|
343
343
|
super(data_type)
|
|
344
344
|
end
|
|
345
345
|
|
|
@@ -364,8 +364,8 @@ class CACategoricalIterator < CASegmentIterator
|
|
|
364
364
|
# sums. Any other payload -- complex, boolean, object, a Face -- the kernel
|
|
365
365
|
# refuses, for an answer that never depended on the payload, so it is
|
|
366
366
|
# counted from the codes and the value's mask, one group at a time.
|
|
367
|
-
def axis_counts
|
|
368
|
-
h = @
|
|
367
|
+
def axis_counts(axis, op)
|
|
368
|
+
h = @source
|
|
369
369
|
return axis_moments(axis, op)[:count] if kernel_reads?(h.data_type)
|
|
370
370
|
axis = checked_axis(axis, op)
|
|
371
371
|
full_c = resolve_axis_codes(@cat.codes, h.shape, axis)
|
|
@@ -399,9 +399,9 @@ class CACategoricalIterator < CASegmentIterator
|
|
|
399
399
|
# data can produce. The axis: path materialises nothing else, so holding
|
|
400
400
|
# this one thing was the odd choice; a caller who wants a fused kernel's
|
|
401
401
|
# four answers shares them by keeping the result.
|
|
402
|
-
def axis_moments
|
|
403
|
-
return axis_moments_by_core(axis, op) unless kernel_reads?(@
|
|
404
|
-
h = @
|
|
402
|
+
def axis_moments(axis, op)
|
|
403
|
+
return axis_moments_by_core(axis, op) unless kernel_reads?(@source.data_type)
|
|
404
|
+
h = @source
|
|
405
405
|
axis = checked_axis(axis, op)
|
|
406
406
|
band = h.shape.dup; band.delete_at(axis)
|
|
407
407
|
return band_only_moments(h, axis, band, op) if band_only?(@cat.codes, h.shape, axis)
|
|
@@ -418,7 +418,7 @@ class CACategoricalIterator < CASegmentIterator
|
|
|
418
418
|
|
|
419
419
|
# The classifier is band-only, as resolve_axis_codes decides it (case A
|
|
420
420
|
# wins when both shapes fit).
|
|
421
|
-
def band_only?
|
|
421
|
+
def band_only?(codes, h_shape, axis)
|
|
422
422
|
band = h_shape.dup; band.delete_at(axis)
|
|
423
423
|
!band.empty? && codes.shape == band && codes.shape != [h_shape[axis]]
|
|
424
424
|
end
|
|
@@ -436,7 +436,7 @@ class CACategoricalIterator < CASegmentIterator
|
|
|
436
436
|
Ractor.make_shareable(BAND_ONLY_KEYS)
|
|
437
437
|
private_constant :BAND_ONLY_KEYS
|
|
438
438
|
|
|
439
|
-
def band_only_moments
|
|
439
|
+
def band_only_moments(h, axis, band, op)
|
|
440
440
|
nb = band.inject(1, :*)
|
|
441
441
|
shape = [@k] + band
|
|
442
442
|
codes = @cat.codes
|
|
@@ -462,16 +462,24 @@ class CACategoricalIterator < CASegmentIterator
|
|
|
462
462
|
out
|
|
463
463
|
end
|
|
464
464
|
|
|
465
|
-
# Axis-aware sum: the moments sum is
|
|
466
|
-
#
|
|
467
|
-
|
|
468
|
-
|
|
465
|
+
# Axis-aware sum: the moments sum is the core fold in float64 (an empty
|
|
466
|
+
# group cell carries identity 0.0). An integer value's sum is read back in
|
|
467
|
+
# the core's integer type where that is exact, and folded by the core
|
|
468
|
+
# otherwise (integer_total).
|
|
469
|
+
def axis_sum(axis)
|
|
470
|
+
sums = axis_moments(axis, :sum)[:sum]
|
|
471
|
+
return sums unless integer_payload?
|
|
472
|
+
mm = axis_moments(axis, :minmax)
|
|
473
|
+
integer_total(:sum, sums, min: mm[:min], max: mm[:max],
|
|
474
|
+
count: axis_moments(axis, :mean)[:count]) {
|
|
475
|
+
axis_by_core(axis, :sum)
|
|
476
|
+
}
|
|
469
477
|
end
|
|
470
478
|
|
|
471
479
|
# Axis-aware mean: sums / counts (float64); empty group cells (count=0) MASKED.
|
|
472
480
|
# Matches flat #mean per fiber.
|
|
473
|
-
def axis_mean
|
|
474
|
-
return axis_by_core(axis, :mean) unless kernel_reads?(@
|
|
481
|
+
def axis_mean(axis)
|
|
482
|
+
return axis_by_core(axis, :mean) unless kernel_reads?(@source.data_type)
|
|
475
483
|
m = axis_moments(axis, :mean)
|
|
476
484
|
cnt = m[:count]
|
|
477
485
|
out = m[:sum] / cnt.float64 # count 0 -> NaN
|
|
@@ -491,9 +499,9 @@ class CACategoricalIterator < CASegmentIterator
|
|
|
491
499
|
# Cost: K axis-reductions over an h-shaped local (most cells masked away for
|
|
492
500
|
# each c) — bounded by K, typically small. A fused per-fiber variance
|
|
493
501
|
# kernel is a natural follow-on if bench demands it.
|
|
494
|
-
def axis_by_masked_copy
|
|
495
|
-
return axis_by_core(axis, op) if @
|
|
496
|
-
h = @
|
|
502
|
+
def axis_by_masked_copy(axis, op, out_data_type = core_reduce_type(op))
|
|
503
|
+
return axis_by_core(axis, op) if @source.face?
|
|
504
|
+
h = @source
|
|
497
505
|
axis = checked_axis(axis, op)
|
|
498
506
|
full_c = resolve_axis_codes(@cat.codes, h.shape, axis)
|
|
499
507
|
band = h.shape.dup; band.delete_at(axis)
|
|
@@ -526,8 +534,8 @@ class CACategoricalIterator < CASegmentIterator
|
|
|
526
534
|
# their own) cannot be put together, so they are collected as the surface
|
|
527
535
|
# values they are, as the flat form does. Over a 1-D value each group
|
|
528
536
|
# reduces to one cell, kept as a 1-element array so the answers stack.
|
|
529
|
-
def axis_by_core
|
|
530
|
-
h = @
|
|
537
|
+
def axis_by_core(axis, op, *args)
|
|
538
|
+
h = @source
|
|
531
539
|
axis = checked_axis(axis, op)
|
|
532
540
|
flat = (h.ndim == 1)
|
|
533
541
|
groups = (0...[@k, 1].max).map { |c| group_cells(h, axis, c, @k.zero?) }
|
|
@@ -538,17 +546,19 @@ class CACategoricalIterator < CASegmentIterator
|
|
|
538
546
|
if answers.first.face? && !answers.first.__send__(:holds_new_values?)
|
|
539
547
|
answers = answers.map { |a| CArray.object(*a.shape) { a.to_a } }
|
|
540
548
|
end
|
|
541
|
-
|
|
549
|
+
joined = CArray.send(:integer_total_join_type, answers)
|
|
550
|
+
out = flat ? CArray.concatenate(answers, data_type: joined)
|
|
551
|
+
: CArray.stack(answers, axis: 0, data_type: joined).copy
|
|
542
552
|
@k.zero? ? out[0...0, *([nil] * (out.ndim - 1))].copy : out
|
|
543
553
|
end
|
|
544
554
|
|
|
545
|
-
def axis_moments_by_core
|
|
555
|
+
def axis_moments_by_core(axis, op)
|
|
546
556
|
BAND_ONLY_KEYS.fetch(op, [:count]).to_h do |key|
|
|
547
557
|
[key, axis_by_core(axis, key == :count ? :count_not_masked : key)]
|
|
548
558
|
end
|
|
549
559
|
end
|
|
550
560
|
|
|
551
|
-
def group_cells
|
|
561
|
+
def group_cells(h, axis, c, none)
|
|
552
562
|
codes = @cat.codes
|
|
553
563
|
# Classified along the axis, a group is taken by index; the general way,
|
|
554
564
|
# masking the rest of a copy, gives the same cells.
|
|
@@ -574,54 +584,58 @@ class CACategoricalIterator < CASegmentIterator
|
|
|
574
584
|
# form's is; the kernel runs when it answers in the core's data type, and
|
|
575
585
|
# otherwise each group's positions are scanned by the core along the axis.
|
|
576
586
|
# Read when called, as the other axis: members are.
|
|
577
|
-
def axis_scan
|
|
578
|
-
h = @
|
|
587
|
+
def axis_scan(op, axis)
|
|
588
|
+
h = @source
|
|
579
589
|
axis = checked_axis(axis, op)
|
|
580
590
|
classified_along!(op, axis)
|
|
581
591
|
answer = core_scan_answer(op)
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
end
|
|
587
|
-
each_group_along(axis, ->(shape) { blank_like(answer, shape) }) do |g|
|
|
588
|
-
g.public_send(op, axis: axis)
|
|
592
|
+
by_core = lambda do
|
|
593
|
+
each_group_along(axis, ->(shape) { blank_like(answer, shape) }) do |g|
|
|
594
|
+
g.public_send(op, axis: axis)
|
|
595
|
+
end
|
|
589
596
|
end
|
|
597
|
+
integer = CArray.__fast_paths_enabled__(:segment_scan_kernel) &&
|
|
598
|
+
CAIterator.send(:integer_running?, op, h, answer)
|
|
599
|
+
return by_core.call unless integer || kernel_scans?(op, answer)
|
|
600
|
+
bundle = @k > 0 ? [@cat.codes, @k, [axis]]
|
|
601
|
+
: [CArray.int64(h.shape[axis]).fill(-1), 1, [axis]]
|
|
602
|
+
out = h.__scan_by_axis_groups__([axis], [bundle], op)
|
|
603
|
+
integer ? CAIterator.send(:integer_running, out, answer, &by_core) : out
|
|
590
604
|
end
|
|
591
605
|
|
|
592
606
|
# #lag along an axis (case A): each group's positions along the axis, every
|
|
593
607
|
# other axis kept, lagged along it, so a lag never reaches another group.
|
|
594
|
-
def axis_lag
|
|
608
|
+
def axis_lag(n, axis)
|
|
595
609
|
axis = checked_axis(axis, :lag)
|
|
596
610
|
classified_along!(:lag, axis)
|
|
597
|
-
each_group_along(axis, ->(shape) { blank_like(@
|
|
611
|
+
each_group_along(axis, ->(shape) { blank_like(@source, shape) }) do |g|
|
|
598
612
|
g.lag(n, axis: axis)
|
|
599
613
|
end
|
|
600
614
|
end
|
|
601
615
|
|
|
602
616
|
# #strip_mask(method:) along an axis (case A): each group's positions along
|
|
603
617
|
# the axis filled along it, fiber by fiber.
|
|
604
|
-
def axis_hold
|
|
618
|
+
def axis_hold(method, axis)
|
|
605
619
|
axis = checked_axis(axis, :strip_mask)
|
|
606
620
|
classified_along!(:strip_mask, axis)
|
|
607
|
-
each_group_along(axis, ->(shape) { blank_like(@
|
|
621
|
+
each_group_along(axis, ->(shape) { blank_like(@source, shape) }) do |g|
|
|
608
622
|
g.strip_mask(method: method, axis: axis)
|
|
609
623
|
end
|
|
610
624
|
end
|
|
611
625
|
|
|
612
|
-
# #
|
|
626
|
+
# #rank_index along an axis (case A): each group's positions along the axis
|
|
613
627
|
# ranked along it, fiber by fiber.
|
|
614
|
-
def
|
|
615
|
-
axis = checked_axis(axis, :
|
|
616
|
-
classified_along!(:
|
|
628
|
+
def axis_rank_index(axis, **kw)
|
|
629
|
+
axis = checked_axis(axis, :rank_index)
|
|
630
|
+
classified_along!(:rank_index, axis)
|
|
617
631
|
each_group_along(axis, ->(shape) { CArray.int64(*shape) }) do |g|
|
|
618
|
-
g.
|
|
632
|
+
g.rank_index(axis: axis, **kw)
|
|
619
633
|
end
|
|
620
634
|
end
|
|
621
635
|
|
|
622
636
|
# #map along an axis (case A): the block receives each group's positions
|
|
623
637
|
# along the axis, every other axis kept, and its answer goes back to them.
|
|
624
|
-
def axis_map
|
|
638
|
+
def axis_map(axis, data_type, &block)
|
|
625
639
|
axis = checked_axis(axis, :map)
|
|
626
640
|
classified_along!(:map, axis)
|
|
627
641
|
each_group_along(axis, ->(shape) { blank_output(shape, data_type) }, &block)
|
|
@@ -629,8 +643,8 @@ class CACategoricalIterator < CASegmentIterator
|
|
|
629
643
|
|
|
630
644
|
# Yields each group's positions along +axis+ as a view of the value and
|
|
631
645
|
# writes the block's answer back to them; a position in no group is UNDEF.
|
|
632
|
-
def each_group_along
|
|
633
|
-
h = @
|
|
646
|
+
def each_group_along(axis, blank)
|
|
647
|
+
h = @source
|
|
634
648
|
out = blank.call(h.shape)
|
|
635
649
|
out[] = UNDEF
|
|
636
650
|
@k.times do |c|
|
|
@@ -639,29 +653,31 @@ class CACategoricalIterator < CASegmentIterator
|
|
|
639
653
|
next if sel.count(true).zero?
|
|
640
654
|
idx = [nil] * h.ndim
|
|
641
655
|
idx[axis] = sel.where
|
|
642
|
-
|
|
656
|
+
answer = yield(h[*idx])
|
|
657
|
+
out = CAIterator.send(:widen_for_running, out, answer)
|
|
658
|
+
out[*idx] = surface_for(out, answer)
|
|
643
659
|
end
|
|
644
660
|
out
|
|
645
661
|
end
|
|
646
662
|
|
|
647
663
|
# A running value or a transform along an axis follows the positions along
|
|
648
664
|
# it, so the categorical has to classify those positions.
|
|
649
|
-
def classified_along!
|
|
650
|
-
n = @
|
|
665
|
+
def classified_along!(op, axis)
|
|
666
|
+
n = @source.shape[axis]
|
|
651
667
|
return if @cat.codes.shape == [n]
|
|
652
668
|
raise ArgumentError,
|
|
653
669
|
"#{op}(axis: #{axis}): needs a categorical that classifies the " \
|
|
654
670
|
"positions along axis #{axis} (shape [#{n}]); got shape " \
|
|
655
671
|
"#{@cat.codes.shape.inspect}"
|
|
656
672
|
end
|
|
657
|
-
private :axis_scan, :axis_lag, :
|
|
673
|
+
private :axis_scan, :axis_lag, :axis_rank_index, :axis_hold, :axis_map, :each_group_along, :classified_along!
|
|
658
674
|
|
|
659
675
|
# Axis-aware wsum + wmean fused (single kernel call, both outputs). Returns
|
|
660
676
|
# [wsum_ca, wmean_ca]. Weights must match source shape exactly (explicit
|
|
661
677
|
# broadcast on the call site for 1-D or band-shape weights). A cell
|
|
662
678
|
# contributes iff its value AND its weight are present.
|
|
663
|
-
def axis_wsum_wmean
|
|
664
|
-
h = @
|
|
679
|
+
def axis_wsum_wmean(weights, axis, op)
|
|
680
|
+
h = @source
|
|
665
681
|
axis = checked_axis(axis, op)
|
|
666
682
|
unless weights.is_a?(CArray) && weights.shape == h.shape
|
|
667
683
|
raise ArgumentError,
|
|
@@ -678,19 +694,24 @@ class CACategoricalIterator < CASegmentIterator
|
|
|
678
694
|
wm_out = CArray.float64(*([@k] + band))
|
|
679
695
|
h.__send__(:__fiber_scatter_wsum_wmean__, codes_h_shape, weights_f64,
|
|
680
696
|
axis, @k, ws_out, wm_out)
|
|
697
|
+
products, unsigned = h.send(:integer_weighted_products, weights) if op == :wsum
|
|
698
|
+
if products
|
|
699
|
+
ws_out = CArray.send(:integer_weighted_total,
|
|
700
|
+
products.group_by_category(@cat).sum(axis: axis), unsigned)
|
|
701
|
+
end
|
|
681
702
|
[ws_out, wm_out]
|
|
682
703
|
end
|
|
683
704
|
|
|
684
705
|
# wsum and wmean of each group by the core: the group's cells of the value
|
|
685
706
|
# and of the weights, each other cell masked, reduced along the axis.
|
|
686
|
-
def weighted_by_core
|
|
687
|
-
h = @
|
|
707
|
+
def weighted_by_core(weights, axis)
|
|
708
|
+
h = @source
|
|
688
709
|
%i[wsum wmean].map do |op|
|
|
689
710
|
answers = (0...[@k, 1].max).map do |c|
|
|
690
711
|
group_cells(h, axis, c, @k.zero?)
|
|
691
712
|
.public_send(op, group_cells(weights, axis, c, @k.zero?), axis: axis)
|
|
692
713
|
end
|
|
693
|
-
out = CArray.stack(answers, axis: 0).copy
|
|
714
|
+
out = CArray.stack(answers, axis: 0, data_type: CArray.send(:integer_total_join_type, answers)).copy
|
|
694
715
|
@k.zero? ? out[0...0, *([nil] * (out.ndim - 1))].copy : out
|
|
695
716
|
end
|
|
696
717
|
end
|
|
@@ -698,27 +719,28 @@ class CACategoricalIterator < CASegmentIterator
|
|
|
698
719
|
|
|
699
720
|
# Axis-aware prod: dedicated kernel (identity 1.0, separate from moments to
|
|
700
721
|
# avoid conflating with sum's zero-identity memset).
|
|
701
|
-
def axis_prod
|
|
702
|
-
return axis_by_core(axis, :prod) unless kernel_reads?(@
|
|
703
|
-
h = @
|
|
722
|
+
def axis_prod(axis)
|
|
723
|
+
return axis_by_core(axis, :prod) unless kernel_reads?(@source.data_type)
|
|
724
|
+
h = @source
|
|
704
725
|
axis = checked_axis(axis, :prod)
|
|
705
726
|
codes_h_shape = resolve_axis_codes(@cat.codes, h.shape, axis)
|
|
706
727
|
band = h.shape.dup; band.delete_at(axis)
|
|
707
728
|
out = CArray.float64(*([@k] + band))
|
|
708
729
|
h.__send__(:__fiber_scatter_prod__, codes_h_shape, axis, @k, out)
|
|
709
|
-
out
|
|
730
|
+
return out unless integer_payload?
|
|
731
|
+
integer_total(:prod, out) { axis_by_core(axis, :prod) }
|
|
710
732
|
end
|
|
711
733
|
|
|
712
|
-
# Broadcast `codes` to `h_shape`
|
|
734
|
+
# Broadcast `codes` to `h_shape` by the 3-case positional rule.
|
|
713
735
|
# Returns a broadcast view of codes at h_shape (or codes itself for case B).
|
|
714
736
|
# Raises ArgumentError with a message that enumerates all 3 accepted shapes.
|
|
715
737
|
# The axis of an axis: reduction, normalised against the source: a
|
|
716
738
|
# negative axis counts from the end, as everywhere else.
|
|
717
|
-
def checked_axis
|
|
718
|
-
@
|
|
739
|
+
def checked_axis(axis, op)
|
|
740
|
+
@source.normalize_axis(axis, op.to_s)
|
|
719
741
|
end
|
|
720
742
|
|
|
721
|
-
def resolve_axis_codes
|
|
743
|
+
def resolve_axis_codes(codes, h_shape, axis)
|
|
722
744
|
ndim = h_shape.size
|
|
723
745
|
band = h_shape.dup; band.delete_at(axis)
|
|
724
746
|
# Chosen by shape, not by rank. For a 2-D source the case A shape and the
|
|
@@ -742,7 +764,7 @@ class CACategoricalIterator < CASegmentIterator
|
|
|
742
764
|
end
|
|
743
765
|
end
|
|
744
766
|
|
|
745
|
-
def axis_shape_mismatch!
|
|
767
|
+
def axis_shape_mismatch!(cat_shape, h_shape, axis, band)
|
|
746
768
|
# No method name: the one place that resolves this serves sum, mean, min,
|
|
747
769
|
# max, count and the rest alike, and naming one of them would be wrong for
|
|
748
770
|
# the others. The backtrace says which was called.
|
|
@@ -756,7 +778,7 @@ class CACategoricalIterator < CASegmentIterator
|
|
|
756
778
|
|
|
757
779
|
# median / percentile along an axis: the core's own order statistic of each
|
|
758
780
|
# group's cells, stacked -- the axis_by_core path, one core call per group.
|
|
759
|
-
def axis_order_stat
|
|
781
|
+
def axis_order_stat(axis, op, *args)
|
|
760
782
|
axis_by_core(axis, op, *args)
|
|
761
783
|
end
|
|
762
784
|
end
|
|
@@ -770,7 +792,7 @@ class CArray
|
|
|
770
792
|
# @return [CACategoricalIterator]
|
|
771
793
|
# @raise [TypeError] when `cat` is not a CACategorical.
|
|
772
794
|
# @raise [ArgumentError] when element counts differ.
|
|
773
|
-
def group_by_category
|
|
795
|
+
def group_by_category(cat)
|
|
774
796
|
unless cat.is_a?(CACategorical)
|
|
775
797
|
raise TypeError,
|
|
776
798
|
"group_by_category: expects a CACategorical (got #{cat.class}); " \
|
|
@@ -789,7 +811,7 @@ class CArray
|
|
|
789
811
|
# no present cell yields zero groups rather than raising. 1-D only.
|
|
790
812
|
#
|
|
791
813
|
# ```ruby
|
|
792
|
-
# prec =
|
|
814
|
+
# prec = CA_FLOAT64([1,2,2,2,0,0,0,2,1,2,0,0,0,3,2,3,2,1,0,0,0])
|
|
793
815
|
# grp = prec.mask_where(:le, 0).group_by_run
|
|
794
816
|
# grp.sum # => [7.0, 5.0, 11.0] per-run accumulation
|
|
795
817
|
# grp.count # => [4, 3, 5] per-run length
|
data/lib/carray/complex.rb
CHANGED
|
@@ -52,49 +52,52 @@ class CArray
|
|
|
52
52
|
# framework's read-only chain does not replace the CAField mutable view).
|
|
53
53
|
# ---------------------------------------------------------------------------
|
|
54
54
|
|
|
55
|
-
# Whether a view kept by {#real} / {#imag} was made for this array.
|
|
56
|
-
# `dup` and `clone` copy instance variables, so a copy arrives holding the
|
|
57
|
-
# original's view, whose parent is the original; the copy makes its own.
|
|
58
|
-
def part_of_self? (view)
|
|
59
|
-
view && view.parent.equal?(self)
|
|
60
|
-
end
|
|
61
|
-
private :part_of_self?
|
|
62
|
-
|
|
63
55
|
# @overload real
|
|
64
|
-
# Returns the real part of `self
|
|
65
|
-
#
|
|
66
|
-
#
|
|
67
|
-
#
|
|
56
|
+
# Returns the real part of `self`. For a complex array it is a
|
|
57
|
+
# mutable {CAField} view of the real-part slot; for a real numeric
|
|
58
|
+
# array a {CARefer} view of `self`. Writing to either view updates
|
|
59
|
+
# `self` in place. Each call returns a new view, so freezing one or
|
|
60
|
+
# giving it attributes does not reach `self` or later calls. For an
|
|
61
|
+
# object array it is a new array of each cell's `real`, as {#imag} gives
|
|
62
|
+
# each cell's `imaginary`. Masked cells stay masked.
|
|
68
63
|
# @return [CArray]
|
|
64
|
+
# @raise [CArray::DataTypeError] for a fixlen array or a Face (a time,
|
|
65
|
+
# a duration, a categorical), whose cells are not numbers.
|
|
69
66
|
def real
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
67
|
+
refuse_partless("real")
|
|
68
|
+
return send(:real_i) if object?
|
|
69
|
+
if complex?
|
|
70
|
+
case data_type
|
|
71
|
+
when CA_CMPLX64 then field(0, CA_FLOAT32)
|
|
72
|
+
when CA_CMPLX128 then field(0, CA_FLOAT64)
|
|
73
|
+
end
|
|
74
|
+
else
|
|
75
|
+
scalar? ? self : self[]
|
|
76
|
+
end
|
|
80
77
|
end
|
|
81
78
|
|
|
82
79
|
# @overload real=(val)
|
|
83
80
|
# Sets the real-part slot to `val` via {#real}.
|
|
84
81
|
# @param val [CArray, Numeric] value to broadcast.
|
|
85
82
|
# @return [Object] `val`.
|
|
86
|
-
|
|
83
|
+
# @raise [CArray::DataTypeError] for a fixlen array or a Face (a time,
|
|
84
|
+
# a duration, a categorical), whose cells are not numbers.
|
|
85
|
+
def real=(val)
|
|
86
|
+
refuse_partless("real=")
|
|
87
87
|
real[] = val
|
|
88
88
|
end
|
|
89
89
|
|
|
90
90
|
# @overload imag
|
|
91
91
|
# Returns the imaginary part of `self`. For a complex array the
|
|
92
|
-
# result is a mutable {CAField} view of the imaginary slot
|
|
93
|
-
# writing to it updates `self` in place. For an object array each
|
|
92
|
+
# result is a new mutable {CAField} view of the imaginary slot on
|
|
93
|
+
# each call; writing to it updates `self` in place. For an object array each
|
|
94
94
|
# cell's `imaginary`; for a real array a fresh independent CArray
|
|
95
95
|
# filled with 0. Masked cells stay masked.
|
|
96
96
|
# @return [CArray]
|
|
97
|
+
# @raise [CArray::DataTypeError] for a fixlen array or a Face (a time,
|
|
98
|
+
# a duration, a categorical), whose cells are not numbers.
|
|
97
99
|
def imag
|
|
100
|
+
refuse_partless("imag")
|
|
98
101
|
if object?
|
|
99
102
|
return imag_i
|
|
100
103
|
end
|
|
@@ -103,12 +106,10 @@ class CArray
|
|
|
103
106
|
zero.inherit_mask(self) if has_mask?
|
|
104
107
|
return zero
|
|
105
108
|
end
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
end
|
|
111
|
-
frozen? ? view : (@__imag__ = view)
|
|
109
|
+
case data_type
|
|
110
|
+
when CA_CMPLX64 then field(4, CA_FLOAT32)
|
|
111
|
+
when CA_CMPLX128 then field(8, CA_FLOAT64)
|
|
112
|
+
end
|
|
112
113
|
end
|
|
113
114
|
|
|
114
115
|
# @overload imag=(val)
|
|
@@ -116,7 +117,8 @@ class CArray
|
|
|
116
117
|
# @param val [CArray, Numeric] value to broadcast.
|
|
117
118
|
# @return [Object] `val`.
|
|
118
119
|
# @raise [CArray::DataTypeError] when `self` is not a complex array.
|
|
119
|
-
def imag=
|
|
120
|
+
def imag=(val)
|
|
121
|
+
refuse_partless("imag=")
|
|
120
122
|
if complex?
|
|
121
123
|
imag[] = val
|
|
122
124
|
else
|
|
@@ -127,30 +129,48 @@ class CArray
|
|
|
127
129
|
# @overload real?
|
|
128
130
|
# Returns whether every element of `self` is real (imaginary
|
|
129
131
|
# part is zero for complex arrays; always `true` for real
|
|
130
|
-
# numeric arrays
|
|
131
|
-
# @return [Boolean
|
|
132
|
+
# numeric arrays).
|
|
133
|
+
# @return [Boolean]
|
|
134
|
+
# @raise [CArray::DataTypeError] when `self` is not a numeric array.
|
|
132
135
|
def real?
|
|
133
136
|
if complex?
|
|
134
137
|
imag.eq(0).all
|
|
135
|
-
elsif numeric?
|
|
138
|
+
elsif numeric? && ! face?
|
|
136
139
|
true
|
|
137
140
|
else
|
|
138
|
-
|
|
141
|
+
refuse_non_numeric_realness("real?")
|
|
139
142
|
end
|
|
140
143
|
end
|
|
141
144
|
|
|
142
145
|
# @overload is_real
|
|
143
146
|
# Returns an element-wise boolean CArray marking cells whose
|
|
144
|
-
# imaginary part is zero (all-true for real numeric arrays
|
|
145
|
-
#
|
|
146
|
-
# @
|
|
147
|
+
# imaginary part is zero (all-true for real numeric arrays).
|
|
148
|
+
# @return [CArray]
|
|
149
|
+
# @raise [CArray::DataTypeError] when `self` is not a numeric array.
|
|
147
150
|
def is_real
|
|
148
151
|
if complex?
|
|
149
152
|
imag.eq(0)
|
|
150
|
-
elsif numeric?
|
|
153
|
+
elsif numeric? && ! face?
|
|
151
154
|
self.true
|
|
152
155
|
else
|
|
153
|
-
|
|
156
|
+
refuse_non_numeric_realness("is_real")
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
# An array whose cells are not numbers (object, fixlen, boolean, a Face)
|
|
161
|
+
# has no real / imaginary parts to answer about.
|
|
162
|
+
private def refuse_non_numeric_realness(verb)
|
|
163
|
+
what = CArray::Inspector.type_label(self)
|
|
164
|
+
what = "data_type #{what}" unless face?
|
|
165
|
+
raise CArray::DataTypeError, "#{verb}: not defined for #{what}"
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
# Fixlen cells and a Face's cells are not numbers, so they have no real
|
|
169
|
+
# and imaginary parts. Boolean cells are the numbers 0 and 1, and an
|
|
170
|
+
# object cell answers for itself.
|
|
171
|
+
private def refuse_partless(verb)
|
|
172
|
+
if face? || ! ( numeric? || boolean? || object? )
|
|
173
|
+
refuse_non_numeric_realness(verb)
|
|
154
174
|
end
|
|
155
175
|
end
|
|
156
176
|
|