Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:24:23

0001 #include <cppunit/extensions/HelperMacros.h>
0002 #include "PhysicsTools/Utilities/interface/Operations.h"
0003 #include "PhysicsTools/Utilities/interface/FunctionsIO.h"
0004 #include "PhysicsTools/Utilities/interface/Variables.h"
0005 #include "PhysicsTools/Utilities/interface/Fraction.h"
0006 #include "PhysicsTools/Utilities/interface/Simplify.h"
0007 #include "PhysicsTools/Utilities/interface/Expression.h"
0008 #include <sstream>
0009 #include <iostream>
0010 class testFunctionsIO : public CppUnit::TestFixture {
0011   CPPUNIT_TEST_SUITE(testFunctionsIO);
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(testFunctionsIO);
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 testFunctionsIO::checkAll() {
0034   using namespace funct;
0035   X x;
0036   Y y;
0037   Z z;
0038   x = 1, y = 2, z = 3;
0039   funct::Numerical<0> _0;
0040   funct::Numerical<1> _1;
0041   funct::Numerical<2> _2;
0042   funct::Numerical<3> _3;
0043   funct::Numerical<-1> _m1;
0044   funct::Numerical<-2> _m2;
0045   funct::Numerical<-3> _m3;
0046   CHECK(x, "x");
0047   CHECK(-x, "-x");
0048 
0049   CHECK(sqrt(x), "sqrt(x)");
0050   CHECK(exp(x), "exp(x)");
0051   CHECK(log(x), "log(x)");
0052   CHECK(sin(x), "sin(x)");
0053   CHECK(cos(x), "cos(x)");
0054   CHECK(abs(x), "abs(x)");
0055   CHECK(sgn(x), "sgn(x)");
0056 
0057   CHECK(x + y, "x + y");
0058   CHECK(x - y, "x - y");
0059   CHECK(x * y, "x y");
0060   CHECK((x ^ y), "x^y");
0061   CHECK(x / y, "x/y");
0062 
0063   CHECK(_1, "1");
0064   CHECK(_2, "2");
0065   CHECK(_3, "3");
0066 
0067   CHECK(_2 + _3, "5");
0068   CHECK(_2 - _3, "-1");
0069   CHECK(_2 * _3, "6");
0070   CHECK(_2 / _3, "2/3");
0071   CHECK((_2 ^ _3), "8");
0072   CHECK((_2 ^ _m3), "1/8");
0073   CHECK((_m2 ^ _3), "-8");
0074   CHECK((_m2 ^ _m3), "-1/8");
0075 
0076   CHECK((fract<1, 2>()), "1/2");
0077   CHECK((fract<4, 2>()), "2");
0078   CHECK((fract<1, -2>()), "-1/2");
0079 
0080   CHECK(-x - y, "- x - y");
0081   CHECK(x + _1, "x + 1");
0082   CHECK(x - _1, "x - 1");
0083   CHECK(-x + _1, "- x + 1");
0084   CHECK(-x - _1, "- x - 1");
0085 
0086   CHECK(-(-x), "x");
0087   CHECK(-(x + y), "- x - y");
0088 
0089   // simplifications
0090   CHECK(x + x, "2 x");
0091   CHECK(_1 + x, "x + 1");
0092   CHECK(x + _0, "x");
0093   CHECK(_0 + x, "x");
0094   CHECK(_0 - x, "-x");
0095   CHECK(x - (-y), "x + y");
0096   CHECK(_3 * x + _2 * x, "5 x");
0097 
0098   CHECK(_0 * x, "0");
0099   CHECK(_1 * x, "x");
0100   CHECK(x * _2, "2 x");
0101   CHECK((_1 * fract<3, 4>()), "3/4");
0102   CHECK(_m1 * x, "-x");
0103   CHECK(x * (-y), "-x y");
0104   CHECK(_1 * (-x), "-x");
0105   CHECK(x * (y / z), "( x y )/z");
0106   CHECK((-x) * (-y), "x y");
0107   CHECK((x * y) * (-z), "-x y z");
0108   CHECK((-x) * y, "-x y");
0109   CHECK(_2 * (x / y), "( 2 x )/y");
0110   CHECK(x * _2, "2 x");
0111   CHECK((x ^ y) * (x ^ z), "x^( y + z )");
0112 
0113   CHECK(_0 / x, "0");
0114   CHECK(x / _1, "x");
0115   CHECK(x / _m1, "-x");
0116   CHECK(x / _m2, "-x/2");
0117   CHECK((-x) / y, "-x/y");
0118   CHECK(x / y / z, "x/( y z )");
0119   CHECK((_3 * x) / (_2 * y), "3/2 ( x/y )");
0120 
0121   CHECK((x ^ _1), "x");
0122   CHECK((x ^ _0), "1");
0123   CHECK((x ^ _m1), "1/x");
0124   CHECK((x ^ _m2), "1/x^2");
0125   CHECK((x ^ fract<1, 2>()), "sqrt(x)");
0126   CHECK(((x ^ y) ^ z), "x^( y + z )");
0127 
0128   CHECK(log(exp(x)), "x");
0129   CHECK(exp(log(x)), "x");
0130   CHECK((log(x ^ y)), "y log(x)");
0131   CHECK(exp(x) * exp(y), "exp(x + y)");
0132   CHECK(log(x * y), "log(x) + log(y)");
0133   CHECK(log(x / y), "log(x) - log(y)");
0134 
0135   CHECK(sin(-x), "-sin(x)");
0136   CHECK(cos(-x), "cos(x)");
0137   CHECK(sin(x) / cos(x), "tan(x)");
0138   CHECK(cos(x) * tan(x), "sin(x)");
0139   CHECK(sin(x) / tan(x), "cos(x)");
0140   CHECK((sin(x) ^ _2) + (cos(x) ^ _2), "1");
0141 
0142   CHECK(x * y + x * z, "x ( y + z )");
0143 
0144   Expression expr = sin(x) * cos(x);
0145   CHECK(expr, "sin(x) cos(x)");
0146 }