polynomials 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +4 -0
- data/README.markdown +11 -7
- data/VERSION +1 -1
- data/examples/plot_only_mutual_data.rb +4 -3
- data/lib/polynomials/analyzable.rb +3 -2
- data/lib/polynomials/polynomial.rb +14 -5
- data/polynomials.gemspec +3 -2
- data/test/analyzable_test.rb +15 -0
- data/test/polynomial_test.rb +12 -0
- metadata +4 -3
data/.travis.yml
ADDED
data/README.markdown
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# Polynomials
|
2
2
|
|
3
|
+
[![Build Status](http://travis-ci.org/mkorfmann/polynomials.png)](http://travis-ci.org/mkorfmann/polynomials)
|
4
|
+
|
3
5
|
## Usage
|
4
6
|
|
5
7
|
<pre><code>
|
@@ -14,16 +16,18 @@ points = polynomial.roots | polynomial.local_extrema | polynomial.inflection_po
|
|
14
16
|
|
15
17
|
max_x = points.max_by(&:x).x
|
16
18
|
min_x = points.min_by(&:x).x
|
19
|
+
max_x = max_x ? max_x.x : 1
|
20
|
+
min_x = min_x ? min_x.x : -1
|
17
21
|
|
18
|
-
|
19
|
-
stop = (max_x + max_x.abs/2)
|
20
|
-
|
21
|
-
step = (max_x-min_x).abs/200
|
22
|
+
difference = (max_x-min_x).abs
|
22
23
|
|
23
|
-
|
24
|
+
start = min_x - difference/4.0
|
25
|
+
stop = max_x + difference/4.0
|
26
|
+
step = difference/200.0
|
24
27
|
|
25
|
-
|
26
|
-
|
28
|
+
pointset = polynomial.pointset(start,stop,step)
|
29
|
+
data_x = pointset.map(&:first)
|
30
|
+
data_y = pointset.map(&:last)
|
27
31
|
|
28
32
|
Gnuplot.open do |gp|
|
29
33
|
Gnuplot::Plot.new(gp) do |plot|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.4.
|
1
|
+
0.4.1
|
@@ -9,14 +9,14 @@ points = polynomial.roots | polynomial.local_extrema | polynomial.inflection_po
|
|
9
9
|
|
10
10
|
max_x = points.max_by(&:x)
|
11
11
|
min_x = points.min_by(&:x)
|
12
|
-
max_x = max_x ? max_x.x : 1
|
13
|
-
min_x = min_x ? min_x.x : -1
|
12
|
+
max_x = max_x == 0 ? max_x.x : 1
|
13
|
+
min_x = min_x == 0 ? min_x.x : -1
|
14
14
|
|
15
15
|
difference = (max_x-min_x).abs
|
16
16
|
|
17
17
|
start = min_x - difference/4.0
|
18
18
|
stop = max_x + difference/4.0
|
19
|
-
step =
|
19
|
+
step = (start-stop).abs/1000.0
|
20
20
|
|
21
21
|
pointset = polynomial.pointset(start,stop,step)
|
22
22
|
data_x= pointset.map(&:first)
|
@@ -33,6 +33,7 @@ Gnuplot.open do |gp|
|
|
33
33
|
ds.with = "lines"
|
34
34
|
ds.linewidth = 0.2
|
35
35
|
ds.title = "f(x) = #{polynomial}"
|
36
|
+
ds.smooth
|
36
37
|
end
|
37
38
|
|
38
39
|
[:inflection_point,:root, :maximum, :minimum].each do |kind_of_point|
|
@@ -5,8 +5,9 @@ module Polynomials
|
|
5
5
|
|
6
6
|
def strives_for
|
7
7
|
roots = self.roots.map(&:x)
|
8
|
-
|
9
|
-
|
8
|
+
local_extrema = self.local_extrema.map(&:x)
|
9
|
+
return nil if roots.empty? && local_extrema.empty?
|
10
|
+
[self.((roots.min || local_extrema.min) - 1).sign, self.((roots.max || local_extrema.max) + 1).sign].map { |d| d * Infinity }
|
10
11
|
end
|
11
12
|
|
12
13
|
def inflection_points
|
@@ -31,14 +31,13 @@ module Polynomials
|
|
31
31
|
|
32
32
|
def derivative
|
33
33
|
new_function = self.alter do |nf, term|
|
34
|
-
nf.terms[term.exponent - 1].coefficient += term.exponent * term.coefficient
|
34
|
+
(nf.terms[term.exponent - 1].coefficient += term.exponent * term.coefficient) unless term.exponent.zero?
|
35
35
|
end
|
36
|
-
|
37
|
-
return new_function
|
36
|
+
|
38
37
|
end
|
39
38
|
|
40
39
|
def roots
|
41
|
-
if !terms.empty? and terms.keys.none?(&:zero?)
|
40
|
+
if !terms.empty? and (terms.keys.none?(&:zero?) || terms[0].coefficient.zero?)
|
42
41
|
self.alter { |nf, term| nf.terms[term.exponent-self.lt.exponent].coefficient = term.coefficient }.roots << Root.new(0.0)
|
43
42
|
else
|
44
43
|
case self.degree
|
@@ -77,7 +76,7 @@ module Polynomials
|
|
77
76
|
end
|
78
77
|
|
79
78
|
def lt
|
80
|
-
self.terms[self.terms.min_by{ |_,t| t.exponent}.first]
|
79
|
+
self.terms[self.terms.reject { |_,t| t.coefficient.zero? }.min_by{ |_,t| t.exponent}.first]
|
81
80
|
end
|
82
81
|
|
83
82
|
def pointset(start,stop,step=0.1)
|
@@ -90,6 +89,16 @@ module Polynomials
|
|
90
89
|
data << [stop,self.(stop)]
|
91
90
|
end
|
92
91
|
|
92
|
+
def grouped_pointset(start,stop,step=0.1,grouped)
|
93
|
+
grouped.each do |key,intervals|
|
94
|
+
intervals.reject! { |s,e| e < start || s > stop }
|
95
|
+
intervals.map! do |s,e|
|
96
|
+
self.pointset([s,start].max, [e,stop].min, step)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
return grouped
|
100
|
+
end
|
101
|
+
|
93
102
|
def coefficients_till(n)
|
94
103
|
coefficients = []
|
95
104
|
(0..n).each do |e|
|
data/polynomials.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{polynomials}
|
8
|
-
s.version = "0.4.
|
8
|
+
s.version = "0.4.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Manuel Korfmann"]
|
12
|
-
s.date = %q{2011-05-
|
12
|
+
s.date = %q{2011-05-19}
|
13
13
|
s.description = %q{
|
14
14
|
This is a gem dedicated to parsing, manipulating and finding roots,extrema and inflection points of polynomials.
|
15
15
|
It can solve polynomial equations with a degree up to 4 by implementing formulas for solving quadratic, cubic and quartic functions.
|
@@ -21,6 +21,7 @@ It can solve polynomial equations with a degree up to 4 by implementing formulas
|
|
21
21
|
]
|
22
22
|
s.files = [
|
23
23
|
".document",
|
24
|
+
".travis.yml",
|
24
25
|
"Gemfile",
|
25
26
|
"Gemfile.lock",
|
26
27
|
"LICENSE.txt",
|
data/test/analyzable_test.rb
CHANGED
@@ -73,4 +73,19 @@ class TestAnalyzable < MiniTest::Unit::TestCase
|
|
73
73
|
polynomial = Polynomial.parse('+ 1.0 x^4 + 5.0 x^3 - 1.0 x^2 + 3.0 x + 5.0').derivative
|
74
74
|
assert_equal [-Infinity,Infinity], polynomial.strives_for
|
75
75
|
end
|
76
|
+
|
77
|
+
def test_inflection_points_some_coefficients_of_derivative2_zero
|
78
|
+
polynomial = Polynomial.parse('x^5 + x^1')
|
79
|
+
assert_set_eql Set[], polynomial.inflection_points
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_curvature_behaviour_degree_4
|
83
|
+
polynomial = Polynomial.parse('x^5 + x^1')
|
84
|
+
assert_equal({ :right => [[-Infinity,0.0]], :left => [[0.0, Infinity]] }, polynomial.curvature_behaviour)
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_strives_for_no_roots
|
88
|
+
polynomial = Polynomial.new(1,0,0,0,0)
|
89
|
+
assert_equal [Infinity,Infinity], polynomial.strives_for
|
90
|
+
end
|
76
91
|
end
|
data/test/polynomial_test.rb
CHANGED
@@ -73,4 +73,16 @@ class TestPolynomial < MiniTest::Unit::TestCase
|
|
73
73
|
polynomial = Polynomial.new(1,0,0)
|
74
74
|
assert_equal [[0.1,0.1**2],[0.2,0.2**2],[0.25,0.25**2]], polynomial.pointset(0.1,0.25,0.1)
|
75
75
|
end
|
76
|
+
|
77
|
+
def test_grouped_pointset
|
78
|
+
polynomial = Polynomial.new(1,0,0)
|
79
|
+
assert_equal({ :jellyfish => [[[-0.1,0.1**2,],[0,0.0]]] , :peanut_butter => [[[0,0.0],[0.1,0.1**2],[0.2,0.2**2],[0.25,0.25**2]]]},
|
80
|
+
polynomial.grouped_pointset(-0.1,0.25,0.1, { :jellyfish => [[-Infinity,0]], :peanut_butter => [[0,Infinity]] }))
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_grouped_pointset_omitting_inteval_through_start_out_of_range
|
84
|
+
polynomial = Polynomial.new(1,0,0)
|
85
|
+
assert_equal({ :jellyfish => [[[-0.005,0.005**2,],[0,0.0]]] , :peanut_butter => [[[0,0.0],[0.1,0.1**2],[0.2,0.2**2],[0.25,0.25**2]]]},
|
86
|
+
polynomial.grouped_pointset(-0.005,0.25,0.1, { :jellyfish => [[-0.01,0]], :peanut_butter => [[-Infinity,-0.1],[0,Infinity]] }))
|
87
|
+
end
|
76
88
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: polynomials
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.4.
|
5
|
+
version: 0.4.1
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Manuel Korfmann
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-05-
|
13
|
+
date: 2011-05-19 00:00:00 +02:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -92,6 +92,7 @@ extra_rdoc_files:
|
|
92
92
|
- README.markdown
|
93
93
|
files:
|
94
94
|
- .document
|
95
|
+
- .travis.yml
|
95
96
|
- Gemfile
|
96
97
|
- Gemfile.lock
|
97
98
|
- LICENSE.txt
|
@@ -127,7 +128,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
127
128
|
requirements:
|
128
129
|
- - ">="
|
129
130
|
- !ruby/object:Gem::Version
|
130
|
-
hash:
|
131
|
+
hash: 805629987972417453
|
131
132
|
segments:
|
132
133
|
- 0
|
133
134
|
version: "0"
|