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/math.rb
CHANGED
|
@@ -6,16 +6,19 @@ module CAMath
|
|
|
6
6
|
# CArray by mkkernel. A CArray first argument answers through its own
|
|
7
7
|
# method, in its own data_type; any other first argument, and an
|
|
8
8
|
# integer CArray for a function defined only on floats, is taken as
|
|
9
|
-
# float64, so `CAMath.hypot(3, arr)` works.
|
|
9
|
+
# float64, so `CAMath.hypot(3, arr)` works. When no argument is a
|
|
10
|
+
# CArray, the answer is the one cell as a Ruby value
|
|
11
|
+
# (`CAMath.hypot(3, 4)` is `5.0`), as for the one-argument functions
|
|
12
|
+
# defined in C.
|
|
10
13
|
|
|
11
14
|
# @overload expm1(x)
|
|
12
15
|
# Returns `exp(x) - 1` element-wise. A CArray answers in its own
|
|
13
16
|
# `data_type` (integer input widens to float64, as {CArray#expm1}
|
|
14
17
|
# does); any other value is taken as float64.
|
|
15
18
|
# @param x [CArray, Numeric] input value.
|
|
16
|
-
# @return [CArray]
|
|
19
|
+
# @return [CArray, Numeric] a Ruby number when no argument is a CArray.
|
|
17
20
|
def expm1(x)
|
|
18
|
-
x.is_a?(CArray) ? x.expm1 :
|
|
21
|
+
x.is_a?(CArray) ? x.expm1 : number_answer(float_operand(x, "expm1").expm1)
|
|
19
22
|
end
|
|
20
23
|
|
|
21
24
|
# @overload log1p(x)
|
|
@@ -23,9 +26,9 @@ module CAMath
|
|
|
23
26
|
# `data_type` (integer input widens to float64, as {CArray#log1p}
|
|
24
27
|
# does); any other value is taken as float64.
|
|
25
28
|
# @param x [CArray, Numeric] input value.
|
|
26
|
-
# @return [CArray]
|
|
29
|
+
# @return [CArray, Numeric] a Ruby number when no argument is a CArray.
|
|
27
30
|
def log1p(x)
|
|
28
|
-
x.is_a?(CArray) ? x.log1p :
|
|
31
|
+
x.is_a?(CArray) ? x.log1p : number_answer(float_operand(x, "log1p").log1p)
|
|
29
32
|
end
|
|
30
33
|
|
|
31
34
|
# @overload atan2(y, x)
|
|
@@ -33,88 +36,122 @@ module CAMath
|
|
|
33
36
|
# selection.
|
|
34
37
|
# @param y [CArray, Numeric] numerator.
|
|
35
38
|
# @param x [CArray, Numeric] denominator.
|
|
36
|
-
# @return [CArray]
|
|
37
|
-
def atan2(y, x); float_operand(y).atan2(x); end
|
|
39
|
+
# @return [CArray, Numeric] a Ruby number when no argument is a CArray.
|
|
40
|
+
def atan2(y, x); number_answer(float_operand(y, "atan2").atan2(x), y, x); end
|
|
38
41
|
|
|
39
42
|
# @overload hypot(x, y)
|
|
40
43
|
# Returns the element-wise Euclidean distance `sqrt(x^2 + y^2)`.
|
|
41
44
|
# @param x [CArray, Numeric] first leg.
|
|
42
45
|
# @param y [CArray, Numeric] second leg.
|
|
43
|
-
# @return [CArray]
|
|
44
|
-
def hypot(x, y); float_operand(x).hypot(y); end
|
|
46
|
+
# @return [CArray, Numeric] a Ruby number when no argument is a CArray.
|
|
47
|
+
def hypot(x, y); number_answer(float_operand(x, "hypot").hypot(y), x, y); end
|
|
45
48
|
|
|
46
49
|
# @overload copysign(x, y)
|
|
47
50
|
# Returns `|x|` with the sign of `y`, element-wise.
|
|
48
51
|
# @param x [CArray, Numeric] magnitude source.
|
|
49
52
|
# @param y [CArray, Numeric] sign source.
|
|
50
|
-
# @return [CArray]
|
|
51
|
-
def copysign(x, y); float_operand(x).copysign(y); end
|
|
53
|
+
# @return [CArray, Numeric] a Ruby number when no argument is a CArray.
|
|
54
|
+
def copysign(x, y); number_answer(float_operand(x, "copysign").copysign(y), x, y); end
|
|
52
55
|
|
|
53
56
|
# @overload logaddexp(x, y)
|
|
54
57
|
# Returns `log(exp(x) + exp(y))` computed to avoid overflow,
|
|
55
58
|
# element-wise.
|
|
56
59
|
# @param x [CArray, Numeric] first log-space value.
|
|
57
60
|
# @param y [CArray, Numeric] second log-space value.
|
|
58
|
-
# @return [CArray]
|
|
59
|
-
def logaddexp(x, y); float_operand(x).logaddexp(y); end
|
|
61
|
+
# @return [CArray, Numeric] a Ruby number when no argument is a CArray.
|
|
62
|
+
def logaddexp(x, y); number_answer(float_operand(x, "logaddexp").logaddexp(y), x, y); end
|
|
60
63
|
|
|
61
64
|
# @overload nextafter(x, y)
|
|
62
65
|
# Returns the next representable float from `x` toward `y`,
|
|
63
66
|
# element-wise.
|
|
64
67
|
# @param x [CArray, Numeric] starting value.
|
|
65
68
|
# @param y [CArray, Numeric] direction target.
|
|
66
|
-
# @return [CArray]
|
|
67
|
-
def nextafter(x, y); float_operand(x).nextafter(y); end
|
|
69
|
+
# @return [CArray, Numeric] a Ruby number when no argument is a CArray.
|
|
70
|
+
def nextafter(x, y); number_answer(float_operand(x, "nextafter").nextafter(y), x, y); end
|
|
68
71
|
|
|
69
72
|
# @overload fmod(x, y)
|
|
70
73
|
# Returns the C-style `fmod(x, y)` element-wise (sign follows `x`).
|
|
71
74
|
# @param x [CArray, Numeric] dividend.
|
|
72
75
|
# @param y [CArray, Numeric] divisor.
|
|
73
|
-
# @return [CArray]
|
|
74
|
-
def fmod(x, y)
|
|
76
|
+
# @return [CArray, Numeric] a Ruby number when no argument is a CArray.
|
|
77
|
+
def fmod(x, y)
|
|
78
|
+
number_answer((x.is_a?(CArray) ? x : float_operand(x, "fmod")).fmod(y), x, y)
|
|
79
|
+
end
|
|
75
80
|
|
|
76
|
-
# A float or complex CArray as it is; anything else as float64.
|
|
77
|
-
|
|
78
|
-
|
|
81
|
+
# A float or complex CArray as it is; anything else as float64. A Ruby
|
|
82
|
+
# Array is refused, as it is in the other argument.
|
|
83
|
+
def float_operand(x, name)
|
|
84
|
+
return x if x.is_a?(CArray) && (x.float? || x.complex?)
|
|
85
|
+
CArray.send(:refuse_ruby_array, name, "an operand") if x.is_a?(Array)
|
|
86
|
+
CArray.wrap_readonly(x, :float64)
|
|
79
87
|
end
|
|
80
88
|
private_class_method :float_operand
|
|
81
89
|
|
|
82
|
-
#
|
|
83
|
-
#
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
90
|
+
# The answer out of a function: the one cell as a Ruby value when no
|
|
91
|
+
# operand is a CArray, else out.
|
|
92
|
+
def number_answer(out, *operands)
|
|
93
|
+
operands.any?(CArray) ? out : out[0]
|
|
94
|
+
end
|
|
95
|
+
private_class_method :number_answer
|
|
96
|
+
|
|
97
|
+
# A number as a one-cell CArray, as the one-argument functions defined
|
|
98
|
+
# in C take it: an Integer in the int64 range, a Float and a Complex in
|
|
99
|
+
# their own types, any other number as an object cell. A CArray is
|
|
100
|
+
# returned as it is; anything else is refused.
|
|
101
|
+
def number_operand(x, name)
|
|
102
|
+
return x if x.is_a?(CArray)
|
|
103
|
+
CArray.send(:refuse_ruby_array, name, "an operand") if x.is_a?(Array)
|
|
104
|
+
unless x.is_a?(Numeric)
|
|
105
|
+
raise TypeError, "#{name}: #{x.class} is not a number or a CArray"
|
|
106
|
+
end
|
|
107
|
+
if x.is_a?(Integer) && x.abs >= 2**63
|
|
108
|
+
CScalar.new(:object) { x }
|
|
95
109
|
else
|
|
96
|
-
|
|
110
|
+
CArray.cast(x)
|
|
97
111
|
end
|
|
98
|
-
|
|
112
|
+
end
|
|
113
|
+
private_class_method :number_operand
|
|
114
|
+
|
|
115
|
+
# @overload min(*argv)
|
|
116
|
+
# Returns the element-wise minimum of the arguments, compared as
|
|
117
|
+
# {CArray#pmin} compares them: the answer is in the type that `pmin`
|
|
118
|
+
# gives (`CAMath.min(CA_INT64([3]), 2.5)` is a float64 array of
|
|
119
|
+
# `2.5`). A number is taken as the one-argument functions take it.
|
|
120
|
+
# When no argument is a CArray, the answer is a Ruby number
|
|
121
|
+
# (`CAMath.min(3, 2.5)` is `2.5`).
|
|
122
|
+
# @param argv [Array<CArray, Numeric>] operands.
|
|
123
|
+
# @return [CArray, Numeric] a fresh CArray, or a Ruby number when no
|
|
124
|
+
# argument is a CArray.
|
|
125
|
+
# @raise [ArgumentError] when no argument is given.
|
|
126
|
+
# @raise [TypeError] when an argument is neither a number nor a CArray.
|
|
127
|
+
def min(*argv)
|
|
128
|
+
fold_operands(argv, :pmin, "min")
|
|
99
129
|
end
|
|
100
130
|
|
|
101
131
|
# @overload max(*argv)
|
|
102
|
-
# Returns the element-wise maximum of the
|
|
103
|
-
#
|
|
132
|
+
# Returns the element-wise maximum of the arguments, compared as
|
|
133
|
+
# {CArray#pmax} compares them: the answer is in the type that `pmax`
|
|
134
|
+
# gives (`CAMath.max(CA_INT64([3]), 2.5)` is a float64 array of
|
|
135
|
+
# `3.0`). A number is taken as the one-argument functions take it.
|
|
136
|
+
# When no argument is a CArray, the answer is a Ruby number
|
|
137
|
+
# (`CAMath.max(3, 2.5)` is `3.0`).
|
|
104
138
|
# @param argv [Array<CArray, Numeric>] operands.
|
|
105
|
-
# @return [CArray] fresh CArray
|
|
106
|
-
#
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
139
|
+
# @return [CArray, Numeric] a fresh CArray, or a Ruby number when no
|
|
140
|
+
# argument is a CArray.
|
|
141
|
+
# @raise [ArgumentError] when no argument is given.
|
|
142
|
+
# @raise [TypeError] when an argument is neither a number nor a CArray.
|
|
143
|
+
def max(*argv)
|
|
144
|
+
fold_operands(argv, :pmax, "max")
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
# The operands of min / max folded left to right with op (pmin or pmax).
|
|
148
|
+
def fold_operands(argv, op, name)
|
|
149
|
+
raise ArgumentError, "#{name}: no operand is given" if argv.empty?
|
|
150
|
+
operands = argv.map { |x| number_operand(x, name) }
|
|
151
|
+
out = operands.drop(1).inject(operands.first) { |acc, x| acc.send(op, x) }
|
|
152
|
+
out = out.copy if operands.size == 1
|
|
153
|
+
number_answer(out, *argv)
|
|
118
154
|
end
|
|
155
|
+
private_class_method :fold_operands
|
|
119
156
|
|
|
120
157
|
end
|
data/lib/carray/meld_reduce.rb
CHANGED
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
# stay on the SRC_ATTACH path, which is already essentially parity because
|
|
29
29
|
# the sort cost dominates.
|
|
30
30
|
#
|
|
31
|
-
# See docs/objects/CAMeld.md
|
|
31
|
+
# See docs/objects/CAMeld.md.
|
|
32
32
|
|
|
33
33
|
class CAMeld
|
|
34
34
|
|
|
@@ -37,10 +37,9 @@ class CAMeld
|
|
|
37
37
|
def sum(*args, **kw)
|
|
38
38
|
return super unless args.empty? && meld_reduce_fast_path_ok?(kw)
|
|
39
39
|
axis = meld_fast_path_axis(kw)
|
|
40
|
-
if axis.nil?
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
parents.map { |p| p.sum(axis: axis) }.inject(:+)
|
|
40
|
+
if axis.nil? || meld_axis_normalized?(axis)
|
|
41
|
+
total = meld_sum_of_parents(axis)
|
|
42
|
+
total.nil? ? super : total
|
|
44
43
|
else
|
|
45
44
|
non_meld_axis_decompose(:sum, axis)
|
|
46
45
|
end
|
|
@@ -62,12 +61,14 @@ class CAMeld
|
|
|
62
61
|
# where none does, and never raises.
|
|
63
62
|
if axis.nil?
|
|
64
63
|
return super if parents.all? { |p| p.elements == 0 }
|
|
65
|
-
total_sum =
|
|
64
|
+
total_sum = meld_sum_of_parents(nil)
|
|
65
|
+
return super if total_sum.nil?
|
|
66
66
|
total_count = parents.map(&:elements).inject(:+)
|
|
67
67
|
total_sum / total_count.to_f
|
|
68
68
|
elsif meld_axis_normalized?(axis)
|
|
69
69
|
return super if parents.all? { |p| p.shape[meld_axis] == 0 }
|
|
70
|
-
total_sum =
|
|
70
|
+
total_sum = meld_sum_of_parents(axis)
|
|
71
|
+
return super if total_sum.nil?
|
|
71
72
|
total_count = parents.map { |p| p.shape[meld_axis] }.inject(:+)
|
|
72
73
|
total_sum / total_count.to_f
|
|
73
74
|
else
|
|
@@ -195,6 +196,26 @@ class CAMeld
|
|
|
195
196
|
ndim == 1 ? nil : kw[:axis]
|
|
196
197
|
end
|
|
197
198
|
|
|
199
|
+
# The sum along the meld axis (of the whole array when +axis+ is nil),
|
|
200
|
+
# from the sums of the parents. An integer array's sum is int64 (uint64
|
|
201
|
+
# when unsigned) while it fits and float64 past that range, so the
|
|
202
|
+
# parents' integer sums are added by the core's own sum over them; `+`
|
|
203
|
+
# would wrap. nil when a parent's own sum is already past the range: its
|
|
204
|
+
# float64 total is no longer exact, and the caller takes the generic path.
|
|
205
|
+
def meld_sum_of_parents(axis)
|
|
206
|
+
parts = axis.nil? ? parents.map(&:sum) : parents.map { |p| p.sum(axis: axis) }
|
|
207
|
+
return parts.inject(:+) unless integer? || boolean?
|
|
208
|
+
if axis.nil?
|
|
209
|
+
return nil unless parts.all?(Integer)
|
|
210
|
+
unsigned = boolean? || [CA_UINT8, CA_UINT16, CA_UINT32, CA_UINT64].include?(data_type)
|
|
211
|
+
(unsigned ? CA_UINT64(parts) : CA_INT64(parts)).sum
|
|
212
|
+
else
|
|
213
|
+
types = parts.map(&:data_type).uniq
|
|
214
|
+
return nil unless types.size == 1 && [CA_INT64, CA_UINT64].include?(types.first)
|
|
215
|
+
CArray.stack(parts).sum(axis: 0)
|
|
216
|
+
end
|
|
217
|
+
end
|
|
218
|
+
|
|
198
219
|
# The per-parent answers of a whole-array min / max, combined by core's
|
|
199
220
|
# own min / max so NaN loses the way it loses inside one parent (Ruby's
|
|
200
221
|
# Array#min would raise on it).
|
|
@@ -218,7 +239,8 @@ class CAMeld
|
|
|
218
239
|
parts = parents.map { |p| p.public_send(op, axis: axis) }
|
|
219
240
|
axis_norm = normalize_axis(axis)
|
|
220
241
|
new_meld_axis = axis_norm < meld_axis ? meld_axis - 1 : meld_axis
|
|
221
|
-
CArray.
|
|
242
|
+
joined = CArray.send(:integer_total_join_type, parts)
|
|
243
|
+
CArray.meld(parts, axis: new_meld_axis, data_type: joined).copy
|
|
222
244
|
end
|
|
223
245
|
|
|
224
246
|
def variance_family(args, kw, sample:, sqrt:)
|
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
class CArray
|
|
2
2
|
|
|
3
|
+
# An array to align: a CArray as it is, a Ruby Array refused (it has no
|
|
4
|
+
# data type, and to_ca would make it an object array, where 2 and 2.0 are
|
|
5
|
+
# two values), anything else through its to_ca.
|
|
6
|
+
def self.align_operand(a, name)
|
|
7
|
+
return a if a.is_a?(CArray)
|
|
8
|
+
refuse_ruby_array(name, "an array to align") if a.is_a?(Array)
|
|
9
|
+
a.to_ca
|
|
10
|
+
end
|
|
11
|
+
private_class_method :align_operand
|
|
12
|
+
|
|
3
13
|
# @overload align_addr(*arrays, join: :outer)
|
|
4
14
|
# Aligns several arrays onto one common set of coordinate values and
|
|
5
15
|
# returns, for each array, the flat addresses that gather it onto that
|
|
@@ -36,16 +46,17 @@ class CArray
|
|
|
36
46
|
# equality). Arrays are coerced to the first array's data type within the same
|
|
37
47
|
# family (cross-family raises). Masked cells do not enter `common`.
|
|
38
48
|
#
|
|
39
|
-
# @param arrays [Array<CArray>] two or more arrays (
|
|
40
|
-
#
|
|
41
|
-
# values).
|
|
49
|
+
# @param arrays [Array<CArray>] two or more arrays (a Range is coerced via
|
|
50
|
+
# `to_ca`; a Ruby Array is refused, as it has no data type). One array is
|
|
51
|
+
# allowed (degenerate: `common` is its distinct values).
|
|
42
52
|
# @param join [:outer, :inner, :left, :right] how to build `common`.
|
|
43
53
|
# @return [Array<CArray>] `[common, idx_0, idx_1, ...]`.
|
|
44
54
|
# @raise [ArgumentError] when no array is given or `join` is not one of the
|
|
45
55
|
# accepted symbols.
|
|
46
|
-
|
|
56
|
+
# @raise [TypeError] when an array is a Ruby Array.
|
|
57
|
+
def self.align_addr(*arrays, join: :outer)
|
|
47
58
|
raise ArgumentError, "align_addr: need at least one array" if arrays.empty?
|
|
48
|
-
arrays = arrays.map { |a|
|
|
59
|
+
arrays = arrays.map { |a| align_operand(a, "align_addr") }
|
|
49
60
|
# Seed the fold with the first array's distinct values so N == 1 and the
|
|
50
61
|
# union/intersection folds all agree (a bare reduce over one element would
|
|
51
62
|
# return it with duplicates intact).
|
|
@@ -64,13 +75,13 @@ class CArray
|
|
|
64
75
|
[common, *idxs]
|
|
65
76
|
end
|
|
66
77
|
|
|
67
|
-
# @overload align_nearest_addr(*arrays, grid: nil, direction: :round,
|
|
78
|
+
# @overload align_nearest_addr(*arrays, grid: nil, direction: :round, tol: nil)
|
|
68
79
|
# Aligns several arrays onto one common coordinate grid by nearest match,
|
|
69
80
|
# the ordered-lane (continuous) sibling of {align_addr}. Returns
|
|
70
81
|
# `[common, idx_0, idx_1, ...]` with the same reindex contract: `idx_k` is a
|
|
71
82
|
# `common`-shaped `:int64` array of flat addresses into `arrays[k]` giving,
|
|
72
83
|
# for each grid point, the nearest value in that array — exactly
|
|
73
|
-
# `common.locate_nearest_addr(arrays[k], direction:,
|
|
84
|
+
# `common.locate_nearest_addr(arrays[k], direction:, tol:)`.
|
|
74
85
|
#
|
|
75
86
|
# Unlike {align_addr}, the common grid is **not** built by a set union:
|
|
76
87
|
# continuous coordinates rarely coincide exactly, so a union would merely
|
|
@@ -84,35 +95,36 @@ class CArray
|
|
|
84
95
|
# a_on_grid = a_data.project(ia) # each grid point <- nearest a value
|
|
85
96
|
# b_on_grid = b_data.project(ib)
|
|
86
97
|
#
|
|
87
|
-
# `direction:` (`:round` / `:floor` / `:ceil`) and `
|
|
98
|
+
# `direction:` (`:round` / `:floor` / `:ceil`) and `tol:` are
|
|
88
99
|
# forwarded to {#locate_nearest_addr}: grid points with no match in the
|
|
89
100
|
# direction (before an array's first value under `:floor`, after its last
|
|
90
|
-
# under `:ceil`), and points whose match is farther than `
|
|
101
|
+
# under `:ceil`), and points whose match is farther than `tol`, come
|
|
91
102
|
# back masked.
|
|
92
103
|
#
|
|
93
|
-
# @param arrays [Array<CArray>] one or more arrays (
|
|
94
|
-
#
|
|
95
|
-
# @param grid [CArray,
|
|
104
|
+
# @param arrays [Array<CArray>] one or more arrays (a Range is coerced via
|
|
105
|
+
# `to_ca`; a Ruby Array is refused, as it has no data type).
|
|
106
|
+
# @param grid [CArray, Range, nil] the reference coordinate grid;
|
|
96
107
|
# `nil` uses the first array verbatim.
|
|
97
108
|
# @param direction [:round, :floor, :ceil] rounding rule for the nearest
|
|
98
109
|
# match.
|
|
99
|
-
# @param
|
|
110
|
+
# @param tol [Numeric, String, CATimedelta::Element, nil] maximum
|
|
100
111
|
# accepted distance; farther grid points are masked. For time keys it
|
|
101
112
|
# is a duration (`"10 minutes"` or a CATimedelta value), as in
|
|
102
113
|
# {#locate_nearest_addr}. `nil` disables the check.
|
|
103
114
|
# @return [Array<CArray>] `[common, idx_0, idx_1, ...]`.
|
|
104
115
|
# @raise [ArgumentError] when no array is given (or `direction` is invalid,
|
|
105
116
|
# raised by {#locate_nearest_addr}).
|
|
106
|
-
|
|
117
|
+
# @raise [TypeError] when an array or `grid` is a Ruby Array.
|
|
118
|
+
def self.align_nearest_addr(*arrays, grid: nil, direction: :round, tol: nil)
|
|
107
119
|
raise ArgumentError, "align_nearest_addr: need at least one array" if arrays.empty?
|
|
108
|
-
arrays = arrays.map { |a|
|
|
120
|
+
arrays = arrays.map { |a| align_operand(a, "align_nearest_addr") }
|
|
109
121
|
common = if grid.nil?
|
|
110
122
|
arrays.first
|
|
111
123
|
else
|
|
112
|
-
|
|
124
|
+
align_operand(grid, "align_nearest_addr")
|
|
113
125
|
end
|
|
114
126
|
idxs = arrays.map { |a|
|
|
115
|
-
common.locate_nearest_addr(a, direction: direction,
|
|
127
|
+
common.locate_nearest_addr(a, direction: direction, tol: tol)
|
|
116
128
|
}
|
|
117
129
|
[common, *idxs]
|
|
118
130
|
end
|
data/lib/carray/methods/bin.rb
CHANGED
|
@@ -46,6 +46,7 @@ class CArray
|
|
|
46
46
|
# temp.bin(0, 1, bins: 100) # 100 equal-width bins over [0, 1]
|
|
47
47
|
# temp.bin(0, 9, 1, lfill: 0, ufill: 8) # clamp OOB to end bins
|
|
48
48
|
def bin(vmin, vmax, step = nil, bins: nil, lfill: nil, ufill: nil, include_max: true)
|
|
49
|
+
include_max = CArray.send(:bool_arg, include_max, "include_max", "bin")
|
|
49
50
|
if step.nil? == bins.nil?
|
|
50
51
|
raise ArgumentError, "bin: give exactly one of `step` or `bins:`"
|
|
51
52
|
end
|
|
@@ -98,13 +99,18 @@ class CArray
|
|
|
98
99
|
# @param ufill [Integer, nil] fill for above-range cells; `nil`
|
|
99
100
|
# masks them.
|
|
100
101
|
# @param include_max [Boolean] fold values equal to `edges[-1]`
|
|
101
|
-
# into the last bin instead of treating them as above-range.
|
|
102
|
+
# into the last bin instead of treating them as above-range. Off by
|
|
103
|
+
# default: the edges are bin bounds, each bin half-open (`bin`, which
|
|
104
|
+
# takes a range `[vmin, vmax]`, includes `vmax` by default).
|
|
102
105
|
# @return [CArray] `CA_INT64` array with the same shape as `self`
|
|
103
106
|
# holding bin indices in `[0, N-1]` (or the fill values / mask
|
|
104
107
|
# for out-of-range and masked cells).
|
|
105
108
|
# @raise [ArgumentError] when `edges` is not 1-D, has fewer than 2
|
|
106
109
|
# values, has a masked value or NaN, or decreases anywhere. Equal
|
|
107
110
|
# edges (a bin of zero width) and infinite ones are accepted.
|
|
111
|
+
# Values and edges are compared as float64, so int64 values beyond
|
|
112
|
+
# 2**53 are compared at that precision.
|
|
113
|
+
#
|
|
108
114
|
# @example
|
|
109
115
|
# e = CA_FLOAT64([0, 1, 10, 100])
|
|
110
116
|
# v = CA_FLOAT64([0.5, 5.0, 50.0, -1.0, 200.0])
|
|
@@ -112,6 +118,7 @@ class CArray
|
|
|
112
118
|
# v.bin_to(e, lfill: 0, ufill: 2)
|
|
113
119
|
# # => [0, 1, 2, 0, 2]
|
|
114
120
|
def bin_to(edges, lfill: nil, ufill: nil, include_max: false)
|
|
121
|
+
include_max = CArray.send(:bool_arg, include_max, "include_max", "bin_to")
|
|
115
122
|
e = CArray.wrap_readonly(edges, :float64)
|
|
116
123
|
raise ArgumentError, "bin_to: edges must be 1-D" unless e.ndim == 1
|
|
117
124
|
raise ArgumentError, "bin_to: edges needs at least 2 values" if e.elements < 2
|
|
@@ -27,8 +27,8 @@ class CArray
|
|
|
27
27
|
# labels = CA_INT32([0, 1, 1, 2, 0, 1])
|
|
28
28
|
# labels.bincount # => CA_UINT32([2, 3, 1])
|
|
29
29
|
# labels.bincount(length: 5) # => CA_UINT32([2, 3, 1, 0, 0])
|
|
30
|
-
# weights =
|
|
31
|
-
# labels.bincount(weights: weights) # =>
|
|
30
|
+
# weights = CA_FLOAT64([1, 2, 3, 4, 5, 6])
|
|
31
|
+
# labels.bincount(weights: weights) # => CA_FLOAT64([6, 11, 4])
|
|
32
32
|
def bincount(weights: nil, length: 0)
|
|
33
33
|
unless [CA_INT8, CA_INT16, CA_INT32, CA_INT64,
|
|
34
34
|
CA_UINT8, CA_UINT16, CA_UINT32, CA_UINT64].include?(data_type)
|
|
@@ -42,6 +42,7 @@ class CArray
|
|
|
42
42
|
raise ArgumentError, "bincount: length: must not be negative (got #{length})"
|
|
43
43
|
end
|
|
44
44
|
unless weights.nil?
|
|
45
|
+
CArray.send(:refuse_ruby_array, "bincount", "a weight array") if weights.is_a?(Array)
|
|
45
46
|
unless weights.is_a?(CArray)
|
|
46
47
|
raise ArgumentError, "bincount: weights must be a CArray"
|
|
47
48
|
end
|
|
@@ -86,6 +87,7 @@ class CArray
|
|
|
86
87
|
n = [length, label_max + 1].max
|
|
87
88
|
|
|
88
89
|
if weights
|
|
90
|
+
CArray.send(:refuse_ruby_array, "bincount", "a weight array") if weights.is_a?(Array)
|
|
89
91
|
unless weights.is_a?(CArray)
|
|
90
92
|
raise ArgumentError, "bincount: weights must be a CArray"
|
|
91
93
|
end
|
|
@@ -8,44 +8,32 @@ class CArray
|
|
|
8
8
|
# bits per element.
|
|
9
9
|
# @param nb [Integer] bits per element.
|
|
10
10
|
# @return [String] byte string of length `ceil(nb * elements / 8)`.
|
|
11
|
-
def to_bit_string
|
|
11
|
+
def to_bit_string(nb)
|
|
12
12
|
hex = CArray.uint8(((nb*elements)/8.0).ceil)
|
|
13
|
-
hex.
|
|
14
|
-
hex.
|
|
15
|
-
return hex.
|
|
13
|
+
hex.bitarray[nil].paste([0], self.bitarray[false, (nb-1)..0].flatten)
|
|
14
|
+
hex.bitarray[] = hex.bitarray[nil, -1..0]
|
|
15
|
+
return hex.dump_binary
|
|
16
16
|
end
|
|
17
17
|
|
|
18
|
-
# @overload from_bit_string(bstr, nb)
|
|
19
|
-
# Sets `self` by unpacking `bstr` as a packed-bit byte string
|
|
20
|
-
# with `nb` bits per element.
|
|
21
|
-
# @param bstr [String] packed byte string.
|
|
22
|
-
# @param nb [Integer] bits per element.
|
|
23
|
-
# @return [self]
|
|
24
|
-
def from_bit_string (bstr, nb)
|
|
25
|
-
hex = CArray.uint8(bstr.length).load_binary(bstr)
|
|
26
|
-
hex.bits[] = hex.bits[nil,[-1..0]]
|
|
27
|
-
bits = hex.bits.flatten
|
|
28
|
-
self.bits[false,[(nb-1)..0]][nil].paste([0], bits)
|
|
29
|
-
return self
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
# @overload from_bit_string(bstr, nb, data_type = CA_INT32, dim = nil)
|
|
18
|
+
# @overload from_bit_string(bstr, nb, data_type: :int32, shape: nil)
|
|
33
19
|
# Returns a new CArray built by unpacking `bstr` as a packed-bit
|
|
34
|
-
# byte string with `nb` bits per element
|
|
20
|
+
# byte string with `nb` bits per element, the inverse of
|
|
21
|
+
# {#to_bit_string}.
|
|
35
22
|
# @param bstr [String] packed byte string.
|
|
36
23
|
# @param nb [Integer] bits per element.
|
|
37
|
-
# @param data_type [Symbol
|
|
38
|
-
# @param
|
|
39
|
-
#
|
|
24
|
+
# @param data_type [Symbol] result `data_type`.
|
|
25
|
+
# @param shape [Array<Integer>, nil] result shape. When `nil` the
|
|
26
|
+
# result is 1-D with `floor(bstr.bytesize * 8 / nb)` elements, so the
|
|
27
|
+
# padding bits of the last byte also become elements; pass `shape:`
|
|
28
|
+
# to get back exactly the elements that were packed.
|
|
40
29
|
# @return [CArray]
|
|
41
|
-
def self.from_bit_string
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
obj.from_bit_string(bstr, nb)
|
|
30
|
+
def self.from_bit_string(bstr, nb, data_type: :int32, shape: nil)
|
|
31
|
+
shape ||= [((bstr.bytesize*8)/nb.to_f).floor]
|
|
32
|
+
obj = CArray.new(data_type, shape)
|
|
33
|
+
hex = CArray.uint8(bstr.bytesize).load_binary(bstr)
|
|
34
|
+
hex.bitarray[] = hex.bitarray[nil, -1..0]
|
|
35
|
+
bits = hex.bitarray.flatten
|
|
36
|
+
obj.bitarray[false, (nb-1)..0][nil].paste([0], bits)
|
|
49
37
|
return obj
|
|
50
38
|
end
|
|
51
39
|
|
|
@@ -5,7 +5,8 @@ class CArray
|
|
|
5
5
|
# yields the broadcast tuple to a block.
|
|
6
6
|
#
|
|
7
7
|
# Non-CArray arguments (Float / Integer) pass through unchanged
|
|
8
|
-
# unless `expand_scalar` is true.
|
|
8
|
+
# unless `expand_scalar` is true. A Ruby Array is refused: it has no
|
|
9
|
+
# data type, so make it a CArray of the type you mean first. CScalar instances are treated
|
|
9
10
|
# as ndim-0 scalars and excluded from the ndim check.
|
|
10
11
|
#
|
|
11
12
|
# All real CArray arguments must share the same `ndim`; implicit
|
|
@@ -22,7 +23,9 @@ class CArray
|
|
|
22
23
|
# @return [Array<CArray, Numeric>] broadcast arguments, or the
|
|
23
24
|
# block's return value when a block is given.
|
|
24
25
|
# @raise [ArgumentError] when CArray arguments differ in `ndim`.
|
|
25
|
-
|
|
26
|
+
# @raise [TypeError] when an argument is a Ruby Array.
|
|
27
|
+
def CArray.broadcast(*argv, expand_scalar: false, &block)
|
|
28
|
+
expand_scalar = CArray.send(:bool_arg, expand_scalar, "expand_scalar", "broadcast")
|
|
26
29
|
|
|
27
30
|
sel = argv.select { |arg| arg.is_a?(CArray) && !arg.is_a?(CScalar) }
|
|
28
31
|
return argv if sel.empty?
|
|
@@ -51,8 +54,10 @@ class CArray
|
|
|
51
54
|
expand_scalar ? arg.broadcast_to(*dim) : arg
|
|
52
55
|
when CArray
|
|
53
56
|
arg.broadcast_to(*dim)
|
|
57
|
+
when Array
|
|
58
|
+
CArray.send(:refuse_ruby_array, "broadcast", "an array to broadcast")
|
|
54
59
|
else
|
|
55
|
-
|
|
60
|
+
arg
|
|
56
61
|
end
|
|
57
62
|
end
|
|
58
63
|
|
|
@@ -21,13 +21,13 @@ class CArray
|
|
|
21
21
|
# @raise [IndexError] if a label that is not masked is negative or
|
|
22
22
|
# not less than the number of choices.
|
|
23
23
|
# @example
|
|
24
|
-
# ref =
|
|
25
|
-
# a = CArray.
|
|
26
|
-
# b = CArray.
|
|
27
|
-
# c = CArray.
|
|
24
|
+
# ref = CA_INT32([[0, 1, 2], [1, 2, 0], [2, 0, 1]])
|
|
25
|
+
# a = CArray.int32(3, 3).seq(1)
|
|
26
|
+
# b = CArray.int32(3, 3).seq(11)
|
|
27
|
+
# c = CArray.int32(3, 3).seq(21)
|
|
28
28
|
# ref.choose([a, b, c]) # per-cell pick from a / b / c
|
|
29
29
|
# ref.choose(["a", "b", "c"]) # recode labels to values
|
|
30
|
-
def choose
|
|
30
|
+
def choose(choices, data_type: nil)
|
|
31
31
|
outside = (self.lt(0) | self.ge(choices.size)).where
|
|
32
32
|
if outside.elements > 0
|
|
33
33
|
addr = outside[0]
|