Class: Polynomials::Polynomial
Constant Summary
Constants included
from Analyzable
Analyzable::AfterExtremaCurvatureMapping, Analyzable::MinimumOrMaximum
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Plotable
#grouped_pointset, #pointset
Methods included from Analyzable
#curvature_behaviour, #inflection_points, #local_extrema, #strives_for
Constructor Details
#initialize(*args) ⇒ Polynomial
Returns a new instance of Polynomial.
17
18
19
20
21
22
23
24
|
# File 'lib/polynomials/polynomial.rb', line 17
def initialize(*args)
self.terms = Hash.new { |hash, key| hash[key] = Term.new(key) }
unless args.empty?
args.reverse.each.with_index do |coefficient,exponent|
self.terms[exponent].coefficient += coefficient
end
end
end
|
Instance Attribute Details
#terms ⇒ Object
Returns the value of attribute terms.
6
7
8
|
# File 'lib/polynomials/polynomial.rb', line 6
def terms
@terms
end
|
Class Method Details
.parse(string) ⇒ Object
8
9
10
11
12
13
14
15
|
# File 'lib/polynomials/polynomial.rb', line 8
def self.parse(string)
polynomial = self.new
string.split(/(?=[-+])/).each do |term|
parsed = Term.parse(term)
polynomial.terms[parsed.exponent].coefficient += parsed.coefficient
end
return polynomial
end
|
Instance Method Details
#==(other) ⇒ Object
69
70
71
72
|
# File 'lib/polynomials/polynomial.rb', line 69
def ==(other)
check_if_zero = proc { |_,t| t.coefficient.zero? }
self.terms.delete_if(&check_if_zero) == other.terms.delete_if(&check_if_zero)
end
|
#calculate(x) ⇒ Object
Also known as:
call
26
27
28
29
30
|
# File 'lib/polynomials/polynomial.rb', line 26
def calculate(x)
self.terms.values.inject(0.0) do |acc,t|
acc + (t.coefficient.to_f * (x**t.exponent))
end
end
|
#degree ⇒ Object
58
59
60
|
# File 'lib/polynomials/polynomial.rb', line 58
def degree
self.terms.keys.max || 0
end
|
#derivative ⇒ Object
33
34
35
36
37
|
# File 'lib/polynomials/polynomial.rb', line 33
def derivative
new_function = self.alter do |new_function, term|
(new_function.terms[term.exponent - 1].coefficient += term.exponent * term.coefficient) unless term.exponent.zero?
end
end
|
#gt ⇒ Object
75
76
77
|
# File 'lib/polynomials/polynomial.rb', line 75
def gt
self.terms[self.degree]
end
|
#lt ⇒ Object
80
81
82
83
84
85
|
# File 'lib/polynomials/polynomial.rb', line 80
def lt
self.terms[
self.terms.reject { |_,t| t.coefficient.zero? }
.min_by{ |_,t| t.exponent}.first
]
end
|
#roots ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/polynomials/polynomial.rb', line 39
def roots
if terms[0].coefficient.zero? and !terms.values.map(&:coefficient).all?(&:zero?)
self.alter { |nf, term| nf.terms[term.exponent-self.lt.exponent].coefficient = term.coefficient }.roots << Root.new(0.0)
else
case self.degree
when 1
Set[-self.terms[0].coefficient / self.terms[1].coefficient]
when 2
Formulas.roots_of_quadratic_function(*coefficients_till(2))
when 3
Formulas.roots_of_cubic_function(*coefficients_till(3))
when 4
Formulas.roots_of_quartic_function(*coefficients_till(4))
else
Set[]
end.map { |x| Root.new(x) }.to_set
end
end
|
#to_s ⇒ Object
62
63
64
65
66
67
|
# File 'lib/polynomials/polynomial.rb', line 62
def to_s
terms.sort_by { |_,t| -t.exponent }.inject("") do |string,(_,term)|
string << term.to_s unless term.coefficient.zero? && !term.exponent.zero?
string
end.strip.sub(/\A\+\s/, '')
end
|