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

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 $leftleftzto -sqrt1+2,iright,leftztosqrt1+2,irightright$, but how do I force Mathematica to always output on the form $a+b,i$, $a,binmathbbR$? Or, if there is no output form from Solve to do this, to convert/transform the answer to the $a+b,i$ form?


Solve



I tried


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



which returns



$$leftleftato-sqrtfrac12left(1+sqrt5right),btosqrtfrac12left(1+sqrt5right)-fracleft(1+sqrt5right)^3/22sqrt2right,leftato sqrtfrac12left(1+sqrt5right),btofracleft(1+sqrt5right)^3/22sqrt2-sqrtfrac12left(1+sqrt5right)rightright$$
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=sqrtfrac1+sqrt52+isqrtfrac21+sqrt5$$
Can this be output from Solve (or transformation of the output from Solve)?


Solve





$begingroup$
Solve[z^2 == 1 + 2 I] // ComplexExpand // FunctionExpand gets you pretty close.
$endgroup$
– Bill Watts
Sep 9 '18 at 23:57


Solve[z^2 == 1 + 2 I] // ComplexExpand // FunctionExpand





$begingroup$
Related link: mathematica.stackexchange.com/questions/173930/…
$endgroup$
– mathe
Sep 10 '18 at 2:21




2 Answers
2



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


FunctionExpand@ComplexExpand[Solve[z^2 == 1 + 2 I]] /.
x_?NumericQ :> ToRadicals@FullSimplify[Re[x]] + I ToRadicals@FullSimplify[Im[x]]
(*
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])]
*)





$begingroup$
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?
$endgroup$
– mf67
Sep 10 '18 at 0:16





$begingroup$
@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/…
$endgroup$
– 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:


simplify


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



Thanks for contributing an answer to Mathematica Stack Exchange!



But avoid



Use MathJax to format equations. MathJax reference.



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế