carray 3.0.3 → 3.0.5

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.
Files changed (221) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +469 -0
  3. data/README.md +3 -2
  4. data/carray.gemspec +1 -1
  5. data/ext/ca_array_pool.c +25 -1
  6. data/ext/ca_axis_descriptor.h +27 -24
  7. data/ext/ca_axis_dispatch.c +14 -26
  8. data/ext/ca_axis_group.c +27 -27
  9. data/ext/ca_binop_dispatch.c +1 -29
  10. data/ext/ca_binop_dispatch.h +0 -1
  11. data/ext/ca_categorical_iterator.c +231 -153
  12. data/ext/ca_compare.c +9 -20
  13. data/ext/ca_composite_dispatch.c +9 -9
  14. data/ext/ca_group_iter.c +16 -17
  15. data/ext/ca_kernel_iterator.c +1240 -2000
  16. data/ext/ca_kernel_iterator.h +502 -519
  17. data/ext/ca_monop_dispatch.c +82 -59
  18. data/ext/ca_monop_dispatch.h +1 -0
  19. data/ext/ca_obj_array.c +152 -45
  20. data/ext/ca_obj_bincmp.c +25 -21
  21. data/ext/ca_obj_binop.c +16 -26
  22. data/ext/ca_obj_bitarray.c +8 -3
  23. data/ext/ca_obj_bitfield.c +63 -49
  24. data/ext/ca_obj_block.c +18 -9
  25. data/ext/ca_obj_byte_swap.c +7 -7
  26. data/ext/ca_obj_const_string.c +188 -22
  27. data/ext/ca_obj_face.c +280 -58
  28. data/ext/ca_obj_face.h +19 -0
  29. data/ext/ca_obj_fake.c +15 -29
  30. data/ext/ca_obj_farray.c +6 -3
  31. data/ext/ca_obj_field.c +30 -8
  32. data/ext/ca_obj_fixlen_string.c +2 -3
  33. data/ext/ca_obj_grid.c +455 -134
  34. data/ext/ca_obj_meld.c +172 -34
  35. data/ext/ca_obj_moncmp.c +23 -8
  36. data/ext/ca_obj_monop.c +113 -67
  37. data/ext/ca_obj_object.c +210 -52
  38. data/ext/ca_obj_record.c +15 -7
  39. data/ext/ca_obj_reduce.c +17 -8
  40. data/ext/ca_obj_refer.c +42 -35
  41. data/ext/ca_obj_remap.c +30 -9
  42. data/ext/ca_obj_repeat.c +9 -10
  43. data/ext/ca_obj_roll.c +31 -10
  44. data/ext/ca_obj_select.c +48 -14
  45. data/ext/ca_obj_select_axis.c +15 -47
  46. data/ext/ca_obj_shift.c +41 -7
  47. data/ext/ca_obj_stack.c +180 -34
  48. data/ext/ca_obj_stride.c +190 -134
  49. data/ext/ca_obj_string.c +80 -4
  50. data/ext/ca_obj_tile.c +32 -11
  51. data/ext/ca_obj_time.c +22 -6
  52. data/ext/ca_obj_timedelta.c +22 -6
  53. data/ext/ca_obj_transpose.c +13 -7
  54. data/ext/ca_obj_triop.c +26 -32
  55. data/ext/ca_obj_window.c +267 -74
  56. data/ext/ca_op_ipower.c +4 -4
  57. data/ext/ca_sort_kernels.h +6 -3
  58. data/ext/ca_sweep_engine.c +2 -2
  59. data/ext/caframe_csv_reader.c +80 -1
  60. data/ext/carray.h +214 -75
  61. data/ext/carray_access.c +200 -99
  62. data/ext/carray_address_basis.c +343 -87
  63. data/ext/carray_attribute.c +113 -6
  64. data/ext/carray_bincount.c +10 -8
  65. data/ext/carray_broadcast.c +13 -8
  66. data/ext/carray_build_flags.h +3 -0
  67. data/ext/carray_call_cfunc.c +108 -0
  68. data/ext/carray_cast.c +375 -96
  69. data/ext/carray_cast_func.rb +90 -20
  70. data/ext/carray_class.c +4 -3
  71. data/ext/carray_config.h +28 -0
  72. data/ext/carray_conversion.c +18 -9
  73. data/ext/carray_copy.c +51 -14
  74. data/ext/carray_core.c +150 -55
  75. data/ext/carray_count.c +56 -20
  76. data/ext/carray_element.c +81 -33
  77. data/ext/carray_factorize.c +138 -84
  78. data/ext/carray_generate.c +199 -52
  79. data/ext/carray_histogram.c +63 -18
  80. data/ext/carray_hold.c +12 -15
  81. data/ext/carray_index_classifier.c +388 -176
  82. data/ext/carray_internal.h +348 -29
  83. data/ext/carray_kernels_bincmp.c +4453 -0
  84. data/ext/carray_kernels_binop.c +11368 -0
  85. data/ext/carray_kernels_init.c +1734 -0
  86. data/ext/carray_kernels_moncmp.c +2104 -0
  87. data/ext/carray_kernels_monop.c +18369 -0
  88. data/ext/carray_kernels_reduce_aggregate.c +23818 -0
  89. data/ext/carray_kernels_reduce_boolean.c +406 -0
  90. data/ext/carray_kernels_reduce_cumulative.c +10086 -0
  91. data/ext/carray_kernels_reduce_extreme.c +17634 -0
  92. data/ext/carray_kernels_reduce_variance.c +3849 -0
  93. data/ext/carray_kernels_scan.c +4302 -0
  94. data/ext/carray_kernels_search.c +33829 -0
  95. data/ext/carray_kernels_sort.c +10897 -0
  96. data/ext/carray_kernels_triop.c +1864 -0
  97. data/ext/carray_key_windows.c +220 -0
  98. data/ext/carray_lazy.c +170 -111
  99. data/ext/carray_mask.c +337 -94
  100. data/ext/carray_median_percentile.c +82 -28
  101. data/ext/carray_memory_view.c +669 -561
  102. data/ext/carray_operator.c +129 -52
  103. data/ext/carray_order.c +604 -77
  104. data/ext/carray_partition.c +28 -27
  105. data/ext/carray_random.c +85 -31
  106. data/ext/carray_round.c +273 -0
  107. data/ext/carray_scatter.c +87 -45
  108. data/ext/carray_segment.c +3 -3
  109. data/ext/carray_slab.c +123 -37
  110. data/ext/carray_sort.c +259 -51
  111. data/ext/carray_sort_kernel.c +5 -7
  112. data/ext/carray_struct.c +54 -61
  113. data/ext/carray_test.c +26 -5
  114. data/ext/carray_time_text.c +4 -2
  115. data/ext/carray_undef.c +4 -0
  116. data/ext/carray_utils.c +309 -66
  117. data/ext/mk_call_cfunc.rb +19 -2
  118. data/ext/mkkernel.rb +403 -350
  119. data/ext/ruby_carray.c +10 -4
  120. data/ext/version.h +4 -4
  121. data/lib/carray/attribute.rb +139 -22
  122. data/lib/carray/autoload_carray.rb +8 -31
  123. data/lib/carray/autoload_method_extension.rb +14 -5
  124. data/lib/carray/axis_group.rb +255 -106
  125. data/lib/carray/basics.rb +105 -17
  126. data/lib/carray/bincount_nd.rb +12 -2
  127. data/lib/carray/block_iterator.rb +126 -23
  128. data/lib/carray/boolean_reduce.rb +17 -17
  129. data/lib/carray/categorical.rb +195 -59
  130. data/lib/carray/categorical_iterator.rb +132 -29
  131. data/lib/carray/complex.rb +5 -5
  132. data/lib/carray/conditional.rb +67 -18
  133. data/lib/carray/const_string.rb +24 -5
  134. data/lib/carray/construct.rb +15 -11
  135. data/lib/carray/data_type_extension.rb +9 -1
  136. data/lib/carray/distance_limit.rb +77 -0
  137. data/lib/carray/fixlen_string.rb +48 -3
  138. data/lib/carray/frame/column_verbs.rb +71 -0
  139. data/lib/carray/frame/concat.rb +17 -3
  140. data/lib/carray/frame/convert.rb +69 -0
  141. data/lib/carray/frame/csv_parser.rb +111 -17
  142. data/lib/carray/frame/describe.rb +3 -2
  143. data/lib/carray/frame/frame.rb +252 -28
  144. data/lib/carray/frame/group.rb +603 -101
  145. data/lib/carray/frame/io.rb +117 -28
  146. data/lib/carray/frame/join.rb +312 -51
  147. data/lib/carray/frame/records.rb +83 -7
  148. data/lib/carray/frame/reshape.rb +42 -35
  149. data/lib/carray/frame/sort.rb +7 -19
  150. data/lib/carray/frame/verbs.rb +188 -48
  151. data/lib/carray/frame/windows.rb +117 -0
  152. data/lib/carray/frame.rb +2 -0
  153. data/lib/carray/fuse_source.rb +42 -6
  154. data/lib/carray/fusion.rb +40 -16
  155. data/lib/carray/histogram.rb +6 -1
  156. data/lib/carray/inspect.rb +31 -8
  157. data/lib/carray/iterator.rb +85 -7
  158. data/lib/carray/key_windows.rb +620 -0
  159. data/lib/carray/lazy.rb +217 -183
  160. data/lib/carray/mask_gap_fill.rb +63 -57
  161. data/lib/carray/meld_reduce.rb +12 -14
  162. data/lib/carray/methods/bin.rb +26 -18
  163. data/lib/carray/methods/bincount.rb +16 -0
  164. data/lib/carray/methods/bit_string.rb +1 -1
  165. data/lib/carray/methods/choose.rb +16 -3
  166. data/lib/carray/methods/composition.rb +3 -1
  167. data/lib/carray/methods/gather_nd.rb +14 -5
  168. data/lib/carray/methods/insert_block.rb +2 -2
  169. data/lib/carray/methods/is_between.rb +59 -0
  170. data/lib/carray/methods/is_in.rb +61 -37
  171. data/lib/carray/methods/join.rb +1 -6
  172. data/lib/carray/methods/lag.rb +49 -0
  173. data/lib/carray/methods/locate_addr.rb +47 -51
  174. data/lib/carray/methods/mode.rb +1 -1
  175. data/lib/carray/methods/nunique.rb +11 -6
  176. data/lib/carray/methods/resize.rb +3 -3
  177. data/lib/carray/methods/value_counts.rb +4 -11
  178. data/lib/carray/record_order.rb +3 -3
  179. data/lib/carray/rng.rb +3 -0
  180. data/lib/carray/runtime.rb +53 -8
  181. data/lib/carray/segment_iterator.rb +323 -88
  182. data/lib/carray/serialize.rb +45 -14
  183. data/lib/carray/slab_iterator.rb +50 -0
  184. data/lib/carray/stack.rb +15 -12
  185. data/lib/carray/string.rb +113 -5
  186. data/lib/carray/string_operation_extension.rb +21 -6
  187. data/lib/carray/struct.rb +9 -21
  188. data/lib/carray/struct_builder.rb +24 -11
  189. data/lib/carray/time.rb +819 -158
  190. data/lib/carray/window_iterator.rb +122 -16
  191. data/lib/carray.rb +7 -0
  192. data/yard-stubs/ca_obj_bitfield.rb +1 -1
  193. data/yard-stubs/ca_obj_field.rb +4 -0
  194. data/yard-stubs/ca_obj_grid.rb +21 -10
  195. data/yard-stubs/ca_obj_refer.rb +2 -1
  196. data/yard-stubs/ca_obj_roll.rb +2 -2
  197. data/yard-stubs/ca_obj_shift.rb +5 -1
  198. data/yard-stubs/ca_obj_tile.rb +1 -1
  199. data/yard-stubs/ca_obj_transpose.rb +5 -4
  200. data/yard-stubs/ca_obj_window.rb +8 -3
  201. data/yard-stubs/carray_access.rb +6 -1
  202. data/yard-stubs/carray_attribute.rb +3 -6
  203. data/yard-stubs/carray_cast.rb +12 -0
  204. data/yard-stubs/carray_conversion.rb +7 -2
  205. data/yard-stubs/carray_copy.rb +4 -2
  206. data/yard-stubs/carray_count.rb +4 -4
  207. data/yard-stubs/carray_element.rb +17 -10
  208. data/yard-stubs/carray_generate.rb +21 -1
  209. data/yard-stubs/carray_mask.rb +32 -3
  210. data/yard-stubs/carray_math.rb +41 -5
  211. data/yard-stubs/carray_median_percentile.rb +5 -2
  212. data/yard-stubs/carray_memory_view.rb +40 -23
  213. data/yard-stubs/carray_order.rb +129 -22
  214. data/yard-stubs/carray_random.rb +13 -4
  215. data/yard-stubs/carray_scatter.rb +17 -10
  216. data/yard-stubs/carray_slab.rb +9 -2
  217. data/yard-stubs/carray_sort.rb +6 -5
  218. data/yard-stubs/carray_test.rb +5 -3
  219. metadata +25 -3
  220. data/lib/carray/arrow_tensor.rb +0 -401
  221. data/lib/carray/table.rb +0 -108
