Module: Math

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

Overview

COSMOS 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



42
43
44
# File 'lib/cosmos/core_ext/math.rb', line 42

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



107
108
109
# File 'lib/cosmos/core_ext/math.rb', line 107

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



33
34
35
# File 'lib/cosmos/core_ext/math.rb', line 33

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



85
86
87
88
# File 'lib/cosmos/core_ext/math.rb', line 85

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



96
97
98
99
# File 'lib/cosmos/core_ext/math.rb', line 96

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



53
54
55
56
57
58
59
60
# File 'lib/cosmos/core_ext/math.rb', line 53

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



70
71
72
73
74
75
76
77
# File 'lib/cosmos/core_ext/math.rb', line 70

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