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
@@ -0,0 +1,269 @@
1
+ # NumPy / Numo-style factory class methods on CArray, kept as a
2
+ # soft compatibility layer.
3
+ #
4
+ # carray's own idiom for building arrays is the typed constructor
5
+ # (CArray.float64(3, 3) { 0 }, CArray.new(:int32, [3]), CA_INT32([1, 2,
6
+ # 3]) and friends). The methods below -- zeros / ones / eye / identity
7
+ # / linspace / arange / full / empty -- do the same work under
8
+ # NumPy-flavoured names, so that code ported from NumPy or Numo does not
9
+ # stop at the first line that creates an array:
10
+ #
11
+ # np.zeros((3, 3)) -> CArray.zeros([3, 3])
12
+ # np.zeros((3, 3), dtype=np.int32) -> CArray.zeros(3, 3, data_type: :int32)
13
+ # np.full((3, 3), 7) -> CArray.full(3, 3, fill_value: 7)
14
+ # np.linspace(0.0, 1.0, 5) -> CArray.linspace(0.0, 1.0, 5)
15
+ #
16
+ # The shape-taking methods accept both the splat form (zeros(3, 3)) and
17
+ # a single Array (zeros([3, 3])). The element type is given with the
18
+ # `data_type:` keyword, as everywhere else in carray.
19
+ #
20
+ # meshgrid lives in lib/carray/methods/meshgrid.rb (autoloaded).
21
+
22
+ class CArray
23
+
24
+ class << self
25
+
26
+ # @overload zeros(*shape, data_type: :float64)
27
+ # Returns a new CArray of the given shape filled with zeros.
28
+ # Shape accepts both splat Integers and a single Array.
29
+ # @param shape [Array<Integer>, Array<Array<Integer>>] shape
30
+ # of the result.
31
+ # @param data_type [Symbol, String, Integer] element type.
32
+ # @return [CArray]
33
+ def zeros(*args, data_type: CA_FLOAT64)
34
+ CArray.new(data_type, factory_shape(args)).zero
35
+ end
36
+
37
+ # @overload ones(*shape, data_type: :float64)
38
+ # Returns a new CArray of the given shape filled with ones.
39
+ # Same shape rules as {zeros}.
40
+ # @param shape [Array<Integer>, Array<Array<Integer>>]
41
+ # @param data_type [Symbol, String, Integer] element type.
42
+ # @return [CArray]
43
+ def ones(*args, data_type: CA_FLOAT64)
44
+ CArray.new(data_type, factory_shape(args)).one
45
+ end
46
+
47
+ # @overload eye(n, m = n, k = 0, data_type: :float64)
48
+ # Returns a 2-D CArray with ones on the `k`-th diagonal and
49
+ # zeros elsewhere.
50
+ # @param n [Integer] number of rows.
51
+ # @param m [Integer] number of columns; defaults to `n`.
52
+ # @param k [Integer] diagonal offset (positive above the main
53
+ # diagonal, negative below).
54
+ # @param data_type [Symbol, String, Integer] element type.
55
+ # @return [CArray] shape `(n, m)`.
56
+ def eye(n, m = nil, k = 0, data_type: CA_FLOAT64)
57
+ m ||= n
58
+ mat = CArray.new(data_type, [n, m])
59
+ if k >= 0
60
+ count = [n, m - k].min
61
+ start = k
62
+ else
63
+ count = [n + k, m].min
64
+ start = (-k) * m
65
+ end
66
+ if count > 0
67
+ mat[(start..start + (count - 1) * (m + 1)) % (m + 1)] = 1
68
+ end
69
+ mat
70
+ end
71
+
72
+ # @overload identity(n, data_type: :float64)
73
+ # Returns the `n` by `n` identity matrix.
74
+ # @param n [Integer] matrix size.
75
+ # @param data_type [Symbol, String, Integer] element type.
76
+ # @return [CArray] shape `(n, n)`.
77
+ def identity(n, data_type: CA_FLOAT64)
78
+ mat = CArray.new(data_type, [n, n])
79
+ mat[(0..) % (n + 1)] = 1
80
+ mat
81
+ end
82
+
83
+ # @overload linspace(x1, x2, n = 100, data_type: nil)
84
+ # Returns a 1-D CArray of `n` values evenly spaced from `x1`
85
+ # to `x2` inclusive. The values are computed in float64 with the
86
+ # step `(x2 - x1) / (n - 1)`. For an integer `data_type` they are
87
+ # then floored (rounded toward -infinity, not toward zero), so
88
+ # both endpoints are hit exactly when `x1` and `x2` are
89
+ # integer-valued. With `n == 1` the one value is `x1`.
90
+ #
91
+ # Without `data_type:`, the result is float64 (integer, Rational
92
+ # and BigDecimal inputs included). `x1` and `x2` must be real
93
+ # numbers. A Complex is refused; for points along a line in the
94
+ # complex plane, fill a complex array with {#seq}.
95
+ #
96
+ # @param x1 [Numeric] first value.
97
+ # @param x2 [Numeric] last value.
98
+ # @param n [Integer] number of samples.
99
+ # @param data_type [Symbol, String, Integer, nil] element type.
100
+ # @return [CArray]
101
+ # @raise [RangeError] if a value does not fit in `data_type`.
102
+ # @raise [TypeError] if `x1` or `x2` is not a real number (a Complex,
103
+ # nil, a String, a Time, ...).
104
+ def linspace(x1, x2, n = 100, data_type: nil)
105
+ raise TypeError, "linspace: n must be an Integer (got #{n.class})" unless n.is_a?(Integer)
106
+ refuse_complex_bounds("linspace", x1, x2)
107
+ refuse_non_real_bounds("linspace", x1, x2)
108
+ data_type ||= CA_FLOAT64
109
+ # span is float-only ("N evenly-spaced integers" is ambiguous --
110
+ # see basics.rb). Another target goes through a float64 span and
111
+ # is cast; an integer target is floored first.
112
+ float_out = CArray.new(CA_FLOAT64, [n]).span(x1.to_f..x2.to_f)
113
+ return float_out if CArray.data_type_name(data_type) == "float64"
114
+ float_out = float_out.floor if CArray.new(data_type, [0]).integer?
115
+ # to_type masks a cell the type cannot hold; linspace refuses it
116
+ # instead, as arange does.
117
+ # coerce: the type the caller asked for
118
+ out = float_out.to_type(data_type)
119
+ if out.any_masked?
120
+ raise RangeError,
121
+ "linspace: #{x1.inspect}..#{x2.inspect} is out of range for #{out.data_type_name}"
122
+ end
123
+ out
124
+ end
125
+
126
+ # @overload arange(stop, data_type: nil)
127
+ # Returns a 1-D CArray with values `0, 1, ..., stop - 1`.
128
+ # @param stop [Numeric] exclusive upper bound.
129
+ # @param data_type [Symbol, String, Integer, nil] element type;
130
+ # guessed from the arguments when omitted: int64 when every
131
+ # argument is an Integer, float64 when any is a Float, object
132
+ # when any is not an Integer or a Float.
133
+ # @return [CArray]
134
+ # @overload arange(start, stop, data_type: nil)
135
+ # Returns `start, start + 1, ..., stop - 1`.
136
+ # @param start [Numeric] inclusive lower bound.
137
+ # @param stop [Numeric] exclusive upper bound.
138
+ # @param data_type [Symbol, String, Integer, nil] element type.
139
+ # @return [CArray]
140
+ # @overload arange(start, stop, step, data_type: nil)
141
+ # Returns values from `start` to `stop` (exclusive) stepping
142
+ # by `step`.
143
+ # @param start [Numeric] inclusive lower bound.
144
+ # @param stop [Numeric] exclusive upper bound.
145
+ # @param step [Numeric] spacing.
146
+ # @param data_type [Symbol, String, Integer, nil] element type.
147
+ # @return [CArray]
148
+ # @raise [TypeError] if an argument is a Complex: the exclusive stop
149
+ # needs an order, which a complex value has not; fill a complex
150
+ # array with {#seq} instead.
151
+ # @raise [TypeError] if an argument is not a number (a String, nil).
152
+ def arange(*args, data_type: nil)
153
+ case args.size
154
+ when 3
155
+ start, stop, step = *args
156
+ when 2
157
+ start, stop = *args
158
+ step = 1
159
+ when 1
160
+ start = 0
161
+ stop, = *args
162
+ step = 1
163
+ else
164
+ raise ArgumentError, "wrong number of arguments (given #{args.size}, expected 1..3)"
165
+ end
166
+ refuse_complex_bounds("arange", start, stop, step)
167
+ [start, stop, step].each do |v|
168
+ next if v.is_a?(Numeric)
169
+ raise TypeError, "arange: #{v.inspect} (#{v.class}) is not a number"
170
+ end
171
+ raise ArgumentError, "step must not be 0" if step == 0
172
+ data_type ||= guess_data_type_from_values(start, stop, step)
173
+ # The element count comes from the arguments as given, not from
174
+ # the target data type: arange(0, 1, 0.25, data_type: :int32)
175
+ # counts four elements from the float step and truncates them on
176
+ # store. Integer arguments count exactly (divmod) so that a step
177
+ # dividing the span evenly does not gain a spurious element
178
+ # through float rounding.
179
+ span = stop - start
180
+ if span.is_a?(Integer) && step.is_a?(Integer)
181
+ q, r = span.divmod(step)
182
+ n = r.zero? ? q : q + 1
183
+ else
184
+ n = (span.to_f / step).ceil
185
+ end
186
+ n = 0 if n < 0
187
+ CArray.new(data_type, [n]).seq(start, step)
188
+ end
189
+
190
+ # @overload full(*shape, fill_value:, data_type: nil)
191
+ # Returns a new CArray of the given shape filled with
192
+ # `fill_value`. The shape is given as {zeros} takes it
193
+ # (`full(2, 3, fill_value: 7)` or `full([2, 3], fill_value: 7)`).
194
+ # Without `data_type:`, the element type is inferred from
195
+ # `fill_value`.
196
+ # @param shape [Array<Integer>, Array<Array<Integer>>]
197
+ # @param fill_value [Object] value used to fill the array.
198
+ # @param data_type [Symbol, String, Integer, nil] element type.
199
+ # @return [CArray]
200
+ def full(*args, fill_value:, data_type: nil)
201
+ data_type ||= guess_data_type_from_values(fill_value)
202
+ CArray.new(data_type, factory_shape(args)).fill(fill_value)
203
+ end
204
+
205
+ # @overload empty(*shape, data_type: :float64, bytes: nil)
206
+ # Returns a new CArray of the given shape whose contents are
207
+ # **undefined**. It is {zeros} without the fill: the cost of
208
+ # zeroing the buffer is not paid. Use it when every cell is written
209
+ # before it is read. Same shape rules as {zeros}; this is
210
+ # `CArray.new(data_type, shape, bytes: bytes, raw: true)`.
211
+ #
212
+ # `:object` arrays are the exception: their cells are set to 0
213
+ # anyway, because the garbage collector walks them.
214
+ #
215
+ # @param shape [Array<Integer>, Array<Array<Integer>>]
216
+ # @param data_type [Symbol, String] element type.
217
+ # @param bytes [Integer, nil] element width, for `:fixlen` only.
218
+ # @return [CArray]
219
+ # @raise [ArgumentError] if a block is given -- filling an array is
220
+ # what `CArray.new` is for.
221
+ def empty(*args, data_type: CA_FLOAT64, bytes: nil, &block)
222
+ if block
223
+ raise ArgumentError,
224
+ "CArray.empty does not take a block " \
225
+ "(its contents are left undefined); use CArray.new to fill."
226
+ end
227
+ CArray.new(data_type, factory_shape(args), bytes: bytes, raw: true)
228
+ end
229
+
230
+ private
231
+
232
+ # arange stops before stop, which needs an order, and linspace spans a
233
+ # real interval; a complex value has neither.
234
+ def refuse_complex_bounds(name, *values)
235
+ return unless values.any? { |v| v.is_a?(Complex) }
236
+ raise TypeError,
237
+ "#{name}: a Complex bound or step is not accepted; fill a complex " \
238
+ "array with seq (CArray.cmplx128(n).seq(start, step))"
239
+ end
240
+
241
+ # Integer, Float and Complex values together take the widest of
242
+ # int64 < float64 < cmplx128 (arange(0, 5, 0.5) is float64). Any
243
+ # other value (Rational, String, nil, ...) makes the result object.
244
+ def guess_data_type_from_values(*values)
245
+ if values.all? {|v| v == true || v == false }
246
+ CA_BOOLEAN
247
+ elsif !values.all? { |v| v.is_a?(Integer) || v.is_a?(Float) || v.is_a?(Complex) }
248
+ CA_OBJECT
249
+ elsif values.any? { |v| v.is_a?(Complex) }
250
+ CA_CMPLX128
251
+ elsif values.any? { |v| v.is_a?(Float) }
252
+ CA_FLOAT64
253
+ else
254
+ CA_INT64
255
+ end
256
+ end
257
+
258
+ # Accept both foo(3, 4) and foo([3, 4]).
259
+ def factory_shape(args)
260
+ if args.size == 1 && args.first.is_a?(Array)
261
+ args.first
262
+ else
263
+ args
264
+ end
265
+ end
266
+
267
+ end
268
+
269
+ end
@@ -17,7 +17,7 @@ class CArray
17
17
  # CArray.fixlen_string(["ab", "cde"]) # width = max bytesize
