Applying Math with Python - Second Edition

Book description

Discover easy-to-follow solutions and techniques to help you to implement applied mathematical concepts such as probability, calculus, and equations using Python's numeric and scientific libraries

Key Features

  • Compute complex mathematical problems using programming logic with the help of step-by-step recipes
  • Learn how to use Python libraries for computation, mathematical modeling, and statistics
  • Discover simple yet effective techniques for solving mathematical equations and apply them in real-world statistics

Book Description

The updated edition of Applying Math with Python will help you solve complex problems in a wide variety of mathematical fields in simple and efficient ways. Old recipes have been revised for new libraries and several recipes have been added to demonstrate new tools such as JAX.

You'll start by refreshing your knowledge of several core mathematical fields and learn about packages covered in Python's scientific stack, including NumPy, SciPy, and Matplotlib. As you progress, you'll gradually get to grips with more advanced topics of calculus, probability, and networks (graph theory). Once you’ve developed a solid base in these topics, you’ll have the confidence to set out on math adventures with Python as you explore Python's applications in data science and statistics, forecasting, geometry, and optimization. The final chapters will take you through a collection of miscellaneous problems, including working with specific data formats and accelerating code.

By the end of this book, you'll have an arsenal of practical coding solutions that can be used and modified to solve a wide range of practical problems in computational mathematics and data science.

What you will learn

  • Become familiar with basic Python packages, tools, and libraries for solving mathematical problems
  • Explore real-world applications of mathematics to reduce a problem in optimization
  • Understand the core concepts of applied mathematics and their application in computer science
  • Find out how to choose the most suitable package, tool, or technique to solve a problem
  • Implement basic mathematical plotting, change plot styles, and add labels to plots using Matplotlib
  • Get to grips with probability theory with the Bayesian inference and Markov Chain Monte Carlo (MCMC) methods

Who this book is for

Whether you are a professional programmer or a student looking to solve mathematical problems computationally using Python, this is the book for you. Advanced mathematics proficiency is not a prerequisite, but basic knowledge of mathematics will help you to get the most out of this Python math book. Familiarity with the concepts of data structures in Python is assumed.

