class CooCoo::Math::AbstractVector

Public Class Methods

ones(length) click to toggle source
# File lib/coo-coo/math/abstract_vector.rb, line 15
def self.ones(length)
  new(length, 1.0)
end
rand(length, range = nil) click to toggle source
# File lib/coo-coo/math/abstract_vector.rb, line 4
def self.rand(length, range = nil)
  new(length) do |i|
    args = [ range ] if range
    Random.rand(*args)
  end
end
zeros(length) click to toggle source
# File lib/coo-coo/math/abstract_vector.rb, line 11
def self.zeros(length)
  new(length, 0.0)
end

Public Instance Methods

collect_equal?(n) click to toggle source
# File lib/coo-coo/math/abstract_vector.rb, line 88
def collect_equal?(n)
  if n.respond_to?(:each)
    self.class[each.zip(n).collect { |a, b| a == b ? 1.0 : 0.0 }]
  else
    self.class[each.collect { |e| e == n ? 1.0 : 0.0 }]
  end
end
collect_infinite?() click to toggle source
# File lib/coo-coo/math/abstract_vector.rb, line 112
def collect_infinite?
  self.class[each.collect { |e| e.infinite? ? 1.0 : 0.0 }]
end
collect_nan?() click to toggle source
# File lib/coo-coo/math/abstract_vector.rb, line 104
def collect_nan?
  self.class[each.collect { |e| e.nan? ? 1.0 : 0.0 }]
end
collect_not_equal?(n) click to toggle source
# File lib/coo-coo/math/abstract_vector.rb, line 96
def collect_not_equal?(n)
  if n.respond_to?(:each)
    self.class[each.zip(n).collect { |a, b| a != b ? 1.0 : 0.0 }]
  else
    self.class[each.collect { |e| e != n ? 1.0 : 0.0 }]
  end
end
infinite?() click to toggle source
# File lib/coo-coo/math/abstract_vector.rb, line 116
def infinite?
  each.any?(&:infinite?)
end
max() click to toggle source
# File lib/coo-coo/math/abstract_vector.rb, line 23
def max
  minmax[1]
end
min() click to toggle source
# File lib/coo-coo/math/abstract_vector.rb, line 27
def min
  minmax[0]
end
minmax() click to toggle source
# File lib/coo-coo/math/abstract_vector.rb, line 31
def minmax
  each.minmax
end
minmax_normalize(use_zeros = false) click to toggle source
# File lib/coo-coo/math/abstract_vector.rb, line 35
def minmax_normalize(use_zeros = false)
  min, max = minmax
  delta = (max - min)
  if use_zeros && delta == 0.0
    zero
  else
    (self - min) / delta
  end
end
nan?() click to toggle source
# File lib/coo-coo/math/abstract_vector.rb, line 108
def nan?
  each.any?(&:nan?)
end
set2d!(width, src, src_width, x, y) click to toggle source
# File lib/coo-coo/math/abstract_vector.rb, line 73
def set2d!(width, src, src_width, x, y)
  raise ArgumentError.new("src's size needs to be a multiple of the width") if src.kind_of?(self.class) && src.size % src_width > 0
  
  src.each_slice(src_width).with_index do |row, i|
    index = (y+i) * width + x
    next if index >= size
    row.each_with_index do |p, px|
      break if (x + px) >= width
      self[index.to_i + px] = p
    end
  end

  self
end
slice_2d(src_width, src_height, origin_x, origin_y, width, height, initial = 0.0) click to toggle source
# File lib/coo-coo/math/abstract_vector.rb, line 51
def slice_2d(src_width, src_height, origin_x, origin_y, width, height, initial = 0.0)
  samples = height.times.collect do |y|
    py = origin_y + y

    width.times.collect do |x|
      px = origin_x + x
      if px >= 0 && px < src_width
        i = py * src_width + px
        if i >= 0 && i < size
          self[i]
        else
          initial
        end
      else
        initial
      end
    end
  end.flatten

  self.class[samples]
end
zero() click to toggle source
# File lib/coo-coo/math/abstract_vector.rb, line 19
def zero
  self.class.zeros(size)
end