Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org sed Question
 Linux - Newbie This Linux forum is for members that are new to Linux. Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

 09-25-2009, 03:09 AM #1 unihiekka Member   Registered: Aug 2005 Distribution: SuSE Linux / Scientific Linux / [K|X]ubuntu Posts: 273 Rep: sed Question Hi! I have just generated a very large (header) file for C/C++, containing the definition of a rather lengthy mathematical function, with Mathematica. However, since the math function is to be evaluated many times, I want to optimize it. Currently, I have it as a switch-case, but in each case there are often the same basic evaluations several times, e.g. Code: ```... case intnum: z = A*pow(x,2)*(B*pow(y,3.8)*pow(x,2)-C*pow(y,3.8)*pow(x,6)); break; ...``` where z is the return value (usually double, but it's templated to type Dbl, as the function supports multiple precision), and x,y, A B C are doubles as well. Since pow(x,2) and pow(y,3.8) are in the evaluation more than once, I would like to replace it with something like: Code: ```... case intnum: Dbl X2 = pow(x,2); Dbl Y38 = pow(y,3.8); z = A*X2*(B*Y38*pow(x,2)-C*Y38*pow(x,6)); break; ...``` Is it possible to use sed for this and what would a script look like? I need to loop through all instances, and the check has to be done within each case-break block, and if (and only if) there is an expression of the form pow(xy,exp), with xy either x or y and exp a numerical value, it should replace it with some new definition, and add the corresponding line right after the case statement (or just before z = ...). I'm quite sure sed can do this, but this is way beyond me... I don't really care about the name, but if being specific helps, then something like xbdpPadp or ybdpPadp would be nice, where bdp is the part before the decimal point and adp is the part after the decimal point. So, above it would be x2d or x2d0, and y3d8. ----- Yes, I know pow(x,6) = pow(X2,3), but it's only evaluated once in my example, so it's not really a big deal in my computation. Last edited by unihiekka; 09-25-2009 at 03:27 AM.
09-25-2009, 03:23 AM   #2
lutusp
Member

Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep:
Quote:
 Originally Posted by unihiekka Hi! I have just generated a very large (header) file for C/C++, containing the definition of a rather lengthy mathematical function, with Mathematica. However, since the math function is to be evaluated many times, I want to optimize it. Currently, I have it as a switch-case, but in each case there are often the same basic evaluations several times, e.g. Code: ```... case intnum: z = A*pow(x,2)*(B*pow(y,3.8)*pow(x,2)-C*pow(y,3.8)*pow(x,6)); break; ...``` where z is the return value (usually double, but it's templated to type Dbl, as the function supports multiple precision), and x,y, A B C are doubles as well. Since pow(x,2) and pow(y,3.8) are in the evaluation more than once, I would like to replace it with something like: Code: ```... case intnum: Dbl X2 = pow(x,2); Dbl Y38 = pow(y,3.8); z = A*X2*(B*Y38*pow(x,2)-C*Y38*pow(x,6)); break; ...``` Is it possible to use sed for this and what would a script look like? I need to loop through all instances, and the check has to be done within each case-break block, and if there is a pow(xy,exp), with xy either x or y and exp a value of type Dbl, it should replace it with some new definition (I don't really care about its name), and add the corresponding line right after the case statement (or just before z = ...). I'm quite sure sed can do this, but this is way beyond me... ----- Yes, I know pow(x,6) = pow(X2,3), but it's only evaluated once in my example, so it's not really a big deal in my computation.
I do quite a lot of this kind of thing (optimizing expressions derived from Mathematica and other computer algebra systems) and I must say I have not found any alternatives to hand-tuning, e.g. locating and manually separating common subexpressions.

I don't think this sort of thing can be meaningfully automated, at least not with present methods and tools. And I'm not saying this lightly -- I've given this quite a lot of thought, and the outcome matters to me as well.

 09-25-2009, 05:43 AM #3 unihiekka Member   Registered: Aug 2005 Distribution: SuSE Linux / Scientific Linux / [K|X]ubuntu Posts: 273 Original Poster Rep: common subexpression elimination It's good to know that I'm not alone with my problem. Just a question, though, is my request somewhat irrelevant since 'most' compilers do common subexpression replacement automatically if you turn the optimization options on? See e.g. here Still, I would be interested to know if there are sed/perl/bash/... gurus out there who would have a brilliant idea.
 09-25-2009, 12:08 PM #4 unihiekka Member   Registered: Aug 2005 Distribution: SuSE Linux / Scientific Linux / [K|X]ubuntu Posts: 273 Original Poster Rep: For mathematica, this seems a nice way to get the job done.

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post wendtbd Red Hat 4 08-23-2006 11:22 AM G00fy Programming 2 03-20-2006 12:34 AM dolvmin Linux - Software 4 03-09-2004 09:27 AM freelinuxcpp Linux - Software 3 01-20-2004 07:36 AM 360 Programming 2 03-15-2002 09:52 AM

All times are GMT -5. The time now is 12:03 AM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -