Re: Complex Oddity

*To*: mathgroup at smc.vnet.net*Subject*: [mg57475] Re: Complex Oddity*From*: "Carl K. Woll" <carlw at u.washington.edu>*Date*: Sun, 29 May 2005 01:03:32 -0400 (EDT)*Organization*: University of Washington*References*: <d79enu$lbl$1@smc.vnet.net>*Sender*: owner-wri-mathgroup at wolfram.com

John, See below. "John Reed" <nospamjreed at alum.mit.edu> wrote in message news:d79enu$lbl$1 at smc.vnet.net... >I was trying to separate the real and imaginary parts of a complicated > expression, and ended up with something strange. Here is a short version > of > what happened. > > Let z = x + I y, then realPart = z /. {Complex[a_,b_]->a} gives realPart = > x. Great! > > Now, try imagPart = z /. {Complex[a_,b_]->b} returns with imagPart = x + > y. > Oops > Whenever you have unexpected output, it's usually a good idea to use FullForm on your input to see if it matches what you expect. In[4]:= FullForm[x + I y] Out[4]//FullForm= Plus[x, Times[Complex[0, 1], y]] Now, you can see the problem, and why your expected output doesn't occur. Basically, you shouldn't think of Complex[a,b] as an expression with head Complex and a and b as it's arguments. Rather, Complex[a,b] is only defined when a and b are numbers, and even then it isn't an expression, its an atom. > In my original expression, it was harder to see, but the same error was > occuring. What I tried first was using Re[z] and Im[z], but then I have > to > work with Im[y] and Im[x]. It seems to me two things need to be done > here. > First, be able to assign variables so that they always stay real or else > indicate an error is occuring if they turn out to be complex, and second > do > something to avoid errors like the above. I have to say that I don't > trust > Mathematica's answers as much as I did before this came up. Now I feel > like > I better have a good idea of what the answer is before I trust > Mathematica. > The function you should look into is ComplexExpand. ComplexExpand assumes that variables are real and performs simplifications that are possible in that case. In[8]:= ComplexExpand[Re[x + I y]] ComplexExpand[Im[x + I y]] Out[8]= x Out[9]= y > John Reed > Carl Woll