polynomials 0.1.5 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/polynomials.rb +15 -10
- data/lib/term.rb +5 -5
- data/polynomials.gemspec +1 -1
- data/test/polynomial_test.rb +6 -0
- data/test/term_test.rb +16 -1
- metadata +3 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.6
|
data/lib/polynomials.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
require 'set'
|
2
2
|
require_relative 'term'
|
3
3
|
require_relative 'core_ext/math'
|
4
|
+
|
4
5
|
class Polynomial
|
6
|
+
|
5
7
|
MinMaxMapping = { 1.0 => :max, -1.0 => :min }
|
6
8
|
AfterextremaCurvatureMapping = { max: :right, min: :left }
|
7
9
|
NegPosMinMaxExtremumMapping = {[1.0,-1.0] => :max,[-1.0,1.0] => :min}
|
@@ -35,14 +37,6 @@ class Polynomial
|
|
35
37
|
return new_function
|
36
38
|
end
|
37
39
|
|
38
|
-
def alter
|
39
|
-
new_function = self.class.new
|
40
|
-
self.terms.values.each do |term|
|
41
|
-
yield new_function, term
|
42
|
-
end
|
43
|
-
return new_function
|
44
|
-
end
|
45
|
-
|
46
40
|
def roots
|
47
41
|
if terms.keys.none?(&:zero?)
|
48
42
|
self.alter { |nf, term| nf.terms[term.exponent-1].coefficient = term.coefficient }.roots << 0.0
|
@@ -65,8 +59,8 @@ class Polynomial
|
|
65
59
|
def local_extrema
|
66
60
|
derivative = self.derivative
|
67
61
|
max_min_extremum = Hash.new { |hash,key| hash[key] = Set.new }
|
68
|
-
|
69
|
-
|
62
|
+
possible_extrema = derivative.roots.sort
|
63
|
+
unless possible_extrema.empty?
|
70
64
|
|
71
65
|
samples = ([possible_extrema.first - 1] + possible_extrema.sort + [possible_extrema.last + 1]).each_cons(2).map do |before,after|
|
72
66
|
(before + after)/2
|
@@ -120,6 +114,7 @@ class Polynomial
|
|
120
114
|
end
|
121
115
|
|
122
116
|
private
|
117
|
+
|
123
118
|
def coefficients_till(n)
|
124
119
|
coefficients = []
|
125
120
|
(0..n).each do |e|
|
@@ -127,4 +122,14 @@ class Polynomial
|
|
127
122
|
end
|
128
123
|
return coefficients.reverse
|
129
124
|
end
|
125
|
+
|
126
|
+
protected
|
127
|
+
|
128
|
+
def alter
|
129
|
+
new_function = self.class.new
|
130
|
+
self.terms.values.each do |term|
|
131
|
+
yield new_function, term
|
132
|
+
end
|
133
|
+
return new_function
|
134
|
+
end
|
130
135
|
end
|
data/lib/term.rb
CHANGED
@@ -7,11 +7,11 @@ class Term
|
|
7
7
|
|
8
8
|
def self.parse(string)
|
9
9
|
term = self.new
|
10
|
-
float =
|
11
|
-
integer =
|
12
|
-
raw_data = string.match(/([-+]?)\s
|
13
|
-
term.coefficient = (raw_data[
|
14
|
-
term.exponent = (raw_data[
|
10
|
+
float = /\d*(?:\.\d*)?/
|
11
|
+
integer = /\d*/
|
12
|
+
raw_data = string.match(/(?<algebraic_sign>[-+]?)\s*(?<coefficient>#{float})?\s*(?<power>x.*)?\s*\Z/)
|
13
|
+
term.coefficient = (raw_data[:algebraic_sign] + (!raw_data[:coefficient].empty? ? raw_data[:coefficient] : "1")).to_f
|
14
|
+
term.exponent = (raw_data[:power] ? (raw_data[:power] =~ /x\^(#{integer})/ ? $1.to_i : 1) : 0)
|
15
15
|
term
|
16
16
|
end
|
17
17
|
|
data/polynomials.gemspec
CHANGED
data/test/polynomial_test.rb
CHANGED
@@ -50,6 +50,12 @@ class TestPolynomial < MiniTest::Unit::TestCase
|
|
50
50
|
assert_equal({ max: Set[1.0/0, -1.0/0], min: Set[0.0]}, polynomial.extrema)
|
51
51
|
end
|
52
52
|
|
53
|
+
def test_no_local_extrema
|
54
|
+
polynomial = Polynomial.parse('6x^6 - 5x + 50')
|
55
|
+
assert_equal({}, polynomial.local_extrema)
|
56
|
+
assert_equal({max: Set[ -1.0/0, 1.0/0 ]}, polynomial.extrema)
|
57
|
+
end
|
58
|
+
|
53
59
|
def test_no_inflection_points
|
54
60
|
polynomial = Polynomial.parse('1 x^4')
|
55
61
|
assert_equal({ left: Set[-1.0/0..+1.0/0] }, polynomial.curvature_behaviour)
|
data/test/term_test.rb
CHANGED
@@ -5,8 +5,23 @@ class TestTerm < MiniTest::Unit::TestCase
|
|
5
5
|
assert_equal 0, Term.new.exponent
|
6
6
|
end
|
7
7
|
|
8
|
-
def
|
8
|
+
def test_parsing_with_standard_format
|
9
9
|
assert_equal 4, Term.parse('4x^2').coefficient
|
10
10
|
assert_equal 2, Term.parse('4x^2').exponent
|
11
11
|
end
|
12
|
+
|
13
|
+
def test_parsing_with_default_exponent_equal_to_one
|
14
|
+
assert_equal 1, Term.parse('1x').coefficient
|
15
|
+
assert_equal 1, Term.parse('1x').exponent
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_parsing_with_default_coefficient_equal_to_one
|
19
|
+
assert_equal 1.0, Term.parse('x^6').coefficient
|
20
|
+
assert_equal 6, Term.parse('x^6').exponent
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_parsing_variable_omitted
|
24
|
+
assert_equal 6, Term.parse('6').coefficient
|
25
|
+
assert_equal 0, Term.parse('6').exponent
|
26
|
+
end
|
12
27
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 6
|
9
|
+
version: 0.1.6
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Manuel Korfmann
|
@@ -128,7 +128,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
128
128
|
requirements:
|
129
129
|
- - ">="
|
130
130
|
- !ruby/object:Gem::Version
|
131
|
-
hash:
|
131
|
+
hash: 1846189921572599385
|
132
132
|
segments:
|
133
133
|
- 0
|
134
134
|
version: "0"
|