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
@@ -1,5 +1,14 @@
1
1
  class CArray
2
2
 
3
+ # One index argument per axis: the gathered index on the axis being
4
+ # repeated, whole axes everywhere else.
5
+ private def gather_args(index, along)
6
+ return [index] unless along
7
+ args = [nil] * ndim
8
+ args[along] = index
9
+ args
10
+ end
11
+
3
12
  # @overload repeat(count, axis: nil)
4
13
  # @overload repeat(counts, axis: nil)
5
14
  # Returns each element (or each sub-array) of `self` laid down
@@ -10,7 +19,7 @@ class CArray
10
19
  # sub-array when `axis:` is given -- each is repeated its own number
11
20
  # of times, and a count of 0 drops that one:
12
21
  #
13
- # v = CA_INT([10, 20, 30])
22
+ # v = CA_INT32([10, 20, 30])
14
23
  # v.repeat(2) # => [ 10, 10, 20, 20, 30, 30 ]
15
24
  # v.repeat([3, 1, 2]) # => [ 10, 10, 10, 20, 30, 30 ]
16
25
  # v.repeat([2, 0, 1]) # => [ 10, 10, 30 ]
@@ -23,7 +32,7 @@ class CArray
23
32
  # than the cells, so a 2-D array repeats whole rows and the result
24
33
  # keeps its shape apart from that axis:
25
34
  #
26
- # t = CA_INT([[0, 1], [2, 3], [4, 5]])
35
+ # t = CA_INT32([[0, 1], [2, 3], [4, 5]])
27
36
  # t.repeat([2, 1, 1], axis: 0)
28
37
  # # => [ [ 0, 1 ],
29
38
  # # [ 0, 1 ],
@@ -47,7 +56,7 @@ class CArray
47
56
  # @param axis [Integer, nil] axis whose index enumerates the
48
57
  # sub-arrays to repeat; `nil` repeats cells in flatten order.
49
58
  # @return [CArray] the repeated elements, as a view of `self`.
50
- def repeat (count, axis: nil)
59
+ def repeat(count, axis: nil)
51
60
  along = axis.nil? ? nil : normalize_axis(axis, "repeat")
52
61
  width = along ? shape[along] : elements
53
62
 
@@ -89,13 +98,4 @@ class CArray
89
98
  source[*gather_args(CArray.segment_index(lengths: counts), along)]
90
99
  end
91
100
 
92
- # One index argument per axis: the gathered index on the axis being
93
- # repeated, whole axes everywhere else.
94
- private def gather_args (index, along)
95
- return [index] unless along
96
- args = [nil] * ndim
97
- args[along] = index
98
- args
99
- end
100
-
101
101
  end
@@ -24,7 +24,7 @@ class CArray
24
24
  NO_FILL_VALUE = ::Object.new.freeze
25
25
  private_constant :NO_FILL_VALUE
26
26
 
27
- def resize (*newdim, fill_value: NO_FILL_VALUE)
27
+ def resize(*newdim, fill_value: NO_FILL_VALUE)
28
28
  raise ArgumentError, "ndim mismatch" if newdim.size != ndim
29
29
  offset = Array.new(ndim, 0)
30
30
  newdim = newdim.each_with_index.map do |d, i|
@@ -1,5 +1,21 @@
1
1
  class CArray
2
2
 
3
+ # A Ruby Array grid in the type a search query would get: a Float in it
4
+ # makes an integer array's grid float64 (so 0.5 is not truncated to 0),
5
+ # otherwise the grid takes self's type, and an Integer that does not fit
6
+ # it is refused rather than wrapped.
7
+ private def snap_list(list)
8
+ vals = list.to_a
9
+ type = data_type
10
+ type = CA_FLOAT64 if (integer? || data_type == CA_BOOLEAN) && vals.any? { |v| v.is_a?(Float) }
11
+ ref = CArray.wrap_readonly(vals, type)
12
+ if ref.integer?
13
+ bad = vals.zip(ref.to_a).find { |v, w| v != w }
14
+ raise RangeError, "snap_to: #{bad[0].inspect} is out of range for #{ref.data_type_name}" if bad
15
+ end
16
+ ref
17
+ end
18
+
3
19
  # @overload snap(step, offset: 0.0, direction: :round)
4
20
  # Returns each element snapped to a point on the uniform grid
5
21
  # `..., -step + offset, offset, step + offset, 2*step + offset, ...`.
