Module: VectorNumber::Enumerating

Includes:
Enumerable
Included in:
VectorNumber
Defined in:
lib/vector_number/enumerating.rb

Overview

Methods for enumerating values of the number.

Enumerable is included so its methods can be used.

Examples:

using Enumerable methods

VectorNumber["a", "b", 6].include?(["a", 1]) # => true
VectorNumber["a", "b", 6].select { |u, c| u.is_a?(String) } # => [["a", 1], ["b", 1]]

Instance Method Summary collapse

Instance Method Details

#[](unit) ⇒ Integer, ...

Get the coefficient for the unit.

If the unit?(unit) is false, 0 is returned. Note that units for real and imaginary parts are VectorNumber::R and VectorNumber::I respectively.

Examples:

VectorNumber["a", "b", 6]["a"] # => 1
VectorNumber["a", "b", 6]["c"] # => 0

Parameters:

  • unit (Object)

Returns:

  • (Integer, Float, Rational, BigDecimal)

Since:

  • 0.2.4



111
# File 'lib/vector_number/enumerating.rb', line 111

def [](unit) = @data[unit]

#coefficientsArray<Integer, Float, Rational, BigDecimal> Also known as: values

Get a list of non-zero coefficients.

Examples:

VectorNumber["a", "b", 6].coefficients # => [1, 1, 6]
VectorNumber.new.values # => []

Returns:

  • (Array<Integer, Float, Rational, BigDecimal>)

Since:

  • 0.1.0



72
# File 'lib/vector_number/enumerating.rb', line 72

def coefficients = @data.values

#each {|unit, coefficient| ... } ⇒ VectorNumber #eachEnumerator Also known as: each_pair

Iterate through every pair of unit and coefficient. Returns Enumerator (with set size) if no block is given.

Examples:

v = VectorNumber["a", "b", 6]
units = []
v.each { |u, c| units << u unless u.is_a?(Numeric) } # => (1⋅'a' + 1⋅'b' + 6)
units # => ["a", "b"]

Enumerator

v.each.size # => 3
(v.each + [["d", 0]]).map(&:first) # => ["a", "b", 1, "d"]
v.each_pair.peek # => ["a", 1]

Overloads:

  • #each {|unit, coefficient| ... } ⇒ VectorNumber

    Returns self.

    Yield Parameters:

    • unit (Object)
    • coefficient (Integer, Float, Rational, BigDecimal)

    Yield Returns:

    • (void)

    Returns:

  • #eachEnumerator

    Returns:

    • (Enumerator)

See Also:

  • Enumerable
  • Enumerator

Since:

  • 0.1.0



39
40
41
42
43
44
# File 'lib/vector_number/enumerating.rb', line 39

def each(&block)
  return to_enum { size } unless block_given?

  @data.each(&block)
  self
end

#to_h(&block) ⇒ Hash{Object => Integer, Float, Rational, BigDecimal}

Get mutable hash with vector’s data.

Returned hash has a default value of 0.

Examples:

VectorNumber["a", "b", 6].to_h # => {"a"=>1, "b"=>1, 1=>6}
VectorNumber["a", "b", 6].to_h["c"] # => 0

Returns:

  • (Hash{Object => Integer, Float, Rational, BigDecimal})

Since:

  • 0.1.0



88
89
90
91
92
93
94
95
# File 'lib/vector_number/enumerating.rb', line 88

def to_h(&block)
  # TODO: Remove block argument.
  if block_given?
    @data.to_h(&block)
  else
    @data.dup
  end
end

#unit?(unit) ⇒ Boolean Also known as: key?

Check if a unit has a non-zero coefficient.

Examples:

VectorNumber["a", "b", 6].unit?("a") # => true
VectorNumber["a", "b", 6].key?("c") # => false

Parameters:

  • unit (Object)

Returns:

  • (Boolean)

Since:

  • 0.2.4



123
# File 'lib/vector_number/enumerating.rb', line 123

def unit?(unit) = @data.key?(unit)

#unitsArray<Object> Also known as: keys

Get a list of units with non-zero coefficients.

Examples:

VectorNumber["a", "b", 6].units # => ["a", "b", 1]
VectorNumber.new.keys # => []

Returns:

  • (Array<Object>)

Since:

  • 0.1.0



58
# File 'lib/vector_number/enumerating.rb', line 58

def units = @data.keys