lib/polynomials.rb in polynomials-0.1.4 vs lib/polynomials.rb in polynomials-0.1.5

- old
+ new

@@ -1,11 +1,11 @@ require 'set' require_relative 'term' require_relative 'core_ext/math' class Polynomial MinMaxMapping = { 1.0 => :max, -1.0 => :min } - AfterExtremumsCurvatureMapping = { max: :right, min: :left } + AfterextremaCurvatureMapping = { max: :right, min: :left } NegPosMinMaxExtremumMapping = {[1.0,-1.0] => :max,[-1.0,1.0] => :min} attr_accessor :terms def self.parse(string) @@ -60,21 +60,21 @@ Set[] end end end - def local_extremums + def local_extrema derivative = self.derivative max_min_extremum = Hash.new { |hash,key| hash[key] = Set.new } unless derivative.degree == 0 - possible_extremums = derivative.roots.sort + possible_extrema = derivative.roots.sort - samples = ([possible_extremums.first - 1] + possible_extremums.sort + [possible_extremums.last + 1]).each_cons(2).map do |before,after| + samples = ([possible_extrema.first - 1] + possible_extrema.sort + [possible_extrema.last + 1]).each_cons(2).map do |before,after| (before + after)/2 end - possible_extremums.zip(samples.each_cons(2)).each do |pe,(after,before)| + possible_extrema.zip(samples.each_cons(2)).each do |pe,(after,before)| yafter = derivative.calculate(after) ybefore = derivative.calculate(before) kind_of_extremum = NegPosMinMaxExtremumMapping[[yafter/yafter.abs,ybefore/ybefore.abs]] max_min_extremum[kind_of_extremum] << pe if kind_of_extremum end @@ -84,14 +84,14 @@ def curvature_behaviour if degree < 2 return {} else - extremums = self.derivative.extremums + extrema = self.derivative.extrema curvature_behaviour = Hash.new {|h,k|h[k]=Set.new} - extremums.values.inject(&:|).sort.each_cons(2) do |start_point,end_point| - curvature_behaviour[AfterExtremumsCurvatureMapping[extremums.find { |k,v| v.include?(start_point) }.first]] << Range.new(start_point,end_point) + extrema.values.inject(&:|).sort.each_cons(2) do |start_point,end_point| + curvature_behaviour[AfterextremaCurvatureMapping[extrema.find { |k,v| v.include?(start_point) }.first]] << Range.new(start_point,end_point) end end return curvature_behaviour end @@ -108,16 +108,16 @@ def ==(other) delete_zeros = proc{ |_,t| t.coefficient.zero? } self.terms.delete_if(&delete_zeros) == other.terms.delete_if(&delete_zeros) end - def extremums - extremums = local_extremums + def extrema + extrema = local_extrema a = self.terms[self.degree].coefficient max_or_min = (self.degree.even? ? [1,1] : [-1,1]).map { |n| (n * a)/a.abs } - extremums[MinMaxMapping[max_or_min.first]] << -1.0/0 - extremums[MinMaxMapping[max_or_min.last]] << 1.0/0 - return extremums + extrema[MinMaxMapping[max_or_min.first]] << -1.0/0 + extrema[MinMaxMapping[max_or_min.last]] << 1.0/0 + return extrema end private def coefficients_till(n) coefficients = []