PlanarAngle Issues?

17.07.2021, WernerGg
I am observing a very strange behavior from PlanarAngle:
1. The result of PlanarAngle depends on the previous history.
2. PlanerAngle assumes for the first few calls that its arguments are complex. After that, it doesn’t.
​
Fortunately, however, all results are correct.
​
​I am using Version 12.3.1 on current Windows 10.

Initialization dependency

Within a freshly loaded kernel

If you have a freshly loaded kernel and notebook and execute:
In[]:=
PlanarAngle[{0,1}->{{-Sin[ϕ],Cos[ϕ]},{Cos[ϕ],Sin[ϕ]}}]
Out[]=
ArcCos
(-1+Conjugate[Sin[ϕ]])(-1+Cos[ϕ])-Conjugate[Cos[ϕ]]Sin[ϕ]
2
Abs[Cos[ϕ]]
+
2
Abs[-1+Sin[ϕ]]
2
Abs[-1+Cos[ϕ]]
+
2
Abs[Sin[ϕ]]

... PlanarAngle seems to take ϕ as Complex. Which is alright.
​
If you execute it a second time:
In[]:=
PlanarAngle[{0,1}->{{-Sin[ϕ],Cos[ϕ]},{Cos[ϕ],Sin[ϕ]}}]
Out[]=
ArcCos
(-1+Conjugate[Sin[ϕ]])(-1+Cos[ϕ])-Conjugate[Cos[ϕ]]Sin[ϕ]
2
Abs[Cos[ϕ]]
+
2
Abs[-1+Sin[ϕ]]
2
Abs[-1+Cos[ϕ]]
+
2
Abs[Sin[ϕ]]

... it’s still complex. But at the third time - or sometimes the fourth or fifth time:
In[]:=
PlanarAngle[{0,1}->{{-Sin[ϕ],Cos[ϕ]},{Cos[ϕ],Sin[ϕ]}}]
Out[]=
ArcCos-
-1+Cos[ϕ]+Sin[ϕ]
2
(-1+Cos[ϕ])(-1+Sin[ϕ])

In[]:=
PlanarAngle[{0,1}->{{-Sin[ϕ],Cos[ϕ]},{Cos[ϕ],Sin[ϕ]}}]
Out[]=
ArcCos-
-1+Cos[ϕ]+Sin[ϕ]
2
(-1+Cos[ϕ])(-1+Sin[ϕ])

... it' s Real now.

With Remove

To make this reproducible without loading I use Remove, ClearAll or Clear:
In[]:=
(*FirstexecutionafterRemove*)​​Remove["Global`*"];​​PlanarAngle[{0,1}->{{-Sin[ϕ],Cos[ϕ]},{Cos[ϕ],Sin[ϕ]}}]
Out[]=
ArcCos
(-1+Conjugate[Sin[ϕ]])(-1+Cos[ϕ])-Conjugate[Cos[ϕ]]Sin[ϕ]
2
Abs[Cos[ϕ]]
+
2
Abs[-1+Sin[ϕ]]
2
Abs[-1+Cos[ϕ]]
+
2
Abs[Sin[ϕ]]

In[]:=
(*SecondexecutionafterRemove*)​​PlanarAngle[{0,1}->{{-Sin[ϕ],Cos[ϕ]},{Cos[ϕ],Sin[ϕ]}}]
Out[]=
ArcCos
(-1+Conjugate[Sin[ϕ]])(-1+Cos[ϕ])-Conjugate[Cos[ϕ]]Sin[ϕ]
2
Abs[Cos[ϕ]]
+
2
Abs[-1+Sin[ϕ]]
2
Abs[-1+Cos[ϕ]]
+
2
Abs[Sin[ϕ]]

... it’s still Complex. But at the third or fourth time:
In[]:=
(*ThirdexecutionafterRemove*)​​PlanarAngle[{0,1}->{{-Sin[ϕ],Cos[ϕ]},{Cos[ϕ],Sin[ϕ]}}]
Out[]=
ArcCos-
-1+Cos[ϕ]+Sin[ϕ]
2
(-1+Cos[ϕ])(-1+Sin[ϕ])

