Optimized code for complex function with Symbolics.jl

Hello,

I need to write up an optimized code for a nasty analytical expression, the baby version is

\begin{aligned} f_L = & \sum_{J} \frac{-\Gamma_J}{\pi^2} \frac{\text{Im}(z_J) \text{Im}(z_L)}{|x - z_J|^2|x - z_L|^2} + \sum_{K \neq L} \frac{\Gamma_K}{2 \pi^2} \text{Re}\left(\frac{1}{x - z_L} \frac{z_K - \overline{z_K}}{(\overline{z_L} - z_K)(\overline{z_L} - \overline{z_K})} \right) + \\ & \sum_{J}\sum_{K \neq J} \frac{\Gamma_J \delta_{K,L}}{2 \pi^2} \text{Re} \left(\frac{1}{x - z_J} \frac{z_K - \overline{z_K}}{(\overline{z_J} - z_K)(\overline{z_J} - \overline{z_K})} \right), \end{aligned}
where \delta_{K,L} denotes the Kronecker delta of K and L, \text{Re}(z), \text{Im}(z) denotes the real, imaginary part of z, respectively. x is a real number, \{\Gamma_1, \ldots, \Gamma_N\} are real numbers, and \{z_1, \ldots, z_N\} are complex numbers. The number of particles N is about 50.

Can Symbolics.jl be useful to develop an optimized serial code for this kind of function?

Yeah, that looks like a case for “expand and simplify”, given that there are things to simplify.

1 Like

My machine has been trying to simplify just the first two terms for over an hour, so while I agree that Symbolics.jl can in principle “expand and simplify”, it’s not currently useful for this kind of function.

2 Likes

Thanks a lot for looking at this problem. Let me know how long that took to simplify the expression.

Did you use the e-graph form?

I haven’t found anything about e-graphs in the Symbolics.jl docs, nor have I been able to find any external tutorials. Could you point me in the right direction?

It’s in GitHub - JuliaSymbolics/Metatheory.jl: General purpose algebraic metaprogramming and symbolic computation library for the Julia programming language: E-Graphs & equality saturation, term rewriting and more.

Share your MWE. Maybe @0x0f0f0f can help. We do need better docs here, but are in the middle of a transition to make this a bit easier and so we might as well make this a docs example. E-Graphs will have much better asymptotic scaling properties so it would be a nice test example.

1 Like

@0x0f0f0f maybe use this to test [WIP] EGraphs optimization by 0x0f0f0f · Pull Request #389 · JuliaSymbolics/SymbolicUtils.jl · GitHub ?

It’s on the way. Just pushed the last commit that should make it work. It’s not yet ready for Symbolics.jl but it should be very soon. This looks like a nice real world application for Metatheory.jl !

1 Like

@mleprovost merged into SymbolicUtils.jl, will merge into Symbolics.jl soon

1 Like