red_amber 0.4.0 → 0.4.2

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.
@@ -27,28 +27,6 @@ module RedAmber
27
27
  instance
28
28
  end
29
29
 
30
- # Return true if it is an aggregation function.
31
- #
32
- # @param function [Symbol]
33
- # function name to test.
34
- # @return [Booleans]
35
- # true if function is a aggregation function, otherwise false.
36
- #
37
- # @example
38
- # Vector.aggregate?(:mean) # => true
39
- #
40
- # Vector.aggregate?(:round) # => false
41
- #
42
- # @since 0.4.0
43
- #
44
- def self.aggregate?(function)
45
- %i[
46
- all all? any any? approximate_median count count_distinct count_uniq
47
- max mean median min min_max product quantile sd std stddev sum
48
- unbiased_variance var variance
49
- ].include?(function.to_sym)
50
- end
51
-
52
30
  # Create a Vector.
53
31
  #
54
32
  # @param array [Array, Vector, Range, Arrow::Array, #to_arrow_array]
@@ -532,13 +510,10 @@ module RedAmber
532
510
 
533
511
  yield self
534
512
  else
535
- function = function&.to_sym
536
- unless function && respond_to?(function) && Vector.aggregate?(function)
537
- raise VectorArgumentError, "illegal function: #{function.inspect}"
538
- end
539
-
540
- send(function)
513
+ send(function&.to_sym)
541
514
  end
515
+ raise VectorArgumentError, 'not an aggregation function' if value.is_a?(Vector)
516
+
542
517
  Vector.new([value] * size)
543
518
  end
544
519
  alias_method :expand, :propagate
@@ -555,8 +530,10 @@ module RedAmber
555
530
  case other
556
531
  when Vector
557
532
  find(function).execute([data, other.data], options)
558
- when Arrow::Array, Arrow::ChunkedArray, Arrow::Scalar,
559
- Array, Numeric, String, TrueClass, FalseClass
533
+ when NilClass
534
+ nils = data.class.new([nil] * size)
535
+ find(function).execute([data, nils], options)
536
+ else
560
537
  find(function).execute([data, other], options)
561
538
  end
562
539
  end
@@ -174,6 +174,13 @@ module RedAmber
174
174
  #
175
175
  define_binary_element_wise :logb
176
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
+
177
184
  # Logical 'or' boolean values with Kleene logic.
178
185
  #
179
186
  # @macro kleene_logic_or
@@ -211,6 +218,13 @@ module RedAmber
211
218
  define_binary_element_wise :add
212
219
  alias_method :'+', :add
213
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
+
214
228
  # Divide the arguments element-wise.
215
229
  #
216
230
  # Integer division by zero returns an error. However, integer overflow
@@ -219,11 +233,51 @@ module RedAmber
219
233
  # @param divisor [Vector, Numeric]
220
234
  # numeric vector or numeric scalar as divisor.
221
235
  # @return [Vector]
222
- # division of self and other.
236
+ # division of self by other.
223
237
  #
224
238
  define_binary_element_wise :divide
225
239
  alias_method :'/', :divide
226
240
 
241
+ # Divide the arguments element-wise.
242
+ #
243
+ # This function is a overflow-checking variant of #divide.
244
+ # @return (see #divide)
245
+ #
246
+ define_binary_element_wise :divide_checked
247
+
248
+ # Returns element-wise modulo.
249
+ #
250
+ # This is equivalent to `self-divisor*(self/divisor).floor`.
251
+ # @note Same behavior as Ruby.
252
+ # @param divisor [Vector, numeric]
253
+ # divisor numeric Vector or numeric scalar.
254
+ # @return [Vector]
255
+ # modulo of dividing self by divisor.
256
+ #
257
+ def modulo(divisor)
258
+ divisor = divisor.data if divisor.is_a?(Vector)
259
+ d = find(:divide).execute([data, divisor])
260
+ d = find(:floor).execute([d]) if d.value.is_a?(Arrow::DoubleArray)
261
+ m = find(:multiply).execute([d, divisor])
262
+ datum = find(:subtract).execute([data, m])
263
+ Vector.create(datum.value)
264
+ end
265
+ alias_method :'%', :modulo
266
+
267
+ # Returns element-wise modulo.
268
+ #
269
+ # This function is a overflow-checking variant of #modulo.
270
+ # @return (see #modulo)
271
+ #
272
+ def modulo_checked(divisor)
273
+ divisor = divisor.data if divisor.is_a?(Vector)
274
+ d = find(:divide_checked).execute([data, divisor])
275
+ d = find(:floor).execute([d]) if d.value.is_a?(Arrow::DoubleArray)
276
+ m = find(:multiply_checked).execute([d, divisor])
277
+ datum = find(:subtract_checked).execute([data, m])
278
+ Vector.create(datum.value)
279
+ end
280
+
227
281
  # Multiply the arguments element-wise.