@@ -8,8 +8,12 @@ class CArray
8
8
  # @overload where
9
9
  # Returns a fresh 1-D CArray of flat addresses where `self` is
10
10
  # non-zero (or `true`). Masked positions are excluded.
11
- # Non-boolean arrays are first coerced to boolean.
11
+ # An integer, float or complex array selects its non-zero cells
12
+ # (NaN is non-zero). Any other array that is not boolean is cast to
13
+ # boolean, which takes only `0`, `1`, `true` and `false`.
12
14
  # @return [CArray] 1-D, `:int64` (`ca_size_t`).
15
+ # @raise [CArray::DataTypeError, TypeError] if an array that is not
16
+ # boolean, integer, float or complex cannot be cast to boolean.
13
17
  # @example
14
18
  # CArray.int32(5) { [0, 1, 0, 2, 0] }.where.to_a
15
19
  # # => [1, 3]
@@ -32,8 +36,15 @@ class CArray
32
36
  # Fills `self` in row-major order with `init_val`, `init_val +
33
37
  # step`, `init_val + 2*step`, ...
34
38
  #
39
+ # On an integer array an `init_val` or `step` that is not an Integer
40
+ # (a Float, a Rational) makes each cell `init_val + step*i` cut
41
+ # toward zero, as a store takes a float: `int32.seq(0.5, 0.5)` is
42
+ # `[0, 1, 1, 2]`. The first and last values must fit the type.
43
+ #
35
44
  # For `:object` arrays only, `step` may be a Symbol naming the
