polynomial_ruby 0.0.1 → 0.0.2
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.
- checksums.yaml +4 -4
- data/lib/polynomial_ruby.rb +7 -4
- data/lib/polynomial_ruby/interpolation.rb +18 -0
- data/lib/polynomial_ruby/interpolation_algorithm.rb +12 -0
- data/lib/polynomial_ruby/lagrange_algorithm.rb +23 -0
- data/lib/polynomial_ruby/lagrange_polynomial.rb +31 -0
- data/lib/polynomial_ruby/neville_algorithm.rb +32 -0
- data/lib/polynomial_ruby/polynomial.rb +79 -0
- data/lib/polynomial_ruby/version.rb +1 -1
- metadata +8 -6
- data/.gitignore +0 -22
- data/Gemfile +0 -4
- data/Rakefile +0 -2
- data/polynomial_ruby.gemspec +0 -24
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 48b49a2e557f6ef07603be79938c3db1c7ab32f9
         | 
| 4 | 
            +
              data.tar.gz: 89adc427db8cd3e577ce8a2f61829158c7edf7ff
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: ae58ef386017a81c234906c7fd9624ad8f289aef6f90ab31ff82b367b73f69375858f6037e3bcad19125aa168415bf690463ff53599ea230f8df0c3160ab33e0
         | 
| 7 | 
            +
              data.tar.gz: 2bc1b11d18736ed4dc53fd539dbb50a7361245b43bd6f51af6a52e09a45cb3ed69a2f34c87716a50dcab07b20d80700876c775a4b708b35cbf72c6820e1e7b3a
         | 
    
        data/lib/polynomial_ruby.rb
    CHANGED
    
    | @@ -1,5 +1,8 @@ | |
| 1 1 | 
             
            require "polynomial_ruby/version"
         | 
| 2 | 
            -
             | 
| 3 | 
            -
             | 
| 4 | 
            -
             | 
| 5 | 
            -
             | 
| 2 | 
            +
            require 'bigdecimal'
         | 
| 3 | 
            +
            require 'polynomial_ruby/polynomial'
         | 
| 4 | 
            +
            require 'polynomial_ruby/interpolation'
         | 
| 5 | 
            +
            require 'polynomial_ruby/interpolation_algorithm'
         | 
| 6 | 
            +
            require 'polynomial_ruby/lagrange_algorithm'
         | 
| 7 | 
            +
            require 'polynomial_ruby/lagrange_polynomial'
         | 
| 8 | 
            +
            require 'polynomial_ruby/neville_algorithm'
         | 
| @@ -0,0 +1,18 @@ | |
| 1 | 
            +
            require 'scanf'
         | 
| 2 | 
            +
            require 'bigdecimal'
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            points_count = scanf("%d")[0]
         | 
| 5 | 
            +
            points = Array.new(points_count) {
         | 
| 6 | 
            +
              scan = scanf("%s%s")
         | 
| 7 | 
            +
              [BigDecimal(scan[0]), BigDecimal(scan[1])]
         | 
| 8 | 
            +
            }
         | 
| 9 | 
            +
             | 
| 10 | 
            +
            point = BigDecimal(scanf("%s")[0])
         | 
| 11 | 
            +
             | 
| 12 | 
            +
            lagrange_polynomial = LagrangePolynomial.new(points).call
         | 
| 13 | 
            +
            lagrange_solution   = LagrangeAlgorithm.new(points).call(point)
         | 
| 14 | 
            +
            neville_solution    = NevilleAlgorithm.new(points).call(point)
         | 
| 15 | 
            +
             | 
| 16 | 
            +
            puts "Lagrange polynomial: #{lagrange_polynomial}"
         | 
| 17 | 
            +
            puts "Lagrange algorithm solution for #{point}: #{lagrange_solution}"
         | 
| 18 | 
            +
            puts "Neville algorithm solution for #{point}: #{neville_solution}"
         | 
| @@ -0,0 +1,12 @@ | |
| 1 | 
            +
            class InterpolationAlgorithm
         | 
| 2 | 
            +
              attr_accessor :points, :values
         | 
