class KXI::Math::Vector

Represents a vector

Public Class Methods

new(dim) { |d| ... } click to toggle source

Instantiates the {KXI::Math::Vector} class @param [Integer] dim Dimension of vector @yield [d] Generator function @yieldparam Dimension to generate @yieldreturn [Numeric] Result of generator @raise [KXI::Exceptions::InvalidTypeException] When generator function returns value of invalid type

# 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

Public Instance Methods

*(other) click to toggle source

Multiplies vector @overload *(other)

Preforms scalar-vector multiplication
@param [Numeric] other Scalar to multiply with
@raise [KXI::Exceptions::InvalidTypeException] When other value is of invalid type
@return [KXI::Math::Vector] New vector equivalent to the scaled vector

@overload *(other)

Preforms vector-vector multiplication
@param [Numeric] other Vector to multiply with
@raise [KXI::Exceptions::InvalidTypeException] When other value is of invalid type
@return [KXI::Math::Vector] New vector equivalent to the multiplied vector
# 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) click to toggle source

Adds vectors @param [Vector] other Vector to add @raise [KXI::Exceptions::InvalidTypeException] When type of value is invalid @return [KXI::Math::Vector] New vector equivalent to the sum of vectors

# 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) click to toggle source

Subtracts vectors @param [Vector] other Vector to subtract @raise [KXI::Exceptions::InvalidTypeException] When type of value is invalid @return [KXI::Math::Vector] New vector equivalent to the difference of vectors

# 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
-@() click to toggle source

Multiplies the vector with -1 @return [KXI::Math::Vector] New opposite vector

# File lib/kxi/math/vector.rb, line 142
def -@
        return KXI::Math::Vector.new(@dim) { |d| -@data[d] }
end
==(other) click to toggle source

Compares vector @param [void] other Value to compare to @return [Boolean] True if vector is equivalent to given value; false otherwise

# 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) click to toggle source

Gets the value of vector at specific dimension @param [Integer] idx Dimension to return @raise [KXI::Exceptions::OutOfRangeException] When dimension is out of range @return [Numeric] Value of vector at given dimension

# 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) click to toggle source

Sets the value of vector at specific dimension @overload []=(idx, val)

Sets the value of vector at specific dimension
@param [Integer] idx Dimension to set
@param [Integer] val Value to set the dimension to
@raise [KXI::Exceptions::OutOfRangeException] When dimension is out of range
@raise [KXI::Exceptions::InvalidTypeException] When value has invalid type
@return [Numeric] Value passed to function

@overload []=(idx, val)

Sets the values of vector to range of values starting from specific dimension
@param [Integer] idx Dimension to start at
@param [Array] val Values to set the dimensions to
@raise [KXI::Exceptions::OutOfRangeException] When dimension is out of range
@raise [KXI::Exceptions::InvalidTypeException] When value has invalid type
@return [Numeric] Value passed to function
# 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() click to toggle source

Returns the dimension of vector @return [Integer] Dimension of vector

# File lib/kxi/math/vector.rb, line 9
def dimension
        @dim
end
dot(vec) click to toggle source

Dot multiplies vectors @param [KXI::Math::Vector] vec Vector to multiply with @return [Numeric] Dot product of vectors

# 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, data| ... } click to toggle source

Iterates over each element of vector @yield [d, val] Iterator function @yieldparam [Integer] d Dimension passed to iterator @yieldparam [Numeric] val Value at that specific dimension

# 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) click to toggle source

Converts vector to string @param [Boolean] d Determines whether string should be decorated @return [String] String representation of vector

# 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

Private Instance Methods

str(num) click to toggle source
# File lib/kxi/math/vector.rb, line 33
def str(num)
        return (num == num.to_i ? num.to_i : num).to_s
end