Module: EnumerableNumerics
- Included in:
- ArrayOfNumerics
- Defined in:
- lib/kyanite/enumerable/enumerable_numerics.rb
Overview
Enumeration Of Numerics
- Kyanite definitions
- Kyanite class with module included
- Kyanite tests and examples
-
TestKyaniteEnumerableNumerics
- Usage
-
require ‘kyanite/enumerable/enumerable_numerics’
Mean Values collapse
-
#mean ⇒ Float
(also: #avg, #average, #mean_arithmetric)
Arithmetic mean.
-
#mean_geometric ⇒ Float
Geometric mean.
-
#mean_harmonic(options = {}) ⇒ Float
Harmonic mean.
Sum, Product, Parallel collapse
-
#parallel ⇒ Float
Parallel.
-
#prd ⇒ Numeric
Product.
-
#summation ⇒ Numeric
Sum.
Instance Method Details
#mean ⇒ Float Also known as: avg, average, mean_arithmetric
Arithmetic mean
Tests and examples here.
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 |
#mean_geometric ⇒ Float
Geometric mean
77 78 79 |
# File 'lib/kyanite/enumerable/enumerable_numerics.rb', line 77 def mean_geometric self.prd ** ( 1.0/self.size ) end |
#mean_harmonic(options = {}) ⇒ Float
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.
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( ={} ) return 0 if self.empty? return self.first if self.size == 1 allow_negative = [: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 |
#parallel ⇒ Float
Parallel
Result is equal to the total resistance of resistors in parallel circuits. Tests and examples here.
112 113 114 |
# File 'lib/kyanite/enumerable/enumerable_numerics.rb', line 112 def parallel mean_harmonic / size end |
#prd ⇒ Numeric
Product
Tests and examples here.
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 |
#summation ⇒ Numeric
Sum
Tests and examples here.
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 |