Book Image

Scientific Computing with Python - Second Edition

By : Claus Führer, Jan Erik Solem, Olivier Verdier
Book Image

Scientific Computing with Python - Second Edition

By: Claus Führer, Jan Erik Solem, Olivier Verdier

Overview of this book

Python has tremendous potential within the scientific computing domain. This updated edition of Scientific Computing with Python features new chapters on graphical user interfaces, efficient data processing, and parallel computing to help you perform mathematical and scientific computing efficiently using Python. This book will help you to explore new Python syntax features and create different models using scientific computing principles. The book presents Python alongside mathematical applications and demonstrates how to apply Python concepts in computing with the help of examples involving Python 3.8. You'll use pandas for basic data analysis to understand the modern needs of scientific computing, and cover data module improvements and built-in features. You'll also explore numerical computation modules such as NumPy and SciPy, which enable fast access to highly efficient numerical algorithms. By learning to use the plotting module Matplotlib, you will be able to represent your computational results in talks and publications. A special chapter is devoted to SymPy, a tool for bridging symbolic and numerical computations. By the end of this Python book, you'll have gained a solid understanding of task automation and how to implement and test mathematical algorithms within the realm of scientific computing.
Table of Contents (23 chapters)
20
About Packt
22
References

Real and imaginary parts

You may access the real and imaginary parts of a complex number  using the real and imag attributes. Those attributes are read-only; in other words, they cannot be changed:

z = 1j 
z.real # 0.0
z.imag # 1.0
z.imag = 2 # AttributeError: readonly attribute

It is not possible to convert a complex number to a real number:

z = 1 + 0j 
z == 1 # True
float(z) # TypeError

Interestingly, the real and imag attributes as well as the conjugate method work just as well for complex arrays; see also Section 4.3.1, Array properties. We demonstrate this by computing the Nth roots of unity, which are , that is, the  solutions of the equation :

from matplotlib.pyplot import *
N = 10
# the following vector contains the Nth roots of unity:
unity_roots = array([exp(1j*2*pi*k/N) for k in range(N)])
# access all the real or imaginary parts with real or imag:
axes(aspect='equal')
plot(unity_roots.real, unity_roots.imag, 'o')
allclose(unity_roots**N, 1) # True

The resulting figure shows the 10 roots of unity. In Figure 2.2, it is completed by a title and axes labels and shown together with the unit circle. (For more details on how to make plots, see Chapter 6: Plotting.)

Figure 2.2: Roots of unity together with the unit circle

It is, of course, possible to mix the previous methods, as illustrated by the following examples:

z = 3.2+5.2j 
(z + z.conjugate()) / 2. # returns (3.2+0j)
((z + z.conjugate()) / 2.).real # returns 3.2
(z - z.conjugate()) / 2. # returns 5.2j
((z - z.conjugate()) / 2.).imag # returns 5.2
sqrt(z * z.conjugate()) # returns (6.1057350089894991+0j)