       Re: Algebra on complex expressions: Collect

• To: mathgroup at smc.vnet.net
• Subject: [mg15244] Re: [mg15225] Algebra on complex expressions: Collect
• From: Andrzej Kozlowski <andrzej at tuins.ac.jp>
• Date: Sun, 27 Dec 1998 03:58:35 -0500
• Sender: owner-wri-mathgroup at wolfram.com

```I am not quite sure why you find the existing Re and Im functions
unsatisfactory, but here are a few comments which may be helpful. I
assume you want to be able to deal both with numberic complex numbers,
like 2+3I and symboloc ones like a+b*I, where a and b are meant to be
real. It is useful first to load the package

<<Algebra`ReIm`

This make sit possible to declare certain symbols to be real, e.g.:
a/:Im[a]=0;b/:Im[b]=0;c/:Im[c]=0;d/:Im[d]=0; declares a, b, c, d to be
real.

Mathematica can now find real and imaginary parts of symboloc
expressions like:

In:=
Im[(a+b*I)/(c+d*I)]
Out=
b c       a d
------- - -------
2    2    2    2
c  + d    c  + d

You can if you want define your own complexcollect function:

In:=
complexcollect[x_]:=Re[x]+I*Im[x]

This will work well in the above example:

In:=
complexcollect[(a+b*I)/(c+d*I)]
Out=
a c       b d          b c       a d
------- + ------- + I (------- - -------)
2    2    2    2       2    2    2    2
c  + d    c  + d       c  + d    c  + d

but now always, e.g.

In:=
complexcollect[3+4I+Pi+E*I]
Out=
3 + I (4 + E) + Pi

The point is that Mathematica will order the symbols according to its
built in ordering principle and, depending on the names of your
symbols, the real numbers may be separated by the complex part. Of
course you can make things look right by choosing the letters you use
for your real and imaginary parts carefully, but sometimes, as in the
above example, you have no choice! I do not know of any simple way of
solving this "problem", if it is a problem, though it would be easy
enough to do if you were only concerned with the expression "looking
right".

Finally, if you decide to use ComplexExpand you will get:

In:=
ComplexExpand[(a+b*I)/(c+d*I)]
Out=
a c            I b c           I a d            b d
------------- + ------------- - ------------- + -------------
2               2               2               2
Abs[c + I d]    Abs[c + I d]    Abs[c + I d]    Abs[c + I d]

Abs disregards the fact that we declared c and d to be real. One can
modify Abs to pay attention to this:

In:=
Unprotect[Abs]
Abs[a_+b_*I]/;(Im[a]==0&&Im[b]==0):=Sqrt[a^2+b^2] Protect[Abs]

Now,

In:=
ComplexExpand[(a+b*I)/(c+d*I)]
Out=
a c      I b c     I a d      b d
------- + ------- - ------- + -------
2    2    2    2    2    2    2    2
c  + d    c  + d    c  + d    c  + d

I hope this is of some help. If any one knows of a better way to answer
this question I would like to hear of it myself

On Wed, Dec 23, 1998, Ross, Sean <rosss at plk.af.mil> wrote:

D>Does anyone know a way to have a Collect -like function work on
>expressions with complex numbers?  If I have an expression with "x" in
>it, I can use Collect[expr,x], but if I have an expression with "I" in
>it, Collect[expr,I] does not separate the expression into a part with
>and without "I" which would be equivalent to separating the expression
>into real and imaginary parts given all symbols were real.
>
>The RealOnly package does not seem to apply as it excludes imaginary
>numbers alltogether.  ComplexExpand results in a ridiculous amount of
>complexity and is worse than nothing for this purpose.  Expanding and
>multiplying numerator and denominator separately by the complex
>conjugate of the denominator is tedious to do manually and results in
>the same problem of no way to neatly separate the real and imaginary
>parts of a symbolic expression given that all symbols are real.
>
>Thanks.
>
>Sean Ross
>
>Please reply to rosss at plk.af.mil as I no longer subscribe to the
>mathgroup.
>

Andrzej Kozlowski
Toyama International University
JAPAN
http://sigma.tuins.ac.jp/
http://eri2.tuins.ac.jp/

```

• Prev by Date: List of Matrices
• Next by Date: RE: Getting rid of annoying zeroes in algebraic expressions
• Previous by thread: Re: Algebra on complex expressions: Collect
• Next by thread: Re: Algebra on complex expressions: Collect