1
$\begingroup$

How does one get Mma to expand

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

into

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

Even evaluating this second expression results in the first one. For many things the second form is more useful that the first one, but it gets «simplified» out of existence… This is correct, of course, but unsightly. Apart, Expand and its friends, and a few others do nothing to the ugly iterated fraction.

$\endgroup$
7
  • $\begingroup$ It looks like Mathematica like to automatically write it in this form (i.e combines real/part of complex number). !Mathematica graphics but the question is: why does it matter to you? If it is just a display thing, then there are many way to make display as you want (but then you can't use that to compute with, as it will have wrapper around it). Can you show a case where the second form is needed to compute something you want and you can't do it on the first form as is? $\endgroup$
    – Nasser
    Jun 19, 2023 at 6:51
  • $\begingroup$ I am generating numbers to include in text to be typeset as part of an example in some lecture notes, and it is more or less standard tradition to write numbers in the way I want them :-) I know I can produce strings in the shape I want, format using wrappers and so on, but I find it rather wierd that there is no way afaict to tell Mma to do it. $\endgroup$ Jun 19, 2023 at 8:25
  • $\begingroup$ If you give a small example of what you want to generate (i.e. small example of input/output) I am sure someone will show you how to do it without having the original expression written as you want it. $\endgroup$
    – Nasser
    Jun 19, 2023 at 8:27
  • 1
    $\begingroup$ I want exactly what I described! $\endgroup$ Jun 19, 2023 at 8:48
  • $\begingroup$ Row[{Re[expr], "+", I Im[expr]}] // TraditionalForm where expr = (1/2 + I/2)/Sqrt[2] $\endgroup$
    – Syed
    Jun 19, 2023 at 8:54

3 Answers 3

1
$\begingroup$

One of many ways to do what you seem to want is to define a new output form:

myForm[z_?NumericQ] := With[{x=Re[z], y=Im[z]}, DisplayForm@
   If[y==0, x, RowBox[{x, If[y<0, "-", "+"], I*y*Sign[y]}]]];

Use this as follows:

(1/2 - I/2)/Sqrt[2] //myForm
(* 1/(2 Sqrt[2]) - I/(2 Sqrt[2]) *)

This way may or may not be what you wanted.

$\endgroup$
1
$\begingroup$

Possible (not very neat) workarounds?

exp=(1/2 + I/2)/Sqrt[2]

Defer[Plus[#,I#]]&@@ReIm@exp

$$\frac{1}{2 \sqrt{2}}+\frac{i}{2 \sqrt{2}}$$

Or:

HoldForm[Plus[#,I#]]&@@ReIm@exp

$$\frac{1}{2 \sqrt{2}}+\frac{i}{2 \sqrt{2}}$$

Check:

(HoldForm[Plus[#,I#]]&@@ReIm@exp//ReleaseHold)==exp

(* True *) 
$\endgroup$
0
$\begingroup$

By FullForm you see, that it contains

  Complex[Rationa[1,2],Rational[1,2]]. 

All normal commands respect complexes, because it needs 3 terms of studying, before you learn to deal with functions of z and Conjugate[z]. This is not complex calculus, but differential Kähler geometry in $R^2$

$\endgroup$
2
  • 4
    $\begingroup$ This seems more appropriate as a comment than an answer. $\endgroup$
    – bbgodfrey
    Jun 19, 2023 at 15:54
  • 1
    $\begingroup$ This strange fact, that MMA is handling exact rational complex numbers as atoms is the source of despair and a waist of time for many users; with no hint anywhere, that atoms don't simplify. So, as an answer, its perhaps easier to google. $\endgroup$
    – Roland F
    Jun 19, 2023 at 21:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.