# Forcing complex output to take the form $a + b\,i$

Solving the complex equation $z^2=1+2\,i$ using

Solve[z^2 == 1 + 2 I]


returns $\left\{\left\{z\to -\sqrt{1+2\,i}\right\},\left\{z\to\sqrt{1+2\,i}\right\}\right\}$, but how do I force Mathematica to always output on the form $a+b\,i$, $a,b\in\mathbb{R}$? Or, if there is no output form from Solve to do this, to convert/transform the answer to the $a+b\,i$ form?

I tried

z = a + b I;
Solve[{z^2 == 1 + 2 I, {a, b} ∈ Reals}, {a, b}]


which returns

$$\left\{\left\{a\to-\sqrt{\frac{1}{2}\left(1+\sqrt{5}\right)},b\to\sqrt{\frac{1}{2}\left(1+\sqrt{5}\right)}-\frac{\left(1+\sqrt{5}\right)^{3/2}}{2\sqrt{2}}\right\},\left\{a\to \sqrt{\frac{1}{2}\left(1+\sqrt{5}\right)},b\to\frac{\left(1+\sqrt{5}\right)^{3/2}}{2\sqrt{2}}-\sqrt{\frac{1}{2}\left(1+\sqrt{5}\right)}\right\}\right\}$$ but don't think it's a very elegant (and short) way to solve the equation.

One solution is, in its best presentation $$z_1=\sqrt{\frac{1+\sqrt{5}}{2}}+i\sqrt{\frac{2}{1+\sqrt{5}}}$$ Can this be output from Solve (or transformation of the output from Solve)?

Perhaps this?

ComplexExpand[Solve[z^2 == 1 + 2 I], TargetFunctions -> {Re, Im}]
(*
{{z -> -5^(1/4) Cos[ArcTan[2]/2] - I 5^(1/4) Sin[ArcTan[2]/2]},
{z ->  5^(1/4) Cos[ArcTan[2]/2] + I 5^(1/4) Sin[ArcTan[2]/2]}}
*)


Update: I saw Bill Watts' comment after I posted my first answer, which suggests FunctionExpand will help with the trig. functions. Simplifying the separate parts as follows gets the result closer to the desired form:

FunctionExpand@ComplexExpand[Solve[z^2 == 1 + 2 I]] /.
(*
{{z -> -I Sqrt[1/2 (-1 + Sqrt[5])] - Sqrt[1/2 (1 + Sqrt[5])]},
{z ->  I Sqrt[1/2 (-1 + Sqrt[5])] + Sqrt[1/2 (1 + Sqrt[5])]}}
*)

• Truly amazing. Will it work on any "simpler" complex equation or is it targeted to a special form of z^2=a+bi equations? How on earth do one learn this syntax? – mf67 Sep 10 '18 at 0:16
• @mf67 It should be general, although it's hard to say whether what Mathematica considers simplified will coincide with what is desired. Here are some tutorials on transformation rules: reference.wolfram.com/language/tutorial/… – Michael E2 Sep 10 '18 at 0:38

Well, you can define your own simplifying/expanding functions. For example, the following does the trick:

simplify[exp_] := FullSimplify[FunctionExpand[exp], ComplexityFunction -> ((LeafCount[#] + 100 Count[#, (_Root | _Sin | _Cos), {0, Infinity}]) &)]
rootExpand[exp_] := exp /. Sqrt[Complex[a_, b_]] :> (simplify[(a^2 + b^2)^(1/4) Cos[1/2 Arg[a + I b]]] + I simplify[(a^2 + b^2)^(1/4) Sin[1/2 Arg[a + I b]]])


Use them as follows:

Solve[z^2 == 1 + 2 I]
% // rootExpand

(* {{z -> -Sqrt[1 + 2 I]}, {z -> Sqrt[1 + 2 I]}} *)
(* {{z -> -I Sqrt[1/2 (-1 + Sqrt[5])] - Sqrt[1/2 (1 + Sqrt[5])]}, {z -> I Sqrt[1/2 (-1 + Sqrt[5])] + Sqrt[1/2 (1 + Sqrt[5])]}} *)

Solve[z^2 == 7 + 4 I]
% // rootExpand

(* {{z -> -Sqrt[7 + 4 I]}, {z -> Sqrt[7 + 4 I]}} *)
(* {{z -> -I Sqrt[1/2 (-7 + Sqrt[65])] - Sqrt[1/2 (7 + Sqrt[65])]}, {z -> I Sqrt[1/2 (-7 + Sqrt[65])] + Sqrt[1/2 (7 + Sqrt[65])]}} *)


FWIW: I think your solution is nice too (I don't find it inelegant nor unreasonably long). You can use the function simplify I defined above to make your output slightly nicer looking:

Solve[{(a + I b)^2 == 1 + 2 I, {a, b} \[Element] Reals}, {a, b}] // simplify
(* {{a -> -Sqrt[1/2 (1 + Sqrt[5])], b -> -Sqrt[1/2 (-1 + Sqrt[5])]}, {a -> Sqrt[1/2 (1 + Sqrt[5])], b -> Sqrt[1/2 (-1 + Sqrt[5])]}} *)