       Re: (1) Symbolic complex algebra in mathematica

• To: mathgroup at yoda.ncsa.uiuc.edu
• Subject: Re: (1) Symbolic complex algebra in mathematica
• From: CAMERON at midd.cc.middlebury.edu
• Date: Mon, 26 Nov 90 13:04 EDT

```In a recent posting, Ken Yip (yip-ken at CS.YALE.EDU) said:

> I want to define a complex quantity "z = x + I y" so that
>
>         Re[z] ==> x
>         Im[z] ==> y
>         Conjugate[z] ==> x - I y
>
> etc.
> Mathematica doesn't seem to know how to do this even after I
> define x and y to be real by "x/: RealQ[x] = True".
> I tried to define some transformation rules, but then I couldn't find
> a way to specify the pattern variable to be of type real AND symbol.

(I took the liberty of reformatting.)

This calls for an answer in three parts:

> I want to define a complex quantity "z = x + I y" so that (...) etc.

I have attached a tiny package below that does what is requested here,
and a bit more.  I wrote it some time ago for a friend.  It relies
on two other packages, "ReIm.m" and "Trigonometry.m", both of which
are part of the standard Mma 1.2 distribution (in the "Algebra"
subdirectory of the "Packages" directory).  All I do is add a couple
of extra simplification rules to the ones in those two packages, in order
to cover a couple of cases that they seem to have missed.  I also define
a function "makereal" that makes it easy to declare a variable to be
real.  I've included a short session transcript below to illustrate
what the package does.

For the more advanced Mma users out there, I might point out that
the only tricky part of writing this was avoiding the Mma bug that
makes "Sqrt[a^2]" simplify to "a" for all expressions "a".
This bug bit me in a preliminary version of the simplification rule
for Abs.  I had to add the condition "imagpart =!= 0" to
that rule to keep Abs from disappearing around variables that had been
declared to be real via "makereal".  This happened because "Abs[a]"
got rewritten to "Sqrt[ Re[a]^2 + Im[a]^2 ]", which then became
"Sqrt[a^2]" (because if "a" is real then "Re[a]" is just "a" and
"Im[a]" is 0), and this in turn got rewritten (incorrectly!) to "a".
The extra condition prevents this rewriting rule for Abs from being
applied to quantities that are already known to be real.

> Mathematica doesn't seem to know how to do this even after I
> define x and y to be real by "x/: RealQ[x] = True".

"RealQ" is no longer used for this purpose, and hasn't been for ages
-- ever since the release of Mma 1.2 in August of 1989.  The function
"makereal" in the package below does it in the currently approved
fashion: "makereal[x]" results in "Re[x] ^:= x" and "Im[x] ^= 0".
If you are still using Mma 1.1 or earlier then you might want to
consider upgrading, as you are forgoing much functionality and
enduring many bugs that have been fixed.  (In fact, the code below
won't work in 1.1, although it could be rewritten to do so -- which
wouldn't help, since it relies on the 1.2 versions of "ReIm.m" and
"Trigonometry.m".)

> I tried to define some transformation rules, but then I couldn't find
> a way to specify the pattern variable to be of type real AND symbol.

If "RealQ" *were* still being used for this purpose, a pattern variable
could be restricted to match only real symbols by a pattern of the
form "x_Symbol?RealQ".  We can define "RealQ" and use it this way --
here's an example:

RealQ[x_] := Im[x]===0

foo[x_Symbol?RealQ] := StringJoin[ToString[x]," is a real symbol."]

foo[x_Symbol] := StringJoin[ToString[x]," is not known to be real."]

foo[x_] = "foo[] tests symbols only!";

Typing this into Mma gives the following results:

In:= makereal[a]

In:= foo[a]

Out= a is a real symbol.

In:= foo[z]

Out= z is not known to be real.

In:= foo[2+I]

Out= foo[] tests symbols only!

Here's the package (I call it "extraReIm.m"):
--------------------------------------------------------------------------

Needs["Algebra`ReIm`"]

Begin["Algebra`ReIm`Private`"]

`z

`realpart

`imagpart

Unprotect[Abs,Conjugate]

Conjugate[z_] := Block[ {realpart=Re[z],imagpart=Im[z]},
realpart - I imagpart /;
]

Abs[z_] := Block[ {realpart=Re[z],imagpart=Im[z]},
Sqrt[ realpart^2 + imagpart^2 ] /;
]

Protect[Abs,Conjugate]

`y

Algebra`ReIm`makereal

makereal[z_] := ( Re[z] ^:= z ; Im[z] ^= 0 ; )

makereal[z__] := ( makereal /@ {z} ; )

makereal[] = Null

End[]

Needs["Algebra`Trigonometry`"]

Begin["Algebra`Trigonometry`Private`"]

`x

`y

Unprotect[ComplexToTrig]

ComplexToTrig[ Exp[x_+y__] ] := ComplexToTrig[Exp[x]] ComplexToTrig[Exp[y]]

Protect[ComplexToTrig]

End[]

Null
--------------------------------------------------------------------------

Here's a sample session to show what it does.
First, in In, I load the package.
Next, in In, I define "z" to be the expression "x + I y".
In and In show what Conjugate and Abs do if they don't
know that "x" and "y" are supposed to be real.
In uses "makereal" to make "x" and "y" act like reals.
In and In show that Conjugate and Abs now do the right things.
In and In show that complex exponentials can be converted to
trigonometric expressions (this was a requirement of the friend for
whom I wrote the code) and In--In show that the resulting
expressions interact nicely with Re and Im (if the constituent
variables are known to be real).
--------------------------------------------------------------------------
Mathematica (NeXT) 1.2 (January 20, 1990) [With pre-loaded data]
by S. Wolfram, D. Grayson, R. Maeder, H. Cejtin,
S. Omohundro, D. Ballman and J. Keiper
with I. Rivin and D. Withoff

In:= << extraReIm.m

In:= z = x + I y

Out= x + I y

In:= Conjugate[z]

Out= Conjugate[x] + -I Conjugate[y]

In:= Abs[z]

2                  2
Out= Sqrt[(-Im[y] + Re[x])  + (Im[x] + Re[y]) ]

In:= makereal[x,y]

In:= Conjugate[z]

Out= x + -I y

In:= Abs[z]

2    2
Out= Sqrt[x  + y ]

In:= ComplexToTrig[ E^(3 I k d) ]

Out= Cos[3 d k] + I Sin[3 d k]

In:= result = ComplexToTrig[ E^(2 I k d + I omega t) ]

Out= (Cos[2 d k] + I Sin[2 d k]) (Cos[omega t] + I Sin[omega t])

In:= makereal[d,k,omega,t]

In:= Re[result]

Out= Cos[2 d k] Cos[omega t] - Sin[2 d k] Sin[omega t]

In:= Im[result]

Out= Cos[omega t] Sin[2 d k] + Cos[2 d k] Sin[omega t]

In:= Quit

--------------------------------------------------------------------------

Hope this is useful.

--Cameron Smith
Mathematica Consultant
CAMERON at MIDD.BITNET  --or--  cameron at midd.cc.middlebury.edu

```

• Prev by Date: Re: Mathematica for NeXT, Unbundled Price
• Next by Date: a==b and FixedPoint
• Previous by thread: Re: Symbolic complex algebra in mathematica
• Next by thread: a==b and FixedPoint