Class: RedAmber::Vector

Inherits:
Object
  • Object
show all
Includes:
Enumerable, ArrowFunction, Helper, VectorSelectable, VectorStringFunction, VectorUpdatable
Defined in:
lib/red_amber/vector.rb,
lib/red_amber/vector_aggregation.rb,
lib/red_amber/vector_unary_element_wise.rb,
lib/red_amber/vector_binary_element_wise.rb

Overview

Representing a series of data.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from VectorStringFunction

#count_substring, #end_with, #find_substring, #match_like, #match_substring, #start_with

Methods included from VectorSelectable

#[], #drop_nil, #filter, #first, #index, #is_in, #last, #rank, #sample, #sort, #take

Methods included from VectorUpdatable

#cast, #concatenate, #fill_nil, #if_else, #list_flatten, #list_separate, #list_sizes, #merge, #primitive_invert, #replace, #shift, #split, #split_to_columns, #split_to_rows

Methods included from ArrowFunction

arrow_doc, find

Constructor Details

#initialize(*array) ⇒ Vector

Note:

default is headless Vector and ‘@key == nil’

Create a Vector.

Parameters:



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/red_amber/vector.rb', line 71

def initialize(*array)
  @data =
    case array
    in [Vector => v]
      v.data
    in [Range => r]
      Arrow::Array.new(Array(r))
    in [Arrow::Array | Arrow::ChunkedArray]
      array[0]
    in [arrow_array_like] if arrow_array_like.respond_to?(:to_arrow_array)
      arrow_array_like.to_arrow_array
    else
      Arrow::Array.new(array.flatten)
    end
end

Instance Attribute Details

#dataArrow::Array (readonly) Also known as: to_arrow_array

Entity of Vector.

Returns:

  • (Arrow::Array)


21
22
23
# File 'lib/red_amber/vector.rb', line 21

def data
  @data
end

#keySymbol

Associated key name when self is in a DataFrame.

Default Vector is ‘head-less’ (key-less).

Returns:

  • (Symbol)


29
30
31
# File 'lib/red_amber/vector.rb', line 29

def key
  @key
end

Class Method Details

.[]Vector

