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:= res1 = Re[1/a] only gives 1 Out= Re[-] and similarly a In:= res2 = Re[(a + z)^2] only gives 2 Out= Re[(a + z) ] Pretty dull ! BUT, with the algebra`ReIm` package loaded the inputs are expanded (simplified ?). i.e., In:= Needs["algebra`ReIm`"]; In:= res1 now gives Re[a] Out= --------------- and 2 2 Im[a] + Re[a] In:= res2 now gives 2 2 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., In:= CleanSlate["algebra`ReIm`"]; (* Note: this just gets rid of the algebra`ReIm` package *) In:= Im[a]^=0; (* declares a as real *) In:= res1 again gives 1 Out= Re[-] and a In:= res2 again gives 2 Out= Re[(a + z) ] With the algebra`ReIm` package loaded the 'realness' of a is taken into account In:= Needs["algebra`ReIm`"] (* load the package again *) In:= res1 1 Out= - a In:= res2 2 2 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 Out= 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:= Needs["algebra`AbsArg`"] Then, Abs also produces expansions and takes into account properties of symbols. In:= Abs[a] now gives 2 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; In:= Abs[a] Out= a That was a trivial example, here something more substancial. With no knowledge about x and y In:= Abs[ x y] gives 2 2 Out= Sqrt[(Im[y] Re[x] + Im[x] Re[y]) + (-(Im[x] Im[y]) + Re[x] Re[y]) ] With a few declared properties In:= 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 Out= x In:= Im[x] Out= 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.