18
18
  # CArray.fixlen_string([a, nil, b], bytes: 8) # nil → masked element
19
19
  #
20
- # The bounded slot width is the storage seam CAFixlenString exposes.
20
+ # CAFixlenString exposes the bounded slot width of its storage.
21
21
  # `truncate:` controls what happens when
22
22
  # a value exceeds `bytes` (only reachable when `bytes` is given explicitly;
23
23
  # the auto width can never overflow):
@@ -31,12 +31,14 @@ class CArray
31
31
  # ({CArray.string_face_of}): a raw CA_FIXLEN of matching width wraps
32
32
  # zero-copy, other string-bearing arrays materialise, numeric is rejected.
33
33
  # @overload fixlen_string(values, bytes: nil, truncate: :error)
34
- # @param values [Array<String, nil>] source values.
34
+ # @param values [Array<String, nil>] source values; nil and UNDEF are
35
+ # masked cells.
35
36
  # @param bytes [Integer, nil] slot width, 1 or more; defaults to the
36
37
  # max bytesize (1 when every string is empty).
37
38
  # @param truncate [Symbol] `:error` or `:silent`.
38
39
  # @return [CAFixlenString]
39
40
  # @raise [ArgumentError] when `bytes` is given below 1.
41
+ # @raise [TypeError] when a value is not a String, nil or UNDEF.
40
42
  # @overload fixlen_string(ca, bytes: nil, truncate: :error)
