# How to speed up an optimization with very long symbolic expressions?

I have 8 very long symbolic expressions (each having around 200,000 summands) - Simplify will run forever, so that is unfortunately not an option. Each of those summands is in principle pretty simple, so only fractions of the six variables with different exponents are involved - nothing trigonometric, no roots etc. My goal is to determine values for those six variables by e.g. calling NMinimize. But simply evaluating one of the expressions takes roughly 3-5 minutes, so that would be all but time-efficient.

Can you give me some general advice on how to approach that kind of problem with respect to be time-efficient? I do have 128GB of memory, so that should not be an issue.

I was thinking about using Compile to maybe achieve some speed up. For this, I will probably be going along this direction. If Compile is advisable, then how can I compile expressions like this:

tmp = a1 + a2


where a1 and a2 are my variables. The problem is that tmp is not a function, it is just a variable and cannot be compiled (easily?). I could rerun lots of computation and turn tmp into a function that can easily be compiled, but I would prefer a way without doing the latter.

## Update

Please find the first five summands of one term here at pastebin. If you want a longer expression, you can download a compressed (.7z, 9MB) .m file here or alternatively a zip archive here which only contains the relevant terms itself (as a list), so you will need to assign the Import to some variable and apply Total to the imported file (importing takes around 3.5min for me). Consider for example those values for the fixed (for each optimization) parameters:

Δ = 2*π*(-35/100);
δ = 2*π*(45/1000);
tg = 30;


a11,a21,a31,a12,a22,a32 are my function variables.

• Could you post an example expression (in a pastebin, as it won't fit here)? It would make it easier for people to experiment. – Szabolcs Jul 16 '15 at 12:18
• A 600 MB file is a little ridiculous. You could 7zip that down to 8 MB. I'd also recommend losing the Greek letters to save space. – wxffles Jul 17 '15 at 2:39
• @wxffles Thanks for mentioning using archives - I completely ignored that. Onces Dropbox works again I will exchange the link to a compressed file. Could you (or someone else) briefly explain why avoiding Greek letters saves space? Or do you just mean filesize-wise for the exported .m's due to fewer String characters? – Lukas Jul 17 '15 at 7:58

I can't open the 9MB file as it is a .7z extension and I don't know what that is.

However, if your expressions has sets of Denominators that are the same then the following will spread the Simplify over your kernels by ParallelMaping it.

tmp is the first 5 summands you shared in PasteBin

Simplify@Total@ParallelMap[Simplify[Total[#]] &, GatherBy[List @@ tmp, Denominator]]

(*
((a11^3 + 3 a11 a21^2 + a21^3 +
3 a11^2 (a21 + a31)) \[Pi]^3 (-2 tg \[CapitalDelta] +
Sin[2 tg \[CapitalDelta]]))/(32 (a11 + a21 +
a31)^3 tg^3 \[CapitalDelta]^3)
*)


To get a list of the summands List is Apply'ed to tmp to replace the Plus head with List. The list of summands is then GatherBy the Denominators. This gives a list with each entry a list of the summands that share the same denominator.

Next ParallelMap maps Simplify[Total[#]]& on to each list of summands with common denominators. Total adds them up into on expression. Simplify will speed through each of these expressions since there is only work to do in the numerator.

Now there is a list with one summand for each common denominator. One final round of Simplify@Total@ is applied. The Total to the summands in one expression and the Simplify to realise any final common denominators in the expression.

Hope this helps.

• Thanks for your answer. .7z is the file format of 7zip. I also attached a .zip file now which should be more common to people. I will give it a try once my computation here has finished (amount of kernels limited by license) and will report back about performance. I am confident that it will help, since that GatherBy trick is pretty neat! – Lukas Jul 17 '15 at 10:38
• Simplification was done over the weekend and it was indeed able to reduce the length of each expression to ~25.000 summands, so almost decreased by an order of magnitude. So your answer definitely helped alot! – Lukas Jul 20 '15 at 5:19