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.
Files changed (277) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +1 -1
  3. data/CHANGELOG.md +7 -1830
  4. data/README.md +62 -35
  5. data/carray.gemspec +16 -4
  6. data/docs/changelogs/CHANGELOG-3.0.md +2464 -0
  7. data/ext/ca_array_pool.c +4 -5
  8. data/ext/ca_axis_descriptor.h +45 -30
  9. data/ext/ca_axis_dispatch.c +7 -4
  10. data/ext/ca_axis_group.c +12 -15
  11. data/ext/ca_bincmp_dispatch.h +16 -38
  12. data/ext/ca_binop_dispatch.c +35 -0
  13. data/ext/ca_binop_dispatch.h +22 -46
  14. data/ext/ca_categorical_iterator.c +36 -34
  15. data/ext/ca_compare.c +0 -1
  16. data/ext/ca_compare.h +8 -1
  17. data/ext/ca_composite_dispatch.h +6 -0
  18. data/ext/ca_for_buffer.h +6 -1
  19. data/ext/ca_group_iter.c +17 -18
  20. data/ext/ca_iter_state_fields.h +196 -0
  21. data/ext/ca_iter_substrate.h +84 -103
  22. data/ext/ca_kernel_iterator.c +125 -89
  23. data/ext/ca_kernel_iterator.h +229 -1504
  24. data/ext/ca_kernel_iterator_internal.h +1167 -0
  25. data/ext/ca_moncmp_dispatch.h +17 -24
  26. data/ext/ca_monop_dispatch.h +25 -74
  27. data/ext/ca_obj_array.c +92 -165
  28. data/ext/ca_obj_bincmp.c +65 -48
  29. data/ext/ca_obj_binop.c +74 -49
  30. data/ext/ca_obj_bitarray.c +7 -21
  31. data/ext/ca_obj_bitfield.c +93 -26
  32. data/ext/ca_obj_block.c +11 -12
  33. data/ext/ca_obj_byte_swap.c +34 -34
  34. data/ext/ca_obj_const_string.c +464 -304
  35. data/ext/ca_obj_face.c +167 -121
  36. data/ext/ca_obj_face.h +32 -170
  37. data/ext/ca_obj_fake.c +127 -34
  38. data/ext/ca_obj_farray.c +13 -9
  39. data/ext/ca_obj_field.c +17 -13
  40. data/ext/ca_obj_fixlen_string.c +57 -34
  41. data/ext/ca_obj_grid.c +149 -113
  42. data/ext/ca_obj_meld.c +28 -26
  43. data/ext/ca_obj_moncmp.c +10 -21
  44. data/ext/ca_obj_monop.c +88 -73
  45. data/ext/ca_obj_object.c +60 -46
  46. data/ext/ca_obj_record.c +74 -22
  47. data/ext/ca_obj_reduce.c +10 -14
  48. data/ext/ca_obj_refer.c +146 -39
  49. data/ext/ca_obj_remap.c +11 -29
  50. data/ext/ca_obj_repeat.c +41 -23
  51. data/ext/ca_obj_roll.c +20 -28
  52. data/ext/ca_obj_select.c +17 -27
  53. data/ext/ca_obj_select_axis.c +35 -42
  54. data/ext/ca_obj_shift.c +22 -42
  55. data/ext/ca_obj_source.c +1 -5
  56. data/ext/ca_obj_stack.c +33 -29
  57. data/ext/ca_obj_stride.c +99 -62
  58. data/ext/ca_obj_string.c +8 -20
  59. data/ext/ca_obj_tile.c +14 -16
  60. data/ext/ca_obj_time.c +12 -23
  61. data/ext/ca_obj_timedelta.c +12 -23
  62. data/ext/ca_obj_transpose.c +8 -10
  63. data/ext/ca_obj_triop.c +48 -49
  64. data/ext/ca_obj_window.c +87 -99
  65. data/ext/ca_op_cmplx64.h +4 -0
  66. data/ext/ca_op_ipower.c +97 -27
  67. data/ext/ca_op_powi.h +9 -28
  68. data/ext/ca_pow5_table.h +2 -1
  69. data/ext/ca_rng_normal.h +4 -0
  70. data/ext/ca_rng_xoshiro256pp.h +3 -0
  71. data/ext/ca_sort_kernels.h +23 -26
  72. data/ext/ca_sweep_engine.c +92 -26
  73. data/ext/ca_sweep_engine.h +22 -15
  74. data/ext/ca_transform_common.c +3 -8
  75. data/ext/ca_triop_dispatch.h +20 -32
  76. data/ext/caframe_csv_reader.c +12 -12
  77. data/ext/carray.h +300 -1009
  78. data/ext/carray_access.c +320 -207
  79. data/ext/carray_address_basis.c +128 -59
  80. data/ext/carray_attribute.c +81 -104
  81. data/ext/carray_bincount.c +12 -14
  82. data/ext/carray_broadcast.c +31 -27
  83. data/ext/carray_call_cfunc.c +174 -285
  84. data/ext/carray_call_cfunc.h +36 -28
  85. data/ext/carray_cast.c +1191 -351
  86. data/ext/carray_cast_func.rb +68 -8
  87. data/ext/carray_class.c +6 -11
  88. data/ext/carray_conversion.c +98 -39
  89. data/ext/carray_copy.c +52 -26
  90. data/ext/carray_core.c +286 -132
  91. data/ext/carray_count.c +53 -22
  92. data/ext/carray_element.c +49 -28
  93. data/ext/carray_factorize.c +64 -63
  94. data/ext/carray_generate.c +26 -19
  95. data/ext/carray_histogram.c +9 -11
  96. data/ext/carray_hold.c +7 -9
  97. data/ext/carray_index_classifier.c +127 -230
  98. data/ext/carray_index_classifier.h +11 -10
  99. data/ext/carray_integer_total.c +523 -0
  100. data/ext/carray_internal.h +1364 -156
  101. data/ext/carray_key_windows.c +3 -3
  102. data/ext/carray_lazy.c +37 -37
  103. data/ext/carray_loop.c +27 -23
  104. data/ext/carray_mask.c +235 -150
  105. data/ext/carray_math_kernel.h +23 -47
  106. data/ext/carray_mathfunc.c +10 -3
  107. data/ext/carray_median_percentile.c +106 -100
  108. data/ext/carray_memory_view.c +83 -69
  109. data/ext/carray_operator.c +490 -128
  110. data/ext/carray_order.c +163 -84
  111. data/ext/carray_partition.c +14 -16
  112. data/ext/carray_random.c +48 -42
  113. data/ext/carray_round.c +33 -5
  114. data/ext/carray_scatter.c +57 -39
  115. data/ext/carray_segment.c +42 -46
  116. data/ext/carray_slab.c +77 -51
  117. data/ext/carray_slab.h +44 -33
  118. data/ext/carray_sort.c +37 -31
  119. data/ext/carray_sort_kernel.c +36 -10
  120. data/ext/carray_struct.c +44 -34
  121. data/ext/carray_test.c +131 -81
  122. data/ext/carray_time_text.c +30 -31
  123. data/ext/carray_undef.c +50 -7
  124. data/ext/carray_utils.c +373 -139
  125. data/ext/extconf.rb +24 -17
  126. data/ext/mk_call_cfunc.rb +25 -31
  127. data/ext/mk_pow5_table.rb +2 -1
  128. data/ext/mkkernel.rb +819 -488
  129. data/ext/ruby_carray.c +47 -42
  130. data/ext/version.h +7 -5
  131. data/lib/carray/attribute.rb +19 -18
  132. data/lib/carray/autoload_carray.rb +20 -15
  133. data/lib/carray/autoload_method_extension.rb +64 -57
  134. data/lib/carray/axis_group.rb +272 -185
  135. data/lib/carray/basics.rb +298 -56
  136. data/lib/carray/bincount_nd.rb +41 -18
  137. data/lib/carray/block_iterator.rb +167 -119
  138. data/lib/carray/boolean_reduce.rb +7 -4
  139. data/lib/carray/categorical.rb +84 -59
  140. data/lib/carray/categorical_iterator.rb +90 -67
  141. data/lib/carray/complex.rb +61 -41
  142. data/lib/carray/conditional.rb +98 -23
  143. data/lib/carray/const_string.rb +112 -46
  144. data/lib/carray/core_extensions.rb +20 -69
  145. data/lib/carray/distance_limit.rb +7 -5
  146. data/lib/carray/factories.rb +269 -0
  147. data/lib/carray/fixlen_string.rb +95 -12
  148. data/lib/carray/frame/column_verbs.rb +76 -9
  149. data/lib/carray/frame/concat.rb +51 -7
  150. data/lib/carray/frame/convert.rb +2 -1
  151. data/lib/carray/frame/csv_parser.rb +20 -5
  152. data/lib/carray/frame/frame.rb +73 -31
  153. data/lib/carray/frame/group.rb +247 -187
  154. data/lib/carray/frame/io.rb +36 -12
  155. data/lib/carray/frame/join.rb +56 -17
  156. data/lib/carray/frame/records.rb +7 -5
  157. data/lib/carray/frame/reshape.rb +27 -7
  158. data/lib/carray/frame/sort.rb +10 -8
  159. data/lib/carray/frame/verbs.rb +217 -134
  160. data/lib/carray/frame/windows.rb +254 -19
  161. data/lib/carray/frame.rb +1 -2
  162. data/lib/carray/fuse_source.rb +28 -23
  163. data/lib/carray/fusion.rb +30 -30
  164. data/lib/carray/histogram.rb +35 -16
  165. data/lib/carray/inspect.rb +7 -7
  166. data/lib/carray/iterator.rb +104 -23
  167. data/lib/carray/key_windows.rb +93 -61
  168. data/lib/carray/lazy.rb +208 -111
  169. data/lib/carray/limits.rb +80 -0
  170. data/lib/carray/mask_gap_fill.rb +21 -16
  171. data/lib/carray/math.rb +87 -50
  172. data/lib/carray/meld_reduce.rb +30 -8
  173. data/lib/carray/methods/align_addr.rb +29 -17
  174. data/lib/carray/methods/bin.rb +8 -1
  175. data/lib/carray/methods/bincount.rb +4 -2
  176. data/lib/carray/methods/bit_string.rb +19 -31
  177. data/lib/carray/methods/broadcast.rb +8 -3
  178. data/lib/carray/methods/choose.rb +5 -5
  179. data/lib/carray/methods/composition.rb +73 -72
  180. data/lib/carray/methods/{index.rb → coordinate.rb} +13 -13
  181. data/lib/carray/methods/discovery_along.rb +3 -3
  182. data/lib/carray/methods/factorize.rb +2 -0
  183. data/lib/carray/methods/gather_nd.rb +106 -103
  184. data/lib/carray/methods/insert_block.rb +2 -2
  185. data/lib/carray/methods/is_between.rb +6 -4
  186. data/lib/carray/methods/is_in.rb +183 -143
  187. data/lib/carray/methods/join.rb +2 -1
  188. data/lib/carray/methods/lag.rb +6 -2
  189. data/lib/carray/methods/locate_addr.rb +157 -94
  190. data/lib/carray/methods/mask_duplicates.rb +1 -1
  191. data/lib/carray/methods/meshgrid.rb +28 -16
  192. data/lib/carray/methods/mode.rb +67 -67
  193. data/lib/carray/methods/nunique.rb +9 -7
  194. data/lib/carray/methods/repeat.rb +23 -13
  195. data/lib/carray/methods/resize.rb +1 -1
  196. data/lib/carray/methods/snap.rb +24 -20
  197. data/lib/carray/methods/string_format.rb +2 -2
  198. data/lib/carray/methods/unique.rb +4 -2
  199. data/lib/carray/methods/value_counts.rb +1 -1
  200. data/lib/carray/mkmf.rb +25 -28
  201. data/lib/carray/percentile_arguments.rb +82 -0
  202. data/lib/carray/record_order.rb +50 -20
  203. data/lib/carray/rng.rb +1 -0
  204. data/lib/carray/runtime.rb +195 -17
  205. data/lib/carray/segment_iterator.rb +262 -137
  206. data/lib/carray/serialize.rb +635 -566
  207. data/lib/carray/slab_iterator.rb +114 -114
  208. data/lib/carray/stack.rb +14 -15
  209. data/lib/carray/string.rb +16 -22
  210. data/lib/carray/string_operation_extension.rb +97 -52
  211. data/lib/carray/struct.rb +122 -164
  212. data/lib/carray/struct_builder.rb +247 -73
  213. data/lib/carray/time.rb +663 -365
  214. data/lib/carray/window_iterator.rb +232 -155
  215. data/lib/carray.rb +6 -11
  216. data/yard-stubs/ca_obj_array.rb +20 -135
  217. data/yard-stubs/ca_obj_bitarray.rb +1 -8
  218. data/yard-stubs/ca_obj_bitfield.rb +6 -2
  219. data/yard-stubs/ca_obj_byte_swap.rb +21 -7
  220. data/yard-stubs/ca_obj_fake.rb +3 -1
  221. data/yard-stubs/ca_obj_farray.rb +1 -1
  222. data/yard-stubs/ca_obj_field.rb +8 -6
  223. data/yard-stubs/ca_obj_refer.rb +10 -5
  224. data/yard-stubs/ca_obj_roll.rb +7 -6
  225. data/yard-stubs/ca_obj_shift.rb +2 -3
  226. data/yard-stubs/ca_obj_stride.rb +5 -7
  227. data/yard-stubs/ca_obj_tile.rb +1 -1
  228. data/yard-stubs/ca_obj_time.rb +43 -0
  229. data/yard-stubs/ca_obj_transpose.rb +1 -1
  230. data/yard-stubs/ca_obj_window.rb +28 -16
  231. data/yard-stubs/carray_access.rb +55 -6
  232. data/yard-stubs/carray_attribute.rb +26 -38
  233. data/yard-stubs/carray_cast.rb +121 -162
  234. data/yard-stubs/carray_class.rb +3 -4
  235. data/yard-stubs/carray_conversion.rb +6 -7
  236. data/yard-stubs/carray_copy.rb +2 -1
  237. data/yard-stubs/carray_count.rb +10 -5
  238. data/yard-stubs/carray_element.rb +4 -3
  239. data/yard-stubs/carray_limits.rb +100 -0
  240. data/yard-stubs/carray_loop.rb +0 -20
  241. data/yard-stubs/carray_mask.rb +59 -41
  242. data/yard-stubs/carray_math.rb +162 -100
  243. data/yard-stubs/carray_mathfunc.rb +415 -3
  244. data/yard-stubs/carray_median_percentile.rb +29 -11
  245. data/yard-stubs/carray_memory_view.rb +2 -2
  246. data/yard-stubs/carray_order.rb +72 -33
  247. data/yard-stubs/carray_random.rb +6 -6
  248. data/yard-stubs/carray_reduce.rb +353 -0
  249. data/yard-stubs/carray_rng.rb +92 -0
  250. data/yard-stubs/carray_scatter.rb +12 -3
  251. data/yard-stubs/carray_slab.rb +7 -4
  252. data/yard-stubs/carray_sort.rb +7 -6
  253. data/yard-stubs/carray_test.rb +18 -5
  254. data/yard-stubs/carray_undef.rb +44 -5
  255. data/yard-stubs/ruby_carray.rb +75 -40
  256. metadata +63 -25
  257. data/CHANGELOG.v1.md +0 -126
  258. data/ext/carray_build_flags.h +0 -3
  259. data/ext/carray_config.h +0 -28
  260. data/ext/carray_data_type.c +0 -90
  261. data/ext/carray_kernels_bincmp.c +0 -6457
  262. data/ext/carray_kernels_binop.c +0 -31534
  263. data/ext/carray_kernels_init.c +0 -1734
  264. data/ext/carray_kernels_moncmp.c +0 -2104
  265. data/ext/carray_kernels_monop.c +0 -25387
  266. data/ext/carray_kernels_reduce_aggregate.c +0 -27545
  267. data/ext/carray_kernels_reduce_boolean.c +0 -1924
  268. data/ext/carray_kernels_reduce_cumulative.c +0 -11646
  269. data/ext/carray_kernels_reduce_extreme.c +0 -21631
  270. data/ext/carray_kernels_reduce_variance.c +0 -5981
  271. data/ext/carray_kernels_scan.c +0 -4302
  272. data/ext/carray_kernels_search.c +0 -37776
  273. data/ext/carray_kernels_sort.c +0 -10814
  274. data/ext/carray_kernels_triop.c +0 -1864
  275. data/lib/carray/construct.rb +0 -360
  276. data/lib/carray/data_type_extension.rb +0 -261
  277. data/lib/carray/data_type_limits.rb +0 -91
