Class: SPCore::Scale

Inherits:
Object
  • Object
show all
Defined in:
lib/spcore/util/scale.rb

Overview

Provide methods for generating sequences that scale linearly or exponentially.

Class Method Summary collapse

Class Method Details

.exponential(range, n_points) ⇒ Object

Parameters:

  • range (Range)

    The start and end values the set should include.

  • n_points (Fixnum)

    The number of points to create for the sequence, including the start and end points.

Raises:

  • (ArgumentError)

    if n_points is < 2.



27
28
29
30
31
32
33
34
35
36
37
38
39
# 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) ⇒ Object

Produce a sequence of values that progresses in a linear fashion.

Parameters:

  • range (Range)

    The start and end values the set should include.

  • n_points (Fixnum)

    The number of points to create for the sequence, including the start and end points.

Raises:

  • (ArgumentError)

    if n_points is < 2.



8
9
10
11
12
13
14
15
16
17
18
19
20
# 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