4
$\begingroup$

Here's a simple Manipulate demonstration of complex values:

Manipulate[ ComplexPlot[zz, {zz, -2 - 2 I, 2 + 2 I}, Epilog -> {Arrow[{{0, 0}, z}]}], {z, {-2, -2}, {2, 2}}] 

The user adjusts the Control thereby placing the arrow tip at a complex point z.

I'd like to have text presumably exploiting Epilog that reads something like:

$z = 0.8 - 1.1 i$

$\quad = 1.36 e^{-0.94\ i}$

placed in the upper-left of the plot, with the equals signs aligned one atop the other (and small line spacing).

You'll recall from mathematics that if a complex number is $z = a + b i$, then its polar representation is $\sqrt{a^2 + b^2} e^{\tan^{-1} (b/a)}$.

I'd like the original $z$ in the text to be held fixed in place in the plot.

The problems that have stumped me are the following:

  • Ensuring the original $z$ stays fixed in place on the ComplexPlot as the values (and width of text) changes. (Surely there must be a TextAlignment -> Left somewhere in the code.)
  • Typesetting the "+" or "-" in the first row. The naive approach is to include ...<>"+"<>.... between the two entries in z, but then you sometimes get the ugly and incorrect $0.8 + - 1.1$
  • Representing the magnitude on the second line ($\sqrt{a^2 + b^2}$) to two-digit accuracy. I've tried N[Norm[z],2] but somehow I always get five digits.
  • Typesetting the $e^{\tan^{-1}(a/b) i}$. (Because the location of z in the complex quadrants is important for the angle, we should use ArcTan[z[[2]],z[[1]]])

I'd also like to use the special $i$ and $e$ typeset in Mathematica.

The whole task seems an exercise in frustration, and I was hoping there is some elegant approach that avoids all the calls of ToString, Superscript, Text, etc.


Here's code that doesn't work:

Epilog -> {Black, PointSize[0.02], Point[zz], Arrow[{{0, 0}, zz}], Text[Style[ "z = " <> ToString[zz[[1]]] <> "+" <> ToString[zz[[2]]] <> "\[ImaginaryI]" <> "\n =" <> ToString[N[Sqrt[zz . zz], 2]] <> "\!\(\*SuperscriptBox[\(\[ExponentialE]\), \(\[ImaginaryI]\)]\)" \ <> ToString[ArcTan[zz[[1]], zz[[2]]]], 20, Black, Italic, FontFamily -> "Latin Modern Roman", LineSpacing -> -10], {-1, 1.65}]} 
$\endgroup$
0

3 Answers 3

4
$\begingroup$
grid[z_] := Grid[ {{"z", " = ", Item[Round[Complex @@ z, .01], ItemSize -> {12, 1}]}, {"", " = ", Round[Norm[z], .01] Exp[Round[ArcTan @@ z, .01] Defer[I]]}}, Alignment -> {Left, Center}] grid[{1.2345, -2.89123}] // Style[#, 16] & 

enter image description here

Notes:

  1. Specified ItemSize in last column of the first row to prevent columns from moving around as the content size of the last column entries change.

  2. Wrapped I with Defer to prevent default expression formatting in the second row.

We can use grid[z] as Inset in Epilog option setting:

cp = ComplexPlot[zz, {zz, -2 - 2 I, 2 + 2 I}]; Manipulate[ Show[cp, PlotLabel -> Style[PromptForm["z", z], 14], Epilog -> {PointSize @ Large, Point @ z, Inset[Style[#, 16] & @ grid[z], Offset[{10, -10}, {-2, 2}], {Left, Top}], Arrow[{{0, 0}, z}]}], {{z, {.8, -1.1}}, {-2, -2}, {2, 2}}] 

enter image description here

FWIW: You can use a Locator as control instead of Slider2D with a simple modification of control spec as

{{z, {.8, -1.1}}, {-2, -2}, {2, 2}, Locator, Appearance -> None} 

to get

enter image description here

$\endgroup$
1
  • 1
    $\begingroup$ Clean and elegant. Note, you're missing an "@" in your Manipulate before Grid[z], but otherwise superb (as always). I didn't know that the Exp[k] would automatically be converted to $e^{k}$ when rendered. And yes, the Locator is a good idea, which I'll use. All very helpful. ($+1, \checkmark$) $\endgroup$ Commented Jan 27, 2024 at 17:05
3
$\begingroup$

Something like:

Manipulate[ ComplexPlot[zz, {zz, -2 - 2 I, 2 + 2 I}, Epilog -> {Arrow[{{0, 0}, z}], Text[ "z = {" <> ToString[z . {1, "\[ImaginaryI]"}] <> "}", {-1.9, 1.9}, {-1, 0}], Text[" = " <> ToString[Norm@z] Superscript["\[ExponentialE]", Arg[z . {1, I}]], {-1.9, 1.7}, {-1, 0}]}], {z, {-2, -2}, {2, 2}}] 

enter image description here

$\endgroup$
1
  • $\begingroup$ Clever use of the dot product. Thanks! ($+1$) $\endgroup$ Commented Jan 27, 2024 at 18:40
2
$\begingroup$

Use the third argument of Inset to specify the anchoring. Use NumberForm to get two-digit representation. Use ResourceFunction["ComplexToPolar"] to get the polar representation of your number.

A simple example:

ctp = ResourceFunction["ComplexToPolar"]; Manipulate[ ComplexPlot[w, {w, -2 - 2 I, 2 + 2 I}, Epilog -> {Black, PointSize[0.02], Point[z], Arrow[{{0, 0}, z}], FontSize -> 20, With[{zz = Total[{1, I} z]}, Inset[Column[{zz, NumberForm[ctp[zz], 2]}], Scaled[{.1, .9}], {Left, Top}]]} ], {z, {-2, -2}, {2, 2}}] 

enter image description here

What will probably bother you now is the presence of ×, representing inactive form of Times. Let's do some hackery ...

Unprotect[Inactive]; Format[Inactive[Times][x_, y_]] := Row[{x, " ", y}] Protect[Inactive]; 

enter image description here

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.