Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.