ComplexExpand[Re[Expr]] does not seem to work.

I have tried this in both Mathematica 9 and 10 — same result. Please see the attached notebook file. Basically, the ComplexExpand[Re[Eq==0]] produces the same result as ComplexExpand[Eq == 0] i.e. the real part of the complex expression is not taken.

This seems to work. A file is attached here.

It produces an answer, but this answer is not correct. There should be no cos(S/h) and sin(S/h) terms left.

I would disagree with that assessment.

\[CapitalPsi][x_, t_] := Sqrt[\[Rho][x, t]]*Exp[(I/\[HBar])*S[x, t]]; 
Eq = I*\[HBar]*D[\[CapitalPsi][x, t], t] + (\[HBar]^2/(2*m))*
        D[\[CapitalPsi][x, t], x, x] - U[x, t]*\[CapitalPsi][x, t]

(* Out[1]= (-E^((I*S[x, t])/\[HBar]))*U[x, t]*Sqrt[\[Rho][x, t]] + 
   I*\[HBar]*((I*E^((I*S[x, t])/\[HBar])*Sqrt[\[Rho][x, t]]*
             Derivative[0, 1][S][x, t])/\[HBar] + 
        (E^((I*S[x, t])/\[HBar])*Derivative[0, 1][\[Rho]][x, t])/
          (2*Sqrt[\[Rho][x, t]])) + (1/(2*m))*
     (\[HBar]^2*(-((E^((I*S[x, t])/\[HBar])*Sqrt[\[Rho][x, t]]*
                   Derivative[1, 0][S][x, t]^2)/\[HBar]^2) + 
           (I*E^((I*S[x, t])/\[HBar])*Derivative[1, 0][S][x, t]*
                Derivative[1, 0][\[Rho]][x, t])/(\[HBar]*
         Sqrt[\[Rho][x, t]]) - 
           (E^((I*S[x, t])/\[HBar])*Derivative[1, 0][\[Rho]][x, t]^2)/
             (4*\[Rho][x, t]^(3/2)) + (I*E^((I*S[x, t])/\[HBar])*
                Sqrt[\[Rho][x, t]]*
         Derivative[2, 0][S][x, t])/\[HBar] + 
           (E^((I*S[x, t])/\[HBar])*Derivative[2, 0][\[Rho]][x, t])/
             (2*Sqrt[\[Rho][x, t]]))) *)

Now expand, convert to trigs, and have a look at what appears without complex multipliers.

ExpToTrig[Expand[Eq]]

(* Out[9]= (-Cos[S[x, t]/\[HBar]])*U[x, t]*Sqrt[\[Rho][x, t]] - 
   I*Sin[S[x, t]/\[HBar]]*U[x, t]*Sqrt[\[Rho][x, t]] - 
   Cos[S[x, t]/\[HBar]]*Sqrt[\[Rho][x, t]]*Derivative[0, 1][S][x, 
       t] - I*Sin[S[x, t]/\[HBar]]*Sqrt[\[Rho][x, t]]*
     Derivative[0, 1][S][x, t] + 
   (I*\[HBar]*Cos[S[x, t]/\[HBar]]*Derivative[0, 1][\[Rho]][x, t])/
     (2*Sqrt[\[Rho][x, t]]) - (\[HBar]*Sin[S[x, t]/\[HBar]]*
        Derivative[0, 1][\[Rho]][x, t])/(2*Sqrt[\[Rho][x, t]]) - 
   (Cos[S[x, t]/\[HBar]]*Sqrt[\[Rho][x, t]]*Derivative[1, 0][S][x, t]^
          2)/(2*m) - (I*Sin[S[x, t]/\[HBar]]*Sqrt[\[Rho][x, t]]*
        Derivative[1, 0][S][x, t]^2)/(2*m) + 
   (I*\[HBar]*Cos[S[x, t]/\[HBar]]*Derivative[1, 0][S][x, t]*
        Derivative[1, 0][\[Rho]][x, t])/(2*m*Sqrt[\[Rho][x, t]]) - 
   (\[HBar]*Sin[S[x, t]/\[HBar]]*Derivative[1, 0][S][x, t]*
        Derivative[1, 0][\[Rho]][x, t])/(2*m*Sqrt[\[Rho][x, t]]) - 
   (\[HBar]^2*Cos[S[x, t]/\[HBar]]*Derivative[1, 0][\[Rho]][x, t]^2)/
     (8*m*\[Rho][x, t]^(3/2)) - (I*\[HBar]^2*Sin[S[x, t]/\[HBar]]*
        Derivative[1, 0][\[Rho]][x, t]^2)/(8*m*\[Rho][x, t]^(3/2)) + 
   (I*\[HBar]*Cos[S[x, t]/\[HBar]]*Sqrt[\[Rho][x, t]]*
        Derivative[2, 0][S][x, t])/(2*m) - 
   (\[HBar]*Sin[S[x, t]/\[HBar]]*Sqrt[\[Rho][x, t]]*
    Derivative[2, 0][S][
          x, t])/(2*m) + (\[HBar]^2*Cos[S[x, t]/\[HBar]]*
        Derivative[2, 0][\[Rho]][x, t])/(4*m*Sqrt[\[Rho][x, t]]) + 
   (I*\[HBar]^2*Sin[S[x, t]/\[HBar]]*Derivative[2, 0][\[Rho]][x, t])/
     (4*m*Sqrt[\[Rho][x, t]]) *)

