Numpy Complex Number Argumentative Essay
This chapter is taken from the book A Primer on Scientific Programming with Python by H. P. Langtangen, 5th edition, Springer, 2016.
Complex numbers
Suppose \( x^2=2 \). Then most of us are able to find out that \( x=\sqrt{2} \) is a solution to the equation. The more mathematically interested reader will also remark that \( x=\sqrt{2} \) is another solution. But faced with the equation \( x^2=2 \), very few are able to find a proper solution without any previous knowledge of complex numbers. Such numbers have many applications in science, and it is therefore important to be able to use such numbers in our programs.
On the following pages we extend the previous material on computing with real numbers to complex numbers. The text is optional, and readers without knowledge of complex numbers can safely drop this section and jump to the section Summary.
A complex number is a pair of real numbers \( a \) and \( b \), most often written as \( a+bi \), or \( a+ib \), where \( i \) is called the imaginary unit and acts as a label for the second term. Mathematically, \( i=\sqrt{1} \). An important feature of complex numbers is definitely the ability to compute square roots of negative numbers. For example, \( \sqrt{2}=\sqrt{2}i \) (i.e., \( \sqrt{2}\sqrt{1} \)). The solutions of \( x^2=2 \) are thus \( x_1=+\sqrt{2}i \) and \( x_2=\sqrt{2}i \).
There are rules for addition, subtraction, multiplication, and division between two complex numbers. There are also rules for raising a complex number to a real power, as well as rules for computing \( \sin z \), \( \cos z \), \( \tan z \), \( e^z \), \( \ln z \), \( \sinh z \), \( \cosh z \), \( \tanh z \), etc. for a complex number \( z = a+ib \). We assume in the following that you are familiar with the mathematics of complex numbers, at least to the degree encountered in the program examples. $$ \begin{equation*} \hbox{let } u=a+bi\hbox{ and }v=c+di\end{equation*} $$
The following rules reflect complex arithmetics: $$ \begin{align*} u &= v\quad\Rightarrow\quad a=c, \ b=d\\ u &= a bi\\ u^* &\equiv a  bi\quad\hbox{(complex conjugate)}\\ u+ v &= (a+c) + (b+d)i\\ u  v &= (ac) + (bd)i\\ uv &= (ac  bd) + (bc+ad)i\\ u/v &= \frac{ac +bd}{c^2 + d^2} + \frac{bcad}{c^2+d^2}i\\ u &= \sqrt{a^2 + b^2}\\ e^{iq} &= \cos q + i\sin q \end{align*} $$
Complex arithmetics in Python
Python supports computation with complex numbers. The imaginary unit is written as in Python, instead of \( i \) as in mathematics. A complex number \( 23i \) is therefore expressed as in Python. We remark that the number \( i \) is written as , not just . Below is a sample session involving definition of complex numbers and some simple arithmetics:
A object has functionality for extracting the real and imaginary parts as well as computing the complex conjugate:
Complex functions in Python
Taking the sine of a complex number does not work:
The reason is that the function from the module only works with real () arguments, not complex. A similar module, , defines functions that take a complex number as argument and return a complex number as result. As an example of using the module, we can demonstrate that the relation \( \sin(ai)=i\sinh a \) holds:
Another relation, \( e^{iq} = \cos q + i\sin q \), is exemplified next:
Unified treatment of complex and real functions
The functions always return complex numbers. It would be nice to have functions that return a object if the result is a real number and a object if the result is a complex number. The Numerical Python package has such versions of the basic mathematical functions known from and . By taking a
one obtains access to these flexible versions of mathematical functions. The functions also get imported by any of the statements
A session will illustrate what we obtain. Let us first use the function in the module:
If we now import from ,
the previous function is overwritten by the new one. More precisely, the name was previously bound to a function from the module, but is now bound to another function from the module. In this case, any square root results in a object:
If we now take
we import (among other things) a new function. This function is slower than the versions from and , but it has more flexibility since the returned object is if that is mathematically possible, otherwise a is returned:
As a further illustration of the need for flexible treatment of both complex and real numbers, we may code the formulas for the roots of a quadratic function \( f(x)=ax^2 + bx + c \):
Using the up arrow, we may go back to the definitions of the coefficients and change them so the roots become real numbers:
Going back to the computations of and and performing them again, we get
That is, the two results are objects. Had we applied from , and would always be objects, while from the module would not handle the first (complex) case.
Return the dot product of two vectors.
The vdot(a, b) function handles complex numbers differently than dot(a, b). If the first argument is complex the complex conjugate of the first argument is used for the calculation of the dot product.
Note that handles multidimensional arrays differently than : it does not perform a matrix product, but flattens input arguments to 1D vectors first. Consequently, it should only be used for vectors.
Parameters:  a : array_like
b : array_like


Returns:  output : ndarray

See also
 Return the dot product without using the complex conjugate of the first argument.
Examples
Note that higherdimensional arrays are flattened!
Leave a Comment
(0 Comments)