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

What this book covers

Chapter 1, Getting Started, addresses the main language elements of Python without going into detail. Here we will have a brief tour of everything. It is a good starting point for those who want to start directly. It is a quick reference for those readers who want to revise their basic understanding of constructs such as functions.

Chapter 2, Variables and Basic Types, presents the most important and basic types in Python. Float is the most important data type in scientific computing together with the special numbers nan and inf. Booleans, integers, complex data types, and strings are other basic data types that will be used throughout this book.

Chapter 3, Container Types, explains how to work with container types, mainly lists. Dictionaries and tuples will be explained as well as indexing and looping through container objects. Occasionally, we can even use sets as a special container type.

Chapter 4, Linear Algebra - Arrays, covers the most important objects in linear algebra – vectors and matrices. This book chooses the NumPy array as the central tool for describing matrices and even higher-order tensors. Arrays have many advanced features and also allow universal functions to act on matrices or vectors elementwise. The book focuses on array indexing, slices, and the dot product as the basic operations in most computing tasks. Some linear algebra examples are shown to demonstrate the use of SciPy's linalg submodule. 

Chapter 5, Advanced Array Concepts, explains some more advanced aspects of arrays. The difference between array copies and views is explained extensively as views make programs that use arrays very fast but are often a source of errors that are hard to debug. The use of Boolean arrays to write effective, compact, and readable code is shown and demonstrated. Finally, the technique of array broadcasting – a unique feature of NumPy arrays – is explained by comparing it to operations being performed on functions.

Chapter 6, Plotting, shows how to make plots, mainly classical x/y plots but also 3D plots and histograms. Scientific computing requires good tools for visualizing the results. Python's matplotlib module is introduced, starting with the handy plotting commands in its pyplot submodule. Finetuning and modifying plots becomes possible by creating graphical objects such as axes. We will show how attributes of these objects can be changed and how annotations can be made.

Chapter 7, Functions, looks at functions, which form a fundamental building block in programming that is closely linked to some underlying mathematical concepts. Function definition and function calls are explained as the different ways to set function arguments. Anonymous lambda functions are introduced and used in various examples throughout the book.

Chapter 8, Classes, defines objects as instances of classes, which we provide with methods and attributes. In mathematics, class attributes often depend on each other, which requires special programming techniques for setter and getter functions. Basic mathematical operations such as addition can be defined for special mathematical data types. Inheritance and abstraction are mathematical concepts that are reflected by object-oriented programming. We demonstrate the use of inheritance using a simple solver class for ordinary differential equations.

Chapter 9, Iterating, presents iteration using loops and iterators. There is now a chapter in this book without loops and iterations, but here we will come to the principles of iterators and create our own generator objects. In this chapter, you will learn why a generator can be exhausted and how infinite loops can be programmed. Python's itertools module is a useful companion for this chapter.

Chapter 10, Series and DataFrames – Working with pandas,  gives a brief introduction to pandas. This chapter will teach you how to work with various time series in Python, the concept of DataFrames, and how to access and visualize data. This chapter will also cover how the concept of NumPy arrays is extended to pandas DataFrames.

Chapter 11, Communication by a Graphical User Interface,  shows the basic principles of GUI programming within
Matplotlib. The role of events, slider movements, or mouseclicks and their interaction with so-called callback functions is explained along with a couple of examples.

Chapter 12, Error and Exception Handling, covers errors and exceptions and how to find and fix them. An error or an exception is an event that breaks the execution of a program unit. This chapter shows what to do then, that is, how an exception can be handled. You will learn how to define your own exception classes and how to provide valuable information that can be used for catching these exceptions. Error handling is more than printing an error message.

Chapter 13, Namespaces, Scopes, and Modules, covers Python modules. What are local and global variables? When is a variable known and when is it unknown to a program unit? This is discussed in this chapter. A variable can be passed to a function by a parameter list or tacitly injected by making use of its scope. When should this technique be applied and when shouldn't it? This chapter tries to give an answer to this central question.

Chapter 14, Input and Output, covers some options for handling data files. Data files are used for storing and providing data for a given problem, often large-scale measurements. This chapter describes how this data can be accessed and modified using different formats.

Chapter 15, Testing, focuses on testing for scientific programming. The key tool is unittest, which allows automatic testing and parametrized tests. By considering the classical bisection algorithm in numerical mathematics, we exemplify different steps to designing meaningful tests, which as a side effect also deliver documentation of the use of a piece of code. Careful testing provides test protocols that can be helpful later when debugging complex code often written by many different programmers.

Chapter 16, Symbolic Computations – SymPy, is all about symbolic computations. Scientific computing is mainly numeric computations with inexact data and approximative results. This is contrasted with symbolic computations' often formal manipulation, which aims for exact solutions in a closed-form expression. In this chapter, we introduce this technique in Python, which is often used to derive and verify theoretically mathematical models and numerical results. We focus on high-precision floating-point evaluation of symbolic expressions.

Chapter 17, Interacting with the Operating System, demonstrates the interaction of a Python script with system commands. The chapter is based on Linux systems such as Ubuntu and serves only as a demonstration of concepts and possibilities. It allows putting scientific computing tasks in an application context, where often different software have to be combined. Even hardware components might come into play.

Chapter 18, Python for Parallel Computing, covers parallel computing and the mpi4py module. In this chapter, we see how to execute copies of the same script on different processors in parallel. The commands presented in this chapter are provided by the mpi4py Python module, which is a Python wrapper to realize the MPI standard in C. After working through this chapter, you will be able to work on your own scripts for parallel programming, and you will find that we described only the most essential commands and concepts here. 

Chapter 19, Comprehensive Examples, presents some comprehensive and longer examples together with a brief introduction to the theoretical background and their complete implementation. These examples make use of all the constructs shown in the book so far and put them in a larger and more complex context. They are open to extension by the reader.