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

- old
+ new

@@ -41,13 +41,13 @@ yield new_function, term end return new_function end - def nulls + def roots if terms.keys.none?(&:zero?) - self.alter { |nf, term| nf.terms[term.exponent-1].coefficient = term.coefficient }.nulls << 0.0 + self.alter { |nf, term| nf.terms[term.exponent-1].coefficient = term.coefficient }.roots << 0.0 else case self.degree when 1 Set[-self.terms[0].coefficient / self.terms[1].coefficient] when 2 @@ -62,21 +62,23 @@ end end def local_extremums derivative = self.derivative - possible_extremums = derivative.nulls.sort max_min_extremum = Hash.new { |hash,key| hash[key] = Set.new } + unless derivative.degree == 0 + possible_extremums = derivative.roots.sort - samples = ([possible_extremums.first - 1] + possible_extremums.sort + [possible_extremums.last + 1]).each_cons(2).map do |before,after| - (before + after)/2 - end + samples = ([possible_extremums.first - 1] + possible_extremums.sort + [possible_extremums.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)| - 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 + possible_extremums.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 end return max_min_extremum end def curvature_behaviour