36
45
  # stepping method to invoke on the previous element (e.g. `:succ`).
46
+ # A CAString takes only a run of Strings (`s.seq!("a", :succ)`), as a
47
+ # store into it does.
37
48
  # @param init_val [Numeric, Object]
38
49
  # @param step [Numeric, Symbol]
39
50
  # @return [self]
@@ -49,6 +60,15 @@ class CArray
49
60
  # @return [self]
50
61
  # @raise [ArgumentError] if `axis` is out of range or not a single
51
62
  # integer.
63
+ # @raise [RangeError] for an integer array, if `init_val` does not fit
64
+ # its type, or an Integer `step` is larger than the type can hold. An
65
+ # unsigned array takes a negative step (a descending run). A run of
66
+ # Integers that passes the end of the type wraps, as integer
67
+ # arithmetic does; a real run (a Float or Rational argument) whose
68
+ # first or last value does not fit raises.
69
+ # @raise [FloatDomainError] for an integer array given a NaN argument.
70
+ # @raise [TypeError] if `init_val` or `step` is not a number (a Symbol
71
+ # `step` on an `:object` array excepted).
52
72
  def seq!(*); end
53
73
 
54
74
  # @overload seq(init_val = 0, step = 1, axis: nil)
@@ -27,6 +27,13 @@ class CArray
27
27
  # mask (`value_array? == true`).
28
28
  #
29
29
  # Use this to read or write the data slot of masked elements.
30
+ # Nothing in a value array is masked, so `unmask` on it does nothing,
31
+ # while a write that would mask a cell (`UNDEF`, `mask=`,
32
+ # `invert_mask`) raises TypeError.
33
+ #
34
+ # On a Face the data slot is read through the Face. A {CACategorical}
35
+ # cell excluded by the sentinel code has no label, so its value reads
36
+ # as `nil` (the codes are in `codes`).
30
37
  # @return [CArray]
31
38
  def value; end
32
39
 
@@ -35,6 +42,11 @@ class CArray
35
42
  # element is `1` where `self` is masked and `0` where it is not.
36
43
  # The returned view itself cannot carry a mask.
37
44
  #
45
+ # The mask of a view is a view over its parent's mask: it follows
46
+ # later changes to the parent's mask, and so does its `dup` or
47
+ # `clone`, which is another view onto the same parent mask (a write
48
+ # to it lands in the parent's mask). Use `copy` for a mask of its own.
49
+ #
38
50
  # Returns `0` (Integer) if `self` has no mask array attached.
39
51
  # @return [CArray, Integer]
40
52
  def mask; end
@@ -43,6 +55,12 @@ class CArray
43
55
  # Sets the mask array of `self` to `new_mask`. Allocates the mask
44
56
  # array first if `self` does not yet have one. Cannot be called on
45
57
  # a value array or a mask array.
58
+ #
59
+ # `mask=` replaces the whole mask with `new_mask`. A masked cell of
60
+ # `new_mask` makes the cell masked, as a masked value does in any
61
+ # store. To mask the cells where a condition holds, prefer
62
+ # `self[cond] = UNDEF`. There a masked cell of `cond` selects nothing,
63
+ # as it does in any index.
46
64
  # @param new_mask [CArray, Boolean, Integer] mask values. A
47
65
  # non-CArray value is broadcast and stored elementwise; a CArray
48
66
  # of any type is coerced to boolean.
@@ -91,7 +109,11 @@ class CArray
91
109
  # linearly by index from the two bracketing valid cells (numeric
