carray 3.0.7 → 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 (275) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +1 -1
  3. data/CHANGELOG.md +7 -1940
  4. data/README.md +38 -32
  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 +6 -9
  11. data/ext/ca_bincmp_dispatch.h +16 -38
  12. data/ext/ca_binop_dispatch.c +27 -0
  13. data/ext/ca_binop_dispatch.h +22 -47
  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 -1506
  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 +90 -163
  28. data/ext/ca_obj_bincmp.c +47 -46
  29. data/ext/ca_obj_binop.c +38 -38
  30. data/ext/ca_obj_bitarray.c +5 -17
  31. data/ext/ca_obj_bitfield.c +15 -30
  32. data/ext/ca_obj_block.c +11 -12
  33. data/ext/ca_obj_byte_swap.c +32 -30
  34. data/ext/ca_obj_const_string.c +57 -62
  35. data/ext/ca_obj_face.c +99 -91
  36. data/ext/ca_obj_face.h +32 -170
  37. data/ext/ca_obj_fake.c +15 -21
  38. data/ext/ca_obj_farray.c +7 -9
  39. data/ext/ca_obj_field.c +17 -13
  40. data/ext/ca_obj_fixlen_string.c +8 -19
  41. data/ext/ca_obj_grid.c +137 -106
  42. data/ext/ca_obj_meld.c +23 -21
  43. data/ext/ca_obj_moncmp.c +10 -21
  44. data/ext/ca_obj_monop.c +24 -35
  45. data/ext/ca_obj_object.c +51 -37
  46. data/ext/ca_obj_record.c +74 -22
  47. data/ext/ca_obj_reduce.c +8 -10
  48. data/ext/ca_obj_refer.c +42 -29
  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 +18 -24
  52. data/ext/ca_obj_select.c +10 -22
  53. data/ext/ca_obj_select_axis.c +27 -36
  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 +28 -24
  57. data/ext/ca_obj_stride.c +53 -54
  58. data/ext/ca_obj_string.c +6 -18
  59. data/ext/ca_obj_tile.c +12 -12
  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 +35 -39
  64. data/ext/ca_obj_window.c +67 -86
  65. data/ext/ca_op_cmplx64.h +4 -0
  66. data/ext/ca_op_ipower.c +34 -12
  67. data/ext/ca_op_powi.h +4 -0
  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 +20 -26
  72. data/ext/ca_sweep_engine.c +2 -3
  73. data/ext/ca_sweep_engine.h +17 -9
  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 +11 -11
  77. data/ext/carray.h +300 -1011
  78. data/ext/carray_access.c +234 -180
  79. data/ext/carray_address_basis.c +33 -38
  80. data/ext/carray_attribute.c +81 -104
  81. data/ext/carray_bincount.c +12 -14
  82. data/ext/carray_broadcast.c +30 -26
  83. data/ext/carray_call_cfunc.c +169 -277
  84. data/ext/carray_call_cfunc.h +36 -28
  85. data/ext/carray_cast.c +912 -346
  86. data/ext/carray_cast_func.rb +31 -3
  87. data/ext/carray_class.c +6 -11
  88. data/ext/carray_conversion.c +43 -38
  89. data/ext/carray_copy.c +28 -26
  90. data/ext/carray_core.c +217 -126
  91. data/ext/carray_count.c +27 -15
  92. data/ext/carray_element.c +40 -25
  93. data/ext/carray_factorize.c +53 -52
  94. data/ext/carray_generate.c +26 -19
  95. data/ext/carray_histogram.c +8 -10
  96. data/ext/carray_hold.c +7 -9
  97. data/ext/carray_index_classifier.c +123 -228
  98. data/ext/carray_index_classifier.h +11 -10
  99. data/ext/carray_integer_total.c +523 -0
  100. data/ext/carray_internal.h +1274 -160
  101. data/ext/carray_key_windows.c +3 -3
  102. data/ext/carray_lazy.c +21 -36
  103. data/ext/carray_loop.c +19 -22
  104. data/ext/carray_mask.c +123 -145
  105. data/ext/carray_math_kernel.h +23 -47
  106. data/ext/carray_mathfunc.c +10 -3
  107. data/ext/carray_median_percentile.c +95 -94
  108. data/ext/carray_memory_view.c +42 -47
  109. data/ext/carray_operator.c +423 -122
  110. data/ext/carray_order.c +121 -85
  111. data/ext/carray_partition.c +13 -15
  112. data/ext/carray_random.c +47 -41
  113. data/ext/carray_round.c +9 -4
  114. data/ext/carray_scatter.c +54 -40
  115. data/ext/carray_segment.c +41 -45
  116. data/ext/carray_slab.c +64 -47
  117. data/ext/carray_slab.h +44 -33
  118. data/ext/carray_sort.c +32 -29
  119. data/ext/carray_sort_kernel.c +12 -10
  120. data/ext/carray_struct.c +38 -43
  121. data/ext/carray_test.c +47 -43
  122. data/ext/carray_time_text.c +24 -31
  123. data/ext/carray_undef.c +50 -7
  124. data/ext/carray_utils.c +258 -94
  125. data/ext/extconf.rb +24 -17
  126. data/ext/mk_call_cfunc.rb +19 -20
  127. data/ext/mk_pow5_table.rb +2 -1
  128. data/ext/mkkernel.rb +782 -461
  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 +264 -180
  135. data/lib/carray/basics.rb +272 -48
  136. data/lib/carray/bincount_nd.rb +33 -16
  137. data/lib/carray/block_iterator.rb +123 -102
  138. data/lib/carray/boolean_reduce.rb +7 -4
  139. data/lib/carray/categorical.rb +63 -51
  140. data/lib/carray/categorical_iterator.rb +89 -67
  141. data/lib/carray/complex.rb +61 -41
  142. data/lib/carray/conditional.rb +49 -22
  143. data/lib/carray/const_string.rb +54 -62
  144. data/lib/carray/core_extensions.rb +1 -1
  145. data/lib/carray/distance_limit.rb +7 -5
  146. data/lib/carray/factories.rb +269 -0
  147. data/lib/carray/fixlen_string.rb +25 -24
  148. data/lib/carray/frame/column_verbs.rb +20 -8
  149. data/lib/carray/frame/concat.rb +3 -3
  150. data/lib/carray/frame/convert.rb +2 -1
  151. data/lib/carray/frame/csv_parser.rb +7 -1
  152. data/lib/carray/frame/frame.rb +35 -25
  153. data/lib/carray/frame/group.rb +183 -150
  154. data/lib/carray/frame/io.rb +26 -7
  155. data/lib/carray/frame/join.rb +37 -12
  156. data/lib/carray/frame/records.rb +1 -1
  157. data/lib/carray/frame/reshape.rb +3 -3
  158. data/lib/carray/frame/sort.rb +10 -8
  159. data/lib/carray/frame/verbs.rb +148 -118
  160. data/lib/carray/frame/windows.rb +232 -21
  161. data/lib/carray/frame.rb +1 -2
  162. data/lib/carray/fuse_source.rb +24 -21
  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 +97 -24
  167. data/lib/carray/key_windows.rb +83 -57
  168. data/lib/carray/lazy.rb +196 -94
  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 +5 -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 +72 -71
  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/gather_nd.rb +106 -106
  183. data/lib/carray/methods/insert_block.rb +2 -2
  184. data/lib/carray/methods/is_between.rb +6 -4
  185. data/lib/carray/methods/is_in.rb +165 -145
  186. data/lib/carray/methods/join.rb +2 -1
  187. data/lib/carray/methods/lag.rb +6 -2
  188. data/lib/carray/methods/locate_addr.rb +150 -138
  189. data/lib/carray/methods/mask_duplicates.rb +1 -1
  190. data/lib/carray/methods/meshgrid.rb +28 -16
  191. data/lib/carray/methods/mode.rb +67 -67
  192. data/lib/carray/methods/nunique.rb +9 -7
  193. data/lib/carray/methods/repeat.rb +12 -12
  194. data/lib/carray/methods/resize.rb +1 -1
  195. data/lib/carray/methods/snap.rb +24 -20
  196. data/lib/carray/methods/string_format.rb +2 -2
  197. data/lib/carray/methods/unique.rb +4 -2
  198. data/lib/carray/methods/value_counts.rb +1 -1
  199. data/lib/carray/mkmf.rb +25 -28
  200. data/lib/carray/percentile_arguments.rb +82 -0
  201. data/lib/carray/record_order.rb +18 -17
  202. data/lib/carray/rng.rb +1 -0
  203. data/lib/carray/runtime.rb +190 -22
  204. data/lib/carray/segment_iterator.rb +251 -129
  205. data/lib/carray/serialize.rb +618 -571
  206. data/lib/carray/slab_iterator.rb +114 -114
  207. data/lib/carray/stack.rb +11 -12
  208. data/lib/carray/string.rb +16 -22
  209. data/lib/carray/string_operation_extension.rb +76 -52
  210. data/lib/carray/struct.rb +122 -164
  211. data/lib/carray/struct_builder.rb +243 -72
  212. data/lib/carray/time.rb +496 -342
  213. data/lib/carray/window_iterator.rb +191 -150
  214. data/lib/carray.rb +6 -11
  215. data/yard-stubs/ca_obj_array.rb +20 -135
  216. data/yard-stubs/ca_obj_bitarray.rb +1 -8
  217. data/yard-stubs/ca_obj_bitfield.rb +4 -3
  218. data/yard-stubs/ca_obj_byte_swap.rb +21 -7
  219. data/yard-stubs/ca_obj_fake.rb +3 -1
  220. data/yard-stubs/ca_obj_farray.rb +1 -1
  221. data/yard-stubs/ca_obj_field.rb +8 -6
  222. data/yard-stubs/ca_obj_refer.rb +7 -5
  223. data/yard-stubs/ca_obj_roll.rb +7 -6
  224. data/yard-stubs/ca_obj_shift.rb +2 -3
  225. data/yard-stubs/ca_obj_stride.rb +5 -7
  226. data/yard-stubs/ca_obj_tile.rb +1 -1
  227. data/yard-stubs/ca_obj_time.rb +43 -0
  228. data/yard-stubs/ca_obj_transpose.rb +1 -1
  229. data/yard-stubs/ca_obj_window.rb +26 -15
  230. data/yard-stubs/carray_access.rb +51 -4
  231. data/yard-stubs/carray_attribute.rb +26 -38
  232. data/yard-stubs/carray_cast.rb +118 -165
  233. data/yard-stubs/carray_class.rb +3 -4
  234. data/yard-stubs/carray_conversion.rb +6 -7
  235. data/yard-stubs/carray_copy.rb +2 -1
  236. data/yard-stubs/carray_count.rb +5 -3
  237. data/yard-stubs/carray_element.rb +4 -3
  238. data/yard-stubs/carray_limits.rb +100 -0
  239. data/yard-stubs/carray_loop.rb +0 -20
  240. data/yard-stubs/carray_mask.rb +59 -41
  241. data/yard-stubs/carray_math.rb +157 -100
  242. data/yard-stubs/carray_mathfunc.rb +415 -3
  243. data/yard-stubs/carray_median_percentile.rb +29 -11
  244. data/yard-stubs/carray_memory_view.rb +2 -2
  245. data/yard-stubs/carray_order.rb +67 -34
  246. data/yard-stubs/carray_random.rb +6 -6
  247. data/yard-stubs/carray_reduce.rb +353 -0
  248. data/yard-stubs/carray_rng.rb +92 -0
  249. data/yard-stubs/carray_scatter.rb +10 -2
  250. data/yard-stubs/carray_slab.rb +7 -4
  251. data/yard-stubs/carray_sort.rb +7 -6
  252. data/yard-stubs/carray_undef.rb +44 -5
  253. data/yard-stubs/ruby_carray.rb +75 -40
  254. metadata +62 -24
  255. data/CHANGELOG.v1.md +0 -126
  256. data/ext/carray_build_flags.h +0 -3
  257. data/ext/carray_config.h +0 -28
  258. data/ext/carray_data_type.c +0 -90
  259. data/ext/carray_kernels_bincmp.c +0 -6457
  260. data/ext/carray_kernels_binop.c +0 -31534
  261. data/ext/carray_kernels_init.c +0 -1734
  262. data/ext/carray_kernels_moncmp.c +0 -2104
  263. data/ext/carray_kernels_monop.c +0 -25337
  264. data/ext/carray_kernels_reduce_aggregate.c +0 -27545
  265. data/ext/carray_kernels_reduce_boolean.c +0 -1924
  266. data/ext/carray_kernels_reduce_cumulative.c +0 -11646
  267. data/ext/carray_kernels_reduce_extreme.c +0 -21631
  268. data/ext/carray_kernels_reduce_variance.c +0 -5981
  269. data/ext/carray_kernels_scan.c +0 -4302
  270. data/ext/carray_kernels_search.c +0 -37776
  271. data/ext/carray_kernels_sort.c +0 -10814
  272. data/ext/carray_kernels_triop.c +0 -1864
  273. data/lib/carray/construct.rb +0 -360
  274. data/lib/carray/data_type_extension.rb +0 -262
  275. 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