| 3 | 
            +
             | 
| 4 | 
            +
              def initialize(input)
         | 
| 5 | 
            +
                self.points = input.map { |a, _| a }
         | 
| 6 | 
            +
                self.values = input.map { |_, b| b }
         | 
| 7 | 
            +
              end
         | 
| 8 | 
            +
             | 
| 9 | 
            +
              def call(point)
         | 
| 10 | 
            +
                raise NotImplementedError, "Implement #call in #{self.class}"
         | 
| 11 | 
            +
              end
         | 
| 12 | 
            +
            end
         | 
| @@ -0,0 +1,23 @@ | |
| 1 | 
            +
            class LagrangeAlgorithm < InterpolationAlgorithm
         | 
| 2 | 
            +
              def call(point)
         | 
| 3 | 
            +
                basis_polynomials(point).zip(values)
         | 
| 4 | 
            +
                                 .map { |p, v| p * v }
         | 
| 5 | 
            +
                                 .reduce(:+)
         | 
| 6 | 
            +
              end
         | 
| 7 | 
            +
             | 
| 8 | 
            +
              private
         | 
| 9 | 
            +
             | 
| 10 | 
            +
              def basis_polynomials(point)
         | 
| 11 | 
            +
                Array.new(points.size) { |j|
         | 
| 12 | 
            +
                  numerator = BigDecimal(1)
         | 
| 13 | 
            +
                  denominator = BigDecimal(1)
         | 
| 14 | 
            +
                  points.each_with_index { |el, m|
         | 
| 15 | 
            +
                    numerator *= (point - points[m]) unless(j == m)
         | 
| 16 | 
            +
                  }
         | 
| 17 | 
            +
                  points.each_with_index { |el, m|
         | 
| 18 | 
            +
                    denominator *= (points[j] - points[m]) unless(j == m)
         | 
| 19 | 
            +
                  }
         | 
| 20 | 
            +
                  numerator / denominator
         | 
| 21 | 
            +
                }
         | 
| 22 | 
            +
              end
         | 
| 23 | 
            +
            end
         | 
| @@ -0,0 +1,31 @@ | |
| 1 | 
            +
            class LagrangePolynomial
         | 
| 2 | 
            +
              attr_accessor :points, :values
         | 
| 3 | 
            +
             | 
| 4 | 
            +
              def initialize(input)
         | 
| 5 | 
            +
                self.points = input.map { |a, _| a }
         | 
| 6 | 
            +
                self.values = input.map { |_, b| b }
         | 
| 7 | 
            +
              end
         | 
| 8 | 
            +
             | 
| 9 | 
            +
              def call
         | 
| 10 | 
            +
                basis_polynomials.zip(values)
         | 
| 11 | 
            +
                                 .map { |p, v| p * v }
         | 
| 12 | 
            +
                                 .reduce(:+)
         | 
| 13 | 
            +
              end
         | 
| 14 | 
            +
             | 
| 15 | 
            +
              private
         | 
| 16 | 
            +
             | 
| 17 | 
            +
              def basis_polynomials
         | 
| 18 | 
            +
                Array.new(points.size) { |j|
         | 
| 19 | 
            +
                  numerator = Polynomial.new([BigDecimal(1)])
         | 
| 20 | 
            +
                  denominator = BigDecimal(1)
         | 
| 21 | 
            +
                  points.each_with_index { |el, m|
         | 
| 22 | 
            +
                    numerator *= Polynomial.new([BigDecimal(0), BigDecimal(1)]) - el unless(j == m)
         | 
| 23 | 
            +
                  }
         | 
| 24 | 
            +
                  points.each_with_index { |el, m|
         | 
| 25 | 
            +
                    denominator *= (points[j] - points[m]) unless(j == m)
         | 
| 26 | 
            +
                  }
         | 
| 27 | 
            +
                  numerator / denominator
         | 
| 28 | 
            +
                }
         | 
| 29 | 
            +
              end
         | 
| 30 | 
            +
            end
         | 
| 31 | 
            +
             | 
