red_amber 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +39 -20
  3. data/.yardopts +2 -0
  4. data/CHANGELOG.md +113 -0
  5. data/Gemfile +1 -1
  6. data/LICENSE +1 -1
  7. data/README.md +25 -26
  8. data/benchmark/basic.yml +2 -2
  9. data/benchmark/combine.yml +2 -2
  10. data/benchmark/dataframe.yml +2 -2
  11. data/benchmark/group.yml +2 -2
  12. data/benchmark/reshape.yml +2 -2
  13. data/benchmark/vector.yml +3 -0
  14. data/doc/DataFrame.md +32 -12
  15. data/doc/DataFrame_Comparison.md +65 -0
  16. data/doc/SubFrames.md +11 -0
  17. data/doc/Vector.md +207 -1
  18. data/doc/yard-templates/default/fulldoc/html/css/common.css +6 -0
  19. data/lib/red_amber/data_frame.rb +429 -75
  20. data/lib/red_amber/data_frame_combinable.rb +516 -66
  21. data/lib/red_amber/data_frame_displayable.rb +244 -14
  22. data/lib/red_amber/data_frame_indexable.rb +121 -18
  23. data/lib/red_amber/data_frame_loadsave.rb +78 -10
  24. data/lib/red_amber/data_frame_reshaping.rb +184 -14
  25. data/lib/red_amber/data_frame_selectable.rb +622 -66
  26. data/lib/red_amber/data_frame_variable_operation.rb +446 -34
  27. data/lib/red_amber/group.rb +187 -22
  28. data/lib/red_amber/helper.rb +70 -10
  29. data/lib/red_amber/refinements.rb +12 -5
  30. data/lib/red_amber/subframes.rb +1066 -0
  31. data/lib/red_amber/vector.rb +385 -11
  32. data/lib/red_amber/vector_aggregation.rb +312 -0
  33. data/lib/red_amber/vector_binary_element_wise.rb +387 -0
  34. data/lib/red_amber/vector_selectable.rb +217 -12
  35. data/lib/red_amber/vector_unary_element_wise.rb +436 -0
  36. data/lib/red_amber/vector_updatable.rb +278 -34
  37. data/lib/red_amber/version.rb +2 -1
  38. data/lib/red_amber.rb +13 -1
  39. data/red_amber.gemspec +2 -2
  40. metadata +13 -8
  41. data/doc/image/dataframe/reshaping_DataFrames.png +0 -0
  42. data/lib/red_amber/vector_functions.rb +0 -242
@@ -4,7 +4,7 @@
4
4
  # reference: https://arrow.apache.org/docs/cpp/compute.html
5
5
 
6
6
  module RedAmber
7
- # mix-in for class Vector
7
+ # Mix-in for class Vector
8
8
  # Functions to select some data.
9
9
  module VectorSelectable
10
10
  using RefineArray
@@ -12,11 +12,14 @@ module RedAmber
12
12
 
13
13
  # Select elements in the self by indices.
14
14
  #
15
- # @param indices [Array<Numeric>, Vector] indices.
16
- # @yield [Array<Numeric>, Vector] indices.
17
- # @return [Vector] Vector by selected elements.
15
+ # @param indices [Array<Numeric>, Vector]
16
+ # an array-like of indices.
17
+ # @yieldreturn [Array<Numeric>, Vector]
18
+ # an array-like of indices from the block.
19
+ # @return [Vector]
20
+ # vector by selected elements.
18
21
  #
19
- # TODO: support for the option `boundscheck: true`
22
+ # TODO: support for the option `boundscheck: true`
20
23
  def take(*indices, &block)
21
24
  if block
22
25
  unless indices.empty?
@@ -47,11 +50,14 @@ module RedAmber
47
50
 
48
51
  # Select elements in the self by booleans.
49
52
  #
50
- # @param booleans [Array<true, false, nil>, Vector] booleans.
51
- # @yield [Array<true, false, nil>, Vector] booleans.
52
- # @return [Vector] Vector by selected elements.
53
+ # @param booleans [Array<true, false, nil>, Vector]
54
+ # an array-like of booleans.
55
+ # @yieldreturn [Array<true, false, nil>, Vector]
56
+ # an array-like of booleans from the block.
57
+ # @return [Vector]
58
+ # vector by selected elements.
53
59
  #
54
- # TODO: support for the option `null_selection_behavior: :drop`
60
+ # TODO: support for the option `null_selection_behavior: :drop`
55
61
  def filter(*booleans, &block)
