| 1 | 
tim | 
1797 | 
#include "math/PolynomialTestCase.hpp" | 
| 2 | 
  | 
  | 
 | 
| 3 | 
  | 
  | 
// Registers the fixture into the 'registry' | 
| 4 | 
  | 
  | 
CPPUNIT_TEST_SUITE_REGISTRATION( PolynomialTestCase ); | 
| 5 | 
  | 
  | 
 | 
| 6 | 
  | 
  | 
 | 
| 7 | 
  | 
  | 
void PolynomialTestCase::testPolynomial(){ | 
| 8 | 
  | 
  | 
    DoublePolynomial dp1; | 
| 9 | 
  | 
  | 
 | 
| 10 | 
  | 
  | 
    dp1.setCoefficient(1, 3.0); | 
| 11 | 
  | 
  | 
    dp1.setCoefficient(2, 1.0); | 
| 12 | 
  | 
  | 
    dp1.setCoefficient(3, 2.0); | 
| 13 | 
  | 
  | 
 | 
| 14 | 
  | 
  | 
    CPPUNIT_ASSERT_DOUBLES_EQUAL(dp1.getCoefficient(1), 3.0, 0.000001); | 
| 15 | 
  | 
  | 
    CPPUNIT_ASSERT_DOUBLES_EQUAL(dp1.getCoefficient(2), 1.0, 0.000001); | 
| 16 | 
  | 
  | 
    CPPUNIT_ASSERT_DOUBLES_EQUAL(dp1.getCoefficient(3), 2.0, 0.000001); | 
| 17 | 
  | 
  | 
 | 
| 18 | 
  | 
  | 
    CPPUNIT_ASSERT(dp1.size() == 3); | 
| 19 | 
  | 
  | 
    CPPUNIT_ASSERT(dp1.begin()->first == 1); | 
| 20 | 
  | 
  | 
    CPPUNIT_ASSERT_DOUBLES_EQUAL(dp1.begin()->second ,3.0, 0.000001); | 
| 21 | 
  | 
  | 
     | 
| 22 | 
  | 
  | 
 | 
| 23 | 
  | 
  | 
    dp1.addCoefficient(2, 1.5); | 
| 24 | 
  | 
  | 
    CPPUNIT_ASSERT_DOUBLES_EQUAL(dp1.getCoefficient(2), 2.5, 0.000001); | 
| 25 | 
  | 
  | 
 | 
| 26 | 
  | 
  | 
    CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, dp1.evaluate(0.0), 0.000001); | 
| 27 | 
  | 
  | 
    CPPUNIT_ASSERT_DOUBLES_EQUAL(7.5, dp1.evaluate(1.0), 0.000001); | 
| 28 | 
  | 
  | 
    CPPUNIT_ASSERT_DOUBLES_EQUAL(32.0, dp1.evaluate(2.0), 0.000001); | 
| 29 | 
  | 
  | 
    CPPUNIT_ASSERT_DOUBLES_EQUAL(-2.5, dp1.evaluate(-1.0), 0.000001); | 
| 30 | 
  | 
  | 
     | 
| 31 | 
  | 
  | 
    CPPUNIT_ASSERT_DOUBLES_EQUAL(14.0, dp1.evaluateDerivative(1.0), 0.000001); | 
| 32 | 
  | 
  | 
 | 
| 33 | 
  | 
  | 
 | 
| 34 | 
  | 
  | 
    //dp2 = x^2 + 3x +2 | 
| 35 | 
  | 
  | 
    DoublePolynomial dp2; | 
| 36 | 
  | 
  | 
    dp2.setCoefficient(0, 2.0); | 
| 37 | 
  | 
  | 
    dp2.setCoefficient(1, 3.0); | 
| 38 | 
  | 
  | 
    dp2.setCoefficient(2, 1.0); | 
| 39 | 
  | 
  | 
 | 
| 40 | 
  | 
  | 
    //dp3 = x^3 + 2x | 
