polynomials 0.4.2 → 0.4.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +22 -11
- data/VERSION +1 -1
- data/lib/polynomials/formulas.rb +3 -3
- data/lib/polynomials/polynomial.rb +5 -5
- data/polynomials.gemspec +2 -2
- data/test/polynomial_test.rb +10 -0
- metadata +4 -4
data/README.markdown
CHANGED
@@ -2,6 +2,11 @@
|
|
2
2
|
|
3
3
|
[![Build Status](http://travis-ci.org/mkorfmann/polynomials.png)](http://travis-ci.org/mkorfmann/polynomials)
|
4
4
|
|
5
|
+
## Example App
|
6
|
+
|
7
|
+
Checkout my [Example App](http://polynomials-app.heroku.com/graph) to
|
8
|
+
see the gem in action!
|
9
|
+
|
5
10
|
## Usage
|
6
11
|
|
7
12
|
<pre><code>
|
@@ -12,22 +17,23 @@ include Polynomials
|
|
12
17
|
|
13
18
|
polynomial = Polynomial.parse(ARGV[0])
|
14
19
|
|
15
|
-
points = polynomial.roots | polynomial.local_extrema |
|
20
|
+
points = polynomial.roots | polynomial.local_extrema |
|
21
|
+
polynomial.inflection_points
|
16
22
|
|
17
|
-
max_x = points.max_by(&:x)
|
18
|
-
min_x = points.min_by(&:x)
|
19
|
-
max_x = max_x ? max_x.x : 1
|
20
|
-
min_x = min_x ? min_x.x : -1
|
23
|
+
max_x = points.max_by(&:x)
|
24
|
+
min_x = points.min_by(&:x)
|
25
|
+
max_x = max_x == 0 ? max_x.x : 1
|
26
|
+
min_x = min_x == 0 ? min_x.x : -1
|
21
27
|
|
22
28
|
difference = (max_x-min_x).abs
|
23
29
|
|
24
30
|
start = min_x - difference/4.0
|
25
31
|
stop = max_x + difference/4.0
|
26
|
-
step =
|
32
|
+
step = (start-stop).abs/1000.0
|
27
33
|
|
28
34
|
pointset = polynomial.pointset(start,stop,step)
|
29
|
-
data_x
|
30
|
-
data_y = pointset.map(
|
35
|
+
data_x= pointset.map(&:first)
|
36
|
+
data_y = pointset.map(&:last)
|
31
37
|
|
32
38
|
Gnuplot.open do |gp|
|
33
39
|
Gnuplot::Plot.new(gp) do |plot|
|
@@ -40,11 +46,16 @@ Gnuplot.open do |gp|
|
|
40
46
|
ds.with = "lines"
|
41
47
|
ds.linewidth = 0.2
|
42
48
|
ds.title = "f(x) = #{polynomial}"
|
49
|
+
ds.smooth
|
43
50
|
end
|
44
51
|
|
45
|
-
[:inflection_point,:root, :maximum, :minimum].each do
|
46
|
-
|
47
|
-
|
52
|
+
[:inflection_point,:root, :maximum, :minimum].each do
|
53
|
+
|kind_of_point|
|
54
|
+
selected_points =
|
55
|
+
points.select(&:"#{kind_of_point}?")
|
56
|
+
plot.data <<
|
57
|
+
Gnuplot::DataSet.new([selected_points.map(&:x),
|
58
|
+
selected_points.map(&:y)]) do |ds|
|
48
59
|
ds.with = "points"
|
49
60
|
ds.linewidth = 2
|
50
61
|
ds.title = kind_of_point.to_s.pluralize.titleize
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.4.
|
1
|
+
0.4.3
|
data/lib/polynomials/formulas.rb
CHANGED
@@ -40,9 +40,9 @@ module Polynomials
|
|
40
40
|
def roots_of_quadratic_function(a,b,c)
|
41
41
|
p = b/a
|
42
42
|
q = c/a
|
43
|
-
|
44
|
-
return Set[] if
|
45
|
-
root = Math.sqrt(
|
43
|
+
radicand = (p/2.0)**2 - q
|
44
|
+
return Set[] if radicand < 0
|
45
|
+
root = Math.sqrt(radicand)
|
46
46
|
Set.new([:+,:-].map{ |operator| (-(p/2.0)).send(operator, root)}.map { |n| n.round(10) })
|
47
47
|
end
|
48
48
|
|
@@ -90,13 +90,13 @@ module Polynomials
|
|
90
90
|
end
|
91
91
|
|
92
92
|
def grouped_pointset(start,stop,step=0.1,grouped)
|
93
|
-
grouped.
|
94
|
-
intervals.reject
|
95
|
-
intervals.map
|
93
|
+
Hash[grouped.map do |key,intervals|
|
94
|
+
intervals = intervals.reject { |s,e| e < start || s > stop }
|
95
|
+
intervals = intervals.map do |s,e|
|
96
96
|
self.pointset([s,start].max, [e,stop].min, step)
|
97
97
|
end
|
98
|
-
|
99
|
-
|
98
|
+
[key,intervals]
|
99
|
+
end]
|
100
100
|
end
|
101
101
|
|
102
102
|
def coefficients_till(n)
|
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.3"
|
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-25}
|
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.
|
data/test/polynomial_test.rb
CHANGED
@@ -85,4 +85,14 @@ class TestPolynomial < MiniTest::Unit::TestCase
|
|
85
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
86
|
polynomial.grouped_pointset(-0.005,0.25,0.1, { :jellyfish => [[-0.01,0]], :peanut_butter => [[-Infinity,-0.1],[0,Infinity]] }))
|
87
87
|
end
|
88
|
+
|
89
|
+
def test_calculation_of_function_with_absolute_term
|
90
|
+
polynomial = Polynomial.parse('5x^2 + 5x + 2')
|
91
|
+
assert_equal 2, polynomial.(0)
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_calculation_of_function_with_absolute_term_linear
|
95
|
+
polynomial = Polynomial.parse('5x + 2')
|
96
|
+
assert_equal 7, polynomial.(1)
|
97
|
+
end
|
88
98
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 4
|
8
|
-
-
|
9
|
-
version: 0.4.
|
8
|
+
- 3
|
9
|
+
version: 0.4.3
|
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-25 00:00:00 +02:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -148,7 +148,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
148
148
|
requirements:
|
149
149
|
- - ">="
|
150
150
|
- !ruby/object:Gem::Version
|
151
|
-
hash:
|
151
|
+
hash: 2798926848061420980
|
152
152
|
segments:
|
153
153
|
- 0
|
154
154
|
version: "0"
|