Table of contents

  1. Applying Math with Python
  2. Contributors
  3. About the author
  4. About the reviewer
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Download the color images
    6. Conventions used
    7. Get in touch
    8. Share your thoughts
    9. Download a free PDF copy of this book
  6. Chapter 1: An Introduction to Basic Packages, Functions, and Concepts
    1. Technical requirements
    2. Exploring Python numerical types
      1. Decimal type
      2. Fraction type
      3. Complex type
    3. Understanding basic mathematical functions
    4. Diving into the world of NumPy
      1. Element access
      2. Array arithmetic and functions
      3. Useful array creation routines
      4. Higher-dimensional arrays
    5. Working with matrices and linear algebra
      1. Basic methods and properties
      2. Matrix multiplication
      3. Determinants and inverses
      4. Systems of equations
      5. Eigenvalues and eigenvectors
      6. Sparse matrices
    6. Summary
    7. Further reading
  7. Chapter 2: Mathematical Plotting with Matplotlib
    1. Technical requirements
    2. Basic plotting with Matplotlib
      1. Getting ready
      2. How to do it...
      3. How it works…
      4. There’s more…
    3. Adding subplots
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
      5. See also
    4. Plotting with error bars
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There’s more...
    5. Saving Matplotlib figures
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
      5. See also
    6. Surface and contour plots
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
      5. See also
    7. Customizing three-dimensional plots
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
    8. Plotting vector fields with quiver plots
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There’s more…
    9. Further reading
  8. Chapter 3: Calculus and Differential Equations
    1. Technical requirements
    2. Primer on calculus
    3. Working with polynomials and calculus
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
      5. See also
    4. Differentiating and integrating symbolically using SymPy
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
    5. Solving equations
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
    6. Integrating functions numerically using SciPy
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
    7. Solving simple differential equations numerically
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
      5. See also
    8. Solving systems of differential equations
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
    9. Solving partial differential equations numerically
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
      5. See also
    10. Using discrete Fourier transforms for signal processing
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
      5. See also
    11. Automatic differentiation and calculus using JAX
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There’s more…
      5. See also
    12. Solving differential equations using JAX
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    13. Further reading
  9. Chapter 4: Working with Randomness and Probability
    1. Technical requirements
    2. Selecting items at random
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
    3. Generating random data
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
    4. Changing the random number generator
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
    5. Generating normally distributed random numbers
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
    6. Working with random processes
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
    7. Analyzing conversion rates with Bayesian techniques
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
    8. Estimating parameters with Monte Carlo simulations
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
      5. See also
    9. Further reading
  10. Chapter 5: Working with Trees and Networks
    1. Technical requirements
    2. Creating networks in Python
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
    3. Visualizing networks
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
    4. Getting the basic characteristics of networks
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
    5. Generating the adjacency matrix for a network
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
    6. Creating directed and weighted networks
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
    7. Finding the shortest paths in a network
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
    8. Quantifying clustering in a network
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
    9. Coloring a network
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
    10. Finding minimal spanning trees and dominating sets
      1. Getting ready
      2. How to do it...
      3. How it works...
    11. Further reading
  11. Chapter 6: Working with Data and Statistics
    1. What is statistics?
    2. Technical requirements
    3. Creating Series and DataFrame objects
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
      5. See also
    4. Loading and storing data from a DataFrame
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    5. Manipulating data in DataFrames
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
    6. Plotting data from a DataFrame
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
    7. Getting descriptive statistics from a DataFrame
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
    8. Understanding a population using sampling
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    9. Performing operations on grouped data in a DataFrame
      1. Getting ready
      2. How to do it...
      3. How it works...
    10. Testing hypotheses using t-tests
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
    11. Testing hypotheses using ANOVA
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
    12. Testing hypotheses for non-parametric data
      1. Getting ready
      2. How to do it...
      3. How it works...
    13. Creating interactive plots with Bokeh
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
    14. Further reading
  12. Chapter 7: Using Regression and Forecasting
    1. Technical requirements
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
    2. Using multilinear regression
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Classifying using logarithmic regression
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
    4. Modeling time series data with ARMA
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
    5. Forecasting from time series data using ARIMA
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Forecasting seasonal data using ARIMA
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
    7. Using Prophet to model time series data
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
    8. Using signatures to summarize time series data
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There’s more…
      5. See also
    9. Further reading
  13. Chapter 8: Geometric Problems
    1. Technical requirements
    2. Visualizing two-dimensional geometric shapes
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
      5. See also
    3. Finding interior points
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Finding edges in an image
      1. Getting ready
      2. How to do it…
      3. How it works...
    5. Triangulating planar figures
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
      5. See also
    6. Computing convex hulls
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Constructing Bezier curves
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
    8. Further reading
  14. Chapter 9: Finding Optimal Solutions
    1. Technical requirements
    2. Minimizing a simple linear function
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
    3. Minimizing a non-linear function
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
    4. Using gradient descent methods in optimization
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
    5. Using least squares to fit a curve to data
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
    6. Analyzing simple two-player games
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
    7. Computing Nash equilibria
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
      5. See also
    8. Further reading
  15. Chapter 10: Improving Your Productivity
    1. Technical requirements
    2. Keeping track of units with Pint
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
    3. Accounting for uncertainty in calculations
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
    4. Loading and storing data from NetCDF files
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
    5. Working with geographical data
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Executing a Jupyter notebook as a script
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
    7. Validating data
      1. Getting ready
      2. How to do it...
      3. How it works...
    8. Accelerating code with Cython
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
    9. Distributing computing with Dask
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There’s more...
    10. Writing reproducible code for data science
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There’s more…
      5. See also...
  16. Index
    1. Why subscribe?
  17. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share your thoughts
    3. Download a free PDF copy of this book

Product information

  • Title: Applying Math with Python - Second Edition
  • Author(s): Sam Morley
  • Release date: December 2022
  • Publisher(s): Packt Publishing
  • ISBN: 9781804618370