@@ -450,6 +454,11 @@ class CArray
450
454
  face_among_operands?(self, other)
451
455
  return computed_operand(self) ** computed_operand(other)
452
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
453
462
  if other.is_a?(Integer) && (float? || complex?) &&
454
463
  (lazy_view? || (other.is_a?(CArray) && other.lazy_view?))
455
464
  CABinOp.__build__(self, CScalar.int64.tap { |s| s[0] = other },
@@ -465,10 +474,18 @@ class CArray
465
474
  # `pow` is the canonical C method (defined by rb_define_method in
466
475
  # ext/ca_op_ipower.c) that `**` is rb_define_alias'd to. The `**`
467
476
  # dispatch in this file does not reach `pow` through the C alias, so
468
- # `a.lazy.pow(2)` would fall to eager; `pow` is aliased again here.
469
- # An alias copies the method as it is at that moment, so this follows
470
- # the integer-exponent `**` above, not the template one.
471
- 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
472
489
 
473
490
  # coerce: when self is a lazy view and the scalar appears on the LEFT
474
491
  # (e.g. `2 * a.lazy`), Ruby's Numeric#* calls a.lazy.coerce(2). The
@@ -548,7 +565,7 @@ class CArray
548
565
  if face_among_operands?(self, other)
549
566
  return computed_operand(self).__send__(__method__, computed_operand(other), tol)
550
567
  end
551
- CABinCmp.__build__(self, other, __VALUE__, tol.to_f.abs)
568
+ CABinCmp.__build__(self, other, __VALUE__, tol)
552
569
  else
553
570
  super(other, tol)
554
571
  end
@@ -591,26 +608,46 @@ class CArray
591
608
 
592
609
  # An Integer the array's type cannot hold, on the left of an operator.
593
610
  # A comparison is answered from the array's side, where it compares by
594
- # 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.
595
615
  class OutOfRangeInteger
596
- def initialize (value, array, error)
616
+ def initialize(value, array, error)
597
617
  @value, @array, @error = value, array, error
598
618
  end
599
619
 
600
- def < (other) = flip(other, :>)
601
- def <= (other) = flip(other, :>=)
602
- def > (other) = flip(other, :<)
603
- 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, :^)
630
+
631
+ def -(other)
632
+ return -flip(other, :-)
633
+ end
604
634
 