41
43
  # @param ca [CArray] a String Face, CA_OBJECT, or raw CA_FIXLEN array.
42
44
  # @return [CAFixlenString]
@@ -46,7 +48,7 @@ class CArray
46
48
  # @param bytes [Integer, nil] slot width; defaults to the max bytesize.
47
49
  # @param truncate [Symbol] `:error` or `:silent`.
48
50
  # @return [CAFixlenString]
49
- def self.fixlen_string (arg, bytes: nil, truncate: :error, &block)
51
+ def self.fixlen_string(arg, bytes: nil, truncate: :error, &block)
50
52
  if arg.is_a?(CArray)
51
53
  return string_face_of(arg, "CArray.fixlen_string").to_fixlen_string(bytes: bytes, truncate: truncate)
52
54
  end
@@ -64,17 +66,18 @@ class CArray
64
66
  else
65
67
  values = arg.to_a
66
68
  end
69
+ values = CArray.send(:string_cells, values, "CAFixlenString")
67
70
 
68
71
  unless bytes.nil? || (bytes.is_a?(Integer) && bytes >= 1)
69
72
  raise ArgumentError, "CArray.fixlen_string: invalid bytes: #{bytes.inspect} (a cell needs bytes: of 1 or more)"
70
73
  end
71
- width = bytes || values.compact.map { |s| s.to_s.bytesize }.max || 1
74
+ width = bytes || values.compact.map(&:bytesize).max || 1
72
75
  width = 1 if width < 1 # every string empty: the narrowest cell