| @@ -0,0 +1,32 @@ | |
| 1 | 
            +
            class NevilleAlgorithm < InterpolationAlgorithm
         | 
| 2 | 
            +
              def call(point)
         | 
| 3 | 
            +
                Solution.new(points, values, point).call
         | 
| 4 | 
            +
              end
         | 
| 5 | 
            +
             | 
| 6 | 
            +
              private
         | 
| 7 | 
            +
             | 
| 8 | 
            +
              Solution = Struct.new(:points, :values, :point) do
         | 
| 9 | 
            +
                def call
         | 
| 10 | 
            +
                  at(0, size)
         | 
| 11 | 
            +
                end
         | 
| 12 | 
            +
             | 
| 13 | 
            +
                private
         | 
| 14 | 
            +
             | 
| 15 | 
            +
                def at(i, j)
         | 
| 16 | 
            +
                  @at ||= {}
         | 
| 17 | 
            +
                  @at.fetch([i, j]) {
         | 
| 18 | 
            +
                    value = if i == j
         | 
| 19 | 
            +
                      values[i]
         | 
| 20 | 
            +
                    else
         | 
| 21 | 
            +
                      ((points[j]-point)*at(i, j-1)+(point-points[i])*at(i+1,j))/(points[j]-points[i])
         | 
| 22 | 
            +
                    end
         | 
| 23 | 
            +
                    @at[[i, j]] = value
         | 
| 24 | 
            +
                    value
         | 
| 25 | 
            +
                  }
         | 
| 26 | 
            +
                end
         | 
| 27 | 
            +
             | 
| 28 | 
            +
                def size
         | 
| 29 | 
            +
                  points.size - 1
         | 
| 30 | 
            +
                end
         | 
| 31 | 
            +
              end
         | 
| 32 | 
            +
            end
         | 
| @@ -0,0 +1,79 @@ | |
| 1 | 
            +
            require 'bigdecimal'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            class Polynomial
         | 
| 4 | 
            +
             | 
| 5 | 
            +
                attr_reader :coefficients
         | 
| 6 | 
            +
             | 
| 7 | 
            +
                def initialize(coefficients)
         | 
| 8 | 
            +
                  raise "At least one coefficient is required" if coefficients.size == 0
         | 
| 9 | 
            +
                  coefficients = coefficients[0...-1] while coefficients[-1] == 0 && coefficients.size > 1
         | 
| 10 | 
            +
                  @coefficients = Array(coefficients)
         | 
| 11 | 
            +
                end
         | 
| 12 | 
            +
             | 
| 13 | 
            +
                def degree
         | 
| 14 | 
            +
                  coefficients.size - 1
         | 
| 15 | 
            +
                end
         | 
| 16 | 
            +
             | 
| 17 | 
            +
                def +(other)
         | 
| 18 | 
            +
                  if other.kind_of?(Numeric)
         | 
| 19 | 
            +
                    self + self.class.new([other])
         | 
| 20 | 
            +
                  elsif other.kind_of?(self.class)
         | 
| 21 | 
            +
                    coef = Array.new([other.degree, degree].max + 1) { |i|
         | 
| 22 | 
            +
                      (other.coefficients[i] || 0) + (coefficients[i] || 0)
         | 
| 23 | 
            +
                    }
         | 
| 24 | 
            +
                    self.class.new(coef)
         | 
| 25 | 
            +
                  else
         | 
| 26 | 
            +
                    raise "Cannot sum #{self} with #{other}"
         | 
| 27 | 
            +
                  end
         | 
| 28 | 
            +
                end
         | 
| 29 | 
            +
             | 
| 30 | 
            +
                def *(other)
         | 
| 31 | 
            +
                  if other.kind_of?(Numeric)
         | 
| 32 | 
            +
                    self.class.new(coefficients.map { |c| c * other })
         | 
| 33 | 
            +
                  elsif other.kind_of?(self.class)
         | 
| 34 | 
            +
                    coefficients.map.with_index { |c, idx|
         | 
| 35 | 
            +
                      self.class.new(Array.new(idx, 0) + other.coefficients.map { |o| o * c })
         | 
| 36 | 
            +
                    }.reduce { |sum, p|
         | 
| 37 | 
            +
                      sum + p
         | 
| 38 | 
            +
                    }
         | 
| 39 | 
            +
                  else
         | 
| 40 | 
            +
                    raise "Cannot multiply #{self} by #{other}"
         | 
| 41 | 
            +
                  end
         | 
| 42 | 
            +
                end
         | 
| 43 | 
            +
             | 
| 44 | 
            +
                def -(other)
         | 
| 45 | 
            +
                  self + (other * -1)
         | 
| 46 | 
            +
                end
         | 
| 47 | 
            +
             | 
| 48 | 
            +
                def /(other)
         | 
| 49 | 
            +
                  if (other.kind_of?(Numeric))
         | 
| 50 | 
            +
                    Polynomial.new(coefficients.map { |c| c / other })
         | 
| 51 | 
            +
                  else
         | 
| 52 | 
            +
                    raise "cannot divide #{self} by #{other}"
         | 
| 53 | 
            +
                  end
         | 
| 54 | 
            +
                end
         | 
| 55 | 
            +
             | 
| 56 | 
            +
                def ==(other)
         | 
| 57 | 
            +
                  if (other.kind_of?(self.class))
         | 
| 58 | 
            +
                    coefficients == other.coefficients
         | 
| 59 | 
            +
                  else
         | 
| 60 | 
            +
                    super
         | 
| 61 | 
            +
                  end
         | 
| 62 | 
            +
                end
         | 
| 63 | 
            +
             | 
| 64 | 
            +
                def to_s
         | 
| 65 | 
            +
                  coefficients[1..-1].reverse.map.with_index { |c, i|
         | 
| 66 | 
            +
                    sign = if (i != 0)
         | 
| 67 | 
            +
                             if (c > 0) then
         | 
| 68 | 
            +
                               "+"
         | 
| 69 | 
            +
                             end
         | 
| 70 | 
            +
                           end
         | 
| 71 | 
            +
                    [sign, "#{c}x^#{degree-i}"].join unless c == 0
         | 
| 72 | 
            +
                  }.push(coefficients[0] != 0 ? "+ #{coefficients[0]}" : nil).compact.join
         | 
| 73 | 
            +
                end
         | 
| 74 | 
            +
             | 
| 75 | 
            +
                def inspect
         | 
| 76 | 
            +
                  "#<Polynomial: #{to_s} >"
         | 
| 77 | 
            +
                end
         | 
| 78 | 
            +
             | 
| 79 | 
            +
            end
         | 
    
        metadata
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: polynomial_ruby
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0.0. | 
| 4 | 
            +
              version: 0.0.2
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Dustin Zeisler
         | 
| @@ -53,21 +53,23 @@ dependencies: | |
| 53 53 | 
             
                  - !ruby/object:Gem::Version
         | 
| 54 54 | 
             
                    version: '2.14'
         | 
| 55 55 | 
             
            description: Forked from https://github.com/pewniak747/polynomial-ruby and made into
         | 
| 56 | 
            -
              a gem
         | 
| 56 | 
            +
              a gem.
         | 
| 57 57 | 
             
            email:
         | 
| 58 58 | 
             
            - dustin@zive.me
         | 
| 59 59 | 
             
            executables: []
         | 
| 60 60 | 
             
            extensions: []
         | 
| 61 61 | 
             
            extra_rdoc_files: []
         | 
| 62 62 | 
             
            files:
         | 
| 63 | 
            -
            - ".gitignore"
         | 
| 64 | 
            -
            - Gemfile
         | 
| 65 63 | 
             
            - LICENSE.txt
         | 
| 66 64 | 
             
            - README.md
         | 
| 67 | 
            -
            - Rakefile
         | 
| 68 65 | 
             
            - lib/polynomial_ruby.rb
         | 
| 66 | 
            +
            - lib/polynomial_ruby/interpolation.rb
         | 
| 67 | 
            +
            - lib/polynomial_ruby/interpolation_algorithm.rb
         | 
| 68 | 
            +
            - lib/polynomial_ruby/lagrange_algorithm.rb
         | 
