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
data/lib/carray/methods/is_in.rb
CHANGED
|
@@ -1,137 +1,34 @@
|
|
|
1
1
|
class CArray
|
|
2
2
|
|
|
3
|
-
# @overload is_in(values)
|
|
4
|
-
# Returns a boolean CArray of the same shape as `self`, `true` at
|
|
5
|
-
# each cell whose value appears in the set `values`.
|
|
6
|
-
#
|
|
7
|
-
# `values` is treated as a set, not as an operand to broadcast: it
|
|
8
|
-
# may be any shape (or an Array) and is flattened to a
|
|
9
|
-
# single seen-set, so its shape need not match `self`. Only one
|
|
10
|
-
# argument is accepted; to test a few immediate values pass an
|
|
11
|
-
# Array (`a.is_in([0, -1])`). A single value is a set of one, as
|
|
12
|
-
# `[value]` is (`a.is_in(2)` is `a.is_in([2])`).
|
|
13
|
-
#
|
|
14
|
-
# When `self` and `values` have different numeric data types they are
|
|
15
|
-
# promoted to a common type first (the same promotion binops use,
|
|
16
|
-
# {CArray.result_type}), so membership is value-correct across
|
|
17
|
-
# data types (e.g. an int cell equals a float set element of the same
|
|
18
|
-
# value, and a fractional set element never truncates onto an int
|
|
19
|
-
# cell). Integers of mixed sign are matched by value, as {#eq}
|
|
20
|
-
# compares them: `-1` in an int8 array never matches `255` in a uint8
|
|
21
|
-
# one, and the set operations answer such a pair in a type that holds
|
|
22
|
-
# both (int16 for uint8 with int8, object for uint64 with int64).
|
|
23
|
-
# Genuinely incompatible data types (e.g. numeric vs fixlen) raise.
|
|
24
|
-
#
|
|
25
|
-
# Membership is value-based and shares the distinctness of the
|
|
26
|
-
# value-hash discovery family ({#unique} / {#value_counts}):
|
|
27
|
-
# numeric follows `==` with all NaN collapsed to one value and
|
|
28
|
-
# -0.0 == +0.0; `CA_OBJECT` follows Ruby `hash` / `eql?` with Float
|
|
29
|
-
# NaN collapsed; `CA_FIXLEN` follows byte equality over the whole
|
|
30
|
-
# cell, with a short String in the set padded out to the cell width
|
|
31
|
-
# (so a 5-byte array written from `"be"` is `is_in(["be"])`). A
|
|
32
|
-
# `CA_FIXLEN` set given as a CArray must already be of that width.
|
|
33
|
-
#
|
|
34
|
-
# An object array keeps Ruby's own split between its two kinds of
|
|
35
|
-
# equality. A lookup through a hash -- `is_in`, the set operations,
|
|
36
|
-
# {#locate_addr}, {#unique} and the rest of the discovery family --
|
|
37
|
-
# uses `eql?`, as `Array#uniq`, `Array#&` and `Set` do, so `1` and
|
|
38
|
-
# `1.0` are different values there. A cell-by-cell comparison --
|
|
39
|
-
# {#eq}, {#count}, {#search}, {#bsearch} -- uses `==`, as
|
|
40
|
-
# `Array#include?` does, so they are equal there. To match by value
|
|
41
|
-
# across Integer and Float, convert the array to a numeric type first.
|
|
42
|
-
#
|
|
43
|
-
# Masked cells of `values` do not enter the set. Masked cells of
|
|
44
|
-
# `self` stay masked in the result (membership is unknown), so
|
|
45
|
-
# `is_in` propagates `self`'s mask like an element-wise comparison.
|
|
46
|
-
#
|
|
47
|
-
# For a per-fiber "does this fiber contain any of these values"
|
|
48
|
-
# reduction, compose with {#any}: `a.is_in(values).any(axis: k)`.
|
|
49
|
-
#
|
|
50
|
-
# @param values [CArray, Array] the set to test membership
|
|
51
|
-
# against. Promoted with `self` to a common data type.
|
|
52
|
-
# @raise [ArgumentError] for a Range, which reads as an interval rather
|
|
53
|
-
# than the set of its steps.
|
|
54
|
-
# Between two time arrays the question is about instants, not ticks:
|
|
55
|
-
# `values` is reconciled into `self`'s unit first, so a `:D` array and an
|
|
56
|
-
# `:h` array match on the instants they share. The same holds for the set
|
|
57
|
-
# operations below, whose results come back as `self`'s own type.
|
|
58
|
-
#
|
|
59
|
-
# @return [CArray] boolean CArray of the same shape as `self`.
|
|
60
|
-
def is_in (values)
|
|
61
|
-
refuse_range_set(values, "is_in")
|
|
62
|
-
a, b = promote_value_set(values)
|
|
63
|
-
a.__send__(:__is_in__, b)
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
# @overload intersection(other, sort: false)
|
|
67
|
-
# Returns a 1-D CArray of the distinct values appearing in both
|
|
68
|
-
# `self` and `other`, in `self`'s first-appearance order.
|
|
69
|
-
#
|
|
70
|
-
# Value-based, sharing the distinctness of the discovery family
|
|
71
|
-
# (see {#is_in}); `self` and `other` are promoted to a common data type.
|
|
72
|
-
# Masked cells of either array do not participate. The result is
|
|
73
|
-
# always flat, like {#unique}, because the distinct values of a
|
|
74
|
-
# fiber vary in number.
|
|
75
|
-
#
|
|
76
|
-
# @param other [CArray, Array] promoted with `self` (a Range is refused, as by {#is_in}).
|
|
77
|
-
# @param sort [Boolean] when true, return the values sorted
|
|
78
|
-
# ascending instead of in first-appearance order.
|
|
79
|
-
# @return [CArray] 1-D CArray of the common distinct values.
|
|
80
|
-
def intersection (other, sort: false)
|
|
81
|
-
refuse_range_set(other, "intersection")
|
|
82
|
-
a, b = promote_value_set(other)
|
|
83
|
-
r = a.__send__(:__intersection__, b)
|
|
84
|
-
r = r.to_unit(unit) if !a.equal?(self) && respond_to?(:in_common_unit_with)
|
|
85
|
-
sort ? r.sort : r
|
|
86
|
-
end
|
|
87
|
-
|
|
88
|
-
# @overload difference(other, sort: false)
|
|
89
|
-
# Returns a 1-D CArray of the distinct values in `self` that are
|
|
90
|
-
# absent from `other`, in `self`'s first-appearance order.
|
|
91
|
-
# See {#intersection} for the shared semantics and options.
|
|
92
|
-
#
|
|
93
|
-
# @param other [CArray, Array] promoted with `self` (a Range is refused, as by {#is_in}).
|
|
94
|
-
# @param sort [Boolean] when true, return the values sorted ascending.
|
|
95
|
-
# @return [CArray] 1-D CArray of the self-only distinct values.
|
|
96
|
-
def difference (other, sort: false)
|
|
97
|
-
refuse_range_set(other, "difference")
|
|
98
|
-
a, b = promote_value_set(other)
|
|
99
|
-
r = a.__send__(:__difference__, b)
|
|
100
|
-
r = r.to_unit(unit) if !a.equal?(self) && respond_to?(:in_common_unit_with)
|
|
101
|
-
sort ? r.sort : r
|
|
102
|
-
end
|
|
103
|
-
|
|
104
|
-
# @overload union(other, sort: false)
|
|
105
|
-
# Returns a 1-D CArray of the distinct values appearing in either
|
|
106
|
-
# `self` or `other`, in self-then-other first-appearance order.
|
|
107
|
-
# See {#intersection} for the shared semantics and options. Two time
|
|
108
|
-
# arrays of different units give their union in the finer unit, which
|
|
109
|
-
# holds the values of both ({#intersection} and {#difference} answer in
|
|
110
|
-
# `self`'s unit, since their values are `self`'s).
|
|
111
|
-
#
|
|
112
|
-
# @param other [CArray, Array] promoted with `self` (a Range is refused, as by {#is_in}).
|
|
113
|
-
# @param sort [Boolean] when true, return the values sorted ascending
|
|
114
|
-
# (a merged, ordered set — e.g. a common time axis).
|
|
115
|
-
# @return [CArray] 1-D CArray of the combined distinct values.
|
|
116
|
-
def union (other, sort: false)
|
|
117
|
-
refuse_range_set(other, "union")
|
|
118
|
-
a, b = promote_value_set(other)
|
|
119
|
-
r = a.__send__(:__union__, b)
|
|
120
|
-
sort ? r.sort : r
|
|
121
|
-
end
|
|
122
|
-
|
|
123
|
-
private
|
|
124
|
-
|
|
125
3
|
# A Range reads as an interval in Ruby, which a set of its steps is not
|
|
126
4
|
# (2.5 is not in [2, 3, 4]), so the set-valued verbs refuse one rather
|
|
127
5
|
# than answer for the steps.
|
|
128
|
-
def refuse_range_set
|
|
6
|
+
private def refuse_range_set(values, verb)
|
|
129
7
|
return unless values.is_a?(Range)
|
|
130
8
|
raise ArgumentError,
|
|
131
9
|
"#{verb}: a Range is not taken as a set; pass its values as an Array " \
|
|
132
10
|
"((#{values.inspect}).to_a), or test an interval with is_between(#{values.inspect})"
|
|
133
11
|
end
|
|
134
12
|
|
|
13
|
+
# The set as the array reads it. nil is no value of an array that can not
|
|
14
|
+
# hold it, so it enters no set, and true and false are the numbers 1 and 0
|
|
15
|
+
# to an integer, float or complex array, as they are to eq.
|
|
16
|
+
private def values_of_set(values)
|
|
17
|
+
return values if data_type == CA_OBJECT
|
|
18
|
+
numeric = !face? && (integer? || float? || complex?)
|
|
19
|
+
case values
|
|
20
|
+
when Array
|
|
21
|
+
values = values.compact
|
|
22
|
+
numeric ? values.map { |v| (v == true) ? 1 : (v == false) ? 0 : v } : values
|
|
23
|
+
when CArray
|
|
24
|
+
( values.data_type == CA_OBJECT ) ? values.mask_eq(nil) : values
|
|
25
|
+
when true, false
|
|
26
|
+
numeric ? (values ? 1 : 0) : values
|
|
27
|
+
else
|
|
28
|
+
values
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
135
32
|
# Reconcile a set-valued argument with self to a common data_type via
|
|
136
33
|
# CArray.result_type (the single-source promotion rule the eager binop and
|
|
137
34
|
# lazy CABinOp share), returning [self', set']. Only data types are reconciled,
|
|
@@ -146,10 +43,14 @@ class CArray
|
|
|
146
43
|
# is not eql? Integer 2). A CArray argument uses its own data type; any other
|
|
147
44
|
# operand (Numo, a MemoryView producer, ...) comes in through wrap_readonly,
|
|
148
45
|
# the canonical type-coercion entry, so its format's data type drives the promote.
|
|
149
|
-
|
|
46
|
+
#
|
|
47
|
+
# With `lookup: true` (is_in) a value of a time set that no cell can be
|
|
48
|
+
# matches nothing (comparable_pair's lookup:); the set operations, whose
|
|
49
|
+
# results hold the values, refuse it.
|
|
50
|
+
private def promote_value_set(values, lookup: false)
|
|
150
51
|
# Two time arrays of different units meet in the finer one.
|
|
151
52
|
if values.is_a?(CArray) && values.class == self.class &&
|
|
152
|
-
respond_to?(:in_common_unit_with)
|
|
53
|
+
respond_to?(:in_common_unit_with, true)
|
|
153
54
|
return in_common_unit_with(values)
|
|
154
55
|
end
|
|
155
56
|
values = values_for_lookup(values) if values.is_a?(CArray)
|
|
@@ -160,27 +61,27 @@ class CArray
|
|
|
160
61
|
# A Face that reconciles operands (CATime) reads a bare Array as values
|
|
161
62
|
# of its own kind -- instants written as Time / String / elements -- and
|
|
162
63
|
# meets them as a comparison does (comparable_pair).
|
|
163
|
-
if values.is_a?(Array) && face? && respond_to?(:comparable_pair)
|
|
164
|
-
return comparable_pair(values)
|
|
64
|
+
if values.is_a?(Array) && face? && respond_to?(:comparable_pair, true)
|
|
65
|
+
return comparable_pair(values, lookup: lookup)
|
|
165
66
|
end
|
|
166
67
|
# A single value (a Time, an element, a bare number) is a set of one,
|
|
167
68
|
# read the same way; a bare number is refused like any other operand
|
|
168
69
|
# that is not of its kind.
|
|
169
70
|
if !values.is_a?(CArray) && !values.is_a?(Range) && face? &&
|
|
170
|
-
respond_to?(:comparable_pair)
|
|
171
|
-
return comparable_pair([values])
|
|
71
|
+
respond_to?(:comparable_pair, true)
|
|
72
|
+
return comparable_pair([values], lookup: lookup)
|
|
172
73
|
end
|
|
173
74
|
# ...and so does an array of values that are not of its kind (an object
|
|
174
75
|
# column of Time values, a string column).
|
|
175
|
-
if values.is_a?(CArray) && respond_to?(:comparable_pair) &&
|
|
176
|
-
!values.respond_to?(:comparable_pair)
|
|
177
|
-
return comparable_pair(values)
|
|
76
|
+
if values.is_a?(CArray) && respond_to?(:comparable_pair, true) &&
|
|
77
|
+
!values.respond_to?(:comparable_pair, true)
|
|
78
|
+
return comparable_pair(values, lookup: lookup)
|
|
178
79
|
end
|
|
179
80
|
# The same from the other side: values of a time array looked for in an
|
|
180
81
|
# array that is not one.
|
|
181
|
-
if values.is_a?(CArray) && values.respond_to?(:comparable_pair) &&
|
|
182
|
-
!respond_to?(:comparable_pair)
|
|
183
|
-
set, own = values.comparable_pair
|
|
82
|
+
if values.is_a?(CArray) && values.respond_to?(:comparable_pair, true) &&
|
|
83
|
+
!respond_to?(:comparable_pair, true)
|
|
84
|
+
set, own = values.send(:comparable_pair, self, lookup: lookup)
|
|
184
85
|
return [own, set]
|
|
185
86
|
end
|
|
186
87
|
# A Face whose storage is not of its surface type. When its cells are
|
|
@@ -193,7 +94,7 @@ class CArray
|
|
|
193
94
|
set = values.is_a?(Range) ? values.to_a : values
|
|
194
95
|
return [self, set.is_a?(CArray) ? set : CArray.wrap_readonly(set)]
|
|
195
96
|
end
|
|
196
|
-
s, set = storage.send(:promote_value_set, values)
|
|
97
|
+
s, set = storage.send(:promote_value_set, values, lookup: lookup)
|
|
197
98
|
return [s.equal?(storage) ? self : s, set]
|
|
198
99
|
end
|
|
199
100
|
case values
|
|
@@ -218,7 +119,7 @@ class CArray
|
|
|
218
119
|
# fractional literal promotes self to float instead of truncating. Against
|
|
219
120
|
# object / fixlen self the elements are values, not data type specifiers (a
|
|
220
121
|
# String is a value, not a type name), so build the set in self's data type.
|
|
221
|
-
def promote_elements
|
|
122
|
+
private def promote_elements(elems)
|
|
222
123
|
if data_type == CA_FIXLEN
|
|
223
124
|
# A fixlen value is a cell-width blob, so the set has to be built at
|
|
224
125
|
# this array's width -- to_type(:fixlen) with no bytes: builds it at
|
|
@@ -238,11 +139,18 @@ class CArray
|
|
|
238
139
|
elsif data_type == CA_OBJECT
|
|
239
140
|
# coerce: an object set holds values of self's kind
|
|
240
141
|
[self, elems.to_ca.to_type(data_type)]
|
|
241
|
-
elsif elems.any? { |v| v.is_a?(String) || v.is_a?(Symbol) }
|
|
142
|
+
elsif elems.any? { |v| v.is_a?(String) || v.is_a?(Symbol) || v.is_a?(Array) }
|
|
242
143
|
# A String is a value, which result_type would read as the name of a
|
|
243
|
-
# data type; a numeric self meets
|
|
144
|
+
# data type, and an Array has no data type; a numeric self meets them
|
|
145
|
+
# in the object lane.
|
|
244
146
|
[to_type(CA_OBJECT), elems.to_ca.to_type(CA_OBJECT)]
|
|
245
147
|
else
|
|
148
|
+
# true and false are the numbers 1 and 0 beside a numeric self, as
|
|
149
|
+
# they are to an operator; cast from an object array they would be
|
|
150
|
+
# masked cells instead.
|
|
151
|
+
unless data_type == CA_BOOLEAN
|
|
152
|
+
elems = elems.map { |v| v == true ? 1 : v == false ? 0 : v }
|
|
153
|
+
end
|
|
246
154
|
t = CArray.result_type(self, *elems)
|
|
247
155
|
[coerce_self(t), elems.to_ca.to_type(t)]
|
|
248
156
|
end
|
|
@@ -253,7 +161,7 @@ class CArray
|
|
|
253
161
|
# strings, and a categorical by its labels, since their storage is not
|
|
254
162
|
# their values; anything else, or the same kind of array as self, is
|
|
255
163
|
# taken as it is. A record is left to be refused.
|
|
256
|
-
def values_for_lookup
|
|
164
|
+
private def values_for_lookup(other)
|
|
257
165
|
return other if other.send(:orderable_storage?)
|
|
258
166
|
return other if face? && other.class == self.class
|
|
259
167
|
if other.respond_to?(:to_string)
|
|
@@ -271,19 +179,151 @@ class CArray
|
|
|
271
179
|
# it is held at. Raw fixlen cells of different widths are values of
|
|
272
180
|
# different lengths, not padded strings, and are left as they are (the
|
|
273
181
|
# lookup refuses them, naming the widths).
|
|
274
|
-
def fixlen_widened_with
|
|
182
|
+
private def fixlen_widened_with(other)
|
|
275
183
|
return [self, other] if bytes == other.bytes
|
|
276
184
|
return [self, other] unless is_a?(CAFixlenString) && other.is_a?(CAFixlenString)
|
|
277
185
|
w = [bytes, other.bytes].max
|
|
278
186
|
[widen_fixlen_string(self, w), widen_fixlen_string(other, w)]
|
|
279
187
|
end
|
|
280
188
|
|
|
281
|
-
def widen_fixlen_string
|
|
189
|
+
private def widen_fixlen_string(x, w)
|
|
282
190
|
x.bytes == w ? x : x.to_fixlen_string(bytes: w)
|
|
283
191
|
end
|
|
284
192
|
|
|
285
|
-
def coerce_self
|
|
193
|
+
private def coerce_self(t)
|
|
286
194
|
CArray.result_type(self) == t ? self : to_type(t)
|
|
287
195
|
end
|
|
288
196
|
|
|
197
|
+
# @overload is_in(values)
|
|
198
|
+
# Returns a boolean CArray of the same shape as `self`, `true` at
|
|
199
|
+
# each cell whose value appears in the set `values`.
|
|
200
|
+
#
|
|
201
|
+
# `values` is treated as a set, not as an operand to broadcast: it
|
|
202
|
+
# may be any shape (or an Array) and is flattened to a
|
|
203
|
+
# single seen-set, so its shape need not match `self`. Only one
|
|
204
|
+
# argument is accepted; to test a few immediate values pass an
|
|
205
|
+
# Array (`a.is_in([0, -1])`). A single value is a set of one, as
|
|
206
|
+
# `[value]` is (`a.is_in(2)` is `a.is_in([2])`).
|
|
207
|
+
#
|
|
208
|
+
# When `self` and `values` have different numeric data types they are
|
|
209
|
+
# promoted to a common type first (the same promotion binops use,
|
|
210
|
+
# {CArray.result_type}), so membership is value-correct across
|
|
211
|
+
# data types (e.g. an int cell equals a float set element of the same
|
|
212
|
+
# value, and a fractional set element never truncates onto an int
|
|
213
|
+
# cell). Integers of mixed sign are matched by value, as {#eq}
|
|
214
|
+
# compares them: `-1` in an int8 array never matches `255` in a uint8
|
|
215
|
+
# one, and the set operations answer such a pair in a type that holds
|
|
216
|
+
# both (int16 for uint8 with int8, object for uint64 with int64).
|
|
217
|
+
# Genuinely incompatible data types (e.g. numeric vs fixlen) raise.
|
|
218
|
+
#
|
|
219
|
+
# Membership is value-based and shares the distinctness of the
|
|
220
|
+
# value-hash discovery family ({#unique} / {#value_counts}):
|
|
221
|
+
# numeric follows `==` with all NaN collapsed to one value and
|
|
222
|
+
# -0.0 == +0.0; `CA_OBJECT` follows Ruby `hash` / `eql?` with Float
|
|
223
|
+
# NaN collapsed; `CA_FIXLEN` follows byte equality over the whole
|
|
224
|
+
# cell, with a short String in the set padded out to the cell width
|
|
225
|
+
# (so a 5-byte array written from `"be"` is `is_in(["be"])`). A
|
|
226
|
+
# `CA_FIXLEN` set given as a CArray must already be of that width.
|
|
227
|
+
#
|
|
228
|
+
# An object array keeps Ruby's own split between its two kinds of
|
|
229
|
+
# equality. A lookup through a hash -- `is_in`, the set operations,
|
|
230
|
+
# {#locate_addr}, {#unique} and the rest of the discovery family --
|
|
231
|
+
# uses `eql?`, as `Array#uniq`, `Array#&` and `Set` do, so `1` and
|
|
232
|
+
# `1.0` are different values there. A cell-by-cell comparison --
|
|
233
|
+
# {#eq}, {#count}, {#search}, {#bsearch} -- uses `==`, as
|
|
234
|
+
# `Array#include?` does, so they are equal there. To match by value
|
|
235
|
+
# across Integer and Float, convert the array to a numeric type first.
|
|
236
|
+
#
|
|
237
|
+
# Masked cells of `values` do not enter the set, and neither does `nil`
|
|
238
|
+
# unless `self` is an `:object` array, which can hold it. Masked cells
|
|
239
|
+
# of `self` stay masked in the result (membership is unknown), so
|
|
240
|
+
# `is_in` propagates `self`'s mask like an element-wise comparison.
|
|
241
|
+
#
|
|
242
|
+
# For a per-fiber "does this fiber contain any of these values"
|
|
243
|
+
# reduction, compose with {#any}: `a.is_in(values).any(axis: k)`.
|
|
244
|
+
#
|
|
245
|
+
# @param values [CArray, Array] the set to test membership
|
|
246
|
+
# against. Promoted with `self` to a common data type.
|
|
247
|
+
# @raise [ArgumentError] for a Range, which reads as an interval rather
|
|
248
|
+
# than the set of its steps.
|
|
249
|
+
# Between two time arrays the question is about instants, not ticks:
|
|
250
|
+
# `values` is reconciled into `self`'s unit first, so a `:D` array and an
|
|
251
|
+
# `:h` array match on the instants they share. The same holds for the set
|
|
252
|
+
# operations below, whose results come back as `self`'s own type.
|
|
253
|
+
#
|
|
254
|
+
# @return [CArray] boolean CArray of the same shape as `self`.
|
|
255
|
+
def is_in(values)
|
|
256
|
+
refuse_range_set(values, "is_in")
|
|
257
|
+
a, b = promote_value_set(values_of_set(values), lookup: true)
|
|
258
|
+
r = a.__send__(:__is_in__, b)
|
|
259
|
+
# A cell of self that the set's kind can not hold (a Float looked for
|
|
260
|
+
# among times) is masked in a, and is in no set.
|
|
261
|
+
if !a.equal?(self) && a.has_mask?
|
|
262
|
+
gone = has_mask? ? a.is_masked.and(is_not_masked) : a.is_masked
|
|
263
|
+
r[gone] = false
|
|
264
|
+
end
|
|
265
|
+
r
|
|
266
|
+
end
|
|
267
|
+
|
|
268
|
+
# @overload intersection(other, sort: false)
|
|
269
|
+
# Returns a 1-D CArray of the distinct values appearing in both
|
|
270
|
+
# `self` and `other`, in `self`'s first-appearance order.
|
|
271
|
+
#
|
|
272
|
+
# Value-based, sharing the distinctness of the discovery family
|
|
273
|
+
# (see {#is_in}); `self` and `other` are promoted to a common data type.
|
|
274
|
+
# Masked cells of either array do not participate. The result is
|
|
275
|
+
# always flat, like {#unique}, because the distinct values of a
|
|
276
|
+
# fiber vary in number.
|
|
277
|
+
#
|
|
278
|
+
# @param other [CArray, Array] promoted with `self` (a Range is refused, as by {#is_in}).
|
|
279
|
+
# @param sort [Boolean] when true, return the values sorted
|
|
280
|
+
# ascending instead of in first-appearance order (`nil` reads as
|
|
281
|
+
# false; any other value raises ArgumentError).
|
|
282
|
+
# @return [CArray] 1-D CArray of the common distinct values.
|
|
283
|
+
def intersection(other, sort: false)
|
|
284
|
+
sort = sort_flag(sort, "intersection")
|
|
285
|
+
refuse_range_set(other, "intersection")
|
|
286
|
+
a, b = promote_value_set(other)
|
|
287
|
+
r = a.__send__(:__intersection__, b)
|
|
288
|
+
r = r.to_unit(unit) if !a.equal?(self) && respond_to?(:in_common_unit_with, true)
|
|
289
|
+
sort ? r.sort : r
|
|
290
|
+
end
|
|
291
|
+
|
|
292
|
+
# @overload difference(other, sort: false)
|
|
293
|
+
# Returns a 1-D CArray of the distinct values in `self` that are
|
|
294
|
+
# absent from `other`, in `self`'s first-appearance order.
|
|
295
|
+
# See {#intersection} for the shared semantics and options.
|
|
296
|
+
#
|
|
297
|
+
# @param other [CArray, Array] promoted with `self` (a Range is refused, as by {#is_in}).
|
|
298
|
+
# @param sort [Boolean] when true, return the values sorted ascending.
|
|
299
|
+
# @return [CArray] 1-D CArray of the self-only distinct values.
|
|
300
|
+
def difference(other, sort: false)
|
|
301
|
+
sort = sort_flag(sort, "difference")
|
|
302
|
+
refuse_range_set(other, "difference")
|
|
303
|
+
a, b = promote_value_set(other)
|
|
304
|
+
r = a.__send__(:__difference__, b)
|
|
305
|
+
r = r.to_unit(unit) if !a.equal?(self) && respond_to?(:in_common_unit_with, true)
|
|
306
|
+
sort ? r.sort : r
|
|
307
|
+
end
|
|
308
|
+
|
|
309
|
+
# @overload union(other, sort: false)
|
|
310
|
+
# Returns a 1-D CArray of the distinct values appearing in either
|
|
311
|
+
# `self` or `other`, in self-then-other first-appearance order.
|
|
312
|
+
# See {#intersection} for the shared semantics and options. Two time
|
|
313
|
+
# arrays of different units give their union in the finer unit, which
|
|
314
|
+
# holds the values of both ({#intersection} and {#difference} answer in
|
|
315
|
+
# `self`'s unit, since their values are `self`'s).
|
|
316
|
+
#
|
|
317
|
+
# @param other [CArray, Array] promoted with `self` (a Range is refused, as by {#is_in}).
|
|
318
|
+
# @param sort [Boolean] when true, return the values sorted ascending
|
|
319
|
+
# (a merged, ordered set — e.g. a common time axis).
|
|
320
|
+
# @return [CArray] 1-D CArray of the combined distinct values.
|
|
321
|
+
def union(other, sort: false)
|
|
322
|
+
sort = sort_flag(sort, "union")
|
|
323
|
+
refuse_range_set(other, "union")
|
|
324
|
+
a, b = promote_value_set(other)
|
|
325
|
+
r = a.__send__(:__union__, b)
|
|
326
|
+
sort ? r.sort : r
|
|
327
|
+
end
|
|
328
|
+
|
|
289
329
|
end
|
data/lib/carray/methods/join.rb
CHANGED
|
@@ -33,7 +33,8 @@ class CArray
|
|
|
33
33
|
# @note The 2.x multi-separator form `a.join("\n", ",")` was removed
|
|
34
34
|
# in 3.0; use the axis form and chain, e.g.
|
|
35
35
|
# `a.join(",", axis: 1).join("\n")`.
|
|
36
|
-
def join
|
|
36
|
+
def join(*argv, axis: nil, keep_axis: false)
|
|
37
|
+
keep_axis = CArray.send(:bool_arg, keep_axis, "keep_axis", "join")
|
|
37
38
|
if argv.size > 1
|
|
38
39
|
raise ArgumentError,
|
|
39
40
|
"join accepts at most one positional separator " \
|
data/lib/carray/methods/lag.rb
CHANGED
|
@@ -4,7 +4,11 @@ class CArray
|
|
|
4
4
|
# Returns the value `n` steps earlier along `axis`, cell by cell: cell
|
|
5
5
|
# `i` of the result is cell `i - n` of `self`. A cell with no value `n`
|
|
6
6
|
# steps back is UNDEF, so a difference against it is UNDEF rather than
|
|
7
|
-
# a difference against a stand-in value
|
|
7
|
+
# a difference against a stand-in value. (`shift` is the view that
|
|
8
|
+
# moves the cells, and its cells outside take `fill_value`, 0 by
|
|
9
|
+
# default, for the uses that want 0 there; `lag` looks a value up, and
|
|
10
|
+
# a value that is not there is UNDEF, as it is for `project` and
|
|
11
|
+
# `windows`.)
|
|
8
12
|
#
|
|
9
13
|
# ```
|
|
10
14
|
# x = CA_FLOAT64([1.0, 4.0, 9.0])
|
|
@@ -29,7 +33,7 @@ class CArray
|
|
|
29
33
|
# @raise [TypeError] when `n` or `axis` is not an Integer.
|
|
30
34
|
# @raise [ArgumentError] when `axis` is out of range, or left out for an
|
|
31
35
|
# array of more than one dimension.
|
|
32
|
-
def lag
|
|
36
|
+
def lag(n = 1, axis: nil)
|
|
33
37
|
unless n.is_a?(Integer)
|
|
34
38
|
raise TypeError, "lag: n must be an Integer (got #{n.class})"
|
|
35
39
|
end
|