228
282
  #
229
283
  # Results will wrap around on integer overflow.
@@ -234,8 +288,16 @@ module RedAmber
234
288
  # multiplication of self and other.
235
289
  #
236
290
  define_binary_element_wise :multiply
291
+ alias_method :mul, :multiply
237
292
  alias_method :'*', :multiply
238
293
 
294
+ # Multiply the arguments element-wise.
295
+ #
296
+ # This function is a overflow-checking variant of #multiply.
297
+ # @return (see #multiply)
298
+ #
299
+ define_binary_element_wise :multiply_checked
300
+
239
301
  # Raise arguments to power element-wise.
240
302
  #
241
303
  # Integer to negative integer power returns an error.
@@ -248,8 +310,72 @@ module RedAmber
248
310
  # power operation of self and other.
249
311
  #
250
312
  define_binary_element_wise :power
313
+ alias_method :pow, :power
251
314
  alias_method :'**', :power
252
315
 
316
+ # Raise arguments to power element-wise.
317
+ #
318
+ # This function is a overflow-checking variant of #power.
319
+ # @return (see #power)
320
+ #
321
+ define_binary_element_wise :power_checked
322
+
323
+ # Returns element-wise quotient by double Vector.
324
+ #
325
+ # @param divisor [Vector, numeric]
326
+ # divisor numeric Vector or numeric scalar.
327
+ # @return [Vector]
328
+ # quotient of dividing self by divisor.
329
+ #
330
+ def fdiv(divisor)
331
+ divisor = divisor.data if divisor.is_a?(Vector)
332
+ datum = find(:divide).execute([Arrow::DoubleArray.new(data), divisor])
333
+ Vector.create(datum.value)
334
+ end
335
+
336
+ # Returns element-wise quotient by double Vector.
337
+ #
338
+ # This function is a overflow-checking variant of #quotient.
339
+ # @return (see #quotient)
340
+ #
341
+ def fdiv_checked(divisor)
342
+ divisor = divisor.data if divisor.is_a?(Vector)
343
+ datum = find(:divide_checked).execute([Arrow::DoubleArray.new(data), divisor])
344
+ Vector.create(datum.value)
345
+ end
346
+
347
+ # Returns element-wise remainder.
348
+ #
349
+ # This is equivalent to `self-divisor*(self/divisor).trunc`.
350
+ # @note Same behavior as Ruby's remainder.
351
+ # @param divisor [Vector, numeric]
352
+ # divisor numeric Vector or numeric scalar.
353
+ # @return [Vector]
354
+ # modulo of dividing self by divisor.
355
+ #
356
+ def remainder(divisor)
357
+ divisor = divisor.data if divisor.is_a?(Vector)
358
+ d = find(:divide).execute([data, divisor])
359
+ d = find(:trunc).execute([d]) if d.value.is_a?(Arrow::DoubleArray)
360
+ m = find(:multiply).execute([d, divisor])
361
+ datum = find(:subtract).execute([data, m])
362
+ Vector.create(datum.value)
363
+ end
364
+
365
+ # Returns element-wise modulo.
366
+ #
367
+ # This function is a overflow-checking variant of #modulo.
368
+ # @return (see #modulo)
369
+ #
370
+ def remainder_checked(divisor)
371
+ divisor = divisor.data if divisor.is_a?(Vector)
372
+ d = find(:divide_checked).execute([data, divisor])
373
+ d = find(:trunc).execute([d]) if d.value.is_a?(Arrow::DoubleArray)
374
+ m = find(:multiply_checked).execute([d, divisor])
375
+ datum = find(:subtract_checked).execute([data, m])
376
+ Vector.create(datum.value)
377
+ end
378
+
253
379
  # Subtract the arguments element-wise.
254
380
  #
255
381
  # Results will wrap around on integer overflow.
@@ -260,8 +386,16 @@ module RedAmber
260
386
  # subtraction of self and other.
261
387
  #
262
388
  define_binary_element_wise :subtract
389
+ alias_method :sub, :subtract
263
390
  alias_method :'-', :subtract
264
391
 
392
+ # Subtract the arguments element-wise.
393
+ #
394
+ # This function is a overflow-checking variant of #subtract.
395
+ # @return (see #subtract)
396
+ #
397
+ define_binary_element_wise :subtract_checked
398
+
265
399
  # Left shift of self by other.
