lib/utils/mathutil.rb in stella-0.5.1 vs lib/utils/mathutil.rb in stella-0.5.3
- old
+ new
@@ -1,8 +1,44 @@
+module MathUtil
+
+ def self.variance(population)
+ n = 0
+ mean = 0.0
+ s = 0.0
+ population.each { |x|
+ n = n + 1
+ delta = (x - mean).to_f
+ mean = (mean + (delta / n)).to_f
+ s = (s + delta * (x - mean)).to_f
+ }
+
+ return s / n
+ end
+ # calculate the standard deviation of a population
+ # accepts: an array, the population
+ # returns: the standard deviation
+ def self.standard_deviation(population)
+ Math.sqrt(variance(population))
+ end
+
+ # enforce_limit
+ #
+ # Enforce a minimum and maximum value
+ def self.enforce_limit(val,min,max)
+ val = min if val < min
+ val = max if val > max
+ val
+ end
+
+end
+
+
+
+
module Enumerable
##
# Sum of all the elements of the Enumerable
@@ -37,40 +73,6 @@
def standard_deviation
return Math.sqrt(self.sample_variance)
end
-end
-
-module MathUtil
-
- def self.variance(population)
- n = 0
- mean = 0.0
- s = 0.0
- population.each { |x|
- n = n + 1
- delta = (x - mean).to_f
- mean = (mean + (delta / n)).to_f
- s = (s + delta * (x - mean)).to_f
- }
-
- return s / n
- end
-
- # calculate the standard deviation of a population
- # accepts: an array, the population
- # returns: the standard deviation
- def self.standard_deviation(population)
- Math.sqrt(variance(population))
- end
-
- # enforce_limit
- #
- # Enforce a minimum and maximum value
- def self.enforce_limit(val,min,max)
- val = min if val < min
- val = max if val > max
- val
- end
-
end
\ No newline at end of file