carray 3.0.6 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (277) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +1 -1
  3. data/CHANGELOG.md +7 -1830
  4. data/README.md +62 -35
  5. data/carray.gemspec +16 -4
  6. data/docs/changelogs/CHANGELOG-3.0.md +2464 -0
  7. data/ext/ca_array_pool.c +4 -5
  8. data/ext/ca_axis_descriptor.h +45 -30
  9. data/ext/ca_axis_dispatch.c +7 -4
  10. data/ext/ca_axis_group.c +12 -15
  11. data/ext/ca_bincmp_dispatch.h +16 -38
  12. data/ext/ca_binop_dispatch.c +35 -0
  13. data/ext/ca_binop_dispatch.h +22 -46
  14. data/ext/ca_categorical_iterator.c +36 -34
  15. data/ext/ca_compare.c +0 -1
  16. data/ext/ca_compare.h +8 -1
  17. data/ext/ca_composite_dispatch.h +6 -0
  18. data/ext/ca_for_buffer.h +6 -1
  19. data/ext/ca_group_iter.c +17 -18
  20. data/ext/ca_iter_state_fields.h +196 -0
  21. data/ext/ca_iter_substrate.h +84 -103
  22. data/ext/ca_kernel_iterator.c +125 -89
  23. data/ext/ca_kernel_iterator.h +229 -1504
  24. data/ext/ca_kernel_iterator_internal.h +1167 -0
  25. data/ext/ca_moncmp_dispatch.h +17 -24
  26. data/ext/ca_monop_dispatch.h +25 -74
  27. data/ext/ca_obj_array.c +92 -165
  28. data/ext/ca_obj_bincmp.c +65 -48
  29. data/ext/ca_obj_binop.c +74 -49
  30. data/ext/ca_obj_bitarray.c +7 -21
  31. data/ext/ca_obj_bitfield.c +93 -26
  32. data/ext/ca_obj_block.c +11 -12
  33. data/ext/ca_obj_byte_swap.c +34 -34
  34. data/ext/ca_obj_const_string.c +464 -304
  35. data/ext/ca_obj_face.c +167 -121
  36. data/ext/ca_obj_face.h +32 -170
  37. data/ext/ca_obj_fake.c +127 -34
  38. data/ext/ca_obj_farray.c +13 -9
  39. data/ext/ca_obj_field.c +17 -13
  40. data/ext/ca_obj_fixlen_string.c +57 -34
  41. data/ext/ca_obj_grid.c +149 -113
  42. data/ext/ca_obj_meld.c +28 -26
  43. data/ext/ca_obj_moncmp.c +10 -21
  44. data/ext/ca_obj_monop.c +88 -73
  45. data/ext/ca_obj_object.c +60 -46
  46. data/ext/ca_obj_record.c +74 -22
  47. data/ext/ca_obj_reduce.c +10 -14
  48. data/ext/ca_obj_refer.c +146 -39
  49. data/ext/ca_obj_remap.c +11 -29
  50. data/ext/ca_obj_repeat.c +41 -23
  51. data/ext/ca_obj_roll.c +20 -28
  52. data/ext/ca_obj_select.c +17 -27
  53. data/ext/ca_obj_select_axis.c +35 -42
  54. data/ext/ca_obj_shift.c +22 -42
  55. data/ext/ca_obj_source.c +1 -5
  56. data/ext/ca_obj_stack.c +33 -29
  57. data/ext/ca_obj_stride.c +99 -62
  58. data/ext/ca_obj_string.c +8 -20
  59. data/ext/ca_obj_tile.c +14 -16
  60. data/ext/ca_obj_time.c +12 -23
  61. data/ext/ca_obj_timedelta.c +12 -23
  62. data/ext/ca_obj_transpose.c +8 -10
  63. data/ext/ca_obj_triop.c +48 -49
  64. data/ext/ca_obj_window.c +87 -99
  65. data/ext/ca_op_cmplx64.h +4 -0
  66. data/ext/ca_op_ipower.c +97 -27
  67. data/ext/ca_op_powi.h +9 -28
  68. data/ext/ca_pow5_table.h +2 -1
  69. data/ext/ca_rng_normal.h +4 -0
  70. data/ext/ca_rng_xoshiro256pp.h +3 -0
  71. data/ext/ca_sort_kernels.h +23 -26
  72. data/ext/ca_sweep_engine.c +92 -26
  73. data/ext/ca_sweep_engine.h +22 -15
  74. data/ext/ca_transform_common.c +3 -8
  75. data/ext/ca_triop_dispatch.h +20 -32
  76. data/ext/caframe_csv_reader.c +12 -12
  77. data/ext/carray.h +300 -1009
  78. data/ext/carray_access.c +320 -207
  79. data/ext/carray_address_basis.c +128 -59
  80. data/ext/carray_attribute.c +81 -104
  81. data/ext/carray_bincount.c +12 -14
  82. data/ext/carray_broadcast.c +31 -27
  83. data/ext/carray_call_cfunc.c +174 -285
  84. data/ext/carray_call_cfunc.h +36 -28
  85. data/ext/carray_cast.c +1191 -351
  86. data/ext/carray_cast_func.rb +68 -8
  87. data/ext/carray_class.c +6 -11
  88. data/ext/carray_conversion.c +98 -39
  89. data/ext/carray_copy.c +52 -26
  90. data/ext/carray_core.c +286 -132
  91. data/ext/carray_count.c +53 -22
  92. data/ext/carray_element.c +49 -28
  93. data/ext/carray_factorize.c +64 -63
  94. data/ext/carray_generate.c +26 -19
  95. data/ext/carray_histogram.c +9 -11
  96. data/ext/carray_hold.c +7 -9
  97. data/ext/carray_index_classifier.c +127 -230
  98. data/ext/carray_index_classifier.h +11 -10
  99. data/ext/carray_integer_total.c +523 -0
  100. data/ext/carray_internal.h +1364 -156
  101. data/ext/carray_key_windows.c +3 -3
  102. data/ext/carray_lazy.c +37 -37
  103. data/ext/carray_loop.c +27 -23
  104. data/ext/carray_mask.c +235 -150
  105. data/ext/carray_math_kernel.h +23 -47
  106. data/ext/carray_mathfunc.c +10 -3
  107. data/ext/carray_median_percentile.c +106 -100
  108. data/ext/carray_memory_view.c +83 -69
  109. data/ext/carray_operator.c +490 -128
  110. data/ext/carray_order.c +163 -84
  111. data/ext/carray_partition.c +14 -16
  112. data/ext/carray_random.c +48 -42
  113. data/ext/carray_round.c +33 -5
  114. data/ext/carray_scatter.c +57 -39
  115. data/ext/carray_segment.c +42 -46
  116. data/ext/carray_slab.c +77 -51
  117. data/ext/carray_slab.h +44 -33
  118. data/ext/carray_sort.c +37 -31
  119. data/ext/carray_sort_kernel.c +36 -10
  120. data/ext/carray_struct.c +44 -34
  121. data/ext/carray_test.c +131 -81
  122. data/ext/carray_time_text.c +30 -31
  123. data/ext/carray_undef.c +50 -7
  124. data/ext/carray_utils.c +373 -139
  125. data/ext/extconf.rb +24 -17
  126. data/ext/mk_call_cfunc.rb +25 -31
  127. data/ext/mk_pow5_table.rb +2 -1
  128. data/ext/mkkernel.rb +819 -488
  129. data/ext/ruby_carray.c +47 -42
  130. data/ext/version.h +7 -5
  131. data/lib/carray/attribute.rb +19 -18
  132. data/lib/carray/autoload_carray.rb +20 -15
  133. data/lib/carray/autoload_method_extension.rb +64 -57
  134. data/lib/carray/axis_group.rb +272 -185
  135. data/lib/carray/basics.rb +298 -56
  136. data/lib/carray/bincount_nd.rb +41 -18
  137. data/lib/carray/block_iterator.rb +167 -119
  138. data/lib/carray/boolean_reduce.rb +7 -4
  139. data/lib/carray/categorical.rb +84 -59
  140. data/lib/carray/categorical_iterator.rb +90 -67
  141. data/lib/carray/complex.rb +61 -41
  142. data/lib/carray/conditional.rb +98 -23
  143. data/lib/carray/const_string.rb +112 -46
  144. data/lib/carray/core_extensions.rb +20 -69
  145. data/lib/carray/distance_limit.rb +7 -5
  146. data/lib/carray/factories.rb +269 -0
  147. data/lib/carray/fixlen_string.rb +95 -12
  148. data/lib/carray/frame/column_verbs.rb +76 -9
  149. data/lib/carray/frame/concat.rb +51 -7
  150. data/lib/carray/frame/convert.rb +2 -1
  151. data/lib/carray/frame/csv_parser.rb +20 -5
  152. data/lib/carray/frame/frame.rb +73 -31
  153. data/lib/carray/frame/group.rb +247 -187
  154. data/lib/carray/frame/io.rb +36 -12
  155. data/lib/carray/frame/join.rb +56 -17
  156. data/lib/carray/frame/records.rb +7 -5
  157. data/lib/carray/frame/reshape.rb +27 -7
  158. data/lib/carray/frame/sort.rb +10 -8
  159. data/lib/carray/frame/verbs.rb +217 -134
  160. data/lib/carray/frame/windows.rb +254 -19
  161. data/lib/carray/frame.rb +1 -2
  162. data/lib/carray/fuse_source.rb +28 -23
  163. data/lib/carray/fusion.rb +30 -30
  164. data/lib/carray/histogram.rb +35 -16
  165. data/lib/carray/inspect.rb +7 -7
  166. data/lib/carray/iterator.rb +104 -23
  167. data/lib/carray/key_windows.rb +93 -61
  168. data/lib/carray/lazy.rb +208 -111
  169. data/lib/carray/limits.rb +80 -0
  170. data/lib/carray/mask_gap_fill.rb +21 -16
  171. data/lib/carray/math.rb +87 -50
  172. data/lib/carray/meld_reduce.rb +30 -8
  173. data/lib/carray/methods/align_addr.rb +29 -17
  174. data/lib/carray/methods/bin.rb +8 -1
  175. data/lib/carray/methods/bincount.rb +4 -2
  176. data/lib/carray/methods/bit_string.rb +19 -31
  177. data/lib/carray/methods/broadcast.rb +8 -3
  178. data/lib/carray/methods/choose.rb +5 -5
  179. data/lib/carray/methods/composition.rb +73 -72
  180. data/lib/carray/methods/{index.rb → coordinate.rb} +13 -13
  181. data/lib/carray/methods/discovery_along.rb +3 -3
  182. data/lib/carray/methods/factorize.rb +2 -0
  183. data/lib/carray/methods/gather_nd.rb +106 -103
  184. data/lib/carray/methods/insert_block.rb +2 -2
  185. data/lib/carray/methods/is_between.rb +6 -4
  186. data/lib/carray/methods/is_in.rb +183 -143
  187. data/lib/carray/methods/join.rb +2 -1
  188. data/lib/carray/methods/lag.rb +6 -2
  189. data/lib/carray/methods/locate_addr.rb +157 -94
  190. data/lib/carray/methods/mask_duplicates.rb +1 -1
  191. data/lib/carray/methods/meshgrid.rb +28 -16
  192. data/lib/carray/methods/mode.rb +67 -67
  193. data/lib/carray/methods/nunique.rb +9 -7
  194. data/lib/carray/methods/repeat.rb +23 -13
  195. data/lib/carray/methods/resize.rb +1 -1
  196. data/lib/carray/methods/snap.rb +24 -20
  197. data/lib/carray/methods/string_format.rb +2 -2
  198. data/lib/carray/methods/unique.rb +4 -2
  199. data/lib/carray/methods/value_counts.rb +1 -1
  200. data/lib/carray/mkmf.rb +25 -28
  201. data/lib/carray/percentile_arguments.rb +82 -0
  202. data/lib/carray/record_order.rb +50 -20
  203. data/lib/carray/rng.rb +1 -0
  204. data/lib/carray/runtime.rb +195 -17
  205. data/lib/carray/segment_iterator.rb +262 -137
  206. data/lib/carray/serialize.rb +635 -566
  207. data/lib/carray/slab_iterator.rb +114 -114
  208. data/lib/carray/stack.rb +14 -15
  209. data/lib/carray/string.rb +16 -22
  210. data/lib/carray/string_operation_extension.rb +97 -52
  211. data/lib/carray/struct.rb +122 -164
  212. data/lib/carray/struct_builder.rb +247 -73
  213. data/lib/carray/time.rb +663 -365
  214. data/lib/carray/window_iterator.rb +232 -155
  215. data/lib/carray.rb +6 -11
  216. data/yard-stubs/ca_obj_array.rb +20 -135
  217. data/yard-stubs/ca_obj_bitarray.rb +1 -8
  218. data/yard-stubs/ca_obj_bitfield.rb +6 -2
  219. data/yard-stubs/ca_obj_byte_swap.rb +21 -7
  220. data/yard-stubs/ca_obj_fake.rb +3 -1
  221. data/yard-stubs/ca_obj_farray.rb +1 -1
  222. data/yard-stubs/ca_obj_field.rb +8 -6
  223. data/yard-stubs/ca_obj_refer.rb +10 -5
  224. data/yard-stubs/ca_obj_roll.rb +7 -6
  225. data/yard-stubs/ca_obj_shift.rb +2 -3
  226. data/yard-stubs/ca_obj_stride.rb +5 -7
  227. data/yard-stubs/ca_obj_tile.rb +1 -1
  228. data/yard-stubs/ca_obj_time.rb +43 -0
  229. data/yard-stubs/ca_obj_transpose.rb +1 -1
  230. data/yard-stubs/ca_obj_window.rb +28 -16
  231. data/yard-stubs/carray_access.rb +55 -6
  232. data/yard-stubs/carray_attribute.rb +26 -38
  233. data/yard-stubs/carray_cast.rb +121 -162
  234. data/yard-stubs/carray_class.rb +3 -4
  235. data/yard-stubs/carray_conversion.rb +6 -7
  236. data/yard-stubs/carray_copy.rb +2 -1
  237. data/yard-stubs/carray_count.rb +10 -5
  238. data/yard-stubs/carray_element.rb +4 -3
  239. data/yard-stubs/carray_limits.rb +100 -0
  240. data/yard-stubs/carray_loop.rb +0 -20
  241. data/yard-stubs/carray_mask.rb +59 -41
  242. data/yard-stubs/carray_math.rb +162 -100
  243. data/yard-stubs/carray_mathfunc.rb +415 -3
  244. data/yard-stubs/carray_median_percentile.rb +29 -11
  245. data/yard-stubs/carray_memory_view.rb +2 -2
  246. data/yard-stubs/carray_order.rb +72 -33
  247. data/yard-stubs/carray_random.rb +6 -6
  248. data/yard-stubs/carray_reduce.rb +353 -0
  249. data/yard-stubs/carray_rng.rb +92 -0
  250. data/yard-stubs/carray_scatter.rb +12 -3
  251. data/yard-stubs/carray_slab.rb +7 -4
  252. data/yard-stubs/carray_sort.rb +7 -6
  253. data/yard-stubs/carray_test.rb +18 -5
  254. data/yard-stubs/carray_undef.rb +44 -5
  255. data/yard-stubs/ruby_carray.rb +75 -40
  256. metadata +63 -25
  257. data/CHANGELOG.v1.md +0 -126
  258. data/ext/carray_build_flags.h +0 -3
  259. data/ext/carray_config.h +0 -28
  260. data/ext/carray_data_type.c +0 -90
  261. data/ext/carray_kernels_bincmp.c +0 -6457
  262. data/ext/carray_kernels_binop.c +0 -31534
  263. data/ext/carray_kernels_init.c +0 -1734
  264. data/ext/carray_kernels_moncmp.c +0 -2104
  265. data/ext/carray_kernels_monop.c +0 -25387
  266. data/ext/carray_kernels_reduce_aggregate.c +0 -27545
  267. data/ext/carray_kernels_reduce_boolean.c +0 -1924
  268. data/ext/carray_kernels_reduce_cumulative.c +0 -11646
  269. data/ext/carray_kernels_reduce_extreme.c +0 -21631
  270. data/ext/carray_kernels_reduce_variance.c +0 -5981
  271. data/ext/carray_kernels_scan.c +0 -4302
  272. data/ext/carray_kernels_search.c +0 -37776
  273. data/ext/carray_kernels_sort.c +0 -10814
  274. data/ext/carray_kernels_triop.c +0 -1864
  275. data/lib/carray/construct.rb +0 -360
  276. data/lib/carray/data_type_extension.rb +0 -261
  277. data/lib/carray/data_type_limits.rb +0 -91