56
62
  if block
57
63
  unless booleans.empty?
@@ -87,9 +93,12 @@ module RedAmber
87
93
 
88
94
  # Select elements in the self by indices or booleans.
89
95
  #
90
- # @param args [Array<Numeric, true, false, nil>, Vector] specifier.
91
- # @yield [Array<Numeric, true, false, nil>, Vector] specifier.
92
- # @return [scalar, Array] returns scalar or array.
96
+ # @param args [Array<Numeric, true, false, nil>, Vector]
97
+ # specifier. Indices or booleans.
98
+ # @yieldparam [Array<Numeric, true, false, nil>, Vector]
99
+ # specifier. Indices or booleans.
100
+ # @return [scalar, Array]
101
+ # returns scalar or array.
93
102
  #
94
103
  def [](*args)
95
104
  array =
@@ -139,11 +148,207 @@ module RedAmber
139
148
  to_a.index(element)
140
149
  end
141
150
 
151
+ # Drop nil in self and returns a new Vector as a result.
152
+ #
153
+ # @return [Vector]
154
+ # a Vector without nils.
155
+ #
142
156
  def drop_nil
143
157
  datum = find(:drop_null).execute([data])
144
158
  Vector.create(datum.value)
145
159
  end
146
160
 
161
+ # Arrange values in Vector.
162
+ #
163
+ # @param order [Symbol]
164
+ # sort order.
165
+ # - `:+`, `:ascending` or without argument will sort in increasing order.
166
+ # - `:-` or `:descending` will sort in decreasing order.
167
+ # @return [Vector]
168
+ # sorted Vector.
169
+ # @example Sort in increasing order (default)
170
+ # Vector.new(%w[B D A E C]).sort
171
+ # # same as #sort(:+)
172
+ # # same as #sort(:ascending)
173
+ #
174
+ # # =>
175
+ # #<RedAmber::Vector(:string, size=5):0x000000000000c134>
176
+ # ["A", "B", "C", "D", "E"]
177
+ #
178
+ # @example Sort in decreasing order
179
+ # Vector.new(%w[B D A E C]).sort(:-)
180
+ # # same as #sort(:descending)
181
+ #
182
+ # # =>
183
+ # #<RedAmber::Vector(:string, size=5):0x000000000000c148>
184
+ # ["E", "D", "C", "B", "A"]
185
+ #
186
+ # @since 0.4.0
187
+ #
188
+ def sort(order = :ascending)
189
+ order =
190
+ case order.to_sym
191
+ when :+, :ascending, :increasing
192
+ :ascending
193
+ when :-, :descending, :decreasing
194
+ :descending
195
+ else
196
+ raise VectorArgumentError, "illegal order option: #{order}"
197
+ end
198
+ take(sort_indices(order: order))
199
+ end
200
+
201
+ # Returns numerical rank of self.
202
+ # - Nil values are considered greater than any value.
203
+ # - NaN values are considered greater than any value but smaller than nil values.
204
+ # - Tiebreakers are ranked in order of appearance.
205
+ # - `RankOptions` in C++ function is not implemented in C GLib yet.
206
+ # This method is currently fixed to the default behavior.
207
+ #
208
+ # @return [Vector]
209
+ # 0-based rank of self (0...size in range).
210
+ # @example Rank of float Vector
211
+ # fv = Vector.new(0.1, nil, Float::NAN, 0.2, 0.1); fv
212
+ #
213
+ # # =>
214
+ # #<RedAmber::Vector(:double, size=5):0x000000000000c65c>
215
+ # [0.1, nil, NaN, 0.2, 0.1]
216
+ #
217
+ # fv.rank
218
+ #
219
+ # # =>
220
+ # #<RedAmber::Vector(:uint64, size=5):0x0000000000003868>
221
+ # [0, 4, 3, 2, 1]
222
+ #
223
+ # @example Rank of string Vector
224
+ # sv = Vector.new("A", "B", nil, "A", "C"); sv
225
+ #
226
+ # # =>
227
+ # #<RedAmber::Vector(:string, size=5):0x0000000000003854>
228
+ # ["A", "B", nil, "A", "C"]
229
+ #
230
+ # sv.rank
231
+ #
232
+ # # =>
233
+ # #<RedAmber::Vector(:uint64, size=5):0x0000000000003868>
234
+ # [0, 2, 4, 1, 3]
235
+ #
236
+ # @since 0.4.0
237
+ #
238
+ def rank
239
+ datum = Arrow::Function.find(:rank).execute([data])
240
+ Vector.create(datum.value) - 1
241
+ end
242
+
243
+ # Pick up elements at random.
244
+ #
245
+ # @overload sample()
246
+ # Return a randomly selected element.
247
+ # This is one of an aggregation function.
248
+ #
249
+ # @return [scalar]
250
+ # one of an element in self.
251
+ # @example Sample a element
252
+ # v = Vector.new('A'..'H'); v
253
+ #
254
+ # # =>
255
+ # #<RedAmber::Vector(:string, size=8):0x0000000000011b20>
256
+ # ["A", "B", "C", "D", "E", "F", "G", "H"]
257
+ #
258
+ # v.sample
259
+ #
260
+ # # =>
261
+ # "C"
262
+ #
263
+ # @overload sample(n)
264
+ # Pick up n elements at random.
265
+ #
266
+ # @param n [Integer]
267
+ # positive number of elements to pick.
268
+ # If n is smaller or equal to size, elements are picked by non-repeating.
269
+ # If n is greater than `size`, elements are picked repeatedly.
270
+ # @return [Vector]
271
+ # sampled elements.
272
+ # If n == 1 (in case of `sample(1)`), it returns a Vector of size == 1
273
+ # not a scalar.
274
+ # @example Sample Vector in size 1
275
+ # v.sample(1)
276
+ #
277
+ # # =>
278
+ # #<RedAmber::Vector(:string, size=1):0x000000000001a3b0>
279
+ # ["H"]
280
+ #
281
+ # @example Sample same size of self: every element is picked in random order
282
+ # v.sample(8)
283
+ #
284
+ # # =>
285
+ # #<RedAmber::Vector(:string, size=8):0x000000000001bda0>
286
+ # ["H", "D", "B", "F", "E", "A", "G", "C"]
287
+ #
288
+ # @example Over sampling: "E" and "A" are sampled repeatedly
289
+ # v.sample(9)
290
+ #
291
+ # # =>
292
+ # #<RedAmber::Vector(:string, size=9):0x000000000001d790>
293
+ # ["E", "E", "A", "D", "H", "C", "A", "F", "H"]
294
+ #
295
+ # @overload sample(prop)
296
+ # Pick up elements by proportion `prop` at random.
297
+ #
298
+ # @param prop [Float]
299
+ # positive proportion of elements to pick.
300
+ # Absolute number of elements to pick:`prop*size` is rounded (by `half: :up``).
301
+ # If prop is smaller or equal to 1.0, elements are picked by non-repeating.
302
+ # If prop is greater than 1.0, some elements are picked repeatedly.
303
+ # @return [Vector]
304
+ # sampled elements.
305
+ # If picked element is only one, it returns a Vector of size == 1
306
+ # not a scalar.
307
+ # @example Sample same size of self: every element is picked in random order
308
+ # v.sample(1.0)
309
+ #
310
+ # # =>
311
+ # #<RedAmber::Vector(:string, size=8):0x000000000001bda0>
312
+ # ["D", "H", "F", "C", "A", "B", "E", "G"]
313
+ #
314
+ # @example 2 times over sampling
315
+ # v.sample(2.0)
316
+ #
317
+ # # =>
318
+ # #<RedAmber::Vector(:string, size=16):0x00000000000233e8>
319
+ # ["H", "B", "C", "B", "C", "A", "F", "A", "E", "C", "H", "F", "F", "A", ... ]
320
+ #
321
+ # @since 0.4.0
322
+ #
323
+ def sample(n_or_prop = nil)
324
+ require 'arrow-numo-narray'
325
+
326
+ return nil if size == 0
327
+
328
+ n_sample =
329
+ case n_or_prop
330
+ in Integer
331
+ n_or_prop
332
+ in Float
333
+ (n_or_prop * size).round
334
+ in nil
335
+ return to_a.sample
336
+ else
337
+ raise VectorArgumentError, "must specify Integer or Float but #{n_or_prop}"
338
+ end
339
+ if n_or_prop < 0
340
+ raise VectorArgumentError, '#sample does not accept negative number.'
341
+ end
342
+ return Vector.new([]) if n_sample == 0
343
+
344
+ over_sample = [8 * size, n_sample].max
345
+ over_size = n_sample > size ? n_sample / size * size * 2 : size
346
+ over_vector =
347
+ Vector.create(Numo::UInt32.new(over_size).rand(over_sample).to_arrow_array)
348
+ indices = over_vector.rank.take(*0...n_sample)
349
+ take(indices - ((indices / size) * size))
350
+ end
351
+
147
352
  private
