Reader small image

You're reading from  Applying Math with Python - Second Edition

Product typeBook
Published inDec 2022
PublisherPackt
ISBN-139781804618370
Edition2nd Edition
Concepts
Right arrow
Author (1)
Sam Morley
Sam Morley
author image
Sam Morley

Sam Morley is an experienced lecturer in mathematics and a researcher in pure mathematics. He is currently a research software engineer at the University of Oxford working on the DataSig project. He was previously a lecturer in mathematics at the University of East Anglia and Nottingham Trent University. His research interests lie in functional analysis, especially Banach algebras. Sam has a firm commitment to providing high-quality, inclusive, and enjoyable teaching, with the aim of inspiring his students and spreading his enthusiasm for mathematics.
Read more about Sam Morley

Right arrow

Preface

Python is a powerful and flexible programming language that is fun and easy to learn. It is the programming language of choice for many professionals, hobbyists, and scientists. The power of Python comes from its large ecosystem of packages and a friendly community, and its ability to communicate seamlessly with compiled extension modules. This means that Python is ideal for solving problems of all kinds, especially mathematical problems.

Mathematics is usually associated with calculations and equations but, in reality, these are very small parts of a much larger subject. At its core, mathematics is about solving problems, and the logically structured approach to solutions. Once you explore past the equations, calculations, derivatives, and integrals, you discover a vast world of beautiful, elegant structures.

This book is an introduction to solving mathematical problems using Python. It introduces some of the basic concepts of mathematics and how to use Python to work with these concepts. It also introduces some basic templates for solving a variety of mathematical problems across a large number of topics within mathematics. The first few chapters focus on core skills such as working with NumPy arrays, plotting, calculus, and probability. These topics are very important throughout mathematics and act as a foundation for the rest of the book. In the remaining chapters, we discuss more practical problems, covering topics such as data analysis and statistics, networks, regression and forecasting, optimization, and game theory. We hope that this book provides a basis for solving mathematical problems and the tools for you to further explore the world of mathematics.

Who this book is for

This book is primarily aimed at people who have some experience with Python and have projects (for work or just for fun) that involve solving some kind of mathematical problem. In the first few chapters we aim to give a small taste of the mathematical background for those who are not familiar with the basics, but we are rather limited by space. I’ve provided some suggestions for further reading at the end of each chapter to point you to resources where you can learn more. I hope that this book gets you started on your mathematical problems, and sparks your curiosity to learn more about the mathematics behind these subjects.

What this book covers

Chapter 1, An Introduction to Basic Packages, Functions, and Concepts, introduces some of the basic tools and concepts that will be needed in the rest of the book, including the main Python packages for mathematical programming, NumPy and SciPy.

Chapter 2, Mathematical Plotting with Matplotlib, covers the basics of plotting with Matplotlib, which is useful when solving almost all mathematical problems.

Chapter 3, Calculus and Differential Equations, introduces topics from calculus such as differentiation and integration, and some more advanced topics such as ordinary and partial differential equations.

Chapter 4, Working with Randomness and Probability, introduces the fundamentals of randomness and probability, and how to use Python to explore these ideas.

Chapter 5, Working with Trees and Networks, covers working with trees and networks (graphs) in Python using the NetworkX package.

Chapter 6, Working with Data and Statistics, gives various techniques for handling, manipulating, and analyzing data using Python.

Chapter 7, Using Regression and Forecasting, describes various techniques for modeling data and predicting future values using the Statsmodels package and scikit-learn.

Chapter 8, Geometric Problems, demonstrates various techniques for working with geometric objects in Python using the Shapely package.

Chapter 9, Finding Optimal solutions, introduces optimization and game theory, which uses mathematical methods to find the best solutions to problems.

Chapter 10, Increasing your Productivity, covers an assortment of situations you might encounter while solving mathematical problems using Python.

To get the most out of this book

You will need to have a basic knowledge of Python. We don’t assume any knowledge of mathematics, although if you are familiar with some basic mathematical concepts, you will better understand the context and details of the techniques we discuss.

The only requirement throughout this book is a recent version of Python – at least Python 3.6, but higher versions are preferable. (The code for this edition has been tested on Python 3.10, but should work on earlier versions too.) You might prefer to use the Anaconda distribution of Python, which comes with many of the packages and tools required in this book. If this is the case, you should use the conda package manager to install the packages. Python is supported on all major operating systems – Windows, macOS, and Linux – and on many platforms.

