Class Vector
In: aprl/base/ap_lina.rb
Parent: Array
Vector Matrix Array dot/f_26.png

Methods

*   +@   -@   -@   add   add   angle_to   center   collect_together   collect_together!   collect_together!   convolve   dot   dot   each_index   each_iv   each_together   equals?   fill   from   from_angle   from_array   index_collect   index_collect!   iv_collect   len   mul   neg   new   norm   norm!   scale   scale   sub   sub   sum_iv   to_s  

Public Class methods

[Source]

# File aprl/base/ap_lina.rb, line 52
  def Vector.add(x,y)
    z=Vector.new
    y.length.times do |i|
      z.push x[i].to_f+y[i].to_f
    end
    z
  end

center of a number of vectors

[Source]

# File aprl/base/ap_lina.rb, line 228
  def Vector.center(a)
    sum=Vector.from_array(a[0])
    a.each1 do |x|
      sum.add(x)
    end
    sum.scale(1.0/a.length.to_f)
  end

dot product

[Source]

# File aprl/base/ap_lina.rb, line 85
  def Vector.dot(a,b)
    case a.length
    when 2
      a[0]*b[1]-a[1]*b[0]
    else
      0
    end
  end

init from list of values

[Source]

# File aprl/base/ap_lina.rb, line 44
  def Vector.from(*a)
    v=Vector.new
    a.each do |a|
      v.push a #.to_f

    end
    v
  end

[Source]

# File aprl/base/ap_lina.rb, line 236
  def Vector.from_angle(a)
    z = Vector.new(2) 
    z[0]=Math.cos(a) 
    z[1]=Math.sin(a)
    z
  end

init from array of values

[Source]

# File aprl/base/ap_lina.rb, line 35
  def Vector.from_array(a)
    v=Vector.new
    a.each do |a|
      v.push a #.to_f

    end
    v
  end

[Source]

# File aprl/base/ap_lina.rb, line 68
  def Vector.mul(x,y)
    z=0.0
    y.length.times do |i|
      z=z+x[i].to_f*y[i].to_f   
    end
    z
  end

[Source]

# File aprl/base/ap_lina.rb, line 30
  def initialize(dim=0)
    super(dim,0.0)
  end

[Source]

# File aprl/base/ap_lina.rb, line 76
  def Vector.scale(x,f)
    z=Vector.new
    x.length.times do |i|
      z.push x[i].to_f*f.to_f   
    end
    z
  end

[Source]

# File aprl/base/ap_lina.rb, line 60
  def Vector.sub(x,y)
    z=Vector.new
    y.length.times do |i|
      z.push x[i].to_f-y[i].to_f
    end
    z
  end

Public Instance methods

[Source]

# File aprl/base/ap_lina.rb, line 116
  def *(y)
    if y.kind_of?(Array)
      Vector.mul(self,y)
    else
      Vector.scale(self,y)
    end    
  end

[Source]

# File aprl/base/ap_lina.rb, line 278
  def +@(y)
    Vector.add(self,y)
  end

[Source]

# File aprl/base/ap_lina.rb, line 282
  def -@(y)
    Vector.sub(self,y)
  end

[Source]

# File aprl/base/ap_lina.rb, line 266
  def -@()
    neg()
  end

+= add a vector to ourselves

[Source]

# File aprl/base/ap_lina.rb, line 271
  def add(y)
    y.length.times do |i|
      self[i]=self[i].to_f+y[i].to_f
    end
    self
  end

angle between vectors

[Source]

# File aprl/base/ap_lina.rb, line 299
  def angle_to(x)
    Math.acos(norm()*(x.norm))
  end

[Source]

# File aprl/base/ap_lina.rb, line 194
  def collect_together(o)
    ret=Vector.new
    each_index do |i| 
      break if i>o.length 
      ret.push(yield(self[i],o[i]))
    end
    ret
  end

[Source]

# File aprl/base/ap_lina.rb, line 219
  def collect_together!(v,w)
    each_index do |i| 
      break if i>o.length 
      self[i]=yield(v[i],w[i])
    end
    self    
  end

