2
$\begingroup$

I know Mathematica has a strong tendency to reordering expressions, but this is too much and there must be a way. So I'll give you an extremely simple example.

I define this Complex number in Mathematica:

z = (2 + Sqrt[3]) + I Sqrt[3]

When I execute this, the output shows:

2 + (1 + I) Sqrt[3]

Mathematica insists in taking Sqrt[3] as common factor instead of I, but I want to see the original version (first the Real part, with our without parentheses, and then i times the imaginary part, or the imaginary part times i).

I have seen similar questions here, to no avail. Things I have tried:

  • Collect[z, I] (but it does not work; apparently Collect is not meant for that)

  • ComplexExpand[z] (but Mathematica insists in showing the expression with Sqrt[3] as common factor)

  • HoldForm[Re[z] + I Im[z]] (but output is literally Re[z]+I Im[z])

  • Defer[Re[z] + I Im[z]] (same)

  • etc.

Anybody knows? I don't mind creating my own little function with = or with :=

Thank you!

$\endgroup$
3
  • $\begingroup$ I just got closer. The following: HoldForm[Plus[2+Sqrt[3], Times[I ,Sqrt[3]]]] shows the right output. But I cannot yet use := to create a function to automate it. $\endgroup$ Commented Oct 24, 2022 at 12:16
  • 1
    $\begingroup$ Maybe reimForm[z_?NumericQ] := HoldForm[+##] & @@ ({1, I} ReIm[z]) or reimForm[z_?NumericQ] := HoldForm[+##] & @@ ComplexExpand[{1, I} ReIm[z]]? Or reimForm[z_?NumericQ] := {1, HoldForm@I} . ReIm[z]? Or reimForm[z_?NumericQ] := Apply[Inactive@Plus, {1, I} ReIm[z]] -- I guess I'll stop now... $\endgroup$
    – Michael E2
    Commented Oct 24, 2022 at 12:24
  • $\begingroup$ Thank you, Michael E2! All of them work. Also thank you to lericr, it also works! $\endgroup$ Commented Oct 25, 2022 at 8:32

2 Answers 2

3
$\begingroup$

Okay, this might seem a bit complicated, but I think it will be more comprehensive and reliable than what you've tried.

ComplexForm[z_?NumericQ] := 
  TemplateApply[
    TemplateExpression[Defer[TemplateSlot[1] + TemplateSlot[2] I]],
    ReIm[z]]

Here's your original example:

ComplexForm[(2 + Sqrt[3]) + I Sqrt[3]]
(* Defer[(2 + Sqrt[3]) + Sqrt[3]*I] *)

This also forces the format for reals:

ComplexForm[2]
(* Defer[2 + 0*I] *)

The nice thing about using the Defer wrapper is that the expressions are available for subsequent evaluation without dealing with Hold. Of course, you could use HoldForm (or whatever) if that is what you prefer.

Using templates feels a bit heavy-weight, but it allows us to set up the expression in held form without having to do the hold/evaluate/unevaluated hopscotch ourselves.

$\endgroup$
1
$\begingroup$

From all the answers given (thank you!) I think this tweak might be the simplest that works for what I want:

reimform[z_] := Re[z] + HoldForm[I] Im[z]

or Defer instead of HoldForm seems to work too.

$\endgroup$

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.