polynomials 0.1.2 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION +1 -1
- data/lib/polynomials.rb +13 -11
- data/polynomials.gemspec +2 -3
- data/test/math_test.rb +23 -23
- data/test/polynomial_test.rb +3 -0
- metadata +4 -5
- data/polynomial.gemspec +0 -76
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.4
|
data/lib/polynomials.rb
CHANGED
@@ -43,9 +43,9 @@ class Polynomial
|
|
43
43
|
return new_function
|
44
44
|
end
|
45
45
|
|
46
|
-
def
|
46
|
+
def roots
|
47
47
|
if terms.keys.none?(&:zero?)
|
48
|
-
self.alter { |nf, term| nf.terms[term.exponent-1].coefficient = term.coefficient }.
|
48
|
+
self.alter { |nf, term| nf.terms[term.exponent-1].coefficient = term.coefficient }.roots << 0.0
|
49
49
|
else
|
50
50
|
case self.degree
|
51
51
|
when 1
|
@@ -64,18 +64,20 @@ class Polynomial
|
|
64
64
|
|
65
65
|
def local_extremums
|
66
66
|
derivative = self.derivative
|
67
|
-
possible_extremums = derivative.nulls.sort
|
68
67
|
max_min_extremum = Hash.new { |hash,key| hash[key] = Set.new }
|
68
|
+
unless derivative.degree == 0
|
69
|
+
possible_extremums = derivative.roots.sort
|
69
70
|
|
70
|
-
|
71
|
-
|
72
|
-
|
71
|
+
samples = ([possible_extremums.first - 1] + possible_extremums.sort + [possible_extremums.last + 1]).each_cons(2).map do |before,after|
|
72
|
+
(before + after)/2
|
73
|
+
end
|
73
74
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
75
|
+
possible_extremums.zip(samples.each_cons(2)).each do |pe,(after,before)|
|
76
|
+
yafter = derivative.calculate(after)
|
77
|
+
ybefore = derivative.calculate(before)
|
78
|
+
kind_of_extremum = NegPosMinMaxExtremumMapping[[yafter/yafter.abs,ybefore/ybefore.abs]]
|
79
|
+
max_min_extremum[kind_of_extremum] << pe if kind_of_extremum
|
80
|
+
end
|
79
81
|
end
|
80
82
|
return max_min_extremum
|
81
83
|
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.1.
|
8
|
+
s.version = "0.1.4"
|
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-13}
|
13
13
|
s.description = %q{
|
14
14
|
* Parses polynomials
|
15
15
|
* Provides useful methods on top of the Polynomial class, like:
|
@@ -34,7 +34,6 @@ Gem::Specification.new do |s|
|
|
34
34
|
"lib/core_ext/math.rb",
|
35
35
|
"lib/polynomials.rb",
|
36
36
|
"lib/term.rb",
|
37
|
-
"polynomial.gemspec",
|
38
37
|
"polynomials.gemspec",
|
39
38
|
"test/helper.rb",
|
40
39
|
"test/math_test.rb",
|
data/test/math_test.rb
CHANGED
@@ -1,65 +1,65 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
class TestMath < MiniTest::Unit::TestCase
|
3
|
-
def
|
3
|
+
def test_roots_for_constant_functions
|
4
4
|
polynomial = Polynomial.parse('5')
|
5
|
-
assert_equal Set[],polynomial.
|
5
|
+
assert_equal Set[],polynomial.roots
|
6
6
|
end
|
7
7
|
|
8
|
-
def
|
8
|
+
def test_roots_for_linear_functions
|
9
9
|
polynomial = Polynomial.parse('5x + 2')
|
10
|
-
assert_equal Set[-2.0/5.0],polynomial.
|
10
|
+
assert_equal Set[-2.0/5.0],polynomial.roots
|
11
11
|
end
|
12
12
|
|
13
|
-
def
|
13
|
+
def test_roots_quadratic_functions
|
14
14
|
polynomial = Polynomial.parse('3x^2 + 2x - 40')
|
15
15
|
p = 2.0/3.0
|
16
16
|
q = -40.0/3.0
|
17
17
|
root = Math.sqrt((p/2)**2 - q)
|
18
18
|
fraction = -(p/2)
|
19
|
-
assert_equal Set[(fraction - root).round(10), (fraction + root).round(10)],polynomial.
|
19
|
+
assert_equal Set[(fraction - root).round(10), (fraction + root).round(10)],polynomial.roots
|
20
20
|
|
21
21
|
polynomial = Polynomial.parse('3x^2 - 40')
|
22
|
-
assert_equal Set[Math.sqrt(40.0/3).round(10), -Math.sqrt(40.0/3).round(10)], polynomial.
|
22
|
+
assert_equal Set[Math.sqrt(40.0/3).round(10), -Math.sqrt(40.0/3).round(10)], polynomial.roots
|
23
23
|
end
|
24
24
|
|
25
|
-
def
|
25
|
+
def test_roots_for_cubic_functions
|
26
26
|
polynomial = Polynomial.parse('1x^3 + 1x^2 + 2x - 1')
|
27
|
-
assert_equal(1, polynomial.
|
28
|
-
assert_equal Set[0.3926467817], polynomial.
|
27
|
+
assert_equal(1, polynomial.roots.size)
|
28
|
+
assert_equal Set[0.3926467817], polynomial.roots
|
29
29
|
|
30
30
|
polynomial = Polynomial.parse('2x^3 - 4x^2 - 22x + 24')
|
31
|
-
assert_equal Set[4.0,-3.0,1.0], polynomial.
|
31
|
+
assert_equal Set[4.0,-3.0,1.0], polynomial.roots
|
32
32
|
end
|
33
33
|
|
34
|
-
def
|
34
|
+
def test_roots_for_cubic_functions_with_complex
|
35
35
|
coefficients = Polynomial.parse('3x^3 - 10x^2 + 14x + 27').terms.first(4).map{ |t| t.last.coefficient }
|
36
36
|
assert_equal Set[-1.0, Complex(2.1666666666666634,2.0749832663314605), Complex(2.1666666666666634,-2.0749832663314605)],Math.roots_of_cubic_function(*coefficients, true)
|
37
37
|
end
|
38
38
|
|
39
|
-
def
|
39
|
+
def test_roots_for_cubic_functions_one_real_all_equal
|
40
40
|
polynomial = Polynomial.parse('1x^3 + 6x^2 + 12x + 8')
|
41
|
-
assert_equal(1, polynomial.
|
42
|
-
assert_equal(-2, polynomial.
|
41
|
+
assert_equal(1, polynomial.roots.size)
|
42
|
+
assert_equal(-2, polynomial.roots.first)
|
43
43
|
end
|
44
44
|
|
45
|
-
def
|
45
|
+
def test_roots_for_quartic_functions
|
46
46
|
polynomial = Polynomial.parse('3x^4 + 6x^3 - 123x^2 - 126x + 1080')
|
47
|
-
assert_equal Set[5.0, 3.0, -4.0, -6.0], polynomial.
|
47
|
+
assert_equal Set[5.0, 3.0, -4.0, -6.0], polynomial.roots
|
48
48
|
|
49
49
|
polynomial = Polynomial.parse('-20x^4 + 5x^3 + 17x^2 - 29x + 87')
|
50
|
-
assert_equal Set[-1.6820039266,1.4875831103], polynomial.
|
50
|
+
assert_equal Set[-1.6820039266,1.4875831103], polynomial.roots
|
51
51
|
end
|
52
52
|
|
53
|
-
def
|
53
|
+
def test_roots_without_term_with_exponent_of_zero
|
54
54
|
polynomial = Polynomial.parse('3x^3 + 3x^2')
|
55
|
-
assert_equal Set[0.0,-1.0], polynomial.
|
55
|
+
assert_equal Set[0.0,-1.0], polynomial.roots
|
56
56
|
|
57
57
|
polynomial = Polynomial.parse('1 x^4')
|
58
|
-
assert_equal Set[0.0], polynomial.
|
58
|
+
assert_equal Set[0.0], polynomial.roots
|
59
59
|
end
|
60
60
|
|
61
|
-
def
|
61
|
+
def test_roots_biquadratic_equation
|
62
62
|
polynomial = Polynomial.parse('4x^4 + 2x^2 - 1')
|
63
|
-
assert_equal Set[0.5558929703, -0.5558929703], polynomial.
|
63
|
+
assert_equal Set[0.5558929703, -0.5558929703], polynomial.roots
|
64
64
|
end
|
65
65
|
end
|
data/test/polynomial_test.rb
CHANGED
@@ -56,6 +56,9 @@ class TestPolynomial < MiniTest::Unit::TestCase
|
|
56
56
|
|
57
57
|
polynomial = Polynomial.parse('-1 x^4')
|
58
58
|
assert_equal({ right: Set[-1.0/0..+1.0/0] }, polynomial.curvature_behaviour)
|
59
|
+
|
60
|
+
polynomial = Polynomial.parse('5x^2')
|
61
|
+
assert_equal({ left: Set[-1.0/0..+1.0/0] }, polynomial.curvature_behaviour)
|
59
62
|
end
|
60
63
|
|
61
64
|
def test_no_curve_and_no_inflection_points
|
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
|
+
- 4
|
9
|
+
version: 0.1.4
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Manuel Korfmann
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-05-
|
17
|
+
date: 2011-05-13 00:00:00 +02:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -109,7 +109,6 @@ files:
|
|
109
109
|
- lib/core_ext/math.rb
|
110
110
|
- lib/polynomials.rb
|
111
111
|
- lib/term.rb
|
112
|
-
- polynomial.gemspec
|
113
112
|
- polynomials.gemspec
|
114
113
|
- test/helper.rb
|
115
114
|
- test/math_test.rb
|
@@ -130,7 +129,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
130
129
|
requirements:
|
131
130
|
- - ">="
|
132
131
|
- !ruby/object:Gem::Version
|
133
|
-
hash: -
|
132
|
+
hash: -100517783123906074
|
134
133
|
segments:
|
135
134
|
- 0
|
136
135
|
version: "0"
|
data/polynomial.gemspec
DELETED
@@ -1,76 +0,0 @@
|
|
1
|
-
# Generated by jeweler
|
2
|
-
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
-
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
-
# -*- encoding: utf-8 -*-
|
5
|
-
|
6
|
-
Gem::Specification.new do |s|
|
7
|
-
s.name = %q{polynomial}
|
8
|
-
s.version = "0.1.1"
|
9
|
-
|
10
|
-
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
-
s.authors = ["Manuel Korfmann"]
|
12
|
-
s.date = %q{2011-05-12}
|
13
|
-
s.description = %q{
|
14
|
-
* Parses polynomials
|
15
|
-
* Provides useful methods on top of the Polynomial class, like:
|
16
|
-
** Polynomial#local_extremums
|
17
|
-
** Polynomial#nulls
|
18
|
-
** Polynomial#curvature_behaviour
|
19
|
-
* Implements root's finding formulas for quadratic, cubic and quartic functions
|
20
|
-
}
|
21
|
-
s.email = %q{manu@korfmann.info}
|
22
|
-
s.extra_rdoc_files = [
|
23
|
-
"LICENSE.txt",
|
24
|
-
"README.rdoc"
|
25
|
-
]
|
26
|
-
s.files = [
|
27
|
-
".document",
|
28
|
-
"Gemfile",
|
29
|
-
"Gemfile.lock",
|
30
|
-
"LICENSE.txt",
|
31
|
-
"README.rdoc",
|
32
|
-
"Rakefile",
|
33
|
-
"VERSION",
|
34
|
-
"lib/core_ext/math.rb",
|
35
|
-
"lib/polynomials.rb",
|
36
|
-
"lib/term.rb",
|
37
|
-
"polynomial.gemspec",
|
38
|
-
"polynomials.gemspec",
|
39
|
-
"test/helper.rb",
|
40
|
-
"test/math_test.rb",
|
41
|
-
"test/polynomial_test.rb",
|
42
|
-
"test/term_test.rb",
|
43
|
-
"test/test_helper.rb"
|
44
|
-
]
|
45
|
-
s.homepage = %q{http://github.com/mkorfmann/polynomials}
|
46
|
-
s.licenses = ["MIT"]
|
47
|
-
s.require_paths = ["lib"]
|
48
|
-
s.rubygems_version = %q{1.3.7}
|
49
|
-
s.summary = %q{Polyomial calculations library}
|
50
|
-
|
51
|
-
if s.respond_to? :specification_version then
|
52
|
-
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
53
|
-
s.specification_version = 3
|
54
|
-
|
55
|
-
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
56
|
-
s.add_development_dependency(%q<shoulda>, [">= 0"])
|
57
|
-
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
58
|
-
s.add_development_dependency(%q<jeweler>, ["~> 1.6.0"])
|
59
|
-
s.add_development_dependency(%q<rcov>, [">= 0"])
|
60
|
-
s.add_development_dependency(%q<awesome_print>, [">= 0"])
|
61
|
-
else
|
62
|
-
s.add_dependency(%q<shoulda>, [">= 0"])
|
63
|
-
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
64
|
-
s.add_dependency(%q<jeweler>, ["~> 1.6.0"])
|
65
|
-
s.add_dependency(%q<rcov>, [">= 0"])
|
66
|
-
s.add_dependency(%q<awesome_print>, [">= 0"])
|
67
|
-
end
|
68
|
-
else
|
69
|
-
s.add_dependency(%q<shoulda>, [">= 0"])
|
70
|
-
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
71
|
-
s.add_dependency(%q<jeweler>, ["~> 1.6.0"])
|
72
|
-
s.add_dependency(%q<rcov>, [">= 0"])
|
73
|
-
s.add_dependency(%q<awesome_print>, [">= 0"])
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|