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: [mg42030] RE: [mg41983] Extracting Re and Im parts of a symbolic expression
  • From: "David Park" <djmp at earthlink.net>
  • Date: Tue, 17 Jun 2003 05:42:55 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

Carlos,

What's wrong with

Re[x + I y]
% // ComplexExpand
-Im[y] + Re[x]
x

Still, the following usually doesn't do what you want.

Abs[x + I y]
% // ComplexExpand
Abs[x + I*y]
Abs[x + I*y]

The solution is to set the TargetFunctions in ComplexExpand.

Abs[x + I y]
% // ComplexExpand[#, TargetFunctions -> {Re, Im}] &
Abs[x + I*y]
Sqrt[x^2 + y^2]

I found it convenient to define the following function.

ReImExpand[expr_] := ComplexExpand[expr, TargetFunctions -> {Re, Im}]
ReImExpand[expr_, complexlist_] :=
  ComplexExpand[expr, complexlist, TargetFunctions -> {Re, Im}]

Then..

Abs[x + I y]
% // ReImExpand
Abs[x + I*y]
Sqrt[x^2 + y^2]

David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/



From: Carlos Felippa [mailto:carlos at colorado.edu]
To: mathgroup at smc.vnet.net

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?


  • Prev by Date: Re: Extracting Re and Im parts of a symbolic expression
  • Next by Date: Re: loading packages
  • 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