266
400
  #
267
401
  # The shift operates as if on the two's complement representation of the number.
@@ -278,6 +412,13 @@ module RedAmber
278
412
  define_binary_element_wise :shift_left
279
413
  alias_method :'<<', :shift_left
280
414
 
415
+ # Left shift of self by other.
416
+ #
417
+ # This function is a overflow-checking variant of #shift_left.
418
+ # @return (see #shift_left)
419
+ #
420
+ define_binary_element_wise :shift_left_checked
421
+
281
422
  # Right shift of self by other.
282
423
  #
283
424
  # This is equivalent to dividing `x` by 2 to the power `y`.
@@ -292,6 +433,13 @@ module RedAmber
292
433
  define_binary_element_wise :shift_right
293
434
  alias_method :'>>', :shift_right
294
435
 
436
+ # Right shift of self by other.
437
+ #
438
+ # This function is a overflow-checking variant of #shift_right.
439
+ # @return (see #shift_right)
440
+ #
441
+ define_binary_element_wise :shift_right_checked
442
+
295
443
  # Logical 'xor' boolean values
296
444
  #
297
445
  # When a nil is encountered in either input, a nil is output.
@@ -128,24 +128,55 @@ module RedAmber
128
128
  raise VectorArgumentError, "Invalid argument: #{args}"
129
129
  end
130
130
 
131
- # @param values [Array, Arrow::Array, Vector]
131
+ # Check if elements of self are in the other values.
132
+ #
133
+ # @param values [Vector, Array, Arrow::Array, Arrow::ChunkedArray]
134
+ # values to test existence.
135
+ # @return [Vector]
136
+ # boolean Vector.
137
+ #
132
138
  def is_in(*values)
133
- self_data = chunked? ? data.pack : data
134
-
135
- array =
139
+ enum =
136
140
  case values
137
- in [Vector] | [Arrow::Array] | [Arrow::ChunkedArray]
138
- values[0].to_a
141
+ in [] | [[]] | [nil] |[[nil]]
142
+ return Vector.new([false] * size)
143
+ in [Vector | Arrow::Array | Arrow::ChunkedArray]
144
+ values[0].each
139
145
  else
140
- Array(values).flatten
146
+ parse_args(values, size, symbolize: false)
141
147
  end
142
-
143
- Vector.create(self_data.is_in(array))
148
+ enum.filter_map { self == _1 unless _1.nil? }.reduce(&:|)
144
149
  end
145
150
 
146
- # Arrow's support required
151
+ # Returns index of first matched position of element in self.
152
+ #
153
+ # @param element
154
+ # an element of self.
155
+ # @return [integer, nil]
156
+ # founded position of element. If it is not found, returns nil.
157
+ #
147
158
  def index(element)
148
- to_a.index(element)
159
+ (0...size).find { |i| self[i] == element }
160
+ end
161
+
162
+ # Returns first element of self.
163
+ #
164
+ # @return
165
+ # the first element.
166
+ # @since 0.4.1
167
+ #
168
+ def first
169
+ data[0]
170
+ end
171
+
172
+ # Returns last element of self.
173
+ #
174
+ # @return
175
+ # the last element.
176
+ # @since 0.4.1
177
+ #
178
+ def last
179
+ data[-1]
149
180
  end
150
181
 
151
182
  # Drop nil in self and returns a new Vector as a result.
@@ -236,7 +267,13 @@ module RedAmber
236
267
  # @since 0.4.0
237
268
  #
238
269
  def rank
239
- datum = Arrow::Function.find(:rank).execute([data])
270
+ datum =
271
+ case data
272
+ when Arrow::ChunkedArray
273
+ Arrow::Function.find(:rank).execute([data.pack])
274
+ else
275
+ Arrow::Function.find(:rank).execute([data])
276
+ end
240
277
  Vector.create(datum.value) - 1
241
278
  end
242
279
 
@@ -56,6 +56,13 @@ module RedAmber
56
56
  #
57
57
  define_unary_element_wise :abs
58
58
 
59
+ # Calculate the absolute value of self element-wise.
60
+ #
61
+ # This function is a overflow-checking variant of #abs.
62
+ # @return (see #abs)
63
+ #
64
+ define_unary_element_wise :abs_checked
65
+
59
66
  # Compute the inverse cosine of self element-wise.
60
67
  #
61
68
  # NaN is returned for invalid input values.
@@ -64,6 +71,13 @@ module RedAmber
64
71
  #
65
72
  define_unary_element_wise :acos