605
- def method_missing (*)
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(*)
606
643
  raise @error
607
644
  end
608
645
 
609
- def respond_to_missing? (*) = false
646
+ def respond_to_missing?(*) = false
610
647
 
611
648
  private
612
649
 
613
- def flip (other, op)
650
+ def flip(other, op)
614
651
  raise @error unless other.equal?(@array)
615
652
  @array.send(op, @value)
616
653
  end
@@ -643,36 +680,8 @@ class CAMonOp
643
680
  end
644
681
  private :op_label
645
682
 
646
- # @overload inspect
647
- # Returns a short summary of the lazy chain without
648
- # materialising.
649
- # @return [String]
650
- def inspect
651
- "#<CAMonOp #{op_label}(#{parent.inspect})>"
652
- end
653
-
654
- # @overload to_s
655
- # Alias of {#inspect}.
656
- # @return [String]
657
- def to_s
658
- inspect
659
- end
660
-
661
- # @overload dump_tree(indent = 0)
662
- # Returns an ASCII summary of the lazy expression tree.
663
- # Does not materialise.
664
- # @param indent [Integer] leading indentation depth.
665
- # @return [String]
666
- def dump_tree(indent = 0)
667
- pad = " " * indent
668
- out = +"#{pad}#{op_label}\n"
669
- out << if parent.is_a?(CAMonOp)
670
- parent.dump_tree(indent + 1)
671
- else
672
- "#{" " * (indent + 1)}#{parent.class}(#{parent.data_type_name}, #{parent.shape.inspect})\n"
673
- end
674
- out
675
- end
683
+ def tree_operands = [parent]
684
+ private :tree_operands
676
685
 
677
686
  # @overload each { |value| ... }
678
687
  # Materialises `self` and yields each element of the resulting
@@ -733,35 +742,16 @@ class CABinOp
733
742
  end
734
743
  private :op_label
735
744
 
736
- # @overload inspect
737
- # Returns a summary of the binop chain without materialising.
738
- # @return [String]
739
- def inspect
740
- "#<CABinOp #{op_label}(#{parent.inspect}, #{__binop_right__.inspect})>"
741
- end
742
-
743
- # @overload to_s
744
- # Alias of {#inspect}.
745
- # @return [String]
746
- def to_s
747
- inspect
748
- end
745
+ def tree_operands = [parent, __binop_right__]
746
+ private :tree_operands
749
747
 
750
- # @overload dump_tree(indent = 0)
751
- # Returns an ASCII summary of the lazy expression tree, walking
752
- # both children. Does not materialise.
753
- # @return [String]
754
- def dump_tree(indent = 0)
755
- pad = " " * indent
756
- out = +"#{pad}#{op_label}\n"
757
- [parent, __binop_right__].each do |child|
758
- out << if child.is_a?(CAMonOp) || child.is_a?(CABinOp)
759
- child.dump_tree(indent + 1)
760
- else
761
- "#{" " * (indent + 1)}#{child.class}(#{child.data_type_name}, #{child.shape.inspect})\n"
762
- end
763
- end
764
- 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__]
765
755
  end