73
76
 
74
77
  if truncate == :error
75
78
  values.each_with_index do |s, i|
76
79
  next if s.nil?
77
- b = s.to_s.bytesize
80
+ b = s.bytesize
78
81
  if b > width
79
82
  raise ArgumentError,
80
83
  "CArray.fixlen_string: value at #{i} is #{b} bytes, exceeds slot width #{width} " \
@@ -85,7 +88,7 @@ class CArray
85
88
 
86
89
  entity = CArray.new(CA_FIXLEN, [values.size], :bytes => width)
87
90
  values.each_with_index do |s, i|
88
- entity[i] = s.nil? ? UNDEF : s.to_s
91
+ entity[i] = s.nil? ? UNDEF : s
89
92
  end
90
93
  CAFixlenString.wrap(entity, encoding: CAFixlenString.__send__(:encoding_of_strings, values))
91
94
  end
@@ -100,33 +103,31 @@ class CAFixlenString
100
103
  # CAFixlenString of the same shape or one String. The result is as wide as
101
104
  # the wider of the two.
102
105
  # @return [CAFixlenString]
103
- def pmax (other)
106
+ def pmax(other)
104
107
  refuse_unordered_operands(__callee__, other)
105
- pick_by_order(other) { |a, b| a.lt(b) }
108
+ pick_by_order(__callee__, other) { |a, b| a.lt(b) }
106
109
  end
107
110
 
108
111
  # The lesser String of each cell and `other`; see {#pmax}.
109
112
  # @return [CAFixlenString]
110
- def pmin (other)
113
+ def pmin(other)
111
114
  refuse_unordered_operands(__callee__, other)
112
- pick_by_order(other) { |a, b| a.gt(b) }
115
+ pick_by_order(__callee__, other) { |a, b| a.gt(b) }
113
116
  end
114
117
 
115
118
  # A String has no NaN to let win, so these are other names of pmax / pmin.
116
119
  alias_method :maximum, :pmax
117
120
  alias_method :minimum, :pmin
118
- alias_method :fmax, :pmax
119
- alias_method :fmin, :pmin
120
121
 
121
122
  # @overload min_addr(axis: nil)
122
123
  # @return [Integer, CArray] the flat address of the smallest String.
123
- def min_addr (*args, **kw)
124
+ def min_addr(*args, **kw)
124
125
  to_string.min_addr(*args, **kw)
125
126
  end
126
127
 
127
128
  # @overload max_addr(axis: nil)
128
129
  # @return [Integer, CArray] the flat address of the largest String.
129
- def max_addr (*args, **kw)
130
+ def max_addr(*args, **kw)
130
131
  to_string.max_addr(*args, **kw)
131
132
  end
132
133
 
@@ -136,7 +137,7 @@ class CAFixlenString
136
137
  [:eq, :ne, :count, :search, :search_addr, :is_in, :union, :difference, :intersection],
137
138
  :through_strings_unless_text, super_block: true)
138
139
 
139
- private def through_strings_unless_text (op, args, kw)
140
+ private def through_strings_unless_text(op, args, kw)
140
141
  v = args[0]
