4
$\begingroup$

I am trying to calculate the imaginary part of a long expression. It's a long enough expression that Mathematica "hangs" when you run:

imFUN2 = ComplexExpand[Im[expression]];

Is there something I can do that can help speed things up?

Here is my full code:

expression = -((I Ωc (4 γa^4 + 16 Δd^4 - 48 Δd^3 Δp + 48 Δd^2 Δp^2 - 16 Δd Δp^3 + 4 I γa^3 
  (3 Δc + 6 Δd - 4 Δp - Δs) - 16 Δd^3 Δs + 32 Δd^2 Δp Δs - 16 Δd Δp^2 Δs - 4 Δd^2 Ωc^2 + 
  4 Δd Δp Ωc^2 + 4 Δd Δs Ωc^2 - 4 I dephasing Δd Ωd^2 + 12 Δd^2 Ωd^2 + 
  4 I dephasing Δp Ωd^2 - 24 Δd Δp Ωd^2 + 12 Δp^2 Ωd^2 - 8 Δd Δs Ωd^2 + 
  8 Δp Δs Ωd^2 - Ωc^2 Ωd^2 + Ωd^4 + 4 Δc^2 (4 Δd^2 - 4 Δd Δp + Ωd^2) - 
  2 γa^2 (4 Δc^2 + 26 Δd^2 + 10 Δp^2 + 2 Δc (13 Δd - 7 Δp - 2 Δs) + 
  6 Δp Δs - 2 Δd (18 Δp + 5 Δs) - Ωc^2 + 4 Ωd^2) + 4 Δc (8 Δd^3 - 4 Δd^2 (4 Δp + Δs) - 
  (4 Δp + Δs) Ωd^2 + Δd (8 Δp^2 + 4 Δp Δs - Ωc^2 + 4 Ωd^2)) - 2 I γa (24 Δd^3 + 
  4 Δc^2 (3 Δd - Δp) - 4 Δp^3 - 4 Δp^2 Δs - 4 Δd^2 (13 Δp + 4 Δs) + Δp Ωc^2 + Δs Ωc^2 - 
  2 I dephasing Ωd^2 - 10 Δp Ωd^2 - 3 Δs Ωd^2 + Δc (36 Δd^2 + 8 Δp^2 + 4 Δp Δs - 
  4 Δd (11 Δp + 3 Δs) - Ωc^2 + 7 Ωd^2) + Δd (32 Δp^2 + 20 Δp Δs - 3 Ωc^2 + 10 Ωd^2))))/
  ((γa + 2 I Δd) (2 γa^2 - 4 Δc^2 + 4 Δd Δp - 4 Δp^2 + 4 Δd Δs - 8 Δp Δs - 4 Δs^2 + 
  2 I γa (3 Δc + Δd - 3 (Δp + Δs)) + Δc (-4 Δd + 8 (Δp + Δs)) + Ωd^2) (4 I γa^3 (Δc - Δp) - 
  16 Δc^2 Δd Δp - 16 Δc Δd^2 Δp + 16 Δc^2 Δp^2 + 48 Δc Δd Δp^2 + 16 Δd^2 Δp^2 - 32 Δc Δp^3 -
  32 Δd Δp^3 + 16 Δp^4 - 4 Δc Δd Ωc^2 - 4 Δd^2 Ωc^2 + 8 Δc Δp Ωc^2 + 8 Δd Δp Ωc^2 - 
  8 Δp^2 Ωc^2 + Ωc^4 - 4 Δc^2 Ωd^2 - 4 Δc Δd Ωd^2 + 8 Δc Δp Ωd^2 + 8 Δd Δp Ωd^2 - 
  8 Δp^2 Ωd^2 - 2 Ωc^2 Ωd^2 + Ωd^4 + 2 γa^2 (-4 Δc^2 - 6 Δc Δd + 14 Δc Δp + 6 Δd Δp - 
  10 Δp^2 + Ωc^2 + Ωd^2) - 2 I γa (4 Δc^2 (Δd - 3 Δp) - 4 Δd^2 Δp + Δd (20 Δp^2 - 
  3 Ωc^2 - Ωd^2) + Δc (4 Δd^2 - 24 Δd Δp + 28 Δp^2 - 3 Ωc^2 - Ωd^2) + 4 Δp (-4 Δp^2 + 
  Ωc^2 + Ωd^2)) + 2 dephasing (2 γa^3 + 2 I γa^2 (2 Δc + 3 Δd - 5 Δp) + γa (-4 Δd^2 - 
  4 Δc (Δd - 3 Δp) + 20 Δd Δp - 16 Δp^2 + Ωc^2 + Ωd^2) + 2 I (4 Δd^2 Δp - 8 Δd Δp^2 +
  4 Δp^3 - Δp Ωc^2 + Δd Ωd^2 - Δp Ωd^2 + Δc (4 Δd Δp - 4 Δp^2 + Ωd^2)))))) /.
      {γa -> 1, dephasing -> 10^-4};

imFUN2 = ComplexExpand[Im[expression]];
$\endgroup$

1 Answer 1

8
$\begingroup$

Not an elegant solution but it works

num = Numerator[expression];
den = Denominator[expression];
{reNum, imNum} = ComplexExpand[ReIm[num]];
{reDen, imDen} = ComplexExpand[ReIm[den]];
(imNum reDen - reNum imDen)/(reDen^2 + imDen^2)

I think the problem is the denominator which is huge but I am surprised that my approach is not included in ComplexExpand

$\endgroup$
1
  • $\begingroup$ This works for me, thanks. It is interesting though that you call ComplexExpand[ReIm[num]] inside your function. I guess I should always make sure I don't have fractions-inside-fractions before calling this. Also for completion it might be helpful for others to know that the real part is: (reNum reDen + imNum imDen)/(reDen^2 + imDen^2) $\endgroup$ Commented Mar 18, 2019 at 21:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.