66
73
 
74
+ # Compute the inverse cosine of self element-wise.
75
+ #
76
+ # This function is a overflow-checking variant of #acos.
77
+ # @return (see #acos)
78
+ #
79
+ define_unary_element_wise :acos_checked
80
+
67
81
  # Compute the inverse sine of self element-wise.
68
82
  #
69
83
  # NaN is returned for invalid input values.
@@ -72,6 +86,13 @@ module RedAmber
72
86
  #
73
87
  define_unary_element_wise :asin
74
88
 
89
+ # Compute the inverse sine of self element-wise.
90
+ #
91
+ # This function is a overflow-checking variant of #asin.
92
+ # @return (see #asin)
93
+ #
94
+ define_unary_element_wise :asin_checked
95
+
75
96
  # Return the indices that would sort self.
76
97
  #
77
98
  # Computes indices Vector that define a stable sort of self.
@@ -129,8 +150,16 @@ module RedAmber
129
150
  #
130
151
  define_unary_element_wise :cos
131
152
 
153
+ # Compute the cosine of self element-wise.
154
+ #
155
+ # This function is a overflow-checking variant of #cos.
156
+ # @return (see #cos)
157
+ #
158
+ define_unary_element_wise :cos_checked
159
+
132
160
  # Compute cumulative sum over the numeric Vector.
133
161
  #
162
+ # This function is a overflow-checking variant of #cumsum.
134
163
  # @note Self must be numeric.
135
164
  # @note Return error for integer overflow.
136
165
  # @return [Vector]
@@ -261,6 +290,13 @@ module RedAmber
261
290
  #
262
291
  define_unary_element_wise :ln
263
292
 
293
+ # Compute natural logarithm.
294
+ #
295
+ # This function is a overflow-checking variant of #ln.
296
+ # @return (see #ln)
297
+ #
298
+ define_unary_element_wise :ln_checked
299
+
264
300
  # Compute base 10 logarithm.
265
301
  #
266
302
  # Non-positive values return -inf or NaN. Nil values return nil.
@@ -269,6 +305,13 @@ module RedAmber
269
305
  #
270
306
  define_unary_element_wise :log10
271
307
 
308
+ # Compute base 10 logarithm.
309
+ #
310
+ # This function is a overflow-checking variant of #log10.
311
+ # @return (see #log10)
312
+ #
313
+ define_unary_element_wise :log10_checked
314
+
272
315
  # Compute natural log of (1+x).
273
316
  #
274
317
  # Non-positive values return -inf or NaN. Nil values return nil.
@@ -278,6 +321,13 @@ module RedAmber
278
321
  #
279
322
  define_unary_element_wise :log1p
280
323
 
324
+ # Compute natural log of (1+x).
325
+ #
326
+ # This function is a overflow-checking variant of #log1p.
327
+ # @return (see #log1p)
328
+ #
329
+ define_unary_element_wise :log1p_checked
330
+
281
331
  # Compute base 2 logarithm.
282
332
  #
283
333
  # Non-positive values return -inf or NaN. Nil values return nil.
@@ -286,6 +336,13 @@ module RedAmber
286
336
  #
287
337
  define_unary_element_wise :log2
288
338
 
339
+ # Compute base 2 logarithm.
340
+ #
341
+ # This function is a overflow-checking variant of #log2.
342
+ # @return (see #log2)
343
+ #
344
+ define_unary_element_wise :log2_checked
345
+
289
346
  # Round to a given precision.
290
347
  #
291
348
  # Options are used to control the number of digits and rounding mode.
@@ -390,6 +447,28 @@ module RedAmber
390
447
  #
391
448
  define_unary_element_wise :sin
392
449
 
450
+ # Compute the sine of self element-wise.
451
+ #
452
+ # This function is a overflow-checking variant of #sin.
453
+ # @return (see #sin)
454
+ #
455
+ define_unary_element_wise :sin_checked
456
+
457
+ # Compute square root of self.
458
+ #
459
+ # NaN is returned for invalid input values.
460
+ # @return [Vector]
461
+ # sqrt of each element of self.
462
+ #
463
+ define_unary_element_wise :sqrt
464
+
465
+ # Compute square root of self.
466
+ #
467
+ # This function is a overflow-checking variant of #sqrt.
468
+ # @return (see #sqrt)
469
+ #
470
+ define_unary_element_wise :sqrt_checked
471
+
393
472
  # Compute the tangent of self element-wise.
394
473
  #
395
474
  # NaN is returned for invalid input values.
@@ -398,6 +477,13 @@ module RedAmber
398
477
  #
