carray 3.0.5 → 3.0.7

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 (173) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +343 -0
  3. data/README.md +25 -4
  4. data/carray.gemspec +3 -2
  5. data/ext/ca_axis_dispatch.c +6 -0
  6. data/ext/ca_axis_group.c +7 -7
  7. data/ext/ca_binop_dispatch.c +8 -0
  8. data/ext/ca_binop_dispatch.h +1 -0
  9. data/ext/ca_categorical_iterator.c +14 -14
  10. data/ext/ca_kernel_iterator.c +116 -231
  11. data/ext/ca_kernel_iterator.h +416 -258
  12. data/ext/ca_obj_array.c +4 -4
  13. data/ext/ca_obj_bincmp.c +28 -4
  14. data/ext/ca_obj_binop.c +46 -12
  15. data/ext/ca_obj_bitarray.c +2 -4
  16. data/ext/ca_obj_bitfield.c +88 -4
  17. data/ext/ca_obj_byte_swap.c +4 -4
  18. data/ext/ca_obj_const_string.c +422 -257
  19. data/ext/ca_obj_face.c +73 -33
  20. data/ext/ca_obj_fake.c +114 -13
  21. data/ext/ca_obj_farray.c +7 -1
  22. data/ext/ca_obj_fixlen_string.c +51 -17
  23. data/ext/ca_obj_grid.c +179 -19
  24. data/ext/ca_obj_meld.c +74 -118
  25. data/ext/ca_obj_moncmp.c +9 -1
  26. data/ext/ca_obj_monop.c +100 -36
  27. data/ext/ca_obj_object.c +63 -29
  28. data/ext/ca_obj_reduce.c +17 -5
  29. data/ext/ca_obj_refer.c +162 -23
  30. data/ext/ca_obj_remap.c +69 -10
  31. data/ext/ca_obj_repeat.c +1 -1
  32. data/ext/ca_obj_roll.c +3 -4
  33. data/ext/ca_obj_select.c +12 -7
  34. data/ext/ca_obj_select_axis.c +9 -6
  35. data/ext/ca_obj_shift.c +1 -0
  36. data/ext/ca_obj_stack.c +315 -116
  37. data/ext/ca_obj_stride.c +177 -53
  38. data/ext/ca_obj_string.c +20 -4
  39. data/ext/ca_obj_tile.c +3 -4
  40. data/ext/ca_obj_triop.c +24 -13
  41. data/ext/ca_obj_window.c +23 -15
  42. data/ext/ca_op_ipower.c +71 -23
  43. data/ext/ca_op_powi.h +6 -29
  44. data/ext/ca_sort_kernels.h +3 -0
  45. data/ext/ca_sweep_engine.c +90 -23
  46. data/ext/ca_sweep_engine.h +5 -6
  47. data/ext/caframe_csv_reader.c +2 -2
  48. data/ext/carray.h +5 -2
  49. data/ext/carray_access.c +125 -103
  50. data/ext/carray_address_basis.c +209 -1
  51. data/ext/carray_attribute.c +8 -0
  52. data/ext/carray_broadcast.c +5 -6
  53. data/ext/carray_build_flags.h +1 -1
  54. data/ext/carray_call_cfunc.c +5 -8
  55. data/ext/carray_cast.c +319 -48
  56. data/ext/carray_cast_func.rb +37 -5
  57. data/ext/carray_conversion.c +65 -7
  58. data/ext/carray_copy.c +24 -0
  59. data/ext/carray_core.c +874 -72
  60. data/ext/carray_count.c +28 -9
  61. data/ext/carray_element.c +22 -12
  62. data/ext/carray_factorize.c +11 -11
  63. data/ext/carray_histogram.c +27 -5
  64. data/ext/carray_index_classifier.c +7 -5
  65. data/ext/carray_internal.h +241 -12
  66. data/ext/carray_kernels_bincmp.c +2007 -3
  67. data/ext/carray_kernels_binop.c +25502 -5336
  68. data/ext/carray_kernels_init.c +22 -22
  69. data/ext/carray_kernels_monop.c +10739 -3771
  70. data/ext/carray_kernels_reduce_aggregate.c +4429 -702
  71. data/ext/carray_kernels_reduce_boolean.c +1533 -15
  72. data/ext/carray_kernels_reduce_cumulative.c +1808 -248
  73. data/ext/carray_kernels_reduce_extreme.c +4508 -511
  74. data/ext/carray_kernels_reduce_variance.c +2284 -152
  75. data/ext/carray_kernels_scan.c +240 -240
  76. data/ext/carray_kernels_search.c +4211 -264
  77. data/ext/carray_kernels_sort.c +367 -450
  78. data/ext/carray_lazy.c +63 -1
  79. data/ext/carray_loop.c +10 -3
  80. data/ext/carray_mask.c +247 -149
  81. data/ext/carray_median_percentile.c +17 -12
  82. data/ext/carray_memory_view.c +54 -29
  83. data/ext/carray_operator.c +219 -55
  84. data/ext/carray_order.c +55 -11
  85. data/ext/carray_partition.c +1 -1
  86. data/ext/carray_random.c +5 -2
  87. data/ext/carray_round.c +24 -1
  88. data/ext/carray_scatter.c +4 -0
  89. data/ext/carray_segment.c +1 -1
  90. data/ext/carray_slab.c +110 -25
  91. data/ext/carray_sort.c +7 -4
  92. data/ext/carray_sort_kernel.c +24 -0
  93. data/ext/carray_struct.c +21 -6
  94. data/ext/carray_test.c +154 -53
  95. data/ext/carray_time_text.c +8 -2
  96. data/ext/carray_utils.c +124 -54
  97. data/ext/extconf.rb +11 -0
  98. data/ext/mk_call_cfunc.rb +6 -11
  99. data/ext/mkkernel.rb +383 -121
  100. data/ext/ruby_carray.c +4 -1
  101. data/ext/version.h +4 -4
  102. data/lib/carray/attribute.rb +25 -1
  103. data/lib/carray/autoload_carray.rb +3 -3
  104. data/lib/carray/autoload_method_extension.rb +5 -3
  105. data/lib/carray/axis_group.rb +22 -13
  106. data/lib/carray/basics.rb +27 -9
  107. data/lib/carray/bincount_nd.rb +8 -2
  108. data/lib/carray/block_iterator.rb +65 -24
  109. data/lib/carray/categorical.rb +27 -16
  110. data/lib/carray/categorical_iterator.rb +4 -3
  111. data/lib/carray/complex.rb +2 -2
  112. data/lib/carray/conditional.rb +55 -6
  113. data/lib/carray/const_string.rb +74 -0
  114. data/lib/carray/construct.rb +2 -2
  115. data/lib/carray/core_extensions.rb +19 -68
  116. data/lib/carray/data_type_extension.rb +1 -0
  117. data/lib/carray/fixlen_string.rb +83 -1
  118. data/lib/carray/frame/column_verbs.rb +102 -16
  119. data/lib/carray/frame/concat.rb +50 -6
  120. data/lib/carray/frame/convert.rb +19 -0
  121. data/lib/carray/frame/csv_parser.rb +45 -36
  122. data/lib/carray/frame/describe.rb +4 -2
  123. data/lib/carray/frame/frame.rb +436 -83
  124. data/lib/carray/frame/group.rb +731 -78
  125. data/lib/carray/frame/io.rb +216 -73
  126. data/lib/carray/frame/join.rb +166 -43
  127. data/lib/carray/frame/records.rb +9 -7
  128. data/lib/carray/frame/reshape.rb +109 -11
  129. data/lib/carray/frame/sort.rb +57 -9
  130. data/lib/carray/frame/verbs.rb +448 -60
  131. data/lib/carray/frame/windows.rb +71 -9
  132. data/lib/carray/fuse_source.rb +4 -2
  133. data/lib/carray/inspect.rb +7 -2
  134. data/lib/carray/iterator.rb +40 -0
  135. data/lib/carray/key_windows.rb +10 -4
  136. data/lib/carray/lazy.rb +28 -24
  137. data/lib/carray/mask_gap_fill.rb +37 -17
  138. data/lib/carray/meld_reduce.rb +1 -1
  139. data/lib/carray/methods/bin.rb +3 -0
  140. data/lib/carray/methods/choose.rb +3 -2
  141. data/lib/carray/methods/composition.rb +1 -1
  142. data/lib/carray/methods/factorize.rb +2 -0
  143. data/lib/carray/methods/gather_nd.rb +4 -1
  144. data/lib/carray/methods/is_in.rb +23 -3
  145. data/lib/carray/methods/locate_addr.rb +63 -12
  146. data/lib/carray/methods/meshgrid.rb +3 -1
  147. data/lib/carray/methods/repeat.rb +11 -1
  148. data/lib/carray/record_order.rb +35 -6
  149. data/lib/carray/runtime.rb +57 -8
  150. data/lib/carray/segment_iterator.rb +42 -24
  151. data/lib/carray/serialize.rb +56 -17
  152. data/lib/carray/slab_iterator.rb +1 -1
  153. data/lib/carray/stack.rb +21 -9
  154. data/lib/carray/string.rb +6 -6
  155. data/lib/carray/string_operation_extension.rb +25 -4
  156. data/lib/carray/struct_builder.rb +4 -1
  157. data/lib/carray/time.rb +195 -46
  158. data/lib/carray/window_iterator.rb +64 -16
  159. data/yard-stubs/ca_obj_array.rb +26 -1
  160. data/yard-stubs/ca_obj_bitfield.rb +4 -1
  161. data/yard-stubs/ca_obj_refer.rb +3 -0
  162. data/yard-stubs/ca_obj_window.rb +2 -1
  163. data/yard-stubs/carray_access.rb +4 -2
  164. data/yard-stubs/carray_cast.rb +14 -5
  165. data/yard-stubs/carray_conversion.rb +8 -2
  166. data/yard-stubs/carray_count.rb +6 -3
  167. data/yard-stubs/carray_mask.rb +20 -1
  168. data/yard-stubs/carray_math.rb +36 -16
  169. data/yard-stubs/carray_memory_view.rb +7 -0
  170. data/yard-stubs/carray_order.rb +6 -0
  171. data/yard-stubs/carray_scatter.rb +2 -1
  172. data/yard-stubs/carray_test.rb +23 -7
  173. metadata +17 -3
