red_amber 0.4.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.
@@ -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,52 @@ 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
239
+ alias_method :div, :divide
225
240
  alias_method :'/', :divide
226
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
+
227
282
  # Multiply the arguments element-wise.
228
283
  #
229
284
  # Results will wrap around on integer overflow.
@@ -234,8 +289,16 @@ module RedAmber
234
289
  # multiplication of self and other.
235
290
  #
236
291
  define_binary_element_wise :multiply
292
+ alias_method :mul, :multiply
237
293
  alias_method :'*', :multiply
238
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
+
239
302
  # Raise arguments to power element-wise.
240
303
  #
241
304
  # Integer to negative integer power returns an error.
@@ -248,8 +311,42 @@ module RedAmber
248
311
  # power operation of self and other.
249
312
  #
250
313
  define_binary_element_wise :power
314
+ alias_method :pow, :power
251
315
  alias_method :'**', :power
252
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
+
253
350
  # Subtract the arguments element-wise.
254
351
  #
255
352
  # Results will wrap around on integer overflow.
@@ -260,8 +357,16 @@ module RedAmber
260
357
  # subtraction of self and other.
261
358
  #
262
359
  define_binary_element_wise :subtract
360
+ alias_method :sub, :subtract
263
361
  alias_method :'-', :subtract
264
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
+
265
370
  # Left shift of self by other.
266
371
  #
267
372
  # The shift operates as if on the two's complement representation of the number.
@@ -278,6 +383,13 @@ module RedAmber
278
383
  define_binary_element_wise :shift_left
279
384
  alias_method :'<<', :shift_left
280
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
+
281
393
  # Right shift of self by other.
282
394
  #
283
395
  # This is equivalent to dividing `x` by 2 to the power `y`.
@@ -292,6 +404,13 @@ module RedAmber
292
404
  define_binary_element_wise :shift_right
293
405
  alias_method :'>>', :shift_right
294
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
+
295
414
  # Logical 'xor' boolean values
296
415
  #
297
416
  # 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.1'
6
6
  end
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.1
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-03-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: red-arrow
@@ -133,7 +133,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
133
133
  - !ruby/object:Gem::Version
134
134
  version: '0'
135
135
  requirements: []
136
- rubygems_version: 3.2.33
136
+ rubygems_version: 3.4.1
137
137
  signing_key:
138
138
  specification_version: 4
139
139
  summary: Simple dataframe library for Ruby