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, #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_i) }
  )
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

#integralObject



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/maxima/histogram.rb', line 62

def integral()
  begin
    sum = 0
    Histogram.new(
      points.map do |(one, two)|
        sum += two
        [one, 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



58
59
60
# File 'lib/maxima/histogram.rb', line 58

def to_a
  @points
end

#to_gnu_plotObject



74
75
76
# File 'lib/maxima/histogram.rb', line 74

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

#to_percentageObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/maxima/histogram.rb', line 43

def to_percentage()
  @to_percentage ||=
    begin
      sum = points.sum(&:x)
      Histogram.new(
        points.map do |point|
          Point.new(
            point.x,
            point.y.fdiv(sum)
          )
        end
      )
    end
end