C++ code generation: any way of deflating large expressions?
I wonder if someone has run into the following and has found a easy way out: In generating C-source code from symbolic expressions it is always possible to run into trouble with large size expression output. For example, if one stores in an ex variable something like: ex SUM; `` SUM = \sum_{i=1}^{10000} x[i] ; '' then printing this out as csrc will generate a huge line. The best way is to see if there is an easy algorithmic construct (perhaps within GiNaC, or hints provided for its efficient implementation) so that at each level of the tree all operation-containing nodes are assigned a dummy variable (e.g. those of arithmetic operations or implementing a function). Then upon exhaustion, to reverse the procedure from the leaves of the tree to the root and thus generate a C++ code fragment with all necessary steps & intermediate variables to finally get the value of the root. ----------------------------------------------------------------- Dr. Vassilis S. Vassiliadis, Lecturer, UNIVERSITY OF CAMBRIDGE, DEPARTMENT OF CHEMICAL ENGINEERING Pembroke Street, Cambridge CB2 3RA, UK. Tel: (reception): +44 1223 334777 Fax: (departmental): +44 1223 334796 e-mail: vsv20@cheng.cam.ac.uk -----------------------------------------------------------------
Hello, I also ran into the problem of generating source code. I wrote a C++ class to do it. It is attached. The following code fragment hopefully makes more or less clear how to use it exoutclass p; p.getfunction(symbol("matelsq"),matelsq); p.setfunction(eta,eta); p.setfunction(z,z); p.setfunction(xi,xi); p.setfunction(t,t); p.setfunction(s,s); ofstream hfile("regmsq.h"); hfile << "#include \"msq.h\"\n"; p.writeouth("regmsq","double",print_csrc_double(hfile),"msq"); hfile << "extern double mmu;" << endl; hfile << "extern double MW;" << endl; hfile.close(); ofstream Cfile("regmsq.C"); p.writeoutC("regmsq","double",print_csrc_double(Cfile)); Cfile << "double mmu=0;" << endl; Cfile << "double MW=" << MWval << ";" << endl; Cfile.close(); The idea is that this creates a C++ class with methods sets(), sett(), setxi(), setz() and seteta() and a method getmatelsq() that lets you get the result. The set methods should be called in opposite order as they are created with the setfunction methods. If you want to change, say, the value of xi, the methods setxi(), setz() and seteta() should be called in that order before calling getmatelsq(). In the setfunction the first argument should be a symbol of which the name occurs in the C++ output as a variable name, the second argument is the expression that gets a value by calling the appropriate setvariable() method. Bye, Chris Dams
participants (2)
-
Chris Dams
-
Dr. Vassilis S. Vassiliadis