polynomials 0.1.0
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/.document +5 -0
- data/Gemfile +16 -0
- data/Gemfile.lock +22 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +19 -0
- data/Rakefile +60 -0
- data/VERSION +1 -0
- data/lib/core_ext/math.rb +60 -0
- data/lib/polynomial.rb +127 -0
- data/lib/term.rb +32 -0
- data/polynomials.gemspec +75 -0
- data/test/helper.rb +18 -0
- data/test/math_test.rb +65 -0
- data/test/polynomial_test.rb +74 -0
- data/test/term_test.rb +12 -0
- data/test/test_helper.rb +3 -0
- metadata +152 -0
data/.document
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
gem "shoulda", ">= 0"
|
10
|
+
gem "bundler", "~> 1.0.0"
|
11
|
+
gem "jeweler", "~> 1.6.0"
|
12
|
+
gem "rcov", ">= 0"
|
13
|
+
end
|
14
|
+
group :development, :tests do
|
15
|
+
gem 'awesome_print'
|
16
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
awesome_print (0.3.2)
|
5
|
+
git (1.2.5)
|
6
|
+
jeweler (1.6.0)
|
7
|
+
bundler (~> 1.0.0)
|
8
|
+
git (>= 1.2.5)
|
9
|
+
rake
|
10
|
+
rake (0.8.7)
|
11
|
+
rcov (0.9.9)
|
12
|
+
shoulda (2.11.3)
|
13
|
+
|
14
|
+
PLATFORMS
|
15
|
+
ruby
|
16
|
+
|
17
|
+
DEPENDENCIES
|
18
|
+
awesome_print
|
19
|
+
bundler (~> 1.0.0)
|
20
|
+
jeweler (~> 1.6.0)
|
21
|
+
rcov
|
22
|
+
shoulda
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Manuel Korfmann
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
= polynomials
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Contributing to polynomials
|
6
|
+
|
7
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
8
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
9
|
+
* Fork the project
|
10
|
+
* Start a feature/bugfix branch
|
11
|
+
* Commit and push until you are happy with your contribution
|
12
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
13
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2011 Manuel Korfmann. See LICENSE.txt for
|
18
|
+
further details.
|
19
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "polynomials"
|
18
|
+
gem.homepage = "http://github.com/mkorfmann/polynomials"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{Polyomial calculations library}
|
21
|
+
gem.description = %Q{
|
22
|
+
* Parses polyomials
|
23
|
+
* Provides useful methods on top of the Polyonomial class, like:
|
24
|
+
** Polyonomial#local_extremums
|
25
|
+
** Polyonomial#nulls
|
26
|
+
** Polyonomial#curvature_behaviour
|
27
|
+
* Implements root's finding formulas for quadratic, cubic and quartic functions
|
28
|
+
}
|
29
|
+
gem.email = "manu@korfmann.info"
|
30
|
+
gem.authors = ["Manuel Korfmann"]
|
31
|
+
# dependencies defined in Gemfile
|
32
|
+
end
|
33
|
+
Jeweler::RubygemsDotOrgTasks.new
|
34
|
+
|
35
|
+
require 'rake/testtask'
|
36
|
+
Rake::TestTask.new(:test) do |test|
|
37
|
+
test.libs << 'lib' << 'test'
|
38
|
+
test.pattern = 'test/**/*_test.rb'
|
39
|
+
test.verbose = true
|
40
|
+
end
|
41
|
+
|
42
|
+
require 'rcov/rcovtask'
|
43
|
+
Rcov::RcovTask.new do |test|
|
44
|
+
test.libs << 'test'
|
45
|
+
test.pattern = 'test/**/*_test.rb'
|
46
|
+
test.verbose = true
|
47
|
+
test.rcov_opts << '--exclude "gems/*"'
|
48
|
+
end
|
49
|
+
|
50
|
+
task :default => :test
|
51
|
+
|
52
|
+
require 'rake/rdoctask'
|
53
|
+
Rake::RDocTask.new do |rdoc|
|
54
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
55
|
+
|
56
|
+
rdoc.rdoc_dir = 'rdoc'
|
57
|
+
rdoc.title = "polynomials #{version}"
|
58
|
+
rdoc.rdoc_files.include('README*')
|
59
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
60
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'mathn'
|
2
|
+
module Math
|
3
|
+
def self.roots_of_cubic_function(a,b,c,d,with_complex = false)
|
4
|
+
f = ((3*c/a) - (b**2/a**2))/3
|
5
|
+
g = (((2*b**3)/a**3) - (9*b*c/a**2) + (27*d/a))/27
|
6
|
+
h = (g**2/4) + (f**3/27)
|
7
|
+
if f == 0 && g == 0 && h == 0
|
8
|
+
f = (3*c/a) - (b**2/a**2)
|
9
|
+
g = (2*b**3/a**3) - (9*b*c/a**2) + (27*d/a)
|
10
|
+
h = (g**2/4) + (f**3/27)
|
11
|
+
[-Math.cbrt(d/a)]
|
12
|
+
elsif h <= 0
|
13
|
+
i = Math.sqrt((g**2/4) - h)
|
14
|
+
j = Math.cbrt(i)
|
15
|
+
k = Math.acos(-(g / (2*i)))
|
16
|
+
l = j * -1
|
17
|
+
m = Math.cos(k/3)
|
18
|
+
n = Math.sqrt(3) * Math.sin(k/3)
|
19
|
+
p = (b/(3*a))*-1
|
20
|
+
[2*j*Math.cos(k/3) - (b/(3*a)), l*(m+n)+p, l*(m-n)+p]
|
21
|
+
else
|
22
|
+
f = (((3*c)/a) - (b**2/a**2))/3
|
23
|
+
g = ((2*b**3/a**3) - (9*b*c/a**2) + ((27*d)/a))/27
|
24
|
+
h = (g**2/4) + (f**3/27)
|
25
|
+
r = -(g/2) + Math.sqrt(h)
|
26
|
+
s = Math.cbrt(r)
|
27
|
+
t = -(g/2) - Math.sqrt(h)
|
28
|
+
u = Math.cbrt(t)
|
29
|
+
roots = [(s + u) - (b/(3*a))]
|
30
|
+
roots |= [1,-1].map do |algebraic_sign|
|
31
|
+
Complex((-(s + u)/2) - (b/(3*a)),algebraic_sign * (s-u)*Math.sqrt(3)/2)
|
32
|
+
end if with_complex
|
33
|
+
roots
|
34
|
+
end.map { |n| n.respond_to?(:round) ? n.round(10) : n }.to_set
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.roots_of_quadratic_function(a,b,c)
|
38
|
+
p = b/a
|
39
|
+
q = c/a
|
40
|
+
denom = (p/2.0)**2 - q
|
41
|
+
return Set[] if denom < 0
|
42
|
+
root = Math.sqrt(denom)
|
43
|
+
Set.new([:+,:-].map{ |operator| (-(p/2.0)).send(operator, root)})
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.roots_of_quartic_function(*args)
|
47
|
+
a,b,c,d,e = args.map { |n| n/args.first }
|
48
|
+
f = c - (3*b**2/8)
|
49
|
+
g = d + (b**3/8) - (b*c/2)
|
50
|
+
h = e - (3*b**4/256) + (b**2 * c/16) - ( b*d/4)
|
51
|
+
roots = self.roots_of_cubic_function(1,(f/2),(((f**2)-(4*h))/16),-(g**2)/64,true)
|
52
|
+
combs = roots.reject{ |n| n == 0}.combination(2)
|
53
|
+
only_complex = combs.select { |comb| comb.all? { |n| n.is_a? Complex }}
|
54
|
+
p,q = (only_complex.empty? ? combs.first : only_complex.first).map { |n| Math.sqrt(n) }
|
55
|
+
r = (-g/(8*p*q))
|
56
|
+
s = b/(4*a)
|
57
|
+
roots = Set.new([p + q + r -s, p - q - r -s, -p + q - r -s, -p - q + r -s].map { |n| n.is_a?(Complex) ? (n.real if n.imaginary == 0) : n }.compact )
|
58
|
+
roots.map { |n| n.respond_to?(:round) ? n.round(10) : n }.to_set
|
59
|
+
end
|
60
|
+
end
|
data/lib/polynomial.rb
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
require 'set'
|
2
|
+
require_relative 'term'
|
3
|
+
require_relative 'core_ext/math'
|
4
|
+
class Polynomial
|
5
|
+
MinMaxMapping = { 1.0 => :max, -1.0 => :min }
|
6
|
+
AfterExtremumsCurvatureMapping = { max: :right, min: :left }
|
7
|
+
NegPosMinMaxExtremumMapping = {[1.0,-1.0] => :max,[-1.0,1.0] => :min}
|
8
|
+
attr_accessor :terms
|
9
|
+
|
10
|
+
def self.parse(string)
|
11
|
+
polynomial = self.new
|
12
|
+
string.split(/(?=[-+])/).each do |term|
|
13
|
+
parsed = Term.parse(term)
|
14
|
+
polynomial.terms[parsed.exponent].coefficient += parsed.coefficient
|
15
|
+
end
|
16
|
+
return polynomial
|
17
|
+
end
|
18
|
+
|
19
|
+
def initialize
|
20
|
+
self.terms = Hash.new { |hash, key| hash[key] = Term.new(key) }
|
21
|
+
end
|
22
|
+
|
23
|
+
def calculate(x)
|
24
|
+
self.terms.values.inject(0.0) do |acc,t|
|
25
|
+
acc + (t.coefficient.to_f * (x**t.exponent))
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def derivative
|
30
|
+
new_function = self.alter do |nf, term|
|
31
|
+
nf.terms[term.exponent - 1].coefficient += term.exponent * term.coefficient
|
32
|
+
end
|
33
|
+
new_function.terms.reject! { |_,t| t.coefficient == 0 }
|
34
|
+
return new_function
|
35
|
+
end
|
36
|
+
|
37
|
+
def alter
|
38
|
+
new_function = self.class.new
|
39
|
+
self.terms.values.each do |term|
|
40
|
+
yield new_function, term
|
41
|
+
end
|
42
|
+
return new_function
|
43
|
+
end
|
44
|
+
|
45
|
+
def nulls
|
46
|
+
if terms.keys.none?(&:zero?)
|
47
|
+
self.alter { |nf, term| nf.terms[term.exponent-1].coefficient = term.coefficient }.nulls << 0.0
|
48
|
+
else
|
49
|
+
case self.degree
|
50
|
+
when 1
|
51
|
+
Set[-self.terms[0].coefficient / self.terms[1].coefficient]
|
52
|
+
when 2
|
53
|
+
Math.roots_of_quadratic_function(*coefficients_till(2))
|
54
|
+
when 3
|
55
|
+
Math.roots_of_cubic_function(*coefficients_till(3))
|
56
|
+
when 4
|
57
|
+
Math.roots_of_quartic_function(*coefficients_till(4))
|
58
|
+
else
|
59
|
+
Set[]
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def local_extremums
|
65
|
+
derivative = self.derivative
|
66
|
+
possible_extremums = derivative.nulls.sort
|
67
|
+
max_min_extremum = Hash.new { |hash,key| hash[key] = Set.new }
|
68
|
+
|
69
|
+
samples = ([possible_extremums.first - 1] + possible_extremums.sort + [possible_extremums.last + 1]).each_cons(2).map do |before,after|
|
70
|
+
(before + after)/2
|
71
|
+
end
|
72
|
+
|
73
|
+
possible_extremums.zip(samples.each_cons(2)).each do |pe,(after,before)|
|
74
|
+
yafter = derivative.calculate(after)
|
75
|
+
ybefore = derivative.calculate(before)
|
76
|
+
kind_of_extremum = NegPosMinMaxExtremumMapping[[yafter/yafter.abs,ybefore/ybefore.abs]]
|
77
|
+
max_min_extremum[kind_of_extremum] << pe if kind_of_extremum
|
78
|
+
end
|
79
|
+
return max_min_extremum
|
80
|
+
end
|
81
|
+
|
82
|
+
def curvature_behaviour
|
83
|
+
if degree < 2
|
84
|
+
return {}
|
85
|
+
else
|
86
|
+
extremums = self.derivative.extremums
|
87
|
+
curvature_behaviour = Hash.new {|h,k|h[k]=Set.new}
|
88
|
+
extremums.values.inject(&:|).sort.each_cons(2) do |start_point,end_point|
|
89
|
+
curvature_behaviour[AfterExtremumsCurvatureMapping[extremums.find { |k,v| v.include?(start_point) }.first]] << Range.new(start_point,end_point)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
return curvature_behaviour
|
93
|
+
end
|
94
|
+
|
95
|
+
def degree
|
96
|
+
self.terms.keys.max
|
97
|
+
end
|
98
|
+
|
99
|
+
def to_s
|
100
|
+
terms.delete_if { |_,t| t.coefficient.zero? }.sort_by { |_,t| -t.exponent }.inject("") do |string,(_,term)|
|
101
|
+
string << term.to_s
|
102
|
+
end.strip
|
103
|
+
end
|
104
|
+
|
105
|
+
def ==(other)
|
106
|
+
delete_zeros = proc{ |_,t| t.coefficient.zero? }
|
107
|
+
self.terms.delete_if(&delete_zeros) == other.terms.delete_if(&delete_zeros)
|
108
|
+
end
|
109
|
+
|
110
|
+
def extremums
|
111
|
+
extremums = local_extremums
|
112
|
+
a = self.terms[self.degree].coefficient
|
113
|
+
max_or_min = (self.degree.even? ? [1,1] : [-1,1]).map { |n| (n * a)/a.abs }
|
114
|
+
extremums[MinMaxMapping[max_or_min.first]] << -1.0/0
|
115
|
+
extremums[MinMaxMapping[max_or_min.last]] << 1.0/0
|
116
|
+
return extremums
|
117
|
+
end
|
118
|
+
|
119
|
+
private
|
120
|
+
def coefficients_till(n)
|
121
|
+
coefficients = []
|
122
|
+
(0..n).each do |e|
|
123
|
+
coefficients << self.terms[e].coefficient
|
124
|
+
end
|
125
|
+
return coefficients.reverse
|
126
|
+
end
|
127
|
+
end
|
data/lib/term.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
class Term
|
2
|
+
attr_accessor :coefficient, :exponent
|
3
|
+
|
4
|
+
def initialize(exponent = 0, coefficient = 0)
|
5
|
+
@coefficient, @exponent = coefficient, exponent
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.parse(string)
|
9
|
+
term = self.new
|
10
|
+
float = /(\d*(?:\.\d*)?)/
|
11
|
+
integer = /(\d*)/
|
12
|
+
raw_data = string.match(/([-+]?)\s*#{float}\s*(x.*)?\s*\Z/)
|
13
|
+
term.coefficient = (raw_data[1] + raw_data[2]).to_f
|
14
|
+
term.exponent = (raw_data[3] ? (raw_data[3] =~ /x\^#{integer}/ ? $1.to_i : 1) : 0)
|
15
|
+
term
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_s
|
19
|
+
"#{ (coefficient < 0) ? '-' : '+' } #{coefficient.abs} #{ "x#{"^#{exponent}" unless exponent == 1}" unless exponent.zero?} "
|
20
|
+
end
|
21
|
+
|
22
|
+
def dup
|
23
|
+
duplicate = self.class.new
|
24
|
+
duplicate.coefficient = self.coefficient
|
25
|
+
duplicate.exponent = self.exponent
|
26
|
+
duplicate
|
27
|
+
end
|
28
|
+
|
29
|
+
def ==(other)
|
30
|
+
self.coefficient == other.coefficient && self.exponent == other.exponent
|
31
|
+
end
|
32
|
+
end
|
data/polynomials.gemspec
ADDED
@@ -0,0 +1,75 @@
|
|
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{polynomials}
|
8
|
+
s.version = "0.1.0"
|
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 polyomials
|
15
|
+
* Provides useful methods on top of the Polyonomial class, like:
|
16
|
+
** Polyonomial#local_extremums
|
17
|
+
** Polyonomial#nulls
|
18
|
+
** Polyonomial#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/polynomial.rb",
|
36
|
+
"lib/term.rb",
|
37
|
+
"polynomials.gemspec",
|
38
|
+
"test/helper.rb",
|
39
|
+
"test/math_test.rb",
|
40
|
+
"test/polynomial_test.rb",
|
41
|
+
"test/term_test.rb",
|
42
|
+
"test/test_helper.rb"
|
43
|
+
]
|
44
|
+
s.homepage = %q{http://github.com/mkorfmann/polynomials}
|
45
|
+
s.licenses = ["MIT"]
|
46
|
+
s.require_paths = ["lib"]
|
47
|
+
s.rubygems_version = %q{1.3.7}
|
48
|
+
s.summary = %q{Polyomial calculations library}
|
49
|
+
|
50
|
+
if s.respond_to? :specification_version then
|
51
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
52
|
+
s.specification_version = 3
|
53
|
+
|
54
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
55
|
+
s.add_development_dependency(%q<shoulda>, [">= 0"])
|
56
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
57
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.6.0"])
|
58
|
+
s.add_development_dependency(%q<rcov>, [">= 0"])
|
59
|
+
s.add_development_dependency(%q<awesome_print>, [">= 0"])
|
60
|
+
else
|
61
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
62
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
63
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.0"])
|
64
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
65
|
+
s.add_dependency(%q<awesome_print>, [">= 0"])
|
66
|
+
end
|
67
|
+
else
|
68
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
69
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
70
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.0"])
|
71
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
72
|
+
s.add_dependency(%q<awesome_print>, [">= 0"])
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
data/test/helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'test/unit'
|
11
|
+
require 'shoulda'
|
12
|
+
|
13
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
14
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
15
|
+
require 'polynomials'
|
16
|
+
|
17
|
+
class Test::Unit::TestCase
|
18
|
+
end
|
data/test/math_test.rb
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
class TestMath < MiniTest::Unit::TestCase
|
3
|
+
def test_nulls_for_constant_functions
|
4
|
+
polynomial = Polynomial.parse('5')
|
5
|
+
assert_equal Set[],polynomial.nulls
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_nulls_for_linear_functions
|
9
|
+
polynomial = Polynomial.parse('5x + 2')
|
10
|
+
assert_equal Set[-2.0/5.0],polynomial.nulls
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_nulls_quadratic_functions
|
14
|
+
polynomial = Polynomial.parse('3x^2 + 2x - 40')
|
15
|
+
p = 2.0/3.0
|
16
|
+
q = -40.0/3.0
|
17
|
+
root = Math.sqrt((p/2)**2 - q)
|
18
|
+
fraction = -(p/2)
|
19
|
+
assert_equal Set[fraction - root, fraction + root],polynomial.nulls
|
20
|
+
|
21
|
+
polynomial = Polynomial.parse('3x^2 - 40')
|
22
|
+
assert_equal Set[Math.sqrt(40.0/3), -Math.sqrt(40.0/3)], polynomial.nulls
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_nulls_for_cubic_functions
|
26
|
+
polynomial = Polynomial.parse('1x^3 + 1x^2 + 2x - 1')
|
27
|
+
assert_equal(1, polynomial.nulls.size)
|
28
|
+
assert_equal Set[0.3926467817], polynomial.nulls
|
29
|
+
|
30
|
+
polynomial = Polynomial.parse('2x^3 - 4x^2 - 22x + 24')
|
31
|
+
assert_equal Set[4.0,-3.0,1.0], polynomial.nulls
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_nulls_for_cubic_functions_with_complex
|
35
|
+
coefficients = Polynomial.parse('3x^3 - 10x^2 + 14x + 27').terms.first(4).map{ |t| t.last.coefficient }
|
36
|
+
assert_equal Set[-1.0, Complex(2.1666666666666634,2.0749832663314605), Complex(2.1666666666666634,-2.0749832663314605)],Math.roots_of_cubic_function(*coefficients, true)
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_nulls_for_cubic_functions_one_real_all_equal
|
40
|
+
polynomial = Polynomial.parse('1x^3 + 6x^2 + 12x + 8')
|
41
|
+
assert_equal(1, polynomial.nulls.size)
|
42
|
+
assert_equal(-2, polynomial.nulls.first)
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_nulls_for_quartic_functions
|
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.nulls
|
48
|
+
|
49
|
+
polynomial = Polynomial.parse('-20x^4 + 5x^3 + 17x^2 - 29x + 87')
|
50
|
+
assert_equal Set[-1.6820039266,1.4875831103], polynomial.nulls
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_nulls_without_term_with_exponent_of_zero
|
54
|
+
polynomial = Polynomial.parse('3x^3 + 3x^2')
|
55
|
+
assert_equal Set[0.0,-1.0], polynomial.nulls
|
56
|
+
|
57
|
+
polynomial = Polynomial.parse('1 x^4')
|
58
|
+
assert_equal Set[0.0], polynomial.nulls
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_nulls_biquadratic_equation
|
62
|
+
polynomial = Polynomial.parse('4x^4 + 2x^2 - 1')
|
63
|
+
assert_equal Set[0.5558929703, -0.5558929703], polynomial.nulls
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require_relative 'test_helper'
|
2
|
+
|
3
|
+
class TestPolynomial < MiniTest::Unit::TestCase
|
4
|
+
def test_to_s
|
5
|
+
polynomial = Polynomial.parse('5x + 2x^2 + 20')
|
6
|
+
assert_equal '+ 2.0 x^2 + 5.0 x + 20.0', polynomial.to_s
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_derivative_of_linear_functions
|
10
|
+
polynomial = Polynomial.parse('5x + 2')
|
11
|
+
assert_equal Polynomial.parse('5') ,polynomial.derivative
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_derivative_functions_with_higher_degree
|
15
|
+
polynomial = Polynomial.parse('5.5x^4 + 3.5x^3 + 2x^2 + 2x + 50')
|
16
|
+
assert_equal Polynomial.parse('22x^3 + 10.5x^2 + 4x + 2'), polynomial.derivative
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_calculation
|
20
|
+
polynomial = Polynomial.parse('2x^4 - 1 x')
|
21
|
+
assert_equal 1245, polynomial.calculate(5)
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_parsing
|
25
|
+
assert_equal 0, Polynomial.parse('2').terms.values.first.exponent
|
26
|
+
assert_equal 2, Polynomial.parse('2').terms.values.first.coefficient
|
27
|
+
|
28
|
+
terms = Polynomial.parse('2x^2 + 5.5x^1 - 20').terms.values.sort_by(&:exponent).reverse
|
29
|
+
assert_equal [2,1,0], terms.map(&:exponent)
|
30
|
+
assert_equal [2,5.5,-20], terms.map(&:coefficient)
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_equality
|
34
|
+
assert_equal Polynomial.parse('3x^1 + 5'), Polynomial.parse('3x + 5 x^0')
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_degree
|
38
|
+
assert_equal 5,Polynomial.parse('3x^5 - 5 x + 3').degree
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_extremums
|
42
|
+
polynomial = Polynomial.parse('3x^2 + 2x + 1')
|
43
|
+
assert_equal({ min: Set[-2.0/6.0] }, polynomial.local_extremums)
|
44
|
+
polynomial = Polynomial.parse('5x^3 - 5x^2 + 2x - 2')
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_extremums_with_slope_of_derivative_equal_to_zero
|
48
|
+
polynomial = Polynomial.parse('1x^4')
|
49
|
+
assert_equal({ min: Set[0.0] }, polynomial.local_extremums)
|
50
|
+
assert_equal({ max: Set[1.0/0, -1.0/0], min: Set[0.0]}, polynomial.extremums)
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_no_inflection_points
|
54
|
+
polynomial = Polynomial.parse('1 x^4')
|
55
|
+
assert_equal({ left: Set[-1.0/0..+1.0/0] }, polynomial.curvature_behaviour)
|
56
|
+
|
57
|
+
polynomial = Polynomial.parse('-1 x^4')
|
58
|
+
assert_equal({ right: Set[-1.0/0..+1.0/0] }, polynomial.curvature_behaviour)
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_no_curve_and_no_inflection_points
|
62
|
+
polynomials = []
|
63
|
+
polynomials << Polynomial.parse('5 x + 4')
|
64
|
+
polynomials << Polynomial.parse('4')
|
65
|
+
polynomials.each do |polynomial|
|
66
|
+
assert_equal({}, polynomial.curvature_behaviour)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_two_inflection_points
|
71
|
+
polynomial = Polynomial.parse('+ 1.0 x^4 + 5.0 x^3 - 1.0 x^2 + 3.0 x + 5.0')
|
72
|
+
assert_equal({left:Set[-1.0/0..-2.5649778198382918, 0.06497781983829176..1.0/0], right: Set[-2.5649778198382918..0.06497781983829176] } , polynomial.curvature_behaviour)
|
73
|
+
end
|
74
|
+
end
|
data/test/term_test.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require_relative 'test_helper'
|
2
|
+
class TestTerm < MiniTest::Unit::TestCase
|
3
|
+
def test_initializer
|
4
|
+
assert_equal 0, Term.new.coefficient
|
5
|
+
assert_equal 0, Term.new.exponent
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_parsing
|
9
|
+
assert_equal 4, Term.parse('4x^2').coefficient
|
10
|
+
assert_equal 2, Term.parse('4x^2').exponent
|
11
|
+
end
|
12
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,152 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: polynomials
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Manuel Korfmann
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-05-12 00:00:00 +02:00
|
18
|
+
default_executable:
|
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
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: bundler
|
35
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
36
|
+
none: false
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
segments:
|
41
|
+
- 1
|
42
|
+
- 0
|
43
|
+
- 0
|
44
|
+
version: 1.0.0
|
45
|
+
type: :development
|
46
|
+
prerelease: false
|
47
|
+
version_requirements: *id002
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: jeweler
|
50
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ~>
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
segments:
|
56
|
+
- 1
|
57
|
+
- 6
|
58
|
+
- 0
|
59
|
+
version: 1.6.0
|
60
|
+
type: :development
|
61
|
+
prerelease: false
|
62
|
+
version_requirements: *id003
|
63
|
+
- !ruby/object:Gem::Dependency
|
64
|
+
name: rcov
|
65
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
segments:
|
71
|
+
- 0
|
72
|
+
version: "0"
|
73
|
+
type: :development
|
74
|
+
prerelease: false
|
75
|
+
version_requirements: *id004
|
76
|
+
- !ruby/object:Gem::Dependency
|
77
|
+
name: awesome_print
|
78
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
79
|
+
none: false
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
segments:
|
84
|
+
- 0
|
85
|
+
version: "0"
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: *id005
|
89
|
+
description: "\n\
|
90
|
+
* Parses polyomials\n\
|
91
|
+
* Provides useful methods on top of the Polyonomial class, like:\n ** Polyonomial#local_extremums\n ** Polyonomial#nulls\n ** Polyonomial#curvature_behaviour\n\
|
92
|
+
* Implements root's finding formulas for quadratic, cubic and quartic functions\n "
|
93
|
+
email: manu@korfmann.info
|
94
|
+
executables: []
|
95
|
+
|
96
|
+
extensions: []
|
97
|
+
|
98
|
+
extra_rdoc_files:
|
99
|
+
- LICENSE.txt
|
100
|
+
- README.rdoc
|
101
|
+
files:
|
102
|
+
- .document
|
103
|
+
- Gemfile
|
104
|
+
- Gemfile.lock
|
105
|
+
- LICENSE.txt
|
106
|
+
- README.rdoc
|
107
|
+
- Rakefile
|
108
|
+
- VERSION
|
109
|
+
- lib/core_ext/math.rb
|
110
|
+
- lib/polynomial.rb
|
111
|
+
- lib/term.rb
|
112
|
+
- polynomials.gemspec
|
113
|
+
- test/helper.rb
|
114
|
+
- test/math_test.rb
|
115
|
+
- test/polynomial_test.rb
|
116
|
+
- test/term_test.rb
|
117
|
+
- test/test_helper.rb
|
118
|
+
has_rdoc: true
|
119
|
+
homepage: http://github.com/mkorfmann/polynomials
|
120
|
+
licenses:
|
121
|
+
- MIT
|
122
|
+
post_install_message:
|
123
|
+
rdoc_options: []
|
124
|
+
|
125
|
+
require_paths:
|
126
|
+
- lib
|
127
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
128
|
+
none: false
|
129
|
+
requirements:
|
130
|
+
- - ">="
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
hash: -515319334687155051
|
133
|
+
segments:
|
134
|
+
- 0
|
135
|
+
version: "0"
|
136
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
137
|
+
none: false
|
138
|
+
requirements:
|
139
|
+
- - ">="
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
segments:
|
142
|
+
- 0
|
143
|
+
version: "0"
|
144
|
+
requirements: []
|
145
|
+
|
146
|
+
rubyforge_project:
|
147
|
+
rubygems_version: 1.3.7
|
148
|
+
signing_key:
|
149
|
+
specification_version: 3
|
150
|
+
summary: Polyomial calculations library
|
151
|
+
test_files: []
|
152
|
+
|