148
353
 
149
354
  # Accepts indices by numeric Vector
@@ -0,0 +1,436 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Available functions in Arrow are shown by `Arrow::Function.all.map(&:name)`
4
+ # reference: https://arrow.apache.org/docs/cpp/compute.html
5
+
6
+ module RedAmber
7
+ # Representing a series of data.
8
+ class Vector
9
+ class << self
10
+ private
11
+
12
+ # @!macro [attach] define_unary_element_wise
13
+ # [Unary element-wise function] Returns a Vector.
14
+ #
15
+ def define_unary_element_wise(function)
16
+ define_method(function) do |**options|
17
+ datum = exec_func_unary(function, options)
18
+ Vector.create(datum.value)
19
+ end
20
+ end
21
+ end
22
+
23
+ # @!macro array_sort_options
24
+ # @param order [:ascending, :descending]
25
+ # ascending: Arrange values in increasing order.
26
+ # descending: Arrange values in decreasing order.
27
+
28
+ # rubocop:disable Layout/LineLength
29
+
30
+ # @!macro round_mode
31
+ # @param round_mode [:down, :up, :towards_zero, :towards_infinity, :half_down, :half_up, :half_towards_zero, :half_towards_infinity, :half_to_even, :half_to_odd]
32
+ # Rounding and tie-breaking mode.
33
+ # - down: Round to nearest integer less than or equal in magnitude (aka “floor”).
34
+ # - up: Round to nearest integer greater than or equal in magnitude (aka “ceil”).
35
+ # - towards_zero: Get the integral part without fractional digits (aka “trunc”).
36
+ # - towards_infinity: Round negative values with :down rule and positive values
37
+ # with :up rule (aka “away from zero”).
38
+ # - half_down: Round ties with :down rule (also called
39
+ # “round half towards negative infinity”).
40
+ # - half_up: Round ties with :up rule (also called
41
+ # “round half towards positive infinity”).
42
+ # - half_towards_zero: Round ties with :towards_zero rule (also called
43
+ # “round half away from infinity”).
44
+ # - half_towards_infinity: Round ties with :towards_infinity rule (also called
45
+ # “round half away from zero”).
46
+ # - half_to_even: Round ties to nearest even integer.
47
+ # - half_to_odd: Round ties to nearest odd integer.
48
+
49
+ # rubocop:enable Layout/LineLength
50
+
51
+ # Calculate the absolute value of self element-wise.
52
+ #
53
+ # Results will wrap around on integer overflow.
54
+ # @return [Vector]
55
+ # abs of each element of self.
56
+ #
57
+ define_unary_element_wise :abs
58
+
59
+ # Compute the inverse cosine of self element-wise.
60
+ #
61
+ # NaN is returned for invalid input values.
62
+ # @return [Vector]
63
+ # acos of each element of self.
64
+ #
65
+ define_unary_element_wise :acos
66
+
67
+ # Compute the inverse sine of self element-wise.
68
+ #
69
+ # NaN is returned for invalid input values.
70
+ # @return [Vector]
71
+ # asin of each element of self.
72
+ #
73
+ define_unary_element_wise :asin
74
+
75
+ # Return the indices that would sort self.
76
+ #
77
+ # Computes indices Vector that define a stable sort of self.
78
+ # By default, nils are considered greater than any other value
79
+ # and are therefore sorted at the end of the Vector.
80
+ # For floating-point types, NaNs are considered greater than any
81
+ # other non-nil value, but smaller than nil.
82
+ # @!method array_sort_indices(order: :ascending)
83
+ # @macro array_sort_options
84
+ # @return [Vector]
85
+ # sort indices of self.
86
+ #
87
+ define_unary_element_wise :array_sort_indices
88
+ alias_method :sort_indexes, :array_sort_indices
89
+ alias_method :sort_indices, :array_sort_indices
90
+ alias_method :sort_index, :array_sort_indices
91
+
92
+ # Compute the inverse tangent of self element-wise.
93
+ #
94
+ # the return value is in the range [-pi/2, pi/2].
95
+ # For a full return range [-pi, pi], see {.atan2} .
96
+ # @return [Vector]
97
+ # atan of each element of self.
98
+ #
99
+ define_unary_element_wise :atan
100
+
101
+ # Bit-wise negate by element-wise.
102
+ #
103
+ # nil values reeturn nil.
104
+ # @return [Vector]
105
+ # bit wise not of each element of self.
106
+ #
107
+ define_unary_element_wise :bit_wise_not
108
+
109
+ # Round up to the nearest integer.
110
+ #
111
+ # Compute the smallest integer value not less in magnitude than each element.
112
+ # @return [Vector]
113
+ # ceil of each element of self.
114
+ # @example
115
+ # double = Vector.new([15.15, 2.5, 3.5, -4.5, -5.5])
116
+ # double.ceil
117
+ #
118
+ # # =>
119
+ # #<RedAmber::Vector(:double, size=5):0x000000000000cd00>
120
+ # [16.0, 3.0, 4.0, -4.0, -5.0]
121
+ #
122
+ define_unary_element_wise :ceil
123
+
124
+ # Compute the cosine of self element-wise.
125
+ #
126
+ # NaN is returned for invalid input values.
127
+ # @return [Vector]
128
+ # cos of each element of self.
129
+ #
130
+ define_unary_element_wise :cos
131
+
132
+ # Compute cumulative sum over the numeric Vector.
133
+ #
134
+ # @note Self must be numeric.
135
+ # @note Return error for integer overflow.
136
+ # @return [Vector]
137
+ # cumulative sum of self.
138
+ #
139
+ define_unary_element_wise :cumulative_sum_checked
140
+
141
+ # Compute cumulative sum over the numeric Vector.
142
+ #
143
+ # @note Self must be numeric.
144
+ # @note Try to cast to Int64 if integer overflow occured.
145
+ # @return [Vector]
146
+ # cumulative sum of self.
147
+ #
148
+ def cumsum
149
+ cumulative_sum_checked
150
+ rescue Arrow::Error::Invalid
151
+ Vector.create(Arrow::Int64Array.new(data)).cumulative_sum_checked
152
+ end
153
+
154
+ # Carry non-nil values backward to fill nil slots.
155
+ #
156
+ # Propagate next valid value backward to previous nil values.
157
+ # Or nothing if all next values are nil.
158
+ # @return [Vector]
159
+ # a Vector which filled nil backward.
160
+ # @example
161
+ # integer = Vector.new([0, 1, nil, 3, nil])
162
+ # integer.fill_nil_backward
163
+ #
164
+ # # =>
165
+ # #<RedAmber::Vector(:uint8, size=5):0x000000000000f974>
166
+ # [0, 1, 3, 3, nil]
167
+ #
168
+ define_unary_element_wise :fill_null_backward
169
+ alias_method :fill_nil_backward, :fill_null_backward
170
+
171
+ # Carry non-nil values forward to fill nil slots.
172
+ #
173
+ # Propagate last valid value backward to next nil values.
174
+ # Or nothing if all previous values are nil.
175
+ # @return [Vector]
176
+ # a Vector which filled nil forward.
177
+ # @example
178
+ # integer = Vector.new([0, 1, nil, 3, nil])
179
+ # integer.fill_nil_forward
180
+ #
181
+ # # =>
182
+ # #<RedAmber::Vector(:uint8, size=5):0x000000000000f960>
183
+ # [0, 1, 1, 3, 3]
184
+ #
185
+ define_unary_element_wise :fill_null_forward
186
+ alias_method :fill_nil_forward, :fill_null_forward
187
+
188
+ # Round down to the nearest integer.
189
+ #
190
+ # Compute the largest integer value not greater in magnitude than each element.
191
+ # @return [Vector]
192
+ # floor of each element of self.
193
+ # @example
194
+ # double = Vector.new([15.15, 2.5, 3.5, -4.5, -5.5])
195
+ # double.floor
196
+ #
197
+ # # =>
198
+ # #<RedAmber::Vector(:double, size=5):0x000000000000cd14>
199
+ # [15.0, 2.0, 3.0, -5.0, -6.0]
200
+ #
201
+ define_unary_element_wise :floor
202
+
203
+ # Return true if value is finite.
204
+ #
205
+ # For each input value, emit true if the value is finite.
206
+ # (i.e. neither NaN, inf, nor -inf).
207
+ # @return [Vector]
208
+ # boolean Vector wheather each element is finite.
209
+ #
210
+ define_unary_element_wise :is_finite
211
+
212
+ # Return true if value is infinity.
213
+ #
214
+ # For each input value, emit true if the value is infinite (inf or -inf).
215
+ # @return [Vector]
216
+ # boolean Vector wheather each element is inf.
217
+ #
218
+ define_unary_element_wise :is_inf
219
+
220
+ # return true if value is nil or NaN.
221
+ #
222
+ # For each input value, emit true if the value is nil or NaN.
223
+ # @return [Vector]
224
+ # boolean Vector wheather each element is na.
225
+ #
226
+ def is_na # rubocop:disable Naming/PredicateName
227
+ numeric? ? (is_nil | is_nan) : is_nil
228
+ end
229
+
230
+ # Return true if NaN.
231
+ #
232
+ # For each input value, emit true if the value is NaN.
233
+ # @return [Vector]
234
+ # boolean Vector wheather each element is nan.
235
+ #
236
+ define_unary_element_wise :is_nan
237
+
238
+ # Return true if nil.
239
+ #
240
+ # @note Arrow::NullOptions is not supported yet.
241
+ # For each input value, emit true if the value is nil.
242
+ # @return [Vector]
243
+ # boolean Vector wheather each element is null.
244
+ #
245
+ define_unary_element_wise :is_null
246
+ alias_method :is_nil, :is_null
247
+
248
+ # Return true if non-nil.
249
+ #
250
+ # For each input value, emit true if the value is valid (i.e. non-nil).
251
+ # @return [Vector]
252
+ # boolean Vector wheather each element is valid.
253
+ #
254
+ define_unary_element_wise :is_valid
255
+
256
+ # Compute natural logarithm.
257
+ #
258
+ # Non-positive values return -inf or NaN. Nil values return nil.
259
+ # @return [Vector]
260
+ # natural logarithm of each element of self.
261
+ #
262
+ define_unary_element_wise :ln
263
+
264
+ # Compute base 10 logarithm.
265
+ #
266
+ # Non-positive values return -inf or NaN. Nil values return nil.
267
+ # @return [Vector]
268
+ # base 10 logarithm of each element of self.
269
+ #
270
+ define_unary_element_wise :log10
271
+
272
+ # Compute natural log of (1+x).
273
+ #
274
+ # Non-positive values return -inf or NaN. Nil values return nil.
275
+ # This function may be more precise than log(1 + x) for x close to zero.
276
+ # @return [Vector]
277
+ # natural log of (each element + 1) of self.
278
+ #
279
+ define_unary_element_wise :log1p
280
+
281
+ # Compute base 2 logarithm.
282
+ #
283
+ # Non-positive values return -inf or NaN. Nil values return nil.
284
+ # @return [Vector]
285
+ # base 2 logarithm of each element of self.
286
+ #
287
+ define_unary_element_wise :log2
288
+
289
+ # Round to a given precision.
290
+ #
291
+ # Options are used to control the number of digits and rounding mode.
292
+ # Default behavior is to round to the nearest integer and
293
+ # use half-to-even rule to break ties.
294
+ # @!method round(n_digits: 0, round_mode: :half_to_even)
295
+ # @param n_digits [Integer]
296
+ # Rounding precision (number of digits to round to).
297
+ # @macro round_mode
298
+ # @return [Vector]
299
+ # round of each element of self.
300
+ # @example
301
+ # double = Vector.new([15.15, 2.5, 3.5, -4.5, -5.5])
302
+ # double.round
303
+ # # or double.round(n_digits: 0, mode: :half_to_even)
304
+ #
305
+ # # =>
306
+ # #<RedAmber::Vector(:double, size=5):0x000000000000cd28>
307
+ # [15.0, 2.0, 4.0, -4.0, -6.0]
308
+ #
309
+ # double.round(mode: :towards_infinity)
310
+ #
311
+ # # =>
312
+ # #<RedAmber::Vector(:double, size=5):0x000000000000cd3c>
313
+ # [16.0, 3.0, 4.0, -5.0, -6.0]
314
+ #
315
+ # double.round(mode: :half_up)
316
+ #
317
+ # # =>
318
+ # #<RedAmber::Vector(:double, size=5):0x000000000000cd50>
319
+ # [15.0, 3.0, 4.0, -4.0, -5.0]
320
+ #
321
+ # double.round(mode: :half_towards_zero)
322
+ #
323
+ # # =>
324
+ # #<RedAmber::Vector(:double, size=5):0x000000000000cd64>
325
+ # [15.0, 2.0, 3.0, -4.0, -5.0]
326
+ #
327
+ # double.round(mode: :half_towards_infinity)
328
+ #
329
+ # # =>
330
+ # #<RedAmber::Vector(:double, size=5):0x000000000000cd78>
331
+ # [15.0, 3.0, 4.0, -5.0, -6.0]
332
+ #
333
+ # double.round(mode: :half_to_odd)
334
+ #
335
+ # # =>
336
+ # #<RedAmber::Vector(:double, size=5):0x000000000000cd8c>
337
+ # [15.0, 3.0, 3.0, -5.0, -5.0]
338
+ #
339
+ # double.round(n_digits: 1)
340
+ #
341
+ # # =>
342
+ # #<RedAmber::Vector(:double, size=5):0x000000000000cda0>
343
+ # [15.2, 2.5, 3.5, -4.5, -5.5]
344
+ #
345
+ # double.round(n_digits: -1)
346
+ #
347
+ # # =>
348
+ # #<RedAmber::Vector(:double, size=5):0x000000000000cdb4>
349
+ # [20.0, 0.0, 0.0, -0.0, -10.0]
350
+ #
351
+ define_unary_element_wise :round
352
+
353
+ # Round to a given multiple.
354
+ #
355
+ # Options are used to control the rounding multiple and rounding mode.
356
+ # Default behavior is to round to the nearest integer and
357
+ # use half-to-even rule to break ties.
358
+ # @!method round_to_multiple(multiple: 1.0, round_mode: :half_to_even)
359
+ # @param multiple [Float, Integer]
360
+ # Rounding scale (multiple to round to).
361
+ # Should be a positive numeric scalar of a type compatible with the argument
362
+ # to be rounded. The cast kernel is used to convert the rounding multiple
363
+ # to match the result type.
364
+ # @macro round_mode
365
+ # @return [Vector]
366
+ # round to multiple of each element of self.
367
+ #
368
+ def round_to_multiple(multiple: 1.0, round_mode: :half_to_even)
369
+ datum = exec_func_unary(:round_to_multiple,
370
+ multiple: Arrow::DoubleScalar.new(multiple),
371
+ round_mode: round_mode)
372
+ Vector.create(datum.value)
373
+ end
374
+
375
+ # Get the signedness of the arguments element-wise.
376
+ #
377
+ # Output is any of (-1,1) for nonzero inputs and 0 for zero input.
378
+ # NaN values return NaN. Integral values return signedness as Int8 and
379
+ # floating-point values return it with the same type as the input values.
380
+ # @return [Vector]
381
+ # sign of each element of self.
382
+ #
383
+ define_unary_element_wise :sign
384
+
385
+ # Compute the sine of self element-wise.
386
+ #
387
+ # NaN is returned for invalid input values.
388
+ # @return [Vector]
389
+ # sine of each element of self.
390
+ #
391
+ define_unary_element_wise :sin
392
+
393
+ # Compute the tangent of self element-wise.
394
+ #
395
+ # NaN is returned for invalid input values.
396
+ # @return [Vector]
397
+ # tangent of each element of self.
398
+ #
399
+ define_unary_element_wise :tan
400
+
401
+ # Compute the integral part
402
+ #
403
+ # Compute the nearest integer not greater in magnitude than each element.
404
+ # @return [Vector]
405
+ # trunc of each element of self.
406
+ #
407
+ define_unary_element_wise :trunc
408
+
409
+ # Compute unique elements
410
+ #
411
+ # Return an array with distinct values. Nils in the input are ignored.
412
+ # @return [Vector]
413
+ # uniq element of self.
414
+ #
415
+ define_unary_element_wise :unique
416
+ alias_method :uniq, :unique
417
+
418
+ # Invert boolean values
419
+ #
420
+ # @return [Vector]
421
+ # not of each element of self.
422
+ #
423
+ define_unary_element_wise :invert
424
+ alias_method :'!', :invert # rubocop:disable Lint/SymbolConversion
425
+ alias_method :not, :invert
426
+
427
+ # Negate the argument element-wise
428
+ #
429
+ # Results will wrap around on integer overflow.
430
+ # @return [Vector]
431
+ # negate of each element of self.
432
+ #
433
+ define_unary_element_wise :negate
434
+ alias_method :'-@', :negate # rubocop:disable Lint/SymbolConversion
435
+ end
436
+ end