class Maxima::Histogram
Attributes
points[RW]
Public Class Methods
between(min, max, function = ->(x) { x }
click to toggle source
# File lib/maxima/histogram.rb, line 5 def self.between(min, max, function = ->(x) { x }, steps = 100) Histogram.new( *[].tap do |points| (min..max).step((max - min).fdiv(steps)).each do |x| points.push([x, function.call(x)]) end end ) end
from_csv(csv)
click to toggle source
# File lib/maxima/histogram.rb, line 19 def self.from_csv(csv) Histogram.new( *CSV.read(csv).map { |array| array.map(&:to_f) } ) end
new(*points, **options)
click to toggle source
Calls superclass method
# File lib/maxima/histogram.rb, line 29 def initialize(*points, **options) super(**options) while points.is_a?(Array) && points.first.is_a?(Array) && points.first.first.is_a?(Array) points = points.flatten(1) end unless points.is_a?(Array) && points.first.is_a?(Array) && points.first.length == 2 throw :invalid_histogram_points end @points = points end
parse(s)
click to toggle source
# File lib/maxima/histogram.rb, line 25 def self.parse(s) Histogram.new((eval s), maxima_output: s) end
Public Instance Methods
<=>(other)
click to toggle source
# File lib/maxima/histogram.rb, line 80 def <=>(other) case other when Array, Histogram self.to_a <=> other.to_a else -1 end end
integral()
click to toggle source
literal CDF
# File lib/maxima/histogram.rb, line 64 def integral() begin sum = 0 Histogram.new( points.map do |(x, y)| sum += y [x, sum] end ) end end
polynomial_fit(degrees)
click to toggle source
# File lib/maxima/histogram.rb, line 15 def polynomial_fit(degrees) Polynomial.fit(self, degrees)[:function] end
to_a()
click to toggle source
# File lib/maxima/histogram.rb, line 43 def to_a @points end
to_gnu_plot()
click to toggle source
# File lib/maxima/histogram.rb, line 76 def to_gnu_plot() [*points.map(&:to_a).transpose, w: "points"] end
to_percentage()
click to toggle source
# File lib/maxima/histogram.rb, line 48 def to_percentage() @to_percentage ||= begin sum = points.sum(&:last) Histogram.new( points.map do |(x,y)| [ x, y.fdiv(sum) ] end ) end end