# Finding real and imaginary part of long expression takes too long

I generally try to find real and imaginary parts of long expressions such as in the following example. But it takes too long time.

a) How can we speed up it? (and it includes terms such as Abs[U[\[Eta]]]^(4 n) where n is a positive real number.)

b) How can we automatize the assumptions by assuming the U[\[Eta]] and its derivatives are real numbers?

  expr = (Subscript[c, 4]*U[\[Eta]] + Subscript[c, 3]*U[\[Eta]]*(U[\[Eta]]^2)^(n/2) + (U[\[Eta]]^2)^n*(Subscript[c, 2]*U[\[Eta]] + Subscript[c, 1]*U[\[Eta]]*(U[\[Eta]]^2)^(n/2) +
(U[\[Eta]]^2)^n*(U[\[Eta]]*(\[Gamma] - \[Omega] + \[Kappa]*((-\[Kappa])*Subscript[\[Alpha], 1]) + Subscript[\[Mu], 1]*(U[\[Eta]]^2)^(n/2) +
(U[\[Eta]]^2)^n*(Subscript[\[Mu], 3]*(U[\[Eta]]^2)^(n/2) + Subscript[\[Mu], 4]*(U[\[Eta]]^2)^n)) - I*(v + 3*\[Kappa]^2*Subscript[\[Beta], 2])*Derivative[1][U][\[Eta]] +
Subscript[\[Alpha], 1]*Derivative[2][U][\[Eta]] - 6*\[Kappa]^2*Derivative[2][U][\[Eta]] + 3*\[Kappa]*Subscript[\[Beta], 2]*Derivative[2][U][\[Eta]] -
4*I*\[Kappa]*Derivative[3][U][\[Eta]] + I*Subscript[\[Beta], 2]*Derivative[3][U][\[Eta]] + Subscript[\[Alpha], 2]*Derivative[6][U][\[Eta]])))/(U[\[Eta]]^2)^(2*n);

realpart = FullSimplify[ComplexExpand[Re[expr]], {Element[U[\[Eta]], Reals], Element[Derivative[1][U][\[Eta]], Reals], Element[Derivative[2][U][\[Eta]], Reals],
Element[Derivative[3][U][\[Eta]], Reals], Element[Derivative[4][U][\[Eta]], Reals], Element[Derivative[6][U][\[Eta]], Reals]}]

imagpart=     FullSimplify[ComplexExpand[Im[expr]], {Element[U[\[Eta]], Reals], Element[Derivative[1][U][\[Eta]], Reals], Element[Derivative[2][U][\[Eta]], Reals],
Element[Derivative[3][U][\[Eta]], Reals], Element[Derivative[4][U][\[Eta]], Reals], Element[Derivative[6][U][\[Eta]], Reals]}]

• Use Simplify rather than FullSimplify. The simplification is helped if you can tell it something about n, e.g., integer? nonnegative integer? positive integer? The derivatives can be covered by Element[Derivative[_][U][\[Eta]], Reals] Commented Feb 28, 2023 at 20:43
• n is a positive real number. Commented Feb 28, 2023 at 20:44

Clear["Global*"]

expr = (Subscript[c, 4]*U[η] +
Subscript[c, 3]*
U[η]*(U[η]^2)^(n/2) + (U[η]^2)^
n*(Subscript[c, 2]*U[η] +
Subscript[c, 1]*
U[η]*(U[η]^2)^(n/2) + (U[η]^2)^
n*(U[η]*(γ - ω + κ*((-κ)*
Subscript[α, 1]) +
Subscript[μ, 1]*(U[η]^2)^(n/2) + (U[η]^2)^
n*(Subscript[μ, 3]*(U[η]^2)^(n/2) +
Subscript[μ, 4]*(U[η]^2)^n)) -
I*(v + 3*κ^2*Subscript[β, 2])*
Derivative[1][U][η] +
Subscript[α, 1]*Derivative[2][U][η] -
6*κ^2*Derivative[2][U][η] +
3*κ*Subscript[β, 2]*Derivative[2][U][η] -
4*I*κ*Derivative[3][U][η] +
I*Subscript[β, 2]*Derivative[3][U][η] +
Subscript[α, 2]*
Derivative[6][U][η])))/(U[η]^2)^(2*n);


Including the constraint that n > 0 speeds up both of the calculations. Always provide as much information to Mathematica as possible.

{t1, realpart1} =
AbsoluteTiming[
FullSimplify[
ComplexExpand[Re[expr]], {Element[U[η], Reals],
Element[Derivative[_][U][η], Reals]}]]


{t2, realpart2} =
AbsoluteTiming[
FullSimplify[
ComplexExpand[Re[expr]], {Element[U[η], Reals],
Element[Derivative[_][U][η], Reals], n > 0}]]


t2/t1

(* 0.53913 *)

N[Divide @@ LeafCount /@ {realpart2, realpart1}]

(* 0.929134 *)

{t3, imagpart1} =
AbsoluteTiming[
FullSimplify[
ComplexExpand[Im[expr]], {Element[U[η], Reals],
Element[Derivative[_][U][η], Reals]}]]


{t4, imagpart2} =
AbsoluteTiming[
FullSimplify[
ComplexExpand[Im[expr]], {Element[U[η], Reals],
Element[Derivative[_][U][η], Reals], n > 0}]]


t4/t3

(* 0.666604 *)


However, the additional assumption did not change the result for the imaginary part

imagpart1 === imagpart2

(* True b*)


If speed is the primary concern, then use Simplify rather than FullSimplify`. Obviously, the simplification will generally be less effective.