Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/arraymethods.rb
Overview
require ‘narray’
Instance Method Summary collapse
-
#*(arg) ⇒ Object
Scalar product.
- #mean ⇒ Object
-
#percentile(percent) ⇒ Object
def median NArray.to_na(self).median end.
- #stddev ⇒ Object
- #sum ⇒ Object
- #to_f ⇒ Object
- #to_i ⇒ Object
-
#variance ⇒ Object
Mean square deviation from mean.
Instance Method Details
#*(arg) ⇒ Object
Scalar product
39 40 41 42 43 44 45 46 |
# File 'lib/arraymethods.rb', line 39 def *(arg) if self.size != arg.size raise "Not equal sizes in scalar product" end self.zip(arg).inject(0.0) do |s,(i,j)| s + i * j end end |
#mean ⇒ Object
8 9 10 |
# File 'lib/arraymethods.rb', line 8 def mean self.sum.to_f / self.size end |
#percentile(percent) ⇒ Object
def median
NArray.to_na(self).median
end
16 17 18 19 20 21 22 23 |
# File 'lib/arraymethods.rb', line 16 def percentile(percent) if percent == 0 [self.min,self.max] else sorted = self.sort [ sorted[(percent/100.0*self.size).round], sorted[((100-percent)/100.0*self.size).round] ] end end |
#stddev ⇒ Object
25 26 27 |
# File 'lib/arraymethods.rb', line 25 def stddev Math.sqrt( self.variance ) end |
#sum ⇒ Object
4 5 6 |
# File 'lib/arraymethods.rb', line 4 def sum self.inject(0) { |s,i| s + i } end |
#to_f ⇒ Object
52 53 54 |
# File 'lib/arraymethods.rb', line 52 def to_f self.map{|i| i.to_f} end |
#to_i ⇒ Object
48 49 50 |
# File 'lib/arraymethods.rb', line 48 def to_i self.map{|i| i.to_i} end |
#variance ⇒ Object
Mean square deviation from mean
30 31 32 33 34 35 36 |
# File 'lib/arraymethods.rb', line 30 def variance m = self.mean sum = self.inject(0) do |s,i| s + (i-m)**2 end sum / self.size.to_f end |