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,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 = :cmplx64
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 = :cmplx128
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,262 +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
- raise TypeError, "linspace: n must be an Integer (got #{n.class})" unless n.is_a?(Integer)
155
- data_type = self::DataType
156
- unless data_type
157
- guess = guess_data_type_from_values(x1, x2)
158
- guess = CA_FLOAT64 if guess == CA_INT64
159
- data_type = guess
160
- end
161
- # span is float-only ("N evenly-spaced integers" is ambiguous —
162
- # see basics.rb). Route non-float targets through a float64
163
- # span, then floor + cast; this matches np.linspace(dtype=int)
164
- # bit for bit (floor rounds toward -infinity so negative
165
- # midpoints go the same way as numpy's integer linspace).
166
- float_out = CArray.new(CA_FLOAT64, [n]).span(x1.to_f..x2.to_f)
167
- return float_out if data_type == CA_FLOAT64
168
- # to_type masks a cell the type cannot hold; linspace refuses it
169
- # instead, as arange does.
170
- # coerce: the type the caller asked for
171
- out = float_out.floor.to_type(data_type)
172
- if out.any_masked?
173
- raise RangeError,
174
- "linspace: #{x1.inspect}..#{x2.inspect} is out of range for #{out.data_type_name}"
175
- end
176
- out
177
- end
178
-
179
- # @overload arange(stop)
180
- # Returns a 1-D CArray with values `0, 1, ..., stop - 1`.
181
- # @param stop [Numeric] exclusive upper bound.
182
- # @return [CArray]
183
- # @overload arange(start, stop)
184
- # Returns `start, start + 1, ..., stop - 1`.
185
- # @param start [Numeric] inclusive lower bound.
186
- # @param stop [Numeric] exclusive upper bound.
187
- # @return [CArray]
188
- # @overload arange(start, stop, step)
189
- # Returns values from `start` to `stop` (exclusive) stepping
190
- # by `step`.
191
- # @param start [Numeric] inclusive lower bound.
192
- # @param stop [Numeric] exclusive upper bound.
193
- # @param step [Numeric] spacing.
194
- # @return [CArray]
195
- def arange (*args)
196
- case args.size
197
- when 3
198
- start, stop, step = *args
199
- when 2
200
- start, stop = *args
201
- step = 1
202
- when 1
203
- start = 0
204
- stop, = *args
205
- step = 1
206
- else
207
- raise ArgumentError, "wrong number of arguments (given #{args.size}, expected 1..3)"
208
- end
209
- raise ArgumentError, "step must not be 0" if step == 0
210
- data_type = self::DataType
211
- data_type ||= guess_data_type_from_values(start, stop, step)
212
- # Element count comes from the arguments as given, not from the
213
- # target data type: CArray::Int32.arange(0, 1, 0.25) counts four
214
- # elements from the float step and truncates them on store.
215
- # Integer arguments count exactly (divmod) so that a step dividing
216
- # the span evenly does not gain a spurious element through float
217
- # rounding.
218
- span = stop - start
219
- if span.is_a?(Integer) && step.is_a?(Integer)
220
- q, r = span.divmod(step)
221
- n = r.zero? ? q : q + 1
222
- else
223
- n = (span.to_f / step).ceil
224
- end
225
- n = 0 if n < 0
226
- CArray.new(data_type, [n]).seq(start, step)
227
- end
228
-
229
- # @overload full(shape, fill_value)
230
- # Returns a new CArray of the given shape filled with
231
- # `fill_value`. `shape` accepts an Integer or an Array of
232
- # Integers. The `data_type` is inferred from `fill_value`
233
- # when the receiver is not a typed class.
234
- # @param shape [Integer, Array<Integer>]
235
- # @param fill_value [Object] value used to fill the array.
236
- # @return [CArray]
237
- def full (shape, fill_value)
238
- data_type = self::DataType
239
- data_type ||= guess_data_type_from_values(fill_value)
240
- shape = [shape] unless shape.is_a?(Array)
241
- CArray.new(data_type, shape).fill(fill_value)
242
- end
243
-
244
- # @overload empty(*shape)
245
- # Returns a new CArray of the given shape whose contents are
246
- # **uninitialised**. The caller must overwrite the array
247
- # before reading from it. `CA_OBJECT` silently falls back to
248
- # a zero-VALUE init required for GC safety.
249
- #
250
- # carray's own spelling is `CArray.empty(data_type, dim)`, which
251
- # names the element type instead of taking it from the receiver.
252
- # @param shape [Array<Integer>, Array<Array<Integer>>]
253
- # @return [CArray]
254
- def empty (*args)
255
- CArray.__alloc_uninit__(self::DataType || CA_FLOAT64,
256
- normalize_shape(args))
257
- end
258
-
259
- end
260
-
261
- end
262
-