141
142
  return yield(*args, **kw) if args.empty? || v.is_a?(String) || v.is_a?(CArray) ||
142
143
  v.is_a?(Array) || UNDEF.equal?(v)
@@ -146,8 +147,8 @@ class CAFixlenString
146
147
  # `other` where the block says self's cell loses to it, else self's cell.
147
148
  # Another array of Strings (a CAString, a CAConstString, an object array)
148
149
  # is taken as fixed-length strings, so the result is one.
149
- private def pick_by_order (other)
150
- return copy.tap { |out| out[] = UNDEF } if UNDEF.equal?(other)
150
+ private def pick_by_order(verb, other)
151
+ raise ArgumentError, "#{verb}: comparison of String with UNDEF failed" if UNDEF.equal?(other)
151
152
  if other.is_a?(CArray) && !other.is_a?(CAFixlenString)
152
153
  other = CArray.string(other) if other.data_type == CA_OBJECT && !other.face?
153
154
  other = other.to_fixlen_string if other.respond_to?(:to_fixlen_string)
@@ -156,7 +157,7 @@ class CAFixlenString
156
157
  end
157
158
 
158
159
  # Both branches of a selection at the width of the wider one.
159
- private def branches_in_common (x, y)
160
+ private def branches_in_common(x, y)
160
161
  width = [x, y].map { |v|
161
162
  case v
162
163
  when CArray then v.bytes
@@ -172,7 +173,7 @@ class CAFixlenString
172
173
  # The encoding a column of these strings holds its text in: that of the
173
174
  # strings beyond ASCII, UTF-8 if none is, and BINARY if they disagree,
174
175
  # since the column cannot read them all back.
175
- private_class_method def self.encoding_of_strings (values)
176
+ private_class_method def self.encoding_of_strings(values)
176
177
  text = values.compact.map(&:to_s).reject(&:ascii_only?).map(&:encoding).uniq
177
178
  text.size > 1 ? Encoding::BINARY : (text.first || Encoding::UTF_8)
178
179
  end
@@ -195,28 +196,28 @@ class CAFixlenString
195
196
 
196
197
  # @return [Array(String, String), Array(CAFixlenString, CAFixlenString)]
197
198
  # `[min, max]`, with the keywords of {CArray#min} / {CArray#max}.
198
- def minmax (*args, **kw)
199
+ def minmax(*args, **kw)
199
200
  [min(*args, **kw), max(*args, **kw)]
200
201
  end
201
202
 
202
203
  # @return [CAFixlenString] the running maximum, as {CArray#cummax}.
203
- def cummax (*args, **kw)
204
+ def cummax(*args, **kw)
204
205
  to_string.cummax(*args, **kw).to_fixlen_string(bytes: bytes)
205
206
  end
206
207
 
207
208
  # @return [CAFixlenString] the running minimum, as {CArray#cummin}.
208
- def cummin (*args, **kw)
209
+ def cummin(*args, **kw)
209
210
  to_string.cummin(*args, **kw).to_fixlen_string(bytes: bytes)
210
211
  end
211
212
 
212
213
  # A String has no distance, so there is no nearest one; refused in the
213
214
  # words CAString uses.
214
- def search_nearest (*args, **kw)
215
+ def search_nearest(*args, **kw)
215
216
  to_string.search_nearest(*args, **kw)
216
217
  end
217
218
 
218
219
  # @see #search_nearest
219
- def search_nearest_addr (*args, **kw)
220
+ def search_nearest_addr(*args, **kw)
220
221
  to_string.search_nearest_addr(*args, **kw)
221
222
  end
222
223
  end
@@ -36,7 +36,7 @@ class CAFrame
36
36
  # The row of each cell, UNDEF where the cell is masked: a first or last
37
37
  # taken of it picks the row, and the row picks the value of a Face that
38
38
  # cannot hold new values, as lag picks it.
39
- def present_rows (col)
39
+ def present_rows(col)
40
40
  rows = CArray.int64(col.elements).seq
41
41
  rows[col.is_masked] = UNDEF if col.has_mask?
42
42
  rows
@@ -44,19 +44,19 @@ class CAFrame
44
44
 
45
45
  # A verb whose answers lie on the frame's rows, with no column to compute:
46
46
  # a frame of no column would also have no row.
47
- def refuse_no_column_on_rows (verb, out, nrow)
47
+ def refuse_no_column_on_rows(verb, out, nrow)
48
48
  return unless out.is_a?(Hash) && out.empty? && nrow > 0
49
49
  raise ArgumentError, "#{verb}: no column to compute (every column is a key or has no #{verb})"
50
50
  end
51
51
 
52
- def refuse_unknown_fill_columns (verb, fills)
52
+ def refuse_unknown_fill_columns(verb, fills)
53
53
  return unless fills.is_a?(Hash)
54
54
  unknown = fills.keys - @frame.column_names
55
55
  return if unknown.empty?
56
56
  raise ArgumentError, "#{verb}: fill_value names no column #{unknown.map(&:inspect).join(', ')}"
57
57
  end
58
58
 
59
- def over_named_columns (verb, names, key_names, args = [], keywords = {})
59
+ def over_named_columns(verb, names, key_names, args = [], keywords = {})
60
60
  if names.empty?
61
61
  cols = {}
62
62
  @frame.column_names.each do |name|
@@ -92,7 +92,7 @@ class CAFrame
92
92
  # cells do not answer is left out of a verb run over every column, as a
93
93
  # column of a type without the operation is, wherever those cells sit.
94
94
  # refuse_wrong_reduction_arguments has refused what no column would take.
95
- def answer_of_every_column (col, name)
95
+ def answer_of_every_column(col, name)
96
96
  return yield(col, name) if col.face? || col.data_type != CA_OBJECT
97
97
  begin
98
98
  yield(col, name)
@@ -101,10 +101,22 @@ class CAFrame
101
101
  end
102
102
  end
103
103
 
104
+ # The keywords of first / last / nunique / count / all / any: all and any
105
+ # take skip_masked:, as CArray#all / #any do, and the others none.
106
+ # Without **keywords on those verbs Ruby would hand a keyword in as a
107
+ # column name.
108
+ def value_reduction_keywords(verb, keywords)
109
+ allowed = %i[all any].include?(verb) ? [:skip_masked] : []
110
+ unknown = keywords.keys - allowed
111
+ return keywords if unknown.empty?
112
+ raise ArgumentError,
113
+ "unknown keyword#{unknown.size > 1 ? "s" : ""}: " + unknown.map(&:inspect).join(", ")
114
+ end
115
+
104
116
  # The arguments of a reduction, asked of the core on a number before any
105
117
  # column, so a wrong one is not taken for a column that does not answer.
106
118
  # fill_value: is of each column's type, and stays out of this.
107
- def refuse_wrong_reduction_arguments (verb, args, keywords)
119
+ def refuse_wrong_reduction_arguments(verb, args, keywords)
108
120
  return if verb == :count
109
121
  CArray.float64(1).public_send(verb, *args, **keywords.reject { |k, _| k == :fill_value })
110
122
  rescue CArray::DataTypeError, NoMethodError
@@ -118,7 +130,7 @@ class CAFrame
118
130
  # the same: the type would be asked of masked cells only, which answer
119
131
  # with the identity whatever the type. With no rows the type is asked of
120
132
  # blank cells of its kind.
121
- def valueless_object_column? (col, verb, args)
133
+ def valueless_object_column?(col, verb, args)
122
134
  return false if col.data_type != CA_OBJECT
123
135
  return false if col.face? && col.elements == 0
124
136
  return false if %i[count_not_masked count_masked].include?(verb)
@@ -129,7 +141,7 @@ class CAFrame
129
141
  # count with no value counts the present cells, which every type has;
130
142
  # a Face's own count takes a value (CACategorical#count(label)), so the
131
143
  # type is asked about count_not_masked instead.
132
- def column_has? (col, verb, args, keywords)
144
+ def column_has?(col, verb, args, keywords)
133
145
  verb = :count_not_masked if verb == :count && args.empty?
134
146
  # Two cells: one cell does not reach what combines two (a String's
135
147
  # median interpolates, its sum adds), and an empty column answers
@@ -1,4 +1,4 @@
1
- # CAFrame row concatenation (memo §5, spine = addressing + gather).
1
+ # CAFrame row concatenation (memo §5; the core is addressing + gather).
2
2
  #
3
3
  # Two entry points, mirroring the CArray-level taxonomy:
4
4
  #
@@ -207,8 +207,8 @@ class CAFrame
207
207
  first = pieces.first
208
208
  return pieces unless first.face? && pieces.all? { |p| p.class == first.class }
209
209
  # names-check: units, not names
210
- if first.respond_to?(:in_common_unit_with) && pieces.map(&:unit).uniq.size > 1
211
- common = pieces.drop(1).reduce(first) { |acc, p| acc.in_common_unit_with(p).first }
210
+ if first.respond_to?(:in_common_unit_with, true) && pieces.map(&:unit).uniq.size > 1
211
+ common = pieces.drop(1).reduce(first) { |acc, p| acc.send(:in_common_unit_with, p).first }
212
212
  return pieces.map { |p| p.unit == common.unit ? p : p.to_unit(common.unit) }
213
213
  end
214
214
  # names-check: label lists, not names
@@ -65,6 +65,7 @@ class CAFrame
65
65
  # handed over. Cast the frame first (+df.cast+) when the promoted matrix is
66
66
  # what should be written to.
67
67
  def to_ca(writable: false)
68
+ writable = CArray.send(:bool_arg, writable, "writable", "to_ca")
68
69
  cols = @columns.values
69
70
  raise ArgumentError, "frame has no columns to stack into a matrix" if cols.empty?
70
71
  nd = @columns.find { |_, c| c.ndim != 1 }
@@ -152,7 +153,7 @@ class CArray
152
153
  # @raise [ArgumentError] when `axis` is out of range or is the row axis,
153
154
  # when the number of names differs from the length of `axis`, or when
154
155
  # a name repeats.
155
- def to_frame (*names, axis: -1)
156
+ def to_frame(*names, axis: -1)
156
157
  axis = -1 if axis.nil?
157
158
  if ndim < 2
158
159
  raise ArgumentError,
@@ -58,6 +58,8 @@ class CAFrame
58
58
  super(detail)
59
59
  end
60
60
 
61
+ # @return [String] the detail, with the file and the line it was
62
+ # found at in front when they are known.
61
63
  def message
62
64
  return super if @detail.nil?
63
65
  where =
@@ -110,6 +112,9 @@ class CAFrame
110
112
  unless quote.is_a?(String) && quote.size == 1
111
113
  raise ArgumentError, "quote: must be one character (got #{quote.inspect})"
112
114
  end
115
+ if quote.match?(/[\r\n]/)
116
+ raise ArgumentError, "quote: cannot be a line end (got #{quote.inspect})"
117
+ end
113
118
  if sep.include?(quote)
114
119
  raise ArgumentError, "sep: #{sep.inspect} cannot contain the quote character"
115
120
  end
@@ -770,7 +775,7 @@ class CAFrame
770
775
  # The attributes the named headers give the columns +result+ returns, as
771
776
  # [{key => value}, ...] in the same order. A column of the file the
772
777
  # selection left out takes no part.
773
- def column_attrs (ncol)
778
+ def column_attrs(ncol)
774
779
  return nil unless @named_headers
775
780
  indexes = @selection ? selected_indexes(@names, ncol) : (0...ncol).to_a
776
781
  attrs = indexes.map { {} }
@@ -825,4 +830,5 @@ class CAFrame
825
830
  [names, table]
826
831
  end
827
832
  end
833
+ private_constant :CSVReader
828
834
  end