The packages that are used in this book and their versions at the time of writing: NumPy 1.23.3, SciPy 1.9.1 Matplotlib 3.6.0, Jax 0.3.13 (and jaxlib 0.3.10), Diffrax 0.1.2, PyMC 4.2.2, pandas 1.4.3 Bokeh 2.4.3, NetworkX 3.5.3, Scikit-learn 1.1.2, StatsModels 0.13.2, Shapely 1.8.4, NashPy 0.0.35, Pint 0.20.1, Uncertainties 3.1.7, Xarray 2022.11.0, NetCDF4 1.6.1, Geopandas 0.12.1, CartoPy 0.21.0, Cerberus 1.3.4, Cython 0.29.32, Dask 2022.10.2.

Software/hardware covered in the book

Operating system requirements

Python 3.10

Windows, macOS, or Linux

If you are using the digital version of this book, we advise you to type the code yourself or access the code from the book’s GitHub repository (a link is available in the next section). Doing so will help you avoid any potential errors related to the copying and pasting of code.

You may prefer to work through the code samples in this book in a Jupyter notebook rather than in a simple Python file. There are one or two places in this book where you might need to repeat plotting commands, as plots cannot be updated in later cells in the way that is shown here.

Download the example code files

You can download the example code files for this book from GitHub at https://github.com/PacktPublishing/Applying-Math-with-Python-2nd-Edition. If there’s an update to the code, it will be updated in the GitHub repository.

We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots and diagrams used in this book. You can download it here: http://packt.link/OxkXD.

Conventions used

There are a number of text conventions used throughout this book.

Code in text: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: “The Decimal package also provides the Context object, which allows for fine-grained control over the precision, display, and attributes of Decimal objects.”

A block of code is set as follows:

from decimal import getcontext
ctx = getcontext()
num = Decimal('1.1')
num**4  # Decimal('1.4641')
ctx.prec=4  # set the new precision
num**4  # Decimal('1.464')

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

from numpy import linalg
A = np.array([[3, -2, 1], [1, 1, -2], [-3, -2, 1]])
b = np.array([7, -4, 1])

Any command-line input or output is written as follows:

$ python3.10 -m pip install numpy scipy

Bold: Indicates a new term, an important word, or words that you see onscreen. For instance, words in menus or dialog boxes appear in bold. Here is an example: “Select System info from the Administration panel.”

Tips or important notes

Appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: If you have questions about any aspect of this book, email us at customercare@packtpub.com and mention the book title in the subject of your message.

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/support/errata and fill in the form.

Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at copyright@packt.com with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Share your thoughts

Once you’ve read Applying Math with Python, we’d love to hear your thoughts! Please click here to go straight to the Amazon review page for this book and share your feedback.

Your review is important to us and the tech community and will help us make sure we’re delivering excellent quality content.

Download a free PDF copy of this book

Thanks for purchasing this book!

Do you like to read on the go but are unable to carry your print books everywhere? Is your eBook purchase not compatible with the device of your choice?

Don’t worry, now with every Packt book you get a DRM-free PDF version of that book at no cost.

Read anywhere, any place, on any device. Search, copy, and paste code from your favorite technical books directly into your application.

The perks don’t stop there, you can get exclusive access to discounts, newsletters, and great free content in your inbox daily

Follow these simple steps to get the benefits:

  1. Scan the QR code or visit the link below

https://packt.link/free-ebook/9781804618370

  1. Submit your proof of purchase
  2. That’s it! We’ll send your free PDF and other benefits to your email directly
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Applying Math with Python - Second Edition
Published in: Dec 2022Publisher: PacktISBN-13: 9781804618370
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Sam Morley

Sam Morley is an experienced lecturer in mathematics and a researcher in pure mathematics. He is currently a research software engineer at the University of Oxford working on the DataSig project. He was previously a lecturer in mathematics at the University of East Anglia and Nottingham Trent University. His research interests lie in functional analysis, especially Banach algebras. Sam has a firm commitment to providing high-quality, inclusive, and enjoyable teaching, with the aim of inspiring his students and spreading his enthusiasm for mathematics.
Read more about Sam Morley