39

I have a complex expression with real positive variables only.

Mathematica Input Style:

PP = -((α (γ Cosh[y1 α] + α Sinh[y1 α])(-γ Cosh[(-L +  y2) α] + α Sinh[(-L + y2) α]))
    /(s (2 α γ Cosh[L α] + (α^2 + γ^2) Sinh[L α]))) /. α -> Sqrt[s/d] /. 
    γ -> (kd + s)/ka /. s -> -ω*I

which leads to

$ PP=-\frac{i \sqrt{-\frac{i \omega }{d}} \left(\frac{(\text{kd}-i \omega ) \text{Cosh}\left[\text{y1} \sqrt{-\frac{i \omega }{d}}\right]}{\text{ka}}+\sqrt{-\frac{i \omega }{d}} \text{Sinh}\left[\text{y1} \sqrt{-\frac{i \omega }{d}}\right]\right) \left(-\frac{(\text{kd}-i \omega ) \text{Cosh}\left[(-L+\text{y2}) \sqrt{-\frac{i \omega }{d}}\right]}{\text{ka}}+\sqrt{-\frac{i \omega }{d}} \text{Sinh}\left[(-L+\text{y2}) \sqrt{-\frac{i \omega }{d}}\right]\right)}{\omega \left(\frac{2 (\text{kd}-i \omega ) \sqrt{-\frac{i \omega }{d}} \text{Cosh}\left[L \sqrt{-\frac{i \omega }{d}}\right]}{\text{ka}}+\left(\frac{(\text{kd}-i \omega )^2}{\text{ka}^2}-\frac{i \omega }{d}\right) \text{Sinh}\left[L \sqrt{-\frac{i \omega }{d}}\right]\right)} $

Now I want to extract the real part of this by assuming real only variables and then Re[PP]. Mathematica 8 can't extract it.

I saw on different forums that many people have had difficulties with extracting real or imaginary parts of expressions with built-in Re.

Some propose to replace Re by ComplexExpand[ ( PP + Conjugate[PP] )/2 ], which seems to work well in some cases. Do you have other suggestions?

All the variables are real and positive, so that I define at the beginning of my Mathematica notebook:

$Assumptions = ω > 0 && d > 0 && L > 0 && y1 > 0 && y2 > 0 && 
    y1 < y2 && ka > 0 && kd > 0 && s > 0 && y1 < L && y2 < L

My version of Mathematica is $Version = "8.0 for Linux x86 (64-bit) (October 10, 2011)"

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
max
  • 1,585
  • 1
  • 14
  • 18
  • 1
    Could you post an example where ComplexExpand does not work well ? – b.gates.you.know.what Mar 21 '12 at 10:57
  • 1
    ComplexExpand[Re[PP]] will work. If you want Conjugate in the answer use ComplexExpand[Re[PP], TargetFunctions -> {Conjugate}]. Whatever you do, you will get a very long and complicated expression. Using Re alone is never going to "work" (well, it actually does "work", but does not expand - a different matter) except when dealing with numbers - how could it? The answers depends on which symbols are real and which are not. – Andrzej Kozlowski Mar 21 '12 at 11:24
  • ComplexExpand[Re[PP]] does return a result. The output is ugly because the result also depends on the sign of variables. – Szabolcs Mar 21 '12 at 11:26
  • 1
    Can we assume {d>0,ka>0,kd>0,L>0,y1>0,y2>0,\[Omega]>0} ? – Rolf Mertig Mar 21 '12 at 11:55
  • Every variable is real and >0. I just edited my original post accordingly. – max Mar 21 '12 at 13:08
  • ComplexExpand[Re[PP]] refined with positive "restrictions" works well. It is indeed ugly but that's not mathematica's "fault". Thank you very much to all of you. Could you explain the reason why ComplexExpand[Re[PP]] is working better than Assuming[Re[PP], everything positive] ? – max Mar 21 '12 at 13:17
  • @MaximilienLevesque because by default, Re simply does not perform expansion/simplification. – F'x Mar 21 '12 at 14:14
  • Actually the answers are equivalent but you seem to be using an older version of Mathematica (?) The way to get a simpler answer is to avoid expanding Cosh and Sinh since they are already real. – Andrzej Kozlowski Mar 21 '12 at 14:51
  • @AndrzejKozlowski Thanks anyway for you answer and not letting me trying around your input for days. Maybe you could put something as an answer about ComplexExpand[Re[PP]] so that I can give you a thumb up ? (and then delete this comment)... or whatever is required by stackexchange's quality standards? – max Mar 22 '12 at 09:08
  • See also this question for more about taking complex conjugates when variables can be assumed to be real. – Jess Riedel Sep 27 '17 at 19:17

1 Answers1

62

Removing the imaginary portion of an expression is done by doing

ComplexExpand[Re[expression]].

Using just Re alone will not work as Re does no evaluation on symbols with unknown complex parts.

Now as stated in the problem and the comments above this particular problem requires a fair amount of assumptions. The simplest way to add local assumptions is to use Assuming. But this will not work, so we must instead make use of Simplify. For example:

Simplify[ComplexExpand[Re[expression]], a > 0]

where a is a symbol used in expression and is a real number greater than zero.

rm -rf
  • 88,781
  • 21
  • 293
  • 472
nixeagle
  • 2,263
  • 1
  • 18
  • 24
  • Indeed, the expression is too large for Mathematica 8 to output everything, but I am not sure that is related to the real part extraction. thanks anyway for this clear answer. – max Mar 26 '12 at 08:43
  • This part about Assuming is completely wrong. Assuming has no effect on ComplexExpand. You will need to add Simplify, FullSimplify or Refine. Assuming alone does nothing when applied to functions that do not accept Assumptions. – Andrzej Kozlowski Mar 26 '12 at 18:56
  • 1
    It is also untrue that Re does no evaluation. Try for example Re[Pi + I E]. Obviously it can't do any evaluation if an expression contains symbols about which it is not known if they have non-zero imaginary parts. – Andrzej Kozlowski Mar 26 '12 at 19:07
  • 1
    Oops, in that case feel free to improve the answer. It is a community wiki thing, I don't get any rep or credit from it. I simply attempted to summarize the comments with the purported answers. :) I'll get to correcting it myself soon otherwise. – nixeagle Mar 26 '12 at 20:11
  • Can we use the same command if we have a matrix instead of an expression? – user31694 Feb 17 '23 at 09:57