require "#{File.dirname(File.expand_path(__FILE__))}/test_helper" Infinity = 1.0/0 class TestPolynomial < MiniTest::Unit::TestCase include Polynomials def test_to_s polynomial = Polynomial.parse('5x + 2x^2 + 20') assert_equal '2x^2 + 5x + 20', polynomial.to_s polynomial = Polynomial.parse('6.5435x^234 + 5.0 + 20x') assert_equal '6.5435x^234 + 20x + 5', polynomial.to_s polynomial = Polynomial.parse('0x^0') assert_equal "0", polynomial.to_s end def test_derivative_of_linear_functions polynomial = Polynomial.parse('5x + 2') assert_equal Polynomial.parse('5') ,polynomial.derivative end def test_derivative_functions_with_higher_degree polynomial = Polynomial.parse('5.5x^4 + 3.5x^3 + 2x^2 + 2x + 50') assert_equal Polynomial.parse('22x^3 + 10.5x^2 + 4x + 2'), polynomial.derivative end def test_calculation polynomial = Polynomial.parse('2x^4 - 1 x') assert_equal 1245, polynomial.calculate(5) end def test_parsing assert_equal 0, Polynomial.parse('2').terms.values.first.exponent assert_equal 2, Polynomial.parse('2').terms.values.first.coefficient terms = Polynomial.parse('2x^2 + 5.5x^1 - 20').terms.values.sort_by(&:exponent).reverse assert_equal [2,1,0], terms.map(&:exponent) assert_equal [2,5.5,-20], terms.map(&:coefficient) end def test_equality assert_equal Polynomial.parse('3x^1 + 5'), Polynomial.parse('3x + 5 x^0') end def test_degree assert_equal 5,Polynomial.parse('3x^5 - 5 x + 3').degree end def test_efficient_roots_calculation polynomial = Polynomial.parse('200x^2342435 + 6x^20') assert_set_eql(Set[Root.new(0.0)], polynomial.roots) end def test_lt polynomial = Polynomial.parse('1x^2342435 + 5x') assert_euqal Term.new(5,1), polynomial.lt end def test_lt polynomial = Polynomial.parse('1x^2342435 + 5x') assert_equal Term.new(2342435,1), polynomial.gt end def test_initalizer polynomial = Polynomial.new(5,0,2,-1) assert_equal '5x^3 + 2x - 1', polynomial.to_s end def test_pointset polynomial = Polynomial.new(1,0,0) assert_equal (-2..2).step(0.5).map { |x| [x,x**2] }, polynomial.pointset(-2,2,0.5) end def test_pointset_with_float_start polynomial = Polynomial.new(1,0,0) assert_equal [[0.1,0.1**2],[0.2,0.2**2],[0.25,0.25**2]], polynomial.pointset(0.1,0.25,0.1) end def test_grouped_pointset polynomial = Polynomial.new(1,0,0) assert_equal({ :jellyfish => [[[-0.1,0.1**2,],[0,0.0]]] , :peanut_butter => [[[0,0.0],[0.1,0.1**2],[0.2,0.2**2],[0.25,0.25**2]]]}, polynomial.grouped_pointset(-0.1,0.25,0.1, { :jellyfish => [[-Infinity,0]], :peanut_butter => [[0,Infinity]] })) end def test_grouped_pointset_omitting_inteval_through_start_out_of_range polynomial = Polynomial.new(1,0,0) assert_equal({ :jellyfish => [[[-0.005,0.005**2,],[0,0.0]]] , :peanut_butter => [[[0,0.0],[0.1,0.1**2],[0.2,0.2**2],[0.25,0.25**2]]]}, polynomial.grouped_pointset(-0.005,0.25,0.1, { :jellyfish => [[-0.01,0]], :peanut_butter => [[-Infinity,-0.1],[0,Infinity]] })) end def test_calculation_of_function_with_absolute_term polynomial = Polynomial.parse('5x^2 + 5x + 2') assert_equal 2, polynomial.(0) end def test_calculation_of_function_with_absolute_term_linear polynomial = Polynomial.parse('5x + 2') assert_equal 7, polynomial.(1) end def test_constant_zero_polynomial constant_function = Polynomial.new(0) assert_equal Set[], constant_function.roots assert_equal nil, constant_function.curvature_behaviour end end