Class: KXI::Math::Vector
- Inherits:
-
Object
- Object
- KXI::Math::Vector
- Defined in:
- lib/kxi/math/vector.rb
Overview
Represents a vector
Instance Method Summary collapse
-
#*(other) ⇒ Object
Multiplies vector.
-
#+(other) ⇒ KXI::Math::Vector
Adds vectors.
-
#-(other) ⇒ KXI::Math::Vector
Subtracts vectors.
-
#-@ ⇒ KXI::Math::Vector
Multiplies the vector with -1.
-
#==(other) ⇒ Boolean
Compares vector.
-
#[](idx) ⇒ Numeric
Gets the value of vector at specific dimension.
-
#[]=(idx, val) ⇒ Object
Sets the value of vector at specific dimension.
-
#dimension ⇒ Integer
Returns the dimension of vector.
-
#dot(vec) ⇒ Numeric
Dot multiplies vectors.
-
#each {|d, val| ... } ⇒ Object
Iterates over each element of vector.
-
#initialize(dim) {|d| ... } ⇒ Vector
constructor
Instantiates the Vector class.
-
#to_s(d = true) ⇒ String
Converts vector to string.
Constructor Details
#initialize(dim) {|d| ... } ⇒ Vector
Instantiates the KXI::Math::Vector class
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
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
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
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
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
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
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
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 |
#dimension ⇒ Integer
Returns the dimension of vector
9 10 11 |
# File 'lib/kxi/math/vector.rb', line 9 def dimension @dim end |
#dot(vec) ⇒ Numeric
Dot multiplies vectors
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
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
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 |