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
|
@@ -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,8 +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
|
-
|
|
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
|
|
496
505
|
axis = checked_axis(axis, op)
|
|
497
506
|
full_c = resolve_axis_codes(@cat.codes, h.shape, axis)
|
|
498
507
|
band = h.shape.dup; band.delete_at(axis)
|
|
@@ -525,8 +534,8 @@ class CACategoricalIterator < CASegmentIterator
|
|
|
525
534
|
# their own) cannot be put together, so they are collected as the surface
|
|
526
535
|
# values they are, as the flat form does. Over a 1-D value each group
|
|
527
536
|
# reduces to one cell, kept as a 1-element array so the answers stack.
|
|
528
|
-
def axis_by_core
|
|
529
|
-
h = @
|
|
537
|
+
def axis_by_core(axis, op, *args)
|
|
538
|
+
h = @source
|
|
530
539
|
axis = checked_axis(axis, op)
|
|
531
540
|
flat = (h.ndim == 1)
|
|
532
541
|
groups = (0...[@k, 1].max).map { |c| group_cells(h, axis, c, @k.zero?) }
|
|
@@ -534,20 +543,22 @@ class CACategoricalIterator < CASegmentIterator
|
|
|
534
543
|
flat ? g.__send__(op, *args, axis: axis, keep_axis: true)
|
|
535
544
|
: g.__send__(op, *args, axis: axis)
|
|
536
545
|
end
|
|
537
|
-
if answers.first.face? && answers.first.
|
|
546
|
+
if answers.first.face? && !answers.first.__send__(:holds_new_values?)
|
|
538
547
|
answers = answers.map { |a| CArray.object(*a.shape) { a.to_a } }
|
|
539
548
|
end
|
|
540
|
-
|
|
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
|
|
541
552
|
@k.zero? ? out[0...0, *([nil] * (out.ndim - 1))].copy : out
|
|
542
553
|
end
|
|
543
554
|
|
|
544
|
-
def axis_moments_by_core
|
|
555
|
+
def axis_moments_by_core(axis, op)
|
|
545
556
|
BAND_ONLY_KEYS.fetch(op, [:count]).to_h do |key|
|
|
546
557
|
[key, axis_by_core(axis, key == :count ? :count_not_masked : key)]
|
|
547
558
|
end
|
|
548
559
|
end
|
|
549
560
|
|
|
550
|
-
def group_cells
|
|
561
|
+
def group_cells(h, axis, c, none)
|
|
551
562
|
codes = @cat.codes
|
|
552
563
|
# Classified along the axis, a group is taken by index; the general way,
|
|
553
564
|
# masking the rest of a copy, gives the same cells.
|
|
@@ -573,54 +584,58 @@ class CACategoricalIterator < CASegmentIterator
|
|
|
573
584
|
# form's is; the kernel runs when it answers in the core's data type, and
|
|
574
585
|
# otherwise each group's positions are scanned by the core along the axis.
|
|
575
586
|
# Read when called, as the other axis: members are.
|
|
576
|
-
def axis_scan
|
|
577
|
-
h = @
|
|
587
|
+
def axis_scan(op, axis)
|
|
588
|
+
h = @source
|
|
578
589
|
axis = checked_axis(axis, op)
|
|
579
590
|
classified_along!(op, axis)
|
|
580
591
|
answer = core_scan_answer(op)
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
end
|
|
586
|
-
each_group_along(axis, ->(shape) { blank_like(answer, shape) }) do |g|
|
|
587
|
-
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
|
|
588
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
|
|
589
604
|
end
|
|
590
605
|
|
|
591
606
|
# #lag along an axis (case A): each group's positions along the axis, every
|
|
592
607
|
# other axis kept, lagged along it, so a lag never reaches another group.
|
|
593
|
-
def axis_lag
|
|
608
|
+
def axis_lag(n, axis)
|
|
594
609
|
axis = checked_axis(axis, :lag)
|
|
595
610
|
classified_along!(:lag, axis)
|
|
596
|
-
each_group_along(axis, ->(shape) { blank_like(@
|
|
611
|
+
each_group_along(axis, ->(shape) { blank_like(@source, shape) }) do |g|
|
|
597
612
|
g.lag(n, axis: axis)
|
|
598
613
|
end
|
|
599
614
|
end
|
|
600
615
|
|
|
601
616
|
# #strip_mask(method:) along an axis (case A): each group's positions along
|
|
602
617
|
# the axis filled along it, fiber by fiber.
|
|
603
|
-
def axis_hold
|
|
618
|
+
def axis_hold(method, axis)
|
|
604
619
|
axis = checked_axis(axis, :strip_mask)
|
|
605
620
|
classified_along!(:strip_mask, axis)
|
|
606
|
-
each_group_along(axis, ->(shape) { blank_like(@
|
|
621
|
+
each_group_along(axis, ->(shape) { blank_like(@source, shape) }) do |g|
|
|
607
622
|
g.strip_mask(method: method, axis: axis)
|
|
608
623
|
end
|
|
609
624
|
end
|
|
610
625
|
|
|
611
|
-
# #
|
|
626
|
+
# #rank_index along an axis (case A): each group's positions along the axis
|
|
612
627
|
# ranked along it, fiber by fiber.
|
|
613
|
-
def
|
|
614
|
-
axis = checked_axis(axis, :
|
|
615
|
-
classified_along!(:
|
|
628
|
+
def axis_rank_index(axis, **kw)
|
|
629
|
+
axis = checked_axis(axis, :rank_index)
|
|
630
|
+
classified_along!(:rank_index, axis)
|
|
616
631
|
each_group_along(axis, ->(shape) { CArray.int64(*shape) }) do |g|
|
|
617
|
-
g.
|
|
632
|
+
g.rank_index(axis: axis, **kw)
|
|
618
633
|
end
|
|
619
634
|
end
|
|
620
635
|
|
|
621
636
|
# #map along an axis (case A): the block receives each group's positions
|
|
622
637
|
# along the axis, every other axis kept, and its answer goes back to them.
|
|
623
|
-
def axis_map
|
|
638
|
+
def axis_map(axis, data_type, &block)
|
|
624
639
|
axis = checked_axis(axis, :map)
|
|
625
640
|
classified_along!(:map, axis)
|
|
626
641
|
each_group_along(axis, ->(shape) { blank_output(shape, data_type) }, &block)
|
|
@@ -628,8 +643,8 @@ class CACategoricalIterator < CASegmentIterator
|
|
|
628
643
|
|
|
629
644
|
# Yields each group's positions along +axis+ as a view of the value and
|
|
630
645
|
# writes the block's answer back to them; a position in no group is UNDEF.
|
|
631
|
-
def each_group_along
|
|
632
|
-
h = @
|
|
646
|
+
def each_group_along(axis, blank)
|
|
647
|
+
h = @source
|
|
633
648
|
out = blank.call(h.shape)
|
|
634
649
|
out[] = UNDEF
|
|
635
650
|
@k.times do |c|
|
|
@@ -638,29 +653,31 @@ class CACategoricalIterator < CASegmentIterator
|
|
|
638
653
|
next if sel.count(true).zero?
|
|
639
654
|
idx = [nil] * h.ndim
|
|
640
655
|
idx[axis] = sel.where
|
|
641
|
-
|
|
656
|
+
answer = yield(h[*idx])
|
|
657
|
+
out = CAIterator.send(:widen_for_running, out, answer)
|
|
658
|
+
out[*idx] = surface_for(out, answer)
|
|
642
659
|
end
|
|
643
660
|
out
|
|
644
661
|
end
|
|
645
662
|
|
|
646
663
|
# A running value or a transform along an axis follows the positions along
|
|
647
664
|
# it, so the categorical has to classify those positions.
|
|
648
|
-
def classified_along!
|
|
649
|
-
n = @
|
|
665
|
+
def classified_along!(op, axis)
|
|
666
|
+
n = @source.shape[axis]
|
|
650
667
|
return if @cat.codes.shape == [n]
|
|
651
668
|
raise ArgumentError,
|
|
652
669
|
"#{op}(axis: #{axis}): needs a categorical that classifies the " \
|
|
653
670
|
"positions along axis #{axis} (shape [#{n}]); got shape " \
|
|
654
671
|
"#{@cat.codes.shape.inspect}"
|
|
655
672
|
end
|
|
656
|
-
private :axis_scan, :axis_lag, :
|
|
673
|
+
private :axis_scan, :axis_lag, :axis_rank_index, :axis_hold, :axis_map, :each_group_along, :classified_along!
|
|
657
674
|
|
|
658
675
|
# Axis-aware wsum + wmean fused (single kernel call, both outputs). Returns
|
|
659
676
|
# [wsum_ca, wmean_ca]. Weights must match source shape exactly (explicit
|
|
660
677
|
# broadcast on the call site for 1-D or band-shape weights). A cell
|
|
661
678
|
# contributes iff its value AND its weight are present.
|
|
662
|
-
def axis_wsum_wmean
|
|
663
|
-
h = @
|
|
679
|
+
def axis_wsum_wmean(weights, axis, op)
|
|
680
|
+
h = @source
|
|
664
681
|
axis = checked_axis(axis, op)
|
|
665
682
|
unless weights.is_a?(CArray) && weights.shape == h.shape
|
|
666
683
|
raise ArgumentError,
|
|
@@ -677,19 +694,24 @@ class CACategoricalIterator < CASegmentIterator
|
|
|
677
694
|
wm_out = CArray.float64(*([@k] + band))
|
|
678
695
|
h.__send__(:__fiber_scatter_wsum_wmean__, codes_h_shape, weights_f64,
|
|
679
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
|
|
680
702
|
[ws_out, wm_out]
|
|
681
703
|
end
|
|
682
704
|
|
|
683
705
|
# wsum and wmean of each group by the core: the group's cells of the value
|
|
684
706
|
# and of the weights, each other cell masked, reduced along the axis.
|
|
685
|
-
def weighted_by_core
|
|
686
|
-
h = @
|
|
707
|
+
def weighted_by_core(weights, axis)
|
|
708
|
+
h = @source
|
|
687
709
|
%i[wsum wmean].map do |op|
|
|
688
710
|
answers = (0...[@k, 1].max).map do |c|
|
|
689
711
|
group_cells(h, axis, c, @k.zero?)
|
|
690
712
|
.public_send(op, group_cells(weights, axis, c, @k.zero?), axis: axis)
|
|
691
713
|
end
|
|
692
|
-
out = CArray.stack(answers, axis: 0).copy
|
|
714
|
+
out = CArray.stack(answers, axis: 0, data_type: CArray.send(:integer_total_join_type, answers)).copy
|
|
693
715
|
@k.zero? ? out[0...0, *([nil] * (out.ndim - 1))].copy : out
|
|
694
716
|
end
|
|
695
717
|
end
|
|
@@ -697,27 +719,28 @@ class CACategoricalIterator < CASegmentIterator
|
|
|
697
719
|
|
|
698
720
|
# Axis-aware prod: dedicated kernel (identity 1.0, separate from moments to
|
|
699
721
|
# avoid conflating with sum's zero-identity memset).
|
|
700
|
-
def axis_prod
|
|
701
|
-
return axis_by_core(axis, :prod) unless kernel_reads?(@
|
|
702
|
-
h = @
|
|
722
|
+
def axis_prod(axis)
|
|
723
|
+
return axis_by_core(axis, :prod) unless kernel_reads?(@source.data_type)
|
|
724
|
+
h = @source
|
|
703
725
|
axis = checked_axis(axis, :prod)
|
|
704
726
|
codes_h_shape = resolve_axis_codes(@cat.codes, h.shape, axis)
|
|
705
727
|
band = h.shape.dup; band.delete_at(axis)
|
|
706
728
|
out = CArray.float64(*([@k] + band))
|
|
707
729
|
h.__send__(:__fiber_scatter_prod__, codes_h_shape, axis, @k, out)
|
|
708
|
-
out
|
|
730
|
+
return out unless integer_payload?
|
|
731
|
+
integer_total(:prod, out) { axis_by_core(axis, :prod) }
|
|
709
732
|
end
|
|
710
733
|
|
|
711
|
-
# Broadcast `codes` to `h_shape`
|
|
734
|
+
# Broadcast `codes` to `h_shape` by the 3-case positional rule.
|
|
712
735
|
# Returns a broadcast view of codes at h_shape (or codes itself for case B).
|
|
713
736
|
# Raises ArgumentError with a message that enumerates all 3 accepted shapes.
|
|
714
737
|
# The axis of an axis: reduction, normalised against the source: a
|
|
715
738
|
# negative axis counts from the end, as everywhere else.
|
|
716
|
-
def checked_axis
|
|
717
|
-
@
|
|
739
|
+
def checked_axis(axis, op)
|
|
740
|
+
@source.normalize_axis(axis, op.to_s)
|
|
718
741
|
end
|
|
719
742
|
|
|
720
|
-
def resolve_axis_codes
|
|
743
|
+
def resolve_axis_codes(codes, h_shape, axis)
|
|
721
744
|
ndim = h_shape.size
|
|
722
745
|
band = h_shape.dup; band.delete_at(axis)
|
|
723
746
|
# Chosen by shape, not by rank. For a 2-D source the case A shape and the
|
|
@@ -741,7 +764,7 @@ class CACategoricalIterator < CASegmentIterator
|
|
|
741
764
|
end
|
|
742
765
|
end
|
|
743
766
|
|
|
744
|
-
def axis_shape_mismatch!
|
|
767
|
+
def axis_shape_mismatch!(cat_shape, h_shape, axis, band)
|
|
745
768
|
# No method name: the one place that resolves this serves sum, mean, min,
|
|
746
769
|
# max, count and the rest alike, and naming one of them would be wrong for
|
|
747
770
|
# the others. The backtrace says which was called.
|
|
@@ -755,7 +778,7 @@ class CACategoricalIterator < CASegmentIterator
|
|
|
755
778
|
|
|
756
779
|
# median / percentile along an axis: the core's own order statistic of each
|
|
757
780
|
# group's cells, stacked -- the axis_by_core path, one core call per group.
|
|
758
|
-
def axis_order_stat
|
|
781
|
+
def axis_order_stat(axis, op, *args)
|
|
759
782
|
axis_by_core(axis, op, *args)
|
|
760
783
|
end
|
|
761
784
|
end
|
|
@@ -769,7 +792,7 @@ class CArray
|
|
|
769
792
|
# @return [CACategoricalIterator]
|
|
770
793
|
# @raise [TypeError] when `cat` is not a CACategorical.
|
|
771
794
|
# @raise [ArgumentError] when element counts differ.
|
|
772
|
-
def group_by_category
|
|
795
|
+
def group_by_category(cat)
|
|
773
796
|
unless cat.is_a?(CACategorical)
|
|
774
797
|
raise TypeError,
|
|
775
798
|
"group_by_category: expects a CACategorical (got #{cat.class}); " \
|
|
@@ -788,7 +811,7 @@ class CArray
|
|
|
788
811
|
# no present cell yields zero groups rather than raising. 1-D only.
|
|
789
812
|
#
|
|
790
813
|
# ```ruby
|
|
791
|
-
# 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])
|
|
792
815
|
# grp = prec.mask_where(:le, 0).group_by_run
|
|
793
816
|
# grp.sum # => [7.0, 5.0, 11.0] per-run accumulation
|
|
794
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
|
|