class CooCoo::NMatrix::Vector

Attributes

elements[R]

Public Class Methods

[](value, max_size = nil, default_value = 0.0) click to toggle source
# File lib/coo-coo/math.rb, line 333
def self.[](value, max_size = nil, default_value = 0.0)
  if value.kind_of?(::NMatrix)
    v = new(nil)
    v.instance_variable_set('@elements', value)
    v
  elsif value.respond_to?(:[])
    v = new(max_size || value.size, default_value) do |i|
      value[i] || default_value
    end
  else
    v = new(max_size || value.size, default_value) do |i|
      begin
        value.next || default_value
      rescue StopIteration
        default_value
      end
    end
  end
end
_load(args) click to toggle source
# File lib/coo-coo/math.rb, line 381
def self._load(args)
  arr = args.unpack('E*')
  self[arr]
end
new(length, initial_value = 0.0, &block) click to toggle source
# File lib/coo-coo/math.rb, line 319
def initialize(length, initial_value = 0.0, &block)
  if length != nil
    if length <= 0
      raise ArgumentError.new("size must be larger than zero")
    end
    @elements = ::NMatrix.new([ 1, length ], initial_value)
    if block
      @elements.size.times do |i|
        @elements[i] = block.call(i)
      end
    end
  end
end
ones(length) click to toggle source
# File lib/coo-coo/math.rb, line 357
def self.ones(length)
  self[::NMatrix.ones([1, length])]
end
zeros(length) click to toggle source
# File lib/coo-coo/math.rb, line 353
def self.zeros(length)
  self[::NMatrix.zeros([1, length])]
end

Public Instance Methods

*(other) click to toggle source
# File lib/coo-coo/math.rb, line 525
def *(other)
  if other.kind_of?(self.class)
    self.class[@elements * other.elements]
  elsif other.kind_of?(Numeric)
    self.class[@elements * other]
  else
    self * self.class[other]
  end
end
**(other) click to toggle source
# File lib/coo-coo/math.rb, line 535
def **(other)
  if other.kind_of?(self.class)
    self.class[@elements ** other.elements]
  elsif other.kind_of?(Numeric)
    self.class[@elements ** other]
  else
    self ** self.class[other]
  end
end
+(other) click to toggle source
# File lib/coo-coo/math.rb, line 493
def +(other)
  if other.kind_of?(self.class)
    self.class[@elements + other.elements]
  elsif other.kind_of?(Numeric)
    self.class[@elements + other]
  else
    self + self.class[other]
  end
end
-(other) click to toggle source
# File lib/coo-coo/math.rb, line 507
def -(other)
  if other.kind_of?(self.class)
    self.class[@elements - other.elements]
  elsif other.kind_of?(Numeric)
    self.class[@elements - other]
  else
    self - self.class[other]
  end
end
-@() click to toggle source
# File lib/coo-coo/math.rb, line 503
def -@
  self * -1.0
end
/(other) click to toggle source
# File lib/coo-coo/math.rb, line 545
def /(other)
  if other.kind_of?(self.class)
    self.class[@elements / other.elements]
  elsif other.kind_of?(Numeric)
    self.class[@elements / other]
  else
    self / self.class[other]
  end
end
==(other) click to toggle source
# File lib/coo-coo/math.rb, line 555
def ==(other)
  if other.kind_of?(self.class)
    size == other.size && @elements == other.elements
  elsif other != nil
    a, b = coerce(other)
    a == b
  else
    false
  end
end
[](i, len = nil) click to toggle source
# File lib/coo-coo/math.rb, line 386
def [](i, len = nil)
  i = size + i if i < 0
  raise RangeError.new if i >= size || i < 0

  if len
    len = (size - i) if (i + len) >= size
    raise ArgumentError.new("length must be > 0") if len <= 0
  end
  
  v = @elements[0, (i...(i + (len || 1))) ]
  
  if len
    self.class[v]
  else
    v[0]
  end
end
[]=(i, l, v = nil) click to toggle source
# File lib/coo-coo/math.rb, line 404
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
  # @elements[i] = v
end
_dump(depth) click to toggle source
# File lib/coo-coo/math.rb, line 377
def _dump(depth)
  @elements.to_a.pack('E*')
end
append(other) click to toggle source
# File lib/coo-coo/math.rb, line 427
def append(other)
  if other.kind_of?(self.class)
    self.class[@elements.concat(other.elements)]
  else
    append(self.class[other])
  end
end
coerce(other) click to toggle source
# File lib/coo-coo/math.rb, line 361
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, oheight) click to toggle source
# File lib/coo-coo/math.rb, line 475
def dot(width, height, other, owidth, oheight)
  owidth ||= width
  oheight ||= height
  
  if other.kind_of?(self.class)
    raise ArgumentError.new("invalid size") if other.size != owidth * oheight
    raise ArgumentError.new("invalid size") if size != width * height

    product = @elements.reshape([ height, width ]).
      dot(other.elements.reshape([ oheight, owidth ]))
    
    self.class[product.
               reshape([1, height * owidth ])]
  else
    self.dot(width, height, self.class[other], owidth, oheight)
  end
end
each(&block) click to toggle source
# File lib/coo-coo/math.rb, line 435
def each(&block)
  @elements.each(&block)
end
each_slice(n, &block) click to toggle source
# File lib/coo-coo/math.rb, line 443
def each_slice(n, &block)
  if block
    last_slice = (size / n.to_f).ceil.to_i
    
    @elements.each_slice(n).with_index do |slice, i|
      if i == last_slice - 1
        slice = slice + Array.new(n - slice.size)
      end
      
      block.call(self.class[slice])
    end
  else
    to_enum(__method__, n)
  end
end
each_with_index(&block) click to toggle source
# File lib/coo-coo/math.rb, line 439
def each_with_index(&block)
  @elements.each_with_index(&block)
end
length() click to toggle source
# File lib/coo-coo/math.rb, line 521
def length
  @elements.shape[1]
end
magnitude() click to toggle source
# File lib/coo-coo/math.rb, line 467
def magnitude
  magnitude_squared.sqrt
end
magnitude_squared() click to toggle source
# File lib/coo-coo/math.rb, line 463
def magnitude_squared
  (self * self).sum
end
normalize() click to toggle source
# File lib/coo-coo/math.rb, line 471
def normalize
  self / magnitude
end
set(values) click to toggle source
# File lib/coo-coo/math.rb, line 416
def set(values)
  values = [ values ].each.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 517
def size
  length
end
sum() click to toggle source
# File lib/coo-coo/math.rb, line 459
def sum
  @elements.each.sum
end
to_a() click to toggle source
# File lib/coo-coo/math.rb, line 369
def to_a
  @elements.to_a
end
to_s() click to toggle source
# File lib/coo-coo/math.rb, line 373
def to_s
  "[" + to_a.join(", ") + "]"
end