Re: Extracting Re and Im parts of a symbolic expression
- To: mathgroup at smc.vnet.net
- Subject: [mg42031] Re: [mg41983] Extracting Re and Im parts of a symbolic expression
- From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
- Date: Tue, 17 Jun 2003 05:42:56 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
You seem to have completely missed the point of ComplexExpand: In[1]:= z = x + I*y Out[1]= x + I*y In[2]:= ComplexExpand[Re[z]] Out[2]= x Note that CompelxExpand[Re[z]] works, Re[ComplexExpand[z]] is pointless since it is just Re[z]. Andrzej Kozlowski Yokohama, Japan http://www.mimuw.edu.pl/~akoz/ http://platon.c.u-tokyo.ac.jp/andrzej/ On Monday, June 16, 2003, at 04:56 PM, Carlos Felippa wrote: > Apologies if this topic has been posted before (I did only a > perfunctory back search of this NG). > > Is there a simple way to extract the real and imaginary part of > a complex expression, assuming *all* variables are real? For > definiteness assume x,y are reals and z = x+I*y. Then > > Re[z] gives -Im[y] + Re[x] Im[z] gives Im[x] + Re[y] > > because is no way to tell Re and Im that x,y are real. (The lack of a > variable-type global database clearly hurts here.) Here are 5 ideas. > > (1) Re[ComplexExpand[z]] Im[ComplexExpand[z]] do not work since > the "reality" effect of ComplexExpand does not propagate. > > (2) (z+Conjugate(z))/2 (z-Conjugate(z))/2 fails as expected > > (3) Coefficient[z,I] complains: I is not a variable, so lets make it > one ... > > (4) Coefficient[ComplexExpand[z]/.I->iunit,iunit] for imaginary > part > z-I*Coefficient[ComplexExpand[z]/.I->iunit,iunit] for real part > This works in the cases I tried but looks contrived. > > (5) Print ComplexExpand[z] in InputForm, do cut and paste. Works > but is time consuming (human in the loop) and error prone. > In my program x and y were actually fairly complicate > functions (one screenful each) > > Clearly missing is a ComplexExpandReIm (say) function which > assumes all variables in z are real, so that I can write > > {x,y}=ComplexExpandReIm[z] > > Of course one can define > > ComplexExpandReIm[z_]:= Module[{iunit}, > {z-I*Coefficient[ComplexExpand[z]/.I->iunit,iunit], > Coefficient[ComplexExpand[z]/.I->iunit,iunit]}]; > > to hide the ugliness of scheme (4). Any suggestions on a cleaner > method? > > >