# Optimized code for complex function with Symbolics.jl

**URL:** <https://discourse.julialang.org/t/optimized-code-for-complex-function-with-symbolics-jl/70360>\
**Category:** General Usage\
**Tags:** question, symbolics\
**Created:** [October 25, 2021, 11:57pm UTC](https://discourse.julialang.org/t/optimized-code-for-complex-function-with-symbolics-jl/70360 "2021-10-25T23:57:44Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![mleprovost](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mleprovost/32/7166_2.png) [@mleprovost](https://discourse.julialang.org/u/mleprovost)\
**Post date:** [October 25, 2021, 11:57pm UTC](https://discourse.julialang.org/t/optimized-code-for-complex-function-with-symbolics-jl/70360/1 "2021-10-25T23:57:44Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [October 26, 2021, 4:36pm UTC](https://discourse.julialang.org/t/optimized-code-for-complex-function-with-symbolics-jl/70360/2 "2021-10-26T16:36:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [October 26, 2021, 9:08pm UTC](https://discourse.julialang.org/t/optimized-code-for-complex-function-with-symbolics-jl/70360/3 "2021-10-26T21:08:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mleprovost](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mleprovost/32/7166_2.png) [@mleprovost](https://discourse.julialang.org/u/mleprovost)\
**Post date:** [October 26, 2021, 9:44pm UTC](https://discourse.julialang.org/t/optimized-code-for-complex-function-with-symbolics-jl/70360/4 "2021-10-26T21:44:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [October 27, 2021, 2:03am UTC](https://discourse.julialang.org/t/optimized-code-for-complex-function-with-symbolics-jl/70360/5 "2021-10-27T02:03:56Z")

</div>

Did you use the e-graph form?

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [October 27, 2021, 3:45am UTC](https://discourse.julialang.org/t/optimized-code-for-complex-function-with-symbolics-jl/70360/6 "2021-10-27T03:45:58Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Gianluca\_Salogni](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gianluca_salogni/32/20448_2.png) [@Gianluca\_Salogni](https://discourse.julialang.org/u/Gianluca_Salogni)\
**Post date:** [October 27, 2021, 8:14am UTC](https://discourse.julialang.org/t/optimized-code-for-complex-function-with-symbolics-jl/70360/7 "2021-10-27T08:14:53Z")

</div>

It’s in [https://github.com/JuliaSymbolics/Metatheory.jl](https://github.com/JuliaSymbolics/Metatheory.jl)

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [October 27, 2021, 10:49am UTC](https://discourse.julialang.org/t/optimized-code-for-complex-function-with-symbolics-jl/70360/8 "2021-10-27T10:49:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [October 27, 2021, 11:11am UTC](https://discourse.julialang.org/t/optimized-code-for-complex-function-with-symbolics-jl/70360/9 "2021-10-27T11:11:19Z")

</div>

@0x0f0f0f maybe use this to test [https://github.com/JuliaSymbolics/SymbolicUtils.jl/pull/389](https://github.com/JuliaSymbolics/SymbolicUtils.jl/pull/389) ?

---

<div class="post-metadata">

**Author:** ![0x0f0f0f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/0x0f0f0f/32/20806_2.png) [@0x0f0f0f](https://discourse.julialang.org/u/0x0f0f0f)\
**Post date:** [October 27, 2021, 6:58pm UTC](https://discourse.julialang.org/t/optimized-code-for-complex-function-with-symbolics-jl/70360/10 "2021-10-27T18:58:16Z")

</div>

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 !

---

<div class="post-metadata">

**Author:** ![0x0f0f0f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/0x0f0f0f/32/20806_2.png) [@0x0f0f0f](https://discourse.julialang.org/u/0x0f0f0f)\
**Post date:** [October 30, 2021, 6:33pm UTC](https://discourse.julialang.org/t/optimized-code-for-complex-function-with-symbolics-jl/70360/11 "2021-10-30T18:33:23Z")

</div>

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