Class: Maxima::Histogram

Inherits:
Unit
  • Object
show all
Defined in:
lib/maxima/histogram.rb

Instance Attribute Summary collapse

Attributes inherited from Unit

#maxima_output, #plot_title

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Unit

#==, #===, #at, #gnu_plot_options, #gnu_plot_text, #gnu_plot_w, #imaginary?, #inspect, #negative?, parse_float, #positive?, #real?, #simplified, #through_maxima, #to_f, #to_maxima_input, #to_pdf, #to_s, #with_plot_title, #zero?

Constructor Details

#initialize(*points, **options) ⇒ Histogram

Returns a new instance of Histogram.



29
30
31
32
33
34
35
36
37
38
39
40
41
# 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

Instance Attribute Details

#pointsObject

Returns the value of attribute points.



3
4
5
# File 'lib/maxima/histogram.rb', line 3

def points
  @points
end

Class Method Details

.between(min, max, function = ->(x) { x }, steps = 100) ⇒ Object



5
6
7
8
9
10
11
12
13
# 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) ⇒ Object



19
20
21
22
23
# File 'lib/maxima/histogram.rb', line 19

def self.from_csv(csv)
  Histogram.new(
    *CSV.read(csv).map { |array| array.map(&:to_f) }
  )
end

.parse(s) ⇒ Object



25
26
27
# File 'lib/maxima/histogram.rb', line 25

def self.parse(s)
  Histogram.new((eval s), maxima_output: s)
end

Instance Method Details

#<=>(other) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/maxima/histogram.rb', line 80

def <=>(other)
  case other
  when Array, Histogram
    self.to_a <=> other.to_a
  else
    -1
  end
end

#integralObject

literal CDF



64
65
66
67
68
69
70
71
72
73
74
# 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) ⇒ Object



15
16
17
# File 'lib/maxima/histogram.rb', line 15

def polynomial_fit(degrees)
  Polynomial.fit(self, degrees)[:function]
end

#to_aObject



43
44
45
# File 'lib/maxima/histogram.rb', line 43

def to_a
  @points
end

#to_gnu_plotObject



76
77
78
# File 'lib/maxima/histogram.rb', line 76

def to_gnu_plot()
  [*points.map(&:to_a).transpose, w: "points"]
end

#to_percentageObject

PDF



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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