polynomials 0.1.7 → 0.2.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/VERSION +1 -1
 - data/lib/polynomials.rb +33 -21
 - data/lib/term.rb +21 -6
 - data/polynomials.gemspec +2 -3
 - data/test/math_test.rb +1 -1
 - data/test/polynomial_test.rb +42 -6
 - data/test/term_test.rb +12 -0
 - metadata +5 -6
 - data/tags +0 -91
 
    
        data/VERSION
    CHANGED
    
    | 
         @@ -1 +1 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            0. 
     | 
| 
      
 1 
     | 
    
         
            +
            0.2.0
         
     | 
    
        data/lib/polynomials.rb
    CHANGED
    
    | 
         @@ -19,8 +19,13 @@ class Polynomial 
     | 
|
| 
       19 
19 
     | 
    
         
             
                return polynomial
         
     | 
| 
       20 
20 
     | 
    
         
             
              end
         
     | 
| 
       21 
21 
     | 
    
         | 
| 
       22 
     | 
    
         
            -
              def initialize
         
     | 
| 
      
 22 
     | 
    
         
            +
              def initialize(*args)
         
     | 
| 
       23 
23 
     | 
    
         
             
                self.terms = Hash.new { |hash, key| hash[key] = Term.new(key) }
         
     | 
| 
      
 24 
     | 
    
         
            +
                unless args.empty?
         
     | 
| 
      
 25 
     | 
    
         
            +
                  args.reverse.each.with_index do |coefficient,exponent|
         
     | 
| 
      
 26 
     | 
    
         
            +
                    self.terms[exponent].coefficient += coefficient
         
     | 
| 
      
 27 
     | 
    
         
            +
                  end
         
     | 
| 
      
 28 
     | 
    
         
            +
                end
         
     | 
| 
       24 
29 
     | 
    
         
             
              end
         
     | 
| 
       25 
30 
     | 
    
         | 
| 
       26 
31 
     | 
    
         
             
              def calculate(x)
         
     | 
| 
         @@ -30,16 +35,16 @@ class Polynomial 
     | 
|
| 
       30 
35 
     | 
    
         
             
              end
         
     | 
| 
       31 
36 
     | 
    
         | 
| 
       32 
37 
     | 
    
         
             
              def derivative
         
     | 
| 
       33 
     | 
    
         
            -
             
     | 
| 
       34 
     | 
    
         
            -
             
     | 
| 
       35 
     | 
    
         
            -
             
     | 
| 
       36 
     | 
    
         
            -
             
     | 
| 
      
 38 
     | 
    
         
            +
                new_function = self.alter do |nf, term|
         
     | 
| 
      
 39 
     | 
    
         
            +
                  nf.terms[term.exponent - 1].coefficient += term.exponent * term.coefficient
         
     | 
| 
      
 40 
     | 
    
         
            +
                end
         
     | 
| 
      
 41 
     | 
    
         
            +
                new_function.terms.reject! { |_,t| t.coefficient == 0 }
         
     | 
| 
       37 
42 
     | 
    
         
             
                return new_function
         
     | 
| 
       38 
43 
     | 
    
         
             
              end
         
     | 
| 
       39 
44 
     | 
    
         | 
| 
       40 
45 
     | 
    
         
             
              def roots
         
     | 
| 
       41 
46 
     | 
    
         
             
                if !terms.empty? and terms.keys.none?(&:zero?)
         
     | 
