require "#{File.dirname(File.expand_path(__FILE__))}/test_helper" Infinity = 1.0/0 class TestAnalyzable < MiniTest::Unit::TestCase include Polynomials def test_local_extrema polynomial = Polynomial.parse('3x^2 + 2x + 1') assert_set_eql(Set[ Extremum.new(-2.0/6.0,polynomial.calculate(-2.0/6.0), :minimum) ], polynomial.local_extrema) end def test_local_extrema_with_slope_of_derivative_equal_to_zero polynomial = Polynomial.parse('1x^4') assert_set_eql(Set[ Extremum.new(0.0,polynomial.calculate(0.0), :minimum) ], polynomial.local_extrema) end def test_no_local_extrema polynomial = Polynomial.parse('6x^6 - 5x + 50') assert_equal(Set[], polynomial.local_extrema) end def test_curvature_behaviour_no_inflection_points polynomial = Polynomial.parse('1 x^4') assert_equal({ left: [[-1.0/0,+1.0/0]] }, polynomial.curvature_behaviour) polynomial = Polynomial.parse('-1 x^4') assert_equal({ right: [[-1.0/0,+1.0/0]] }, polynomial.curvature_behaviour) polynomial = Polynomial.parse('5x^2') assert_equal({ left: [[-1.0/0,+1.0/0]] }, polynomial.curvature_behaviour) end def test_no_curvature polynomials = [] polynomials << Polynomial.parse('0') polynomials << Polynomial.parse('4') polynomials.each do |polynomial| assert_equal(nil, polynomial.curvature_behaviour) end end def test_no_local_extrema_of_derivative_leads_to_no_curvature polynomial = Polynomial.parse('50x^10 - 20x^2') assert_equal({:left=>[[-Infinity, Infinity]]}, polynomial.curvature_behaviour) end def test_curvature_behaviour_two_inflection_points polynomial = Polynomial.parse('+ 1.0 x^4 + 5.0 x^3 - 1.0 x^2 + 3.0 x + 5.0') assert_equal({left:[[-1.0/0,-2.5649778198], [0.0649778198,1.0/0]], right: [[-2.5649778198,0.0649778198]] } , polynomial.curvature_behaviour) end def test_curvature_behaviour_three_inflection_points polynomial = Polynomial.new(20,4,0,-1,-200) assert_equal( {:right=>[[(-1/10),0.0]], :left=>[[-Infinity,(-1/10)], [0.0,Infinity]]}, polynomial.curvature_behaviour) end def test_inflection_points polynomial = Polynomial.new(20,4,0,-1,-200) assert_set_eql Set[InflectionPoint.new(-1/10,polynomial.(-1/10)),InflectionPoint.new(0,polynomial.(0))], polynomial.inflection_points end def test_strives_for polynomial = Polynomial.new(2,0,0) assert_equal [Infinity,Infinity], polynomial.strives_for polynomial = Polynomial.new(-2,0,0) assert_equal [-Infinity,-Infinity], polynomial.strives_for polynomial = Polynomial.new(2,0,0,0) assert_equal [-Infinity,Infinity], polynomial.strives_for polynomial = Polynomial.new(-2,0,0,0) assert_equal [Infinity,-Infinity], polynomial.strives_for polynomial = Polynomial.new(2) assert_equal nil, polynomial.strives_for polynomial = Polynomial.parse('+ 1.0 x^4 + 5.0 x^3 - 1.0 x^2 + 3.0 x + 5.0').derivative assert_equal [-Infinity,Infinity], polynomial.strives_for end end