@@ -70,6 +70,8 @@ class CAWindowIterator < CAIterator
70
70
  # forward-looking). `bounds:` selects the margin policy (`:skip` /
71
71
  # `:nearest` / `:truncate`). `fill_value:` is a constant margin value
72
72
  # (an escape for `:constant` padding); when given it overrides `:skip`.
73
+ # Over a categorical or a const string, which hold no new value, it has
74
+ # to be a value some cell already holds.
73
75
  #
74
76
  # For backward compatibility `initialize(window_view)` with no ranges
75
77
  # accepts a CAWindow view (the old `CAWindowIterator.new(a.window(...))`
@@ -82,6 +84,8 @@ class CAWindowIterator < CAIterator
82
84
  # @param ranges [Array<Range>] per-axis offset ranges.
83
85
  # @param bounds [Symbol] `:skip` / `:nearest` / `:truncate`.
84
86
  # @param fill_value [Object, nil] constant margin value, overriding :skip.
87
+ # @raise [ArgumentError] when a categorical or const string source holds
88
+ # no cell with `fill_value` (raised when the windows are first read).
85
89
  def initialize (source, *ranges, bounds: :skip, fill_value: nil)
86
90
  if ranges.empty? && source.is_a?(CArray) && source.obj_type == CA_OBJ_WINDOW
