Collecting disjoint terms in polynomial expressions

This is my first post here, so thanks in advance and bear with me if I don't follow the format guide.

My problem is this: I have multiple polynomial expressions in the variable "s" up to a power of 10. Each of the coefficients is then a function of up to 10 other variables. In total, the code for all coefficient functions is about 800 lines of code with single coefficients 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 can see that I spend 78% of my time on this feature. For optimization, I would like to find all the code and find redundant computations, compute them at the beginning of the procedure, and replace all their occurrences with the previously computed expression. The problem is that the most common expressions can be something like this:

a0 = ... + R1*R2*G1*R3 + R1*R2*H1*R3 + ...;

I would like to find a way to search the lines and sort the terms R1*R2*R3

to replace them with something like X

where X = R1*R2*R3;

declared at the beginning of the code. These regexes can happen several hundred times throughout the code, so I'm sure this can improve my runtime significantly. Also, I can group things separated by multiplication rather than addition.

Basically, I need a placeholder function that can find disjoint strings whose members are separated by other members and characters *

, but not +

. It could be high order, or incredibly simple, I'm really not sure.

I have Mathematica, MATLAB and Maple for me and running Debian, so I can download something if it's open source, which might be more helpful. I usually use Emacs for my programming, although I am by no means familiar with all of its features. I am open to any suggestions and really appreciate your help.

+3


source to share


1 answer


Using standard kernel-utils, you can do the following:

cat filename.cc | tr " +" "\n\n" | grep "*" | sort | uniq -c

      



In plain English, this means: read the file, convert all spaces and pluses to new lines. Then only keep strings containing multiplication, sort them, and show unique occurrences with frequency.

+1


source







All Articles