Module: NumericArray::InstanceMethods
- Defined in:
- lib/numeric_array.rb
Constant Summary collapse
- NUMERIC_REGEX =
/^-?\d+.?\d*e?-?\d*$/
Instance Method Summary collapse
-
#average ⇒ Object
(also: #avg, #mean)
average of an array of numbers.
- #check_numeric_array! ⇒ Object
- #non_numeric_elements ⇒ Object
- #numeric_array? ⇒ Boolean (also: #numeric?)
- #numerify ⇒ Object
- #numerify! ⇒ Object
- #sample_standard_deviation ⇒ Object (also: #sample_std_dev)
- #sample_variance ⇒ Object
-
#standard_deviation(sample = false) ⇒ Object
(also: #std_dev)
standard deviation of an array of numbers.
-
#sum(identity = 0, &block) ⇒ Object
Don’t require numeric for this Array#sum.
-
#variance(sample = false) ⇒ Object
variance of an array of numbers.
Instance Method Details
#average ⇒ Object Also known as: avg, mean
average of an array of numbers
16 17 18 19 20 |
# File 'lib/numeric_array.rb', line 16 def average check_numeric_array! a = numerify a.sum / length.to_f end |
#check_numeric_array! ⇒ Object
47 48 49 |
# File 'lib/numeric_array.rb', line 47 def check_numeric_array! raise ArgumentError, "Array cannot have the following non-numeric elements: #{non_numeric_elements.collect(&:inspect).join(", ")}" unless numeric_array? end |
#non_numeric_elements ⇒ Object
56 57 58 |
# File 'lib/numeric_array.rb', line 56 def non_numeric_elements self.select{|obj| !(obj.kind_of?(Numeric) or obj.to_s =~ NUMERIC_REGEX)} end |
#numeric_array? ⇒ Boolean Also known as: numeric?
51 52 53 |
# File 'lib/numeric_array.rb', line 51 def numeric_array? !self.detect{|obj| !(obj.kind_of?(Numeric) or obj.to_s =~ NUMERIC_REGEX) } end |
#numerify ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/numeric_array.rb', line 60 def numerify self.collect do |obj| if obj.kind_of? Numeric obj elsif obj.to_s =~ NUMERIC_REGEX obj.to_f else raise ArgumentError, "Array element #{obj.inspect} cannot be converted into a numeric value" end end end |
#numerify! ⇒ Object
72 73 74 |
# File 'lib/numeric_array.rb', line 72 def numerify! self.replace(numerify) end |
#sample_standard_deviation ⇒ Object Also known as: sample_std_dev
42 43 44 |
# File 'lib/numeric_array.rb', line 42 def sample_standard_deviation standard_deviation(true) end |
#sample_variance ⇒ Object
32 33 34 |
# File 'lib/numeric_array.rb', line 32 def sample_variance variance(true) end |
#standard_deviation(sample = false) ⇒ Object Also known as: std_dev
standard deviation of an array of numbers
37 38 39 |
# File 'lib/numeric_array.rb', line 37 def standard_deviation(sample = false) Math.sqrt(self.variance(sample)) end |
#sum(identity = 0, &block) ⇒ Object
Don’t require numeric for this Array#sum. ActiveSupport uses it as well: [“foo”, “bar”].sum #=> “foobar”
7 8 9 10 11 12 13 |
# File 'lib/numeric_array.rb', line 7 def sum(identity = 0, &block) if block_given? map(&block).sum(identity) else inject { |sum, element| sum + element } || identity end end |
#variance(sample = false) ⇒ Object
variance of an array of numbers
25 26 27 28 29 30 |
# File 'lib/numeric_array.rb', line 25 def variance(sample = false) a = numerify avg = a.average sum = a.inject(0) { |sum, value| sum + (value - avg) ** 2} (1 / (a.length.to_f - (sample ? 1 : 0)) * sum) end |