data/lib/carray/lazy.rb CHANGED
@@ -90,6 +90,7 @@ class CArray
90
90
  # can be called from any Ractor.
91
91
  module LazyDispatch; end
92
92
  prepend LazyDispatch
93
+ private_constant :LazyDispatch
93
94
 
94
95
  CArray.send(:define_named_methods, LazyDispatch,
95
96
  LAZY_MONOP_OP_IDS.reject { |name, _| name == :round }, template: <<~'RUBY')
@@ -102,6 +103,10 @@ class CArray
102
103
  end
103
104
  RUBY
104
105
 
106
+ # A kernel named `<x>_i` is an internal part, bound privately in C; its
107
+ # lazy dispatch is private too.
108
+ LazyDispatch.send(:private, *LAZY_MONOP_OP_IDS.keys.grep(/_i\z/))
109
+
105
110
  # round takes digits, which the lazy node does not carry: rounding to
106
111
  # digits evaluates the expression and rounds the result.
107
112
  module LazyDispatch
@@ -195,6 +200,8 @@ class CArray
195
200
  module LazyDispatch
196
201
  def real
197
202
  if lazy_view?
203
+ refuse_partless("real")
204
+ return super() if object?
198
205
  if complex?
199
206
  float_dt = (data_type == CA_CMPLX64) ? CA_FLOAT32 : CA_FLOAT64
