MathGroup Archive 1994

[Date Index] [Thread Index] [Author Index]

Search the Archive

re: Specifying rules OR "How to do complex math in Mathematica"

  • To: mathgroup at yoda.physics.unc.edu
  • Subject: re: Specifying rules OR "How to do complex math in Mathematica"
  • From: Simon Chandler <simonc at hpcpbla.bri.hp.com>
  • Date: Mon, 27 Jun 1994 13:15:00 +0100

27/6/94

Dear MathGroup,

Marnix Van Daele writes

>Hello, I have a problem with complex numbers !

> I want to tell Mma that a is real, such that
> Im[a+I] will give 1,
> but I do not get this result.

> I have tried

> a /: Im[a]=0

> Can anyone help me ?

There have been several replies already, suggesting the package
Algebra`ReIm` is the answer - which indeed it is to the question
asked.  Here is a more general description of how to 'teach'
Mathematica about complex maths, including the functions Abs and Arg.
I wrote the notes as a reminder for myself, so forgive me if they're a
bit patronizing or cynical.

Dr Simon Chandler
Hewlett-Packard Ltd (CPB)
Filton Road
Stoke Gifford
Bristol
BS12 6QZ

Tel:   0272 228109
Fax:   0272 236091
email: simonc at bri.hp.com

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

10/3/94

Complex Algebra with Mathematica
================================

What's the problem ?
--------------------
The built in functions Re, Im, Conjugate, Abs and Arg evaluate for
numbers only.

What's the solution ?
---------------------
The package algebra`ReIm` extends Re, Im and Conjugate to symbols and
expressions. Another package, AbsArg.m, extends Abs and Arg in
addition to Re, Im and Conjugate.

What does all this mean ?
-------------------------
Firstly, the raw Mathematica kernel is pretty stupid at doing complex
algebra of anything that contains symbols rather than just numbers.
For example...

In[1]:=
	res1 = Re[1/a]			only gives

	   1
Out[1]=	Re[-]				and similarly
	   a

In[2]:=
	res2 = Re[(a + z)^2]		only gives

	          2
Out[2]=	Re[(a + z) ]

Pretty dull !

BUT, with the algebra`ReIm` package loaded the inputs are expanded
(simplified ?). i.e.,

In[3]:= Needs["algebra`ReIm`"];

In[4]:=	res1				now gives

	     Re[a]
Out[4]=	---------------			and
	     2        2
	Im[a]  + Re[a]


In[5]:=	res2				now gives


               		2                  2
Out[5]=	-(Im[a] + Im[z])  + (Re[a] + Re[z])


Mathematica does not know a priori if a symbol stands for a real or a
complex number, so is assuming that both parts may be present. A
symbol can be declared as real by:

z /: Im[a] = 0;

or, equivalently, by:

Im[z] ^= 0;

If the symbol has been declared in this way then raw Mathematica
(without the algebra`ReIm` package) still can't do complex algebra.
So, again, you'd get the dull results. i.e.,

In[6]:=	CleanSlate["algebra`ReIm`"];
	(* Note: this just gets rid of the algebra`ReIm` package *)

In[1]:=	Im[a]^=0;  (* declares a as real *)

In[2]:=	res1				again gives

   	   1
Out[2]= Re[-]				and
   	   a

In[3]:=	res2				again gives

                  2
Out[3]= Re[(a + z) ]


With the algebra`ReIm` package loaded the 'realness' of a is taken
into account

In[4]:= Needs["algebra`ReIm`"] 		(* load the package again *)

In[5]:= res1

	1
Out[5]= -
	a

In[6]:= res2

              2              2
Out[6]= -Im[z]  + (a + Re[z])

Even with the algebra`ReIm` package, Mathematica doesn't know how to
simplify expressions involving Abs[] and Arg[].  For example

In[7]:= Abs[a]			gives

Out[7]= Abs[a]

To expand Mathematica's knowledge about complex arithmetic to take the
Abs[] and Arg[] functions into account you must load another package,
algebra`AbsArg` (actually the 'teaching process' is done in one step
by loading this package, since it automatically loads algebra`ReIm`
too - see my summary below).

In[8]:= Needs["algebra`AbsArg`"]

Then, Abs[] also produces expansions and takes into account properties
of symbols.

In[9]:= Abs[a]			now gives

              2
Out[9]= Sqrt[a ]

Further simplification may be possible if information about the
positivity of symbols is given. This can be done with the functions
Positive, Negative and NonNegative (or with NonNegativeQ from the
package algebra`NonNegativeQ`, which is also loaded-in automatically
when you load in algebra`AbsArg` )

In[10]:= Positive[a] ^= True;

In[11]:= Abs[a]

Out[11]= a


That was a trivial example, here something more substancial.  With no
knowledge about x and y

In[12]:= Abs[ x y]	gives

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

With a few declared properties

In[13]:=
	Positive[x] ^= True;
	Negative[y] ^= True;

this simplifies to

In[15]:= Abs[ x y]

Out[15]= -(x y)

Note that using Positive[x], declares x as both real and > 0, so

In[16]:= Re[x]		now gives

Out[16]= x

In[17]:= Im[x]

Out[17]= 0

Summary
-------
The bottom line is: if you want to do any complex algebra with
Mathematica then you must teach it how to.  Just load in the package
algebra`AbsArg` with

	Needs["algebra`AbsArg`"]

Not only does this teach Mathematica about Abs and Arg, it also loads
in the algebra`ReIm` package, so teaching it about Re and Im etc.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Here endeth the first lesson.








  • Prev by Date: Re: Inequalities into Standard Form
  • Next by Date: Timing[10000!;]
  • Previous by thread: v2.2.2 for Windows and remote kernels
  • Next by thread: Timing[10000!;]