Class: Polynomials::Polynomial
- Inherits:
-
Object
- Object
- Polynomials::Polynomial
show all
- Includes:
- Analyzable
- Defined in:
- lib/polynomials/polynomial.rb
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 Analyzable
#curvature_behaviour, #inflection_points, #local_extrema, #strives_for
Constructor Details
#initialize(*args) ⇒ Polynomial
Returns a new instance of Polynomial.
16
17
18
19
20
21
22
23
|
# File 'lib/polynomials/polynomial.rb', line 16
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.
5
6
7
|
# File 'lib/polynomials/polynomial.rb', line 5
def terms
@terms
end
|
Class Method Details
.parse(string) ⇒ Object
7
8
9
10
11
12
13
14
|
# File 'lib/polynomials/polynomial.rb', line 7
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)
delete_zeros = proc{ |_,t| t.coefficient.zero? }
self.terms.delete_if(&delete_zeros) == other.terms.delete_if(&delete_zeros)
end
|
#calculate(x) ⇒ Object
Also known as:
call
25
26
27
28
29
|
# File 'lib/polynomials/polynomial.rb', line 25
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
32
33
34
35
36
37
|
# File 'lib/polynomials/polynomial.rb', line 32
def derivative
new_function = self.alter do |nf, term|
(nf.terms[term.exponent - 1].coefficient += term.exponent * term.coefficient) unless term.exponent.zero?
end
end
|
#grouped_pointset(start, stop, step = 0.1, grouped) ⇒ Object
92
93
94
95
96
97
98
99
100
|
# File 'lib/polynomials/polynomial.rb', line 92
def grouped_pointset(start,stop,step=0.1,grouped)
Hash[grouped.map do |key,intervals|
intervals = intervals.reject { |s,e| e < start || s > stop }
intervals = intervals.map do |s,e|
self.pointset([s,start].max, [e,stop].min, step)
end
[key,intervals]
end]
end
|
#gt ⇒ Object
74
75
76
|
# File 'lib/polynomials/polynomial.rb', line 74
def gt
self.terms[self.degree]
end
|
#lt ⇒ Object
78
79
80
|
# File 'lib/polynomials/polynomial.rb', line 78
def lt
self.terms[self.terms.reject { |_,t| t.coefficient.zero? }.min_by{ |_,t| t.exponent}.first]
end
|
#pointset(start, stop, step = 0.1) ⇒ Object
82
83
84
85
86
87
88
89
90
|
# File 'lib/polynomials/polynomial.rb', line 82
def pointset(start,stop,step=0.1)
data = []
x = start
while x < stop
data << [x,self.(x)]
x += step
end
data << [stop,self.(stop)]
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.empty? and (terms.keys.none?(&:zero?) || terms[0].coefficient.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
|