MathGroup Archive 2003

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Extracting Re and Im parts of a symbolic expression

  • To: mathgroup at smc.vnet.net
  • Subject: [mg42053] Re: [mg41983] Extracting Re and Im parts of a symbolic expression
  • From: Daniel Lichtblau <danl at wolfram.com>
  • Date: Tue, 17 Jun 2003 05:43:52 -0400 (EDT)
  • References: <200306160756.DAA17929@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

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?

The URLs below go to threads that discuss various aspects of this.
Possibly one of the approaches therein will work well for your purposes.

http://forums.wolfram.com/mathgroup/archive/1998/May/msg00158.html
http://forums.wolfram.com/mathgroup/archive/1998/May/msg00213.html

Daniel Lichtblau
Wolfram Research


  • Prev by Date: RE: loading packages
  • Next by Date: Re: Extracting Re and Im parts of a symbolic expression
  • Previous by thread: Extracting Re and Im parts of a symbolic expression
  • Next by thread: Re: Extracting Re and Im parts of a symbolic expression