... it’s Real now. Very, very strange. I don’t understand a word.
How did I find that?
Until now I had a first initialization cell with Remove in all my notebooks. I did that to get rid of globally defined protected symbols and functions created by code. With that cell at any time I can reset the notebook to a clear initial state just by executing all initialization cells. This cell was:
In[]:=
If[True,
Unprotect["`*"]; Quiet[Remove["Global`*"], {Remove::rmnsm}]
,
ClearAll["Global`*"]
]

With ClearAll

Let' s try ClearAll instead of Remove. At first I get back to the state after Remove:
In[]:=
(*Getridofeverything*)​​Remove["Global`*"]
Then I do a ClearAll and execute PlanarAngle for the first time:
In[]:=
(*FirstexecutionafterClearAll*)​​ClearAll["Global`*"];​​PlanarAngle[{0,1}->{{-Sin[ϕ],Cos[ϕ]},{Cos[ϕ],Sin[ϕ]}}]
Out[]=
ArcCos
(-1+Conjugate[Sin[ϕ]])(-1+Cos[ϕ])-Conjugate[Cos[ϕ]]Sin[ϕ]
2
Abs[Cos[ϕ]]
+
2
Abs[-1+Sin[ϕ]]
2
Abs[-1+Cos[ϕ]]
+
2
Abs[Sin[ϕ]]

... PlanarAngle takes ϕ as Complex again.
​
If I execute it a second time:
In[]:=
(*SecondexecutionafterClearAll*)​​PlanarAngle[{0,1}->{{-Sin[ϕ],Cos[ϕ]},{Cos[ϕ],Sin[ϕ]}}]
Out[]=
ArcCos
(-1+Conjugate[Sin[ϕ]])(-1+Cos[ϕ])-Conjugate[Cos[ϕ]]Sin[ϕ]
2
Abs[Cos[ϕ]]
+
2
Abs[-1+Sin[ϕ]]
2
Abs[-1+Cos[ϕ]]
+
2
Abs[Sin[ϕ]]

... it’s still Complex. But at the third time:
In[]:=
(*ThirdexecutionafterClearAll*)​​PlanarAngle[{0,1}->{{-Sin[ϕ],Cos[ϕ]},{Cos[ϕ],Sin[ϕ]}}]
Out[]=
ArcCos-
-1+Cos[ϕ]+Sin[ϕ]
2
(-1+Cos[ϕ])(-1+Sin[ϕ])

In[]:=
(*FourthexecutionafterClearAll*)​​PlanarAngle[{0,1}->{{-Sin[ϕ],Cos[ϕ]},{Cos[ϕ],Sin[ϕ]}}]
Out[]=
ArcCos-
-1+Cos[ϕ]+Sin[ϕ]
2
(-1+Cos[ϕ])(-1+Sin[ϕ])

... it' s Real now.
​
Hence there is no difference to initialization with Remove.

With Clear

Maybe I have to use only Clear?
In[]:=
(*Getridofeverything*)​​Remove["Global`*"]
Then I do a Clear and execute PlanarAngle for the first time:
In[]:=
(*FirstexecutionafterClear*)​​Clear["Global`*"];​​PlanarAngle[{0,1}->{{-Sin[ϕ],Cos[ϕ]},{Cos[ϕ],Sin[ϕ]}}]
Out[]=
ArcCos
(-1+Conjugate[Sin[ϕ]])(-1+Cos[ϕ])-Conjugate[Cos[ϕ]]Sin[ϕ]
2
Abs[Cos[ϕ]]
+
2
Abs[-1+Sin[ϕ]]
2
Abs[-1+Cos[ϕ]]
+
2
Abs[Sin[ϕ]]

... PlanarAngle takes ϕ as Complex again.
​
If I execute it a second time:
In[]:=
(*SecondexecutionafterClear*)​​PlanarAngle[{0,1}->{{-Sin[ϕ],Cos[ϕ]},{Cos[ϕ],Sin[ϕ]}}]
Out[]=
ArcCos
(-1+Conjugate[Sin[ϕ]])(-1+Cos[ϕ])-Conjugate[Cos[ϕ]]Sin[ϕ]
2
Abs[Cos[ϕ]]
+
2
Abs[-1+Sin[ϕ]]
2
Abs[-1+Cos[ϕ]]
+
2
Abs[Sin[ϕ]]

... it’s still Complex. But at the third time:
In[]:=
(*ThirdexecutionafterClear*)​​PlanarAngle[{0,1}->{{-Sin[ϕ],Cos[ϕ]},{Cos[ϕ],Sin[ϕ]}}]
Out[]=
ArcCos-
-1+Cos[ϕ]+Sin[ϕ]
2
(-1+Cos[ϕ])(-1+Sin[ϕ])

... it' s Real now.
​
Hence there is no difference to initialization with Remove.

Using ϕ, then Clear

Maybe I have to use ϕ in some way and then Clear it?
In[]:=
(*Getridofeverything*)​​Remove["Global`*"]
In[]:=
(*useϕinsomeway*)​​ϕ=π
Out[]=
π
In[]:=
(*Clearϕandexecute*)​​Clear[ϕ];​​PlanarAngle[{0,1}->{{-Sin[ϕ],Cos[ϕ]},{Cos[ϕ],Sin[ϕ]}}]
Out[]=
ArcCos
(-1+Conjugate[Sin[ϕ]])(-1+Cos[ϕ])-Conjugate[Cos[ϕ]]Sin[ϕ]
2
Abs[Cos[ϕ]]
+
2
Abs[-1+Sin[ϕ]]
2
Abs[-1+Cos[ϕ]]
+
2
Abs[Sin[ϕ]]

This doesn’t help either. I have no idea what happens.

What is happening?

Maybe this is not a particular problem of PlanarAngle but my misunderstanding of the initialization process, Remove, ClearAll and Clear. But even though it's strange that PlanarAngle seems to change its behavior after some few calls.
As a brute bypass I change my initialization cell to:
If[True,
Unprotect["`*"]; Quiet[Remove["Global`*"], {Remove::rmnsm}]
,
ClearAll["Global`*"]
];
PlanarAngle[{0, 1} -> {{-Sin[ϕ], Cos[ϕ]}, {Cos[ϕ], Sin[ϕ]}}];
PlanarAngle[{0, 1} -> {{-Sin[ϕ], Cos[ϕ]}, {Cos[ϕ], Sin[ϕ]}}];
PlanarAngle[{0, 1} -> {{-Sin[ϕ], Cos[ϕ]}, {Cos[ϕ], Sin[ϕ]}}];
Note : Within this example notebook, this is not an initialization cell of course.

Why takes PlanerAngle symbols as Complex only some few times?
Are the results equal?

I showed this already in the section above. Here it’s a bit more elaborated.
Execute the following at least twice. On first execution you will get striking different results.
When results are stable it seems to hold true:
1: It’s Real after some calls (see above).
2: Even though PlanarAngle should take no Assumptions (See documentation), it does. But in a wrong way. Assuming ϕ ∈ Reals makes PlanarAngle think it’s complex.
3, 4, 5: Refine and Simplify are ways to force real and equal results.
At least, all the results seem to be equal. We can prove that all results after the first call are equal for 0 < ϕ < 2 π and unequal to π/2.

PlanerAngle does correct calculation

As you can see from the following picture my example is based on a 1st-quarter unit square rotated around the origin by ϕ. The angel α is the angle at the original upper left corner between the halflines to the rotated upper left corner and the rotated lower right corner.
Here are some examples for 0 < ϕ < 2 π :
All cases are correct, even for large ϕ.

Version