| 41 | 
  | 
  | 
    DoublePolynomial dp3; | 
| 42 | 
  | 
  | 
    dp3.setCoefficient(1, 2.0); | 
| 43 | 
  | 
  | 
    dp3.setCoefficient(3, 1.0); | 
| 44 | 
  | 
  | 
 | 
| 45 | 
  | 
  | 
    //dp4 = x^3 + x^2 +5x + 2 | 
| 46 | 
  | 
  | 
    DoublePolynomial dp4 = dp2 + dp3; | 
| 47 | 
  | 
  | 
    CPPUNIT_ASSERT(dp4.size() == 4); | 
| 48 | 
  | 
  | 
    CPPUNIT_ASSERT_DOUBLES_EQUAL(dp4.getCoefficient(0), 2.0, 0.000001); | 
| 49 | 
  | 
  | 
    CPPUNIT_ASSERT_DOUBLES_EQUAL(dp4.getCoefficient(1), 5.0, 0.000001); | 
| 50 | 
  | 
  | 
    CPPUNIT_ASSERT_DOUBLES_EQUAL(dp4.getCoefficient(2), 1.0, 0.000001); | 
| 51 | 
  | 
  | 
    CPPUNIT_ASSERT_DOUBLES_EQUAL(dp4.getCoefficient(3), 1.0, 0.000001); | 
| 52 | 
  | 
  | 
     | 
| 53 | 
  | 
  | 
 | 
| 54 | 
  | 
  | 
     | 
| 55 | 
  | 
  | 
    //dp5 = -x^3 + x^2 +x + 2 | 
| 56 | 
  | 
  | 
    DoublePolynomial dp5 = dp2 - dp3; | 
| 57 | 
  | 
  | 
    CPPUNIT_ASSERT(dp5.size() == 4);     | 
| 58 | 
  | 
  | 
    CPPUNIT_ASSERT_DOUBLES_EQUAL(dp5.getCoefficient(0), 2.0, 0.000001); | 
| 59 | 
  | 
  | 
    CPPUNIT_ASSERT_DOUBLES_EQUAL(dp5.getCoefficient(1), 1.0, 0.000001); | 
| 60 | 
  | 
  | 
    CPPUNIT_ASSERT_DOUBLES_EQUAL(dp5.getCoefficient(2), 1.0, 0.000001); | 
| 61 | 
  | 
  | 
    CPPUNIT_ASSERT_DOUBLES_EQUAL(dp5.getCoefficient(3), -1.0, 0.000001); | 
| 62 | 
  | 
  | 
 | 
| 63 | 
  | 
  | 
 | 
| 64 | 
  | 
  | 
    //dp6 = x^5 + 3x^4 + 4x^3 + 6x^2 + 4x | 
| 65 | 
  | 
  | 
    DoublePolynomial dp6 = dp2 * dp3; | 
| 66 | 
  | 
  | 
    CPPUNIT_ASSERT(dp6.size() == 5); | 
| 67 | 
  | 
  | 
    CPPUNIT_ASSERT_DOUBLES_EQUAL(dp6.getCoefficient(1), 4, 0.000001); | 
| 68 | 
  | 
  | 
    CPPUNIT_ASSERT_DOUBLES_EQUAL(dp6.getCoefficient(2), 6, 0.000001); | 
| 69 | 
  | 
  | 
    CPPUNIT_ASSERT_DOUBLES_EQUAL(dp6.getCoefficient(3), 4, 0.000001); | 
| 70 | 
  | 
  | 
    CPPUNIT_ASSERT_DOUBLES_EQUAL(dp6.getCoefficient(4), 3, 0.000001); | 
| 71 | 
  | 
  | 
    CPPUNIT_ASSERT_DOUBLES_EQUAL(dp6.getCoefficient(5), 1, 0.000001); | 
| 72 | 
  | 
  | 
     | 
| 73 | 
  | 
  | 
} | 
| 74 | 
  | 
  | 
 |