polynomials 0.1.6 → 0.1.7
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/Gemfile +0 -1
- data/Gemfile.lock +0 -2
- data/VERSION +1 -1
- data/lib/polynomials.rb +13 -15
- data/polynomials.gemspec +2 -4
- data/tags +91 -0
- data/test/polynomial_test.rb +5 -0
- metadata +12 -24
data/Gemfile
CHANGED
@@ -6,7 +6,6 @@ source "http://rubygems.org"
|
|
6
6
|
# Add dependencies to develop your gem here.
|
7
7
|
# Include everything needed to run rake, tests, features, etc.
|
8
8
|
group :development do
|
9
|
-
gem "shoulda", ">= 0"
|
10
9
|
gem "bundler", "~> 1.0.0"
|
11
10
|
gem "jeweler", "~> 1.6.0"
|
12
11
|
gem "rcov", ">= 0"
|
data/Gemfile.lock
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.7
|
data/lib/polynomials.rb
CHANGED
@@ -38,7 +38,7 @@ class Polynomial
|
|
38
38
|
end
|
39
39
|
|
40
40
|
def roots
|
41
|
-
if terms.keys.none?(&:zero?)
|
41
|
+
if !terms.empty? and terms.keys.none?(&:zero?)
|
42
42
|
self.alter { |nf, term| nf.terms[term.exponent-1].coefficient = term.coefficient }.roots << 0.0
|
43
43
|
else
|
44
44
|
case self.degree
|
@@ -61,7 +61,6 @@ class Polynomial
|
|
61
61
|
max_min_extremum = Hash.new { |hash,key| hash[key] = Set.new }
|
62
62
|
possible_extrema = derivative.roots.sort
|
63
63
|
unless possible_extrema.empty?
|
64
|
-
|
65
64
|
samples = ([possible_extrema.first - 1] + possible_extrema.sort + [possible_extrema.last + 1]).each_cons(2).map do |before,after|
|
66
65
|
(before + after)/2
|
67
66
|
end
|
@@ -77,20 +76,17 @@ class Polynomial
|
|
77
76
|
end
|
78
77
|
|
79
78
|
def curvature_behaviour
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
curvature_behaviour
|
85
|
-
|
86
|
-
curvature_behaviour[AfterextremaCurvatureMapping[extrema.find { |k,v| v.include?(start_point) }.first]] << Range.new(start_point,end_point)
|
87
|
-
end
|
79
|
+
hash = Hash.new {|h,k|h[k]=Set.new}
|
80
|
+
extrema = self.derivative.extrema
|
81
|
+
extrema.values.inject(Set[],&:|).sort.each_cons(2).inject(hash) do |curvature_behaviour,(start_point,end_point)|
|
82
|
+
kind_of_curvature = AfterextremaCurvatureMapping[extrema.find { |k,v| v.include?(start_point) }.first]
|
83
|
+
curvature_behaviour[kind_of_curvature] << Range.new(start_point,end_point)
|
84
|
+
curvature_behaviour
|
88
85
|
end
|
89
|
-
return curvature_behaviour
|
90
86
|
end
|
91
87
|
|
92
88
|
def degree
|
93
|
-
self.terms.keys.max
|
89
|
+
self.terms.keys.max || 0
|
94
90
|
end
|
95
91
|
|
96
92
|
def to_s
|
@@ -107,9 +103,11 @@ class Polynomial
|
|
107
103
|
def extrema
|
108
104
|
extrema = local_extrema
|
109
105
|
a = self.terms[self.degree].coefficient
|
110
|
-
|
111
|
-
|
112
|
-
|
106
|
+
unless self.degree == 0
|
107
|
+
max_or_min = (self.degree.even? ? [1,1] : [-1,1]).map { |n| (n * a)/a.abs }
|
108
|
+
extrema[MinMaxMapping[max_or_min.first]] << -1.0/0
|
109
|
+
extrema[MinMaxMapping[max_or_min.last]] << 1.0/0
|
110
|
+
end
|
113
111
|
return extrema
|
114
112
|
end
|
115
113
|
|
data/polynomials.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
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.7"
|
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"]
|
@@ -31,6 +31,7 @@ It can solve polynomial equations with a degree up to 4 by implementing formulas
|
|
31
31
|
"lib/polynomials.rb",
|
32
32
|
"lib/term.rb",
|
33
33
|
"polynomials.gemspec",
|
34
|
+
"tags",
|
34
35
|
"test/helper.rb",
|
35
36
|
"test/math_test.rb",
|
36
37
|
"test/polynomial_test.rb",
|
@@ -48,20 +49,17 @@ It can solve polynomial equations with a degree up to 4 by implementing formulas
|
|
48
49
|
s.specification_version = 3
|
49
50
|
|
50
51
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
51
|
-
s.add_development_dependency(%q<shoulda>, [">= 0"])
|
52
52
|
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
53
53
|
s.add_development_dependency(%q<jeweler>, ["~> 1.6.0"])
|
54
54
|
s.add_development_dependency(%q<rcov>, [">= 0"])
|
55
55
|
s.add_development_dependency(%q<awesome_print>, [">= 0"])
|
56
56
|
else
|
57
|
-
s.add_dependency(%q<shoulda>, [">= 0"])
|
58
57
|
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
59
58
|
s.add_dependency(%q<jeweler>, ["~> 1.6.0"])
|
60
59
|
s.add_dependency(%q<rcov>, [">= 0"])
|
61
60
|
s.add_dependency(%q<awesome_print>, [">= 0"])
|
62
61
|
end
|
63
62
|
else
|
64
|
-
s.add_dependency(%q<shoulda>, [">= 0"])
|
65
63
|
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
66
64
|
s.add_dependency(%q<jeweler>, ["~> 1.6.0"])
|
67
65
|
s.add_dependency(%q<rcov>, [">= 0"])
|
data/tags
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
!_TAG_FILE_FORMAT 2 /extended format; --format=1 will not append ;" to lines/
|
2
|
+
!_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted, 2=foldcase/
|
3
|
+
!_TAG_PROGRAM_AUTHOR Darren Hiebert /dhiebert@users.sourceforge.net/
|
4
|
+
!_TAG_PROGRAM_NAME Exuberant Ctags //
|
5
|
+
!_TAG_PROGRAM_URL http://ctags.sourceforge.net /official site/
|
6
|
+
!_TAG_PROGRAM_VERSION 5.8 //
|
7
|
+
== lib/polynomials.rb /^ def ==(other)$/;" f class:Polynomial
|
8
|
+
== lib/term.rb /^ def ==(other)$/;" f class:Term
|
9
|
+
AfterExtremumsCurvatureMapping rdoc/Polynomial.html /^ <dt><a name="AfterExtremumsCurvatureMapping">AfterExtremumsCurvatureMapping<\/a><\/dt>$/;" a
|
10
|
+
Math lib/core_ext/math.rb /^module Math$/;" m
|
11
|
+
MinMaxMapping rdoc/Polynomial.html /^ <dt><a name="MinMaxMapping">MinMaxMapping<\/a><\/dt>$/;" a
|
12
|
+
NegPosMinMaxExtremumMapping rdoc/Polynomial.html /^ <dt><a name="NegPosMinMaxExtremumMapping">NegPosMinMaxExtremumMapping<\/a><\/dt>$/;" a
|
13
|
+
Polynomial lib/polynomials.rb /^class Polynomial$/;" c
|
14
|
+
Term lib/term.rb /^class Term$/;" c
|
15
|
+
Test test/helper.rb /^class Test::Unit::TestCase$/;" c
|
16
|
+
TestMath test/math_test.rb /^class TestMath < MiniTest::Unit::TestCase$/;" c
|
17
|
+
TestPolynomial test/polynomial_test.rb /^class TestPolynomial < MiniTest::Unit::TestCase$/;" c
|
18
|
+
TestTerm test/term_test.rb /^class TestTerm < MiniTest::Unit::TestCase$/;" c
|
19
|
+
alter lib/polynomials.rb /^ def alter$/;" f class:Polynomial
|
20
|
+
calculate lib/polynomials.rb /^ def calculate(x)$/;" f class:Polynomial
|
21
|
+
coefficient rdoc/Term.html /^ <a name="coefficient"><\/a>$/;" a
|
22
|
+
coefficient= rdoc/Term.html /^ <a name="coefficient="><\/a>$/;" a
|
23
|
+
coefficients_till lib/polynomials.rb /^ def coefficients_till(n)$/;" f class:Polynomial
|
24
|
+
curvature_behaviour lib/polynomials.rb /^ def curvature_behaviour$/;" f class:Polynomial
|
25
|
+
degree lib/polynomials.rb /^ def degree$/;" f class:Polynomial
|
26
|
+
derivative lib/polynomials.rb /^ def derivative$/;" f class:Polynomial
|
27
|
+
dup lib/term.rb /^ def dup$/;" f class:Term
|
28
|
+
exponent rdoc/Term.html /^ <a name="exponent"><\/a>$/;" a
|
29
|
+
exponent= rdoc/Term.html /^ <a name="exponent="><\/a>$/;" a
|
30
|
+
extrema lib/polynomials.rb /^ def extrema$/;" f class:Polynomial
|
31
|
+
initialize lib/polynomials.rb /^ def initialize$/;" f class:Polynomial
|
32
|
+
initialize lib/term.rb /^ def initialize(exponent = 0, coefficient = 0)$/;" f class:Term
|
33
|
+
local_extrema lib/polynomials.rb /^ def local_extrema$/;" f class:Polynomial
|
34
|
+
method-c-new rdoc/Polynomial.html /^ <a name="method-c-new"><\/a>$/;" a
|
35
|
+
method-c-new rdoc/Term.html /^ <a name="method-c-new"><\/a>$/;" a
|
36
|
+
method-c-parse rdoc/Polynomial.html /^ <a name="method-c-parse"><\/a>$/;" a
|
37
|
+
method-c-parse rdoc/Term.html /^ <a name="method-c-parse"><\/a>$/;" a
|
38
|
+
method-c-roots_of_cubic_function rdoc/Math.html /^ <a name="method-c-roots_of_cubic_function"><\/a>$/;" a
|
39
|
+
method-c-roots_of_quadratic_function rdoc/Math.html /^ <a name="method-c-roots_of_quadratic_function"><\/a>$/;" a
|
40
|
+
method-c-roots_of_quartic_function rdoc/Math.html /^ <a name="method-c-roots_of_quartic_function"><\/a>$/;" a
|
41
|
+
method-i-%3D%3D rdoc/Polynomial.html /^ <a name="method-i-%3D%3D"><\/a>$/;" a
|
42
|
+
method-i-%3D%3D rdoc/Term.html /^ <a name="method-i-%3D%3D"><\/a>$/;" a
|
43
|
+
method-i-alter rdoc/Polynomial.html /^ <a name="method-i-alter"><\/a>$/;" a
|
44
|
+
method-i-calculate rdoc/Polynomial.html /^ <a name="method-i-calculate"><\/a>$/;" a
|
45
|
+
method-i-coefficients_till rdoc/Polynomial.html /^ <a name="method-i-coefficients_till"><\/a>$/;" a
|
46
|
+
method-i-curvature_behaviour rdoc/Polynomial.html /^ <a name="method-i-curvature_behaviour"><\/a>$/;" a
|
47
|
+
method-i-degree rdoc/Polynomial.html /^ <a name="method-i-degree"><\/a>$/;" a
|
48
|
+
method-i-derivative rdoc/Polynomial.html /^ <a name="method-i-derivative"><\/a>$/;" a
|
49
|
+
method-i-dup rdoc/Term.html /^ <a name="method-i-dup"><\/a>$/;" a
|
50
|
+
method-i-extremums rdoc/Polynomial.html /^ <a name="method-i-extremums"><\/a>$/;" a
|
51
|
+
method-i-local_extremums rdoc/Polynomial.html /^ <a name="method-i-local_extremums"><\/a>$/;" a
|
52
|
+
method-i-nulls rdoc/Polynomial.html /^ <a name="method-i-nulls"><\/a>$/;" a
|
53
|
+
method-i-to_s rdoc/Polynomial.html /^ <a name="method-i-to_s"><\/a>$/;" a
|
54
|
+
method-i-to_s rdoc/Term.html /^ <a name="method-i-to_s"><\/a>$/;" a
|
55
|
+
parse lib/polynomials.rb /^ def self.parse(string)$/;" F class:Polynomial
|
56
|
+
parse lib/term.rb /^ def self.parse(string)$/;" F class:Term
|
57
|
+
roots lib/polynomials.rb /^ def roots$/;" f class:Polynomial
|
58
|
+
roots_of_cubic_function lib/core_ext/math.rb /^ def self.roots_of_cubic_function(a,b,c,d,with_complex = false)$/;" F class:Math
|
59
|
+
roots_of_quadratic_function lib/core_ext/math.rb /^ def self.roots_of_quadratic_function(a,b,c)$/;" F class:Math.roots_of_cubic_function
|
60
|
+
roots_of_quartic_function lib/core_ext/math.rb /^ def self.roots_of_quartic_function(*args)$/;" F class:Math.roots_of_cubic_function
|
61
|
+
terms rdoc/Polynomial.html /^ <a name="terms"><\/a>$/;" a
|
62
|
+
terms= rdoc/Polynomial.html /^ <a name="terms="><\/a>$/;" a
|
63
|
+
test_calculation test/polynomial_test.rb /^ def test_calculation$/;" f class:TestPolynomial
|
64
|
+
test_degree test/polynomial_test.rb /^ def test_degree$/;" f class:TestPolynomial
|
65
|
+
test_derivative_functions_with_higher_degree test/polynomial_test.rb /^ def test_derivative_functions_with_higher_degree$/;" f class:TestPolynomial
|
66
|
+
test_derivative_of_linear_functions test/polynomial_test.rb /^ def test_derivative_of_linear_functions$/;" f class:TestPolynomial
|
67
|
+
test_equality test/polynomial_test.rb /^ def test_equality$/;" f class:TestPolynomial
|
68
|
+
test_extrema test/polynomial_test.rb /^ def test_extrema$/;" f class:TestPolynomial
|
69
|
+
test_extrema_with_slope_of_derivative_equal_to_zero test/polynomial_test.rb /^ def test_extrema_with_slope_of_derivative_equal_to_zero$/;" f class:TestPolynomial
|
70
|
+
test_initializer test/term_test.rb /^ def test_initializer$/;" f class:TestTerm
|
71
|
+
test_no_curve_and_no_inflection_points test/polynomial_test.rb /^ def test_no_curve_and_no_inflection_points$/;" f class:TestPolynomial
|
72
|
+
test_no_inflection_points test/polynomial_test.rb /^ def test_no_inflection_points$/;" f class:TestPolynomial
|
73
|
+
test_no_local_extrema test/polynomial_test.rb /^ def test_no_local_extrema$/;" f class:TestPolynomial
|
74
|
+
test_parsing test/polynomial_test.rb /^ def test_parsing$/;" f class:TestPolynomial
|
75
|
+
test_parsing_variable_omitted test/term_test.rb /^ def test_parsing_variable_omitted$/;" f class:TestTerm
|
76
|
+
test_parsing_with_default_coefficient_equal_to_one test/term_test.rb /^ def test_parsing_with_default_coefficient_equal_to_one$/;" f class:TestTerm
|
77
|
+
test_parsing_with_default_exponent_equal_to_one test/term_test.rb /^ def test_parsing_with_default_exponent_equal_to_one$/;" f class:TestTerm
|
78
|
+
test_parsing_with_standard_format test/term_test.rb /^ def test_parsing_with_standard_format$/;" f class:TestTerm
|
79
|
+
test_roots_biquadratic_equation test/math_test.rb /^ def test_roots_biquadratic_equation$/;" f class:TestMath
|
80
|
+
test_roots_for_constant_functions test/math_test.rb /^ def test_roots_for_constant_functions$/;" f class:TestMath
|
81
|
+
test_roots_for_cubic_functions test/math_test.rb /^ def test_roots_for_cubic_functions$/;" f class:TestMath
|
82
|
+
test_roots_for_cubic_functions_one_real_all_equal test/math_test.rb /^ def test_roots_for_cubic_functions_one_real_all_equal$/;" f class:TestMath
|
83
|
+
test_roots_for_cubic_functions_with_complex test/math_test.rb /^ def test_roots_for_cubic_functions_with_complex$/;" f class:TestMath
|
84
|
+
test_roots_for_linear_functions test/math_test.rb /^ def test_roots_for_linear_functions$/;" f class:TestMath
|
85
|
+
test_roots_for_quartic_functions test/math_test.rb /^ def test_roots_for_quartic_functions$/;" f class:TestMath
|
86
|
+
test_roots_quadratic_functions test/math_test.rb /^ def test_roots_quadratic_functions$/;" f class:TestMath
|
87
|
+
test_roots_without_term_with_exponent_of_zero test/math_test.rb /^ def test_roots_without_term_with_exponent_of_zero$/;" f class:TestMath
|
88
|
+
test_to_s test/polynomial_test.rb /^ def test_to_s$/;" f class:TestPolynomial
|
89
|
+
test_two_inflection_points test/polynomial_test.rb /^ def test_two_inflection_points$/;" f class:TestPolynomial
|
90
|
+
to_s lib/polynomials.rb /^ def to_s$/;" f class:Polynomial
|
91
|
+
to_s lib/term.rb /^ def to_s$/;" f class:Term
|
data/test/polynomial_test.rb
CHANGED
@@ -67,6 +67,11 @@ class TestPolynomial < MiniTest::Unit::TestCase
|
|
67
67
|
assert_equal({ left: Set[-1.0/0..+1.0/0] }, polynomial.curvature_behaviour)
|
68
68
|
end
|
69
69
|
|
70
|
+
def test_no_extremums
|
71
|
+
polynomial = Polynomial.parse('5')
|
72
|
+
assert_equal({}, polynomial.extrema)
|
73
|
+
end
|
74
|
+
|
70
75
|
def test_no_curve_and_no_inflection_points
|
71
76
|
polynomials = []
|
72
77
|
polynomials << Polynomial.parse('5 x + 4')
|
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
|
+
- 7
|
9
|
+
version: 0.1.7
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Manuel Korfmann
|
@@ -17,22 +17,9 @@ cert_chain: []
|
|
17
17
|
date: 2011-05-13 00:00:00 +02:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
|
-
- !ruby/object:Gem::Dependency
|
21
|
-
name: shoulda
|
22
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
23
|
-
none: false
|
24
|
-
requirements:
|
25
|
-
- - ">="
|
26
|
-
- !ruby/object:Gem::Version
|
27
|
-
segments:
|
28
|
-
- 0
|
29
|
-
version: "0"
|
30
|
-
type: :development
|
31
|
-
prerelease: false
|
32
|
-
version_requirements: *id001
|
33
20
|
- !ruby/object:Gem::Dependency
|
34
21
|
name: bundler
|
35
|
-
requirement: &
|
22
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
36
23
|
none: false
|
37
24
|
requirements:
|
38
25
|
- - ~>
|
@@ -44,10 +31,10 @@ dependencies:
|
|
44
31
|
version: 1.0.0
|
45
32
|
type: :development
|
46
33
|
prerelease: false
|
47
|
-
version_requirements: *
|
34
|
+
version_requirements: *id001
|
48
35
|
- !ruby/object:Gem::Dependency
|
49
36
|
name: jeweler
|
50
|
-
requirement: &
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
51
38
|
none: false
|
52
39
|
requirements:
|
53
40
|
- - ~>
|
@@ -59,10 +46,10 @@ dependencies:
|
|
59
46
|
version: 1.6.0
|
60
47
|
type: :development
|
61
48
|
prerelease: false
|
62
|
-
version_requirements: *
|
49
|
+
version_requirements: *id002
|
63
50
|
- !ruby/object:Gem::Dependency
|
64
51
|
name: rcov
|
65
|
-
requirement: &
|
52
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
66
53
|
none: false
|
67
54
|
requirements:
|
68
55
|
- - ">="
|
@@ -72,10 +59,10 @@ dependencies:
|
|
72
59
|
version: "0"
|
73
60
|
type: :development
|
74
61
|
prerelease: false
|
75
|
-
version_requirements: *
|
62
|
+
version_requirements: *id003
|
76
63
|
- !ruby/object:Gem::Dependency
|
77
64
|
name: awesome_print
|
78
|
-
requirement: &
|
65
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
79
66
|
none: false
|
80
67
|
requirements:
|
81
68
|
- - ">="
|
@@ -85,7 +72,7 @@ dependencies:
|
|
85
72
|
version: "0"
|
86
73
|
type: :development
|
87
74
|
prerelease: false
|
88
|
-
version_requirements: *
|
75
|
+
version_requirements: *id004
|
89
76
|
description: "\n\
|
90
77
|
This is a gem dedicated to parsing, manipulating and finding roots,extrema and inflection points of polynomials.\n\
|
91
78
|
It can solve polynomial equations with a degree up to 4 by implementing formulas for solving quadratic, cubic and quartic functions.\n "
|
@@ -109,6 +96,7 @@ files:
|
|
109
96
|
- lib/polynomials.rb
|
110
97
|
- lib/term.rb
|
111
98
|
- polynomials.gemspec
|
99
|
+
- tags
|
112
100
|
- test/helper.rb
|
113
101
|
- test/math_test.rb
|
114
102
|
- test/polynomial_test.rb
|
@@ -128,7 +116,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
128
116
|
requirements:
|
129
117
|
- - ">="
|
130
118
|
- !ruby/object:Gem::Version
|
131
|
-
hash:
|
119
|
+
hash: -2463697157255217048
|
132
120
|
segments:
|
133
121
|
- 0
|
134
122
|
version: "0"
|