lib/more_math/histogram.rb
# Computes the histogram and returns it as an array of tuples (l, c, r).
Computes the histogram and returns it as an array of tuples (l, c, r).
Computes the histogram and returns it as an array of tuples ( l c r ) .
def compute
      @sequence.empty? and return []
      last_r = -Infinity
      min = @sequence.min
      max = @sequence.max
      step = (max - min) / bins.to_f
      Array.new(bins) do |i|
        l = min + i  * step
        r = min + (i + 1) * step
        c = 0
        @sequence.each do |x|
          x > last_r and (x <= r || i == bins - 1) and c += 1
        end
        last_r = r
        Bin.new(l, r, c)
      end
    end
def compute @sequence . empty? and return [ ] last_r = - Infinity min = @sequence . min max = @sequence . max step = ( max - min ) / bins . to_f Array . new ( bins ) do | i | l = min + i * step r = min + ( i + 1 ) * step c = 0 @sequence . each do | x | x > last_r and ( x <= r || i == bins - 1 ) and c += 1 end last_r = r Bin . new ( l , r , c ) end end
Computes the histogram and returns it as an array of tuples (l, c, r).
Computes the histogram and returns it as an array of tuples ( l c r ) .