Class: Maxima::Polynomial
- Inherits:
-
Unit
- Object
- Unit
- Maxima::Polynomial
show all
- Defined in:
- lib/maxima/polynomial.rb
Instance Attribute Summary
Attributes inherited from Unit
#maxima_output, #plot_title
Class Method Summary
collapse
Methods inherited from Unit
#==, #===, #at, #gnu_plot_options, #gnu_plot_text, #gnu_plot_w, #imaginary?, #initialize, #inspect, #negative?, parse, parse_float, #positive?, #real?, #simplified, #simplified!, #through_maxima, #to_f, #to_gnu_plot, #to_maxima_input, #to_pdf, #to_s, #with_plot_title, #zero?
Constructor Details
This class inherits a constructor from Maxima::Unit
Class Method Details
.fit(histogram, degrees) ⇒ Object
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
# File 'lib/maxima/polynomial.rb', line 4
def self.fit(histogram, degrees)
throw :degrees_must_be_zero_or_positive if degrees < 0
equation_string, variables = polynomial_equation(degrees)
results = Maxima.lsquares_estimation(histogram.to_a, [:x, :y], "y = #{equation_string}", variables)
mse = results.delete(:mse)
results.each do |variable, value|
equation_string.gsub!("(#{variable})", value.to_s)
end
{
function: Maxima::Function.new(equation_string),
mse: mse
}
end
|
.fit_function(min, max, x: "x") ⇒ Object
21
22
23
24
25
26
27
28
|
# File 'lib/maxima/polynomial.rb', line 21
def self.fit_function(min, max, x: "x")
Enumerator.new do |e|
(min..max).each do |degrees|
equation_string, variables = polynomial_equation(degrees, f_of: x)
e.<<(equation_string, variables)
end
end
end
|
.polynomial_equation(degrees, f_of: "x") ⇒ Object
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/maxima/polynomial.rb', line 30
def self.polynomial_equation(degrees, f_of: "x")
constant_variables = degrees.times.map { |degree| "c#{degree}" }
free_polynomial = constant_variables.each do |degree|
case degree
when 0
"(#{constant_variable})"
when 1
"(#{constant_variable}) * #{f_of}"
else
"(#{constant_variable}) * #{f_of} ^ #{degree}"
end
end
[
free_polynomial.join(" + "),
variables
]
end
|