Extracting Re and Im parts of a symbolic expression
- To: mathgroup at smc.vnet.net
- Subject: [mg41983] Extracting Re and Im parts of a symbolic expression
- From: carlos at colorado.edu (Carlos Felippa)
- Date: Mon, 16 Jun 2003 03:56:31 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
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?
- Follow-Ups:
- Re: Extracting Re and Im parts of a symbolic expression
- From: Daniel Lichtblau <danl@wolfram.com>
- Re: Extracting Re and Im parts of a symbolic expression