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



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

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



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

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



24
25
26
# File 'lib/cosmos/core_ext/math.rb', line 24

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



76
77
78
79
# File 'lib/cosmos/core_ext/math.rb', line 76

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



87
88
89
90
# File 'lib/cosmos/core_ext/math.rb', line 87

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



44
45
46
47
48
49
50
51
# File 'lib/cosmos/core_ext/math.rb', line 44

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



61
62
63
64
65
66
67
68
# File 'lib/cosmos/core_ext/math.rb', line 61

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