92
110
  # data_type, or a time Face — `CATime` / `CATimedelta` interpolate
93
111
  # through their own `linear_fetch`, so the filled values stay on the
94
- # array's unit and are rounded to it).
112
+ # array's unit and are rounded to it). An integer array takes the
113
+ # nearest integer, half away from zero (`[190, _, _, 341]` fills
114
+ # `240, 291`). The interpolation runs in float64, so in an int64
115
+ # array with values beyond 2**53 the filled cells may be off by the
116
+ # float64 spacing there; the valid cells are not touched.
95
117
  # @param axis [Integer, nil] scan axis (single axis). `nil` flattens.
96
118
  # @return [self]
97
119
  def unmask(*, method: nil, axis: nil); end
@@ -162,8 +184,11 @@ class CArray
162
184
  # of `self` and the masks of each array in `others`. Mutates
163
185
  # `self`.
164
186
  # @param others [Array<CArray>] arrays whose mask states are
165
- # OR-ed into `self`. Non-CArray arguments are ignored.
187
+ # OR-ed into `self`.
166
188
  # @return [self]
189
+ # @raise [ArgumentError] if an array in `others` other than a scalar
190
+ # has a different number of elements from `self`.
191
+ # @raise [TypeError] if an argument is not a CArray.
167
192
  def inherit_mask(*others); end
168
193
 
169
194
  # @overload inherit_mask_replace(*others)
@@ -173,6 +198,8 @@ class CArray
173
198
  # @param others [Array<CArray>] arrays whose mask states form the
174
199
  # new mask of `self`.
175
200
  # @return [self]
201
+ # @raise [ArgumentError] if an array in `others` other than a scalar
202
+ # has a different number of elements from `self`.
176
203
  def inherit_mask_replace(*others); end
177
204
 
178
205
  # @overload count_masked
@@ -226,7 +253,9 @@ class CArray
226
253
  # In-place equivalent: `ca[:eq, v] = UNDEF`.
227
254
  #
228
255
  # Replaces the removed `maskout(v)` from 2.x.
229
- # @param v [Object] value to mask. Cast to `self.data_type`.
256
+ # `v` is compared as {#eq} compares it, by value: `1.5` masks nothing
257
+ # in an integer array, and `-1` does not mask `255` in a `uint8` one.
258
+ # @param v [Object] value to mask.
230
259
  # @return [CArray]
231
260
  def mask_eq(v); end
232
261
 
@@ -11,6 +11,11 @@
11
11
  # builds a node in the expression tree instead of evaluating, and the
12
12
  # eager and lazy paths are required to agree element for element.
13
13
  #
14
+ # The transcendental functions call the platform's C math library, so
15
+ # their last bits, and the complex functions' answers at special values
16
+ # (signed zeros, infinities, NaN), follow that library and can differ
17
+ # between operating systems.
18
+ #
14
19
  # See yard-stubs/README.md and yard-stubs/STYLE.md.
15
20
 
16
21
  class CArray
@@ -125,7 +130,33 @@ class CArray
125
130
  # keeps its sign. Masked elements stay masked. An `:object`
126
131
  # array delegates to each element's `#round`.
127
132
  # @return [CArray] same `data_type` as `self`.
128
- def round; end
133
+ # @overload round(ndigits)
134
+ # Returns a new array with each element rounded to `ndigits` decimal
135
+ # digits, to the value Ruby's `Float#round(ndigits)` gives for a float
136
+ # element and `Integer#round(ndigits)` for an integer one, half away
137
+ # from zero: `CA_FLOAT64([1.005, 2.675]).round(2)` is `[1.01, 2.68]`,
138
+ # as `1.005.round(2)` and `2.675.round(2)` are. A negative `ndigits`
139
+ # rounds to tens, hundreds, ...: `CA_INT32([125, -125]).round(-1)` is
140
+ # `[130, -130]`. The result keeps `self`'s `data_type`, so a float
141
+ # array stays float for every `ndigits`, and a `:float32` element is
142
+ # rounded from its own value. `NaN` and the infinities are left as
143
+ # they are. Masked elements stay masked. An `:object` array calls
144
+ # each element's `round(ndigits)`. `round(0)` is `round`.
145
+ # @param ndigits [Integer] decimal digits to keep; negative rounds to
146
+ # the left of the decimal point.
147
+ # @return [CArray] same `data_type` as `self`.
148
+ # @raise [TypeError] if `ndigits` is not an Integer.
149
+ # @raise [RangeError] if a rounded integer does not fit the
150
+ # `data_type` (`CA_INT8([125]).round(-1)`).
151
+ # @raise [CArray::DataTypeError] for a complex or fixed-length array.
152
+ def round(ndigits = nil); end
153
+
154
+ # @overload round!(ndigits = nil)
155
+ # Rounds `self` in place, as {#round} does. A boolean array can not
156
+ # hold the rounded values and raises when `ndigits` is given.
157
+ # @param ndigits [Integer, nil] decimal digits to keep.
158
+ # @return [self]
159
+ def round!(ndigits = nil); end
129
160
 
130
161
  # @overload frac
131
162
  # Returns a new array of the fractional part of each element:
@@ -572,6 +603,8 @@ class CArray
572
603
  # Returns each element of `self` with the magnitude it already has
573
604
  # and the sign of the matching element of `other`. The sign of a
574
605
  # zero is respected, so `copysign` distinguishes `-0.0` from `0.0`.
606
+ # The sign of a NaN that a computation produced (`sqrt(-1)`,
607
+ # `Inf * 0`) depends on the CPU: x86 sets it, ARM does not.
575
608
  # Accepts float and `:object` arrays only, as {#atan2}.
576
609
  # @param other [CArray, Numeric] sign source, broadcast to
577
610
  # `self.shape`.
@@ -657,8 +690,8 @@ class CArray
657
690
  # @overload pmax(other)
658
691
  # Returns the element-wise larger of `self` and `other`, ignoring
659
692
  # `NaN`: where one element is `NaN` the other is returned, and only
660
- # two `NaN`s give `NaN`. This is C's `fmax`. For the form that lets
661
- # a `NaN` win see {#maximum}.
693
+ # two `NaN`s give `NaN`. Of `-0.0` and `0.0` the result is `0.0`,
694
+ # in either order. For the form that lets a `NaN` win see {#maximum}.
662
695
  # @param other [CArray, Numeric] the other operand, broadcast to
663
696
  # `self.shape`.
664
697
  # @return [CArray] result of the promoted `data_type` of the two
@@ -671,7 +704,8 @@ class CArray
671
704
 
672
705
  # @overload pmin(other)
673
706
  # Returns the element-wise smaller of `self` and `other`, ignoring
674
- # `NaN`. The counterpart of {#pmax}.
707
+ # `NaN`. Of `-0.0` and `0.0` the result is `-0.0`, in either order.
708
+ # The counterpart of {#pmax}.
675
709
  # @param other [CArray, Numeric] the other operand.
676
710
  # @return [CArray]
677
711
  # @raise [CArray::DataTypeError] for a complex array.
@@ -903,7 +937,9 @@ class CArray
903
937
  # @overload signbit
904
938
  # Returns a boolean array that is true where an element carries a
905
939
  # negative sign bit. A negative zero is true, which distinguishes
906
- # `-0.0` from `0.0` where a `< 0` comparison cannot.
940
+ # `-0.0` from `0.0` where a `< 0` comparison cannot. On a NaN that a
941
+ # computation produced (`sqrt(-1)`, `Inf * 0`) the answer depends on
942
+ # the CPU: x86 sets the sign bit, ARM does not.
907
943
  # @return [CArray] `:boolean`.
908
944
  # @raise [CArray::DataTypeError] for a complex array.
909
945
  # @example
@@ -27,7 +27,9 @@ class CArray
27
27
  # `UNDEF` when `fill_value` is `nil`).
28
28
  #
29
29
  # With `keep_axis: true`, the reduced axis is retained with
30
- # length 1 rather than dropped.
30
+ # length 1 rather than dropped. Along the only axis of a 1-D array
31
+ # the result is a bare value, as `sum(axis: 0)` is (a one-cell array
32
+ # with `keep_axis: true`).
31
33
  # @param axis [Integer, nil]
32
34
  # @param min_count [Integer] minimum not-masked count required.
33
35
  # @param fill_value [Object, nil] replacement when the not-masked
@@ -47,7 +49,8 @@ class CArray
47
49
  # `p` may be given as individual positional arguments, a single
48
50
  # `Array<Numeric>`, or a single 1-D `CArray`. When the effective
49
51
  # `p` count (after flattening) is 1, the result is unwrapped:
50
- # flat form returns a `Float`, per-axis form returns a `CArray`.
52
+ # flat form returns a `Float`, per-axis form returns a `CArray`
53
+ # (a bare value along the only axis of a 1-D array, as {#median}).
51
54
  # With 2 or more `p` values the result is an `Array` whose length
52
55
  # matches the number of requested `p` values.
53
56
  #
@@ -7,13 +7,14 @@ class CArray
7
7
  # @!group MemoryView interop
8
8
 
9
9
  # @overload memory_view_available?(obj)
10
- # Returns `true` if `obj` exposes the Ruby `rb_memory_view`
11
- # protocol and a view can be acquired from it. Lightweight probe
12
- # used to gate {.from_memory_view} / {.wrap_memory_view} calls.
10
+ # Returns `true` if `obj` is a MemoryView producer (Ruby's
11
+ # `rb_memory_view_available_p`). Lightweight probe used to gate
12
+ # {.from_memory_view} / {.wrap_memory_view} calls; it does not
13
+ # acquire a view.
13
14
  #
14
- # Returning `true` does not guarantee acquisition will succeed —
15
- # a producer may still refuse a particular flag combination at
16
- # acquisition time.
15
+ # Returning `true` does not guarantee the import succeeds — the
16
+ # producer may still refuse the view, or offer a format CArray
17
+ # does not read.
17
18
  # @param obj [Object] any object (typically a foreign array such
18
19
  # as `Numo::NArray`, `Arrow::Array`, a `bytearray`, or a
19
20
  # CArray).
@@ -26,6 +27,7 @@ class CArray
26
27
  # @overload memory_view_reject_reason(obj)
27
28
  # Returns a String explaining why CArray's MemoryView producer
28
29
  # refuses to export `obj`, or `nil` if no problem is detected.
30
+ # For an object that is not a CArray it returns `nil`.
29
31
  #
30
32
  # Diagnostic only; useful when {.memory_view_available?} returns
31
33
  # `false` on a CArray and the generic "memory view not available"
@@ -34,8 +36,8 @@ class CArray
34
36
  # (mask propagation, non-contiguous strided view, etc.) call this
35
37
  # to print a human-readable reason.
36
38
  # @param obj [Object]
37
- # @return [String, nil] reason text, or `nil` when acquisition
38
- # would succeed.
39
+ # @return [String, nil] reason text, or `nil` when the export
40
+ # would succeed or `obj` is not a CArray.
39
41
  def memory_view_reject_reason(obj); end
40
42
 
41
43
  # @overload from_memory_view(src, data_type: nil, mask: nil)
@@ -46,15 +48,20 @@ class CArray
46
48
  #
47
49
  # For typeless producers (format `nil`, e.g. byte blobs from
48
50
  # `IO#read`), `data_type:` is required and the byte buffer is
49
- # reinterpreted as that data type with `ndim = 1`. For typed
51
+ # reinterpreted as that data type with `ndim = 1`; a typeless
52
+ # producer whose own layout is strided is refused. For typed
50
53
  # producers `data_type:` is optional; when given it must match
51
54
  # the producer's format.
52
55
  #
53
56
  # `mask:` accepts a paired buffer-protocol source whose shape
54
- # matches `src` and whose item size is 1 (PEP 3118 `?` / `B` /
55
- # `b`). The mask bytes are copied into the canonical
57
+ # matches `src` and whose item size is 1 (`?` / `B` / `C` /
58
+ # `b` / `c`). The mask bytes are copied into the canonical
56
59
  # `CA_BOOLEAN` mask slot with non-zero coerced to `1`. The copy
