Python For Scientific Computing and A I Information
Python For Scientific Computing and A I Information
net/publication/369596858
CITATIONS READS
0 286
1 author:
Stephen Lynch
Manchester Metropolitan University
115 PUBLICATIONS 1,694 CITATIONS
SEE PROFILE
Some of the authors of this publication are also working on these related projects:
Quantum technologies based on ultracold ions (atoms). Precision measurements. View project
Dynamical Systems with Applications using Mathematica, 2nd Edition View project
All content following this page was uploaded by Stephen Lynch on 29 March 2023.
Summary
Table of Contents
Foreword
Preface
Index
Python for Scientific Computing and Artificial Intelligence is split into 3 parts: in Section 1, the reader is introduced to
the Python programming language and shown how Python can aid in the understanding of advanced High School
Mathematics. In Section 2, the reader is shown how Python can be used to solve real-world problems from a broad
range of scientific disciplines. Finally, in Section 3, the reader is introduced to neural networks and shown how
TensorFlow (written in Python) can be used to solve a large array of problems in Artificial Intelligence (AI).
This book was developed from a series of national and international workshops that the author has been delivering
for over twenty years. The book is beginner friendly and has a strong practical emphasis on programming and
computational modelling.
Features:
• No prior experience of programming is required.
• Online GitHub repository available with codes for readers to practice.
• Covers applications and examples from biology, chemistry, computer science, data science, electrical and
mechanical engineering, economics, mathematics, physics, statistics and binary oscillator computing.
• Full solutions to exercises are available as Jupyter notebooks on the Web.
Support Material
GitHub Repository of Python Files and Notebooks:
https://github.com/proflynch/CRC-Press/
Solutions to All Exercises:
Section 1: An Introduction to Python: https://drstephenlynch.github.io/webpages/Solutions_Section_1.html
Section 2: Python for Scientific Computing: https://drstephenlynch.github.io/webpages/Solutions_Section_2.html
Section 3: Artificial Intelligence: https://drstephenlynch.github.io/webpages/Solutions_Section_3.html PTO
Python for Scientific
Computing and Artificial
Intelligence
Python for Scientific Computing and Artificial Intelligence is split into 3 parts: in Section 1,
the reader is introduced to the Python programming language and shown how Python can aid in
the understanding of advanced High School Mathematics. In Section 2, the reader is shown how
Python can be used to solve real-world problems from a broad range of scientific disciplines.
Finally, in Section 3, the reader is introduced to neural networks and shown how TensorFlow
(written in Python) can be used to solve a large array of problems in Artificial Intelligence (AI).
This book was developed from a series of national and international workshops that the author
has been delivering for over twenty years. The book is beginner friendly and has a strong practi-
cal emphasis on programming and computational modeling.
Features:
In 2022, Stephen Lynch was named a National Teaching Fellow, which celebrates and recognises
individuals who have made an outstanding impact on student outcomes and teaching in higher
education. He won the award for his work in programming in the STEM subjects, research
feeding into teaching, and widening participation (using experiential and object-based learn-
ing). Although educated as a pure mathematician, Stephen’s many interests now include applied
mathematics, cell biology, electrical engineering, computing, neural networks, nonlinear optics
and binary oscillator computing, which he co-invented with a colleague. He has authored 2 in-
ternational patents for inventions, 8 books, 4 book chapters, over 40 journal articles, and a few
conference proceedings. Stephen is a Fellow of the Institute of Mathematics and Its Applica-
tions (FIMA) and a Senior Fellow of the Higher Education Academy (SFHEA). He is currently
a Reader with MMU and was an Associate Lecturer with the Open University from 2008-2012.
In 2010, Stephen volunteered as a STEM Ambassador, in 2012, he was awarded MMU Public
Engagement Champion status, and in 2014 he became a Speaker for Schools. He runs national
workshops on “Python for A-Level Mathematics and Beyond,” and international workshops on
“Python for Scientific Computing and TensorFlow for Artificial Intelligence.” He has run work-
shops in China, Malaysia, Singapore, and the USA.
Chapman & Hall/CRC
The Python Series
About the Series
Python has been ranked as the most popular programming language, and it is widely used in
education and industry. This book series will offer a wide range of books on Python for students
and professionals. Titles in the series will help users learn the language at an introductory and
advanced level, and explore its many applications in data science, AI, and machine learning.
Series titles can also be supplemented with Jupyter notebooks.
Stephen Lynch
Manchester Metropolitan University, United Kingdom
First edition published 2023
by CRC Press
6000 Broken Sound Parkway NW, Suite 300, Boca Raton, FL 33487-2742
Reasonable efforts have been made to publish reliable data and information, but the author and publisher cannot
assume responsibility for the validity of all materials or the consequences of their use. The authors and publishers
have attempted to trace the copyright holders of all material reproduced in this publication and apologize to copyright
holders if permission to publish in this form has not been obtained. If any copyright material has not been acknowl-
edged please write and let us know so we may rectify in any future reprint.
Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, transmitted, or
utilized in any form by any electronic, mechanical, or other means, now known or hereafter invented, including pho-
tocopying, microfilming, and recording, or in any information storage or retrieval system, without written permission
from the publishers.
For permission to photocopy or use material electronically from this work, access www.copyright.com or contact the
Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA 01923, 978-750-8400. For works that are
not available on CCC please contact mpkbookspermissions@tandf.co.uk
Trademark notice: Product or corporate names may be trademarks or registered trademarks and are used only for
identification and explanation without intent to infringe.
DOI: 10.1201/9781003285816
Typeset in LM Roman
by KnowledgeWorks Global Ltd.
Publisher’s note: This book has been prepared from camera-ready copy provided by the authors
To my family,
my brother Mark and my sister Jacqueline,
my wife Gaynor,
and our children, Sebastian and Thalia,
for their continuing love, inspiration and support.
Contents
Foreword xiii
Preface xv
1.1 INTRODUCTION 4
1.1.1 Tutorial One: Using Python as a Powerful Calculator (30
Minutes) 5
1.1.2 Tutorial Two: Lists (20 Minutes) 7
1.2 SIMPLE PROGRAMMING IN PYTHON 8
1.2.1 Tutorial Three: Defining Functions (30 Minutes) 9
1.2.2 Tutorial Four: For and While Loops (20 Minutes) 11
1.2.3 Tutorial Five: If, elif, else constructs (10 Minutes) 11
1.3 THE TURTLE MODULE AND FRACTALS 11
vii
viii ! Contents
Chapter 6 ! Biology 81
Chapter 7 ! Chemistry 95
Index 309
Foreword
I came to know about the author through his previous text: Dynamical Systems with
Applications using Python, Springer International Publishing (2018). I simply loved
this book as it helped me to enhance my knowledge in both Python and Dynamical
Systems. I was so pleased with this book that in collaboration with a senior colleague
in Dynamical Systems, we developed a set of two-semester courses with the same
title as the book. We also invited Stephen to offer for us a short course on Python for
Scientific Computing and TensorFlow for Artificial Intelligence (AI). We were very
pleased that he accepted our offer, as he has been in demand to offer these courses
all over the world. For Pitt, he first offered it online (due to Covid) in summer 2021,
and then in person on Pitt campus in summer 2022. Both of these courses were huge
successes with several hundreds of participants each time. These delegates came from
a wide variety of backgrounds: professors, scientists and engineers from a wide range
of disciplines in industry & government labs, and both graduate and undergraduate
students in a broad range of disciplines. The excellent textbook plus Stephen’s natural
teaching talents made the course very popular. We plan to offer it on a regular basis
as long as Stephen is up for it.
Based on these experiences, I was naturally very eager and excited to read
Stephen’s newest (present) text, and I am not surprised to see that this volume
is also a masterpiece! The topics and approach taken in this book are indeed like no
other! The first section provides an introduction to Python for complete novices and
goes on to show how it can be used for teaching High School mathematics. Readers
are shown how to access Python through the IDLE platform, Spyder and jupyter
notebooks through Anaconda, and cloud computing using Google Colab. The second
section covers a vast array of examples from the scientific fields of biology, chemistry,
data science, economics, engineering, fractals & multifractals, image processing, nu-
merical methods for ordinary and partial differential equations, physics and statistics.
Each chapter covers several examples from the field and there are exercises with even
more examples at the end of each chapter. The final section covers AI, with brain-
inspired computing and neurodynamics and neural networks following on nicely from
Section 2. The final chapters introduce TensorFlow and Keras, and recurrent and con-
volutional neural networks, respectively. There is further reading for those interested
in cyber security, ethics in AI, the internet of things, natural language processing and
reinforcement learning.
All Python and TensorFlow programs can be downloaded through GitHub and full
worked solutions for all of the exercises are available to view on the Web. Overall, the
book will appeal to a very wide range of readers, from high school, to undergraduate
and postgraduate students in science & engineering, to academics and researchers
xiii
xiv ! Foreword
in industry. Novices to programming and even experienced programmers will all get
something from this book.
Professor Lynch has recently been named
name aa National Teaching Fellow in the
UK. I am not very familiar with UK’s system. But as I understand, this fellowship is
“...to recognize and celebrate individuals who have made an outstanding impact on
student outcomes and the teaching profession...” Based on his other books, and his
courses, I can easily see that Stephen is surely deserving of this recognition. He is an
excellent writer, with an exceptional knack for teaching.
I hope that the reader is as excited as I was to get started on this book.
This book has developed from a series of university, national and international work-
shops that I have been delivering for over twenty years. It is based on the program-
ming language Python, which is a high-level programming language and supports
functional, imperative, object-oriented and procedural styles of programming. Prob-
ably the most important aspect of Python is that it is open source and completely
free to the user. Python is currently the most popular programming language in the
world from over 600 programming languages, and it is a relatively user-friendly lan-
guage. The book is split into three sections. In Section 1, the reader is introduced to
the Python programming language and it is shown how Python can aid in the under-
standing of A-Level (or High School) Mathematics. In Section 2, the reader is shown
how Python can be used to solve real-world problems from a broad range of scientific
disciplines and in Section 3, the reader is introduced to neural networks and shown
how TensorFlow (written in Python) can be used to solve a large array of problems
in Artificial Intelligence (AI). As well as learning the most popular programming lan-
guage in the world, the reader will also be shown how Python can be used to perform
computational mathematics to solve real-world problems. By the end of the book,
the reader will be able to solve a great many mathematical problems without having
to learn any of the theory of mathematics. The reader should note that the emphasis
of the book is on programming and computational modeling and not on the math-
ematical theory. For those readers who want to know all of the ins-and-outs of the
mathematics—for High School Mathematics, there are recommended books listed in
Section 1, and for Sections 2 and 3, I recommend my other book, Dynamical Systems
with Applications using Python [1], which covers the mathematical theory in some
detail. To keep the book relatively brief, explanations of the syntax of the code are
not included. Instead, the reader is given full working programs and explanations for
syntax are easily discovered on the Web.
Chapter 1 starts with the Python Integrated Development Learning Environment
(IDLE) which can be downloaded at:
https://www.python.org.
In Section 1.1, the reader is introduced to the IPython Console Window, where
users can use Python as a powerful calculator. As every reader is already familiar
with a calculator, I have found this method of teaching the best way to introduce
Python. The Math library (or module) is introduced and the functions available can
be found using the Help features in IDLE. Next, the reader is shown how to define
their own functions in Python—think of adding buttons to your calculator. For and
xv
xvi ! Preface
while loops are introduced by means of simple examples and then if, elif, else con-
structs are discussed. With these three programming structures, one can write any of
the programs listed throughout the book! To further aid in the understanding of how
programs work, the turtle module is introduced in Section 1.3. Readers may have
heard of the Scratch programming language:
https://scratch.mit.edu
developed at MIT, which is the largest free coding community for kids in the world.
Well, the turtle module is similar to Scratch and provides a pictorial representation to
coding and that is why it is so popular with children. In this book, I use turtle to plot
simple colorful fractals using iteration and readers can figure out how the programs
work by slowing down the turtle, plotting early stage fractals, and following the code
by drawing little pictures—this helps greatly when learning to code. Chapter 1 ends
with some exercises and a short bibliography, where interested readers can find more
introductory texts for Python. It is highly recommended that the reader attempts the
exercises at the end of Chapter 1 before moving on to Chapter 2. Note that, solutions
to the exercises, as in all other chapters can be viewed on the Web, the corresponding
URL will appear at the end of each chapter. IDLE is not suitable for scientific com-
putation, for that you will need Anaconda or Google Colab. In Chapter 2, the reader
is shown how to download Anaconda, the world’s most popular data science platform:
https://www.anaconda.com/products/individual,
simply download Anaconda installers for Linux, Mac or Windows. Within this frame-
work, we can launch Spyder (Scientific Python Development Environment) or a
jupyter notebook. Those of you familiar with MATLAB!, R will see similarities with
Spyder and if any readers have ever used Mathematica!, R you will notice a similarity
with both sets of notebooks. Note that within Anaconda there are other tools available
including the R studio for statistics and Orange 3 and Glueviv for data visualization
and data analysis. In this book, I will only be using Spyder and jupyter notebooks
from Anaconda. Note that although Anaconda is well known for Data Science, it
is much more flexible than that, and is ideal for both scientific computing and AI.
Section 2.1 is concerned with numerical Python using the numpy library (or module):
https://numpy.org,
where lists, arrays, vectors, matrices and tensors are introduced. These concepts are
vitally important in neural networks, machine learning and deep learning, which will
all be covered in the third section of the book. The chapter ends with a tutorial
introduction to the matrix plotting library matplotlib:
https://matplotlib.org,
Preface ! xvii
used for creating static, animated and interactive visualizations in Python. As well
as producing colorful plots, readers will be shown how to save their graphs to file in
any format they like (eps, jpeg, png, tiff etc.) and change the resolution of figures
using dpi (dots per inch). They will also be shown how to label axes and figures with
LaTeX symbols:
https://www.caam.rice.edu/˜heinken/latex/symbols.pdf.
https://www.sympy.org/,
https://colab.research.google.com/.
Note that you need a Google account in order to have access to Google Colab. The
major benefit with this method is that you do not use up memory on your own
computer when you run the Python code. Section 3.1 shows the jupyter notebook
interface and readers are shown how to use Markdown to insert titles, text, html code
and LaTeX mathematical equations into a notebook. Code cells are used to insert
Python code. Cells are compiled by hitting SHIFT + ENTER in the cell or by running
the cell using the small play button. In Section 3.2, the reader will be shown how
to load files and figures into a notebook. Examples demonstrating animations and
interactive plots will be listed here. The notebook can be saved as a Python notebook
(filename.ipynb), a Python file (filename.py) or as a html file (filename.html) so the
notebook can be published on the web. In Section 3.3, Google Colab and GitHub will
be introduced and this again will be useful material for what is to come in the third
section of the book. Chapters 4–5 show how Python can be used to give a deeper
understanding of the new A-Level Mathematics syllabus which was introduced to
the UK in 2017. Most of the topics covered here are covered by many high schools
around the world and once more it is demonstrated how Python can be used to solve
mathematical problems without understanding all of the mathematical theorems and
xviii ! Preface
rules. A-Level (and High School) pupils will find these chapters extremely useful for
providing a deeper understanding of the mathematics.
Section 2 of the book covers scientific computation and follows on nicely from
Section 1. Chapter 6 presents examples from the field of Biology starting with a sim-
ple single population model of insects using the logistic model and iteration. Fixed
points, stability and bifurcation diagrams are introduced. Phase plane portraits are
plotted in the next section and the behavior of interacting species is discussed. The
spread of flu in a school using a compartmental model is covered next. In Section 6.4,
the reader can reproduce results from a recent journal paper where a spring-damper
model is used to simulate hysteresis (the system is history dependent) in single fiber
muscle. Chapter 7 covers Chemistry, balancing chemical-reaction equations, solving
differential equations to model chemical kinetics, and investigating the solubility of
silver chloride in a potassium chloride solution. Chapter 8 covers some topics from
the popular field of data science. The Pandas package is introduced for creating data
frames for data manipulation and analysis. Linear programming is introduced to solve
optimization problems, including the simplex method. Unsupervised learning is used
to cluster data in the third section, and the final section covers decision trees. The field
of data science is huge, and readers should note that statistics, machine learning, and
deep learning, covered later in the book, are also encompassed within data science.
In Chapter 9, the reader is shown a number of examples from the field of Economics,
including a microeconomic model of quantity of production, a macroeconomic model
of economic growth, the Markowitz model—a portfolio optimization model and the
Black-Scholes model for computing the fair price or theoretical value for a call or a
put option based on a function of four variables. In the field of engineering, linear
and nonlinear electric circuits (including Chua’s circuit) are covered in the first half
of Chapter 10, and then mechanical systems of coupled oscillators and nonlinear pen-
dula are modeled. Chapter 11 is concerned with fractals, multifractals and real-world
applications. Image processing is covered in Chapter 12, both grayscale and color
images are investigated, and methods for finding features, edge detection and statis-
tical analysis of images are covered. The chapter ends with examples from medical
imaging, plotting the vascular architecture from a human retinal image using a ridge
filter, and identifying a tumor in a brain image using masking and segmentation. In
the first half of Chapter 13, numerical methods for Ordinary Differential Equations
(ODEs), including Euler’s methods, the fourth order Runga-Kutta method and the
Adams-Bashforth method, are all introduced. Numerical methods for Partial Differ-
ential Equations (PDEs), and in particular finite difference methods, are covered in
the second half of Chapter 13. The standard examples of heat diffusion in a rod
and a flat plate, advection along a one-dimensional channel, and the the vibration
of a string are all included. Chapter 14 is concerned with the field of Physics, and
examples are taken from signal processing, using Fast Fourier Transforms (FFTs)
and nonlinear optics, where the Ikeda map is used to model the propagation of light
waves through a Simple Fiber Ring (SFR) resonator. Josephson junctions, super-
conducting devices that act like biological neurones but oscillate 100,000,000 times
faster are discussed, and the motion of planetary bodies (earth-moon-comet system)
Preface ! xix
is also covered. Chapter 15 shows how Python can be used for statistics, including
linear regression, the student t-test, Markov chains and Monte-Carlo simulation, for
example.
The final section of the book is concerned with neural networks and AI. To follow
Section 2, scientific computation is applied to modeling neurones and simple neural
networks in some detail. Machine learning, deep learning and TensorFlow (written
in Python) are covered later. Chapter 16 covers an invention by the author (and one
of his colleagues) labeled brain inspired computing. The first section deals with the
Hodgkin-Huxley model from the 1950s before moving on to later models that have
been developed over the decades. Using threshold oscillator logic, a binary oscillator
half-adder for logic computation is simulated in Section 16.2, and in the next section
a memory component is modeled using an oscillator-based set reset flip-flop. The
chapter ends with applications and future work. Chapter 17 covers neural networks
and neurodynamics. Simple neural networks are used to model logic AND, OR and
XOR gates. Section 17.2 covers the backpropagation algorithm, with a simple expla-
nation of how this works, and then applied to a neural network model for the Boston
Housing data for valuing homes. The final section presents a simple model of a two-
neuron module. A stability analysis is carried out to determine parameter borders
where the system displays hysteresis, unstable behavior and quasiperiodic (almost pe-
riodic) behavior. Chapter 18 provides an introduction to TensorFlow and the Keras
Application Programming Interface (API). TensorFlow is then used to build networks
for linear regression, the XOR gate and finally the Boston Housing data valuations.
Recurrent Neural Networks (RNNs) are covered in Chapter 19, starting with discrete
and continuous Hopfield models used as associative memory RNNs. The long short-
term memory RNN network is applied to predicting chaotic and financial time series
in sections three and four. Chapter 20 presents an example of a convolutional neu-
ral network (CNN). A simple example illustrating convolving and pooling starts the
chapter, and then a CNN is used on the MNIST handwritten dataset for predicting
the digits 0-9. In the next section, TensorBoard, which is TensorFlow’s visualization
toolkit, is used to see how well your neural network is performing. The final section
covers further reading, natural language processing (NLP), reinforcement learning,
ethics in AI, cyber security and the internet of things (IoT) are briefly discussed.
Chapter 21 presents answers or hints to all of the exercises in the book.
Full solutions to the Exercises in Sections 1, 2 and 3, may be found in the URLs below:
https://drstephenlynch.github.io/webpages/Solutions_Section_1.html.
https://drstephenlynch.github.io/webpages/Solutions_Section_2.html.
https://drstephenlynch.github.io/webpages/Solutions_Section_3.html.
xx ! Preface
Finally, all of the IDLE, Python and jupyter notebooks can be dowloaded through
GitHub:
https://github.com/proflynch/CRC-Press/.
[1] Lynch, S. (2018). Dynamical Systems with Applications using Python. Springer
International Publishing, New York.
Index
309
310 ! Index