87
91
  # Backward-compat: read geometry from a CAWindow view built by #window.
@@ -106,6 +110,14 @@ class CAWindowIterator < CAIterator
106
110
  "windows: expected #{@source.ndim} ranges (one per axis), " \
107
111
  "got #{@ranges.size}"
108
112
  end
113
+ # Each window is read through a view with one axis per anchor and one
114
+ # per cell in the window, twice the source's rank.
115
+ if 2 * @source.ndim > CA_RANK_MAX
116
+ raise ArgumentError,
117
+ "windows: a rank #{@source.ndim} array would need #{2 * @source.ndim} " \
118
+ "axes for its windows, beyond the limit of #{CA_RANK_MAX}; " \
119
+ "windows works up to rank #{CA_RANK_MAX / 2}"
120
+ end
109
121
  @ranges = @ranges.each_with_index.map { |r, k| window_offsets(r, k) }
110
122
 
111
123
  @sndim = @source.ndim
@@ -151,14 +163,15 @@ class CAWindowIterator < CAIterator
151
163
 
152
164
  # ---- padded entity + sliding_windows view -----------------------------
153
165
  #
154
- # The engine: pad the source once (:skip -> UNDEF, :nearest -> edge, :constant
166
+ # The engine: pad the source (:skip -> UNDEF, :nearest -> edge, :constant
155
167
  # -> value; :truncate does not pad), then take a sliding_windows view. Both
156
- # are memoised: every named reduction reuses the one pad + one view.
168
+ # are kept for one call, so the helpers a reduction runs share one pad and
169
+ # one view, and the next call pads the source as it is then.
157
170
 
158
171
  # @overload sliding_view
159
172
  # Returns the sliding_windows view feeding the reductions. For :truncate
160
173
  # this is the source's own view (zero-copy); otherwise it is the view over
161
- # the padded entity.
174
+ # a padded copy made from the source as it is at this call.
162
175
  # @return [CArray]
163
176
  def sliding_view
164
177
  @sliding_view ||= @shape.include?(0) ? no_windows : anchored_buffer.sliding_windows(*@widths)
@@ -273,15 +286,32 @@ class CAWindowIterator < CAIterator
273
286
  def pad_face (src, lefts, rights, mode, value)
274
287
  storage = src.__send__(:face_storage)
275
288
  if mode == :constant && !value.nil?
276
- cell = src.__send__(:blank_like, [1])
277
- cell[0] = value
278
- padded = pad_source(storage, lefts, rights, :constant, cell.parent[0])
289
+ padded = pad_source(storage, lefts, rights, :constant, margin_storage(src, storage, value))
279
290
  else
280
291
  padded = pad_source(storage, lefts, rights, mode, value)
281
292
  end
282
293
  padded.face_lift(src)
283
294
  end
284
295
 
296
+ # The storage of a `:constant` margin value. A Face that holds new values
297
+ # turns the value into storage; one that holds none (a const string's
298
+ # records index a shared pool, a categorical's codes index its labels) can
299
+ # only repeat the storage of a cell that already holds the value.
300
+ def margin_storage (src, storage, value)
301
+ if src.__send__(:holds_new_values?)
302
+ cell = src.__send__(:blank_like, [1])
303
+ cell[0] = value
304
+ return cell.parent[0]
305
+ end
306
+ hit = src.eq(value).where
307
+ if hit.elements == 0
308
+ raise ArgumentError,
309
+ "windows: fill_value #{value.inspect} is not a value of this " \
310
+ "#{src.class}, which can only repeat a value it already holds"
311
+ end
312
+ storage.flatten[hit[0]]
313
+ end
314
+
285
315
  # Edge-replicate the margins of a padded buffer whose interior already holds
286
316
  # the source. Per axis, the left margin rows copy the first interior row and
287
317
  # the right margin rows copy the last; done axis by axis over the whole
@@ -553,16 +583,20 @@ class CAWindowIterator < CAIterator
553
583
  # Runs the fold by offset, or returns nil when this window is not one it can
554
584
  # answer for -- in which case the caller delegates as before.
555
585
  def fold_by_offset (op, min_count, fill_value = nil)
556
- return nil unless CArray.__fast_paths_enabled__
586
+ return nil unless CArray.__fast_paths_enabled__(:window_fold_by_offset)
557
587
  need = CArray.__present_cells_needed_per_result_cell__(op, min_count, 0)
558
588
  return nil unless op == :mean || OFFSET_FOLD.key?(op)
559
589
  # The elementwise kernels act on a Face's storage; its reductions are its own.
560
590
  return nil if @source.face?
591
+ # Fixed-length cells have no elementwise kernels and no neutral value.
592
+ return nil if @source.data_type == CA_FIXLEN
593
+ # An undefined constant margin is a masked one, which a fold must skip.
594
+ return nil if @fill_value.equal?(UNDEF)
561
595
  return nil if @widths.any? { |width| width > OFFSET_FOLD_MAX_WIDTH }
562
- return nil if neutral_value(op).nil?
563
596
  # pmin / pmax let NaN lose, so a window holding only NaN would fold to the
564
597
  # neutral value; the core answers NaN there.
565
598
  return nil if (op == :min || op == :max) && source_has_nan?
599
+ return nil if neutral_value(op).nil?
566
600
 
567
601
  folded = op == :mean ? fold_mean : accumulate_offsets(op)
568
602
  folded = mask_empty_windows(folded) if EMPTY_WINDOW_IS_UNDEFINED.include?(op)
@@ -631,6 +665,7 @@ class CAWindowIterator < CAIterator
631
665
  end
632
666
 
633
667
  def source_has_nan?