57
- # is independent of both buffers after the call.
60
+ # is independent of both buffers after the call. A boolean
61
+ # producer's bytes other than 0 and 1 are read as true.
62
+ #
63
+ # The result is a plain `CArray` whatever the receiver; a
64
+ # receiver other than `CArray` or a `CAWrap` raises `TypeError`.
58
65
  # @param src [Object] any MemoryView producer.
59
66
  # @param data_type [Symbol, Integer, Class, nil] target data
60
67
  # type. Required for typeless producers; optional (must match
@@ -63,7 +70,11 @@ class CArray
63
70
  # source. Must match `src.shape` and use a 1-byte/element
64
71
  # format.
65
72
  # @return [CArray] independent copy; not a `CAWrap`.
73
+ # @raise [TypeError] if the receiver is neither `CArray` nor a
74
+ # `CAWrap`.
66
75
  # @raise [ArgumentError] if `src` is not a MemoryView producer,
76
+ # if its shape is negative, too large, or does not fit in the
77
+ # bytes it reports,
67
78
  # if `data_type:` is missing for a typeless source, if
68
79
  # `data_type:` conflicts with the producer's format, or if
69
80
  # the supplied `mask:` fails shape / data type / contiguity
@@ -92,19 +103,23 @@ class CArray
92
103
  # when the wrap is garbage-collected.
93
104
  #
94
105
  # Contiguous typed producers return a `CAWrap`. Strided typed
95
- # producers return a `CAStride` layered on an inner `CAWrap` of
96
- # the producer's first byte, preserving the full stride pattern
106
+ # producers return a `CAStride` over a byte `CAWrap` of the
107
+ # bytes its elements reach, preserving the full stride pattern
97
108
  # (including negative strides where the producer offers them).
98
109
  # Typeless producers (format `nil`) require `data_type:` and are
99
- # reinterpreted with `ndim = 1`.
110
+ # reinterpreted with `ndim = 1`; a strided typeless producer is
111
+ # refused. A boolean producer must hold 0 or 1 in every byte.
100
112
  #
101
113
  # `mask:` accepts a paired buffer-protocol source whose shape
102
- # matches `src` and whose item size is 1 (PEP 3118 `?` / `B` /
103
- # `b`). The mask buffer is borrowed; writes to either side
104
- # propagate, and the mask MemoryView is released when the wrap
105
- # is collected. In this release, `mask:` requires a row-major
106
- # contiguous data source and a row-major contiguous mask
107
- # buffer.
114
+ # matches `src` and whose item size is 1 (`?` / `B` / `C` /
115
+ # `b` / `c`), holding 0 or 1 in every byte. The mask buffer is
116
+ # borrowed; writes to either side propagate, and the mask
117
+ # MemoryView is released when the wrap is collected. In this
118
+ # release, `mask:` requires a row-major contiguous data source
119
+ # and a row-major contiguous mask buffer that shares no bytes
120
+ # with the data. A read-only mask makes the whole array
121
+ # read-only, since a store writes the mask cell of each element
122
+ # it stores.
108
123
  #
109
124
  # The receiver picks the class of the result. Called on `CArray`
110
125
  # it builds a `CAWrap`; called on a subclass of `CAWrap` it
@@ -132,10 +147,12 @@ class CArray
132
147
  # @raise [TypeError] if the receiver is neither `CArray` nor a
133
148
  # subclass of `CAWrap`.
134
149
  # @raise [ArgumentError] if `src` is not a MemoryView producer,
135
- # if `data_type:` is missing for a typeless source, if
150
+ # if its shape is negative, too large, or does not fit in the
151
+ # bytes it reports, if a boolean buffer holds a byte other than
152
+ # 0 and 1, if `data_type:` is missing for a typeless source, if
136
153
  # `data_type:` conflicts with the producer's format, or if
137
154
  # the supplied `mask:` fails validation (shape / ndim /
138
- # data type / item_size / contiguity). Also raises when `mask:`
155
+ # data type / item_size / contiguity / overlap with the data). Also raises when `mask:`
139
156
  # is combined with a strided data source — that combination
140
157
  # is not supported in this release.
141
158
  # @example Zero-copy write propagation
@@ -12,16 +12,15 @@ class CArray
12
12
  #
13
13
  # Out-of-range positions are filled with `lval` (below-range) and
14
14
  # `uval` (above-range); when `lval` is given alone, it fills
15
- # both. Without either, out-of-range positions are masked.
15
+ # both. Without either, out-of-range positions are masked, and so
16
+ # they are with a fill of `UNDEF`.
16
17
  #
17
18
  # When `self` is a Face (CATime / CATimedelta / CACategorical),
18
- # the result is the same Face type, carrying the unit / labels; the
19
- # gather runs on the underlying storage and misses become UNDEF. Fill
20
- # (`lval` / `uval`) is not supported for a Face and raises ArgumentError
21
- # — omit the fill args to get UNDEF at misses.
19
+ # the result is the same Face type, carrying the unit / labels, and a
20
+ # fill is one of its values (a time for a CATime).
22
21
  # @param idx [CArray] flat addresses, of any shape.
23
- # @param lval [Object, nil] fill for indices `< 0` (non-Face only).
24
- # @param uval [Object, nil] fill for indices `>= self.elements` (non-Face only).
22
+ # @param lval [Object, nil] fill for indices `< 0`.
23
+ # @param uval [Object, nil] fill for indices `>= self.elements`.
25
24
  # @return [CArray] a fresh array shaped like `idx`; a Face of the same
26
25
  # type when `self` is a Face.
27
26
  # @raise [ArgumentError] if `lval` / `uval` is given and `self` is a Face.
@@ -97,17 +96,19 @@ class CArray
97
96
  # `method:` selects how ties are ranked:
98
97
  #
99
98
  # - `:ordinal` (default) — every cell gets a distinct rank; ties
