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
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)
Tel: 0272 228109
Fax: 0272 236091
email: simonc at bri.hp.com
Complex Algebra with Mathematica
What's the problem ?
The built in functions Re, Im, Conjugate, Abs and Arg evaluate for
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.
res1 = Re[1/a] only gives
Out= Re[-] and similarly
res2 = Re[(a + z)^2] only gives
Out= Re[(a + z) ]
Pretty dull !
BUT, with the algebra`ReIm` package loaded the inputs are expanded
(simplified ?). i.e.,
In:= res1 now gives
Out= --------------- and
Im[a] + Re[a]
In:= res2 now gives
Out= -(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.,
(* Note: this just gets rid of the algebra`ReIm` package *)
In:= Im[a]^=0; (* declares a as real *)
In:= res1 again gives
Out= Re[-] and
In:= res2 again gives
Out= Re[(a + z) ]
With the algebra`ReIm` package loaded the 'realness' of a is taken
In:= Needs["algebra`ReIm`"] (* load the package again *)
Out= -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:= Abs[a] gives
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).
Then, Abs also produces expansions and takes into account properties
In:= Abs[a] now gives
Out= 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:= Positive[a] ^= True;
That was a trivial example, here something more substancial. With no
knowledge about x and y
In:= Abs[ x y] gives
Out= Sqrt[(Im[y] Re[x] + Im[x] Re[y]) + (-(Im[x] Im[y]) + Re[x] Re[y]) ]
With a few declared properties
Positive[x] ^= True;
Negative[y] ^= True;
this simplifies to
In:= Abs[ x y]
Out= -(x y)
Note that using Positive[x], declares x as both real and > 0, so
In:= Re[x] now gives
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
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:
Previous by thread:
v2.2.2 for Windows and remote kernels
Next by thread: