21

For some reason Mathematica does not properly simplify this expression:

In[7]:= FullSimplify[ArcTan[-Re[x + z], y], (x | y | z) \[Element] Reals]
Out[7]= ArcTan[-Re[x + z], y]

Obviously, if x and z are real, then so is x+z, so Re[x + z] should be replaced by x + z. Strangely enough, dropping any small part of the input fixes the problem, here are some examples.
No minus sign:

In[8]:= FullSimplify[ ArcTan[Re[x + z], y], (x | y | z) \[Element] Reals]
Out[8]= ArcTan[x + z, y]

No z:

In[9]:= FullSimplify[ArcTan[-Re[x], y], (x | y | z) \[Element] Reals]
Out[9]= ArcTan[-x, y]

No y:

In[10]:= FullSimplify[ArcTan[-Re[x + z]], (x | y | z) \[Element] Reals]
Out[10]= -ArcTan[x + z]

Of course I can just drop the Re function manually, but this is just a small fragment of the actual expression I'm trying to simplify, and I would like to avoid going though the whole expression looking for this specific pattern.
Anyone knows how to fix this? Is this a bug or what? (I'm using version 8.0.4.0)

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
Joe
  • 1,471
  • 10
  • 23
  • It also doesn't work with FullSimplify[{-Re[x + z], 0}, (x|z) \[Element] Reals]. – celtschk Apr 11 '12 at 14:27
  • 1
    Simplify[Re[x + z], Assumptions -> {x \[Element] Reals, y \[Element] Reals, z \[Element] Reals}] does give x+z so it has to do with ArcTan – b.gates.you.know.what Apr 11 '12 at 14:27
  • ComplexExpand doesn't drop it either. – Szabolcs Apr 11 '12 at 14:37
  • FullSimplify[ArcTan[-(y/Re[x + z])], Assumptions -> {x + z > 0, y [Element] Reals}] also works. – b.gates.you.know.what Apr 11 '12 at 14:37
  • @celtschk - you're right, the ArcTan is redundant – Joe Apr 11 '12 at 14:44
  • @b.gatessucks - yes that works, but you loose the functionality of the two-argument ArcTan, which takes into account which quadrant the point (x+z,y) is in. – Joe Apr 11 '12 at 14:47
  • @b.gatessucks: Note that my example has no ArcTan and yet is not simplified. This clearly shows that the problem is not specifically with ArcTan but with any expression with two (and probably also more) arguments. It doesn't matter if the head of the expression is ArcTan, f or List. As soon as there are two arguments, -Re[x+z] is no longer simplified (also note that I completely removed y in my example). – celtschk Apr 11 '12 at 14:54
  • @celtschk I think the simplification does occur if one lists all the assumptions; I agree it does not if one uses "|" to specify the assumptions. – b.gates.you.know.what Apr 11 '12 at 15:03
  • @b.gatessucks: I just tried FullSimplify[{-Re[x + z], 0}, x \[Element] Reals && z \[Element] Reals] and it doesn't get simplified either. To achieve maximal separation of assumptions, I even tried Assuming[x \[Element] Reals,Assuming[z \[Element] Reals,FullSimplify[{-Re[x + z], 0}]]], again no simplification happens. – celtschk Apr 11 '12 at 15:10
  • I suspect the ArcTan problem arises due to the possibility of an indeterminate answer: Re[x/z] won't simplify further, either. Regardless, it is instructive to examine lots of simple cases at once, as in ClearAll[x, y, z]; FullSimplify[#, (x | z) \[Element] Reals ] & /@ { Re[x + z], - Re[x + z], -y Re[x + z], -Re[x - z], -Re[x z], Re[x / z], -Re[ x z^2], Re[ ArcTan[x, z]], -Re[-Max[x, z]], -Re[Exp[x + z]], -Re[ Log[Exp[x + z]]], -Re[Exp[x]/Exp[z]]} – whuber Apr 11 '12 at 19:03
  • @whuber It doesn't seem to be ArcTan problem at all. If you set ArcTan[-1.0 Re[x + z], y] instead of ArcTan[- Re[x + z], y] it works fine. See my answer for a more throughout discussion. – Artes Apr 11 '12 at 19:23
  • @Artes I did read your reply before posting. Did you try out my suggestion? It shows there is a problem with ArcTan even in the absence of any multiplications by -1. Incidentally, ArcTan[-1.0 Re[x + z], y] does not correctly simplify for me: I'm still running version 8.0.0.0. Maybe we're witnessing some version differences? – whuber Apr 11 '12 at 22:27
  • @whuber It seems the case here, although it is strange enough, because I'm using ver. 8.0.4 but I checked the results as well as in ver. 7.0.1 and haven't detected any problems with ArcTan. – Artes Apr 11 '12 at 22:34

2 Answers2

22

The problem is due to Mathematica thinking that the version with the Re[] is actually simpler. This is because the default complexity function is more or less LeafCount[], and

In[332]:= ArcTan[-Re[x+z],y]//FullForm
Out[332]//FullForm= ArcTan[Times[-1,Re[Plus[x,z]]],y]

whereas

In[334]:= ArcTan[-x-z,y]//FullForm
Out[334]//FullForm= ArcTan[Plus[Times[-1,x],Times[-1,z]],y]

Here is a function that counts leaves without penalizing negation:

In[382]:= f3[e_]:=(LeafCount[e]-2Count[e,Times[-1,_],{0,Infinity}])
{LeafCount[x],LeafCount[-x],f3[x],f3[-x]}
Out[383]= {1,3,1,1}

If you tell mathematica to simplify using this complexity function then you get the expected result:

FullSimplify[ArcTan[-Re[x+z],y],(x|y|z)\[Element]Reals,ComplexityFunction->f3]

Out[375]= ArcTan[-x-z,y]

Lev Bishop
  • 839
  • 6
  • 12
  • 1
    This is exactly the answer I got from Wolfram support. It's pretty amazing that this quirky behavior actually has an explanation. I think that in the root of this is the fact the Mathematica can't have an expression of the form -(x+y) without automatically expanding it. Having some function in the middle like -Re[x+y] allows multiplying by -1 only once. – Joe Apr 13 '12 at 11:12
  • It is a nice example showing that the concept of simplifying something is actually pretty complicated. – bill s Mar 21 '18 at 15:30
7

I think this is a bug.

Close enough expressions yield better results, e.g.

FullSimplify[ 
             ArcTan[ -# Re[x + z], y], (x | y | z) \[Element] Reals
            ] ===
             ArcTan[ -# (x + z), y] & /@
  { 1.0,   1, Sqrt[1.], Exp[0.], 1 - 0., 2, a}
{True, False, True, True, True, True, True}

The problem seems to be specific for a factor -1 before Re[x + z], other factors appear to give what we would expect. If there is ArcTan[-a Re[x + z], y] in the expression it works well. It should be noted that the same issue comes with Simplify and that the problem has nothing to do with ArcTan, because :

(FullSimplify[-# Re[x + z], (x | y | z) \[Element] Reals]
         ===  -# (x + z)) & /@ 
  { 1.0,  1, Sqrt[1.], Exp[0.], 1 - 0., 2, a}
{True, False, True, True, True, True, True}

To fix the problem you could use e.g. Refine instead of FullSimplify,

Refine[ ArcTan[ -Re[x + z], y], (x | y | z) \[Element] Reals]
ArcTan[-x - z, y]

Edit

Another way to deal with similar expressions wolud be hiding a minus sign into Re, e.g.

Re[-(x + z)] instead of -Re[x + z].

Sometimes a more flexible way would be some kind of replacement, i.e. setting ArcTan[-a Re[x + z], y]] wherever in expr one finds ArcTan[- Re[x + z], y]] and then expr /. a->1

FullSimplify[ ArcTan[ -a Re[x + z], y], (x | y | z) \[Element] Reals] /. a -> 1
FullSimplify[ArcTan[ Re[-(x + z)], y], (x | y | z) \[Element] Reals]
ArcTan[-x - z, y]
ArcTan[-x - z, y] 
Artes
  • 57,212
  • 12
  • 157
  • 245