| 69 | 
            +
            - lib/polynomial_ruby/lagrange_polynomial.rb
         | 
| 70 | 
            +
            - lib/polynomial_ruby/neville_algorithm.rb
         | 
| 71 | 
            +
            - lib/polynomial_ruby/polynomial.rb
         | 
| 69 72 | 
             
            - lib/polynomial_ruby/version.rb
         | 
| 70 | 
            -
            - polynomial_ruby.gemspec
         | 
| 71 73 | 
             
            homepage: https://github.com/zeislre/polynomial-ruby
         | 
| 72 74 | 
             
            licenses:
         | 
| 73 75 | 
             
            - MIT
         | 
    
        data/.gitignore
    DELETED
    
    | @@ -1,22 +0,0 @@ | |
| 1 | 
            -
            *.gem
         | 
| 2 | 
            -
            *.rbc
         | 
| 3 | 
            -
            .bundle
         | 
| 4 | 
            -
            .config
         | 
| 5 | 
            -
            .yardoc
         | 
| 6 | 
            -
            Gemfile.lock
         | 
| 7 | 
            -
            InstalledFiles
         | 
| 8 | 
            -
            _yardoc
         | 
| 9 | 
            -
            coverage
         | 
| 10 | 
            -
            doc/
         | 
| 11 | 
            -
            lib/bundler/man
         | 
| 12 | 
            -
            pkg
         | 
| 13 | 
            -
            rdoc
         | 
| 14 | 
            -
            spec/reports
         | 
| 15 | 
            -
            test/tmp
         | 
| 16 | 
            -
            test/version_tmp
         | 
| 17 | 
            -
            tmp
         | 
| 18 | 
            -
            *.bundle
         | 
| 19 | 
            -
            *.so
         | 
| 20 | 
            -
            *.o
         | 
| 21 | 
            -
            *.a
         | 
| 22 | 
            -
            mkmf.log
         | 
    
        data/Gemfile
    DELETED
    
    
    
        data/Rakefile
    DELETED
    
    
    
        data/polynomial_ruby.gemspec
    DELETED
    
    | @@ -1,24 +0,0 @@ | |
| 1 | 
            -
            # coding: utf-8
         | 
| 2 | 
            -
            lib = File.expand_path('../lib', __FILE__)
         | 
| 3 | 
            -
            $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
         | 
| 4 | 
            -
            require 'polynomial_ruby/version'
         | 
| 5 | 
            -
             | 
| 6 | 
            -
            Gem::Specification.new do |spec|
         | 
| 7 | 
            -
              spec.name          = "polynomial_ruby"
         | 
| 8 | 
            -
              spec.version       = PolynomialRuby::VERSION
         | 
| 9 | 
            -
              spec.authors       = ["Dustin Zeisler"]
         | 
| 10 | 
            -
              spec.email         = ["dustin@zive.me"]
         | 
| 11 | 
            -
              spec.summary       = %q{Forked from https://github.com/pewniak747/polynomial-ruby and made into a gem}
         | 
| 12 | 
            -
              spec.description   = %q{Forked from https://github.com/pewniak747/polynomial-ruby and made into a gem}
         | 
| 13 | 
            -
              spec.homepage      = "https://github.com/zeislre/polynomial-ruby"
         | 
| 14 | 
            -
              spec.license       = "MIT"
         | 
| 15 | 
            -
             | 
| 16 | 
            -
              spec.files         = `git ls-files -z`.split("\x0")
         | 
| 17 | 
            -
              spec.executables   = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
         | 
| 18 | 
            -
              spec.test_files    = spec.files.grep(%r{^(test|spec|features)/})
         | 
| 19 | 
            -
              spec.require_paths = ["lib"]
         | 
| 20 | 
            -
             | 
| 21 | 
            -
              spec.add_development_dependency "bundler", "~> 1.6"
         | 
| 22 | 
            -
              spec.add_development_dependency "rake", "10.3"
         | 
| 23 | 
            -
              spec.add_development_dependency "rspec", "2.14"
         | 
| 24 | 
            -
            end
         |