200
207
  # float_dt is a Symbol; convert via data_type_code for kernel
@@ -217,6 +224,7 @@ class CArray
217
224
  module LazyDispatch
218
225
  def imag
219
226
  if lazy_view?
227
+ refuse_partless("imag")
220
228
  imag_i_node = CAMonOp.__build__(self, CAMonOp::OP_IMAG_I)
221
229
  if boolean?
222
230
  # imag_i reads a boolean as int64; the eager imag stays boolean.
@@ -337,16 +345,18 @@ class CArray
337
345
  module LazyDispatch
338
346
  private
339
347
 
340
- def face_among_operands? (*operands)
348
+ def face_among_operands?(*operands)
341
349
  operands.any? { |o| o.is_a?(CArray) && o.face? }
342
350
  end
343
351
 
344
- def computed_operand (operand)
352
+ def computed_operand(operand)
345
353
  ( operand.is_a?(CArray) && operand.lazy_view? ) ? operand.copy : operand
346
354
  end
347
355
  end
348
356
 
349
- CArray.send(:define_named_methods, LazyDispatch, LAZY_BINOP_OP_IDS, template: <<~'RUBY')
357
+ # `**` is written out below, for its Integer exponent.
358
+ CArray.send(:define_named_methods, LazyDispatch,
359
+ LAZY_BINOP_OP_IDS.reject { |name, _| name == :** }, template: <<~'RUBY')
350
360
  def __NAME__(other)