399
478
  define_unary_element_wise :tan
400
479
 
480
+ # Compute the tangent of self element-wise.
481
+ #
482
+ # This function is a overflow-checking variant of #tan.
483
+ # @return (see #tan)
484
+ #
485
+ define_unary_element_wise :tan_checked
486
+
401
487
  # Compute the integral part
402
488
  #
403
489
  # Compute the nearest integer not greater in magnitude than each element.
@@ -432,5 +518,12 @@ module RedAmber
432
518
  #
433
519
  define_unary_element_wise :negate
434
520
  alias_method :'-@', :negate # rubocop:disable Lint/SymbolConversion
521
+
522
+ # Negate the argument element-wise
523
+ #
524
+ # This function is a overflow-checking variant of #negate.
525
+ # @return (see #negate)
526
+ #
527
+ define_unary_element_wise :negate_checked
435
528
  end
436
529
  end
@@ -2,5 +2,5 @@
2
2
 
3
3
  module RedAmber
4
4
  # Library version
5
- VERSION = '0.4.0'
5
+ VERSION = '0.4.2'
6
6
  end
data/red_amber.gemspec CHANGED
@@ -11,13 +11,13 @@ Gem::Specification.new do |spec|
11
11
  spec.summary = 'Simple dataframe library for Ruby'
12
12
  spec.description = 'RedAmber is a simple dataframe library ' \
13
13
  'inspired by Rover-df and powered by Red Arrow.'
14
- spec.homepage = 'https://github.com/heronshoes/red_amber'
14
+ spec.homepage = 'https://github.com/red-data-tools/red_amber'
15
15
  spec.license = 'MIT'
16
16
  spec.required_ruby_version = '>= 3.0'
17
17
 
18
18
  spec.metadata['homepage_uri'] = spec.homepage
19
- spec.metadata['source_code_uri'] = 'https://github.com/heronshoes/red_amber'
20
- spec.metadata['changelog_uri'] = 'https://github.com/heronshoes/red_amber/blob/main/CHANGELOG.md'
19
+ spec.metadata['source_code_uri'] = 'https://github.com/red-data-tools/red_amber'
20
+ spec.metadata['changelog_uri'] = 'https://github.com/red-data-tools/red_amber/blob/main/CHANGELOG.md'
21
21
 
22
22
  # Specify which files should be added to the gem when it is released.
23
23
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: red_amber
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hirokazu SUZUKI (heronshoes)
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-02-25 00:00:00.000000000 Z
11
+ date: 2023-04-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: red-arrow
@@ -87,6 +87,15 @@ files:
87
87
  - doc/tdr.md
88
88
  - doc/tdr_ja.md
89
89
  - doc/yard-templates/default/fulldoc/html/css/common.css
90
+ - docker/.env
91
+ - docker/Dockerfile
92
+ - docker/Gemfile
93
+ - docker/Gemfile.lock
94
+ - docker/docker-compose.yml
95
+ - docker/example
96
+ - docker/notebook/examples_of_red_amber.ipynb
97
+ - docker/notebook/red-amber.ipynb
98
+ - docker/readme.md
90
99
  - lib/red-amber.rb
91
100
  - lib/red_amber.rb
92
101
  - lib/red_amber/data_frame.rb
@@ -110,13 +119,13 @@ files:
110
119
  - lib/red_amber/version.rb
111
120
  - red_amber.gemspec
112
121
  - sig/red_amber.rbs
113
- homepage: https://github.com/heronshoes/red_amber
122
+ homepage: https://github.com/red-data-tools/red_amber
114
123
  licenses:
115
124
  - MIT
116
125
  metadata:
117
- homepage_uri: https://github.com/heronshoes/red_amber
118
- source_code_uri: https://github.com/heronshoes/red_amber
119
- changelog_uri: https://github.com/heronshoes/red_amber/blob/main/CHANGELOG.md
126
+ homepage_uri: https://github.com/red-data-tools/red_amber
127
+ source_code_uri: https://github.com/red-data-tools/red_amber
128
+ changelog_uri: https://github.com/red-data-tools/red_amber/blob/main/CHANGELOG.md
120
129
  rubygems_mfa_required: 'true'
121
130
  post_install_message:
122
131
  rdoc_options: []
@@ -133,7 +142,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
133
142
  - !ruby/object:Gem::Version
134
143
  version: '0'
135
144
  requirements: []
136
- rubygems_version: 3.2.33
145
+ rubygems_version: 3.4.10
137
146
  signing_key:
138
147
  specification_version: 4
139
148
  summary: Simple dataframe library for Ruby