@@ -1,360 +0,0 @@
1
- #
2
- # CA_INT8(data)
3
- # :
4
- # CA_OBJECT(data)
5
- #
6
- # Cast +data+ to a CArray of the data_type indicated by the method name.
7
- # This is a polymorphic cast (not a fresh allocator): the source +data+
8
- # is coerced into a CArray of the target data_type, dispatching on its
9
- # Ruby class. +data+ should be one of the following objects:
10
- #
11
- # * Numeric -> CScalar (0-d CArray)
12
- # * Array -> shape-guessed CArray, element-wise cast
13
- # * CArray -> data_type cast (copy if same data_type, +to_type+ otherwise)
14
- # * Range -> arange-like sequence (start..stop with step 1)
15
- # * String -> parsed (per-data_type parser)
16
- # * nil -> empty CArray
17
- # * any object responding to +to_ca+ or +to_a+ -> coerced then cast
18
- #
19
- # When the block returns a Numeric or CScalar object,
20
- # the resulted array is a CScalar object.
21
-
22
- # CA_BOOLEAN / CA_INT8 / ... / CA_OBJECT (+ type-name aliases CA_BYTE /
23
- # CA_SHORT / CA_INT / CA_FLOAT / CA_DOUBLE / CA_COMPLEX / CA_DCOMPLEX /
24
- # CA_SIZE) and CA_FIXLEN are defined as global functions in C
25
- # (ext/carray_cast.c).
26
-
27
- class CArray
28
-
29
- module DataTypeNewConstructor
30
- end
31
-
32
- module DataTypeExtension
33
- end
34
-
35
- # CArray whose elements are `boolean`. Naming the element type as a
36
- # class lets a constructor be spelled `CArray::Boolean.new(...)`; see
37
- # {DataTypeNewConstructor} for what that form adds.
38
- # @private
39
- class Boolean
40
- extend DataTypeNewConstructor
41
- extend DataTypeExtension
42
- # @!visibility private
43
- TypeSymbol = :boolean
44
- # @!visibility private
45
- DataType = CA_BOOLEAN
46
- end
47
-
48
- # CArray whose elements are `uint8`. Naming the element type as a
49
- # class lets a constructor be spelled `CArray::UInt8.new(...)`; see
50
- # {DataTypeNewConstructor} for what that form adds.
51
- # @private
52
- class UInt8
53
- extend DataTypeNewConstructor
54
- extend DataTypeExtension
55
- # @!visibility private
56
- TypeSymbol = :uint8
57
- # @!visibility private
58
- DataType = CA_UINT8
59
- end
60
-
61
- # CArray whose elements are `uint16`. Naming the element type as a
62
- # class lets a constructor be spelled `CArray::UInt16.new(...)`; see
63
- # {DataTypeNewConstructor} for what that form adds.
64
- # @private
65
- class UInt16
66
- extend DataTypeNewConstructor
67
- extend DataTypeExtension
68
- # @!visibility private
69
- TypeSymbol = :uint16
70
- # @!visibility private
71
- DataType = CA_UINT16
72
- end
73
-
74
- # CArray whose elements are `uint32`. Naming the element type as a
75
- # class lets a constructor be spelled `CArray::UInt32.new(...)`; see
76
- # {DataTypeNewConstructor} for what that form adds.
77
- # @private
78
- class UInt32
79
- extend DataTypeNewConstructor
80
- extend DataTypeExtension
81
- # @!visibility private
82
- TypeSymbol = :uint32
83
- # @!visibility private
84
- DataType = CA_UINT32
85
- end
86
-
87
- # CArray whose elements are `uint64`. Naming the element type as a
88
- # class lets a constructor be spelled `CArray::UInt64.new(...)`; see
89
- # {DataTypeNewConstructor} for what that form adds.
90
- # @private
91
- class UInt64
92
- extend DataTypeNewConstructor
93
- extend DataTypeExtension
94
- # @!visibility private
95
- TypeSymbol = :uint64
96
- # @!visibility private
97
- DataType = CA_UINT64
98
- end
99
-
100
- # CArray whose elements are `int8`. Naming the element type as a
101
- # class lets a constructor be spelled `CArray::Int8.new(...)`; see
102
- # {DataTypeNewConstructor} for what that form adds.
103
- # @private
104
- class Int8
105
- extend DataTypeNewConstructor
106
- extend DataTypeExtension
107
- # @!visibility private
108
- TypeSymbol = :int8
109
- # @!visibility private
110
- DataType = CA_INT8
111
- end
112
-
113
- # CArray whose elements are `int16`. Naming the element type as a
114
- # class lets a constructor be spelled `CArray::Int16.new(...)`; see
115
- # {DataTypeNewConstructor} for what that form adds.
116
- # @private
117
- class Int16
118
- extend DataTypeNewConstructor
119
- extend DataTypeExtension
120
- # @!visibility private
121
- TypeSymbol = :int16
122
- # @!visibility private
123
- DataType = CA_INT16
124
- end
125
-
126
- # CArray whose elements are `int32`. Naming the element type as a
127
- # class lets a constructor be spelled `CArray::Int32.new(...)`; see
128
- # {DataTypeNewConstructor} for what that form adds.
129
- # @private
130
- class Int32
131
- extend DataTypeNewConstructor
132
- extend DataTypeExtension
133
- # @!visibility private
134
- TypeSymbol = :int32
135
- # @!visibility private
136
- DataType = CA_INT32
137
- end
138
-
139
- # CArray whose elements are `int64`. Naming the element type as a
140
- # class lets a constructor be spelled `CArray::Int64.new(...)`; see
141
- # {DataTypeNewConstructor} for what that form adds.
142
- # @private
143
- class Int64
144
- extend DataTypeNewConstructor
145
- extend DataTypeExtension
146
- # @!visibility private
147
- TypeSymbol = :int64
148
- # @!visibility private
149
- DataType = CA_INT64
150
- end
151
-
152
- # CArray whose elements are `float32`. Naming the element type as a
153
- # class lets a constructor be spelled `CArray::Float32.new(...)`; see
154
- # {DataTypeNewConstructor} for what that form adds.
155
- # @private
156
- class Float32
157
- extend DataTypeNewConstructor
158
- extend DataTypeExtension
159
- # @!visibility private
160
- TypeSymbol = :float32
161
- # @!visibility private
162
- DataType = CA_FLOAT32
163
- end
164
-
165
- # Numo-compatible alias of {Float32}.
166
- # @private
167
- SFloat = Float32
168
-
169
- # CArray whose elements are `float64`. Naming the element type as a
170
- # class lets a constructor be spelled `CArray::Float64.new(...)`; see
171
- # {DataTypeNewConstructor} for what that form adds.
172
- # @private
173
- class Float64
174
- extend DataTypeNewConstructor
175
- extend DataTypeExtension
176
- # @!visibility private
177
- TypeSymbol = :float64
178
- # @!visibility private
179
- DataType = CA_FLOAT64
180
- end
181
-
182
- # Numo-compatible alias of {Float64}.
183
- # @private
184
- DFloat = Float64
185
-
186
- # CArray whose elements are `complex64`. Naming the element type as a
187
- # class lets a constructor be spelled `CArray::Complex64.new(...)`; see
188
- # {DataTypeNewConstructor} for what that form adds.
189
- # @private
190
- class Complex64
191
- extend DataTypeNewConstructor
192
- extend DataTypeExtension
193
- # @!visibility private
194
- TypeSymbol = :complex64
195
- # @!visibility private
196
- DataType = CA_CMPLX64
197
- end
198
-
199
- # Numo-compatible alias of {Complex64}.
200
- # @private
201
- SComplex = Complex64
202
-
203
- # CArray whose elements are `complex128`. Naming the element type as a
204
- # class lets a constructor be spelled `CArray::Complex128.new(...)`; see
205
- # {DataTypeNewConstructor} for what that form adds.
206
- # @private
207
- class Complex128
208
- extend DataTypeNewConstructor
209
- extend DataTypeExtension
210
- # @!visibility private
211
- TypeSymbol = :complex128
212
- # @!visibility private
213
- DataType = CA_CMPLX128
214
- end
215
-
216
- # Numo-compatible alias of {Complex128}.
217
- # @private
218
- DComplex = Complex128
219
-
220
- # CArray whose elements are `object`. Naming the element type as a
221
- # class lets a constructor be spelled `CArray::Object.new(...)`; see
222
- # {DataTypeNewConstructor} for what that form adds.
223
- # @private
224
- class Object
225
- extend DataTypeNewConstructor
226
- extend DataTypeExtension
227
- # @!visibility private
228
- TypeSymbol = :object
229
- # @!visibility private
230
- DataType = CA_OBJECT
231
- end
232
-
233
- # Numo-compatible alias of {Object}.
234
- # @private
235
- RObject = Object
236
-
237
- # CArray whose elements are `fixlen`. Naming the element type as a
238
- # class lets a constructor be spelled `CArray::Fixlen.new(...)`; see
239
- # {DataTypeNewConstructor} for what that form adds.
240
- # @private
241
- class Fixlen
242
- extend DataTypeNewConstructor
243
- extend DataTypeExtension
244
- # @!visibility private
245
- TypeSymbol = :fixlen
246
- # @!visibility private
247
- DataType = CA_FIXLEN
248
- end
249
-
250
- end
251
-
252
- class CArray
253
- extend DataTypeExtension
254
- # @!visibility private
255
- # @private
256
- TypeSymbol = nil
257
- # @!visibility private
258
- DataType = nil
259
-
260
- # @overload empty(data_type, dim, bytes: nil)
261
- # Returns a new CArray of `data_type` with the shape `dim`, whose
262
- # contents are **undefined**. This is {CArray.new} without the fill:
263
- # the arguments are read the same way, and the cost of zeroing the
264
- # buffer is not paid. Use it when every cell is written before it is
265
- # read; otherwise use `CArray.new`, which zero-fills.
266
- #
267
- # `CA_OBJECT` is the exception: its cells are zero-initialised
268
- # anyway, because the garbage collector walks them.
269
- #
270
- # @param data_type [Symbol, String, Integer] element type.
271
- # @param dim [Array<Integer>] shape of the new CArray.
272
- # @param bytes [Integer, nil] element width, for `:fixlen` only.
273
- # @return [CArray]
274
- # @raise [ArgumentError] if a block is given -- filling an array is
275
- # what `CArray.new` is for.
276
- # @overload empty(*shape)
277
- # (Numo / NumPy compatibility) Returns an uninitialised CArray of
278
- # the given shape. The element type comes from the receiver, so
279
- # `CArray` itself gives `CA_FLOAT64`; see
280
- # {DataTypeExtension#empty}.
281
- # @param shape [Array<Integer>, Array<Array<Integer>>] shape.
282
- # @return [CArray]
283
- def self.empty (*args, **options, &block)
284
- if block
285
- raise ArgumentError,
286
- "CArray.empty does not take a block " \
287
- "(its contents are left undefined); use CArray.new to fill."
288
- end
289
- if args.size == 2 and args[1].is_a?(Array)
290
- return empty_of_data_type(*args, **options)
291
- end
292
- unless options.empty?
293
- raise ArgumentError,
294
- "CArray.empty(*shape) takes no options; bytes: belongs to " \
295
- "CArray.empty(data_type, dim)"
296
- end
297
- super(*args)
298
- end
299
- end
300
-
301
- class CArray
302
-
303
- # Constructors added to every typed class ({Float64}, {Int32}, ...) so the
304
- # class itself names the element type. Each forwards to the `CArray`
305
- # equivalent with `data_type:` filled in.
306
- module DataTypeNewConstructor
307
-
308
- # @overload new(*shape)
309
- # Allocates a new CArray of this typed subclass with the given
310
- # shape.
311
- # @param shape [Array<Integer>] shape of the new CArray.
312
- # @return [CArray]
313
- def new (*shape)
314
- CArray.new(self::DataType, shape)
315
- end
316
-
317
- # @overload from_memory_view(obj, mask: nil)
318
- # Returns a fresh CArray interpreted as this typed subclass's
319
- # `data_type`, imported by copy from a MemoryView producer.
320
- # Forwards to
321
- # `CArray.from_memory_view(obj, data_type: self::DataType)`. A
322
- # typed producer keeps its shape and must agree with the type; a
323
- # typeless one is read as 1-D (chain `.reshape` for higher rank).
324
- # @param obj [Object] MemoryView producer.
325
- # @param mask [Object, nil] MemoryView producer of the mask.
326
- # @return [CArray]
327
- def from_memory_view (obj, mask: nil)
328
- CArray.from_memory_view(obj, data_type: self::DataType, mask: mask)
329
- end
330
-
331
- # @overload wrap_memory_view(obj, mask: nil)
332
- # Returns a zero-copy CAWrap over `obj`'s MemoryView interpreted
333
- # as this typed subclass's `data_type`. Forwards to
334
- # `CArray.wrap_memory_view(obj, data_type: self::DataType)`. A
335
- # typed producer keeps its shape and must agree with the type; a
336
- # typeless one is read as 1-D (chain `.reshape` for higher rank).
337
- #
338
- # Here the receiver names the element type, not the class of the
339
- # result: the call forwards with `CArray` as the receiver, so a
340
- # `CAWrap` comes back. Choosing the class is the other reading of
341
- # the receiver, and it lives on `CArray.wrap_memory_view` itself.
342
- # @param obj [Object] MemoryView producer.
343
- # @param mask [Object, nil] MemoryView producer of the mask.
344
- # @return [CAWrap]
345
- def wrap_memory_view (obj, mask: nil)
346
- CArray.wrap_memory_view(obj, data_type: self::DataType, mask: mask)
347
- end
348
-
349
- end
350
-
351
- # DataTypeExtension is the carrier module for Numo / NumPy-style
352
- # factory methods that are extended onto CArray and every typed
353
- # CArray::Int32 / Float64 / ... class below. Its body is defined
354
- # in lib/carray/data_type_extension.rb (required after this file),
355
- # keeping the soft-compatibility surface separate from carray's
356
- # native constructor API.
357
- module DataTypeExtension
358
- end
359
-
360
- end
@@ -1,261 +0,0 @@
1
- # Numo / NumPy-style factory class methods, gathered here as a
2
- # best-effort soft compatibility layer.
3
- #
4
- # Rationale (the apology this file owes the rest of carray):
5
- #
6
- # carray's native idiom for building arrays is the typed
7
- # constructor (CArray.float64(3, 3) { 0 }, CA_INT([1, 2, 3]), and
8
- # friends). The methods below -- zeros / ones / eye / identity /
9
- # linspace / arange / full / meshgrid -- duplicate that
10
- # functionality with NumPy / Numo-flavoured names. They exist
11
- # for one specific reason: when a user is porting a NumPy or Numo
12
- # example program over to carray, the first thing they hit is
13
- # the array-creation entry point, and "I have to learn a whole
14
- # different API just to call zeros" is the worst place to lose
15
- # someone. Having these helpers means a paste from
16
- #
17
- # np.zeros((3, 3)) -> CArray.zeros([3, 3])
18
- # np.full((3, 3), 7) -> CArray.full([3, 3], 7)
19
- # np.linspace(0.0, 1.0, 5) -> CArray.linspace(0.0, 1.0, 5)
20
- # Numo::DFloat.zeros(3, 3) -> CArray::Float64.zeros(3, 3)
21
- #
22
- # works with minimal rewriting. Both the splat-shape carray-ish
23
- # form (CArray.zeros(3, 3)) and the array-shape NumPy-ish form
24
- # (CArray.zeros([3, 3])) are accepted by the shape-taking
25
- # factories.
26
- #
27
- # The methods land on CArray itself and on every typed class
28
- # (CArray::Int32, CArray::Float64, etc.) via the
29
- # DataTypeExtension module declared (empty) in
30
- # lib/carray/construct.rb. Required AFTER construct.rb in
31
- # lib/carray.rb so the module exists -- the extends in construct
32
- # see this file's method additions transparently.
33
- #
34
- # meshgrid was previously in this file; it is now in
35
- # lib/carray/methods/meshgrid.rb (autoloaded on first call).
36
- #
37
- # These helpers are not the "main" carray API. When writing new
38
- # carray-flavoured code, prefer the typed constructors. Use
39
- # these when porting external code or when a NumPy / Numo
40
- # spelling reads more naturally to the audience.
41
-
42
- class CArray
43
-
44
- module DataTypeExtension
45
-
46
- def guess_data_type_from_values (*values)
47
- if values.all? {|v| v == true || v == false }
48
- CA_BOOLEAN
49
- elsif values.all? { |v| v.is_a?(Integer) }
50
- CA_INT64
51
- elsif values.all? { |v| v.is_a?(Float) }
52
- CA_FLOAT64
53
- elsif values.all? { |v| v.is_a?(Complex) }
54
- CA_CMPLX128
55
- else
56
- CA_OBJECT
57
- end
58
- end
59
-
60
- private :guess_data_type_from_values
61
-
62
- # Normalize a shape argument list: accept both
63
- #
64
- # foo(3, 4) # splat (carray-style)
65
- # foo([3, 4]) # Array (NumPy-style: foo((3, 4)))
66
- #
67
- # so users can paste NumPy / Numo example code with minimal
68
- # rewriting.
69
- def normalize_shape (args)
70
- if args.size == 1 && args.first.is_a?(Array)
71
- args.first
72
- else
73
- args
74
- end
75
- end
76
-
77
- private :normalize_shape
78
-
79
- # @overload zeros(*shape)
80
- # Returns a new CArray of the given shape filled with zeros.
81
- # Shape accepts both splat Integers and a single Array. When
82
- # called on a typed class the `data_type` is taken from the
83
- # class; otherwise it defaults to `CA_FLOAT64`.
84
- # @param shape [Array<Integer>, Array<Array<Integer>>] shape
85
- # of the result.
86
- # @return [CArray]
87
- def zeros (*args)
88
- CArray.new(self::DataType || CA_FLOAT64, normalize_shape(args)).zero
89
- end
90
-
91
- # @overload ones(*shape)
92
- # Returns a new CArray of the given shape filled with ones.
93
- # Same shape and `data_type` rules as {#zeros}.
94
- # @param shape [Array<Integer>, Array<Array<Integer>>]
95
- # @return [CArray]
96
- def ones (*args)
97
- CArray.new(self::DataType || CA_FLOAT64, normalize_shape(args)).one
98
- end
99
-
100
- # @overload eye(n, m = n, k = 0)
101
- # Returns a 2-D CArray with ones on the `k`-th diagonal and
102
- # zeros elsewhere.
103
- # @param n [Integer] number of rows.
104
- # @param m [Integer] number of columns; defaults to `n`.
105
- # @param k [Integer] diagonal offset (positive above the main
106
- # diagonal, negative below).
107
- # @return [CArray] shape `(n, m)`.
108
- def eye (n, m = nil, k = 0)
109
- m ||= n
110
- mat = CArray.new(self::DataType || CA_FLOAT64, [n, m])
111
- if k >= 0
112
- count = [n, m - k].min
113
- start = k
114
- else
115
- count = [n + k, m].min
116
- start = (-k) * m
117
- end
118
- if count > 0
119
- mat[[start, count, m+1]] = 1
120
- end
121
- mat
122
- end
123
-
124
- # @overload identity(n)
125
- # Returns the `n` by `n` identity matrix.
126
- # @param n [Integer] matrix size.
127
- # @return [CArray] shape `(n, n)`.
128
- def identity (n)
129
- mat = CArray.new(self::DataType || CA_FLOAT64, [n, n])
130
- mat[[nil,n+1]] = 1
131
- mat
132
- end
133
-
134
- # @overload linspace(x1, x2, n = 100)
135
- # Returns a 1-D CArray of `n` values evenly spaced from `x1`
136
- # to `x2` inclusive, matching NumPy's `linspace`. For an integer
137
- # target `data_type`, values are computed in float64 with the
138
- # linspace step `(x2 - x1) / (n - 1)`, then floored to the target
139
- # type — the rule `np.linspace(x1, x2, n, dtype=int)` uses (round
140
- # toward -infinity, not toward zero). Both endpoints are hit
141
- # exactly for integer output when `x1` and `x2` are
142
- # integer-valued.
143
- #
144
- # Called on `CArray` itself (module method) integer inputs promote
145
- # to `CA_FLOAT64` and the result is a float array. Called via an
146
- # integer subclass (`CArray::Int32.linspace(...)`), the result is
147
- # an integer array with floor semantics.
148
- #
149
- # @param x1 [Numeric] first value.
150
- # @param x2 [Numeric] last value.
151
- # @param n [Integer] number of samples.
152
- # @return [CArray]
153
- def linspace (x1, x2, n = 100)
154
- data_type = self::DataType
155
- unless data_type
156
- guess = guess_data_type_from_values(x1, x2)
157
- guess = CA_FLOAT64 if guess == CA_INT64
158
- data_type = guess
159
- end
160
- # span is float-only ("N evenly-spaced integers" is ambiguous —
161
- # see basics.rb). Route non-float targets through a float64
162
- # span, then floor + cast; this matches np.linspace(dtype=int)
163
- # bit for bit (floor rounds toward -infinity so negative
164
- # midpoints go the same way as numpy's integer linspace).
165
- float_out = CArray.new(CA_FLOAT64, [n]).span(x1.to_f..x2.to_f)
166
- return float_out if data_type == CA_FLOAT64
167
- # to_type masks a cell the type cannot hold; linspace refuses it
168
- # instead, as arange does.
169
- # coerce: the type the caller asked for
170
- out = float_out.floor.to_type(data_type)
171
- if out.any_masked?
172
- raise RangeError,
173
- "linspace: #{x1.inspect}..#{x2.inspect} is out of range for #{out.data_type_name}"
174
- end
175
- out
176
- end
177
-
178
- # @overload arange(stop)
179
- # Returns a 1-D CArray with values `0, 1, ..., stop - 1`.
180
- # @param stop [Numeric] exclusive upper bound.
181
- # @return [CArray]
182
- # @overload arange(start, stop)
183
- # Returns `start, start + 1, ..., stop - 1`.
184
- # @param start [Numeric] inclusive lower bound.
185
- # @param stop [Numeric] exclusive upper bound.
186
- # @return [CArray]
187
- # @overload arange(start, stop, step)
188
- # Returns values from `start` to `stop` (exclusive) stepping
189
- # by `step`.
190
- # @param start [Numeric] inclusive lower bound.
191
- # @param stop [Numeric] exclusive upper bound.
192
- # @param step [Numeric] spacing.
193
- # @return [CArray]
194
- def arange (*args)
195
- case args.size
196
- when 3
197
- start, stop, step = *args
198
- when 2
199
- start, stop = *args
200
- step = 1
201
- when 1
202
- start = 0
203
- stop, = *args
204
- step = 1
205
- else
206
- raise ArgumentError, "wrong number of arguments (given #{args.size}, expected 1..3)"
207
- end
208
- raise ArgumentError, "step must not be 0" if step == 0
209
- data_type = self::DataType
210
- data_type ||= guess_data_type_from_values(start, stop, step)
211
- # Element count comes from the arguments as given, not from the
212
- # target data type: CArray::Int32.arange(0, 1, 0.25) counts four
213
- # elements from the float step and truncates them on store.
214
- # Integer arguments count exactly (divmod) so that a step dividing
215
- # the span evenly does not gain a spurious element through float
216
- # rounding.
217
- span = stop - start
218
- if span.is_a?(Integer) && step.is_a?(Integer)
219
- q, r = span.divmod(step)
220
- n = r.zero? ? q : q + 1
221
- else
222
- n = (span.to_f / step).ceil
223
- end
224
- n = 0 if n < 0
225
- CArray.new(data_type, [n]).seq(start, step)
226
- end
227
-
228
- # @overload full(shape, fill_value)
229
- # Returns a new CArray of the given shape filled with
230
- # `fill_value`. `shape` accepts an Integer or an Array of
231
- # Integers. The `data_type` is inferred from `fill_value`
232
- # when the receiver is not a typed class.
233
- # @param shape [Integer, Array<Integer>]
234
- # @param fill_value [Object] value used to fill the array.
235
- # @return [CArray]
236
- def full (shape, fill_value)
237
- data_type = self::DataType
238
- data_type ||= guess_data_type_from_values(fill_value)
239
- shape = [shape] unless shape.is_a?(Array)
240
- CArray.new(data_type, shape).fill(fill_value)
241
- end
242
-
243
- # @overload empty(*shape)
244
- # Returns a new CArray of the given shape whose contents are
245
- # **uninitialised**. The caller must overwrite the array
246
- # before reading from it. `CA_OBJECT` silently falls back to
247
- # a zero-VALUE init required for GC safety.
248
- #
249
- # carray's own spelling is `CArray.empty(data_type, dim)`, which
250
- # names the element type instead of taking it from the receiver.
251
- # @param shape [Array<Integer>, Array<Array<Integer>>]
252
- # @return [CArray]
253
- def empty (*args)
254
- CArray.__alloc_uninit__(self::DataType || CA_FLOAT64,
255
- normalize_shape(args))
256
- end
257
-
258
- end
259
-
260
- end
261
-