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: [mg42027] Re: Extracting Re and Im parts of a symbolic expression
  • From: bobhanlon at aol.com (Bob Hanlon)
  • Date: Tue, 17 Jun 2003 05:42:49 -0400 (EDT)
  • References: <bcjtkc$hnj$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

z=x+I*y;

Simplify[
  {Re[z], Im[z]}, 
  Element[{x,y}, Reals]]

{x, y}

Simplify[
  {Re[z], Im[z]}, 
  Im[x]==0 && Im[y]==0]

{x, y}

ComplexExpand[{Re[z], Im[z]}]

{x, y}

Needs["Algebra`ReIm`"];

x /: Im[x]=0;
y /: Im[y] = 0;

{Re[z], Im[z]}

{x, y}


Bob Hanlon

In article <bcjtkc$hnj$1 at smc.vnet.net>, carlos at colorado.edu (Carlos Felippa)
wrote:

<< Subject:	Extracting Re and Im parts of a symbolic expression
From:		carlos at colorado.edu (Carlos Felippa)
To: mathgroup at smc.vnet.net
Date:		Mon, 16 Jun 2003 08:03:24 +0000 (UTC)

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?
 >><BR><BR>


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