668
+ return true if @bounds == :constant && @fill_value.is_a?(Float) && @fill_value.nan?
634
669
  return false unless @source.float? || @source.data_type == CA_OBJECT
635
670
  @source.is_nan.any
636
671
  end
@@ -650,9 +685,9 @@ class CAWindowIterator < CAIterator
650
685
  # What stands in for a cell the fold must not see: a margin the boundary
651
686
  # policy does not fill, or a masked cell of the source. For sum, prod, all
652
687
  # and any that is the operation's identity. For min and max it is any value
653
- # that cannot win, and the array's own extreme is one -- so no table of
654
- # per-type limits is needed. Nil means there is none to be had (a source
655
- # masked everywhere), and the caller delegates instead.
688
+ # that cannot win, and the extreme of the array and its constant margin is
689
+ # one -- so no table of per-type limits is needed. Nil means there is none
690
+ # to be had (a source masked everywhere), and the caller delegates instead.
656
691
  def neutral_value (op)
657
692
  @neutral_value ||= {}
658
693
  return @neutral_value[op] if @neutral_value.key?(op)
@@ -669,7 +704,11 @@ class CAWindowIterator < CAIterator
669
704
 
670
705
  def source_extreme (op)
671
706
  value = @source.send(op)
672
- value.equal?(UNDEF) ? nil : value
707
+ return nil if value.equal?(UNDEF)
708
+ return value unless @bounds == :constant
709
+ margin = @fill_value || 0
710
+ margin = margin ? 1 : 0 if @source.boolean? # the core answers 0 or 1
711
+ [value, margin].public_send(op)
673
712
  end
674
713
 
675
714
  # The buffer the offsets are read from: the source with its margins filled
@@ -713,13 +752,19 @@ class CAWindowIterator < CAIterator
713
752
  # real values, so the count is the whole window and one Integer says it;
714
753
  # `:skip` counts the in-bounds offsets, which is separable -- the count on
715
754
  # one axis depends on that axis alone. A masked source has to be counted for
716
- # real, by accumulating over the same offsets.
755
+ # real, by accumulating over the same offsets; a cell of a constant margin
756
+ # counts once, whatever its value.
717
757
  def window_cell_counts
718
758
  return @window_cell_counts unless @window_cell_counts.nil?
719
759
  @window_cell_counts =
720
760
  if @source.has_mask?
721
- accumulate_offsets(:sum, pad_for(@source.is_not_masked.int64, 0),
722
- :add!, CA_INT64)
761
+ present = @source.is_not_masked.int64
762
+ present = if @bounds == :constant
763
+ pad_source(present, @lefts, @rights, :constant, 1)
764
+ else
765
+ pad_for(present, 0)
766
+ end
767
+ accumulate_offsets(:sum, present, :add!, CA_INT64)
723
768
  elsif @bounds != :skip
724
769
  @widths.inject(:*)
725
770
  else
@@ -797,7 +842,7 @@ class CAWindowIterator < CAIterator
797
842
  return count_not_masked(**kw) if args.empty?
798
843
  # The sliding_windows view is a CAStride, so its #count is not shadowed;
799
844
  # dispatch CArray#count explicitly anyway, matching the family regularity.