100
- # are broken by original position (stable). This is a total
101
- # order: no two cells ever compare equal.
102
- # - `:dense` — tied values share one rank (no gaps). Useful as a
103
- # `sort_addr` priority key: an `:ordinal` key never ties, so a
104
- # lower-priority key after it is never consulted; a `:dense` key
99
+ # are broken by original position (stable), the earlier cell first
100
+ # in both directions. This is a total order: no two cells ever
101
+ # compare equal.
102
+ # - `:dense` — tied values share one rank (no gaps). Every NaN is
103
+ # one value and shares one rank, placed after the numbers. Useful
104
+ # as a `sort_addr` priority key: an `:ordinal` key never ties, so
105
+ # a lower-priority key after it is never consulted; a `:dense` key
105
106
  # preserves the tie so it is.
106
107
  #
107
- # `descending:` negates the rank order (`(n-1) - rank`, `n` =
108
- # fiber element count minus masked cells). This composes correctly
109
- # with `method: :dense`: ties stay tied after the transform, and
110
- # group-to-group order still reverses.
108
+ # `descending:` ranks the largest value `0`; NaN comes after the
109
+ # numbers in both directions. Tied cells keep their
110
+ # original order under `:ordinal` and share a rank under `:dense`, so
111
+ # dense ranks stay without gaps.
111
112
  #
112
113
  # Masked cells are excluded from ranking (own cell becomes UNDEF,
113
114
  # like `rank_index`); `descending:`/`method:` apply to the
@@ -127,7 +128,8 @@ class CArray
127
128
 
128
129
  # @overload bsearch(val)
129
130
  # Returns the flat address of `val` in `self` via binary search.
130
- # `self` must be sorted along its single (flat) axis. Returns
131
+ # `self` must be sorted along its single (flat) axis. Of several
132
+ # equal cells, the first one's address is returned. Returns
131
133
  # `nil` if `val` is not present. When `val` is a CArray, returns
132
134
  # a CArray of `:int64` addresses (one per element of `val`),
133
135
  # with `UNDEF` at positions where the value is not present.
@@ -158,16 +160,27 @@ class CArray
158
160
  # @overload search(val, eps = nil)
159
161
  # Returns the flat address of the first element of `self` equal
160
162
  # to `val`. For float types, `eps` (default machine epsilon)
161
- # sets the tolerance. Returns `nil` if no match.
163
+ # sets the tolerance; a negative or NaN `eps` raises
164
+ # `ArgumentError`. Returns `nil` if no match.
162
165
  #
163
166
  # `val` is compared in the type both share, as `self.eq(val)`
164
167
  # compares: an integer array searched with a Float is compared in
165
- # float64 (so `3.9` does not match `3`, and `eps` applies), and an
166
- # Integer that does not fit an integer array raises `RangeError`.
168
+ # float64 (so `3.9` does not match `3`, and `eps` applies), a pair of
169
+ # mixed sign is compared by value (`-1` does not find `255`), and an
170
+ # Integer an integer array cannot hold matches nothing.
167
171
  # The same holds for {#bsearch} and {#search_nearest}. On an
168
172
  # `:object` array a cell matches when it is `==` to `val`, as {#eq}
169
173
  # compares (so `1` matches `1.0`, which {#is_in} does not).
170
174
  #
175
+ # An `UNDEF` `val`, or a masked cell of a CArray `val`, finds
176
+ # nothing: `nil`, or `UNDEF` in that cell of the result. The same
177
+ # holds for {#bsearch} and {#search_nearest}.
178
+ #
179
+ # A time array (`CATime`, `CATimedelta`) takes a value of another
180
+ # unit, a `Time` or a String: when it is off the array's grid the
181
+ # two are compared in the finer unit, so it matches nothing (and
182
+ # {#search_nearest} finds the nearest cell).
183
+ #
171
184
  # `self` need not be sorted; this is a linear scan. For sorted
172
185
  # data prefer {#bsearch}.
173
186
  # @param val [Object]
@@ -190,7 +203,10 @@ class CArray
190
203
 
191
204
  # @overload search_nearest(val)
192
205
  # Returns the flat address of the element of `self` whose value
193
- # is closest to `val`.
206
+ # is closest to `val`. Of several equally close elements, the one
207
+ # at the lowest address wins, as {#search} answers the first match
208
+ # (`[0, 2, 4].search_nearest(1)` is 0). {#locate_nearest_addr}
209
+ # rounds a position instead, and answers the larger value on a tie.
194
210
  #
195
211
  # In an `:object` array the distance is `val.distance(other)` when
196
212
  # `val` answers `#distance`, and `(val - other).abs` when it is a
@@ -257,7 +273,9 @@ class CArray
257
273
  # `self` is coerced to `:float64` if it is not already. When
258
274
  # `axis: nil`, `self` is flattened to 1-D first.
259
275
  #
260
- # Out-of-range `addr` returns NaN.
276
+ # A position with no answer -- out of range, or between samples one
277
+ # of which is NaN -- gives `nil` for a scalar `addr` and NaN in the
278
+ # cell of an array result.
261
279
  #
262
280
  # Because this half of the pair returns a *value* rather than a
263
281
  # position, a Face axis gets its Face back: `CATime#linear_fetch` /
@@ -269,5 +287,94 @@ class CArray
269
287
  # @see file:docs/topics/LinearInterpolation.md
270
288
  def linear_fetch(addr, axis: nil); end
271
289
 
290
+ # @overload nlargest(n, axis: nil)
291
+ # Returns the `n` largest values, largest first. Without `axis:` the
292
+ # array is read flat; with it, each fiber along `axis` gives its own
293
+ # `n` and the axis keeps length `n`. A NaN and a masked cell lose to
294
+ # every value; when fewer cells remain than `n`, the result is
295
+ # shorter (or, per fiber, ends in UNDEF).
296
+ # @param n [Integer] how many values, 0 or more.
297
+ # @param axis [Integer, nil]
298
+ # @return [CArray]
299
+ # @raise [ArgumentError] when `n` is negative.
300
+ def nlargest(n, axis: nil); end
301
+
302
+ # @overload nsmallest(n, axis: nil)
303
+ # Returns the `n` smallest values, smallest first. As {#nlargest}
304
+ # otherwise.
305
+ # @param n [Integer]
306
+ # @param axis [Integer, nil]
307
+ # @return [CArray]
308
+ # @raise [ArgumentError] when `n` is negative.
309
+ def nsmallest(n, axis: nil); end
310
+
311
+ # @overload nlargest_index(n, axis:)
312
+ # Returns, along `axis`, the axis-local positions of each fiber's `n`
313
+ # largest values, largest first (`take_along_axis` reads the values
314
+ # back). `axis:` is required.
315
+ # @param n [Integer]
316
+ # @param axis [Integer]
317
+ # @return [CArray] `:int64` positions.
318
+ # @raise [ArgumentError] when `axis:` is missing or `n` is negative.
319
+ def nlargest_index(n, axis:); end
320
+
321
+ # @overload nsmallest_index(n, axis:)
322
+ # As {#nlargest_index}, for the `n` smallest values.
323
+ # @param n [Integer]
324
+ # @param axis [Integer]
325
+ # @return [CArray] `:int64` positions.
326
+ # @raise [ArgumentError] when `axis:` is missing or `n` is negative.
327
+ def nsmallest_index(n, axis:); end
328
+
329
+ # @overload sort_by_key(key, axis: 0)
330
+ # Returns `self` reordered along `axis` by the order of `key` (an
331
+ # array of `self`'s shape): `self[key.sort_addr(axis:)]`.
332
+ # @param key [CArray] the values to order by.
333
+ # @param axis [Integer]
334
+ # @return [CArray]
335
+ # @raise [ArgumentError] when `key`'s shape differs from `self`'s.
336
+ def sort_by_key(key, axis: 0); end
337
+
338
+ # @overload max_by_key(key, axis: nil)
339
+ # Returns the value of `self` where `key` is largest: one value, or
340
+ # one per fiber along `axis`. UNDEF where there is no cell to compare.
341
+ # @param key [CArray] the values to compare, of `self`'s shape.
342
+ # @param axis [Integer, nil]
343
+ # @return [Object, CArray]
344
+ # @raise [ArgumentError] when `key`'s shape differs from `self`'s.
345
+ def max_by_key(key, axis: nil); end
346
+
347
+ # @overload min_by_key(key, axis: nil)
348
+ # As {#max_by_key}, where `key` is smallest.
349
+ # @param key [CArray]
350
+ # @param axis [Integer, nil]
351
+ # @return [Object, CArray]
352
+ # @raise [ArgumentError] when `key`'s shape differs from `self`'s.
353
+ def min_by_key(key, axis: nil); end
354
+
355
+ # @overload take_along_axis(indices, axis: 0)
356
+ # Returns the values of `self` at axis-local `indices` along `axis`
357
+ # (`indices` has `self`'s shape except along `axis`), as the
358
+ # `*_index(axis:)` methods answer them. A masked index reads UNDEF.
359
+ # @param indices [CArray] integer positions along `axis`.
360
+ # @param axis [Integer]
361
+ # @return [CArray]
362
+ def take_along_axis(indices, axis: 0); end
363
+
364
+ # @overload put_along_axis(indices, values, axis: 0)
365
+ # Writes `values` into `self` at axis-local `indices` along `axis`,
366
+ # the inverse of {#take_along_axis}. A masked index writes nothing.
367
+ # @param indices [CArray] integer positions along `axis`.
368
+ # @param values [CArray, Object]
369
+ # @param axis [Integer]
370
+ # @return [self]
371
+ def put_along_axis(indices, values, axis: 0); end
372
+
373
+ # @overload range
374
+ # Returns `min..max` of `self` from one pass (`UNDEF..UNDEF` when no
375
+ # cell takes part).
376
+ # @return [Range]
377
+ def range; end
378
+
272
379
  # @!endgroup
273
380
  end
@@ -24,7 +24,10 @@ class CArray
24
24
  # Fills `self` with uniform random numbers in-place and returns
25
25
  # `self`. Boolean arrays fill 0/1 at 50% probability, ignoring
26
26
  # any range argument. Complex arrays sample real and imaginary
27
- # parts independently from the same range.
27
+ # parts independently from the same range. An integer array takes
28
+ # Integer bounds whose every value fits its type (the whole range
29
+ # of the type included); a float array takes finite bounds. Every
30
+ # cell gets a value, so the mask is cleared, as by `seq!` or `fill`.
28
31
  #
29
32
  # @param low [Numeric] lower bound (inclusive).
30
33
  # @param high [Numeric] upper bound (exclusive).
@@ -35,7 +38,11 @@ class CArray
35
38
  # @raise [CArray::DataTypeError] for `:object` / `:fixlen` arrays.
36
39
  # @raise [ArgumentError] when an integer array is called with no
37
40
  # range, when `low >= high`, when a Range is combined with a
38
- # second positional argument, or when a Range endpoint is nil.
41
+ # second positional argument, when a Range endpoint is nil, or
42
+ # when a float bound is NaN or infinite.
43
+ # @raise [TypeError] when an integer array is given a non-Integer
44
+ # bound.
45
+ # @raise [RangeError] when the integer range does not fit the type.
39
46
  def random!(*args, rng: nil); end
40
47
 
41
48
  # @overload random(rng: nil)
@@ -51,7 +58,7 @@ class CArray
51
58
  # Fills `self` with standard normal `N(0, 1)` samples via
52
59
  # Box-Muller and returns `self`. Restricted to float / complex
53
60
  # data types; complex fills real and imaginary parts as two
54
- # independent normals per cell.
61
+ # independent normals per cell. The mask is cleared.
55
62
  # @param rng [Random, nil] RNG instance; nil uses the per-ractor
56
63
  # default RNG.
57
64
  # @return [self]
@@ -72,7 +79,9 @@ class CArray
72
79
  # Fisher-Yates permutes `self` in-place and returns `self`.
73
80
  # Without `axis:`, shuffles all cells as if flattened. With
74
81
  # `axis:`, permutes slices along that axis (the trailing
75
- # sub-slab is treated as a byte chunk and swapped whole).
82
+ # sub-slab is treated as a byte chunk and swapped whole). The mask
83
+ # moves with the values: a masked cell stays masked wherever it
84
+ # lands.
76
85
  # @param axis [Integer, nil] axis to permute along; nil = flat.
77
86
  # @param rng [Random, nil] RNG instance; nil uses the per-ractor
78
87
  # default RNG.