[Source]

# File aprl/base/ap_lina.rb, line 211
  def collect_together!(o)
    each_index do |i| 
      break if i>o.length 
      self[i]=yield(self[i],o[i])
    end
    self    
  end

parallel iteration, allows for easy convolution realize u=vXw as u = v.convolve(w), give block for other parallel calculation e.g. u*w = v.convolve(w,0.0) { |vi,wi,sum| sum+vi*wi }

[Source]

# File aprl/base/ap_lina.rb, line 154
  def convolve(other,start_value=0.0)
    result = start_value
    if block_given?
      each_iv do |index,value| 
        break if index>other.length 
        result=yield(value,other[index],result)
      end
    else
      each_iv do |index,value| 
        break if index>other.length 
        result=result+value*o[index]
      end
    end
    result
  end

dot product

[Source]

# File aprl/base/ap_lina.rb, line 294
  def dot(x)
    Vector.dot(self,x)
  end

[Source]

# File aprl/base/ap_lina.rb, line 124
  def each_index
    length.times { |i| yield i }
  end

yield index, value

[Source]

# File aprl/base/ap_lina.rb, line 145
  def each_iv
    length.times { |i| yield i,self[i] }
  end

enum togetor wit other vector o

[Source]

# File aprl/base/ap_lina.rb, line 180
  def each_together(o)
    each_iv do |i,v| 
      yield(v,o[i])
    end    
  end

[Source]

# File aprl/base/ap_lina.rb, line 203
  def equals?(o)
    each_index do |i| 
      return false if i>o.length 
      return false if self[i]!=o[i]
    end
    return true
  end

fill by list

[Source]

# File aprl/base/ap_lina.rb, line 187
  def fill(*a)
    collect!{ a.shift }
    while a.length>0
      push(a.shift)
    end
  end

collect, yielding index

[Source]

# File aprl/base/ap_lina.rb, line 135
  def index_collect
    z = Vector.new; length.times { |i| z.push yield(i) }; z
  end

collect, yielding index

[Source]

# File aprl/base/ap_lina.rb, line 129
  def index_collect!
    length.times { |i| self[i]=yield(i) }
    self
  end

collect, yielding index, value

[Source]

# File aprl/base/ap_lina.rb, line 140
  def iv_collect
    z = Vector.new; length.times { |i| z.push yield(i,self[i]) }; z
  end

[Source]

# File aprl/base/ap_lina.rb, line 243
  def len
    sq=Vector.mul(self,self)
    Math.sqrt(sq)
  end

[Source]

# File aprl/base/ap_lina.rb, line 258
  def neg()
    z=Vector.new
    each do |v|
      z.push(-v.to_f)
    end
    z
  end

[Source]

# File aprl/base/ap_lina.rb, line 248
  def norm
    sq=len
    self.scale(1.0/sq)
  end

[Source]

# File aprl/base/ap_lina.rb, line 253
  def norm!
    sq=len
    scale(1.0/sq)
  end

[Source]

# File aprl/base/ap_lina.rb, line 112
  def scale(f)
    Vector.scale(self,f)
  end

[Source]

# File aprl/base/ap_lina.rb, line 286
  def sub(y)
    y.length.times do |i|
      self[i] = self[i].to_f-y[i].to_f
    end
    self
  end

sumarize, yielding index, value

[Source]

# File aprl/base/ap_lina.rb, line 171
  def sum_iv
    z = 0.0
    each_iv do |i,v| 
      z=z+yield(i,v)
    end
    z
  end

to_s with optional formatting

[Source]

# File aprl/base/ap_lina.rb, line 96
  def to_s(fmt=nil)
    s='('
    if fmt!=nil # loop unrolled for efficiency

      length.times do |d|
        s=s+',' if d>0
        s=s+sprintf(fmt,self[d])
      end
    else
      length.times do |d|
        s=s+',' if d>0
        s=s+self[d].to_s
      end
    end
    s+')'    
  end

[Validate]