Reputation: 73
This is my first post on here so thanks in advance and bear with me if I do not follow the format guide.
My problem is as follows: I have several polynomial expressions in the variable "s" up to degree 10. Each of the coefficients are then functions of up to 10 other variables. Overall the code for all of the coefficient functions takes up about 800 text-wrapped lines of code with single coefficients having up to 40 lines of code. I am writing an optimization routine in C++ that tries to determine the optimal values for each of the 10 variables that the coefficients depend on.
Profiling my code, I see that I am spending 78% of my time in this one function. To optimize, I would like to search the whole code and find redundant calculations, compute them at the start of the routine and replace all of their occurrences with the previously calculated expression. The problem is that the most frequently occurring expressions may be something like:
a0 = ... + R1*R2*G1*R3 + R1*R2*H1*R3 + ...;
I would like to find a way to search through the lines and sort out the R1*R2*R3
terms to replace them with something like X
where X = R1*R2*R3;
is declared at the start of the code. These regular expressions may occur several hundred times throughout the code, so I am sure that this can drastically improve my run time. Additionally, I can only group things separated by multiplication, not addition.
Basically, I need a replace string function that can find disjoint strings whose member terms are separated by other terms and *
signs but not +
signs. This may be a tall order, or incredibly simple, I am really not sure.
I have Mathematica, MATLAB, and Maple available to me and run Debian, so I can download something if it is open source that may be more helpful. I typically use Emacs for my programming, though I am by no means proficient with all of its functionality. I am open to any suggestions, and greatly appreciate your help.
Upvotes: 3
Views: 215
Reputation: 11547
Using the standard core-utils, you can do the following:
cat filename.cc | tr " +" "\n\n" | grep "*" | sort | uniq -c
In plain English this translates to: read the file, convert all spaces and pluses into new-lines. Next, only keep the lines containing multiplication, sort them, and display unique occurrences with they frequency.
Upvotes: 1