800
- CArray.instance_method(:count).bind_call(sliding_view, *args, axis: @window_axes,
845
+ count_cells(sliding_view, *args, axis: @window_axes,
801
846
  **CAIterator.reduce_keywords(:count, kw))
802
847
  end
803
848
 
@@ -1150,6 +1195,9 @@ class CAWindowIterator < CAIterator
1150
1195
  yield idx
1151
1196
  end
1152
1197
  end
1198
+
1199
+ read_source_per_call :@sliding_view, :@padded_entity, :@neutral_value,
1200
+ :@offset_fold_base, :@window_cell_counts
1153
1201
  end
1154
1202
 
1155
1203
 
@@ -244,7 +244,8 @@ class CScalar
244
244
  # @!group Construction
245
245
 
246
246
  # @overload initialize(data_type)
247
- # Allocates a CScalar (0-D CArray) of the given `data_type`.
247
+ # Allocates a CScalar of the given `data_type`: one cell on one axis
248
+ # (`ndim` 1, `shape` `[1]`).
248
249
  # @param data_type [Symbol, Integer, Class, String]
249
250
  # @return [self]
250
251
  # @overload initialize(data_type, bytes:)
@@ -269,6 +270,30 @@ class CScalar
269
270
 
270
271
  # @!endgroup
271
272
 
273
+ # @!group Indexing and slicing
274
+
275
+ # @overload []
276
+ # @overload [](0)
277
+ # Reads the scalar's value. A CScalar is one cell and takes no index:
278
+ # `s[]` and `s[0]` both name that cell, and any other index (another
279
+ # integer, a range, `nil`, an index array, a symbol) is refused. A view
280
+ # of the scalar, such as `s.refer(s.data_type, [1])`, takes the usual
281
+ # indexes.
282
+ # @return [Object] the value, or `UNDEF` if it is masked.
283
+ # @raise [ArgumentError] when any other index is given.
284
+ def [](*index); end
285
+
286
+ # @overload []=(value)
287
+ # @overload []=(0, value)
288
+ # Writes the scalar's value. `s[] = v` and `s[0] = v` both write it;
289
+ # any other index is refused.
290
+ # @param value [Object] the value, or `UNDEF` to mask the cell.
291
+ # @return [Object] value
292
+ # @raise [ArgumentError] when any other index is given.
293
+ def []=(*index_and_value); end
294
+
295
+ # @!endgroup
296
+
272
297
  class << self
273
298
  # @!group Construction (typed shorthands)
274
299
 
@@ -30,7 +30,10 @@ class CArray
30
30
  # narrowest unsigned type that holds the field is used: 1 bit →
31
31
  # `:boolean`, 2..8 → `:uint8`, 9..16 → `:uint16`, 17..32 → `:uint32`,
32
32
  # 33..64 → `:uint64`. A write keeps the low bits of the value that
33
- # fit the field, signed or not (-3 into a 3-bit field is `0b101`).
33
+ # fit the field, signed or not, for an Integer of any size (-3 into a
34
+ # 3-bit field is `0b101`, 300 is `0b100`). The same holds for a view
35
+ # taken of the field (a slice, an index, a mask selection). A String
36
+ # is read as a decimal integer, a Float through `Integer()`.
34
37
  #
35
38
  # @overload bitfield(range, type = nil)
36
39
  # @param range [Integer, Range] bit position (single bit) or bit
@@ -35,6 +35,9 @@ class CArray
35
35
  # @raise [ArgumentError] when the byte widths do not divide
36
36
  # evenly, when `offset` is negative, or when the requested
37
37
  # view extends past the parent's data.
38
+ # @raise [TypeError] on a Face, for a data type that is neither its
39
+ # surface type nor its storage type: `to_type` gives the values,
40
+ # `parent.refer` the raw storage.
38
41
  # @raise [CArray::DataTypeError] when reinterpreting a CA_OBJECT parent
39
42
  # with a non-object `data_type`, or a parent of another data type as
40
43
  # `:boolean` (a boolean cell holds 0 or 1; compare instead, `ne(0)`).
@@ -54,7 +54,8 @@ class CArray
54
54
  # or when `bounds` is not a recognised value.
55
55
  # @raise [IndexError] when `bounds` is `"strict"` and a range reaches
56
56
  # outside `self`, or `"ruby"` and a range reaches past the end or
57
- # before the first cell counted from the end.
57
+ # before the first cell counted from the end, or `"nearest"` and a
58
+ # range takes cells of an axis of length 0.
58
59
  def window(*ranges, fill_value: 0, bounds: "fill"); end
59
60
 
60
61
  # @!endgroup
@@ -90,7 +90,8 @@ class CArray
90
90
  # or a CArray of such addresses (arbitrary shape). A negative
91
91
  # address counts from the end.
92
92
  # @return [Array<Integer>, Array<CArray>] one entry per axis.
93
- # @raise [IndexError] when any `addr` is out of range.
93
+ # @raise [IndexError] when any `addr` is out of range, or when `addr` is
94
+ # float, complex, object or boolean.
94
95
  def addr2index(addr); end
95
96
 
96
97
  # @overload index2addr(*index)
@@ -101,7 +102,8 @@ class CArray
101
102
  # must match that shape). Mask propagates from the inputs.
102
103
  # @param index [Array<Integer, CArray>] one entry per axis.
103
104
  # @return [Integer, CArray] the flat address(es).
104
- # @raise [IndexError] on an out-of-range index.
105
+ # @raise [IndexError] on an out-of-range index, or an index array that
106
+ # is float, complex, object or boolean.
105
107
  # @raise [ArgumentError] on shape mismatch between non-scalar inputs.
106
108
  def index2addr(*index); end
107
109
 
@@ -202,10 +202,13 @@ class CArray
202
202
  # -- as_type: reinterpreting view (no copy) ---------------------------
203
203
 
204
204
  # @overload as_type(data_type, bytes: nil)
205
- # Returns a {CAFake} view of `self` reinterpreted as `data_type`
206
- # (with `bytes:` for `:fixlen`). Reads and writes cast on the fly
207
- # through the shared parent storage; no copy is made. For an eager
208
- # copy use `arr.as_type(...).to_ca` or {#to_type}.
205
+ # Returns a view of `self` reinterpreted as `data_type` (with
206
+ # `bytes:` for `:fixlen`): a {CAMonOp} for a numeric type, a {CAFake}
207
+ # otherwise. Reads and writes cast on the fly through the shared
208
+ # parent storage; no copy is made. The view is a new array also when
209
+ # `data_type` is `self`'s own, so an attribute set on it or a freeze
210
+ # of it does not reach `self`. For an eager copy use
211
+ # `arr.as_type(...).copy` or {#to_type}.
209
212
  #
210
213
  # Note: for an `:object` source the on-the-fly cast to a numeric type
211
214
  # uses the lenient path and does NOT mask parse failures — a
@@ -225,7 +228,7 @@ class CArray
225
228
  # @param data_type [Symbol, Integer, Class, String] target element type.
226
229
  # @param bytes [Integer, nil] element width in bytes, required for
227
230
  # `:fixlen`.
228
- # @return [CAFake]
231
+ # @return [CAMonOp, CAFake]
229
232
  # @raise [TypeError] when `self` is a Face and the request would read its
230
233
  # storage under another type.
231
234
  # @raise [FloatDomainError] when a NaN cell that is not masked is read
@@ -487,6 +490,12 @@ class CArray
487
490
  # that does not fit an integer type takes `:int64`. With no array or
488
491
  # data_type representation, every argument promotes by its own type.
489
492
  #
493
+ # This is the type an operator computes in: `int32_array + float32_array`
494
+ # is `:float32`. Arrays joined value for value ({CArray.stack},
495
+ # {CArray.meld}, {CArray.concatenate}) take the type {.promote_list}
496
+ # gives, which is `:float64` for that pair, since `float32` does not
497
+ # hold every `int32` exactly.
498
+ #
490
499
  # Integer args are interpreted as *values*, not as data_type codes:
491
500
  # `result_type(8)` returns `:int64` because the value 8 is an Integer,
492
501
  # not because a data_type code equals 8. Use `result_type(:int64)` or
@@ -81,10 +81,16 @@ class CArray
81
81
  # `String` of the exact size, or any object responding to
82
82
  # `read(n, buf = nil)`. Into a boolean array, a non-zero byte is
83
83
  # read as 1 (true).
84
+ # From an IO into an array that owns its memory, the bytes are written
85
+ # as they are read, so that no second copy of the data is held; when the
86
+ # IO runs out before the array is full, what the array holds afterwards
87
+ # is not defined (the part read so far may already be in it). Into a
88
+ # view the whole data is read first and nothing is written on a short
89
+ # read.
84
90
  # @param io [String, IO, #read]
85
91
  # @return [self]
86
- # @raise [RuntimeError] on a short read.
87
- # @raise [ArgumentError] when a String source is not the array's size.
92
+ # @raise [ArgumentError] on a short read, and when a String source is
93
+ # not the array's size.
88
94
  # @raise [CArray::DataTypeError] if `self.data_type` is
89
95
  # `:object`.
90
96
  def load_binary(io); end
@@ -36,6 +36,9 @@ class CArray
36
36
  # count).
37
37
  # - `v == UNDEF`: forwards to {#count_masked} (UNDEF is mask-state
38
38
  # vocabulary, not a value).
39
+ # - `v` is `nil`, a String, a Symbol or another value that is not a
40
+ # number, on an array that is not `:object` or `:fixlen`: `0`. It
41
+ # is no value of such an array, so no cell equals it.
39
42
  # - `v` is a CArray: broadcasts; each `v[k]` is counted
40
43
  # independently and stacked into a result whose trailing axes
41
44
  # have shape `v.shape`. Not supported when `self.data_type` is
@@ -43,9 +46,9 @@ class CArray
43
46
  # - `self.data_type == :boolean`: `v` must be `true` / `false`, or
44
47
  # the integer literal `1` (= true) / `0` (= false) — boolean
45
48
  # stores 0/1, so `count(1)` == `count(true)`. Any other value
46
- # (`2`, `1.0`, `nil`, …) raises `TypeError`.
47
- # - `self` is numeric, `v` is scalar: `v` must be numeric (true /
48
- # false are rejected -- they are the boolean array's domain).
49
+ # (`2`, `1.0`, …) raises `TypeError`.
50
+ # - `self` is numeric, `v` is scalar: `true` and `false` are the
51
+ # numbers `1` and `0`, as they are to {#eq}.
49
52
  # `v` (a scalar or an array) is compared in the type it and the
50
53
  # array match in, as {#eq} compares: `CA_INT32([1, 2]).count(1.5)`
51
54
  # is `0`, `CA_UINT8([255]).count(-1)` is `0` (-1 is not 255), and an
@@ -26,7 +26,20 @@ class CArray
26
26
  # bypassing the mask. The returned view itself cannot carry a
27
27
  # mask (`value_array? == true`).
28
28
  #
29
- # Use this to read or write the data slot of masked elements.
29
+ # This is a method for peeking at the internal state of an array, and
30
+ # nothing more. What a masked cell holds is not defined: a computation
31
+ # leaves there whatever it happens to. Use it on the spot, as in
32
+ # `a.value.copy`, `a.value[nil, 1].copy` or `a.value[nil, 1] = 2`; do
33
+ # not keep the view to carry around (`b = a.value`,
34
+ # `b = a.value[nil, 1]`), do not compute with it, and do not mix it
35
+ # with other arrays (as an operand, or as a part of {CArray.stack} or
36
+ # {CArray.meld}). To take the mask off, use {#unmask} or
37
+ # {#strip_mask} and give the masked cells a value.
38
+ #
39
+ # A library that handles the mask itself may use `value` on the spot
40
+ # to hand the stored cells on together with {#mask}, as long as it
41
+ # reads nothing from the masked ones.
42
+ #
30
43
  # Nothing in a value array is masked, so `unmask` on it does nothing,
31
44
  # while a write that would mask a cell (`UNDEF`, `mask=`,
32
45
  # `invert_mask`) raises TypeError.
@@ -84,6 +97,12 @@ class CArray
84
97
  # that derives values from neighbouring valid cells (mask gap-fill).
85
98
  # The two are mutually exclusive: passing both raises `ArgumentError`.
86
99
  #
100
+ # On a view, the values are written through to the cells it views. A
101
+ # position with no cell behind it -- the place of a masked cell of an
102
+ # index array (`a[i]`), as a join with `view: :full` gives for an
103
+ # unmatched row -- takes no write and stays masked, without an error.
104
+ # `strip_mask` gives a new array, where every masked cell takes a value.
105
+ #
87
106
  # @overload unmask
88
107
  # Clears the mask state of every element of `self`, leaving the
89
108
  # stored data values untouched. Mutates `self`.
@@ -231,6 +231,13 @@ class CArray
231
231
  # integer base with a negative integer exponent truncates toward
232
232
  # zero rather than producing a fraction (`2 ** -1` is `0`). Cast to
233
233
  # a float `data_type` first when a fractional power is wanted.
234
+ # A float or complex array raised to an Integer is computed by
235
+ # repeated multiplication rather than by `pow`. Its rounding error
236
+ # grows roughly in proportion to the exponent (tens of ulps at 100,
237
+ # hundreds of thousands at a million), where `Float#**` and the same
238
+ # exponent given as a Float are within an ulp or so; give the exponent
239
+ # as a Float when a large one needs full precision. The exponent can
240
+ # be any Integer that fits 64 bits.
234
241
  # Masked elements stay masked.
235
242
  # @param other [CArray, Numeric] exponent, broadcast to `self.shape`.
236
243
  # @return [CArray] result of the promoted `data_type` of the two
@@ -777,6 +784,11 @@ class CArray
777
784
  # point equality is exact here; for a tolerant comparison see
778
785
  # {#is_close} (absolute) or {#is_equiv} (relative). Integers compare
779
786
  # by value, as for {#<}.
787
+ #
788
+ # `eq(UNDEF)` asks the mask, as {#is_masked} does. `nil`, a String, a
789
+ # Symbol or another value that is not a number is no value of a numeric
790
+ # or boolean array, so `eq` is false in every cell there; an `:object`
791
+ # or `:fixlen` array compares it like any other value.
780
792
  # @param other [CArray, Numeric, Object] comparand, broadcast to
781
793
  # `self.shape`.
782
794
  # @return [CArray] `:boolean`, same shape as `self`.
@@ -949,13 +961,15 @@ class CArray
949
961
  # @overload &(other)
950
962
  # Returns the element-wise conjunction of `self` and `other`: a
951
963
  # bitwise `AND` on integer arrays, a logical `AND` on boolean ones.
964
+ # **On boolean arrays it is three-valued (Kleene); {#and} is the
965
+ # two-valued form.** The two give different answers where one side
966
+ # is masked.
952
967
  #
953
- # On boolean arrays the masked elements follow three-valued
954
- # (Kleene) logic rather than propagating blindly: an unknown
955
- # element `AND` `false` is `false`, because the answer does not
956
- # depend on the unknown. Only where the result genuinely depends on
957
- # a masked element is the result masked. {#and} is the two-valued
958
- # form that always propagates.
968
+ # An unknown (masked) element `AND` `false` is `false`, because the
969
+ # answer does not depend on the unknown; only where the result
970
+ # genuinely depends on a masked element is the result masked. So
971
+ # `(a & b).not` is `true` where `a` is `false`, whatever `b` is,
972
+ # while `a.and(b).not` stays masked where `b` is.
959
973
  # @param other [CArray, Integer, true, false] the other operand,
960
974
  # broadcast to `self.shape`.
961
975
  # @return [CArray] `:boolean` for boolean operands, otherwise the
@@ -972,8 +986,9 @@ class CArray
972
986
  # @overload |(other)
973
987
  # Returns the element-wise disjunction of `self` and `other`: a
974
988
  # bitwise `OR` on integer arrays, a logical `OR` on boolean ones.
975
- # Boolean operands follow Kleene logic as in {#&}, so an unknown
976
- # element `OR` `true` is `true`.
989
+ # **On boolean arrays it is three-valued (Kleene) as {#&} is; {#or}
990
+ # is the two-valued form.** An unknown element `OR` `true` is
991
+ # `true`.
977
992
  # @param other [CArray, Integer, true, false] the other operand,
978
993
  # broadcast to `self.shape`.
979
994
  # @return [CArray] `:boolean` for boolean operands, otherwise the
@@ -1055,13 +1070,16 @@ class CArray
1055
1070
  def not; end
1056
1071
 
1057
1072
  # @overload and(other)
1058
- # Returns the two-valued logical `AND` of two boolean arrays.
1073
+ # Returns the logical `AND` of two boolean arrays, **two-valued: a
1074
+ # masked element on either side makes the result masked**, even
1075
+ # where the other side alone would settle the answer. {#&} is the
1076
+ # three-valued form, and the two differ exactly there: `false & U`
1077
+ # is `false`, `false.and(U)` is `U`.
1059
1078
  #
1060
- # Where {#&} applies Kleene logic, this form propagates the mask
1061
- # unconditionally: if either element is masked the result is
1062
- # masked, even when the unmasked element alone would settle the
1063
- # answer. Use it when an unknown operand should make the result
1064
- # unknown regardless.
1079
+ # Use it when the answer should be given only where both inputs are
1080
+ # known -- the element-wise counterpart of keeping only complete
1081
+ # records -- and {#&} when an answer the known side settles should
1082
+ # stand.
1065
1083
  # @param other [CArray, true, false] the other operand, broadcast
1066
1084
  # to `self.shape`.
1067
1085
  # @return [CArray] `:boolean`.
@@ -1070,8 +1088,10 @@ class CArray
1070
1088
  def and(other); end
1071
1089
 
1072
1090
  # @overload or(other)
1073
- # Returns the two-valued logical `OR` of two boolean arrays,
1074
- # propagating the mask unconditionally as {#and} does.
1091
+ # Returns the logical `OR` of two boolean arrays, **two-valued: a
1092
+ # masked element on either side makes the result masked**, as
1093
+ # {#and} does. {#|} is the three-valued form: `true | U` is `true`,
1094
+ # `true.or(U)` is `U`.
1075
1095
  # @param other [CArray, true, false] the other operand.
1076
1096
  # @return [CArray] `:boolean`.
1077
1097
  # @raise [CArray::DataTypeError] for any `data_type` other than
@@ -102,6 +102,13 @@ class CArray
102
102
  # returned object via internal ivars; the source is released
103
103
  # when the wrap is garbage-collected.
104
104
  #
105
+ # Views made from the wrap keep it reachable too, so the view is
106
+ # given back only once all of them are collected. A producer that
107
+ # locks while it is borrowed (`IO::Buffer` refuses `resize` and
108
+ # `free`) stays locked until then; import with
109
+ # {.from_memory_view} to hold no view at all. See
110
+ # {file:docs/interop/MemoryView.md MemoryView} §9.
111
+ #
105
112
  # Contiguous typed producers return a `CAWrap`. Strided typed
106
113
  # producers return a `CAStride` over a byte `CAWrap` of the
107
114
  # bytes its elements reach, preserving the full stride pattern
@@ -24,6 +24,7 @@ class CArray
24
24
  # @return [CArray] a fresh array shaped like `idx`; a Face of the same
25
25
  # type when `self` is a Face.
26
26
  # @raise [ArgumentError] if `lval` / `uval` is given and `self` is a Face.
27
+ # @raise [IndexError] when `idx` is float, complex, object or boolean.
27
28
  def project(idx, lval = nil, uval = nil); end
28
29
 
29
30
  # sort / sort_copy stubs live in yard-stubs/carray_sort.rb (same
@@ -176,6 +177,11 @@ class CArray
176
177
  # nothing: `nil`, or `UNDEF` in that cell of the result. The same
177
178
  # holds for {#bsearch} and {#search_nearest}.
178
179
  #
180
+ # `nil` is no value of an array that is not `:object`, so it finds
181
+ # nothing there; an `:object` array looks for it like any other value.
182
+ # {#bsearch} and {#search_nearest} need an order or a distance, which
183
+ # `nil` does not have, and raise.
184
+ #
179
185
  # A time array (`CATime`, `CATimedelta`) takes a value of another
180
186
  # unit, a `Time` or a String: when it is off the array's grid the
181
187
  # two are compared in the finer unit, so it matches nothing (and
@@ -45,7 +45,8 @@ class CArray
45
45
  # @param addrs [CArray, Array<Integer>]
46
46
  # @param vals [CArray, Numeric]
47
47
  # @return [self]
48
- # @raise [IndexError] for out-of-range addresses.
48
+ # @raise [IndexError] for out-of-range addresses, or when `addrs` is
49
+ # float, complex, object or boolean.
49
50
  # @raise [CArray::DataTypeError] for non-numeric `data_type`.
50
51
  def scatter_add!(addrs, vals); end
51
52
 
@@ -13,6 +13,7 @@ class CArray
13
13
  # @return [Boolean]
14
14
  # @raise [ArgumentError] if the number of indices does not match
15
15
  # `ndim`.
16
+ # @raise [TypeError] if an index is not an Integer.
16
17
  def valid_index?(*idx); end
17
18
 
18
19
  # @overload valid_addr?(addr)
@@ -21,11 +22,16 @@ class CArray
21
22
  # @param addr [Integer] flat address into the contiguous element
22
23
  # sequence.
23
24
  # @return [Boolean]
25
+ # @raise [TypeError] if `addr` is not an Integer.
24
26
  def valid_addr?(addr); end
25
27
 
26
28
  # @overload same_shape?(other)
27
- # Returns `true` if `other` has the same shape as `self`.
28
- # @param other [CArray]
29
+ # Returns `true` if `other` has the same shape as `self`. Only the
30
+ # shape is compared, whatever the data types of the two. A scalar has
31
+ # the same shape as any array. An `other` that is not a CArray is read
32
+ # as an object array: a nested Array by its nesting, anything else as
33
+ # a scalar.
34
+ # @param other [CArray, Object]
29
35
  # @return [Boolean]
30
36
  def same_shape?(other); end
31
37
 
@@ -36,8 +42,11 @@ class CArray
36
42
  # @overload ==(other)
37
43
  # Returns `true` if `other` is a CArray with the same shape,
38
44
  # `data_class`, `data_type`, mask state, and elementwise values as
39
- # `self`. NaN values compare unequal (IEEE semantics), so two
40
- # arrays with NaN at the same position are not `==`.
45
+ # `self`. It asks whether the contents are the same, so a NaN equals
46
+ # a NaN at the same position (whatever its payload, in a float, a
47
+ # complex part or an object cell) and -0.0 equals 0.0: an array with
48
+ # NaN is `==` to its copy. The IEEE comparison, where NaN equals
49
+ # nothing, is {#eq}, cell by cell.
41
50
  #
42
51
  # A Face (a time, string or categorical array) equals only a Face of
43
52
  # its own class, and is compared by its values: two time arrays of
@@ -52,9 +61,10 @@ class CArray
52
61
  # @overload eql?(other)
53
62
  # Returns `true` under Hash-invariant semantics: `data_class`,
54
63
  # `data_type`, shape, and mask state must all match, and elements
55
- # are compared bitwise (for numeric types) or via `Object#eql?`
56
- # (for `:object` arrays). Unlike `==`, `NaN.eql?(NaN)` holds, so
57
- # two mask-free arrays with NaN at the same positions are `eql?`.
64
+ # are compared bitwise (for numeric types) or, for `:object` arrays, via
65
+ # `Object#eql?` with an object equal to itself first, as in
66
+ # `Array#eql?`. In a numeric array -0.0 and 0.0 differ and two NaNs
67
+ # are `eql?` only with the same bits.
58
68
  # Two Faces are `eql?` only when they are of the same class, share
59
69
  # their state (a time array's unit, a categorical's labels) and hold
60
70
  # the same storage.
@@ -69,7 +79,13 @@ class CArray
69
79
  # `ndim`, `bytes`, `elements`, shape, scalar-ness, and mask
70
80
  # presence; for unmasked arrays, samples the leading 64 bytes of
71
81
  # data. Masked arrays skip the data sample.
82
+ #
83
+ # An `:object` array samples the `#hash` of its cells, so an array in
84
+ # a cell is hashed in turn. {#==}, {#eql?} and `Marshal.dump` walk into
85
+ # cells the same way, and all of them refuse arrays nested more than
86
+ # 256 levels deep.
72
87
  # @return [Integer]
88
+ # @raise [ArgumentError] when object cells nest more than 256 levels.
73
89
  def hash; end
74
90
 
75
91
  # @!endgroup
metadata CHANGED
@@ -1,14 +1,28 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: carray
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.5
4
+ version: 3.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - himotoyoshi
8
8
  bindir: bin
9
9
  cert_chain: []
10
10
  date: 1980-01-02 00:00:00.000000000 Z
11
- dependencies: []
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: prism
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '1.0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '1.0'
12
26
  description: |2
13
27
  Ruby/CArray adds multi-dimensional numerical arrays to Ruby. Elements are held
14
28
  in a single flat memory block, so whole-array work -- element-wise arithmetic,
@@ -318,7 +332,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
318
332
  requirements:
319
333
  - - ">="
320
334
  - !ruby/object:Gem::Version
321
- version: '3.0'
335
+ version: '3.2'
322
336
  required_rubygems_version: !ruby/object:Gem::Requirement
323
337
  requirements:
324
338
  - - ">="