Re: Manipulating a complex modulus expression

*To*: mathgroup at smc.vnet.net*Subject*: [mg82882] Re: Manipulating a complex modulus expression*From*: danl at wolfram.com*Date*: Fri, 2 Nov 2007 03:29:37 -0500 (EST)*References*: <fgcahl$9nh$1@smc.vnet.net>

On Nov 1, 5:41 am, car... at colorado.edu wrote: > Could somebody explain why Mathematica does not > return the same expression in the following cases. > Version: 5.2 on Mac OSX 10.4.9. > Let > > d=(Abs[-1+x+I*y])^2; > > The following 3 statements should be equivalent, since the > documentation says that ComplexExpand assumes that all > variables are real: > > s=ComplexExpand[d]; Print[s]; > s=Simplify[d,x=E2=88=88Reals&&y=E2=88=88Reals]; Print[s]; > s=FullSimplify[d,x=E2=88=88Reals&&y=E2=88=88Reals]; Print[s]; These are not "statements", in the sense of mathematics. They are lines of Mathematica code. I raise the distinction because it is not clear to me what you mean by "equivalent". In particular you might mean they should return mathematically equivalent results (they should, and do). Or you might mean they should return syntactically identical results (they do not). > All should return (-1+x)^2+y^2. I fail to see why. Or, to put it more correctly, I see a few reasons why not. > But only the last one does. > Make a slight change: > > d=1/(Abs[-1+x+I*y])^2; > > Now none of the 3 returns 1/ ((-1+x)^2+y^2) . Why? ComplexExpand has the job of returning explicitly real and imaginary parts. In this example we have just that. d = (Abs[-1 + x + I*y])^2; In[17]:= ComplexExpand[d] Out[17]= Abs[-1 + x + I*y]^2 Abs[...] is indeed explicitly real. How to get the result you have in mind, or something bearing resemblance to it? One can tell ComplexExpand to use a subset of the nonanalytic functions it normally recognizes (these being {Re,Im,Abs,Arg,Sign,Conjugate}). In particular I recommend the Cartesian functions {Re,Im}. In[3]:= ComplexExpand[d, TargetFunctions -> {Re, Im}] Out[3]= (-1 + x)^2 + y^2 One might observe that Re and Im are not actually used above. The importance, in this sort of example, is that they also tell ComplexExpand the functions NOT to use in the result, to wit, those of the full set not in the TargetFunctions list. In processing and figuring a "Cartesian" representation, the ComplexExpand code recognizes a situation for which explicit Re and Im use can be avoided. In the case of Abs being allowed, there is no cause to even look for such, because the original expression is fine as is. Let's look at Simplify and FullSimplify, avoiding unicode characters that tend to blight font encodings (and avoiding superfluous Print). In[18]:= ds = Simplify[d, Assumptions -> Element[{x, y}, Reals]] Out[18]= Abs[-1 + x + I*y]^2 In[19]:= dfs = FullSimplify[d, Assumptions -> Element[{x, y}, Reals]] Out[19]= (-1 + x)^2 + y^2 What happens here is easy to explain. And indeed it has been explained in this forum. Many times. As in "a lot". FullSimplify uses more transformations than Simplify, and hits on one that delivers a smaller leaf count. For your reciprocal example, we have similar behavior. dr = 1/d; In[30]:= ComplexExpand[dr, TargetFunctions -> {Re, Im}] Out[30]= 1/((-1 + x)^2 + y^2) In[34]:= FullSimplify[dr, Assumptions -> Element[{x, y}, Reals]] Out[34]= 1/Abs[-1 + x + I*y]^2 Why the different results? They have the same leaf count, and in that case the original generally (or maybe always) wins, in default option settings. THis can be influenced by the ComplexityFunction. One way to coerce your desired result is as below. In[41]:= FullSimplify[dr, Assumptions -> Element[{x, y}, Reals], ComplexityFunction -> (LeafCount[#] + 10*Count[#, Abs, Infinity, Heads -> True] &)] Out[41]= 1/((-1 + x)^2 + y^2) Daniel Lichtblau Wolfram Research