Create a Vector (calling ‘.new`).

Examples:

Create an empty Vector.

Vector[]
# =>
#<RedAmber::Vector(:string, size=0):0x000000000000e2cc>
[]

Parameters:

Returns:

  • (Vector)

    created Vector.

Since:

  • 0.5.0



44
45
46
# File 'lib/red_amber/vector.rb', line 44

def [](...)
  new(...)
end

.atan2(y, x) ⇒ Vector

Compute the inverse tangent of y/x.

Binary element-wise function

Returns a Vector.

The return value is in the range [-pi, pi].

Parameters:

  • y (Vector, array-like)

    numeric array-like.

  • x (Vector, array-like)

    numeric array-like.

Returns:

  • (Vector)

    inverse tangent of y/x.



21
22
23
24
25
26
27
# File 'lib/red_amber/vector_binary_element_wise.rb', line 21

def atan2(y, x) # rubocop:disable Naming/MethodParameterName
  y = y.data if y.is_a? Vector
  x = x.data if x.is_a? Vector

  datum = Arrow::Function.find(:atan2).execute([y, x])
  Vector.create(datum.value)
end

.create(arrow_array) ⇒ Vector

Note:

This method doesn’t check argment type.

Quicker constructor of Vector.

Parameters:

  • arrow_array (Arrow::Array)

    Arrow::Array object to have in the Vector.

Returns:

  • (Vector)

    created Vector.



56
57
58
59
60
# File 'lib/red_amber/vector.rb', line 56

def create(arrow_array)
  instance = allocate
  instance.instance_variable_set(:@data, arrow_array)
  instance
end

Instance Method Details

#absVector

Calculate the absolute value of self element-wise.

Results will wrap around on integer overflow.

Unary element-wise function

Returns a Vector.

Returns:

  • (Vector)

    abs of each element of self.



57
# File 'lib/red_amber/vector_unary_element_wise.rb', line 57

define_unary_element_wise :abs

#abs_checkedVector

Calculate the absolute value of self element-wise.

This function is a overflow-checking variant of #abs.

Unary element-wise function

Returns a Vector.

Returns:

  • (Vector)

    abs of each element of self.



64
# File 'lib/red_amber/vector_unary_element_wise.rb', line 64

define_unary_element_wise :abs_checked

#acosVector

Compute the inverse cosine of self element-wise.

NaN is returned for invalid input values.

Unary element-wise function

Returns a Vector.

Returns:

  • (Vector)

    acos of each element of self.



72
# File 'lib/red_amber/vector_unary_element_wise.rb', line 72

define_unary_element_wise :acos

#acos_checkedVector

Compute the inverse cosine of self element-wise.

This function is a overflow-checking variant of #acos.

Unary element-wise function

Returns a Vector.

Returns:

  • (Vector)

    acos of each element of self.



79
# File 'lib/red_amber/vector_unary_element_wise.rb', line 79

define_unary_element_wise :acos_checked

#add(other) ⇒ Vector Also known as: +

Add the arguments element-wise.

Results will wrap around on integer overflow.

Binary element-wise function

Returns a Vector.

Parameters:

  • other (Vector, Numeric)

    other numeric Vector or numeric scalar.

Returns:

  • (Vector)

    adddition of self and other.



218
# File 'lib/red_amber/vector_binary_element_wise.rb', line 218

define_binary_element_wise :add

#add_checkedVector

Add the arguments element-wise.

This function is a overflow-checking variant of #add.

Binary element-wise function

Returns a Vector.

Returns:

  • (Vector)

    adddition of self and other.



226
# File 'lib/red_amber/vector_binary_element_wise.rb', line 226

define_binary_element_wise :add_checked

#all(skip_nulls: true, min_count: 1) ⇒ true, false Also known as: all?

Test whether all elements in self are evaluated to true.

Unary aggregation function

Returns a scalar.

Examples:

Default.

Vector.new(true, true, nil).all # => true

Skip nils.

Vector.new(true, true, nil).all(skip_nulls: false) # => false

Parameters:

  • skip_nulls (true, false) (defaults to: true)

    If true, nil values are ignored. Otherwise, if any value is nil, emit nil.

  • min_count (Integer) (defaults to: 1)

    if less than this many non-nil values are observed, emit nil. If skip_nulls is false, this option is not respected.

Returns:

  • (true, false)

    ‘all` result of self.



61
# File 'lib/red_amber/vector_aggregation.rb', line 61

define_unary_aggregation :all

#and_kleene(other) ⇒ Vector Also known as: &

Logical ‘and’ boolean values with Kleene logic.

This function behaves as follows with nils:

  • true and nil = nil

  • nil and true = nil

  • false and nil = false

  • nil and false = false

  • nil and nil = nil

In other words, in this context a nil value really means “unknown”, and an unknown value ‘and’ false is always false. For a different nil behavior, see function #and_org.

Binary element-wise function

Returns a Vector.

Parameters:

  • other (Vector, array-like)

    boolean array-like.

Returns:

  • (Vector)

    and_kleene of self and other.



94
# File 'lib/red_amber/vector_binary_element_wise.rb', line 94

define_binary_element_wise :and_kleene

#and_not(other) ⇒ Vector

Logical ‘and not’ boolean values.

When a nil is encountered in either input, a nil is output. For a different nil behavior, see function #and_not_kleene.

Binary element-wise function

Returns a Vector.

Parameters:

  • other (Vector, array-like)

    boolean array-like.

Returns:

  • (Vector)

    and not of self and other.



107
# File 'lib/red_amber/vector_binary_element_wise.rb', line 107

define_binary_element_wise :and_not

#and_not_kleene(other) ⇒ Vector

Logical ‘and not’ boolean values with Kleene logic.

This function behaves as follows with nils:

  • true and not nil = nil

  • nil and not false = nil

  • false and not nil = false

  • nil and not true = false

  • nil and not nil = nil

In other words, in this context a nil value really means “unknown”, and an unknown value ‘and not’ true is always false, as is false ‘and not’ an unknown value. For a different nil behavior, see function #and_not.

Binary element-wise function

Returns a Vector.

Parameters:

  • other (Vector, array-like)

    boolean array-like.

Returns:

  • (Vector)

    and not kleene of self and other.



119
# File 'lib/red_amber/vector_binary_element_wise.rb', line 119

define_binary_element_wise :and_not_kleene

#and_org(other) ⇒ Vector

Logical ‘and’ boolean values.

When a nil is encountered in either input, a nil is output. For a different nil behavior, see function #and_kleene.

Binary element-wise function

Returns a Vector.

Parameters:

  • other (Vector, array-like)

    boolean array-like.

Returns:

  • (Vector)

    evacuated ‘and` of self and other.



131
# File 'lib/red_amber/vector_binary_element_wise.rb', line 131

define_binary_element_wise_logical(:and_org, :and)

#any(skip_nulls: true, min_count: 1) ⇒ true, false Also known as: any?

Test whether any elements in self are evaluated to true.

Unary aggregation function

Returns a scalar.

Examples:

Default.

Vector.new(true, false, nil).any # => true

Parameters:

  • skip_nulls (true, false) (defaults to: true)

    If true, nil values are ignored. Otherwise, if any value is nil, emit nil.

  • min_count (Integer) (defaults to: 1)

    if less than this many non-nil values are observed, emit nil. If skip_nulls is false, this option is not respected.

Returns:

  • (true, false)

    ‘any` result of self.



73
# File 'lib/red_amber/vector_aggregation.rb', line 73

define_unary_aggregation :any

#approximate_median(skip_nulls: true, min_count: 1) ⇒ Float Also known as: median

Approximate median of a numeric Vector with T-Digest algorithm.

Unary aggregation function

Returns a scalar.

Parameters:

  • skip_nulls (true, false) (defaults to: true)

    If true, nil values are ignored. Otherwise, if any value is nil, emit nil.

  • min_count (Integer) (defaults to: 1)

    if less than this many non-nil values are observed, emit nil. If skip_nulls is false, this option is not respected.

Returns:

  • (Float)

    median of self. A nil is returned if there is no valid data point.



84
# File 'lib/red_amber/vector_aggregation.rb', line 84

define_unary_aggregation :approximate_median

#array_sort_indices(order: :ascending) ⇒ Vector Also known as: sort_indexes, sort_indices, sort_index

Return the indices that would sort self.

Computes indices Vector that define a stable sort of self. By default, nils are considered greater than any other value and are therefore sorted at the end of the Vector. For floating-point types, NaNs are considered greater than any other non-nil value, but smaller than nil.

Unary element-wise function

Returns a Vector.

Parameters:

  • order (:ascending, :descending) (defaults to: :ascending)

    ascending: Arrange values in increasing order. descending: Arrange values in decreasing order.

Returns:

  • (Vector)

    sort indices of self.



108
# File 'lib/red_amber/vector_unary_element_wise.rb', line 108

define_unary_element_wise :array_sort_indices

#asinVector

Compute the inverse sine of self element-wise.

NaN is returned for invalid input values.

Unary element-wise function

Returns a Vector.

Returns:

  • (Vector)

    asin of each element of self.



87
# File 'lib/red_amber/vector_unary_element_wise.rb', line 87

define_unary_element_wise :asin

#asin_checkedVector

Compute the inverse sine of self element-wise.

This function is a overflow-checking variant of #asin.

Unary element-wise function

Returns a Vector.

Returns:

  • (Vector)

    asin of each element of self.



94
# File 'lib/red_amber/vector_unary_element_wise.rb', line 94

define_unary_element_wise :asin_checked

#atanVector

Compute the inverse tangent of self element-wise.

the return value is in the range [-pi/2, pi/2]. For a full return range [-pi, pi], see atan2 .

Unary element-wise function

Returns a Vector.

Returns:

  • (Vector)

    atan of each element of self.



120
# File 'lib/red_amber/vector_unary_element_wise.rb', line 120

define_unary_element_wise :atan

#bit_wise_and(other) ⇒ Vector

Bit-wise AND of self and other by element-wise.

Nil values return nil.

Binary element-wise function

Returns a Vector.

Parameters:

  • other (Vector, array-like)

    integer array-like.

Returns:

  • (Vector)

    bit wise and of self and other.



142
# File 'lib/red_amber/vector_binary_element_wise.rb', line 142

define_binary_element_wise :bit_wise_and

#bit_wise_notVector

Bit-wise negate by element-wise.

nil values reeturn nil.

Unary element-wise function

Returns a Vector.

Returns:

  • (Vector)

    bit wise not of each element of self.



128
# File 'lib/red_amber/vector_unary_element_wise.rb', line 128

define_unary_element_wise :bit_wise_not

#bit_wise_or(other) ⇒ Vector

Bit-wise OR of self and other by element-wise.

Nil values return nil.

Binary element-wise function

Returns a Vector.

Parameters:

  • other (Vector, array-like)

    integer array-like.

Returns:

  • (Vector)

    bit wise or of self and other.



153
# File 'lib/red_amber/vector_binary_element_wise.rb', line 153

define_binary_element_wise :bit_wise_or

#bit_wise_xor(other) ⇒ Vector

Bit-wise XOR of self and other by element-wise.

Nil values return nil.

Binary element-wise function

Returns a Vector.

Parameters:

  • other (Vector, array-like)

    integer array-like.

Returns:

  • (Vector)

    bit wise xor of self and other.



164
# File 'lib/red_amber/vector_binary_element_wise.rb', line 164

define_binary_element_wise :bit_wise_xor

#boolean?true, false

Test if self is a boolean Vector.

Returns:

  • (true, false)

    test result.



257
258
259
# File 'lib/red_amber/vector.rb', line 257

def boolean?
  @data.boolean?
end

#ceilVector

Round up to the nearest integer.

Compute the smallest integer value not less in magnitude than each element.

Unary element-wise function

Returns a Vector.

Examples:

double = Vector.new([15.15, 2.5, 3.5, -4.5, -5.5])
double.ceil

# =>
#<RedAmber::Vector(:double, size=5):0x000000000000cd00>
[16.0, 3.0, 4.0, -4.0, -5.0]

Returns:

  • (Vector)

    ceil of each element of self.



143
# File 'lib/red_amber/vector_unary_element_wise.rb', line 143

define_unary_element_wise :ceil

#chunked?true, false

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Tests wheather self is chunked or not.

Returns:

  • (true, false)

    returns true if #data is chunked.



383
384
385
# File 'lib/red_amber/vector.rb', line 383

def chunked?
  @data.is_a? Arrow::ChunkedArray
end

#coerce(other) ⇒ Object

Enable to compute with coercion mechanism.

Examples:

vector = Vector.new(1,2,3)

# =>
#<RedAmber::Vector(:uint8, size=3):0x00000000000decc4>
[1, 2, 3]

# Vector's `#*` method
vector * -1

# =>
#<RedAmber::Vector(:int16, size=3):0x00000000000e3698>
[-1, -2, -3]

# coerced calculation
-1 * vector

# =>
#<RedAmber::Vector(:int16, size=3):0x00000000000ea4ac>
[-1, -2, -3]

# `@-` operator
-vector

# =>
#<RedAmber::Vector(:uint8, size=3):0x00000000000ee7b4>
[255, 254, 253]


487
488
489
# File 'lib/red_amber/vector.rb', line 487

def coerce(other)
  [Vector.new(Array(other) * size), self]
end

#cosVector

Compute the cosine of self element-wise.

NaN is returned for invalid input values.

Unary element-wise function

Returns a Vector.

Returns:

  • (Vector)

    cos of each element of self.



151
# File 'lib/red_amber/vector_unary_element_wise.rb', line 151

define_unary_element_wise :cos

#cos_checkedVector

Compute the cosine of self element-wise.

This function is a overflow-checking variant of #cos.

Unary element-wise function

Returns a Vector.

Returns:

  • (Vector)

    cos of each element of self.



158
# File 'lib/red_amber/vector_unary_element_wise.rb', line 158

define_unary_element_wise :cos_checked

#count(mode: :non_null) ⇒ Integer

Count the number of nil / non-nil values.

Unary aggregation function

Returns a scalar.

Examples:

Count only non-nil (default)

Vector.new(1.0, -2.0, Float::NAN, nil).count # => 3

Count nil only.

Vector.new(1.0, -2.0, Float::NAN, nil).count(mode: :only_null) # => 1

Count both non-nil and nil.

Vector.new(1.0, -2.0, Float::NAN, nil).count(mode: :all) # => 4

Parameters:

  • mode (:only_valid, :only_null, :all) (defaults to: :non_null)

    control count aggregate kernel behavior.

    • only_valid: count only non-nil values.

    • only_null: count only nil.

    • all: count both.

Returns:

  • (Integer)

    count of self.



101
# File 'lib/red_amber/vector_aggregation.rb', line 101

define_unary_aggregation :count

#count_distinct(mode: :only_valid) ⇒ Integer Also known as: count_uniq

Count the number of unique values.

Unary aggregation function

Returns a scalar.

Examples:

vector = Vector.new(1, 1.0, nil, nil, Float::NAN, Float::NAN)
vector

# =>
#<RedAmber::Vector(:double, size=6):0x000000000000d390>
[1.0, 1.0, nil, nil, NaN, NaN]

# Float::NANs are counted as 1.
vector.count_uniq # => 2

# nils are counted as 1.
vector.count_uniq(mode: :only_null) # => 1

vector.count_uniq(mode: :all) # => 3

Parameters:

  • mode (:only_valid, :only_null, :all) (defaults to: :only_valid)

    control count aggregate kernel behavior.

    • only_valid: count only non-nil values.

    • only_null: count only nil.

    • all: count both.

Returns:

  • (Integer)

    unique count of self.



125
# File 'lib/red_amber/vector_aggregation.rb', line 125

define_unary_aggregation :count_distinct

#cumsumVector

Note:

Self must be numeric.

Note:

Try to cast to Int64 if integer overflow occured.

Compute cumulative sum over the numeric Vector.

Returns:

  • (Vector)

    cumulative sum of self.



177
178
179
180
181
# File 'lib/red_amber/vector_unary_element_wise.rb', line 177

def cumsum
  cumulative_sum_checked
rescue Arrow::Error::Invalid
  Vector.create(Arrow::Int64Array.new(data)).cumulative_sum_checked
end

#cumulative_sum_checkedVector

Note:

Self must be numeric.

Note:

Return error for integer overflow.

Compute cumulative sum over the numeric Vector.

This function is a overflow-checking variant of #cumsum.

Unary element-wise function

Returns a Vector.

Returns:

  • (Vector)

    cumulative sum of self.



168
# File 'lib/red_amber/vector_unary_element_wise.rb', line 168

define_unary_element_wise :cumulative_sum_checked

#dictionary?true, false

Test if self is a dictionary Vector.

Returns:

  • (true, false)

    test result.



302
303
304
# File 'lib/red_amber/vector.rb', line 302

def dictionary?
  @data.dictionary?
end

#divide(divisor) ⇒ Vector Also known as: /

Divide the arguments element-wise.

Integer division by zero returns an error. However, integer overflow wraps around, and floating-point division by zero returns an infinite.

Binary element-wise function

Returns a Vector.

Parameters:

  • divisor (Vector, Numeric)

    numeric vector or numeric scalar as divisor.

Returns:

  • (Vector)

    division of self by other.



238
# File 'lib/red_amber/vector_binary_element_wise.rb', line 238

define_binary_element_wise :divide

#divide_checkedVector

Divide the arguments element-wise.

This function is a overflow-checking variant of #divide.

Binary element-wise function

Returns a Vector.

Returns:

  • (Vector)

    division of self by other.



246
# File 'lib/red_amber/vector_binary_element_wise.rb', line 246

define_binary_element_wise :divide_checked

#eachEnumerator #each {|element| ... } ⇒ self

Iterates over Vector elements or returns a Enumerator.

Overloads:

  • #eachEnumerator

    Returns a new Enumerator if no block given.

    Returns:

    • (Enumerator)

      Enumerator of each elements.

  • #each {|element| ... } ⇒ self

    When a block given, passes each element in self to the block.

    Yield Parameters:

    • element (Object)

      passes element by a block parameter.

    Yield Returns:

    • (Object)

      evaluated result value from the block.

    Returns:

    • (self)

      returns self.



342
343
344
345
346
347
348
349
# File 'lib/red_amber/vector.rb', line 342

def each
  return enum_for(:each) unless block_given?

  size.times do |i|
    yield data[i]
  end
  self
end

#empty?true, false

Test wheather self is empty.

Returns:

  • (true, false)

    true if self is empty.



230
231
232
# File 'lib/red_amber/vector.rb', line 230

def empty?
  size.zero?
end

#equal(other) ⇒ Vector Also known as: ==, eq

Compare values for equality (self == other)

A nil on either side emits a nil comparison result.

Binary element-wise function

Returns a Vector.

Parameters:

  • other (Vector)

    other vector or scalar.

Returns:

  • (Vector)

    eq of self and other by a boolean Vector.



464
# File 'lib/red_amber/vector_binary_element_wise.rb', line 464

define_binary_element_wise :equal

#fdiv(divisor) ⇒ Vector

Returns element-wise quotient by double Vector.

Parameters:

  • divisor (Vector, numeric)

    divisor numeric Vector or numeric scalar.

Returns:

  • (Vector)

    quotient of dividing self by divisor.



330
331
332
333
334
# File 'lib/red_amber/vector_binary_element_wise.rb', line 330

def fdiv(divisor)
  divisor = divisor.data if divisor.is_a?(Vector)
  datum = find(:divide).execute([Arrow::DoubleArray.new(data), divisor])
  Vector.create(datum.value)
end

#fdiv_checked(divisor) ⇒ Object

Returns element-wise quotient by double Vector.

This function is a overflow-checking variant of #quotient.



341
342
343
344
345
# File 'lib/red_amber/vector_binary_element_wise.rb', line 341

def fdiv_checked(divisor)
  divisor = divisor.data if divisor.is_a?(Vector)
  datum = find(:divide_checked).execute([Arrow::DoubleArray.new(data), divisor])
  Vector.create(datum.value)
end

#fill_null_backwardVector Also known as: fill_nil_backward

Note:

Use ‘fill_nil(value)` to replace nil by a value.

Carry non-nil values backward to fill nil slots.

Propagate next valid value backward to previous nil values. Or nothing if all next values are nil.

Unary element-wise function

Returns a Vector.

Examples:

integer = Vector.new([0, 1, nil, 3, nil])
integer.fill_nil_backward

# =>
#<RedAmber::Vector(:uint8, size=5):0x000000000000f974>
[0, 1, 3, 3, nil]

Returns:

  • (Vector)

    a Vector which filled nil backward.

See Also:



199
# File 'lib/red_amber/vector_unary_element_wise.rb', line 199

define_unary_element_wise :fill_null_backward

#fill_null_forwardVector Also known as: fill_nil_forward

Note:

Use ‘fill_nil(value)` to replace nil by a value.

Carry non-nil values forward to fill nil slots.

Propagate last valid value backward to next nil values. Or nothing if all previous values are nil.

Unary element-wise function

Returns a Vector.

Examples:

integer = Vector.new([0, 1, nil, 3, nil])
integer.fill_nil_forward

# =>
#<RedAmber::Vector(:uint8, size=5):0x000000000000f960>
[0, 1, 1, 3, 3]

Returns:

  • (Vector)

    a Vector which filled nil forward.

See Also:



218
# File 'lib/red_amber/vector_unary_element_wise.rb', line 218

define_unary_element_wise :fill_null_forward

#float?true, false

Test if self is a float Vector.

Returns:

  • (true, false)

    test result.



275
276
277
# File 'lib/red_amber/vector.rb', line 275

def float?
  @data.float?
end

#floorVector

Round down to the nearest integer.

Compute the largest integer value not greater in magnitude than each element.

Unary element-wise function

Returns a Vector.

Examples:

double = Vector.new([15.15, 2.5, 3.5, -4.5, -5.5])
double.floor

# =>
#<RedAmber::Vector(:double, size=5):0x000000000000cd14>
[15.0, 2.0, 3.0, -5.0, -6.0]

Returns:

  • (Vector)

    floor of each element of self.



234
# File 'lib/red_amber/vector_unary_element_wise.rb', line 234

define_unary_element_wise :floor

#greater(other) ⇒ Vector Also known as: >, gt

Compare values for ordered inequality (self > other).

A nil on either side emits a nil comparison result.

Binary element-wise function

Returns a Vector.

Parameters:

  • other (Vector)

    other vector or scalar.

Returns:

  • (Vector)

    gt of self and other by a boolean Vector.



477
# File 'lib/red_amber/vector_binary_element_wise.rb', line 477

define_binary_element_wise :greater

#greater_equal(other) ⇒ Vector Also known as: >=, ge

Compare values for ordered inequality (self >= other).

A nil on either side emits a nil comparison result.

Binary element-wise function

Returns a Vector.

Parameters:

  • other (Vector)

    other vector or scalar.

Returns:

  • (Vector)

    ge of self and other by a boolean Vector.



490
# File 'lib/red_amber/vector_binary_element_wise.rb', line 490

define_binary_element_wise :greater_equal

#has_nil?true, false

Return true if self has any nil.

Returns:

  • (true, false)

    true or false.



453
454
455
# File 'lib/red_amber/vector.rb', line 453

def has_nil?
  is_nil.any
end

#indicesArray Also known as: indexes, indeces

Indeces from 0 to size-1 by Array.

Returns:

  • (Array)

    indices.



206
207
208
# File 'lib/red_amber/vector.rb', line 206

def indices
  (0...size).to_a
end

#inspect(limit: 80) ⇒ String

String representation of self.

According to ‘ENV [“RED_AMBER_OUTPUT_MODE”].upcase`,

  • If it is ‘MINIMUM’, returns class and size.

  • If it is otherwise, returns class, size and preview. Default value of the ENV is ‘Table’.

Examples:

Default (ENV [‘RED_AMBER_OUTPUT_MODE’] == ‘Table’)

puts vector.inspect

# =>
#<RedAmber::Vector(:uint8, size=3):0x00000000000037f0>
[1, 2, 3]

In case of ENV [‘RED_AMBER_OUTPUT_MODE’] == ‘Minimum’

puts vector.inspect

# =>
RedAmber::Vector(:uint8, size=3)

Parameters:

  • limit (Integer) (defaults to: 80)

    max width of the result.

Returns:

  • (String)

    show information of self as a String.



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/red_amber/vector.rb', line 166

def inspect(limit: 80)
  if ENV.fetch('RED_AMBER_OUTPUT_MODE', 'Table').casecmp('MINIMUM').zero?
    # Better performance than `.upcase == 'MINIMUM'`
    "#{self.class}(:#{type}, size=#{size})"
  else
    sio = StringIO.new << '['
    each.with_index do |e, i|
      next_str = "#{sio.size > 1 ? ', ' : ''}#{e.inspect}"
      if (sio.size + next_str.size) < limit
        sio << next_str
      else
        sio << ', ... ' if i < size
        break
      end
    end
    sio << ']'

    chunked = chunked? ? ', chunked' : ''
    format "#<#{self.class}(:#{type}, size=#{size}#{chunked}):0x%016x>\n%s\n",
           object_id, sio.string
  end
end

#integer?true, false

Test if self is a integer Vector.

Returns:

  • (true, false)

    test result.



284
285
286
# File 'lib/red_amber/vector.rb', line 284

def integer?
  @data.integer?
end

#invertVector Also known as: !, not

Invert boolean values

Unary element-wise function

Returns a Vector.

Returns:

  • (Vector)

    not of each element of self.



513
# File 'lib/red_amber/vector_unary_element_wise.rb', line 513

define_unary_element_wise :invert

#is_finiteVector

Return true if value is finite.

For each input value, emit true if the value is finite. (i.e. neither NaN, inf, nor -inf).

Unary element-wise function

Returns a Vector.

Returns:

  • (Vector)

    boolean Vector wheather each element is finite.



243
# File 'lib/red_amber/vector_unary_element_wise.rb', line 243

define_unary_element_wise :is_finite

#is_infVector

Return true if value is infinity.

For each input value, emit true if the value is infinite (inf or -inf).

Unary element-wise function

Returns a Vector.

Returns:

  • (Vector)

    boolean Vector wheather each element is inf.



251
# File 'lib/red_amber/vector_unary_element_wise.rb', line 251

define_unary_element_wise :is_inf

#is_naVector

return true if value is nil or NaN.

For each input value, emit true if the value is nil or NaN.

Returns:

  • (Vector)

    boolean Vector wheather each element is na.



259
260
261
# File 'lib/red_amber/vector_unary_element_wise.rb', line 259

def is_na # rubocop:disable Naming/PredicateName
  numeric? ? (is_nil | is_nan) : is_nil
end

#is_nanVector

Return true if NaN.

For each input value, emit true if the value is NaN.

Unary element-wise function

Returns a Vector.

Returns:

  • (Vector)

    boolean Vector wheather each element is nan.



269
# File 'lib/red_amber/vector_unary_element_wise.rb', line 269

define_unary_element_wise :is_nan

#is_nullVector Also known as: is_nil

Note:

Arrow::NullOptions is not supported yet.

Return true if nil.

For each input value, emit true if the value is nil.

Unary element-wise function

Returns a Vector.

Returns:

  • (Vector)

    boolean Vector wheather each element is null.



278
# File 'lib/red_amber/vector_unary_element_wise.rb', line 278

define_unary_element_wise :is_null

#is_validVector

Return true if non-nil.

For each input value, emit true if the value is valid (i.e. non-nil).

Unary element-wise function

Returns a Vector.

Returns:

  • (Vector)

    boolean Vector wheather each element is valid.



287
# File 'lib/red_amber/vector_unary_element_wise.rb', line 287

define_unary_element_wise :is_valid

#less(other) ⇒ Vector Also known as: <, lt

Compare values for ordered inequality (self < other).

A nil on either side emits a nil comparison result.

Binary element-wise function

Returns a Vector.

Parameters:

  • other (Vector)

    other vector or scalar.

Returns:

  • (Vector)

    lt of self and other by a boolean Vector.



503
# File 'lib/red_amber/vector_binary_element_wise.rb', line 503

define_binary_element_wise :less

#less_equal(other) ⇒ Vector Also known as: <=, le

Compare values for ordered inequality (self <= other).

A nil on either side emits a nil comparison result.

Binary element-wise function

Returns a Vector.

Parameters:

  • other (Vector)

    other vector or scalar.

Returns:

  • (Vector)

    le of self and other by a boolean Vector.



516
# File 'lib/red_amber/vector_binary_element_wise.rb', line 516

define_binary_element_wise :less_equal

#list?true, false

Test if self is a list Vector.

Returns:

  • (true, false)

    test result.



320
321
322
# File 'lib/red_amber/vector.rb', line 320

def list?
  @data.list?
end

#lnVector

Compute natural logarithm.

Non-positive values return -inf or NaN. Nil values return nil.

Unary element-wise function

Returns a Vector.

Returns:

  • (Vector)

    natural logarithm of each element of self.



295
# File 'lib/red_amber/vector_unary_element_wise.rb', line 295

define_unary_element_wise :ln

#ln_checkedVector

Compute natural logarithm.

This function is a overflow-checking variant of #ln.

Unary element-wise function

Returns a Vector.

Returns:

  • (Vector)

    natural logarithm of each element of self.



302
# File 'lib/red_amber/vector_unary_element_wise.rb', line 302

define_unary_element_wise :ln_checked

#log10Vector

Compute base 10 logarithm.

Non-positive values return -inf or NaN. Nil values return nil.

Unary element-wise function

Returns a Vector.

Returns:

  • (Vector)

    base 10 logarithm of each element of self.



310
# File 'lib/red_amber/vector_unary_element_wise.rb', line 310

define_unary_element_wise :log10

#log10_checkedVector

Compute base 10 logarithm.

This function is a overflow-checking variant of #log10.

Unary element-wise function

Returns a Vector.

Returns:

  • (Vector)

    base 10 logarithm of each element of self.



317
# File 'lib/red_amber/vector_unary_element_wise.rb', line 317

define_unary_element_wise :log10_checked

#log1pVector

Compute natural log of (1+x).

Non-positive values return -inf or NaN. Nil values return nil. This function may be more precise than log(1 + x) for x close to zero.

Unary element-wise function

Returns a Vector.

Returns:

  • (Vector)

    natural log of (each element + 1) of self.



326
# File 'lib/red_amber/vector_unary_element_wise.rb', line 326

define_unary_element_wise :log1p

#log1p_checkedVector

Compute natural log of (1+x).

This function is a overflow-checking variant of #log1p.

Unary element-wise function

Returns a Vector.

Returns:

  • (Vector)

    natural log of (each element + 1) of self.



333
# File 'lib/red_amber/vector_unary_element_wise.rb', line 333

define_unary_element_wise :log1p_checked

#log2Vector

Compute base 2 logarithm.

Non-positive values return -inf or NaN. Nil values return nil.

Unary element-wise function

Returns a Vector.

Returns:

  • (Vector)

    base 2 logarithm of each element of self.



341
# File 'lib/red_amber/vector_unary_element_wise.rb', line 341

define_unary_element_wise :log2

#log2_checkedVector

Compute base 2 logarithm.

This function is a overflow-checking variant of #log2.

Unary element-wise function

Returns a Vector.

Returns:

  • (Vector)

    base 2 logarithm of each element of self.



348
# File 'lib/red_amber/vector_unary_element_wise.rb', line 348

define_unary_element_wise :log2_checked

#logb(b) ⇒ Vector

Compute base ‘b` logarithm of self.

Non positive values return -inf or NaN. Nil values return nil.

Binary element-wise function

Returns a Vector.

Parameters:

  • b (Integer)

    base.

Returns:

  • (Vector)

    logb of self and other.



175
# File 'lib/red_amber/vector_binary_element_wise.rb', line 175

define_binary_element_wise :logb

#logb_checkedVector

Compute base ‘b` logarithm of self.

This function is a overflow-checking variant of #logb.

Binary element-wise function

Returns a Vector.

Returns:

  • (Vector)

    logb of self and other.



182
# File 'lib/red_amber/vector_binary_element_wise.rb', line 182

define_binary_element_wise :logb_checked

#mapEnumerator #map {|element| ... } ⇒ self Also known as: collect

Returns a Vector from collected objects from the block.

Overloads:

  • #mapEnumerator

    Returns a new Enumerator if no block given.

    Returns:

    • (Enumerator)

      a new Enumerator.

  • #map {|element| ... } ⇒ self

    When a block given, calls the block with successive elements. Returns a Vector of the objects returned by the block.

    Yield Parameters:

    • element (Object)

      passes element by a block parameter.

    Yield Returns:

    • (Object)

      evaluated result value from the block.

    Returns:

    • (self)

      returns the collected values from the block as a Vector.



370
371
372
373
374
# File 'lib/red_amber/vector.rb', line 370

def map(&block)
  return enum_for(:map) unless block

  Vector.new(to_a.map(&block))
end

#max(skip_nulls: true, min_count: 1) ⇒ Numeric

Compute maximum value of self.

Unary aggregation function

Returns a scalar.

Parameters:

  • skip_nulls (true, false) (defaults to: true)

    If true, nil values are ignored. Otherwise, if any value is nil, emit nil.

  • min_count (Integer) (defaults to: 1)

    if less than this many non-nil values are observed, emit nil. If skip_nulls is false, this option is not respected.

Returns:

  • (Numeric)

    maximum value of self.



135
# File 'lib/red_amber/vector_aggregation.rb', line 135

define_unary_aggregation :max

#mean(skip_nulls: true, min_count: 1) ⇒ Numeric

Compute mean value of self.

Unary aggregation function

Returns a scalar.

Parameters:

  • skip_nulls (true, false) (defaults to: true)

    If true, nil values are ignored. Otherwise, if any value is nil, emit nil.

  • min_count (Integer) (defaults to: 1)

    if less than this many non-nil values are observed, emit nil. If skip_nulls is false, this option is not respected.

Returns:

  • (Numeric)

    mean of self.



144
# File 'lib/red_amber/vector_aggregation.rb', line 144

define_unary_aggregation :mean

#min(skip_nulls: true, min_count: 1) ⇒ Numeric

Compute minimum value of self.

Unary aggregation function

Returns a scalar.

Parameters:

  • skip_nulls (true, false) (defaults to: true)

    If true, nil values are ignored. Otherwise, if any value is nil, emit nil.

  • min_count (Integer) (defaults to: 1)

    if less than this many non-nil values are observed, emit nil. If skip_nulls is false, this option is not respected.

Returns:

  • (Numeric)

    minimum of self.



153
# File 'lib/red_amber/vector_aggregation.rb', line 153

define_unary_aggregation :min

#min_max(skip_nulls: true, min_count: 1) ⇒ Array<min, max>

Compute the min and max value of self.

Unary aggregation function

Returns a scalar.

Parameters:

  • skip_nulls (true, false) (defaults to: true)

    If true, nil values are ignored. Otherwise, if any value is nil, emit nil.

  • min_count (Integer) (defaults to: 1)

    if less than this many non-nil values are observed, emit nil. If skip_nulls is false, this option is not respected.

Returns:

  • (Array<min, max>)

    min and max of self in an Array.



162
# File 'lib/red_amber/vector_aggregation.rb', line 162

define_unary_aggregation :min_max

#modeHash{'mode'=>mode, 'count'=>count}

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

Self must be a numeric or a boolean Vector.

Note:

ModeOptions are not supported in 0.5.0 . Only one mode value is returned.

Compute the 1 most common values and their respective

occurence counts.

Returns:

Since:

  • 0.5.0



175
176
177
178
# File 'lib/red_amber/vector_aggregation.rb', line 175

def mode
  datum = find(:mode).execute([data])
  datum.value.to_a.first
end

#modulo(divisor) ⇒ Vector Also known as: %

Note:

Same behavior as Ruby.

Returns element-wise modulo.

This is equivalent to ‘self-divisor*(self/divisor).floor`.

Parameters:

  • divisor (Vector, numeric)

    divisor numeric Vector or numeric scalar.

Returns:

  • (Vector)

    modulo of dividing self by divisor.



257
258
259
260
261
262
263
264
# File 'lib/red_amber/vector_binary_element_wise.rb', line 257

def modulo(divisor)
  divisor = divisor.data if divisor.is_a?(Vector)
  d = find(:divide).execute([data, divisor])
  d = find(:floor).execute([d]) if d.value.is_a?(Arrow::DoubleArray)
  m = find(:multiply).execute([d, divisor])
  datum = find(:subtract).execute([data, m])
  Vector.create(datum.value)
end

#modulo_checked(divisor) ⇒ Vector

Returns element-wise modulo.

This function is a overflow-checking variant of #modulo.

Returns:

  • (Vector)

    modulo of dividing self by divisor.



272
273
274
275
276
277
278
279
# File 'lib/red_amber/vector_binary_element_wise.rb', line 272

def modulo_checked(divisor)
  divisor = divisor.data if divisor.is_a?(Vector)
  d = find(:divide_checked).execute([data, divisor])
  d = find(:floor).execute([d]) if d.value.is_a?(Arrow::DoubleArray)
  m = find(:multiply_checked).execute([d, divisor])
  datum = find(:subtract_checked).execute([data, m])
  Vector.create(datum.value)
end

#multiply(other) ⇒ Vector Also known as: mul, *

Multiply the arguments element-wise.

Results will wrap around on integer overflow.

Binary element-wise function

Returns a Vector.

Parameters:

  • other (Vector, Numeric)

    other numeric vector or numeric scalar.

Returns:

  • (Vector)

    multiplication of self and other.



290
# File 'lib/red_amber/vector_binary_element_wise.rb', line 290

define_binary_element_wise :multiply

#multiply_checkedVector

Multiply the arguments element-wise.

This function is a overflow-checking variant of #multiply.

Binary element-wise function

Returns a Vector.

Returns:

  • (Vector)

    multiplication of self and other.



299
# File 'lib/red_amber/vector_binary_element_wise.rb', line 299

define_binary_element_wise :multiply_checked

#n_chunksInteger

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the number of chunks.

Returns:

  • (Integer)

    the number of chunks. If self is not chunked, returns zero.



393
394
395
# File 'lib/red_amber/vector.rb', line 393

def n_chunks
  chunked? ? @data.n_chunks : 0
end

#n_nansInteger

Count NaNs in self if self is a numeric Vector

Returns:

  • (Integer)

    the number of Float::NANs. If self is not a numeric Vector, returns 0.



444
445
446
# File 'lib/red_amber/vector.rb', line 444

def n_nans
  numeric? ? is_nan.to_a.count(true) : 0
end

#n_nullsInteger Also known as: n_nils

Count nils in self.

Returns:

  • (Integer)

    the number of nils.



433
434
435
# File 'lib/red_amber/vector.rb', line 433

def n_nulls
  @data.n_nulls
end

#negateVector Also known as: -@

Negate the argument element-wise

Results will wrap around on integer overflow.

Unary element-wise function

Returns a Vector.

Returns:

  • (Vector)

    negate of each element of self.



523
# File 'lib/red_amber/vector_unary_element_wise.rb', line 523

define_unary_element_wise :negate

#negate_checkedVector

Negate the argument element-wise

This function is a overflow-checking variant of #negate.

Unary element-wise function

Returns a Vector.

Returns:

  • (Vector)

    negate of each element of self.



531
# File 'lib/red_amber/vector_unary_element_wise.rb', line 531

define_unary_element_wise :negate_checked

#not_equal(other) ⇒ Vector Also known as: !=, ne

Compare values for inequality (self != other).

A nil on either side emits a nil comparison result.

Binary element-wise function

Returns a Vector.

Parameters:

  • other (Vector)

    other vector or scalar.

Returns:

  • (Vector)

    ne of self and other by a boolean Vector.



529
# File 'lib/red_amber/vector_binary_element_wise.rb', line 529

define_binary_element_wise :not_equal

#numeric?true, false

Test if self is a numeric Vector.

Returns:

  • (true, false)

    test result.



266
267
268
# File 'lib/red_amber/vector.rb', line 266

def numeric?
  @data.numeric?
end

#oneObject?

Get a non-nil element in self.

Returns:

  • (Object, nil)

    first non-nil value detected. If all elements are nil, return nil.

Since:

  • 0.5.0



266
267
268
# File 'lib/red_amber/vector_aggregation.rb', line 266

def one
  each.find { !_1.nil? }
end

#or_kleene(other) ⇒ Vector Also known as: |

Logical ‘or’ boolean values with Kleene logic.

This function behaves as follows with nils:

  • true or nil = true

  • nil or true = true

  • false or nil = nil

  • nil or false = nil

  • nil or nil = nil

In other words, in this context a nil value really means “unknown”, and an unknown value ‘or’ true is always true. For a different nil behavior, see function #or_org.

Binary element-wise function

Returns a Vector.

Parameters:

  • other (Vector, array-like)

    boolean array-like.

Returns:

  • (Vector)

    or_kleene of self and other.



194
# File 'lib/red_amber/vector_binary_element_wise.rb', line 194

define_binary_element_wise :or_kleene

#or_org(other) ⇒ Vector

Logical ‘or’ boolean values.

When a nil is encountered in either input, a nil is output. For a different nil behavior, see function #or_kleene.

Binary element-wise function

Returns a Vector.

Parameters:

  • other (Vector, array-like)

    boolean array-like.

Returns:

  • (Vector)

    evacuated ‘or` of self and other.



207
# File 'lib/red_amber/vector_binary_element_wise.rb', line 207

define_binary_element_wise_logical(:or_org, :or)

#power(exponent) ⇒ Vector Also known as: pow, **

Raise arguments to power element-wise.

Integer to negative integer power returns an error. However, integer overflow wraps around. If either self or exponent is nil the result will be nil.

Binary element-wise function

Returns a Vector.

Parameters:

  • exponent (Vector, Numeric)

    numeric vector or numeric scalar as exponent.

Returns:

  • (Vector)

    power operation of self and other.



312
# File 'lib/red_amber/vector_binary_element_wise.rb', line 312

define_binary_element_wise :power

#power_checkedVector

Raise arguments to power element-wise.

This function is a overflow-checking variant of #power.

Binary element-wise function

Returns a Vector.

Returns:

  • (Vector)

    power operation of self and other.



321
# File 'lib/red_amber/vector_binary_element_wise.rb', line 321

define_binary_element_wise :power_checked

#product(skip_nulls: true, min_count: 1) ⇒ Numeric

Note:

Self must be a numeric Vector.

Compute product value of self.

Unary aggregation function

Returns a scalar.

Parameters:

  • skip_nulls (true, false) (defaults to: true)

    If true, nil values are ignored. Otherwise, if any value is nil, emit nil.

  • min_count (Integer) (defaults to: 1)

    if less than this many non-nil values are observed, emit nil. If skip_nulls is false, this option is not respected.

Returns:

  • (Numeric)

    product of self.



188
# File 'lib/red_amber/vector_aggregation.rb', line 188

define_unary_aggregation :product

#propagate(function) ⇒ Vector #propagate {|self| ... } ⇒ Vector Also known as: expand

Spread the return value of an aggregate function as if

it is a element-wise function.

Overloads:

  • #propagate(function) ⇒ Vector

    Returns a Vector of same size as self spreading the value from function.

    Examples:

    propagate by an aggragation function name

    vec = Vector.new(1, 2, 3, 4)
    vec.propagate(:mean)
    # =>
    #<RedAmber::Vector(:double, size=4):0x000000000001985c>
    [2.5, 2.5, 2.5, 2.5]

    Parameters:

    • function (Symbol)

      a name of aggregation function for self. Return value of the function must be a scalar.

    Returns:

    • (Vector)

      Returns a Vector that is the same size as self and such that all elements are the same as the result of aggregation ‘function`.

  • #propagate {|self| ... } ⇒ Vector

    Returns a Vector of same size as self spreading the value from block.

    Examples:

    propagate by a block

    vec.propagate { |v| v.mean.round }
    # =>
    #<RedAmber::Vector(:uint8, size=4):0x000000000000cb98>
    [3, 3, 3, 3]

    Yield Parameters:

    • self (Vector)

      gives self to the block.

    Yield Returns:

    • (scalar)

      a scalar value.

    Returns:

    • (Vector)

      returns a Vector that is the same size as self and such that all elements are the same as the yielded value from the block.

Raises:

Since:

  • 0.4.0



526
527
528
529
530
531
532
533
534
535
536
537
538
# File 'lib/red_amber/vector.rb', line 526

def propagate(function = nil, &block)
  value =
    if block
      raise VectorArgumentError, "can't specify both function and block" if function

      yield self
    else
      send(function&.to_sym)
    end
  raise VectorArgumentError, 'not an aggregation function' if value.is_a?(Vector)

  Vector.new([value] * size)
end

#quantile(prob = 0.5, interpolation: :linear, skip_nulls: true, min_count: 0) ⇒ Float

Returns a quantile value.

  • 0.5 quantile (median) is returned by default.

  • Or return quantile for specified probability (prob).

  • If quantile lies between two data points, interpolated value is returned based on selected interpolation method.

  • Nils and NaNs are ignored.

  • Nil is returned if there are no valid data point.

Examples:

penguins[:bill_depth_mm].quantile

# =>
17.3 # defaultis prob = 0.5

Parameters:

  • prob (Float) (defaults to: 0.5)

    probability.

  • interpolation (Symbol) (defaults to: :linear)

    specifies interpolation method to use, when the quantile lies between the data i and j.

    • Default value is :linear, which returns i + (j - i) * fraction.

    • lower: returns i.

    • higher: returns j.

    • nearest: returns i or j, whichever is closer.

    • midpoint: returns (i + j) / 2.

  • skip_nulls (true, false) (defaults to: true)

    If true, nil values are ignored. Otherwise, if any value is nil, emit nil.

  • min_count (Integer) (defaults to: 0)

    if less than this many non-nil values are observed, emit nil. If skip_nulls is false, this option is not respected.

Returns:

  • (Float)

    quantile of self.



290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/red_amber/vector_aggregation.rb', line 290

def quantile(prob = 0.5, interpolation: :linear, skip_nulls: true, min_count: 0)
  unless (0..1).cover? prob
    raise VectorArgumentError,
          "Invalid: probability #{prob} must be between 0 and 1"
  end

  datum = find(:quantile).execute([data],
                                  q: prob,
                                  interpolation: interpolation,
                                  skip_nulls: skip_nulls,
                                  min_count: min_count)
  datum.value.to_a.first
end

#quantiles(probs = [0.0, 0.25, 0.5, 0.75, 1.0], interpolation: :linear, skip_nulls: true, min_count: 0) ⇒ DataFrame

Return quantiles in a DataFrame

Examples:

penguins[:bill_depth_mm].quantiles([0.05, 0.95])

# =>
#<RedAmber::DataFrame : 2 x 2 Vectors, 0x000000000000fb2c>
     probs quantiles
  <double>  <double>
0     0.05      13.9
1     0.95      20.0

Parameters:

  • probs (Array) (defaults to: [0.0, 0.25, 0.5, 0.75, 1.0])

    Array of probabilities. Default probabilities are 0.0, 0.25, 0.5 0.75, 1.0 .

  • interpolation (Symbol) (defaults to: :linear)

    specifies interpolation method to use, when the quantile lies between the data i and j.

    • Default value is :linear, which returns i + (j - i) * fraction.

    • lower: returns i.

    • higher: returns j.

    • nearest: returns i or j, whichever is closer.

    • midpoint: returns (i + j) / 2.

  • skip_nulls (true, false) (defaults to: true)

    If true, nil values are ignored. Otherwise, if any value is nil, emit nil.

  • min_count (Integer) (defaults to: 0)

    if less than this many non-nil values are observed, emit nil. If skip_nulls is false, this option is not respected.

Returns:



322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/red_amber/vector_aggregation.rb', line 322

def quantiles(probs = [0.0, 0.25, 0.5, 0.75, 1.0],
              interpolation: :linear, skip_nulls: true, min_count: 0)
  if probs.empty? || !probs.all? { |q| (0..1).cover?(q) }
    raise VectorArgumentError, "Invarid probavilities #{probs}"
  end

  DataFrame.new(
    probs: probs,
    quantiles: probs.map do |q|
      quantile(q,
               interpolation: interpolation, skip_nulls: skip_nulls,
               min_count: min_count)
    end
  )
end

#remainder(divisor) ⇒ Vector

Note:

Same behavior as Ruby’s remainder.

Returns element-wise remainder.

This is equivalent to ‘self-divisor*(self/divisor).trunc`.

Parameters:

  • divisor (Vector, numeric)

    divisor numeric Vector or numeric scalar.

Returns:

  • (Vector)

    modulo of dividing self by divisor.



356
357
358
359
360
361
362
363
# File 'lib/red_amber/vector_binary_element_wise.rb', line 356

def remainder(divisor)
  divisor = divisor.data if divisor.is_a?(Vector)
  d = find(:divide).execute([data, divisor])
  d = find(:trunc).execute([d]) if d.value.is_a?(Arrow::DoubleArray)
  m = find(:multiply).execute([d, divisor])
  datum = find(:subtract).execute([data, m])
  Vector.create(datum.value)
end

#remainder_checked(divisor) ⇒ Vector

Returns element-wise modulo.

This function is a overflow-checking variant of #modulo.

Returns:

  • (Vector)

    modulo of dividing self by divisor.



370
371
372
373
374
375
376
377
# File 'lib/red_amber/vector_binary_element_wise.rb', line 370

def remainder_checked(divisor)
  divisor = divisor.data if divisor.is_a?(Vector)
  d = find(:divide_checked).execute([data, divisor])
  d = find(:trunc).execute([d]) if d.value.is_a?(Arrow::DoubleArray)
  m = find(:multiply_checked).execute([d, divisor])
  datum = find(:subtract_checked).execute([data, m])
  Vector.create(datum.value)
end

#resolve(other) ⇒ Vector

Return other as a Vector which is same data type as self.

Examples:

Integer to String

Vector.new('A').resolve([1, 2])

# =>
#<RedAmber::Vector(:string, size=2):0x00000000000037b4>
["1", "2"]

String to Ineger

Vector.new(1).resolve(['A'])

# =>
#<RedAmber::Vector(:uint8, size=1):0x00000000000037dc>
[65]

Upcast to uint16

vector = Vector.new(256)

# =>
#<RedAmber::Vector(:uint16, size=1):0x000000000000c1fc>
[256]

vector.resolve([1, 2])

# =>
# Not a uint8 Vector
#<RedAmber::Vector(:uint16, size=2):0x000000000000c328>
[1, 2]

Parameters:

  • other (Vector, Array, Arrow::Array, Arrow::ChunkedArray)

    a source array-like which will be converted.

Returns:

  • (Vector)

    resolved Vector.

Since:

  • 0.4.0



123
124
125
126
127
128
129
130
131
132
# File 'lib/red_amber/vector.rb', line 123

def resolve(other)
  case other
  when Vector
    Vector.create(data.resolve(other.data))
  when Array, Arrow::Array, Arrow::ChunkedArray
    Vector.create(data.resolve(other))
  else
    raise VectorArgumentError, "invalid argument: #{other}"
  end
end

#round(n_digits: 0, round_mode: :half_to_even) ⇒ Vector

Round to a given precision.

Options are used to control the number of digits and rounding mode. Default behavior is to round to the nearest integer and use half-to-even rule to break ties.

Unary element-wise function

Returns a Vector.

Examples:

double = Vector.new([15.15, 2.5, 3.5, -4.5, -5.5])
double.round
# or double.round(n_digits: 0, mode: :half_to_even)

# =>
#<RedAmber::Vector(:double, size=5):0x000000000000cd28>
[15.0, 2.0, 4.0, -4.0, -6.0]

double.round(mode: :towards_infinity)

# =>
#<RedAmber::Vector(:double, size=5):0x000000000000cd3c>
[16.0, 3.0, 4.0, -5.0, -6.0]

double.round(mode: :half_up)

# =>
#<RedAmber::Vector(:double, size=5):0x000000000000cd50>
[15.0, 3.0, 4.0, -4.0, -5.0]

double.round(mode: :half_towards_zero)

# =>
#<RedAmber::Vector(:double, size=5):0x000000000000cd64>
[15.0, 2.0, 3.0, -4.0, -5.0]

double.round(mode: :half_towards_infinity)

# =>
#<RedAmber::Vector(:double, size=5):0x000000000000cd78>
[15.0, 3.0, 4.0, -5.0, -6.0]

double.round(mode: :half_to_odd)

# =>
#<RedAmber::Vector(:double, size=5):0x000000000000cd8c>
[15.0, 3.0, 3.0, -5.0, -5.0]

double.round(n_digits: 1)

# =>
#<RedAmber::Vector(:double, size=5):0x000000000000cda0>
[15.2, 2.5, 3.5, -4.5, -5.5]

double.round(n_digits: -1)

# =>
#<RedAmber::Vector(:double, size=5):0x000000000000cdb4>
[20.0, 0.0, 0.0, -0.0, -10.0]

Parameters:

  • n_digits (Integer) (defaults to: 0)

    Rounding precision (number of digits to round to).

  • round_mode (:down, :up, :towards_zero, :towards_infinity, :half_down, :half_up, :half_towards_zero, :half_towards_infinity, :half_to_even, :half_to_odd) (defaults to: :half_to_even)

    Rounding and tie-breaking mode.

    • down: Round to nearest integer less than or equal in magnitude (aka “floor”).

    • up: Round to nearest integer greater than or equal in magnitude (aka “ceil”).

    • towards_zero: Get the integral part without fractional digits (aka “trunc”).

    • towards_infinity: Round negative values with :down rule and positive values with :up rule (aka “away from zero”).

    • half_down: Round ties with :down rule (also called “round half towards negative infinity”).

    • half_up: Round ties with :up rule (also called “round half towards positive infinity”).

    • half_towards_zero: Round ties with :towards_zero rule (also called “round half away from infinity”).

    • half_towards_infinity: Round ties with :towards_infinity rule (also called “round half away from zero”).

    • half_to_even: Round ties to nearest even integer.

    • half_to_odd: Round ties to nearest odd integer.

Returns:

  • (Vector)

    round of each element of self.



412
# File 'lib/red_amber/vector_unary_element_wise.rb', line 412

define_unary_element_wise :round

#round_to_multiple(multiple: 1.0, round_mode: :half_to_even) ⇒ Vector

Round to a given multiple.

Options are used to control the rounding multiple and rounding mode. Default behavior is to round to the nearest integer and use half-to-even rule to break ties.

Parameters:

  • multiple (Float, Integer) (defaults to: 1.0)

    Rounding scale (multiple to round to). Should be a positive numeric scalar of a type compatible with the argument to be rounded. The cast kernel is used to convert the rounding multiple to match the result type.

  • round_mode (:down, :up, :towards_zero, :towards_infinity, :half_down, :half_up, :half_towards_zero, :half_towards_infinity, :half_to_even, :half_to_odd) (defaults to: :half_to_even)

    Rounding and tie-breaking mode.

    • down: Round to nearest integer less than or equal in magnitude (aka “floor”).

    • up: Round to nearest integer greater than or equal in magnitude (aka “ceil”).

    • towards_zero: Get the integral part without fractional digits (aka “trunc”).

    • towards_infinity: Round negative values with :down rule and positive values with :up rule (aka “away from zero”).

    • half_down: Round ties with :down rule (also called “round half towards negative infinity”).

    • half_up: Round ties with :up rule (also called “round half towards positive infinity”).

    • half_towards_zero: Round ties with :towards_zero rule (also called “round half away from infinity”).

    • half_towards_infinity: Round ties with :towards_infinity rule (also called “round half away from zero”).

    • half_to_even: Round ties to nearest even integer.

    • half_to_odd: Round ties to nearest odd integer.

Returns:

  • (Vector)

    round to multiple of each element of self.



429
430
431
432
433
434
# File 'lib/red_amber/vector_unary_element_wise.rb', line 429

def round_to_multiple(multiple: 1.0, round_mode: :half_to_even)
  datum = exec_func_unary(:round_to_multiple,
                          multiple: Arrow::DoubleScalar.new(multiple),
                          round_mode: round_mode)
  Vector.create(datum.value)
end

#sd(ddof: 1, skip_nulls: true, min_count: 1) ⇒ Float Also known as: std

Note:

Self must be a numeric Vector.

Calculate unbiased standard deviation of self.

Parameters:

  • ddof (0, 1) (defaults to: 1)

    Control Delta Degrees of Freedom (ddof) of Variance and Stddev kernel. The divisor used in calculations is N - ddof, where N is the number of elements. By default, ddof is zero, and population variance or stddev is returned.

  • skip_nulls (true, false) (defaults to: true)

    If true, nil values are ignored. Otherwise, if any value is nil, emit nil.

  • min_count (Integer) (defaults to: 1)

    if less than this many non-nil values are observed, emit nil. If skip_nulls is false, this option is not respected.

Returns:

  • (Float)

    standard deviation of self. Unviased (ddof=1)by default.



208
209
210
# File 'lib/red_amber/vector_aggregation.rb', line 208

def sd
  stddev(ddof: 1)
end

#shift_left(amount) ⇒ Vector Also known as: <<

Left shift of self by other.

The shift operates as if on the two’s complement representation of the number. In other words, this is equivalent to multiplying self by 2 to the power ‘amount’, even if overflow occurs. self is returned if ‘amount’ (the amount to shift by) is negative or greater than or equal to the precision of self.

Binary element-wise function

Returns a Vector.

Parameters:

  • amount (integer)

    the amount to shift by.

Returns:

  • (Vector)

    shift_left of self by amount.



412
# File 'lib/red_amber/vector_binary_element_wise.rb', line 412

define_binary_element_wise :shift_left

#shift_left_checkedVector

Left shift of self by other.

This function is a overflow-checking variant of #shift_left.

Binary element-wise function

Returns a Vector.

Returns:

  • (Vector)

    shift_left of self by amount.



420
# File 'lib/red_amber/vector_binary_element_wise.rb', line 420

define_binary_element_wise :shift_left_checked

#shift_right(amount) ⇒ Vector Also known as: >>

Right shift of self by other.

This is equivalent to dividing ‘x` by 2 to the power `y`. Self is returned if ’amount’ (the amount to shift by) is: negative or greater than or equal to the precision of self.

Binary element-wise function

Returns a Vector.

Parameters:

  • amount (integer)

    the amount to shift by.

Returns:

  • (Vector)

    shift_right of self by amount.



433
# File 'lib/red_amber/vector_binary_element_wise.rb', line 433

define_binary_element_wise :shift_right

#shift_right_checkedVector

Right shift of self by other.

This function is a overflow-checking variant of #shift_right.

Binary element-wise function

Returns a Vector.

Returns:

  • (Vector)

    shift_right of self by amount.



441
# File 'lib/red_amber/vector_binary_element_wise.rb', line 441

define_binary_element_wise :shift_right_checked

#signVector

Get the signedness of the arguments element-wise.

Output is any of (-1,1) for nonzero inputs and 0 for zero input. NaN values return NaN. Integral values return signedness as Int8 and floating-point values return it with the same type as the input values.

Unary element-wise function

Returns a Vector.

Returns:

  • (Vector)

    sign of each element of self.



444
# File 'lib/red_amber/vector_unary_element_wise.rb', line 444

define_unary_element_wise :sign

#sinVector

Compute the sine of self element-wise.

NaN is returned for invalid input values.

Unary element-wise function

Returns a Vector.

Returns:

  • (Vector)

    sine of each element of self.



452
# File 'lib/red_amber/vector_unary_element_wise.rb', line 452

define_unary_element_wise :sin

#sin_checkedVector

Compute the sine of self element-wise.

This function is a overflow-checking variant of #sin.

Unary element-wise function

Returns a Vector.

Returns:

  • (Vector)

    sine of each element of self.



459
# File 'lib/red_amber/vector_unary_element_wise.rb', line 459

define_unary_element_wise :sin_checked

#sizeInteger Also known as: length, n_rows, nrow

Vector size.

Returns:

  • (Integer)

    size of self.



217
218
219
220
# File 'lib/red_amber/vector.rb', line 217

def size
  # only defined :length in Arrow?
  @data.length
end

#sqrtVector

Compute square root of self.

NaN is returned for invalid input values.

Unary element-wise function

Returns a Vector.

Returns:

  • (Vector)

    sqrt of each element of self.



467
# File 'lib/red_amber/vector_unary_element_wise.rb', line 467

define_unary_element_wise :sqrt

#sqrt_checkedVector

Compute square root of self.

This function is a overflow-checking variant of #sqrt.

Unary element-wise function

Returns a Vector.

Returns:

  • (Vector)

    sqrt of each element of self.



474
# File 'lib/red_amber/vector_unary_element_wise.rb', line 474

define_unary_element_wise :sqrt_checked

#stddev(ddof: 0, skip_nulls: true, min_count: 1) ⇒ Float

Note:

Self must be a numeric Vector.

Calculate standard deviation of self.

Unary aggregation function

Returns a scalar.

Parameters:

  • ddof (0, 1) (defaults to: 0)

    Control Delta Degrees of Freedom (ddof) of Variance and Stddev kernel. The divisor used in calculations is N - ddof, where N is the number of elements. By default, ddof is zero, and population variance or stddev is returned.

  • skip_nulls (true, false) (defaults to: true)

    If true, nil values are ignored. Otherwise, if any value is nil, emit nil.

  • min_count (Integer) (defaults to: 1)

    if less than this many non-nil values are observed, emit nil. If skip_nulls is false, this option is not respected.

Returns:

  • (Float)

    standard deviation of self. Biased (ddof=0) by default.



198
# File 'lib/red_amber/vector_aggregation.rb', line 198

define_unary_aggregation :stddev

#string?true, false

Test if self is a string Vector.

Returns:

  • (true, false)

    test result.



293
294
295
# File 'lib/red_amber/vector.rb', line 293

def string?
  @data.string?
end

#subtract(other) ⇒ Vector Also known as: sub, -

Subtract the arguments element-wise.

Results will wrap around on integer overflow.

Binary element-wise function

Returns a Vector.

Parameters:

  • other (Vector, Numeric)

    other numeric vector or numeric scalar.

Returns:

  • (Vector)

    subtraction of self and other.



388
# File 'lib/red_amber/vector_binary_element_wise.rb', line 388

define_binary_element_wise :subtract

#subtract_checkedVector

Subtract the arguments element-wise.

This function is a overflow-checking variant of #subtract.

Binary element-wise function

Returns a Vector.

Returns:

  • (Vector)

    subtraction of self and other.



397
# File 'lib/red_amber/vector_binary_element_wise.rb', line 397

define_binary_element_wise :subtract_checked

#sum(skip_nulls: true, min_count: 1) ⇒ Numeric

Note:

Self must be a numeric Vector.

Compute sum of self.

Unary aggregation function

Returns a scalar.

Parameters:

  • skip_nulls (true, false) (defaults to: true)

    If true, nil values are ignored. Otherwise, if any value is nil, emit nil.

  • min_count (Integer) (defaults to: 1)

    if less than this many non-nil values are observed, emit nil. If skip_nulls is false, this option is not respected.

Returns:

  • (Numeric)

    sum of self.



221
# File 'lib/red_amber/vector_aggregation.rb', line 221

define_unary_aggregation :sum

#tallyHash

Returns a hash containing the counts of equal elements.

  • Each key is an element of self.

  • Each value is the number of elements equal to the key.

Returns:

  • (Hash)

    result in a Hash.



406
407
408
409
410
411
412
413
414
415
416
417
418
419
# File 'lib/red_amber/vector.rb', line 406

def tally
  hash = values.tally
  if (type_class < Arrow::FloatingPointDataType) && is_nan.any
    a = 0
    hash.each do |key, value|
      if key.is_a?(Float) && key.nan?
        hash.delete(key)
        a += value
      end
    end
    hash[Float::NAN] = a
  end
  hash
end

#tanVector

Compute the tangent of self element-wise.

NaN is returned for invalid input values.

Unary element-wise function

Returns a Vector.

Returns:

  • (Vector)

    tangent of each element of self.



482
# File 'lib/red_amber/vector_unary_element_wise.rb', line 482

define_unary_element_wise :tan

#tan_checkedVector

Compute the tangent of self element-wise.

This function is a overflow-checking variant of #tan.

Unary element-wise function

Returns a Vector.

Returns:

  • (Vector)

    tangent of each element of self.



489
# File 'lib/red_amber/vector_unary_element_wise.rb', line 489

define_unary_element_wise :tan_checked

#temporal?true, false

Test if self is a temporal Vector.

Returns:

  • (true, false)

    test result.



311
312
313
# File 'lib/red_amber/vector.rb', line 311

def temporal?
  @data.temporal?
end

#to_aryArray Also known as: to_a, values, entries

Convert to an Array.

Returns:

  • (Array)

    array representation.



194
195
196
# File 'lib/red_amber/vector.rb', line 194

def to_ary
  @data.values
end

#to_sString

String representation of self like an Array.

Returns:

  • (String)

    return self as same as Array’s inspect.



139
140
141
# File 'lib/red_amber/vector.rb', line 139

def to_s
  @data.to_a.inspect
end

#truncVector

Compute the integral part

Compute the nearest integer not greater in magnitude than each element.

Unary element-wise function

Returns a Vector.

Returns:

  • (Vector)

    trunc of each element of self.



497
# File 'lib/red_amber/vector_unary_element_wise.rb', line 497

define_unary_element_wise :trunc

#typeSymbol

Type nickname of self.

Returns:

  • (Symbol)

    type nickname of values.



239
240
241
# File 'lib/red_amber/vector.rb', line 239

def type
  list? ? :list : @data.value_type.nick.to_sym
end

#type_classtype_Class

Type Class of self.

Returns:

  • (type_Class)

    type class.



248
249
250
# File 'lib/red_amber/vector.rb', line 248

def type_class
  @data.type_class
end

#unbiased_variance(ddof: 1, skip_nulls: true, min_count: 1) ⇒ Float Also known as: var

Note:

self must be a numeric Vector.

Calculate unbiased variance of self.

Parameters:

  • ddof (0, 1) (defaults to: 1)

    Control Delta Degrees of Freedom (ddof) of Variance and Stddev kernel. The divisor used in calculations is N - ddof, where N is the number of elements. By default, ddof is zero, and population variance or stddev is returned.

  • skip_nulls (true, false) (defaults to: true)

    If true, nil values are ignored. Otherwise, if any value is nil, emit nil.

  • min_count (Integer) (defaults to: 1)

    if less than this many non-nil values are observed, emit nil. If skip_nulls is false, this option is not respected.

Returns:

  • (Float)

    variance of self. Unviased (ddof=1) by default.



245
246
247
# File 'lib/red_amber/vector_aggregation.rb', line 245

def unbiased_variance
  variance(ddof: 1)
end

#uniqueVector Also known as: uniq

Compute unique elements

Return an array with distinct values. Nils in the input are ignored.

Unary element-wise function

Returns a Vector.

Returns:

  • (Vector)

    uniq element of self.



505
# File 'lib/red_amber/vector_unary_element_wise.rb', line 505

define_unary_element_wise :unique

#value_countsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Arrow imprementation of #tally



423
424
425
426
# File 'lib/red_amber/vector.rb', line 423

def value_counts
  values, counts = Arrow::Function.find(:value_counts).execute([data]).value.fields
  values.zip(counts).to_h
end

#variance(ddof: 0, skip_nulls: true, min_count: 1) ⇒ Float

Note:

Self must be a numeric Vector.

Calculate variance of self.

Unary aggregation function

Returns a scalar.

Parameters:

  • ddof (0, 1) (defaults to: 0)

    Control Delta Degrees of Freedom (ddof) of Variance and Stddev kernel. The divisor used in calculations is N - ddof, where N is the number of elements. By default, ddof is zero, and population variance or stddev is returned.

  • skip_nulls (true, false) (defaults to: true)

    If true, nil values are ignored. Otherwise, if any value is nil, emit nil.

  • min_count (Integer) (defaults to: 1)

    if less than this many non-nil values are observed, emit nil. If skip_nulls is false, this option is not respected.

Returns:

  • (Float)

    unviased (ddof=1) standard deviation of self by default.

  • (Float)

    variance of self. Biased (ddof=0) by default.



235
# File 'lib/red_amber/vector_aggregation.rb', line 235

define_unary_aggregation :variance

#xor(other) ⇒ Vector Also known as: ^

Logical ‘xor’ boolean values

When a nil is encountered in either input, a nil is output.

Binary element-wise function

Returns a Vector.

Parameters:

  • other (Vector)

    other boolean vector or boolean scalar.

Returns:

  • (Vector)

    eq of self and other by a boolean Vector.



452
# File 'lib/red_amber/vector_binary_element_wise.rb', line 452

define_binary_element_wise :xor