ebook img

Applying Math with Python: Over 70 practical recipes for solving real-world computational math problems PDF

376 Pages·2022·37.295 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Applying Math with Python: Over 70 practical recipes for solving real-world computational math problems

Applying Math with Python Over 70 practical recipes for solving real-world computational math problems Sam Morley BIRMINGHAM—MUMBAI Applying Math with Python Copyright © 2022 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. Publishing Product Manager: Dinesh Chaudhary Senior Editor: Sushma Reddy Technical Editor: Sweety Pagaria Copy Editor: Safis Editing Project Coordinator: Farheen Fathima Proofreader: Safis Editing Indexer: Tejal Daruwale Soni Production Designer: Joshua Misquitta Marketing Coordinator: Shifa Ansari First published: July 2020 Second published: December 2022 Production reference: 1291122 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-80461-837-0 www.packt.com For my parents... Sam Morley Contributors About the author Sam Morley is a research software engineer and mathematician working at the University of Oxford working on the DataSig project. He was previously a lecturer in mathematics at the University of East Anglia and a researcher in pure mathematics. These days, Sam spends most of his time writing C++ libraries and Python extension modules, but always enjoys writing Python code. 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 and programming. I would like to thank my friends and colleagues at the University of East Anglia, where most of the first edition was written, and at the University of Oxford and the Alan Turing Institute. I would also like to thank my editorial team and technical reviewers for their hard work. About the reviewer Vangos Pterneas helps innovative companies increase their revenue using motion technology and virtual reality. He is an expert in Kinect, HoloLens, Oculus Rift, and HTC Vive. Microsoft has awarded him the title of Most Valuable Professional for his technical contributions to the open source community. Vangos runs LightBuzz Inc, collaborating with clients from all over the world. He’s also the author of Getting Started with HTML5 WebSocket Programming and The Dark Art of Freelancing. Table of Contents Preface xv 1 An Introduction to Basic Packages, Functions, and Concepts 1 Technical requirements 2 Higher-dimensional arrays 11 Exploring Python numerical types 2 Working with matrices and Decimal type 3 linear algebra 13 Fraction type 4 Basic methods and properties 14 Complex type 4 Matrix multiplication 15 Understanding basic mathematical Determinants and inverses 16 functions 5 Systems of equations 18 Eigenvalues and eigenvectors 20 Diving into the world of NumPy 8 Sparse matrices 23 Element access 9 Array arithmetic and functions 10 Summary 26 Useful array creation routines 11 Further reading 26 2 Mathematical Plotting with Matplotlib 27 Technical requirements 28 How to do it... 36 Basic plotting with Matplotlib 28 How it works... 37 There’s more... 38 Getting ready 28 See also 38 How to do it... 29 How it works… 31 Plotting with error bars 38 There’s more… 34 Getting ready 39 Adding subplots 35 How to do it… 39 How it works… 40 Getting ready 35 viii There’s more... 41 Customizing three-dimensional plots 49 Saving Matplotlib figures 42 Getting ready 49 Getting ready 42 How to do it... 49 How to do it... 42 How it works... 51 How it works... 43 There’s more... 51 There’s more... 43 See also 43 Plotting vector fields with quiver plots 52 Surface and contour plots 43 Getting ready 52 Getting ready 43 How to do it… 52 How to do it... 44 How it works… 53 How it works... 46 There’s more… 54 There’s more... 47 See also 48 Further reading 55 3 Calculus and Differential Equations 57 Technical requirements 58 There’s more... 68 Primer on calculus 58 Integrating functions numerically Working with polynomials and using SciPy 70 calculus 59 Getting ready 70 Getting ready 60 How to do it... 71 How to do it... 60 How it works... 71 How it works... 61 There’s more... 72 There’s more... 62 Solving simple differential See also 63 equations numerically 72 Differentiating and integrating Getting ready 73 symbolically using SymPy 63 How to do it... 74 Getting ready 63 How it works... 75 How to do it... 64 There’s more... 77 How it works... 65 See also 78 There’s more... 65 Solving systems of differential Solving equations 66 equations 78 Getting ready 66 Getting ready 78 How to do it... 67 How to do it... 78 How it works... 68 How it works... 81 ix There’s more... 83 Automatic differentiation and calculus using JAX 96 Solving partial differential Getting ready 96 equations numerically 83 How to do it… 96 Getting ready 84 How it works… 98 How to do it... 84 There’s more… 99 How it works... 86 See also 100 There’s more... 88 See also 89 Solving differential equations using JAX 100 Using discrete Fourier transforms Getting ready 100 for signal processing 89 How to do it… 101 Getting ready 90 How it works… 102 How to do it... 90 See also 102 How it works... 94 There’s more... 95 Further reading 103 See also 96 4 Working with Randomness and Probability 105 Technical requirements 106 Generating normally distributed Selecting items at random 106 random numbers 115 Getting ready 106 Getting ready 115 How to do it... 107 How to do it... 116 How it works... 108 How it works... 117 There’s more... 109 There’s more... 118 Generating random data 109 Working with random processes 118 Getting ready 110 Getting ready 119 How to do it... 110 How to do it... 119 How it works... 112 How it works... 122 There’s more... 112 There’s more... 122 Changing the random number generator 113 Analyzing conversion rates with Bayesian techniques 123 Getting ready 113 Getting ready 124 How to do it... 113 How to do it... 124 How it works... 114 How it works... 126 There’s more... 115

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.