| 
       42 
     | 
    
         
            -
                  self.alter { |nf, term| nf.terms[term.exponent- 
     | 
| 
      
 47 
     | 
    
         
            +
                  self.alter { |nf, term| nf.terms[term.exponent-self.lt.exponent].coefficient = term.coefficient }.roots << 0.0
         
     | 
| 
       43 
48 
     | 
    
         
             
                else
         
     | 
| 
       44 
49 
     | 
    
         
             
                  case self.degree
         
     | 
| 
       45 
50 
     | 
    
         
             
                  when 1
         
     | 
| 
         @@ -67,9 +72,9 @@ class Polynomial 
     | 
|
| 
       67 
72 
     | 
    
         | 
| 
       68 
73 
     | 
    
         
             
                  possible_extrema.zip(samples.each_cons(2)).each do |pe,(after,before)|
         
     | 
| 
       69 
74 
     | 
    
         
             
                    yafter = derivative.calculate(after)
         
     | 
| 
       70 
     | 
    
         
            -
             
     | 
| 
       71 
     | 
    
         
            -
             
     | 
| 
       72 
     | 
    
         
            -
             
     | 
| 
      
 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
         
     | 
| 
       73 
78 
     | 
    
         
             
                  end
         
     | 
| 
       74 
79 
     | 
    
         
             
                end
         
     | 
| 
       75 
80 
     | 
    
         
             
                return max_min_extremum
         
     | 
| 
         @@ -77,11 +82,11 @@ class Polynomial 
     | 
|
| 
       77 
82 
     | 
    
         | 
| 
       78 
83 
     | 
    
         
             
              def curvature_behaviour
         
     | 
| 
       79 
84 
     | 
    
         
             
                hash = Hash.new {|h,k|h[k]=Set.new}
         
     | 
| 
      
 85 
     | 
    
         
            +
                return hash if self.degree > 5
         
     | 
| 
       80 
86 
     | 
    
         
             
                extrema = self.derivative.extrema
         
     | 
| 
       81 
     | 
    
         
            -
                extrema.values.inject(Set[],&:|).sort 
     | 
| 
       82 
     | 
    
         
            -
             
     | 
| 
       83 
     | 
    
         
            -
                   
     | 
| 
       84 
     | 
    
         
            -
                  curvature_behaviour
         
     | 
| 
      
 87 
     | 
    
         
            +
                all_extremas = extrema.values.inject(Set[],&:|).sort
         
     | 
| 
      
 88 
     | 
    
         
            +
                all_extremas.each_cons(2).map { |s,e| Range.new(s,e) }.group_by do |range|
         
     | 
| 
      
 89 
     | 
    
         
            +
                  kind_of_curvature = AfterextremaCurvatureMapping[extrema.find { |k,v| v.include?(range.begin) }.first]
         
     | 
| 
       85 
90 
     | 
    
         
             
                end
         
     | 
| 
       86 
91 
     | 
    
         
             
              end
         
     | 
| 
       87 
92 
     | 
    
         | 
| 
         @@ -90,9 +95,10 @@ class Polynomial 
     | 
|
| 
       90 
95 
     | 
    
         
             
              end
         
     | 
| 
       91 
96 
     | 
    
         | 
| 
       92 
97 
     | 
    
         
             
              def to_s
         
     | 
| 
       93 
     | 
    
         
            -
                terms. 
     | 
| 
       94 
     | 
    
         
            -
                  string << term.to_s
         
     | 
| 
       95 
     | 
    
         
            -
             
     | 
| 
      
 98 
     | 
    
         
            +
                terms.sort_by { |_,t| -t.exponent }.inject("") do |string,(_,term)|
         
     | 
| 
      
 99 
     | 
    
         
            +
                  string << term.to_s unless term.coefficient.zero? && !term.exponent.zero?
         
     | 
| 
      
 100 
     | 
    
         
            +
                  string
         
     | 
| 
      
 101 
     | 
    
         
            +
                end.strip.sub(/\A\+\s/, '')
         
     | 
| 
       96 
102 
     | 
    
         
             
              end
         
     | 
| 
       97 
103 
     | 
    
         | 
| 
       98 
104 
     | 
    
         
             
              def ==(other)
         
     | 
| 
         @@ -102,16 +108,22 @@ class Polynomial 
     | 
|
| 
       102 
108 
     | 
    
         | 
| 
       103 
109 
     | 
    
         
             
              def extrema
         
     | 
| 
       104 
110 
     | 
    
         
             
                extrema = local_extrema
         
     | 
| 
       105 
     | 
    
         
            -
                a = self.terms[self.degree].coefficient
         
     | 
| 
       106 
111 
     | 
    
         
             
                unless self.degree == 0
         
     | 
| 
       107 
     | 
    
         
            -
                   
     | 
| 
      
 112 
     | 
    
         
            +
                  a = self.gt.coefficient
         
     | 
| 
      
 113 
     | 
    
         
            +
                  max_or_min = (self.degree.even? ? [1.0,1.0] : [-1.0,1.0]).map { |n| (n * a)/a.abs  }
         
     | 
| 
       108 
114 
     | 
    
         
             
                  extrema[MinMaxMapping[max_or_min.first]] << -1.0/0
         
     | 
| 
       109 
115 
     | 
    
         
             
                  extrema[MinMaxMapping[max_or_min.last]] << 1.0/0
         
     | 
| 
       110 
116 
     | 
    
         
             
                end
         
     | 
| 
       111 
117 
     | 
    
         
             
                return extrema
         
     | 
| 
       112 
118 
     | 
    
         
             
              end
         
     | 
| 
       113 
119 
     | 
    
         | 
| 
       114 
     | 
    
         
            -
              
     | 
| 
      
 120 
     | 
    
         
            +
              def gt
         
     | 
| 
      
 121 
     | 
    
         
            +
                self.terms[self.degree]
         
     | 
| 
      
 122 
     | 
    
         
            +
              end
         
     | 
| 
      
 123 
     | 
    
         
            +
             
     | 
| 
      
 124 
     | 
    
         
            +
              def lt
         
     | 
| 
      
 125 
     | 
    
         
            +
                self.terms[self.terms.min_by{ |_,t| t.exponent}.first]
         
     | 
| 
      
 126 
     | 
    
         
            +
              end
         
     | 
| 
       115 
127 
     | 
    
         | 
| 
       116 
128 
     | 
    
         
             
              def coefficients_till(n)
         
     | 
| 
       117 
129 
     | 
    
         
             
                coefficients = []
         
     | 
| 
         @@ -120,8 +132,7 @@ class Polynomial 
     | 
|
| 
       120 
132 
     | 
    
         
             
                end
         
     | 
| 
       121 
133 
     | 
    
         
             
                return coefficients.reverse
         
     | 
| 
       122 
134 
     | 
    
         
             
              end
         
     | 
| 
       123 
     | 
    
         
            -
             
     | 
| 
       124 
     | 
    
         
            -
             protected
         
     | 
| 
      
 135 
     | 
    
         
            +
              private :coefficients_till
         
     | 
| 
       125 
136 
     | 
    
         | 
| 
       126 
137 
     | 
    
         
             
              def alter
         
     | 
| 
       127 
138 
     | 
    
         
             
                new_function = self.class.new
         
     | 
| 
         @@ -130,4 +141,5 @@ class Polynomial 
     | 
|
| 
       130 
141 
     | 
    
         
             
                end
         
     | 
| 
       131 
142 
     | 
    
         
             
                return new_function
         
     | 
| 
       132 
143 
     | 
    
         
             
              end
         
     | 
| 
      
 144 
     | 
    
         
            +
              protected :alter
         
     | 
| 
       133 
145 
     | 
    
         
             
            end
         
     | 
    
        data/lib/term.rb
    CHANGED
    
    | 
         @@ -1,3 +1,4 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            class NotParsableError < ArgumentError;end
         
     | 
| 
       1 
2 
     | 
    
         
             
            class Term
         
     | 
| 
       2 
3 
     | 
    
         
             
              attr_accessor :coefficient, :exponent
         
     | 
| 
       3 
4 
     | 
    
         | 
| 
         @@ -7,16 +8,30 @@ class Term 
     | 
|
| 
       7 
8 
     | 
    
         | 
| 
       8 
9 
     | 
    
         
             
              def self.parse(string)
         
     | 
| 
       9 
10 
     | 
    
         
             
                term = self.new
         
     | 
| 
       10 
     | 
    
         
            -
                float = /\d 
     | 
| 
       11 
     | 
    
         
            -
                integer = /\d 
     | 
| 
       12 
     | 
    
         
            -
                 
     | 
| 
       13 
     | 
    
         
            -
             
     | 
| 
       14 
     | 
    
         
            -
             
     | 
| 
      
 11 
     | 
    
         
            +
                float = /\d+(?:\.\d+)?/
         
     | 
| 
      
 12 
     | 
    
         
            +
                integer = /\d+/
         
     | 
| 
      
 13 
     | 
    
         
            +
                polynomial_regex = /
         
     | 
| 
      
 14 
     | 
    
         
            +
                  \A\s*
         
     | 
| 
      
 15 
     | 
    
         
            +
                    (?<algebraic_sign>[-+]?)
         
     | 
| 
      
 16 
     | 
    
         
            +
                  \s*
         
     | 
| 
      
 17 
     | 
    
         
            +
                    (?<coefficient>#{float})?
         
     | 
| 
      
 18 
     | 
    
         
            +
                  \s*
         
     | 
| 
      
 19 
     | 
    
         
            +
                    (?<power>x(?:\^(?<exponent>#{integer}))?)?
         
     | 
| 
      
 20 
     | 
    
         
            +
                  \s*\Z
         
     | 
| 
      
 21 
     | 
    
         
            +
                /x
         
     | 
| 
      
 22 
     | 
    
         
            +
             
     | 
| 
      
 23 
     | 
    
         
            +
                raw_data = string.match(polynomial_regex)
         
     | 
| 
      
 24 
     | 
    
         
            +
                raise NotParsableError unless raw_data
         
     | 
| 
      
 25 
     | 
    
         
            +
                term.coefficient = (raw_data[:algebraic_sign] + ( raw_data[:coefficient] || "1")).to_f
         
     | 
| 
      
 26 
     | 
    
         
            +
                term.exponent = raw_data[:power] ? (raw_data[:exponent] || 1).to_i : 0
         
     | 
| 
       15 
27 
     | 
    
         
             
                term
         
     | 
| 
       16 
28 
     | 
    
         
             
              end
         
     | 
| 
       17 
29 
     | 
    
         | 
| 
       18 
30 
     | 
    
         
             
              def to_s
         
     | 
| 
       19 
     | 
    
         
            -
                 
     | 
| 
      
 31 
     | 
    
         
            +
                pretty_coeffiecent = coefficient.denominator == 1 ? coefficient.abs.to_i : coefficient.abs
         
     | 
| 
      
 32 
     | 
    
         
            +
                algebraic_sign =  coefficient < 0 ? '-' : '+'
         
     | 
| 
      
 33 
     | 
    
         
            +
                power = "x#{"^#{exponent}" unless exponent == 1}"
         
     | 
| 
      
 34 
     | 
    
         
            +
                "#{algebraic_sign} #{pretty_coeffiecent}#{ power unless exponent.zero?} "
         
     | 
| 
       20 
35 
     | 
    
         
             
              end
         
     | 
| 
       21 
36 
     | 
    
         | 
| 
       22 
37 
     | 
    
         
             
              def dup
         
     | 
    
        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.2.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-14}
         
     | 
| 
       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.
         
     | 
| 
         @@ -31,7 +31,6 @@ 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",
         
     | 
| 
       35 
34 
     | 
    
         
             
                "test/helper.rb",
         
     | 
| 
       36 
35 
     | 
    
         
             
                "test/math_test.rb",
         
     | 
| 
       37 
36 
     | 
    
         
             
                "test/polynomial_test.rb",
         
     | 
    
        data/test/math_test.rb
    CHANGED
    
    
    
        data/test/polynomial_test.rb
    CHANGED
    
    | 
         @@ -1,9 +1,14 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            require_relative 'test_helper'
         
     | 
| 
       2 
2 
     | 
    
         | 
| 
      
 3 
     | 
    
         
            +
            Infinity = 1.0/0
         
     | 
| 
       3 
4 
     | 
    
         
             
            class TestPolynomial < MiniTest::Unit::TestCase
         
     | 
| 
       4 
5 
     | 
    
         
             
              def test_to_s
         
     | 
| 
       5 
6 
     | 
    
         
             
                polynomial = Polynomial.parse('5x + 2x^2 + 20')
         
     | 
| 
       6 
     | 
    
         
            -
                assert_equal ' 
     | 
| 
      
 7 
     | 
    
         
            +
                assert_equal '2x^2 + 5x + 20', polynomial.to_s
         
     | 
| 
      
 8 
     | 
    
         
            +
                polynomial = Polynomial.parse('6.5435x^234 + 5.0 + 20x')
         
     | 
| 
      
 9 
     | 
    
         
            +
                assert_equal '6.5435x^234 + 20x + 5', polynomial.to_s
         
     | 
| 
      
 10 
     | 
    
         
            +
                polynomial = Polynomial.parse('0x^0')
         
     | 
| 
      
 11 
     | 
    
         
            +
                assert_equal "0", polynomial.to_s
         
     | 
| 
       7 
12 
     | 
    
         
             
              end
         
     | 
| 
       8 
13 
     | 
    
         | 
| 
       9 
14 
     | 
    
         
             
              def test_derivative_of_linear_functions
         
     | 
| 
         @@ -58,13 +63,13 @@ class TestPolynomial < MiniTest::Unit::TestCase 
     | 
|
| 
       58 
63 
     | 
    
         | 
| 
       59 
64 
     | 
    
         
             
              def test_no_inflection_points
         
     | 
| 
       60 
65 
     | 
    
         
             
                polynomial = Polynomial.parse('1 x^4')
         
     | 
| 
       61 
     | 
    
         
            -
                assert_equal({ left:  
     | 
| 
      
 66 
     | 
    
         
            +
                assert_equal({ left: [-1.0/0..+1.0/0] }, polynomial.curvature_behaviour)
         
     | 
| 
       62 
67 
     | 
    
         | 
| 
       63 
68 
     | 
    
         
             
                polynomial = Polynomial.parse('-1 x^4')
         
     | 
| 
       64 
     | 
    
         
            -
                assert_equal({ right:  
     | 
| 
      
 69 
     | 
    
         
            +
                assert_equal({ right: [-1.0/0..+1.0/0] }, polynomial.curvature_behaviour)
         
     | 
| 
       65 
70 
     | 
    
         | 
| 
       66 
71 
     | 
    
         
             
                polynomial = Polynomial.parse('5x^2')
         
     | 
| 
       67 
     | 
    
         
            -
                assert_equal({ left:  
     | 
| 
      
 72 
     | 
    
         
            +
                assert_equal({ left: [-1.0/0..+1.0/0] }, polynomial.curvature_behaviour)
         
     | 
| 
       68 
73 
     | 
    
         
             
              end
         
     | 
| 
       69 
74 
     | 
    
         | 
| 
       70 
75 
     | 
    
         
             
              def test_no_extremums
         
     | 
| 
         @@ -72,7 +77,7 @@ class TestPolynomial < MiniTest::Unit::TestCase 
     | 
|
| 
       72 
77 
     | 
    
         
             
                assert_equal({}, polynomial.extrema)
         
     | 
| 
       73 
78 
     | 
    
         
             
              end
         
     | 
| 
       74 
79 
     | 
    
         | 
| 
       75 
     | 
    
         
            -
              def  
     | 
| 
      
 80 
     | 
    
         
            +
              def test_no_curvature
         
     | 
| 
       76 
81 
     | 
    
         
             
                polynomials = []
         
     | 
| 
       77 
82 
     | 
    
         
             
                polynomials << Polynomial.parse('5 x + 4')
         
     | 
| 
       78 
83 
     | 
    
         
             
                polynomials << Polynomial.parse('4')
         
     | 
| 
         @@ -81,8 +86,39 @@ class TestPolynomial < MiniTest::Unit::TestCase 
     | 
|
| 
       81 
86 
     | 
    
         
             
                end
         
     | 
| 
       82 
87 
     | 
    
         
             
              end
         
     | 
| 
       83 
88 
     | 
    
         | 
| 
      
 89 
     | 
    
         
            +
              def test_local_extrema_of_derivative_leads_to_no_curvature
         
     | 
| 
      
 90 
     | 
    
         
            +
                polynomial = Polynomial.parse('50x^10 - 20x^2')
         
     | 
| 
      
 91 
     | 
    
         
            +
                assert_equal({}, polynomial.curvature_behaviour)
         
     | 
| 
      
 92 
     | 
    
         
            +
              end
         
     | 
| 
      
 93 
     | 
    
         
            +
             
     | 
| 
       84 
94 
     | 
    
         
             
              def test_two_inflection_points
         
     | 
| 
       85 
95 
     | 
    
         
             
                polynomial = Polynomial.parse('+ 1.0 x^4 + 5.0 x^3 - 1.0 x^2 + 3.0 x + 5.0')
         
     | 
| 
       86 
     | 
    
         
            -
                assert_equal({left: 
     | 
| 
      
 96 
     | 
    
         
            +
                assert_equal({left:[-1.0/0..-2.5649778198, 0.0649778198..1.0/0], right: [-2.5649778198..0.0649778198] } , polynomial.curvature_behaviour)
         
     | 
| 
      
 97 
     | 
    
         
            +
              end
         
     | 
| 
      
 98 
     | 
    
         
            +
             
     | 
| 
      
 99 
     | 
    
         
            +
              def test_three_inflection_points
         
     | 
| 
      
 100 
     | 
    
         
            +
                polynomial = Polynomial.new(20,4,0,-1,-200)
         
     | 
| 
      
 101 
     | 
    
         
            +
                assert_equal( {:right=>[(-1/10)..0.0], :left=>[-Infinity..(-1/10), 0.0..Infinity]}, polynomial.curvature_behaviour)
         
     | 
| 
      
 102 
     | 
    
         
            +
              end
         
     | 
| 
      
 103 
     | 
    
         
            +
             
     | 
| 
      
 104 
     | 
    
         
            +
             
     | 
| 
      
 105 
     | 
    
         
            +
              def test_efficient_roots_calculation
         
     | 
| 
      
 106 
     | 
    
         
            +
                polynomial = Polynomial.parse('200x^2342435 + 6x^20')
         
     | 
| 
      
 107 
     | 
    
         
            +
                assert_equal(Set[0.0], polynomial.roots)
         
     | 
| 
      
 108 
     | 
    
         
            +
              end
         
     | 
| 
      
 109 
     | 
    
         
            +
             
     | 
| 
      
 110 
     | 
    
         
            +
              def test_lt
         
     | 
| 
      
 111 
     | 
    
         
            +
                polynomial = Polynomial.parse('1x^2342435 + 5x')
         
     | 
| 
      
 112 
     | 
    
         
            +
                assert_euqal Term.new(5,1), polynomial.lt
         
     | 
| 
      
 113 
     | 
    
         
            +
              end
         
     | 
| 
      
 114 
     | 
    
         
            +
             
     | 
| 
      
 115 
     | 
    
         
            +
              def test_lt
         
     | 
| 
      
 116 
     | 
    
         
            +
                polynomial = Polynomial.parse('1x^2342435 + 5x')
         
     | 
| 
      
 117 
     | 
    
         
            +
                assert_equal Term.new(2342435,1), polynomial.gt
         
     | 
| 
      
 118 
     | 
    
         
            +
              end
         
     | 
| 
      
 119 
     | 
    
         
            +
             
     | 
| 
      
 120 
     | 
    
         
            +
              def test_initalizer
         
     | 
| 
      
 121 
     | 
    
         
            +
                polynomial = Polynomial.new(5,0,2,-1)
         
     | 
| 
      
 122 
     | 
    
         
            +
                assert_equal '5x^3 + 2x - 1', polynomial.to_s
         
     | 
| 
       87 
123 
     | 
    
         
             
              end
         
     | 
| 
       88 
124 
     | 
    
         
             
            end
         
     | 
    
        data/test/term_test.rb
    CHANGED
    
    | 
         @@ -24,4 +24,16 @@ class TestTerm < MiniTest::Unit::TestCase 
     | 
|
| 
       24 
24 
     | 
    
         
             
                assert_equal 6, Term.parse('6').coefficient
         
     | 
| 
       25 
25 
     | 
    
         
             
                assert_equal 0, Term.parse('6').exponent
         
     | 
| 
       26 
26 
     | 
    
         
             
              end
         
     | 
| 
      
 27 
     | 
    
         
            +
             
     | 
| 
      
 28 
     | 
    
         
            +
              def test_invalid_string_raises_not_parsable_error
         
     | 
| 
      
 29 
     | 
    
         
            +
                ['6x^ -5', '6^20',  '2 2', 'xx', '2 ^ x'].each do |not_parsable_string|
         
     | 
| 
      
 30 
     | 
    
         
            +
                  assert_raises NotParsableError do
         
     | 
| 
      
 31 
     | 
    
         
            +
                    term = Term.parse not_parsable_string
         
     | 
| 
      
 32 
     | 
    
         
            +
                  end
         
     | 
| 
      
 33 
     | 
    
         
            +
                end
         
     | 
| 
      
 34 
     | 
    
         
            +
              end
         
     | 
| 
      
 35 
     | 
    
         
            +
             
     | 
| 
      
 36 
     | 
    
         
            +
              def testing_to_s
         
     | 
| 
      
 37 
     | 
    
         
            +
                assert_equal "+ 6 ", Term.parse('6').to_s
         
     | 
| 
      
 38 
     | 
    
         
            +
              end
         
     | 
| 
       27 
39 
     | 
    
         
             
            end
         
     | 
    
        metadata
    CHANGED
    
    | 
         @@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version 
     | 
|
| 
       4 
4 
     | 
    
         
             
              prerelease: false
         
     | 
| 
       5 
5 
     | 
    
         
             
              segments: 
         
     | 
| 
       6 
6 
     | 
    
         
             
              - 0
         
     | 
| 
       7 
     | 
    
         
            -
              -  
     | 
| 
       8 
     | 
    
         
            -
              -  
     | 
| 
       9 
     | 
    
         
            -
              version: 0. 
     | 
| 
      
 7 
     | 
    
         
            +
              - 2
         
     | 
| 
      
 8 
     | 
    
         
            +
              - 0
         
     | 
| 
      
 9 
     | 
    
         
            +
              version: 0.2.0
         
     | 
| 
       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-14 00:00:00 +02:00
         
     | 
| 
       18 
18 
     | 
    
         
             
            default_executable: 
         
     | 
| 
       19 
19 
     | 
    
         
             
            dependencies: 
         
     | 
| 
       20 
20 
     | 
    
         
             
            - !ruby/object:Gem::Dependency 
         
     | 
| 
         @@ -96,7 +96,6 @@ files: 
     | 
|
| 
       96 
96 
     | 
    
         
             
            - lib/polynomials.rb
         
     | 
| 
       97 
97 
     | 
    
         
             
            - lib/term.rb
         
     | 
| 
       98 
98 
     | 
    
         
             
            - polynomials.gemspec
         
     | 
| 
       99 
     | 
    
         
            -
            - tags
         
     | 
| 
       100 
99 
     | 
    
         
             
            - test/helper.rb
         
     | 
| 
       101 
100 
     | 
    
         
             
            - test/math_test.rb
         
     | 
| 
       102 
101 
     | 
    
         
             
            - test/polynomial_test.rb
         
     | 
| 
         @@ -116,7 +115,7 @@ required_ruby_version: !ruby/object:Gem::Requirement 
     | 
|
| 
       116 
115 
     | 
    
         
             
              requirements: 
         
     | 
| 
       117 
116 
     | 
    
         
             
              - - ">="
         
     | 
| 
       118 
117 
     | 
    
         
             
                - !ruby/object:Gem::Version 
         
     | 
| 
       119 
     | 
    
         
            -
                  hash: - 
     | 
| 
      
 118 
     | 
    
         
            +
                  hash: -2290630464224354984
         
     | 
| 
       120 
119 
     | 
    
         
             
                  segments: 
         
     | 
| 
       121 
120 
     | 
    
         
             
                  - 0
         
     | 
| 
       122 
121 
     | 
    
         
             
                  version: "0"
         
     | 
    
        data/tags
    DELETED
    
    | 
         @@ -1,91 +0,0 @@ 
     | 
|
| 
       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
         
     |