File indexing completed on 2024-04-06 12:24:22
0001 #include <cppunit/extensions/HelperMacros.h>
0002 #include "PhysicsTools/Utilities/interface/Operations.h"
0003 #include "PhysicsTools/Utilities/interface/NthDerivative.h"
0004 #include "PhysicsTools/Utilities/interface/FunctionsIO.h"
0005 #include "PhysicsTools/Utilities/interface/Variables.h"
0006 #include "PhysicsTools/Utilities/interface/Fraction.h"
0007 #include "PhysicsTools/Utilities/interface/Simplify.h"
0008 #include <sstream>
0009 #include <iostream>
0010 class testDerivatives : public CppUnit::TestFixture {
0011 CPPUNIT_TEST_SUITE(testDerivatives);
0012 CPPUNIT_TEST(checkAll);
0013 CPPUNIT_TEST_SUITE_END();
0014
0015 public:
0016 void setUp() {}
0017 void tearDown() {}
0018 void checkAll();
0019 };
0020
0021 CPPUNIT_TEST_SUITE_REGISTRATION(testDerivatives);
0022
0023 #define CHECK(EXPR, STRING) \
0024 { \
0025 std::ostringstream str; \
0026 str << EXPR; \
0027 std::cerr << #EXPR << " = " << str.str() << std::endl; \
0028 CPPUNIT_ASSERT(str.str() == STRING); \
0029 } \
0030 \
0031 struct __useless_igonreme
0032
0033 void testDerivatives::checkAll() {
0034 using namespace funct;
0035 X x;
0036 Y y;
0037 Z z;
0038 x = 1, y = 2, z = 3;
0039 funct::Numerical<1> _1;
0040 funct::Numerical<2> _2;
0041 funct::Numerical<3> _3;
0042
0043 CHECK(derivative<X>(_1), "0");
0044 CHECK(derivative<X>(x), "1");
0045 CHECK(derivative<Y>(x), "0");
0046 CHECK(derivative<X>(x ^ _2), "2 x");
0047 CHECK(derivative<X>(x ^ _3), "3 x^2");
0048 CHECK(derivative<X>(exp(x)), "exp(x)");
0049 CHECK(derivative<X>(log(x)), "1/x");
0050 CHECK(derivative<X>(sin(x)), "cos(x)");
0051 CHECK(derivative<X>(cos(x)), "-sin(x)");
0052
0053 CHECK((nth_derivative<2, X>(sin(x))), "-sin(x)");
0054 CHECK((nth_derivative<3, X>(sin(x))), "-cos(x)");
0055 CHECK((nth_derivative<4, X>(sin(x))), "sin(x)");
0056
0057 CHECK(derivative<X>(sin(x) * cos(x)), "cos(x)^2 - sin(x)^2");
0058 CHECK((nth_derivative<2, X>(sin(x) * cos(x))), "-4 sin(x) cos(x)");
0059 CHECK((nth_derivative<3, X>(sin(x) * cos(x))), "-4 ( cos(x)^2 - sin(x)^2 )");
0060 CHECK((nth_derivative<4, X>(sin(x) * cos(x))), "16 sin(x) cos(x)");
0061 }