9

When I try to evaluate Re[Sqrt[z]], for some values of Mathematica fails to evaluate it.

For example,

Re[Sqrt[2 + I*x]]`

Re[Sqrt[2 + I x]]

How can I get the real number in Sqrt[complex number]?

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
user132682
  • 317
  • 3
  • 8

2 Answers2

7

Given that there is a universal algebraic solution, we should not be satisfied with trigonometric expressions.

The simplest, most direct way I have found to obtain the real part of the root of $x + i y$ begins with the inverse of the square root, suggesting we would like the $u$ part of this solution:

Solve[(u + I v)^2 - (x + I y), {u, v}, Reals] (* Does not succeed *)

Unfortunately Mathematica complains about the presence of imaginary values when real solutions are sought. So we have to force a separation into real and imaginary parts:

u /. #& /@ Solve[ComplexExpand@Through[And[Re[#]==0&, Im[#]==0&][(u+I v)^2-(x+I y)]], {u, v}]

We get four solutions, of which two are evidently imaginary, leaving

$$u = \pm\frac{\sqrt{\sqrt{x^2+y^2}+x}}{\sqrt{2}}$$

as the two real solutions, as expected. (You could specify Reals as an argument to Solve, which will return just the two real solutions--but in the form of Root objects. They take some further manipulation to put into this nice form.)

This technique generalizes in an obvious way to more complicated inverse functions than mere square roots.

whuber
  • 20,544
  • 2
  • 59
  • 111
5

Try playing with this:

ComplexExpand[Re[Sqrt[y + I x]], TargetFunctions -> {Re, Im}]

(x^2 + y^2)^(1/4) Cos[1/2 ArcTan[y, x]]

ComplexExpand[Re[Sqrt[2 + I 5]], TargetFunctions -> {Re, Im}] //TrigToExp //FullSimplify

Sqrt[1/2 (2 + Sqrt[29])]

The function ComplexExpand is quite intelligent. For example, you can have mixed variables - ones that you know are real and those that may be complex. Here how it can derive a nice formula:

ComplexExpand[Re[Sqrt[(2 + I x)/a]], a, TargetFunctions -> {Re, Im}] //
    TrigToExp // FullSimplify // TraditionalForm

enter image description here

Vitaliy Kaurov
  • 73,078
  • 9
  • 204
  • 355