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

**URL:** <https://community.wolfram.com/t/complexexpand-re-expr-does-not-seem-to-work/7457>\
**Category:** Questions\
**Tags:** physics, mathematica\
**Created:** [December 13, 2015, 2:15pm UTC](https://community.wolfram.com/t/complexexpand-re-expr-does-not-seem-to-work/7457 "2015-12-13T14:15:46Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![aivaziantigran](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/aivaziantigran/32/34339_2.png) [@aivaziantigran](https://community.wolfram.com/u/aivaziantigran)\
**Post date:** [December 13, 2015, 2:15pm UTC](https://community.wolfram.com/t/complexexpand-re-expr-does-not-seem-to-work/7457/1 "2015-12-13T14:15:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mangriokamran](https://avatars.discourse-cdn.com/v4/letter/m/b9bd4f/32.png) [@mangriokamran](https://community.wolfram.com/u/mangriokamran)\
**Post date:** [December 13, 2015, 3:19pm UTC](https://community.wolfram.com/t/complexexpand-re-expr-does-not-seem-to-work/7457/2 "2015-12-13T15:19:13Z")

</div>

This seems to work. A file is attached here.

---

<div class="post-metadata">

**Author:** ![aivaziantigran](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/aivaziantigran/32/34339_2.png) [@aivaziantigran](https://community.wolfram.com/u/aivaziantigran)\
**Post date:** [December 13, 2015, 4:31pm UTC](https://community.wolfram.com/t/complexexpand-re-expr-does-not-seem-to-work/7457/3 "2015-12-13T16:31:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![danl](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/danl/32/41267_2.png) [@danl](https://community.wolfram.com/u/danl)\
**Post date:** [December 13, 2015, 8:54pm UTC](https://community.wolfram.com/t/complexexpand-re-expr-does-not-seem-to-work/7457/4 "2015-12-13T20:54:05Z")

</div>

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]]) *)

```

---

<div class="post-metadata">

**Author:** ![aivaziantigran](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/aivaziantigran/32/34339_2.png) [@aivaziantigran](https://community.wolfram.com/u/aivaziantigran)\
**Post date:** [December 13, 2015, 9:56pm UTC](https://community.wolfram.com/t/complexexpand-re-expr-does-not-seem-to-work/7457/5 "2015-12-13T21:56:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![aivaziantigran](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/aivaziantigran/32/34339_2.png) [@aivaziantigran](https://community.wolfram.com/u/aivaziantigran)\
**Post date:** [December 13, 2015, 10:07pm UTC](https://community.wolfram.com/t/complexexpand-re-expr-does-not-seem-to-work/7457/6 "2015-12-13T22:07:36Z")

</div>

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”?

---

<div class="post-metadata">

**Author:** ![danl](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/danl/32/41267_2.png) [@danl](https://community.wolfram.com/u/danl)\
**Post date:** [December 13, 2015, 10:31pm UTC](https://community.wolfram.com/t/complexexpand-re-expr-does-not-seem-to-work/7457/7 "2015-12-13T22:31:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![aivaziantigran](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/aivaziantigran/32/34339_2.png) [@aivaziantigran](https://community.wolfram.com/u/aivaziantigran)\
**Post date:** [December 14, 2015, 9:59am UTC](https://community.wolfram.com/t/complexexpand-re-expr-does-not-seem-to-work/7457/8 "2015-12-14T09:59:24Z")

</div>

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[]`?

---

<div class="post-metadata">

**Author:** ![danl](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/danl/32/41267_2.png) [@danl](https://community.wolfram.com/u/danl)\
**Post date:** [December 14, 2015, 3:42pm UTC](https://community.wolfram.com/t/complexexpand-re-expr-does-not-seem-to-work/7457/9 "2015-12-14T15:42:56Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![aivaziantigran](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/aivaziantigran/32/34339_2.png) [@aivaziantigran](https://community.wolfram.com/u/aivaziantigran)\
**Post date:** [December 14, 2015, 4:07pm UTC](https://community.wolfram.com/t/complexexpand-re-expr-does-not-seem-to-work/7457/10 "2015-12-14T16:07:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![danl](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/danl/32/41267_2.png) [@danl](https://community.wolfram.com/u/danl)\
**Post date:** [December 14, 2015, 5:08pm UTC](https://community.wolfram.com/t/complexexpand-re-expr-does-not-seem-to-work/7457/11 "2015-12-14T17:08:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![aivaziantigran](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/aivaziantigran/32/34339_2.png) [@aivaziantigran](https://community.wolfram.com/u/aivaziantigran)\
**Post date:** [December 14, 2015, 7:42pm UTC](https://community.wolfram.com/t/complexexpand-re-expr-does-not-seem-to-work/7457/12 "2015-12-14T19:42:17Z")

</div>

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

```