@@ -96,8 +112,8 @@ class CArray
96
112
  # to `locate_nearest_addr`.
97
113
  # @return [CArray] snapped values with `list`'s data_type, same
98
114
  # shape as `self`.
99
- # @raise [ArgumentError] when `list` is not 1-D or is empty, or
100
- # `direction` is not one of the accepted symbols.
115
+ # @raise [ArgumentError] when `list` is not 1-D, is empty or
116
+ # decreases, or `direction` is not one of the accepted symbols.
101
117
  # @raise [RangeError] when an Integer in an Array `list` does not
102
118
  # fit `self`'s data_type.
103
119
  # @example
@@ -109,6 +125,12 @@ class CArray
109
125
  raise ArgumentError, "snap_to: list must be 1-D" unless ref.ndim == 1
110
126
  n = ref.elements
111
127
  raise ArgumentError, "snap_to: list must have at least one value" if n < 1
128
+ # The ends of the list bound the clamp, so the list is read in order.
129
+ if n > 1 && (k = ref[1..-1].lt(ref[0..-2]).where.to_a.first)
130
+ raise ArgumentError,
131
+ "snap_to: list must not decrease (#{ref[k + 1].inspect} at #{k + 1} " \
132
+ "after #{ref[k].inspect})"
133
+ end
112
134
 
113
135
  if n == 1
114
136
  # Degenerate: every finite cell snaps to the only value.
@@ -162,22 +184,4 @@ class CArray
162
184
  out
163
185
  end
164
186
 
165
- private
166
-
167
- # A Ruby Array grid in the type a search query would get: a Float in it
168
- # makes an integer array's grid float64 (so 0.5 is not truncated to 0),
169
- # otherwise the grid takes self's type, and an Integer that does not fit
170
- # it is refused rather than wrapped.
171
- def snap_list(list)
172
- vals = list.to_a
173
- type = data_type
174
- type = CA_FLOAT64 if (integer? || data_type == CA_BOOLEAN) && vals.any? { |v| v.is_a?(Float) }
175
- ref = CArray.wrap_readonly(vals, type)
176
- if ref.integer?
177
- bad = vals.zip(ref.to_a).find { |v, w| v != w }
178
- raise RangeError, "snap_to: #{bad[0].inspect} is out of range for #{ref.data_type_name}" if bad
179
- end
180
- ref
181
- end
182
-
183
187
  end
@@ -15,7 +15,7 @@ class CArray
15
15
  # @param argv [Array<CArray, Object>] per-cell CArrays and/or broadcast scalars.
16
16
  # @return [CAString]
17
17
  # @raise [ArgumentError] when no CArray argument is given, or CArray shapes differ.
18
- def self.format (fmt, *argv)
18
+ def self.format(fmt, *argv)
19
19
  cas = argv.select { |a| a.is_a?(CArray) }
20
20
  raise ArgumentError, "CArray.format: at least one CArray argument is required" if cas.empty?
21
21
  shape = cas.first.shape
@@ -50,7 +50,7 @@ class CArray
50
50
  # @param argv [Array<CArray, Object>] additional per-cell CArrays / broadcast scalars.
51
51
  # @return [CAString]
52
52
  # @raise [ArgumentError] when a CArray argument's shape differs from self.
53
- def format (fmt, *argv)
53
+ def format(fmt, *argv)
54
54
  CArray.format(fmt, self, *argv)
55
55
  end
56
56
 
@@ -31,7 +31,8 @@ class CArray
31
31
  # and every NaN as one NaN.
32
32
  #
33
33
  # @param sort [Boolean] when true, return the distinct values
34
- # sorted ascending instead of in first-appearance order. A complex
34
+ # sorted ascending instead of in first-appearance order (`nil` reads
35
+ # as false; any other value raises ArgumentError). A complex
35
36
  # array has no order and raises {CArray::DataTypeError}.
36
37
  # A time array (`CATime` / `CATimedelta`) answers with its own type on
37
38
  # its own unit: the distinct values are values, so the array comes back
@@ -66,7 +67,8 @@ class CArray
66
67
  # @param along [Integer] axis whose index enumerates the sub-arrays.
67
68
  # @return [CArray] the distinct sub-arrays, same shape as `self`
68
69
  # except along `along`.
69
- def unique (sort: false, along: nil)
70
+ def unique(sort: false, along: nil)
71
+ sort = sort_flag(sort, "unique")
70
72
  if along
71
73
  if sort
