1 // Simple example file for the the fparser optimization bug 2 // (c) 2014 by Daniel Schwen 3 // ======================================================== 4 5 // $CXX -o optimizer_bug optimizer_bug.cc -I ../../../installed/include/libmesh/ ../../../build/contrib/fparser/.libs/libfparser_la-fp* 6 7 #include "../fparser.hh" 8 9 #include <iostream> 10 #include <fstream> 11 #include <string> 12 main()13int main() 14 { 15 std::ofstream myfile; 16 double p[3] = {0.0, 2.0, 0.1}; 17 const double step = 0.01; 18 FunctionParser fparser; 19 20 // The White Whale 21 std::string func = "c*2*(1-c)^2 - c^2*(1-c)*2"; 22 23 // Parse the input expression into bytecode 24 fparser.Parse(func, "c"); 25 #ifdef FUNCTIONPARSER_SUPPORT_DEBUGGING 26 std::cout << "Input Expression:\n" << func << std::endl; 27 fparser.PrintByteCode(std::cout); 28 std::cout << std::endl; 29 #endif 30 31 // eval into file 32 myfile.open ("pre.dat"); 33 for (p[0]=0.0; p[0]<=1.0; p[0]+=0.01) myfile << p[0] << ' ' << fparser.Eval(p) << std::endl; 34 myfile.close(); 35 36 FunctionParser fparser2(fparser); 37 38 // output optimized version 39 fparser.Optimize(); 40 #ifdef FUNCTIONPARSER_SUPPORT_DEBUGGING 41 std::cout << "Optimized Input Expression:\n" << func << std::endl; 42 fparser.PrintByteCode(std::cout); 43 std::cout << std::endl; 44 #endif 45 46 // eval into file 47 myfile.open ("post.dat"); 48 for (p[0]=0.0; p[0]<=1.0; p[0]+=0.01) myfile << p[0] << ' ' << fparser.Eval(p) << std::endl; 49 myfile.close(); 50 51 // compute difference 52 double diff = 0.0; 53 for (p[0]=0.0; p[0]<=1.0; p[0]+=step) diff+=step*(fparser.Eval(p)-fparser2.Eval(p)); 54 55 std::cout << "Integral of f_optimized()-f_original() on [0,1] = " << diff << " (should be ~0.0)" << std::endl; 56 return 0; 57 } 58