Module: EnumerableNumerics
- Included in
- ArrayOfNumerics
- Defined in
- lib/kyanite/enumerable/enumerable_numerics.rb
Enumeration Of Numerics
- Kyanite definitions
- Kyanite class with module included
- Kyanite tests and examples
- Usage
-
require 'kyanite/enumerable/enumerable_numerics'
Mean Values (collapse)
-
- (Float) mean
(also: #avg, #average, #mean_arithmetric)
Arithmetic mean.
-
- (Float) mean_geometric
Geometric mean.
-
- (Float) mean_harmonic(options = {})
Harmonic mean.
Sum, Product, Parallel (collapse)
-
- (Float) parallel
Parallel.
-
- (Numeric) prd
Product.
-
- (Numeric) summation
Sum.
Instance Method Details
- (Float) mean 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 |
- (Float) mean_geometric
Geometric mean
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.
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 |
- (Float) parallel
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 |