The result of ComplexExpand[Eq==0] is correct and if I manually separate the real and imaginary parts, then of course the result is correct. But if I let Mathematica do this separation, i.e. ComplexExpand[Re[Eq]==0] then the result is obviously incorrect.

Ah, I should reformat everything in the usual (as on the terminal) input form and show here. Usually I work in a text terminal (was for the past 20 years, since MuPAD 1.4) but today I decided to try this fancy GUI thing called “Mathematica notebook” and got into all this mess… I will present a simple example that illustrates the problem.

Here it is in plain old good terminal text format:

$ math
Mathematica 10.0 for Linux x86 (64-bit)
Copyright 1988-2014 Wolfram Research, Inc.

In[1]:= Psi[x_,t_] := Sqrt[Rho[x,t]]*Exp[I*S[x,t]/h];                                                                                                                  

In[2]:= Eq = I*h*D[Psi[x,t],t] + (h^2/(2*m))*D[Psi[x,t],x,x] - U[x,t]*Psi[x,t];                                                                                        

In[3]:= FullSimplify[ComplexExpand[Eq==0], h>0 && m>0 && Element[U[x,t],Reals] && Rho[x,t]>0 && Element[S[x,t],Reals]]                                                 

         2    (1,0)      2              2                  (0,1)           (1,0)      2        (2,0)
Out[3]= h  Rho     [x, t]  + 4 Rho[x, t]  (2 m (U[x, t] + S     [x, t]) + S     [x, t]  - I h S     [x, t]) == 
 
                                (0,1)            (1,0)        (1,0)               (2,0)
>    2 h Rho[x, t] ((2 I) (m Rho     [x, t] + Rho     [x, t] S     [x, t]) + h Rho     [x, t])

In[4]:= FullSimplify[ComplexExpand[Re[Eq]==0], h>0 && m>0 && Element[U[x,t],Reals] && Rho[x,t]>0 && Element[S[x,t],Reals]]                                             

            S[x, t]    2    (1,0)      2              2                  (0,1)           (1,0)      2       2              (2,0)
Out[4]= Cos[-------] (h  Rho     [x, t]  + 4 Rho[x, t]  (2 m (U[x, t] + S     [x, t]) + S     [x, t] ) - 2 h  Rho[x, t] Rho     [x, t]) + 
               h
 
                        S[x, t]        (0,1)            (1,0)        (1,0)                    (2,0)
>     4 h Rho[x, t] Sin[-------] (m Rho     [x, t] + Rho     [x, t] S     [x, t] + Rho[x, t] S     [x, t]) == 0
                           h

So, why was Mathematica happy to get rid of the whole Exp[I*S/h] factor when asked to deal with the whole “Eq == 0” expression but has kept it (and split into Sin/Cos) when asked for just “Re[Eq]==0”?

Ah, I think I understand the question better now. Let’s factor the equation.

Eq // Factor

