Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/charlie/etc/minireport.rb,
lib/charlie/etc/monkey.rb

Overview

This was handled by ruport earlier, but that turned out to be overkill and give a huge amount of dependencies.

Direct Known Subclasses

Population, Table

Instance Method Summary collapse

Instance Method Details

#at_randObject



51
52
53
# File 'lib/charlie/etc/monkey.rb', line 51

def at_rand
  self[Kernel.rand(size)]
end

#averageObject



60
61
62
# File 'lib/charlie/etc/monkey.rb', line 60

def average
  sum.to_f / size
end

#dot_product(v) ⇒ Object



41
42
43
44
45
# File 'lib/charlie/etc/monkey.rb', line 41

def dot_product(v)
  r=0.0
  each_with_index{|e,i| r+=e*v[i] }
  r
end

#find_index(&b) ⇒ Object



38
# File 'lib/charlie/etc/monkey.rb', line 38

def find_index(&b); index find(&b); end

#map_with_indexObject

TODO 1.9



69
70
71
72
73
# File 'lib/charlie/etc/monkey.rb', line 69

def map_with_index # TODO 1.9
 r=[]
 each_with_index{|e,i| r << yield(e,i) }
 r
end

#rand_indexObject



47
48
49
# File 'lib/charlie/etc/monkey.rb', line 47

def rand_index
  Kernel.rand(size)
end

#shuffleObject



36
# File 'lib/charlie/etc/monkey.rb', line 36

def shuffle ; sort_by { rand }; end

#shuffle!Object



37
# File 'lib/charlie/etc/monkey.rb', line 37

def shuffle!; replace shuffle; end

#statsObject

TODO 1.9, use minmax



55
56
57
58
# File 'lib/charlie/etc/monkey.rb', line 55

def stats # TODO 1.9, use minmax
  return transpose.map(&:stats).transpose if at(0).is_a?(Array) # return stats of each component if elements are arrays
  [min,max,average,stddev]
end

#stddevObject



64
65
66
67
# File 'lib/charlie/etc/monkey.rb', line 64

def stddev
  mu = average
  Math.sqrt( map{|x| (x-mu)*(x-mu) }.sum / size )
end

#swap_element_at_index!(index, value) ⇒ Object

Finds value and swaps it with the element at index.



76
77
78
79
# File 'lib/charlie/etc/monkey.rb', line 76

def swap_element_at_index!(index, value)
  old_element_index = self.index(value)
  self[index], self[old_element_index] = value, self[index]
end

#to_table(colnames = nil) ⇒ Object



5
6
7
# File 'lib/charlie/etc/minireport.rb', line 5

def to_table(colnames=nil)
  Table.new(self,colnames)
end