class CooCoo::Ruby::Vector

Public Class Methods

[](value, max_size = nil, default_value = 0.0) click to toggle source
# File lib/coo-coo/math.rb, line 21
def self.[](value, max_size = nil, default_value = 0.0)
  if value.respond_to?(:[])
    v = new(max_size || value.size, default_value) do |i|
      value[i].to_f || default_value
    end
  else
    v = new(max_size || value.size, default_value) do |i|
      begin
        value.next.to_f || default_value
      rescue StopIteration
        default_value
      end
    end
  end
end
new(length, initial_value = 0.0, &block) click to toggle source
# File lib/coo-coo/math.rb, line 11
def initialize(length, initial_value = 0.0, &block)
  raise ArgumentError.new("Invalid size for a Vector") if length <= 0
  
  if block_given? # eat ruby's warning
    @elements = Array.new(length, &block)
  else
    @elements = Array.new(length, initial_value)
  end
end

Public Instance Methods

!=(other) click to toggle source
# File lib/coo-coo/math.rb, line 270
def !=(other)
  !(self == other)
end
*(other) click to toggle source
# File lib/coo-coo/math.rb, line 217
def *(other)
  v = if other.respond_to?(:each)
        raise ArgumentError.new("Size mismatch") if size != other.size
        other.each.zip(each).collect do |oe, se|
      se * oe
    end
      else
        each.collect do |e|
      e * other
    end
      end

  self.class[v]
end
**(other) click to toggle source
# File lib/coo-coo/math.rb, line 232
def **(other)
  v = if other.respond_to?(:each)
        raise ArgumentError.new("Size mismatch") if size != other.size
        other.each.zip(each).collect do |oe, se|
      se ** oe
    end
      else
        each.collect do |e|
      e ** other
    end
      end

  self.class[v]
end
+(other) click to toggle source
# File lib/coo-coo/math.rb, line 175
def +(other)
  v = if other.respond_to?(:each)
        raise ArgumentError.new("Size mismatch") if size != other.size
        other.each.zip(each).collect do |oe, se|
      se + oe
    end
      else
        each.collect do |e|
      e + other
    end
      end
  
  self.class[v]
end
-(other) click to toggle source
# File lib/coo-coo/math.rb, line 194
def -(other)
  v = if other.respond_to?(:each)
        raise ArgumentError.new("Size mismatch: #{size} != #{other.size}") if size != other.size
        other.each.zip(each).collect do |oe, se|
      se - oe
    end
      else
        each.collect do |e|
      e - other
    end
      end
  
  self.class[v]
end
-@() click to toggle source
# File lib/coo-coo/math.rb, line 190
def -@
  self * -1.0
end
/(other) click to toggle source
# File lib/coo-coo/math.rb, line 247
def /(other)
  v = if other.respond_to?(:each)
        raise ArgumentError.new("Size mismatch") if size != other.size
        other.each.zip(each).collect do |oe, se|
      se / oe
    end
      else
        each.collect do |e|
      e / other
    end
      end

  self.class[v]
end
==(other) click to toggle source
# File lib/coo-coo/math.rb, line 262
def ==(other)
  other && size == other.size && each.zip(other.each).all? do |a, b|
    a == b || (a.nan? && b.nan?)
  end || false
rescue NoMethodError
  false
end
[](i, len = nil) click to toggle source
# File lib/coo-coo/math.rb, line 57
def [](i, len = nil)
  i = size + i if i < 0
  raise RangeError.new if i >= size || i < 0

  v = @elements[i, len || 1]

  if len
    self.class[v]
  elsif v
    v[0]
  end
end
[]=(i, l, v = nil) click to toggle source
# File lib/coo-coo/math.rb, line 70
def []=(i, l, v = nil)
  i = size + i if i < 0
  raise RangeError.new if i >= size || i < 0

  if v
    @elements[i, l] = v
  else
    @elements[i] = l
  end
end
append(other) click to toggle source
# File lib/coo-coo/math.rb, line 120
def append(other)
  v = self.class.new(size + other.size)
  each_with_index do |e, i|
    v[i] = e
  end
  other.each_with_index do |e, i|
    v[i + size] = e
  end
  v
end
coerce(other) click to toggle source
# File lib/coo-coo/math.rb, line 37
def coerce(other)
  if other.respond_to?(:each)
    return self.class[other], self
  else
    return self.class.new(self.size, other), self
  end
end
dot(width, height, other, owidth = nil, oheight = nil) click to toggle source
# File lib/coo-coo/math.rb, line 147
def dot(width, height, other, owidth = nil, oheight = nil)
  if other.kind_of?(self.class) || other.respond_to?(:[])
    owidth ||= width
    oheight ||= height

    if width * height != size
      raise ArgumentError.new("width & height, #{width}x#{height} don't match our size: #{size}")
    end
    if owidth * oheight != other.size
      raise ArgumentError.new("owidth & oheight, #{owidth}x#{oheight} don't match the argument's size: #{other.size}")
    end

    if width != oheight
      raise ArgumentError.new("argument's height != this' width")
    end

    self.class[height.times.collect do |row|
                 owidth.times.collect do |col|
                   oheight.times.collect do |i|
                     self[row * width + i] * other[i * owidth + col]
                   end.sum
                 end
               end.flatten]
  else
    raise ArgumentError.new("argument must be a #{self.class} or enumerable")
  end
end
each(&block) click to toggle source
# File lib/coo-coo/math.rb, line 92
def each(&block)
  @elements.each(&block)
end
each_slice(n, &block) click to toggle source
# File lib/coo-coo/math.rb, line 100
def each_slice(n, &block)
  if block
    num_slices = (size / n.to_f).ceil.to_i
    
    @elements.each_slice(n).with_index do |slice, i|
      block.call(self.class[slice, n])
    end
  else
    to_enum(__method__, n)
  end
end
each_with_index(&block) click to toggle source
# File lib/coo-coo/math.rb, line 96
def each_with_index(&block)
  each.each_with_index(&block)
end
length() click to toggle source
# File lib/coo-coo/math.rb, line 213
def length
  @elements.size
end
magnitude() click to toggle source
# File lib/coo-coo/math.rb, line 139
def magnitude
  magnitude_squared.sqrt
end
magnitude_squared() click to toggle source
# File lib/coo-coo/math.rb, line 135
def magnitude_squared
  (self * self).sum
end
normalize() click to toggle source
# File lib/coo-coo/math.rb, line 143
def normalize
  self / magnitude
end
resize(new_size) click to toggle source
# File lib/coo-coo/math.rb, line 112
def resize(new_size)
  if new_size > size
    @elements = @elements + Array.new(new_size - size)
  elsif new_size < size
    @elements = @elements[0, new_size]
  end
end
set(values) click to toggle source
# File lib/coo-coo/math.rb, line 81
def set(values)
  values = [ values ].cycle(size) if values.kind_of?(Numeric)
  
  values.each_with_index do |v, i|
    break if i >= @elements.size
    @elements[i] = v
  end

  self
end
size() click to toggle source
# File lib/coo-coo/math.rb, line 209
def size
  @elements.size
end
sum() click to toggle source
# File lib/coo-coo/math.rb, line 131
def sum
  @elements.each.sum
end
to_a() click to toggle source
# File lib/coo-coo/math.rb, line 45
def to_a
  @elements
end
to_s() click to toggle source
# File lib/coo-coo/math.rb, line 49
def to_s
  values = each.collect do |e|
    e.to_s
  end

  "[#{values.join(', ')}]"
end