Class: KXI::Math::Vector

Inherits:
Object
  • Object
show all
Defined in:
lib/kxi/math/vector.rb

Overview

Represents a vector

Instance Method Summary collapse

Constructor Details

#initialize(dim) {|d| ... } ⇒ Vector

Instantiates the KXI::Math::Vector class

Parameters:

  • dim (Integer)

    Dimension of vector

Yields:

  • (d)

    Generator function

Yield Parameters:

  • Dimension

    to generate

Yield Returns:

  • (Numeric)

    Result of generator

Raises:



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/kxi/math/vector.rb', line 19

def initialize(dim)
  @dim = dim
  if block_given?
    @data = []
    dim.times do |d|
      val = yield(d)
      raise(KXI::Exceptions::InvalidTypeException.new(val.type, Numeric)) unless val.is_a?(Numeric)
      @data.push(val.to_f)
    end
  else
    @data = [0.0] * dim
  end
end

Instance Method Details

#*(other) ⇒ KXI::Math::Vector #*(other) ⇒ KXI::Math::Vector

Multiplies vector

Overloads:



157
158
159
160
161
162
163
164
# File 'lib/kxi/math/vector.rb', line 157

def *(other)
  if other.is_a?(Numeric)
    return KXI::Math::Vector.new(@dim) { |d| other * @data[d] }
  elsif other.is_a?(KXI::Math::Vector)
    return KXI::Math::Vector.new(@dim) { |d| @data[d] * other[d] }
  elsif raise(KXI::Exceptions::InvalidTypeException.new(other.class, Numeric, KXI::Math::Vector))
  end
end

#+(other) ⇒ KXI::Math::Vector

Adds vectors

Parameters:

  • other (Vector)

    Vector to add

Returns:

Raises:



120
121
122
123
124
125
126
# File 'lib/kxi/math/vector.rb', line 120

def +(other)
  if other.is_a?(KXI::Math::Vector)
    return KXI::Math::Vector.new(@dim) { |d| @data[d] + other[d] }
  else
    raise(KXI::Exceptions::InvalidTypeException.new(other.class, Numeric))
  end
end

#-(other) ⇒ KXI::Math::Vector

Subtracts vectors

Parameters:

  • other (Vector)

    Vector to subtract

Returns:

Raises:



132
133
134
135
136
137
138
# File 'lib/kxi/math/vector.rb', line 132

def -(other)
  if other.is_a?(KXI::Math::Vector)
    return KXI::Math::Vector.new(@dim) { |d| @data[d] - other[d] }
  else
    raise(KXI::Exceptions::InvalidTypeException.new(other.class, Numeric))
  end
end

#-@KXI::Math::Vector

Multiplies the vector with -1

Returns:



142
143
144
# File 'lib/kxi/math/vector.rb', line 142

def -@
  return KXI::Math::Vector.new(@dim) { |d| -@data[d] }
end

#==(other) ⇒ Boolean

Compares vector

Parameters:

  • other (void)

    Value to compare to

Returns:

  • (Boolean)

    True if vector is equivalent to given value; false otherwise



169
170
171
172
173
174
175
176
# File 'lib/kxi/math/vector.rb', line 169

def ==(other)
  return false unless other.is_a?(KXI::Math::Vector)
  return false if @dim != other.dimension
  @dim.times do |d|
    return false if @data[d] != other[d]
  end
  return true
end

#[](idx) ⇒ Numeric

Gets the value of vector at specific dimension

Parameters:

  • idx (Integer)

    Dimension to return

Returns:

  • (Numeric)

    Value of vector at given dimension

Raises:



41
42
43
44
45
# File 'lib/kxi/math/vector.rb', line 41

def [](idx)
  raise(KXI::Exceptions::OutOfRangeException.new(idx, 0, @dim - 1)) if idx < 0 or idx >= @dim
  raise(KXI::Exceptions::InvalidTypeException.new(value.type, Numeric)) unless value.is_a?(Numeric)
  return @data[idx]
end

#[]=(idx, val) ⇒ Numeric #[]=(idx, val) ⇒ Numeric

Sets the value of vector at specific dimension

Overloads:

  • #[]=(idx, val) ⇒ Numeric

    Sets the value of vector at specific dimension

    Parameters:

    • idx (Integer)

      Dimension to set

    • val (Integer)

      Value to set the dimension to

    Returns:

    • (Numeric)

      Value passed to function

    Raises:

  • #[]=(idx, val) ⇒ Numeric

    Sets the values of vector to range of values starting from specific dimension

    Parameters:

    • idx (Integer)

      Dimension to start at

    • val (Array)

      Values to set the dimensions to

    Returns:

    • (Numeric)

      Value passed to function

    Raises:

Raises:



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/kxi/math/vector.rb', line 62

def []=(idx, val)
  raise(KXI::Exceptions::OutOfRangeException.new(idx, 0, @dim - 1)) if idx < 0 or idx >= @dim
  if val.is_a?(Numeric)
    @data[idx] = val.to_f
  elsif val.is_a?(Array)
    i = 0
    while idx + i < @dim and i < val.length
      v = val[i]
      raise(KXI::Exceptions::InvalidTypeException.new(v.type, Numeric)) unless v.is_a?(Numeric)
      @data[idx + i] = v.to_f
    end
  else
    raise(KXI::Exceptions::InvalidTypeException.new(val.type, Numeric, Array))
  end
  return val
end

#dimensionInteger

Returns the dimension of vector

Returns:

  • (Integer)

    Dimension of vector



9
10
11
# File 'lib/kxi/math/vector.rb', line 9

def dimension
  @dim
end

#dot(vec) ⇒ Numeric

Dot multiplies vectors

Parameters:

Returns:

  • (Numeric)

    Dot product of vectors

Raises:

  • (KXI::Exception::DimensionMismatchException)


93
94
95
96
97
98
# File 'lib/kxi/math/vector.rb', line 93

def dot(vec)
  raise(KXI::Exception::DimensionMismatchException.new(vec.dimension, dimension)) unless vec.dimension == dimension
  sum = 0
  @dim.times { |d| sum += @data[d] * vec[d] }
  return sum
end

#each {|d, val| ... } ⇒ Object

Iterates over each element of vector

Yields:

  • (d, val)

    Iterator function

Yield Parameters:

  • d (Integer)

    Dimension passed to iterator

  • val (Numeric)

    Value at that specific dimension



83
84
85
86
87
88
# File 'lib/kxi/math/vector.rb', line 83

def each
  return unless block_given?
  @dim.times do |d|
    yield(d, @data[d])
  end
end

#to_s(d = true) ⇒ String

Converts vector to string

Parameters:

  • d (Boolean) (defaults to: true)

    Determines whether string should be decorated

Returns:

  • (String)

    String representation of vector



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/kxi/math/vector.rb', line 103

def to_s(d = true)
  just = 0
  @dim.times { |m| len = str(@data[m]).length; just = len if len > just }
  ret = ''
  @dim.times do |m|
    ret += $/ if m > 0
    ret += '|' if d
    ret += str(@data[m]).rjust(just, ' ')
    ret += '|' if d
  end
  return ret
end