red_amber 0.3.0 → 0.4.1

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 (42) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +56 -22
  3. data/.yardopts +2 -0
  4. data/CHANGELOG.md +178 -0
  5. data/Gemfile +1 -1
  6. data/LICENSE +1 -1
  7. data/README.md +29 -30
  8. data/benchmark/basic.yml +7 -7
  9. data/benchmark/combine.yml +3 -3
  10. data/benchmark/dataframe.yml +15 -9
  11. data/benchmark/group.yml +6 -6
  12. data/benchmark/reshape.yml +6 -6
  13. data/benchmark/vector.yml +6 -3
  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 +454 -85
  20. data/lib/red_amber/data_frame_combinable.rb +609 -115
  21. data/lib/red_amber/data_frame_displayable.rb +313 -34
  22. data/lib/red_amber/data_frame_indexable.rb +122 -19
  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 +623 -70
  26. data/lib/red_amber/data_frame_variable_operation.rb +452 -35
  27. data/lib/red_amber/group.rb +186 -22
  28. data/lib/red_amber/helper.rb +74 -14
  29. data/lib/red_amber/refinements.rb +26 -6
  30. data/lib/red_amber/subframes.rb +1101 -0
  31. data/lib/red_amber/vector.rb +362 -11
  32. data/lib/red_amber/vector_aggregation.rb +312 -0
  33. data/lib/red_amber/vector_binary_element_wise.rb +506 -0
  34. data/lib/red_amber/vector_selectable.rb +265 -23
  35. data/lib/red_amber/vector_unary_element_wise.rb +529 -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
