Module: EnumerableNumerics

Included in
ArrayOfNumerics
Defined in
lib/kyanite/enumerable/enumerable_numerics.rb

Enumeration Of Numerics

Kyanite definitions

EnumerableNumerics

Kyanite class with module included

ArrayOfNumerics

Kyanite tests and examples

TestKyaniteEnumerableNumerics

Usage

require 'kyanite/enumerable/enumerable_numerics'

Mean Values (collapse)

Sum, Product, Parallel (collapse)

Instance Method Details

- (Float) mean Also known as: avg, average, mean_arithmetric

Arithmetic mean

Tests and examples here.

Returns:



24
25
26
# File 'lib/kyanite/enumerable/enumerable_numerics.rb', line 24

def mean
  self.inject(0.0) { |sum, i | sum += i } / self.length.to_f 
end

- (Float) mean_geometric

Geometric mean

Returns:



77
78
79
# File 'lib/kyanite/enumerable/enumerable_numerics.rb', line 77

def mean_geometric
  self.prd ** ( 1.0/self.size ) 
end

- (Float) mean_harmonic(options = {})

Harmonic mean

Usually, the harmonic mean is defined only for positive numbers. The option :allow_negative => true can also include negative numbers in the calculation. Then the result will be a weighted arithmetic mean of the harmonic mean of all positive elements and the harmonic mean of all negative elements.

Returns:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/kyanite/enumerable/enumerable_numerics.rb', line 39

def mean_harmonic( options={} )
  return 0              if self.empty?  
  return self.first     if self.size == 1
  allow_negative = options[:allow_negative] || false
  unless allow_negative
    summe = 0
    self.each { |x| summe += ( 1.0/x ) }
    return self.size / summe
    
  else
    positives = ArrayOfNumerics.new
    negatives = ArrayOfNumerics.new
    self.each do |e|
      if e >= 0
        positives << e
      else
        negatives << -e
      end
    end #each
    if positives.size > 0 
      if negatives.size > 0
        return  ( positives.mean_harmonic(:allow_negative => false) * positives.size -
                  negatives.mean_harmonic(:allow_negative => false) * negatives.size
                ) / (positives.size + negatives.size).to_f
      else
        return  positives.mean_harmonic(:allow_negative => false)
      end
    else
      return   -negatives.mean_harmonic(:allow_negative => false)
    end

  end #if allow_negative
end

- (Float) parallel

Parallel

Result is equal to the total resistance of resistors in parallel circuits. Tests and examples here.

Returns:



112
113
114
# File 'lib/kyanite/enumerable/enumerable_numerics.rb', line 112

def parallel
  mean_harmonic / size  
end

- (Numeric) prd

Product

Tests and examples here.

Returns:



102
103
104
105
# File 'lib/kyanite/enumerable/enumerable_numerics.rb', line 102

def prd
  # Methode darf nicht product heißen, die gibt es nämlich schon.  
  self.inject(1.0) { |p, i | p *= i }  
end

- (Numeric) summation

Sum

Tests and examples here.

Returns:



92
93
94
95
# File 'lib/kyanite/enumerable/enumerable_numerics.rb', line 92

def summation
  # Methode darf nicht sum heißen, kollidiert sonst schnell mit ActiveRecord.  
  self.inject(0.0) { |sum, i | sum += i }  
end