72
74
  raise ArgumentError,
@@ -34,7 +34,7 @@ class CArray
34
34
  # its own unit); the counts are always a plain `:int64` CArray.
35
35
  #
36
36
  # @return [Array(CArray, CArray)] `[values, counts]`.
37
- def value_counts (sort: false)
37
+ def value_counts(sort: false)
38
38
  unless [false, :count, :value].include?(sort)
39
39
  raise ArgumentError, "value_counts: sort must be false, :count, or :value"
40
40
  end
data/lib/carray/mkmf.rb CHANGED
@@ -5,18 +5,19 @@ require 'mkmf'
5
5
  # top of a companion gem's `extconf.rb`.
6
6
  #
7
7
  # @return [Boolean] whether every required piece was found.
8
- def have_carray
8
+ def have_carray
9
9
  begin
10
10
  require 'carray'
11
- rescue LoadError
12
- abort "Ruby/CArray is not installed"
13
- end
14
- $LOAD_PATH.each do |path|
15
- if File.exist? File.join(path, 'carray.h')
16
- dir_config("carray", path, path)
17
- break
18
- end
11
+ rescue LoadError => e
12
+ abort "Ruby/CArray could not be loaded: #{e.message}"
19
13
  end
14
+ # The header next to the carray_ext that was loaded, so that the two are
15
+ # of one build when several carrays are on the load path; otherwise the
16
+ # first carray.h on it.
17
+ loaded = $LOADED_FEATURES.find { |f| File.basename(f, ".*") == "carray_ext" }
18
+ dirs = [loaded && File.dirname(loaded), *$LOAD_PATH].compact
19
+ dir = dirs.find { |path| File.exist?(File.join(path, 'carray.h')) }
20
+ dir_config("carray", dir, dir) if dir
20
21
  status = true
21
22
  status &= have_header("carray.h")
22
23
  if /cygwin|mingw/ =~ RUBY_PLATFORM
@@ -26,23 +27,22 @@ def have_carray
26
27
  end
27
28
 
28
29
  # @!visibility private
29
- def possible_prefix (*postfixes)
30
+ def possible_prefix(*postfixes)
31
+ # In the home directory only ~/.local is probed, on the ground of the XDG
32
+ # Base Directory layout, which puts a user's own files under ~/.local
33
+ # (~/.local/bin, ~/.local/share). XDG says nothing of lib or include;
34
+ # they are there when a library is installed with
35
+ # --prefix=$HOME/.local, as pip --user does. A library installed
36
+ # elsewhere in the home directory is given with --with-<name>-dir.
30
37
  dirs = [
31
38
  ENV["CONDA_PREFIX"], ### active conda / mamba env
32
39
  ENV["HOMEBREW_PREFIX"], ### Homebrew (non-standard install path)
33
- File.expand_path("~/usr"), ### user's home / usr
34
- File.expand_path("~/local"), ### user's home / local
35
- File.expand_path("~/.local"), ### user's home / .local (XDG, pip --user)
36
- File.expand_path("~"), ### user's home
40
+ File.expand_path("~/.local"), ### XDG's ~/.local, the user's own installs
37
41
  "/opt/local", ### MacPorts
38
42
  "/opt/homebrew", ### Homebrew (Apple Silicon)
39
43
  "/opt", ### UNIX
40
- "/sw/local", ### Mac Fink
41
- "/sw/", ### Mac Fink
42
- "/usr/X11R6", ### UNIX X11
43
44
  "/usr/local", ### UNIX
44
45
  "/usr", ### UNIX
45
- "/" ### UNIX
46
46
  ].compact
47
47
  if postfixes
