4.5 Complex Analysis

4.5.1 A tutorial

The package in the file ComplexRules.m defines three objects:

ComplexRules,    transformation rules
ComplexCoordinates,     a function that applies rules to an expression.
ComplexD[],     takes complex derivatives.

The ComplexRules package is for handling complex algebra and differentiation. The algebra part of ComplexRules has been pretty much superceeded by the standard Mathematica command ComplexExpand[] so we advise using that. Our complex differentiation is still quite useful. ComplexRules.m may not work well with ReIm.m, see the warning at the end of this note.

In[1]:= <<ComplexRules‘  
In[2]:= y = Re[(e + w z )ˆ2]ˆ2  
 
                    2 2  
Out[2]= Re[(e + w z) ]

To rewrite this in terms of variables and their conjugates, apply the list of rules ComplexRules as follows

In[3]:= y //. ComplexRules  
 
                  2                                             2 2  
        ((e + w z)  + (Conjugate[e] + Conjugate[w] Conjugate[z]) )  
Out[3]= -----------------------------------------------------------  
                                     4

You can get the same result with the function ComplexCoordinates[]:

In[4]:= ComplexCoordinates[y]  
 
                 2                                             2 2  
        ((e + w z)  + (Conjugate[e] + Conjugate[w] Conjugate[z]) )  
Out[4]= -----------------------------------------------------------  
                                     4  

Suppose that you know that in the expression above, e ranges in the unit circle of the complex plane, and that w is real. To simplify you can do this:

In[5]:= % /. {Conjugate[e]->1/e,Conjugate[w]->w}  
 
                  2    1                  2 2  
        ((e + w z)  + (- + w Conjugate[z]) )  
                       e  
Out[5]= -------------------------------------  
                          4

Complex derivatives are easy to produce with ComplexD[]:

 
In[6]:= ComplexD[ y , z]  
 
                              2  
Out[6]= w (e + w z) ((e + w z)  
                                                    2  
        + (Conjugate[e] + Conjugate[w] Conjugate[z]) )  

Here is a differentiation with respect to Conjugate[w]:

 
In[7]:= ComplexD[ y , Conjugate[w]]  
 
Out[7]= Conjugate[z] (Conjugate[e] + Conjugate[w] Conjugate[z])  
 
               2                                             2  
>    ((e + w z)  + (Conjugate[e] + Conjugate[w] Conjugate[z]) )  

A mixed second order partial derivative is shown below:

 
In[8]:= ComplexD[ y , Conjugate[z] , z]  
 
Out[8]= 2 w (e + w z) Conjugate[w]  
 
>    (Conjugate[e] + Conjugate[w] Conjugate[z])  

Repeated differentiation is also possible:

 
In[9]:= ComplexD[ y , {Conjugate[z],2}]  
 
                      2                                           2  
Out[9]= 2 Conjugate[w]  (Conjugate[e] + Conjugate[w] Conjugate[z])  +  
 
                 2           2  
>    Conjugate[w]  ((e + w z)  + (Conjugate[e] + Conjugate[w]  
 
                          2  
>    Conjugate[z]) )

Finally, we point out that it is possible that applying ComplexRules to an expression and applying ComplexCoordinates to it may yield different output (the same mathematically of course). Reason: ComplexCoordinates applies ComplexRules to the expression, in addition to a rule for transforming Abs[z] into Sqrt[ z Conjugate[z]]. Example:

In[10]:= Abs[zˆ2 + 1]ˆ2 //. ComplexRules  
 
                  2 2  
Out[10]= Abs[1 + z ]  
 
In[11]:= ComplexCoordinates[ % ]  
 
                 2                   2  
Out[11]= (1 + z ) (1 + Conjugate[z] )

ComplexD[] handles Abs[]2 etc.:

 
In[12]:= ComplexD[ Abs[zˆ2 + 1]ˆ2,z]  
 
                              2  
Out[12]= 2 z (1 + Conjugate[z] )  

ComplexD[] also handles Abs[]1 but the answer does not look as pretty:

In[13]:= ComplexD[ Abs[zˆ2 + 1],z]  
 
                                  2  
               z (1 + Conjugate[z] )  
Out[13]= ----------------------------------  
                    2                   2  
       Sqrt[(1 + z ) (1 + Conjugate[z] )]  

WARNING: The standard Mathematica package ReIm.m sets things so that expressions of complex variables “z” are rewritten in terms of Re[z], Im[z] (for example).

Compare this to the output of functions in the package ComplexRules.m, where the expressions of complex variables “z” are given in terms of z, Conjugate[z].

You may load both ReIm.m and ComplexRules.m, but keep in mind that the objectives of the packages conflict. Furthermore, programs that need ComplexRules to run will sometimes not work if ReIm.m has been loaded.

Mathematica can manipulate complex analysis via X + I Y where X and Y are commutative (e.g., numbers). However, it is often more convenient to calculate in terms of z and the conjugate of z. We implement a few commands in the file NCComplex.m. We discuss these commands below. One may also look at the file NCComplex.m for further documentation.

4.5.2 ComplexRules

Aliases: None
Description: ComplexRules is a set of replacement rules for writing expressions in terms of the variables and their complex conjugates. For example, use this with input containing numbers and variables, as well as operators/functions such as + - * , Re[], Im[], Conjugate[], Exp[], Power[], Sin[], Cos[] and others. Apply the command expr//.ComplexRules. Try the following example:
Re[(1 + zw)2]2 //.ComplexRules
Arguments: None
Comments / Limitations: This only works for expressions defined with the commutative multiplication.

4.5.3 ComplexCoordinates[expr]

Aliases: None
Description: ComplexCoordinates[expr] expands expr in terms of the variables and their complex conjugates. The difference between ComplexCoordinates[expr] and ComplexRules is in the case Abs[z]2//.ComplexRules. This case returns the same expression instead of z and Conjugate[z]. If you desire to use the latter expression, you can use ComplexCoordinates[expr]. This function replaces Abs[z] by Sqrt[z Conjugate[z]], after applying ComplexRules.
Arguments: expr is any expression with + - * , Re[], Im[], Conjugate[], Exp[], Power[], Sin[], Cos[] and others
Comments / Limitations: This only works for expressions defined with the commutative multiplication.

4.5.4 ComplexD[expr, aVariable]

Aliases: None
Description: ComplexD[expr,aV ariable] calculates the derivative of the complex expression expr with respect to the “complex” variable aV ariable. You can also calculate the derivative with respect to Conjugate[aV ariable]. Try these examples:

ComplexD[Conjugate[Exp[z + 1∕Conjugate[z]]2],z];
ComplexD[Re[(1 + zw)2]2,w];
ComplexD[Abs[1(e2 - 1) - z]2,z];
ComplexD[Conjugate[Exp[z + 1∕Conjugate[z]]2],Conjugate[z]];

Here is a second order derivative:

ComplexD[Conjugate[Exp[z + 1∕Conjugate[z]]2,z, 2];
Arguments: expr is a complex expression. aV ariable is the variable in which to take the derivative with respect to.
Comments / Limitations: This only works for expressions defined with the commutative multiplication.