3
$\begingroup$

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]}]
$\endgroup$
2
  • 1
    $\begingroup$ 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] $\endgroup$
    – Bob Hanlon
    Commented Feb 28, 2023 at 20:43
  • $\begingroup$ n is a positive real number. $\endgroup$ Commented Feb 28, 2023 at 20:44

1 Answer 1

3
$\begingroup$
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]}]]

enter image description here

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

enter image description here

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]}]]

enter image description here

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

enter image description here

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.

$\endgroup$

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.