351
361
  if lazy_view? || (other.is_a?(CArray) && other.lazy_view?)
352
362
  if face_among_operands?(self, other)
@@ -384,12 +394,6 @@ class CArray
384
394
  alias_method word, op
385
395
  end
386
396
 
387
- # `fmax` / `fmin` are rb_define_alias'd to `pmax` / `pmin` at Init
388
- # time (the same C alias); they are aliased again beside them, in
389
- # LazyDispatch.
390
- LazyDispatch.send(:alias_method, :fmax, :pmax)
391
- LazyDispatch.send(:alias_method, :fmin, :pmin)
392
-
393
397
  # ---------------------------------------------------------------------------
394
398
  # Triop dispatch (fma / fms / clip). CATriOp is the CABinOp analog for
395
399
  # three-operand element-wise ops. Each Ruby method redefined below
@@ -430,32 +434,31 @@ class CArray
430
434
  private :clamp_between
431
435
  LazyDispatch.send(:private, :clamp_between)
432
436
 
433
- # `**` integer-exponent lazy fast path.
437
+ # `**` with an Integer exponent.
434
438
  #
435
- # The default OP_POW path above goes through the `power` binop kernel,
436
- # which uses `pow(x, p)` / `cpow(x, p)` (transcendental, ~20-50 ns/cell)
437
- # for float / complex parents. When `other` is an Integer, the eager
438
- # `rb_ca_pow` dispatches to `rb_ca_ipower` (= binary exponentiation via
439
- # `op_powi_<type>`, ~1-3 mul/cell, 3-5x faster than the transcendental
440
- # form). Mirror that dispatch here so `a.lazy ** 2` rides the same
441
- # fast path.
442
- #
443
- # Routing rule (matches eager `rb_ca_pow`):
439
+ # The eager `rb_ca_pow` sends a Float / Complex array to an Integer power
440
+ # through `rb_ca_ipower` (ext/ca_op_ipower.c), which keeps the exponent
441
+ # an int64 and computes each cell with pow / powf / cpow, the answer the
442
+ # `power` kernel gives for the same exponent held in an array. The lazy
443
+ # form builds the matching CABinOp(OP_IPOWER) node:
444
444
  # - lazy parent + Integer exponent + Float/Complex parent data_type
445
445
  # -> wrap exponent as CScalar.int64 + CABinOp(OP_IPOWER)
446
- # - lazy parent (other cases) -> CABinOp(OP_POW) [existing]
447
- # - else -> eager (which itself dispatches to ipower internally)
446
+ # - lazy parent (other cases) -> CABinOp(OP_POW)
447
+ # - else -> eager
448
448
  #
449
- # Integer parents intentionally stay on OP_POW: the mkkernel-generated
450
- # `power` binop kernel already uses `op_powi_<type>` for integer data_types
451
- # via the `int:` expr in MkKernel.binop :power, so OP_POW + int parent
452
- # is already fast. The lazy-substrate gap was Float/Complex only.
449
+ # Integer parents stay on OP_POW: the `power` kernel's `int:` expr is
450
+ # `op_powi_<type>`, binary exponentiation in the integer type.
453
451
  module LazyDispatch
454
452
  def **(other)
455
453
  if ( lazy_view? || (other.is_a?(CArray) && other.lazy_view?) ) &&
456
454
  face_among_operands?(self, other)
457
455
  return computed_operand(self) ** computed_operand(other)
458
456
  end
457
+ # An exponent beyond int64 is taken as a Float, as the eager pow does.
458
+ if other.is_a?(Integer) && (float? || complex?) &&
459
+ ! other.between?(-2**63, 2**63 - 1)
460
+ other = other.to_f
461
+ end
459
462
  if other.is_a?(Integer) && (float? || complex?) &&
460
463
  (lazy_view? || (other.is_a?(CArray) && other.lazy_view?))
