Class: Array
- Inherits:
-
Object
- Object
- Array
- 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
Instance Method Summary collapse
- #at_rand ⇒ Object
- #average ⇒ Object
- #dot_product(v) ⇒ Object
- #find_index(&b) ⇒ Object
-
#map_with_index ⇒ Object
TODO 1.9.
- #rand_index ⇒ Object
- #shuffle ⇒ Object
- #shuffle! ⇒ Object
-
#stats ⇒ Object
TODO 1.9, use minmax.
- #stddev ⇒ Object
-
#swap_element_at_index!(index, value) ⇒ Object
Finds value and swaps it with the element at index.
- #to_table(colnames = nil) ⇒ Object
Instance Method Details
#at_rand ⇒ Object
51 52 53 |
# File 'lib/charlie/etc/monkey.rb', line 51 def at_rand self[Kernel.rand(size)] end |
#average ⇒ Object
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_index ⇒ Object
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_index ⇒ Object
47 48 49 |
# File 'lib/charlie/etc/monkey.rb', line 47 def rand_index Kernel.rand(size) end |
#shuffle ⇒ Object
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 |
#stats ⇒ Object
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 |
#stddev ⇒ Object
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 |