polynomials 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +33 -34
- data/VERSION +1 -1
- data/examples/plot_only_mutual_data.rb +15 -15
- data/lib/polynomials.rb +12 -4
- data/lib/polynomials/analyzable.rb +54 -0
- data/lib/polynomials/point.rb +1 -0
- data/lib/polynomials/polynomial.rb +12 -44
- data/polynomials.gemspec +5 -4
- data/test/analyzable_test.rb +76 -0
- data/test/polynomial_test.rb +8 -62
- metadata +7 -27
data/README.markdown
CHANGED
@@ -4,51 +4,50 @@
|
|
4
4
|
|
5
5
|
<pre><code>
|
6
6
|
require 'polynomials'
|
7
|
-
|
8
|
-
|
7
|
+
require 'gnuplot'
|
8
|
+
include Polynomials
|
9
9
|
|
10
10
|
|
11
|
-
|
11
|
+
polynomial = Polynomial.parse(ARGV[0])
|
12
12
|
|
13
|
-
|
13
|
+
points = polynomial.roots | polynomial.local_extrema | polynomial.inflection_points
|
14
14
|
|
15
|
-
|
16
|
-
|
15
|
+
max_x = points.max_by(&:x).x
|
16
|
+
min_x = points.min_by(&:x).x
|
17
17
|
|
18
|
-
|
19
|
-
|
18
|
+
start = (min_x - min_x.abs/2)
|
19
|
+
stop = (max_x + max_x.abs/2)
|
20
20
|
|
21
|
-
|
22
|
-
data_x,data_y = [],[]
|
23
|
-
while x < stop
|
24
|
-
data_x << x
|
25
|
-
data_y << polynomial.(x)
|
26
|
-
x += 0.005
|
27
|
-
end
|
21
|
+
step = (max_x-min_x).abs/200
|
28
22
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
23
|
+
pointset = polynomials.pointset(start,stop,step)
|
24
|
+
|
25
|
+
data_x = pointset.map(&:x)
|
26
|
+
data_y = pointset.map(&:y)
|
27
|
+
|
28
|
+
Gnuplot.open do |gp|
|
29
|
+
Gnuplot::Plot.new(gp) do |plot|
|
30
|
+
plot.xrange "[#{start}:#{stop}]"
|
31
|
+
plot.title "Polynomial"
|
32
|
+
plot.ylabel "f(x)"
|
33
|
+
plot.xlabel "x"
|
34
|
+
plot.grid
|
35
|
+
plot.data << Gnuplot::DataSet.new( [data_x,data_y] ) do |ds|
|
36
|
+
ds.with = "lines"
|
37
|
+
ds.linewidth = 0.2
|
38
|
+
ds.title = "f(x) = #{polynomial}"
|
39
|
+
end
|
41
40
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
end
|
41
|
+
[:inflection_point,:root, :maximum, :minimum].each do |kind_of_point|
|
42
|
+
selected_points = points.select(&:"#{kind_of_point}?")
|
43
|
+
plot.data << Gnuplot::DataSet.new([selected_points.map(&:x), selected_points.map(&:y)]) do |ds|
|
44
|
+
ds.with = "points"
|
45
|
+
ds.linewidth = 2
|
46
|
+
ds.title = kind_of_point.to_s.pluralize.titleize
|
49
47
|
end
|
50
48
|
end
|
51
49
|
end
|
50
|
+
end
|
52
51
|
</code></pre>
|
53
52
|
|
54
53
|
### Bash Command
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.4.0
|
@@ -7,19 +7,20 @@ polynomial = Polynomial.parse(ARGV[0])
|
|
7
7
|
|
8
8
|
points = polynomial.roots | polynomial.local_extrema | polynomial.inflection_points
|
9
9
|
|
10
|
-
max_x = points.max_by(&:x)
|
11
|
-
min_x = points.min_by(&:x)
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
10
|
+
max_x = points.max_by(&:x)
|
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
|
14
|
+
|
15
|
+
difference = (max_x-min_x).abs
|
16
|
+
|
17
|
+
start = min_x - difference/4.0
|
18
|
+
stop = max_x + difference/4.0
|
19
|
+
step = difference/200.0
|
20
|
+
|
21
|
+
pointset = polynomial.pointset(start,stop,step)
|
22
|
+
data_x= pointset.map(&:first)
|
23
|
+
data_y = pointset.map(&:last)
|
23
24
|
|
24
25
|
Gnuplot.open do |gp|
|
25
26
|
Gnuplot::Plot.new(gp) do |plot|
|
@@ -30,7 +31,7 @@ Gnuplot.open do |gp|
|
|
30
31
|
plot.grid
|
31
32
|
plot.data << Gnuplot::DataSet.new( [data_x,data_y] ) do |ds|
|
32
33
|
ds.with = "lines"
|
33
|
-
ds.linewidth = 0.
|
34
|
+
ds.linewidth = 0.2
|
34
35
|
ds.title = "f(x) = #{polynomial}"
|
35
36
|
end
|
36
37
|
|
@@ -44,4 +45,3 @@ Gnuplot.open do |gp|
|
|
44
45
|
end
|
45
46
|
end
|
46
47
|
end
|
47
|
-
|
data/lib/polynomials.rb
CHANGED
@@ -7,13 +7,21 @@ ActiveSupport::Inflector.inflections do |inflect|
|
|
7
7
|
inflect.singular /ma$/, '\1mum'
|
8
8
|
end
|
9
9
|
|
10
|
-
require '
|
10
|
+
require 'exceptions'
|
11
|
+
require 'formulas'
|
11
12
|
require 'set'
|
12
|
-
require 'term'
|
13
13
|
require 'point'
|
14
|
-
require '
|
15
|
-
require '
|
14
|
+
require 'term'
|
15
|
+
require 'analyzable'
|
16
|
+
require 'polynomial'
|
16
17
|
|
17
18
|
|
18
19
|
module Polynomials
|
20
|
+
Infinity = 1.0/0
|
21
|
+
end
|
22
|
+
|
23
|
+
class Numeric
|
24
|
+
def sign
|
25
|
+
self/self.abs
|
26
|
+
end
|
19
27
|
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module Polynomials
|
2
|
+
module Analyzable
|
3
|
+
AfterExtremaCurvatureMapping = { maximum: :right, minimum: :left }
|
4
|
+
MinimumOrMaximum = {[1.0,-1.0] => :maximum,[-1.0,1.0] => :minimum}
|
5
|
+
|
6
|
+
def strives_for
|
7
|
+
roots = self.roots.map(&:x)
|
8
|
+
return nil if roots.empty?
|
9
|
+
[self.(roots.min - 1).sign, self.(roots.max + 1).sign].map { |d| d * Infinity }
|
10
|
+
end
|
11
|
+
|
12
|
+
def inflection_points
|
13
|
+
self.derivative.local_extrema.map { |p| InflectionPoint.new(p.x,self.calculate(p.x)) }.to_set
|
14
|
+
end
|
15
|
+
|
16
|
+
def local_extrema
|
17
|
+
derivative = self.derivative
|
18
|
+
possible_extrema = derivative.roots.sort.map(&:x)
|
19
|
+
|
20
|
+
return Set[] if possible_extrema.empty?
|
21
|
+
|
22
|
+
samples = possible_extrema.sort.each_cons(2).map do |before,after|
|
23
|
+
(before + after)/2
|
24
|
+
end
|
25
|
+
|
26
|
+
samples.unshift possible_extrema.first - 1
|
27
|
+
samples.push possible_extrema.last + 1
|
28
|
+
|
29
|
+
possible_extrema.zip(samples.each_cons(2)).inject(Set[]) do |set,(pe,sample)|
|
30
|
+
directions = sample.map { |x| derivative.(x).sign}
|
31
|
+
kind_of_extremum = MinimumOrMaximum[directions]
|
32
|
+
next set unless kind_of_extremum
|
33
|
+
set << Extremum.new(pe,self.calculate(pe), kind_of_extremum)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def curvature_behaviour
|
38
|
+
hash = Hash.new {|h,k|h[k]=[]}
|
39
|
+
|
40
|
+
dd = self.derivative.derivative
|
41
|
+
return nil if !(strives_for = derivative.strives_for) || dd.(dd.roots.sort.last.to_f + 1) == 0
|
42
|
+
|
43
|
+
([[-Infinity,strives_for[0]]] + derivative.local_extrema.sort + [[Infinity,strives_for[-1]]]).each_cons(2).chunk do |b,_|
|
44
|
+
if b.is_a? Point
|
45
|
+
AfterExtremaCurvatureMapping[b.kind_of_extremum]
|
46
|
+
else
|
47
|
+
b.last < 0 ? :left : :right
|
48
|
+
end
|
49
|
+
end.with_index.inject(hash) do |hash,((curvature,values),i)|
|
50
|
+
hash.tap { |h| h[curvature].concat values.each {|a| a.map! { |p| (p.is_a? Point) ? p.x : p.first }}}
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
data/lib/polynomials/point.rb
CHANGED
@@ -1,9 +1,6 @@
|
|
1
1
|
module Polynomials
|
2
2
|
class Polynomial
|
3
|
-
|
4
|
-
MinMaxMapping = { 1.0 => :maximum, -1.0 => :minimum }
|
5
|
-
AfterExtremaCurvatureMapping = { maximum: :right, minimum: :left }
|
6
|
-
NegPosMinMaxExtremumMapping = {[1.0,-1.0] => :maximum,[-1.0,1.0] => :minimum}
|
3
|
+
include Analyzable
|
7
4
|
|
8
5
|
attr_accessor :terms
|
9
6
|
|
@@ -59,30 +56,6 @@ module Polynomials
|
|
59
56
|
end
|
60
57
|
end
|
61
58
|
|
62
|
-
def local_extrema
|
63
|
-
derivative = self.derivative
|
64
|
-
possible_extrema = derivative.roots.sort.map(&:x)
|
65
|
-
return Set[] if possible_extrema.empty?
|
66
|
-
samples = ([possible_extrema.first - 1] + possible_extrema.sort + [possible_extrema.last + 1]).each_cons(2).map do |before,after|
|
67
|
-
(before + after)/2
|
68
|
-
end
|
69
|
-
possible_extrema.zip(samples.each_cons(2)).inject(Set[]) do |set,(pe,(after,before))|
|
70
|
-
yafter = derivative.calculate(after)
|
71
|
-
ybefore = derivative.calculate(before)
|
72
|
-
kind_of_extremum = NegPosMinMaxExtremumMapping[[yafter/yafter.abs,ybefore/ybefore.abs]]
|
73
|
-
set << Extremum.new(pe,self.calculate(pe), kind_of_extremum) if kind_of_extremum
|
74
|
-
set
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
def curvature_behaviour
|
79
|
-
hash = Hash.new {|h,k|h[k]=Set.new}
|
80
|
-
return hash if self.degree > 5
|
81
|
-
self.derivative.extrema.sort.each_cons(2).group_by do |start,_|
|
82
|
-
kind_of_curvature = AfterExtremaCurvatureMapping[start.kind_of_extremum]
|
83
|
-
end.tap { |h| h.values.each { |v| v.map! { |s,e| Range.new(s.x,e.x) }}}
|
84
|
-
end
|
85
|
-
|
86
59
|
def degree
|
87
60
|
self.terms.keys.max || 0
|
88
61
|
end
|
@@ -99,22 +72,6 @@ module Polynomials
|
|
99
72
|
self.terms.delete_if(&delete_zeros) == other.terms.delete_if(&delete_zeros)
|
100
73
|
end
|
101
74
|
|
102
|
-
def extrema
|
103
|
-
extrema = local_extrema
|
104
|
-
unless self.degree == 0
|
105
|
-
a = self.gt.coefficient
|
106
|
-
max_or_min = (self.degree.even? ? [1.0,1.0] : [-1.0,1.0]).map { |n| (n * a)/a.abs }
|
107
|
-
max_or_min.zip([-Infinity,Infinity]).each do |kind_of_extremum, x|
|
108
|
-
extrema << Extremum.new(x,nil,MinMaxMapping[kind_of_extremum])
|
109
|
-
end
|
110
|
-
end
|
111
|
-
return extrema
|
112
|
-
end
|
113
|
-
|
114
|
-
def inflection_points
|
115
|
-
self.derivative.local_extrema.map { |p| InflectionPoint.new(p.x,self.calculate(p.x)) }.to_set
|
116
|
-
end
|
117
|
-
|
118
75
|
def gt
|
119
76
|
self.terms[self.degree]
|
120
77
|
end
|
@@ -123,6 +80,16 @@ module Polynomials
|
|
123
80
|
self.terms[self.terms.min_by{ |_,t| t.exponent}.first]
|
124
81
|
end
|
125
82
|
|
83
|
+
def pointset(start,stop,step=0.1)
|
84
|
+
data = []
|
85
|
+
x = start
|
86
|
+
while x < stop
|
87
|
+
data << [x,self.(x)]
|
88
|
+
x += step
|
89
|
+
end
|
90
|
+
data << [stop,self.(stop)]
|
91
|
+
end
|
92
|
+
|
126
93
|
def coefficients_till(n)
|
127
94
|
coefficients = []
|
128
95
|
(0..n).each do |e|
|
@@ -141,4 +108,5 @@ module Polynomials
|
|
141
108
|
end
|
142
109
|
protected :alter
|
143
110
|
end
|
111
|
+
|
144
112
|
end
|
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.
|
8
|
+
s.version = "0.4.0"
|
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-18}
|
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.
|
@@ -29,12 +29,14 @@ It can solve polynomial equations with a degree up to 4 by implementing formulas
|
|
29
29
|
"VERSION",
|
30
30
|
"examples/plot_only_mutual_data.rb",
|
31
31
|
"lib/polynomials.rb",
|
32
|
+
"lib/polynomials/analyzable.rb",
|
32
33
|
"lib/polynomials/exceptions.rb",
|
33
34
|
"lib/polynomials/formulas.rb",
|
34
35
|
"lib/polynomials/point.rb",
|
35
36
|
"lib/polynomials/polynomial.rb",
|
36
37
|
"lib/polynomials/term.rb",
|
37
38
|
"polynomials.gemspec",
|
39
|
+
"test/analyzable_test.rb",
|
38
40
|
"test/formulas_test.rb",
|
39
41
|
"test/point_test.rb",
|
40
42
|
"test/polynomial_test.rb",
|
@@ -44,11 +46,10 @@ It can solve polynomial equations with a degree up to 4 by implementing formulas
|
|
44
46
|
s.homepage = %q{http://github.com/mkorfmann/polynomials}
|
45
47
|
s.licenses = ["MIT"]
|
46
48
|
s.require_paths = ["lib"]
|
47
|
-
s.rubygems_version = %q{1.
|
49
|
+
s.rubygems_version = %q{1.6.2}
|
48
50
|
s.summary = %q{parsing and computing data on polynomials}
|
49
51
|
|
50
52
|
if s.respond_to? :specification_version then
|
51
|
-
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
52
53
|
s.specification_version = 3
|
53
54
|
|
54
55
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require "#{File.dirname(File.expand_path(__FILE__))}/test_helper"
|
2
|
+
Infinity = 1.0/0
|
3
|
+
class TestAnalyzable < MiniTest::Unit::TestCase
|
4
|
+
include Polynomials
|
5
|
+
|
6
|
+
def test_local_extrema
|
7
|
+
polynomial = Polynomial.parse('3x^2 + 2x + 1')
|
8
|
+
assert_set_eql(Set[ Extremum.new(-2.0/6.0,polynomial.calculate(-2.0/6.0), :minimum) ], polynomial.local_extrema)
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_local_extrema_with_slope_of_derivative_equal_to_zero
|
12
|
+
polynomial = Polynomial.parse('1x^4')
|
13
|
+
assert_set_eql(Set[ Extremum.new(0.0,polynomial.calculate(0.0), :minimum) ], polynomial.local_extrema)
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_no_local_extrema
|
17
|
+
polynomial = Polynomial.parse('6x^6 - 5x + 50')
|
18
|
+
assert_equal(Set[], polynomial.local_extrema)
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_curvature_behaviour_no_inflection_points
|
22
|
+
polynomial = Polynomial.parse('1 x^4')
|
23
|
+
assert_equal({ left: [[-1.0/0,+1.0/0]] }, polynomial.curvature_behaviour)
|
24
|
+
|
25
|
+
polynomial = Polynomial.parse('-1 x^4')
|
26
|
+
assert_equal({ right: [[-1.0/0,+1.0/0]] }, polynomial.curvature_behaviour)
|
27
|
+
|
28
|
+
polynomial = Polynomial.parse('5x^2')
|
29
|
+
assert_equal({ left: [[-1.0/0,+1.0/0]] }, polynomial.curvature_behaviour)
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_no_curvature
|
33
|
+
polynomials = []
|
34
|
+
polynomials << Polynomial.parse('0')
|
35
|
+
polynomials << Polynomial.parse('4')
|
36
|
+
polynomials.each do |polynomial|
|
37
|
+
assert_equal(nil, polynomial.curvature_behaviour)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_no_local_extrema_of_derivative_leads_to_no_curvature
|
42
|
+
polynomial = Polynomial.parse('50x^10 - 20x^2')
|
43
|
+
assert_equal({:left=>[[-Infinity, Infinity]]}, polynomial.curvature_behaviour)
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_curvature_behaviour_two_inflection_points
|
47
|
+
polynomial = Polynomial.parse('+ 1.0 x^4 + 5.0 x^3 - 1.0 x^2 + 3.0 x + 5.0')
|
48
|
+
assert_equal({left:[[-1.0/0,-2.5649778198], [0.0649778198,1.0/0]], right: [[-2.5649778198,0.0649778198]] } , polynomial.curvature_behaviour)
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_curvature_behaviour_three_inflection_points
|
52
|
+
polynomial = Polynomial.new(20,4,0,-1,-200)
|
53
|
+
assert_equal( {:right=>[[(-1/10),0.0]], :left=>[[-Infinity,(-1/10)], [0.0,Infinity]]}, polynomial.curvature_behaviour)
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_inflection_points
|
57
|
+
polynomial = Polynomial.new(20,4,0,-1,-200)
|
58
|
+
assert_set_eql Set[InflectionPoint.new(-1/10,polynomial.(-1/10)),InflectionPoint.new(0,polynomial.(0))], polynomial.inflection_points
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_strives_for
|
62
|
+
polynomial = Polynomial.new(2,0,0)
|
63
|
+
assert_equal [Infinity,Infinity], polynomial.strives_for
|
64
|
+
polynomial = Polynomial.new(-2,0,0)
|
65
|
+
assert_equal [-Infinity,-Infinity], polynomial.strives_for
|
66
|
+
polynomial = Polynomial.new(2,0,0,0)
|
67
|
+
assert_equal [-Infinity,Infinity], polynomial.strives_for
|
68
|
+
polynomial = Polynomial.new(-2,0,0,0)
|
69
|
+
assert_equal [Infinity,-Infinity], polynomial.strives_for
|
70
|
+
polynomial = Polynomial.new(2)
|
71
|
+
assert_equal nil, polynomial.strives_for
|
72
|
+
|
73
|
+
polynomial = Polynomial.parse('+ 1.0 x^4 + 5.0 x^3 - 1.0 x^2 + 3.0 x + 5.0').derivative
|
74
|
+
assert_equal [-Infinity,Infinity], polynomial.strives_for
|
75
|
+
end
|
76
|
+
end
|
data/test/polynomial_test.rb
CHANGED
@@ -44,65 +44,6 @@ class TestPolynomial < MiniTest::Unit::TestCase
|
|
44
44
|
assert_equal 5,Polynomial.parse('3x^5 - 5 x + 3').degree
|
45
45
|
end
|
46
46
|
|
47
|
-
def test_extrema
|
48
|
-
polynomial = Polynomial.parse('3x^2 + 2x + 1')
|
49
|
-
assert_set_eql(Set[ Extremum.new(-2.0/6.0,polynomial.calculate(-2.0/6.0), :minimum) ], polynomial.local_extrema)
|
50
|
-
polynomial = Polynomial.parse('5x^3 - 5x^2 + 2x - 2')
|
51
|
-
end
|
52
|
-
|
53
|
-
def test_extrema_with_slope_of_derivative_equal_to_zero
|
54
|
-
polynomial = Polynomial.parse('1x^4')
|
55
|
-
assert_set_eql(Set[ Extremum.new(0.0,polynomial.calculate(0.0), :minimum) ], polynomial.local_extrema)
|
56
|
-
assert_set_eql(Set[*[Infinity,-Infinity].map { |x| Extremum.new(x,nil, :maximum)}, Extremum.new(0.0,polynomial.calculate(0.0), :minimum) ], polynomial.extrema)
|
57
|
-
end
|
58
|
-
|
59
|
-
def test_no_local_extrema
|
60
|
-
polynomial = Polynomial.parse('6x^6 - 5x + 50')
|
61
|
-
assert_equal(Set[], polynomial.local_extrema)
|
62
|
-
assert_set_eql(Set[*[ -1.0/0, 1.0/0 ].map { |x| Extremum.new(x, nil, :maximum)}], polynomial.extrema)
|
63
|
-
end
|
64
|
-
|
65
|
-
def test_curvature_behaviour_no_inflection_points
|
66
|
-
polynomial = Polynomial.parse('1 x^4')
|
67
|
-
assert_equal({ left: [-1.0/0..+1.0/0] }, polynomial.curvature_behaviour)
|
68
|
-
|
69
|
-
polynomial = Polynomial.parse('-1 x^4')
|
70
|
-
assert_equal({ right: [-1.0/0..+1.0/0] }, polynomial.curvature_behaviour)
|
71
|
-
|
72
|
-
polynomial = Polynomial.parse('5x^2')
|
73
|
-
assert_equal({ left: [-1.0/0..+1.0/0] }, polynomial.curvature_behaviour)
|
74
|
-
end
|
75
|
-
|
76
|
-
def test_no_extremums
|
77
|
-
polynomial = Polynomial.parse('5')
|
78
|
-
assert_equal(Set[], polynomial.extrema)
|
79
|
-
end
|
80
|
-
|
81
|
-
def test_no_curvature
|
82
|
-
polynomials = []
|
83
|
-
polynomials << Polynomial.parse('5 x + 4')
|
84
|
-
polynomials << Polynomial.parse('4')
|
85
|
-
polynomials.each do |polynomial|
|
86
|
-
assert_equal({}, polynomial.curvature_behaviour)
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
def test_local_extrema_of_derivative_leads_to_no_curvature
|
91
|
-
polynomial = Polynomial.parse('50x^10 - 20x^2')
|
92
|
-
assert_equal({}, polynomial.curvature_behaviour)
|
93
|
-
end
|
94
|
-
|
95
|
-
def test_curvature_behaviour_two_inflection_points
|
96
|
-
polynomial = Polynomial.parse('+ 1.0 x^4 + 5.0 x^3 - 1.0 x^2 + 3.0 x + 5.0')
|
97
|
-
assert_equal({left:[-1.0/0..-2.5649778198, 0.0649778198..1.0/0], right: [-2.5649778198..0.0649778198] } , polynomial.curvature_behaviour)
|
98
|
-
end
|
99
|
-
|
100
|
-
def test_curvature_behaviour_three_inflection_points
|
101
|
-
polynomial = Polynomial.new(20,4,0,-1,-200)
|
102
|
-
assert_equal( {:right=>[(-1/10)..0.0], :left=>[-Infinity..(-1/10), 0.0..Infinity]}, polynomial.curvature_behaviour)
|
103
|
-
end
|
104
|
-
|
105
|
-
|
106
47
|
def test_efficient_roots_calculation
|
107
48
|
polynomial = Polynomial.parse('200x^2342435 + 6x^20')
|
108
49
|
assert_set_eql(Set[Root.new(0.0)], polynomial.roots)
|
@@ -123,8 +64,13 @@ class TestPolynomial < MiniTest::Unit::TestCase
|
|
123
64
|
assert_equal '5x^3 + 2x - 1', polynomial.to_s
|
124
65
|
end
|
125
66
|
|
126
|
-
def
|
127
|
-
polynomial = Polynomial.new(
|
128
|
-
|
67
|
+
def test_pointset
|
68
|
+
polynomial = Polynomial.new(1,0,0)
|
69
|
+
assert_equal (-2..2).step(0.5).map { |x| [x,x**2] }, polynomial.pointset(-2,2,0.5)
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_pointset_with_float_start
|
73
|
+
polynomial = Polynomial.new(1,0,0)
|
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)
|
129
75
|
end
|
130
76
|
end
|
metadata
CHANGED
@@ -1,12 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: polynomials
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
prerelease:
|
5
|
-
|
6
|
-
- 0
|
7
|
-
- 3
|
8
|
-
- 0
|
9
|
-
version: 0.3.0
|
4
|
+
prerelease:
|
5
|
+
version: 0.4.0
|
10
6
|
platform: ruby
|
11
7
|
authors:
|
12
8
|
- Manuel Korfmann
|
@@ -14,7 +10,7 @@ autorequire:
|
|
14
10
|
bindir: bin
|
15
11
|
cert_chain: []
|
16
12
|
|
17
|
-
date: 2011-05-
|
13
|
+
date: 2011-05-18 00:00:00 +02:00
|
18
14
|
default_executable:
|
19
15
|
dependencies:
|
20
16
|
- !ruby/object:Gem::Dependency
|
@@ -24,10 +20,6 @@ dependencies:
|
|
24
20
|
requirements:
|
25
21
|
- - ~>
|
26
22
|
- !ruby/object:Gem::Version
|
27
|
-
segments:
|
28
|
-
- 1
|
29
|
-
- 0
|
30
|
-
- 0
|
31
23
|
version: 1.0.0
|
32
24
|
type: :development
|
33
25
|
prerelease: false
|
@@ -39,10 +31,6 @@ dependencies:
|
|
39
31
|
requirements:
|
40
32
|
- - ~>
|
41
33
|
- !ruby/object:Gem::Version
|
42
|
-
segments:
|
43
|
-
- 1
|
44
|
-
- 6
|
45
|
-
- 0
|
46
34
|
version: 1.6.0
|
47
35
|
type: :development
|
48
36
|
prerelease: false
|
@@ -54,8 +42,6 @@ dependencies:
|
|
54
42
|
requirements:
|
55
43
|
- - ">="
|
56
44
|
- !ruby/object:Gem::Version
|
57
|
-
segments:
|
58
|
-
- 0
|
59
45
|
version: "0"
|
60
46
|
type: :development
|
61
47
|
prerelease: false
|
@@ -67,8 +53,6 @@ dependencies:
|
|
67
53
|
requirements:
|
68
54
|
- - ">="
|
69
55
|
- !ruby/object:Gem::Version
|
70
|
-
segments:
|
71
|
-
- 0
|
72
56
|
version: "0"
|
73
57
|
type: :development
|
74
58
|
prerelease: false
|
@@ -80,8 +64,6 @@ dependencies:
|
|
80
64
|
requirements:
|
81
65
|
- - ">="
|
82
66
|
- !ruby/object:Gem::Version
|
83
|
-
segments:
|
84
|
-
- 0
|
85
67
|
version: "0"
|
86
68
|
type: :development
|
87
69
|
prerelease: false
|
@@ -93,8 +75,6 @@ dependencies:
|
|
93
75
|
requirements:
|
94
76
|
- - ">="
|
95
77
|
- !ruby/object:Gem::Version
|
96
|
-
segments:
|
97
|
-
- 0
|
98
78
|
version: "0"
|
99
79
|
type: :development
|
100
80
|
prerelease: false
|
@@ -120,12 +100,14 @@ files:
|
|
120
100
|
- VERSION
|
121
101
|
- examples/plot_only_mutual_data.rb
|
122
102
|
- lib/polynomials.rb
|
103
|
+
- lib/polynomials/analyzable.rb
|
123
104
|
- lib/polynomials/exceptions.rb
|
124
105
|
- lib/polynomials/formulas.rb
|
125
106
|
- lib/polynomials/point.rb
|
126
107
|
- lib/polynomials/polynomial.rb
|
127
108
|
- lib/polynomials/term.rb
|
128
109
|
- polynomials.gemspec
|
110
|
+
- test/analyzable_test.rb
|
129
111
|
- test/formulas_test.rb
|
130
112
|
- test/point_test.rb
|
131
113
|
- test/polynomial_test.rb
|
@@ -145,7 +127,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
145
127
|
requirements:
|
146
128
|
- - ">="
|
147
129
|
- !ruby/object:Gem::Version
|
148
|
-
hash:
|
130
|
+
hash: 1327775676768691689
|
149
131
|
segments:
|
150
132
|
- 0
|
151
133
|
version: "0"
|
@@ -154,13 +136,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
154
136
|
requirements:
|
155
137
|
- - ">="
|
156
138
|
- !ruby/object:Gem::Version
|
157
|
-
segments:
|
158
|
-
- 0
|
159
139
|
version: "0"
|
160
140
|
requirements: []
|
161
141
|
|
162
142
|
rubyforge_project:
|
163
|
-
rubygems_version: 1.
|
143
|
+
rubygems_version: 1.6.2
|
164
144
|
signing_key:
|
165
145
|
specification_version: 3
|
166
146
|
summary: parsing and computing data on polynomials
|