461
464
  CABinOp.__build__(self, CScalar.int64.tap { |s| s[0] = other },
@@ -471,10 +474,18 @@ class CArray
471
474
  # `pow` is the canonical C method (defined by rb_define_method in
472
475
  # ext/ca_op_ipower.c) that `**` is rb_define_alias'd to. The `**`
473
476
  # dispatch in this file does not reach `pow` through the C alias, so
474
- # `a.lazy.pow(2)` would fall to eager; `pow` is aliased again here.
475
- # An alias copies the method as it is at that moment, so this follows
476
- # the integer-exponent `**` above, not the template one.
477
- alias_method :pow, :**
477
+ # `a.lazy.pow(2)` would fall to eager; `pow` is defined again here to
478
+ # call `**`. It is a method of its own rather than an alias so that a
479
+ # message names `pow` when `pow` was called (a frame of an alias carries
480
+ # the original name).
481
+ #
482
+ # An Integer exponent on an integer array is a count before either form
483
+ # sees it (see CArray::IntegerPowerCount); prepended first, so that `**`
484
+ # goes through it.
485
+ prepend IntegerPowerCount
486
+ def pow(other)
487
+ self ** other
488
+ end
478
489
 
479
490
  # coerce: when self is a lazy view and the scalar appears on the LEFT
480
491
  # (e.g. `2 * a.lazy`), Ruby's Numeric#* calls a.lazy.coerce(2). The
@@ -498,7 +509,8 @@ class CArray
498
509
  # `eq` / `feq`.
499
510
  #
500
511
  # A comparison with UNDEF goes to the eager method, which answers `eq` /
501
- # `ne` from the mask and refuses the others.
512
+ # `ne` from the mask and refuses the others, and so does one with nil on
513
+ # an array that can not hold it.
502
514
  #
503
515
  # `feq` is arity 1 in eager (compile-time FLT_EPSILON / DBL_EPSILON);
504
516
  # we mirror that here. Runtime eps is a future extension (struct field
@@ -553,7 +565,7 @@ class CArray
553
565
  if face_among_operands?(self, other)
554
566
  return computed_operand(self).__send__(__method__, computed_operand(other), tol)
555
567
  end
556
- CABinCmp.__build__(self, other, __VALUE__, tol.to_f.abs)
568
+ CABinCmp.__build__(self, other, __VALUE__, tol)
557
569
  else
558
570
  super(other, tol)
559
571
  end
@@ -596,26 +608,46 @@ class CArray
596
608
 
597
609
  # An Integer the array's type cannot hold, on the left of an operator.
598
610
  # A comparison is answered from the array's side, where it compares by
599
- # value; any other operator refuses the Integer as the array would.
611
+ # value, and so are + * & | ^, which give the same answer either way round
612
+ # (and -, as the negated difference): the array takes the Integer modulo
613
+ # 2 ** bits. ** takes it as the base of each power. Any other operator
614
+ # refuses the Integer as the array would.
600
615
  class OutOfRangeInteger
601
- def initialize (value, array, error)
616
+ def initialize(value, array, error)
602
617
  @value, @array, @error = value, array, error
603
618
  end
604
619
 
605
- def < (other) = flip(other, :>)
606
- def <= (other) = flip(other, :>=)
607
- def > (other) = flip(other, :<)
608
- def >= (other) = flip(other, :<=)
620
+ def <(other) = flip(other, :>)
621
+ def <=(other) = flip(other, :>=)
622
+ def >(other) = flip(other, :<)
623
+ def >=(other) = flip(other, :<=)
624
+
625
+ def +(other) = flip(other, :+)
626
+ def *(other) = flip(other, :*)
627
+ def &(other) = flip(other, :&)
628
+ def |(other) = flip(other, :|)
629
+ def ^(other) = flip(other, :^)
609
630
 
610
- def method_missing (*)
631
+ def -(other)
632
+ return -flip(other, :-)
633
+ end
634
+
635
+ # the base cut to the type where the exponent is 0 or more, 0 where it
636
+ # is negative (CArray::IntegerPowerCount#power_with_unfit_base)
637
+ def **(other)
638
+ raise @error unless other.equal?(@array)
639
+ return @array.send(:power_with_unfit_base, @value)
640
+ end
641
+
642
+ def method_missing(*)
611
643
  raise @error
612
644
  end
613
645
 
614
- def respond_to_missing? (*) = false
646
+ def respond_to_missing?(*) = false
615
647
 
616
648
  private
617
649
 
618
- def flip (other, op)
650
+ def flip(other, op)
619
651
  raise @error unless other.equal?(@array)
620
652
  @array.send(op, @value)
621
653
  end
@@ -648,36 +680,8 @@ class CAMonOp
648
680
  end
649
681
  private :op_label
650
682
 
651
- # @overload inspect
652
- # Returns a short summary of the lazy chain without
653
- # materialising.
654
- # @return [String]
655
- def inspect
656
- "#<CAMonOp #{op_label}(#{parent.inspect})>"
657
- end
658
-
659
- # @overload to_s
660
- # Alias of {#inspect}.
661
- # @return [String]
662
- def to_s
663
- inspect
664
- end
665
-
666
- # @overload dump_tree(indent = 0)
667
- # Returns an ASCII summary of the lazy expression tree.
668
- # Does not materialise.
669
- # @param indent [Integer] leading indentation depth.
670
- # @return [String]
671
- def dump_tree(indent = 0)
672
- pad = " " * indent
673
- out = +"#{pad}#{op_label}\n"
674
- out << if parent.is_a?(CAMonOp)
675
- parent.dump_tree(indent + 1)
676
- else
677
- "#{" " * (indent + 1)}#{parent.class}(#{parent.data_type_name}, #{parent.shape.inspect})\n"
678
- end
679
- out
680
- end
683
+ def tree_operands = [parent]
684
+ private :tree_operands
681
685
 
682
686
  # @overload each { |value| ... }
683
687
  # Materialises `self` and yields each element of the resulting
@@ -738,35 +742,16 @@ class CABinOp
738
742
  end
739
743
  private :op_label
740
744
 
741
- # @overload inspect
742
- # Returns a summary of the binop chain without materialising.
743
- # @return [String]
744
- def inspect
745
- "#<CABinOp #{op_label}(#{parent.inspect}, #{__binop_right__.inspect})>"
746
- end
745
+ def tree_operands = [parent, __binop_right__]
746
+ private :tree_operands
747
747
 
748
- # @overload to_s
749
- # Alias of {#inspect}.
750
- # @return [String]
751
- def to_s
752
- inspect
753
- end
754
-
755
- # @overload dump_tree(indent = 0)
756
- # Returns an ASCII summary of the lazy expression tree, walking
757
- # both children. Does not materialise.
758
- # @return [String]
759
- def dump_tree(indent = 0)
760
- pad = " " * indent
761
- out = +"#{pad}#{op_label}\n"
762
- [parent, __binop_right__].each do |child|
763
- out << if child.is_a?(CAMonOp) || child.is_a?(CABinOp)
764
- child.dump_tree(indent + 1)
765
- else
766
- "#{" " * (indent + 1)}#{child.class}(#{child.data_type_name}, #{child.shape.inspect})\n"
767
- end
768
- end
769
- out
748
+ # @overload parents
749
+ # Returns the two operands, left then right, as the expression holds
750
+ # them. The first is also what `parent` returns. The Array is a new
751
+ # one on each call.
752
+ # @return [Array<CArray>]
753
+ def parents
754
+ [parent, __binop_right__]
770
755
  end
771
756
 
772
757
  # @overload each { |value| ... }
@@ -794,19 +779,130 @@ class CABinOp
794
779
  end
795
780
  end
796
781
 
782
+ class CABinCmp
783
+ # @overload parents
784
+ # Returns the two operands of the comparison, left then right, as the
785
+ # expression holds them. The first is also what `parent` returns. The
786
+ # Array is a new one on each call.
787
+ # @return [Array<CArray>]
788
+ def parents
789
+ [parent, __bincmp_right__]
790
+ end
791
+ end
792
+
793
+ class CATriOp
794
+ # @overload parents
795
+ # Returns the three operands in order, as the expression holds them.
796
+ # The first is also what `parent` returns. The Array is a new one on
797
+ # each call.
798
+ # @return [Array<CArray>]
799
+ def parents
800
+ [parent, __triop_op2__, __triop_op3__]
801
+ end
802
+ end
803
+
804
+ class CATriOp
805
+ OP_NAMES = {
806
+ OP_FMA => :fma, OP_FMS => :fms, OP_CLIP => :clip, OP_SELECT => :select,
807
+ }.freeze
808
+
809
+ def op_label
810
+ op_id = __op_id__
811
+ (OP_NAMES[op_id] || "op_#{op_id}").to_s
812
+ end
813
+ private :op_label
814
+
815
+ def tree_operands = [parent, __triop_op2__, __triop_op3__]
816
+ private :tree_operands
817
+ end
818
+
819
+ class CAMonCmp
820
+ OP_NAMES = CArray::LAZY_MONCMP_OP_IDS.invert.freeze
821
+
822
+ def op_label
823
+ op_id = __op_id__
824
+ (OP_NAMES[op_id] || "op_#{op_id}").to_s
825
+ end
826
+ private :op_label
827
+
828
+ def tree_operands = [parent]
829
+ private :tree_operands
830
+ end
831
+
832
+ class CABinCmp
833
+ # The symbol of an operator (:<) is listed after its word (:lt), so it
834
+ # is the name the inverted table keeps.
835
+ OP_NAMES = CArray::LAZY_BINCMP_OP_IDS.merge(CArray::LAZY_BINCMP_TOL_OP_IDS)
836
+ .invert.freeze
837
+
838
+ def op_label
839
+ op_id = __op_id__
840
+ (OP_NAMES[op_id] || "op_#{op_id}").to_s
841
+ end
842
+ private :op_label
843
+
844
+ def tree_operands = [parent, __bincmp_right__]
845
+ private :tree_operands
846
+ end
847
+
848
+ # inspect and dump_tree of every lazy operation. An operand that is itself a
849
+ # lazy operation is described the same way; any other operand (an array, a
850
+ # scalar, a marker) by its class, data type and shape, not its values, so the
851
+ # description evaluates nothing and does not enter an operand that holds the
852
+ # expression in a cell.
853
+ [CAMonOp, CABinOp, CATriOp, CAMonCmp, CABinCmp].each do |klass|
854
+ klass.class_eval do
855
+ # @overload inspect
856
+ # Returns a summary of the lazy expression, naming each operation and
857
+ # showing the other operands by class, data type and shape. Does not
858
+ # materialise.
859
+ # @return [String]
860
+ def inspect
861
+ operands = tree_operands.map { |child|
862
+ lazy_operation?(child) ? child.inspect : operand_summary(child)
863
+ }
864
+ "#<#{self.class} #{op_label}(#{operands.join(", ")})>"
865
+ end
866
+
867
+ # @overload dump_tree(indent = 0)
868
+ # Returns an ASCII summary of the lazy expression tree, one line per
869
+ # node, with the operands of each operation indented under it.
870
+ # Operands that are lazy operations are expanded; other operands are
871
+ # shown by class, data type and shape. Does not materialise.
872
+ # @param indent [Integer] leading indentation depth.
873
+ # @return [String]
874
+ def dump_tree(indent = 0)
875
+ out = +"#{" " * indent}#{op_label}\n"
876
+ tree_operands.each do |child|
877
+ out << if lazy_operation?(child)
878
+ child.dump_tree(indent + 1)
879
+ else
880
+ "#{" " * (indent + 1)}#{operand_summary(child)}\n"
881
+ end
882
+ end
883
+ out
884
+ end
885
+
886
+ private
887
+
888
+ def lazy_operation?(child)
889
+ child.is_a?(CAMonOp) || child.is_a?(CABinOp) || child.is_a?(CATriOp) ||
890
+ child.is_a?(CAMonCmp) || child.is_a?(CABinCmp)
891
+ end
892
+
893
+ def operand_summary(child)
894
+ "#{child.class}(#{child.data_type_name}, #{child.shape.inspect})"
895
+ end
896
+ end
897
+ end
898
+
797
899
  class CALazyMarker
798
900
  # @overload inspect
799
- # Returns a short summary showing the wrapped entity.
901
+ # Returns a short summary naming the wrapped array by class, data type
902
+ # and shape.
800
903
  # @return [String]
801
904
  def inspect
802
- "#<CALazyMarker over #{parent.inspect}>"
803
- end
804
-
805
- # @overload to_s
806
- # Alias of {#inspect}.
807
- # @return [String]
808
- def to_s
809
- inspect
905
+ "#<CALazyMarker over #{parent.class}(#{parent.data_type_name}, #{parent.shape.inspect})>"
810
906
  end
811
907
 
812
908
  # @overload dump_tree(indent = 0)
@@ -860,6 +956,7 @@ end
860
956
  [CAMonOp, CABinOp, CATriOp, CAMonCmp, CABinCmp, CALazyMarker].each do |klass|
861
957
  klass.class_eval do
862
958
  def to_ca(writable: false)
959
+ writable = CArray.send(:bool_arg, writable, "writable", "to_ca")
863
960
  if writable
864
961
  raise "#{self.class}#to_ca evaluates into a new entity; " \
865
962
  "it can't satisfy `writable: true'"
@@ -926,9 +1023,9 @@ class << CArray
926
1023
  Ractor.current == Ractor.main ? @expression_evaluator : nil
927
1024
  end
928
1025
 
929
- # @param object [#call, nil] see {#expression_evaluator}
1026
+ # @param object [#call, nil] see {CArray.expression_evaluator}
930
1027
  # @raise [ArgumentError] when it cannot be called.
931
- def expression_evaluator= (object)
1028
+ def expression_evaluator=(object)
932
1029
  if object && ! object.respond_to?(:call)
933
1030
  raise ArgumentError,
934
1031
  "an expression evaluator must respond to #call(plan, out)"
@@ -958,7 +1055,7 @@ class << CArray
958
1055
  # @raise [ArgumentError] when the block's source cannot be read -- in
959
1056
  # irb, in `eval`, or from a file that is gone. Write `.lazy` on the
960
1057
  # operands there: `a.lazy + b.lazy`.
961
- def fuse (*args, &block)
1058
+ def fuse(*args, &block)
962
1059
  raise LocalJumpError, "CArray.fuse requires a block" unless block
963
1060
  unless args.empty?
964
1061
  raise ArgumentError,
@@ -966,6 +1063,6 @@ class << CArray
966
1063
  "`CArray.fuse { a + b }`, and the names in it are read from " \
967
1064
  "where the block was written"
968
1065
  end
969
- CArray::FuseSource.evaluate(block)
1066
+ CArray.const_get(:FuseSource).evaluate(block)
970
1067
  end
971
1068
  end
@@ -0,0 +1,80 @@
1
+ # What each numeric data type can hold, as constants on CArray::Limits.
2
+ #
3
+ # CArray::Limits::INT32_MAX # => 2147483647
4
+ # CArray::Limits::FLOAT32_EPSILON # => 1.1920928955078125e-07
5
+ #
6
+ # MIN and MAX bracket the range for every numeric type alike, so for a
7
+ # float type MIN is the bottom of the range, not Ruby's Float::MIN:
8
+ #
9
+ # CArray::Limits::FLOAT64_MIN # => -1.7976931348623157e+308
10
+ # Float::MIN # => 2.2250738585072014e-308
11
+ #
12
+ # Ruby's Float::MIN is the smallest positive normal, which is TINY here.
13
+ # Code that has to bracket a type without knowing whether it is integer
14
+ # or float reads MIN and MAX and is right either way.
15
+ #
16
+ # Only the integer and float types have limits. A complex type is a
17
+ # pair of floats and is bounded by its component's limits (FLOAT32_* for
18
+ # cmplx64, FLOAT64_* for cmplx128). boolean, fixlen and object have no
19
+ # numeric range.
20
+
21
+ class CArray
22
+
23
+ module Limits
24
+
25
+ # The integer widths come from the extension rather than from a
26
+ # table written here, so a platform where a type is not the usual
27
+ # width is described correctly instead of confidently mis-described.
28
+
29
+ {
30
+ int8: true, int16: true, int32: true, int64: true,
31
+ uint8: false, uint16: false, uint32: false, uint64: false,
32
+ }.each do |type, signed|
33
+ bits = 8 * CArray.sizeof(type)
34
+ name = type.to_s.upcase
35
+ if signed
36
+ const_set(:"#{name}_MIN", -(2 ** (bits - 1)))
37
+ const_set(:"#{name}_MAX", 2 ** (bits - 1) - 1)
38
+ else
39
+ const_set(:"#{name}_MIN", 0)
40
+ const_set(:"#{name}_MAX", 2 ** bits - 1)
41
+ end
42
+ end
43
+
44
+ # float32 and float64 are IEEE-754 binary32 and binary64, and the
45
+ # four values below follow from the format: with `p` significand
46
+ # bits (the implicit one included) and `emax` the exponent of the
47
+ # largest finite value,
48
+ #
49
+ # EPSILON = 2 ** (1 - p) the step from 1.0 to the next float
50
+ # MAX = (2 - EPSILON) * 2 ** emax
51
+ # MIN = -MAX every format here is symmetric
52
+ # TINY = 2 ** (1 - emax) the smallest positive normal
53
+ #
54
+ # All four are exactly representable as a Ruby Float, binary32
55
+ # included, so nothing is rounded on the way in. The width is
56
+ # checked rather than assumed: on a platform where C float or double
57
+ # is not one of these formats the arithmetic below would be wrong,
58
+ # and a wrong limit is worse than a missing one.
59
+
60
+ {
61
+ float32: [4, 24, 127],
62
+ float64: [8, 53, 1023],
63
+ }.each do |type, (bytes, precision, max_exponent)|
64
+ actual = CArray.sizeof(type)
65
+ unless actual == bytes
66
+ raise "#{type} is #{actual} bytes wide, not the #{bytes * 8}-bit IEEE-754 " \
67
+ "format its limits are derived from"
68
+ end
69
+ name = type.to_s.upcase
70
+ epsilon = 2.0 ** (1 - precision)
71
+ max = (2.0 - epsilon) * 2.0 ** max_exponent
72
+ const_set(:"#{name}_EPSILON", epsilon)
73
+ const_set(:"#{name}_MAX", max)
74
+ const_set(:"#{name}_MIN", -max)
75
+ const_set(:"#{name}_TINY", 2.0 ** (1 - max_exponent))
76
+ end
77
+
78
+ end
79
+
80
+ end
@@ -14,9 +14,8 @@
14
14
  # (a positional Symbol is already a valid constant fill, e.g.
15
15
  # `strip_mask(:na)`). Passing both raises ArgumentError.
16
16
  #
17
- # Design / rationale: devel/PROPOSAL_UNMASK_FILL_METHOD.md. The forward
18
- # hold is the C primitive #hold_forward; backward = flip -> forward -> flip,
19
- # flatten = flatten -> hold -> reshape, and :linear composes
17
+ # The forward hold is the C primitive #hold_forward; backward = flip ->
18
+ # forward -> flip, flatten = flatten -> hold -> reshape, and :linear composes
20
19
  # linear_section / linear_fetch. User-facing YARD docs live in
21
20
  # yard-stubs/carray_mask.rb.
22
21
  #
@@ -38,7 +37,7 @@ class CArray
38
37
  # unmask / strip_mask with method:, in front of the C constant-fill
39
38
  # primitives, which they reach with super.
40
39
  module GapFill
41
- def unmask (fill = MASK_FILL_UNSET, method: nil, axis: nil)
40
+ def unmask(fill = MASK_FILL_UNSET, method: nil, axis: nil)
42
41
  if method
43
42
  unless fill.equal?(MASK_FILL_UNSET)
44
43
  raise ArgumentError,
@@ -55,10 +54,15 @@ class CArray
55
54
  self[filled] = held[filled] if filled.any
56
55
  return self
57
56
  end
58
- fill.equal?(MASK_FILL_UNSET) ? super() : super(fill)
57
+ if fill.equal?(MASK_FILL_UNSET)
58
+ raise ArgumentError,
59
+ "unmask: a fill value is required (or method:); " \
60
+ "to clear the mask without a value, assign mask = false"
61
+ end
62
+ super(fill)
59
63
  end
60
64
 
61
- def strip_mask (fill = MASK_FILL_UNSET, method: nil, axis: nil)
65
+ def strip_mask(fill = MASK_FILL_UNSET, method: nil, axis: nil)
62
66
  if method
63
67
  unless fill.equal?(MASK_FILL_UNSET)
64
68
  raise ArgumentError,
@@ -73,6 +77,7 @@ class CArray
73
77
  end
74
78
  end
75
79
  prepend GapFill
80
+ private_constant :GapFill
76
81
 
77
82
  private
78
83
 
@@ -80,12 +85,12 @@ class CArray
80
85
  # unfillable cells stay masked; a fully filled result carries no mask
81
86
  # (hold allocates the output mask only when a leading run goes UNDEF;
82
87
  # :linear masks only the exterior via mask_invalid).
83
- def gap_fill (method, axis, name)
88
+ def gap_fill(method, axis, name)
84
89
  axis = normalize_axis(axis, name) unless axis.nil?
85
90
  case method
86
- when :forward, :ffill
91
+ when :forward
87
92
  hold_along_axis(axis, false)
88
- when :backward, :bfill
93
+ when :backward
89
94
  hold_along_axis(axis, true)
90
95
  when :linear
91
96
  gap_fill_linear(axis)
@@ -99,12 +104,12 @@ class CArray
99
104
  # Forward (backward = false) or backward (true) hold along `axis`, already
100
105
  # normalised by gap_fill (axis nil = flatten). Backward reuses the forward primitive on the
101
106
  # reversed view; flatten flattens, holds axis 0, reshapes back.
102
- def hold_along_axis (axis, backward)
107
+ def hold_along_axis(axis, backward)
103
108
  if axis.nil?
104
109
  flat = flatten
105
- flat = flat.reverse if backward
110
+ flat = flat.flip if backward
106
111
  held = flat.send(:hold_forward, 0)
107
- held = held.reverse if backward
112
+ held = held.flip if backward
108
113
  held.reshape(*shape)
109
114
  else
110
115
  if backward
@@ -121,7 +126,7 @@ class CArray
121
126
  # linear_fetch, which keeps its unit and rounds to its grid. Defining one
122
127
  # is how a Face says it interpolates: without it, the core linear_fetch
123
128
  # reads the Face's storage and fails from inside.
124
- def gap_fill_linear (axis)
129
+ def gap_fill_linear(axis)
125
130
  if face?
126
131
  unless interpolable?
127
132
  raise ArgumentError,
@@ -156,7 +161,7 @@ class CArray
156
161
  end
157
162
 
158
163
  # 1-D linear-by-index fill of a single (possibly masked) fiber.
159
- def linear_fill_fiber (vec)
164
+ def linear_fill_fiber(vec)
160
165
  vec.send(:linear_fill_along, CArray.float64(vec.elements) { |i| i.to_f })
161
166
  end
162
167
 
@@ -174,7 +179,7 @@ class CArray
174
179
  # linear_section answers NaN -- a property of x, not of the values, so a
175
180
  # NaN or Inf stored in a present cell is left alone. A Face goes through
176
181
  # its own linear_fetch, which keeps its unit and rounds to its grid.
177
- def linear_fill_along (x)
182
+ def linear_fill_along(x)
178
183
  # A cell whose coordinate is masked, NaN or infinite has no place on the
179
184
  # line: it is no point to interpolate between, and is not filled.
180
185
  placed = x.is_not_masked
@@ -220,7 +225,7 @@ class CArray
220
225
  # Yield an index key (Array with `nil` at `axis`, integers elsewhere)
221
226
  # for every fiber along `axis`. `self[*key]` is that fiber as a masked
222
227
  # 1-D view.
223
- def each_fiber_key (axis)
228
+ def each_fiber_key(axis)
224
229
  outer = shape
225
230
  outer_dims = outer.each_index.reject { |k| k == axis }.map { |k| outer[k] }
226
231
  idx = Array.new(outer_dims.size, 0)