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

Parameters:

  • angle (Float)

    Angle in degrees

Returns:

  • (Float)

    cos(angle) squared



40
41
42
# File 'lib/openc3/core_ext/math.rb', line 40

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.

Parameters:

  • red (Numeric)

    Red RGB value (0 - 255)

  • green (Numeric)

    Green RGB value (0 - 255)

  • blue (Numeric)

    Blue RGB value (0 - 255)

Returns:

  • (Float)

    The calculated luma



105
106
107
# File 'lib/openc3/core_ext/math.rb', line 105

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

Parameters:

  • angle (Float)

    Angle in degrees

Returns:

  • (Float)

    sin(angle) squared



31
32
33
# File 'lib/openc3/core_ext/math.rb', line 31

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.

Parameters:

Returns:

  • (Float, Float)

    The first value is the mean and the second is the standard deviation



83
84
85
86
# File 'lib/openc3/core_ext/math.rb', line 83

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.

Parameters:

Returns:

  • (Float, Float)

    The first value is the mean and the second is the standard deviation



94
95
96
97
# File 'lib/openc3/core_ext/math.rb', line 94

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.

Parameters:

Returns:

  • (Float, Float)

    The first value is the mean and the second is the variance



51
52
53
54
55
56
57
58
# File 'lib/openc3/core_ext/math.rb', line 51

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.

Parameters:

Returns:

  • (Float, Float)

    The first value is the mean and the second is the variance



68
69
70
71
72
73
74
75
# File 'lib/openc3/core_ext/math.rb', line 68

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