LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 09-25-2009, 04:09 AM   #1
unihiekka
Member
 
Registered: Aug 2005
Distribution: SuSE Linux / Scientific Linux / [K|X]ubuntu
Posts: 273

Rep: Reputation: 32
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 04:27 AM.
 
Old 09-25-2009, 04:23 AM   #2
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by unihiekka View Post
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.
 
Old 09-25-2009, 06:43 AM   #3
unihiekka
Member
 
Registered: Aug 2005
Distribution: SuSE Linux / Scientific Linux / [K|X]ubuntu
Posts: 273

Original Poster
Rep: Reputation: 32
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.
 
Old 09-25-2009, 01:08 PM   #4
unihiekka
Member
 
Registered: Aug 2005
Distribution: SuSE Linux / Scientific Linux / [K|X]ubuntu
Posts: 273

Original Poster
Rep: Reputation: 32
For mathematica, this seems a nice way to get the job done.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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



Similar Threads
Thread Thread Starter Forum Replies Last Post
SED and MV question wendtbd Red Hat 4 08-23-2006 12:22 PM
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 01:34 AM
sed question dolvmin Linux - Software 4 03-09-2004 10:27 AM
little sed question freelinuxcpp Linux - Software 3 01-20-2004 08:36 AM
sed question 360 Programming 2 03-15-2002 10:52 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration