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

2.6 Exercises

Ex. 1: Check whether  is a zero of the function:

Ex. 2: According to de Moivre's formula, the following holds:

Choose numbers n and x and verify that formula in Python.

Ex. 3: Complex numbers. Verify Euler's formula in the same way:

Ex. 4: Suppose we are trying to check the convergence of a diverging sequence (here, the sequence is defined by the recursive relation  and ):

u = 1.0 # you have to use a float here!
uold = 10.
for iteration in range(2000):
if not abs(u-uold) > 1.e-8:
print('Convergence')
break # sequence has converged
uold = u
u = 2*u
else:
print('No convergence')
  1. Since the sequence does not converge, the code should print the
     No convergence message. Execute it to see what happens.
  1. What happens if you replace the line
      if not abs(u-uold) > 1.e-8:

with

      if abs(u-uold) < 1.e-8:

It should give exactly the same result, shouldn't it? Run the code again to see what happens.

  1. What happens if you replace u=1.0 with u=1 (without a decimal point)? Run the code to check your predictions.
  2. Explain the unexpected behavior of this code.

Ex. 5: An implication C = (A ⇒ B) is a Boolean expression that is defined as

  • C is True if A is Falseor A and B are both True
  • C is False otherwise

Write a Python function implication(A, B).

Ex. 6: This exercise is to train Boolean operations. Two binary digits (bits) are added by using a logical device called a half adder. It produces a carry bit (the digit of the next higher value) and the sum as defined by the following table, and half adder circuit:

p
q
sum
carry
1
1
0
1
1
0
1
0
0
1
1
0
0
0
0
0

 

Definition of the half adder operation:

Figure 2.3: A half adder circuit

A full adder consists of two half adders and sums two bits and an additional carry bit on the input (see also the following figure):

Figure 2.4: A full adder circuit

Write a function that implements a half adder and another that implements a full adder. Test these functions.