48
48
  dirs = postfixes.inject(dirs) { |list, postfix|
@@ -53,10 +53,7 @@ def possible_prefix (*postfixes)
53
53
  end
54
54
 
55
55
  # @!visibility private
56
- POSSIBLE_PREFIX = possible_prefix()
57
-
58
- # @!visibility private
59
- def possible_libs (*postfixes)
56
+ def possible_libs(*postfixes)
60
57
  dirs = possible_prefix().map{|prefix| File.join(prefix, "lib") }
61
58
  if postfixes
62
59
  dirs = postfixes.inject(dirs) { |list, postfix|
@@ -67,7 +64,7 @@ def possible_libs (*postfixes)
67
64
  end
68
65
 
69
66
  # @!visibility private
70
- def possible_includes (*postfixes)
67
+ def possible_includes(*postfixes)
71
68
  dirs = possible_prefix().map{|prefix| File.join(prefix, "include") }
72
69
  if postfixes
73
70
  dirs = postfixes.inject(dirs) { |list, postfix|
@@ -89,7 +86,7 @@ end
89
86
  # carray_dir_config("nn") # no .pc -> possible_* probe
90
87
  # carray_dir_config("foo", "libfoo") # search foo.pc / libfoo.pc as "libfoo"
91
88
  #
92
- def carray_dir_config (name, pkg = name)
89
+ def carray_dir_config(name, pkg = name)
93
90
  return true if pkg_config(pkg)
94
91
  dir_config(name, possible_includes, possible_libs)
95
92
  true
@@ -98,7 +95,7 @@ end
98
95
  # --- pluggable Fortran backend registry
99
96
  #
100
97
  # CArray no longer tries to enumerate every Fortran compiler. Only
101
- # gfortran ships out of the box. A companion gem (or a user's extconf)
98
+ # gfortran is supported by default. A companion gem (or a user's extconf)
102
99
  # that needs another compiler registers a backend *before* calling
103
100
  # check_fortran:
104
101
  #
@@ -116,7 +113,7 @@ end
116
113
  FORTRAN_BACKENDS = {}
117
114
 
118
115
  # Registers a Fortran runtime backend for {check_fortran} to pick up. Only
119
- # gfortran ships out of the box; a companion gem that needs another compiler
116
+ # gfortran is supported by default; a companion gem that needs another compiler
120
117
  # registers its own before calling {check_fortran}. The block receives the
121
118
  # compiler command and is responsible only for linking that compiler's
122
119
  # runtime library (`dir_config` + `have_library`).
@@ -124,11 +121,11 @@ FORTRAN_BACKENDS = {}
124
121
  # @param pattern [Regexp] matched against the configured compiler command.
125
122
  # @yieldparam fc [String] the compiler command.
126
123
  # @return [void]
127
- def register_fortran_backend (pattern, &block)
124
+ def register_fortran_backend(pattern, &block)
128
125
  FORTRAN_BACKENDS[pattern] = block
129
126
  end
130
127
 
131
- # gfortran: the one compiler CArray supports out of the box.
128
+ # gfortran: the one compiler CArray supports by default.
132
129
  register_fortran_backend(/gfortran/) do |fc|
133
130
  libdir = File.dirname(`#{fc} --print-file-name=libgfortran.a`.strip)
134
131
  dir_config("gfortran", possible_includes, [libdir] + possible_libs)
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+
3
+ # How `percentile` reads its p arguments and how it returns the answers.
4
+ # The form the caller writes p in decides the form of the answer:
5
+ #
6
+ # percentile(50) one answer (a value, or a reduced CArray);
7
+ # a CScalar p is one p too
8
+ # percentile([50, 90]) a Ruby Array with one answer per p
9
+ # percentile(ca) one CArray of shape ca.shape + answer shape
10
+ # percentile(50, 90) one CArray of shape [2] + answer shape
11
+ #
12
+ # CArray#percentile and the iterators that gather their own answers
13
+ # (CABlockIterator, CASlabIterator) read and answer through these two
14
+ # private class methods, so the rule is written once.
15
+ class CArray
16
+
17
+ class << self
18
+
19
+ private
20
+
21
+ # Returns `[pers, form]`: the p values as a flat Ruby Array, and the
22
+ # form the caller used (`:one`, `:array`, or the leading shape of the
23
+ # answer).
24
+ def percentile_arguments(args, name = "percentile")
25
+ if args.empty?
26
+ raise ArgumentError, "#{name}: at least one p value required"
27
+ end
28
+ # A CScalar is one p, as an operator takes it as a scalar.
29
+ args = args.map do |p|
30
+ next p unless p.is_a?(CScalar)
31
+ if p.any_masked?
32
+ raise ArgumentError, "#{name}: p must not have masked elements"
33
+ end
34
+ p[0]
35
+ end
36
+ first = args[0]
37
+ if args.size == 1 && first.is_a?(Array)
38
+ pers, form = first.dup, :array
39
+ elsif args.size == 1 && first.is_a?(CArray)
40
+ if first.any_masked?
41
+ raise ArgumentError, "#{name}: p must not have masked elements"
42
+ end
43
+ pers, form = first.to_a.flatten, first.shape
44
+ else
45
+ if args.any? { |p| p.is_a?(Array) || p.is_a?(CArray) }
46
+ raise ArgumentError,
47
+ "#{name}: give several p values either as separate Numerics " \
48
+ "or as one Array or CArray, not both"
49
+ end
50
+ pers = args.dup
51
+ form = args.size == 1 ? :one : [args.size]
52
+ end
53
+ if pers.empty?
54
+ raise ArgumentError, "#{name}: at least one p value required"
55
+ end
56
+ [pers, form]
57
+ end
58
+
59
+ # Returns the answers (one per p, in order) in the given form. Answers
60
+ # that are single values are put into a CArray of `data_type`; when it
61
+ # is nil, float64 if every answer is a Float or UNDEF, object otherwise.
62
+ def percentile_answer(answers, form, data_type = nil)
63
+ case form
64
+ when :one then answers[0]
65
+ when :array then answers
66
+ else
67
+ if answers[0].is_a?(CArray)
68
+ shape = form + answers[0].shape
69
+ CArray.stack(answers).reshape(*shape).copy
70
+ else
71
+ data_type ||= answers.all? { |v| v.is_a?(Float) || v.equal?(UNDEF) } ?
72
+ :float64 : :object
73
+ out = CArray.new(data_type, form)
74
+ answers.each_with_index { |v, i| out[i] = v }
75
+ out
76
+ end
77
+ end
78
+ end
79
+
80
+ end
81
+
82
+ end
@@ -22,14 +22,14 @@ class CARecord
22
22
  # `order_by:` members. With no `axis:` the array is flattened first.
23
23
  # @return [CARecord]
24
24
  # @raise [ArgumentError] when the struct declares no `order_by:`.
25
- def sort (*args, **kw)
25
+ def sort(*args, **kw)
26
26
  addr = record_rank.sort_addr(*args, **kw)
27
27
  kw[:axis].nil? ? self[addr.flatten] : self[addr]
28
28
  end
29
29
 
30
30
  # @overload sort_copy(axis: nil, kind: :quick, masked_position: :last)
31
31
  # @return [CARecord] an owned sorted copy.
32
- def sort_copy (*args, **kw)
32
+ def sort_copy(*args, **kw)
33
33
  sort(*args, **kw).copy
34
34
  end
35
35
 
@@ -37,9 +37,7 @@ class CARecord
37
37
  # @return [CArray] the view-flat addresses of a sort by the declared order.
38
38
  # @!method sort_index(axis: nil, kind: :quick, masked_position: :last)
39
39
  # @return [CArray] the per-fiber indices of a sort by the declared order.
40
- # @!method rank_index(axis: nil)
41
- # @return [CArray] each record's rank by the declared order.
42
- # @!method order(axis: nil, descending: false, method: :ordinal, kind: :quick)
40
+ # @!method rank_index(axis: nil, descending: false, method: :ordinal, kind: :quick)
43
41
  # @return [CArray] each record's rank by the declared order.
44
42
  # @!method min_index(axis: nil)
45
43
  # @return [Integer, CArray] where the smallest record sits.
@@ -47,7 +45,7 @@ class CARecord
47
45
  # @return [Integer, CArray] where the largest record sits.
48
46
  # @!method partition_index(kth, axis: nil)
49
47
  # @return [CArray] the indices that partition about the `kth` record.
50
- [:sort_addr, :sort_index, :rank_index, :order,
48
+ [:sort_addr, :sort_index, :rank_index,
51
49
  :min_index, :max_index, :partition_index].each do |op|
52
50
  define_method(op, &Ractor.make_shareable(proc { |*args, **kw| record_rank.public_send(op, *args, **kw) }))
53
51
  end
@@ -57,27 +55,30 @@ class CARecord
57
55
  # records; UNDEF when every record is masked. With `axis:` a
58
56
  # {CARecord} of the smallest along that axis.
59
57
  # @return [CAStruct, CARecord]
60
- def min (axis: nil, keep_axis: false, min_count: nil, fill_value: nil)
58
+ def min(axis: nil, keep_axis: false, min_count: nil, fill_value: nil)
59
+ keep_axis = CArray.send(:bool_arg, keep_axis, "keep_axis", "min")
61
60
  record_extreme_kept(:min_index, axis, keep_axis, min_count, fill_value)
62
61
  end
63
62
 
64
63
  # @overload max(axis: nil, keep_axis: false, min_count: nil, fill_value: nil)
65
64
  # The largest record; see {#min}.
66
65
  # @return [CAStruct, CARecord]
67
- def max (axis: nil, keep_axis: false, min_count: nil, fill_value: nil)
66
+ def max(axis: nil, keep_axis: false, min_count: nil, fill_value: nil)
67
+ keep_axis = CArray.send(:bool_arg, keep_axis, "keep_axis", "max")
68
68
  record_extreme_kept(:max_index, axis, keep_axis, min_count, fill_value)
69
69
  end
70
70
 
71
71
  # @overload minmax(axis: nil, keep_axis: false)
72
72
  # @return [Array] `[min, max]`.
73
- def minmax (axis: nil, keep_axis: false)
73
+ def minmax(axis: nil, keep_axis: false)
74
+ keep_axis = CArray.send(:bool_arg, keep_axis, "keep_axis", "minmax")
74
75
  [min(axis: axis, keep_axis: keep_axis), max(axis: axis, keep_axis: keep_axis)]
75
76
  end
76
77
 
77
78
  # @overload partition_copy(kth, axis: nil)
78
79
  # @return [CARecord] partitioned about the `kth` record in the declared
79
80
  # order. With no `axis:` the array is flattened first.
80
- def partition_copy (kth, axis: nil)
81
+ def partition_copy(kth, axis: nil)
81
82
  if axis.nil?
82
83
  flat = flatten
83
84
  return flat[flat.send(:record_rank).partition_index(kth)].copy
@@ -122,23 +123,23 @@ class CARecord
122
123
  record_rank_of(self).reshape(*shape)
123
124
  end
124
125
 
125
- def record_rank_of (records)
126
+ def record_rank_of(records)
126
127
  rank = nil
127
128
  record_order_by.each do |key|
128
- r = records[key].flatten.order(method: :dense)
129
+ r = records[key].flatten.rank_index(method: :dense)
129
130
  if rank.nil?
130
131
  rank = r
131
132
  else
132
133
  top = r.max
133
134
  span = top.equal?(UNDEF) ? 1 : top + 1
134
- rank = (rank * span + r).order(method: :dense)
135
+ rank = (rank * span + r).rank_index(method: :dense)
135
136
  end
136
137
  end
137
138
  rank
138
139
  end
139
140
 
140
141
  # Ranks of self and `other` in one ordering, so they can be compared.
141
- def record_rank_with (other)
142
+ def record_rank_with(other)
142
143
  case other
143
144
  when CARecord
144
145
  unless other.data_class.equal?(data_class)
@@ -164,7 +165,7 @@ class CARecord
164
165
  # The record at each min_index / max_index, gathered from self.
165
166
  # An extreme under the reduction keywords every extreme takes: UNDEF where
166
167
  # fewer than `min_count` cells are present, then `fill_value` for UNDEF.
167
- def record_extreme_kept (index_op, axis, keep_axis, min_count, fill_value)
168
+ def record_extreme_kept(index_op, axis, keep_axis, min_count, fill_value)
168
169
  out = record_extreme(index_op, axis, keep_axis)
169
170
  name = index_op.to_s.sub("_index", "").to_sym
170
171
  need = CArray.__present_cells_needed_per_result_cell__(name, min_count, 0)
@@ -184,7 +185,7 @@ class CARecord
184
185
  CArray.__fill_undefined_result_cells__(out, fill_value)
185
186
  end
186
187
 
187
- def record_extreme (index_op, axis, keep_axis)
188
+ def record_extreme(index_op, axis, keep_axis)
188
189
  rank = record_rank
189
190
  if axis.nil?
190
191
  i = rank.flatten.public_send(index_op)
@@ -217,7 +218,7 @@ class CARecord
217
218
  end
218
219
 
219
220
  # Over several axes: the reduced axes are moved last and merged into one.
220
- def record_extreme_over (index_op, axes, keep_axis)
221
+ def record_extreme_over(index_op, axes, keep_axis)
221
222
  keep = (0...ndim).to_a - axes
222
223
  kept_shape = shape.values_at(*keep)
223
224
  if keep.empty?
data/lib/carray/rng.rb CHANGED
@@ -20,6 +20,7 @@ class CArray
20
20
  :xoshiro256pp => 4,
21
21
  }.freeze
22
22
 
23
+ # The names of the generators {#initialize} takes.
23
24
  GENERATORS = STATE_CELLS.keys.freeze
24
25
 
25
26
  # Where a generator's C lives. Shipped: the gemspec takes ext/*.h,