class SPCore::Scale

Provide methods for generating sequences that scale linearly or exponentially.

Public Class Methods

exponential(range, n_points) click to toggle source

@param [Range] range The start and end values the set should include. @param [Fixnum] n_points The number of points to create for the sequence, including the start and end points. @raise [ArgumentError] if n_points is < 2.

# File lib/spcore/util/scale.rb, line 27
def self.exponential range, n_points
  raise ArgumentError, "n_points is < 2" if n_points < 2
  multiplier = (range.last / range.first)**(1.0/(n_points-1))
  points = Array.new(n_points)
  value = range.first
  
  points.each_index do |i|
    points[i] = value
    value *= multiplier
  end
  
  return points
end
linear(range, n_points) click to toggle source

Produce a sequence of values that progresses in a linear fashion. @param [Range] range The start and end values the set should include. @param [Fixnum] n_points The number of points to create for the sequence, including the start and end points. @raise [ArgumentError] if n_points is < 2.

# File lib/spcore/util/scale.rb, line 8
def self.linear range, n_points
  raise ArgumentError, "n_points is < 2" if n_points < 2
  incr = (range.last - range.first) / (n_points - 1)
  points = Array.new(n_points)
  value = range.first
  
  points.each_index do |i|
    points[i] = value
    value += incr
  end
  
  return points
end