(* Out[70]= -((1/(800*m*Rho[x, t]^(3/2)))*(E^(10*I*S[x, t])*
         (800*m*Rho[x, t]^2*U[x, t] - 40*I*m*Rho[x, t]*
              Derivative[0, 1][Rho][x, t] + 800*m*Rho[x, t]^2*
              Derivative[0, 1][S][x, t] + 
            Derivative[1, 0][Rho][x, t]^2 - 40*I*Rho[x, t]*
              Derivative[1, 0][Rho][x, t]*Derivative[1, 0][S][x, 
                t] + 400*Rho[x, t]^2*Derivative[1, 0][S][x, t]^2 - 
            2*Rho[x, t]*Derivative[2, 0][Rho][x, t] - 
            40*I*Rho[x, t]^2*Derivative[2, 0][S][x, t]))) *)

I am guessing that FullSimplify applies rules to the effect that the “denominator” factor, and the complex exponential factor, cannot vanish. Hence they get removed on equating the expression to zero.

Yes, you are probably correct. But then, let’s try to assign the result of FullSimplify to a variable simple and then try to apply ComplexExpand[Re[simple]], it still doesn’t work:

In[8]:= simple = FullSimplify[ComplexExpand[Eq==0], h>0 && m>0 && Element[U[x,t],Reals] && Rho[x,t]>0 && Element[S[x,t],Reals]];                                       

In[9]:= ComplexExpand[Re[simple]]                                                                                                                                      

                     2                        2  (0,1)          2    (1,0)      2              2  (1,0)      2                    2  (2,0)
Out[9]= 8 m Rho[x, t]  U[x, t] + 8 m Rho[x, t]  S     [x, t] + h  Rho     [x, t]  + 4 Rho[x, t]  S     [x, t]  - (4 I) h Rho[x, t]  S     [x, t] == 
 
                            (0,1)                              (1,0)        (1,0)            2              (2,0)
>    (4 I) h m Rho[x, t] Rho     [x, t] + (4 I) h Rho[x, t] Rho     [x, t] S     [x, t] + 2 h  Rho[x, t] Rho     [x, t]

I think Re[expr] doesn’t work because it doesn’t know which symbols it can assume to be real. Those assumptions were only seen by FullSimplify and then gone. So, how do we make the assumptions visible to Re[]?

Might be easier than you suspect. ComplexExpand assumes all variables and “functions” to be real-valued, unless they appear explicitly in the optional second argument (a list of things it should NOT assume to be real).

Ok, then why do we still have terms with imaginary unit I appearing in the output of what has gone through Re and ComplexExpand?

I assume you refer to the behavior below.

In[300]:= ComplexExpand[Re[a + I*b == c + I*d]]

(* Out[300]= a + I b == c + I d *)

ComplexExpand is a fairly primitive function and, among other things, does not do well with Equal[...] nested
inside functions such as Re. I have filed a bug report for this. For working around it, my suggestion would be to subtract one side from the other and operate on expressions (implicitly regarded as equal to zero) rather than explicit equations.

Yes, thank you for the suggestion. Here is the version that works:

Psi[x_,t_] := Sqrt[Rho[x,t]]*Exp[I*S[x,t]/h];
Eq = I*h*D[Psi[x,t],t] + (h^2/(2*m))*D[Psi[x,t],x,x] - U[x,t]*Psi[x,t];
$Assumptions =  h>0 && m>0 && Element[U[x,t],Reals] && Rho[x,t]>0 && Element[S[x,t],Reals];
rl = FullSimplify[ComplexExpand[Re[Eq/Exp[I*S[x,t]/h]]]];
im = FullSimplify[ComplexExpand[Im[Eq/Exp[I*S[x,t]/h]]]];

So, if I divide the expression by the complex exponential factor before passing to ComplexExpand[Re[Eq]] then the result is correct:

In[5]:= rl == 0 // Simplify                                                                                                                                            

         2    (1,0)      2              2                     (0,1)          (1,0)      2        2              (2,0)
Out[5]= h  Rho     [x, t]  + 4 Rho[x, t]  (2 m U[x, t] + 2 m S     [x, t] + S     [x, t] ) == 2 h  Rho[x, t] Rho     [x, t]

In[6]:= im == 0 // Simplify                                                                                                                                            

             (0,1)            (1,0)        (1,0)                    (2,0)
Out[6]= m Rho     [x, t] + Rho     [x, t] S     [x, t] + Rho[x, t] S     [x, t] == 0