Module: Math

Extended by:
Math
Included in:
Math
Defined in:
lib/openc3/core_ext/math.rb

Overview

OpenC3 specific additions to the Ruby Math module. The algorithms implemented here are based on standard deviation algorithms based on Wikipedia Algorithms for calculating variance, Section 3.

Instance Method Summary collapse

Instance Method Details

#cos_squared(angle) ⇒ Float

Power reduction formula. Calculates cos squared which is (1 + cos(2 * angle)) / 2



45
46
47
# File 'lib/openc3/core_ext/math.rb', line 45

def cos_squared(angle)
  return (1.0 + Math.cos(2.0 * angle)) / 2.0
end

#luma_from_rgb_max_255(red, green, blue) ⇒ Float

Calculates luma, the brightness, given RGB values.



110
111
112
# File 'lib/openc3/core_ext/math.rb', line 110

def luma_from_rgb_max_255(red, green, blue)
  (0.2126 * (red.to_f / 255.0)) + (0.7152 * (green.to_f / 255.0)) + (0.0722 * (blue.to_f / 255.0))
end

#sin_squared(angle) ⇒ Float

Power reduction formula. Calculates sin squared which is (1 - cos(2 * angle)) / 2



36
37
38
# File 'lib/openc3/core_ext/math.rb', line 36

def sin_squared(angle)
  return (1.0 - Math.cos(2.0 * angle)) / 2.0
end

#stddev_population(array) ⇒ Float

Calculates the standard deviation of the population variation by taking the square root of the population variance.



88
89
90
91
# File 'lib/openc3/core_ext/math.rb', line 88

def stddev_population(array)
  mean, variance = self.variance_population(array)
  return [mean, Math.sqrt(variance)]
end

#stddev_sample(array) ⇒ Float

Calculates the standard deviation of the sample variation by taking the square root of the sample variance.



99
100
101
102
# File 'lib/openc3/core_ext/math.rb', line 99

def stddev_sample(array)
  mean, variance = self.variance_sample(array)
  return [mean, Math.sqrt(variance)]
end

#variance_population(array) ⇒ Float

Calculates the population variance of a group of numbers. If the population is finite the population variance is the variance of the underlying probability distribution.



56
57
58
59
60
61
62
63
# File 'lib/openc3/core_ext/math.rb', line 56

def variance_population(array)
  mean, m2, num_values = variance_generic(array)
  if num_values != 0.0
    return [mean, m2 / num_values]
  else
    return [mean, 0.0]
  end
end

#variance_sample(array) ⇒ Float

Calculates the sample variance of a group of numbers. If the population of numbers is very large, it is not possible to count every value in the population, so the computation must be performed on a sample of the population.



73
74
75
76
77
78
79
80
# File 'lib/openc3/core_ext/math.rb', line 73

def variance_sample(array)
  mean, m2, num_values = variance_generic(array)
  if num_values != 1.0
    return [mean, m2 / (num_values - 1.0)]
  else
    return [mean, 0.0]
  end
end