@@ -0,0 +1,506 @@
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
+ # Compute the inverse tangent of y/x.
11
+ #
12
+ # [Binary element-wise function] Returns a Vector.
13
+ # The return value is in the range [-pi, pi].
14
+ # @param y [Vector, array-like]
15
+ # numeric array-like.
16
+ # @param x [Vector, array-like]
17
+ # numeric array-like.
18
+ # @return [Vector]
19
+ # inverse tangent of y/x.
20
+ #
21
+ def atan2(y, x) # rubocop:disable Naming/MethodParameterName
22
+ y = y.data if y.is_a? Vector
23
+ x = x.data if x.is_a? Vector
24
+
25
+ datum = Arrow::Function.find(:atan2).execute([y, x])
26
+ Vector.create(datum.value)
27
+ end
28
+
29
+ private
30
+
31
+ # @!macro [attach] define_binary_element_wise
32
+ # [Binary element-wise function] Returns a Vector.
33
+ #
34
+ def define_binary_element_wise(function)
35
+ define_method(function) do |other, **options|
36
+ datum = exec_func_binary(function, other, options)
37
+ Vector.create(datum.value)
38
+ end
39
+ end
40
+
41
+ # @!macro [attach] define_binary_element_wise_logical
42
+ # [Binary element-wise function] Returns a Vector.
43
+ #
44
+ def define_binary_element_wise_logical(method, function)
45
+ define_method(method) do |other, **options|
46
+ datum = exec_func_binary(function, other, options)
47
+ Vector.create(datum.value)
48
+ end
49
+ end
50
+ end
51
+
52
+ # @!macro kleene_logic_and
53
+ # This function behaves as follows with nils:
54
+ # - true and nil = nil
55
+ # - nil and true = nil
56
+ # - false and nil = false
57
+ # - nil and false = false
58
+ # - nil and nil = nil
59
+ # In other words, in this context a nil value really means "unknown",
60
+ # and an unknown value 'and' false is always false.
61
+
62
+ # @!macro kleene_logic_and_not
63
+ # This function behaves as follows with nils:
64
+ # - true and not nil = nil
65
+ # - nil and not false = nil
66
+ # - false and not nil = false
67
+ # - nil and not true = false
68
+ # - nil and not nil = nil
69
+ # In other words, in this context a nil value really means "unknown",
70
+ # and an unknown value 'and not' true is always false,
71
+ # as is false 'and not' an unknown value.
72
+
73
+ # @!macro kleene_logic_or
74
+ # This function behaves as follows with nils:
75
+ # - true or nil = true
76
+ # - nil or true = true
77
+ # - false or nil = nil
78
+ # - nil or false = nil
79
+ # - nil or nil = nil
80
+ # In other words, in this context a nil value really means "unknown",
81
+ # and an unknown value 'or' true is always true.
82
+
83
+ # rubocop:disable Lint/SymbolConversion)
84
+
85
+ # Logical 'and' boolean values with Kleene logic.
86
+ #
87
+ # @macro kleene_logic_and
88
+ # For a different nil behavior, see function {#and_org}.
89
+ # @!method and_kleene(other)
90
+ # @param other [Vector, array-like]
91
+ # boolean array-like.
92
+ # @return [Vector]
93
+ # and_kleene of self and other.
94
+ define_binary_element_wise :and_kleene
95
+ alias_method :'&', :and_kleene
96
+
97
+ # Logical 'and not' boolean values.
98
+ #
99
+ # When a nil is encountered in either input, a nil is output.
100
+ # For a different nil behavior, see function {#and_not_kleene}.
101
+ # @!method and_not(other)
102
+ # @param other [Vector, array-like]
103
+ # boolean array-like.
104
+ # @return [Vector]
105
+ # and not of self and other.
106
+ #
107
+ define_binary_element_wise :and_not
108
+
109
+ # Logical 'and not' boolean values with Kleene logic.
110
+ #
111
+ # @macro kleene_logic_and_not
112
+ # For a different nil behavior, see function {#and_not}.
113
+ # @!method and_not_kleene(other)
114
+ # @param other [Vector, array-like]
115
+ # boolean array-like.
116
+ # @return [Vector]
117
+ # and not kleene of self and other.
118
+ #
119
+ define_binary_element_wise :and_not_kleene
120
+
121
+ # Logical 'and' boolean values.
122
+ #
123
+ # When a nil is encountered in either input, a nil is output.
124
+ # For a different nil behavior, see function {#and_kleene}.
125
+ # @!method and_org(other)
126
+ # @param other [Vector, array-like]
127
+ # boolean array-like.
128
+ # @return [Vector]
129
+ # evacuated `and` of self and other.
130
+ #
131
+ define_binary_element_wise_logical(:and_org, :and)
132
+
133
+ # Bit-wise AND of self and other by element-wise.
134
+ #
135
+ # Nil values return nil.
136
+ # @!method bit_wise_and(other)
137
+ # @param other [Vector, array-like]
138
+ # integer array-like.
139
+ # @return [Vector]
140
+ # bit wise and of self and other.
141
+ #
142
+ define_binary_element_wise :bit_wise_and
143
+
144
+ # Bit-wise OR of self and other by element-wise.
145
+ #
146
+ # Nil values return nil.
147
+ # @!method bit_wise_or(other)
148
+ # @param other [Vector, array-like]
149
+ # integer array-like.
150
+ # @return [Vector]
151
+ # bit wise or of self and other.
152
+ #
153
+ define_binary_element_wise :bit_wise_or
154
+
155
+ # Bit-wise XOR of self and other by element-wise.
156
+ #
157
+ # Nil values return nil.
158
+ # @!method bit_wise_xor(other)
159
+ # @param other [Vector, array-like]
160
+ # integer array-like.
161
+ # @return [Vector]
162
+ # bit wise xor of self and other.
163
+ #
164
+ define_binary_element_wise :bit_wise_xor
165
+
166
+ # Compute base `b` logarithm of self.
167
+ #
168
+ # Non positive values return -inf or NaN. Nil values return nil.
169
+ # @!method logb(b)
170
+ # @param b [Integer]
171
+ # base.
172
+ # @return [Vector]
173
+ # logb of self and other.
174
+ #
175
+ define_binary_element_wise :logb
176
+
177
+ # Compute base `b` logarithm of self.
178
+ #
179
+ # This function is a overflow-checking variant of #logb.
180
+ # @return (see #logb)
181
+ #
182
+ define_binary_element_wise :logb_checked
183
+
184
+ # Logical 'or' boolean values with Kleene logic.
185
+ #
186
+ # @macro kleene_logic_or
187
+ # For a different nil behavior, see function {#or_org}.
188
+ # @!method or_kleene(other)
189
+ # @param other [Vector, array-like]
190
+ # boolean array-like.
191
+ # @return [Vector]
192
+ # or_kleene of self and other.
193
+ #
194
+ define_binary_element_wise :or_kleene
195
+ alias_method :'|', :or_kleene
196
+
197
+ # Logical 'or' boolean values.
198
+ #
199
+ # When a nil is encountered in either input, a nil is output.
200
+ # For a different nil behavior, see function {#or_kleene}.
201
+ # @!method or_org(other)
202
+ # @param other [Vector, array-like]
203
+ # boolean array-like.
204
+ # @return [Vector]
205
+ # evacuated `or` of self and other.
206
+ #
207
+ define_binary_element_wise_logical(:or_org, :or)
208
+
209
+ # Add the arguments element-wise.
210
+ #
211
+ # Results will wrap around on integer overflow.
212
+ # @!method add(other)
213
+ # @param other [Vector, Numeric]
214
+ # other numeric Vector or numeric scalar.
215
+ # @return [Vector]
216
+ # adddition of self and other.
217
+ #
218
+ define_binary_element_wise :add
219
+ alias_method :'+', :add
220
+
221
+ # Add the arguments element-wise.
222
+ #
223
+ # This function is a overflow-checking variant of #add.
224
+ # @return (see #add)
225
+ #
226
+ define_binary_element_wise :add_checked
227
+
228
+ # Divide the arguments element-wise.
229
+ #
230
+ # Integer division by zero returns an error. However, integer overflow
231
+ # wraps around, and floating-point division by zero returns an infinite.
232
+ # @!method divide(divisor)
233
+ # @param divisor [Vector, Numeric]
234
+ # numeric vector or numeric scalar as divisor.
235
+ # @return [Vector]
236
+ # division of self by other.
237
+ #
238
+ define_binary_element_wise :divide
239
+ alias_method :div, :divide
240
+ alias_method :'/', :divide
241
+
242
+ # Divide the arguments element-wise.
243
+ #
244
+ # This function is a overflow-checking variant of #divide.
245
+ # @return (see #divide)
246
+ #
247
+ define_binary_element_wise :divide_checked
248
+
249
+ # Returns element-wise modulo.
250
+ #
251
+ # This is equivalent to `self-other*(self/other).floor`.
252
+ # @param other [Vector, numeric]
253
+ # other numeric Vector or numeric scalar.
254
+ # @return [Vector]
255
+ # modulo of dividing self by other.
256
+ #
257
+ def modulo(other)
258
+ other = other.data if other.is_a?(Vector)
259
+ d = find(:divide).execute([data, other])
260
+ d = find(:floor).execute([d]) if d.value.is_a?(Arrow::DoubleArray)
261
+ m = find(:multiply).execute([d, other])
262
+ datum = find(:subtract).execute([data, m])
263
+ Vector.create(datum.value)
264
+ end
265
+ alias_method :mod, :modulo
266
+ alias_method :'%', :modulo
267
+
268
+ # Returns element-wise modulo.
269
+ #
270
+ # This function is a overflow-checking variant of #modulo.
271
+ # @return (see #modulo)
272
+ #
273
+ def modulo_checked(other)
274
+ other = other.data if other.is_a?(Vector)
275
+ d = find(:divide_checked).execute([data, other])
276
+ d = find(:floor).execute([d]) if d.value.is_a?(Arrow::DoubleArray)
277
+ m = find(:multiply_checked).execute([d, other])
278
+ datum = find(:subtract_checked).execute([data, m])
279
+ Vector.create(datum.value)
280
+ end
281
+
282
+ # Multiply the arguments element-wise.
283
+ #
284
+ # Results will wrap around on integer overflow.
285
+ # @!method multiply(other)
286
+ # @param other [Vector, Numeric]
287
+ # other numeric vector or numeric scalar.
288
+ # @return [Vector]
289
+ # multiplication of self and other.
290
+ #
291
+ define_binary_element_wise :multiply
292
+ alias_method :mul, :multiply
293
+ alias_method :'*', :multiply
294
+
295
+ # Multiply the arguments element-wise.
296
+ #
297
+ # This function is a overflow-checking variant of #multiply.
298
+ # @return (see #multiply)
299
+ #
300
+ define_binary_element_wise :multiply_checked
301
+
302
+ # Raise arguments to power element-wise.
303
+ #
304
+ # Integer to negative integer power returns an error.
305
+ # However, integer overflow wraps around.
306
+ # If either self or exponent is nil the result will be nil.
307
+ # @!method power(exponent)
308
+ # @param exponent [Vector, Numeric]
309
+ # numeric vector or numeric scalar as exponent.
310
+ # @return [Vector]
311
+ # power operation of self and other.
312
+ #
313
+ define_binary_element_wise :power
314
+ alias_method :pow, :power
315
+ alias_method :'**', :power
316
+
317
+ # Raise arguments to power element-wise.
318
+ #
319
+ # This function is a overflow-checking variant of #power.
320
+ # @return (see #power)
321
+ #
322
+ define_binary_element_wise :power_checked
323
+
324
+ # Returns element-wise quotient by double Vector.
325
+ #
326
+ # @param other [Vector, numeric]
327
+ # other numeric Vector or numeric scalar.
328
+ # @return [Vector]
329
+ # quotient of dividing self by other.
330
+ #
331
+ def quotient(other)
332
+ other = other.data if other.is_a?(Vector)
333
+ datum = find(:divide).execute([Arrow::DoubleArray.new(data), other])
334
+ Vector.create(datum.value)
335
+ end
336
+ alias_method :quo, :quotient
337
+ alias_method :fdiv, :quotient
338
+
339
+ # Returns element-wise quotient by double Vector.
340
+ #
341
+ # This function is a overflow-checking variant of #quotient.
342
+ # @return (see #quotient)
343
+ #
344
+ def quotient_checked(other)
345
+ other = other.data if other.is_a?(Vector)
346
+ datum = find(:divide_checked).execute([Arrow::DoubleArray.new(data), other])
347
+ Vector.create(datum.value)
348
+ end
349
+
350
+ # Subtract the arguments element-wise.
351
+ #
352
+ # Results will wrap around on integer overflow.
353
+ # @!method subtract(other)
354
+ # @param other [Vector, Numeric]
355
+ # other numeric vector or numeric scalar.
356
+ # @return [Vector]
357
+ # subtraction of self and other.
358
+ #
359
+ define_binary_element_wise :subtract
360
+ alias_method :sub, :subtract
361
+ alias_method :'-', :subtract
362
+
363
+ # Subtract the arguments element-wise.
364
+ #
365
+ # This function is a overflow-checking variant of #subtract.
366
+ # @return (see #subtract)
367
+ #
368
+ define_binary_element_wise :subtract_checked
369
+
370
+ # Left shift of self by other.
371
+ #
372
+ # The shift operates as if on the two's complement representation of the number.
373
+ # In other words, this is equivalent to multiplying self by 2 to the power 'amount',
374
+ # even if overflow occurs.
375
+ # self is returned if 'amount' (the amount to shift by) is negative or
376
+ # greater than or equal to the precision of self.
377
+ # @!method shift_left(amount)
378
+ # @param amount [integer]
379
+ # the amount to shift by.
380
+ # @return [Vector]
381
+ # shift_left of self by amount.
382
+ #
383
+ define_binary_element_wise :shift_left
384
+ alias_method :'<<', :shift_left
385
+
386
+ # Left shift of self by other.
387
+ #
388
+ # This function is a overflow-checking variant of #shift_left.
389
+ # @return (see #shift_left)
390
+ #
391
+ define_binary_element_wise :shift_left_checked
392
+
393
+ # Right shift of self by other.
394
+ #
395
+ # This is equivalent to dividing `x` by 2 to the power `y`.
396
+ # Self is returned if 'amount' (the amount to shift by) is: negative or
397
+ # greater than or equal to the precision of self.
398
+ # @!method shift_right(amount)
399
+ # @param amount [integer]
400
+ # the amount to shift by.
401
+ # @return [Vector]
402
+ # shift_right of self by amount.
403
+ #
404
+ define_binary_element_wise :shift_right
405
+ alias_method :'>>', :shift_right
406
+
407
+ # Right shift of self by other.
408
+ #
409
+ # This function is a overflow-checking variant of #shift_right.
410
+ # @return (see #shift_right)
411
+ #
412
+ define_binary_element_wise :shift_right_checked
413
+
414
+ # Logical 'xor' boolean values
415
+ #
416
+ # When a nil is encountered in either input, a nil is output.
417
+ # @!method xor(other)
418
+ # @param other [Vector]
419
+ # other boolean vector or boolean scalar.
420
+ # @return [Vector]
421
+ # eq of self and other by a boolean Vector.
422
+ #
423
+ define_binary_element_wise :xor
424
+ alias_method :'^', :xor
425
+
426
+ # Compare values for equality (self == other)
427
+ #
428
+ # A nil on either side emits a nil comparison result.
429
+ # @!method equal(other)
430
+ # @param other [Vector]
431
+ # other vector or scalar.
432
+ # @return [Vector]
433
+ # eq of self and other by a boolean Vector.
434
+ #
435
+ define_binary_element_wise :equal
436
+ alias_method :'==', :equal
437
+ alias_method :eq, :equal
438
+
439
+ # Compare values for ordered inequality (self > other).
440
+ #
441
+ # A nil on either side emits a nil comparison result.
442
+ # @!method greater(other)
443
+ # @param other [Vector]
444
+ # other vector or scalar.
445
+ # @return [Vector]
446
+ # gt of self and other by a boolean Vector.
447
+ #
448
+ define_binary_element_wise :greater
449
+ alias_method :'>', :greater
450
+ alias_method :gt, :greater
451
+
452
+ # Compare values for ordered inequality (self >= other).
453
+ #
454
+ # A nil on either side emits a nil comparison result.
455
+ # @!method greater_equal(other)
456
+ # @param other [Vector]
457
+ # other vector or scalar.
458
+ # @return [Vector]
459
+ # ge of self and other by a boolean Vector.
460
+ #
461
+ define_binary_element_wise :greater_equal
462
+ alias_method :'>=', :greater_equal
463
+ alias_method :ge, :greater_equal
464
+
465
+ # Compare values for ordered inequality (self < other).
466
+ #
467
+ # A nil on either side emits a nil comparison result.
468
+ # @!method less(other)
469
+ # @param other [Vector]
470
+ # other vector or scalar.
471
+ # @return [Vector]
472
+ # lt of self and other by a boolean Vector.
473
+ #
474
+ define_binary_element_wise :less
475
+ alias_method :'<', :less
476
+ alias_method :lt, :less
477
+
478
+ # Compare values for ordered inequality (self <= other).
479
+ #
480
+ # A nil on either side emits a nil comparison result.
481
+ # @!method less_equal(other)
482
+ # @param other [Vector]
483
+ # other vector or scalar.
484
+ # @return [Vector]
485
+ # le of self and other by a boolean Vector.
486
+ #
487
+ define_binary_element_wise :less_equal
488
+ alias_method :'<=', :less_equal
489
+ alias_method :le, :less_equal
490
+
491
+ # Compare values for inequality (self != other).
492
+ #
493
+ # A nil on either side emits a nil comparison result.
494
+ # @!method not_equal(other)
495
+ # @param other [Vector]
496
+ # other vector or scalar.
497
+ # @return [Vector]
498
+ # ne of self and other by a boolean Vector.
499
+ #
500
+ define_binary_element_wise :not_equal
501
+ alias_method :'!=', :not_equal
502
+ alias_method :ne, :not_equal
503
+
504
+ # rubocop:enable Lint/SymbolConversion)
505
+ end
506
+ end