766
756
 
767
757
  # @overload each { |value| ... }
@@ -789,19 +779,130 @@ class CABinOp
789
779
  end
790
780
  end
791
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
+
792
899
  class CALazyMarker
793
900
  # @overload inspect
794
- # Returns a short summary showing the wrapped entity.
901
+ # Returns a short summary naming the wrapped array by class, data type
902
+ # and shape.
795
903
  # @return [String]
796
904
  def inspect
797
- "#<CALazyMarker over #{parent.inspect}>"
798
- end
799
-
800
- # @overload to_s
801
- # Alias of {#inspect}.
802
- # @return [String]
803
- def to_s
804
- inspect
905
+ "#<CALazyMarker over #{parent.class}(#{parent.data_type_name}, #{parent.shape.inspect})>"
805
906
  end
806
907
 
807
908
  # @overload dump_tree(indent = 0)
@@ -855,6 +956,7 @@ end
855
956
  [CAMonOp, CABinOp, CATriOp, CAMonCmp, CABinCmp, CALazyMarker].each do |klass|
856
957
  klass.class_eval do
857
958
  def to_ca(writable: false)
959
+ writable = CArray.send(:bool_arg, writable, "writable", "to_ca")
858
960
  if writable
859
961
  raise "#{self.class}#to_ca evaluates into a new entity; " \
860
962
  "it can't satisfy `writable: true'"
@@ -921,9 +1023,9 @@ class << CArray
921
1023
  Ractor.current == Ractor.main ? @expression_evaluator : nil
922
1024
  end
923
1025
 
924
- # @param object [#call, nil] see {#expression_evaluator}
1026
+ # @param object [#call, nil] see {CArray.expression_evaluator}
925
1027
  # @raise [ArgumentError] when it cannot be called.
926
- def expression_evaluator= (object)
1028
+ def expression_evaluator=(object)
927
1029
  if object && ! object.respond_to?(:call)
928
1030
  raise ArgumentError,
929
1031
  "an expression evaluator must respond to #call(plan, out)"
@@ -953,7 +1055,7 @@ class << CArray
953
1055
  # @raise [ArgumentError] when the block's source cannot be read -- in
954
1056
  # irb, in `eval`, or from a file that is gone. Write `.lazy` on the
955
1057
  # operands there: `a.lazy + b.lazy`.
956
- def fuse (*args, &block)
1058
+ def fuse(*args, &block)
957
1059
  raise LocalJumpError, "CArray.fuse requires a block" unless block
958
1060
  unless args.empty?
959
1061
  raise ArgumentError,
@@ -961,6 +1063,6 @@ class << CArray
961
1063
  "`CArray.fuse { a + b }`, and the names in it are read from " \
962
1064
  "where the block was written"
963
1065
  end
964
- CArray::FuseSource.evaluate(block)
1066
+ CArray.const_get(:FuseSource).evaluate(block)
965
1067
  end
966
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)