module Polynomials::Analyzable

Constants

AfterExtremaCurvatureMapping
MinimumOrMaximum

Public Instance Methods

curvature_behaviour() click to toggle source
# File lib/polynomials/analyzable.rb, line 41
def curvature_behaviour
  hash = Hash.new {|h,k|h[k]=[]}
  dd = self.derivative.derivative
  strives_for = derivative.strives_for
  return nil if !strives_for

  delimiter = [strives_for[0]] | derivative.local_extrema.to_a | [strives_for[1]]
  delimiter.sort.each_cons(2).group_by do |b,_|
    AfterExtremaCurvatureMapping[b.kind_of_extremum]
  end.tap { |h| h.values.each { |a| a.each { |r| r.map!(&:x) }}}
end
inflection_points() click to toggle source
# File lib/polynomials/analyzable.rb, line 16
def inflection_points
  self.derivative.local_extrema.map { |p| InflectionPoint.new(p.x,self.calculate(p.x)) }.to_set
end
local_extrema() click to toggle source
# File lib/polynomials/analyzable.rb, line 20
def local_extrema
  derivative = self.derivative
  possible_extrema = derivative.roots.sort.map(&:x)

  return Set[] if possible_extrema.empty?

  samples = possible_extrema.sort.each_cons(2).map do |before,after|
    (before + after)/2
  end

  samples.unshift possible_extrema.first - 1
  samples.push possible_extrema.last + 1

  possible_extrema.zip(samples.each_cons(2)).inject(Set[]) do |set,(pe,sample)|
    directions = sample.map { |x| derivative.(x).sign }
    kind_of_extremum = MinimumOrMaximum[directions]
    next set unless kind_of_extremum
    set <<  Extremum.new(pe,self.calculate(pe), kind_of_extremum)
  end
end
strives_for() click to toggle source
# File lib/polynomials/analyzable.rb, line 6
def strives_for
  points = self.roots.map(&:x) | self.local_extrema.map(&:x)
  return nil if points.empty?
  points.minmax.zip([-1,1])
  .map do |point,direction|
    sign = self.(point + direction).sign
    Extremum.new(Infinity * direction, sign * Infinity, sign < 0 ? :minimum : :maximum)
  end
end