0%

In Math for Programmers you’ll explore important mathematical concepts through hands-on coding. Filled with graphics and more than 300 exercises and mini-projects, this book unlocks the door to interesting–and lucrative!–careers in some of today’s hottest fields. As you tackle the basics of linear algebra, calculus, and machine learning, you’ll master the key Python libraries used to turn them into real-world software applications.

Table of Contents

  1. Math for Programmers
  2. Copyright
  3. dedication
  4. contents
  5. front matter
    1. preface
    2. How this book was designed
    3. Mathematical ideas we cover
    4. acknowledgments
    5. about this book
    6. Who should read this book?
    7. How this book is organized
    8. About the code
    9. liveBook discussion forum
    10. about the author
    11. about the cover illustration
  6. 1 Learning math with code
    1. 1.1 Solving lucrative problems with math and software
    2. 1.1.1 Predicting financial market movements
    3. 1.1.2 Finding a good deal
    4. 1.1.3 Building 3D graphics and animations
    5. 1.1.4 Modeling the physical world
    6. 1.2 How not to learn math
    7. 1.2.1 Jane wants to learn some math
    8. 1.2.2 Slogging through math textbooks
    9. 1.3 Using your well-trained left brain
    10. 1.3.1 Using a formal language
    11. 1.3.2 Build your own calculator
    12. 1.3.3 Building abstractions with functions
    13. Summary
  7. Part 1. Vectors and graphics
  8. 2 Drawing with 2D vectors
    1. 2.1 Picturing 2D vectors
    2. 2.1.1 Representing 2D vectors
    3. 2.1.2 2D drawing in Python
    4. 2.1.3 Exercises
    5. 2.2 Plane vector arithmetic
    6. 2.2.1 Vector components and lengths
    7. 2.2.2 Multiplying vectors by numbers
    8. 2.2.3 Subtraction, displacement, and distance
    9. 2.2.4 Exercises
    10. 2.3 Angles and trigonometry in the plane
    11. 2.3.1 From angles to components
    12. 2.3.2 Radians and trigonometry in Python
    13. 2.3.3 From components back to angles
    14. 2.3.4 Exercises
    15. 2.4 Transforming collections of vectors
    16. 2.4.1 Combining vector transformations
    17. 2.4.2 Exercises
    18. 2.5 Drawing with Matplotlib
    19. Summary
  9. 3 Ascending to the 3D world
    1. 3.1 Picturing vectors in 3D space
    2. 3.1.1 Representing 3D vectors with coordinates
    3. 3.1.2 3D drawing with Python
    4. 3.1.3 Exercises
    5. 3.2 Vector arithmetic in 3D
    6. 3.2.1 Adding 3D vectors
    7. 3.2.2 Scalar multiplication in 3D
    8. 3.2.3 Subtracting 3D vectors
    9. 3.2.4 Computing lengths and distances
    10. 3.2.5 Computing angles and directions
    11. 3.2.6 Exercises
    12. 3.3 The dot product: Measuring vector alignment
    13. 3.3.1 Picturing the dot product
    14. 3.3.2 Computing the dot product
    15. 3.3.3 Dot products by example
    16. 3.3.4 Measuring angles with the dot product
    17. 3.3.5 Exercises
    18. 3.4 The cross product: Measuring oriented area
    19. 3.4.1 Orienting ourselves in 3D
    20. 3.4.2 Finding the direction of the cross product
    21. 3.4.3 Finding the length of the cross product
    22. 3.4.4 Computing the cross product of 3D vectors
    23. 3.4.5 Exercises
    24. 3.5 Rendering a 3D object in 2D
    25. 3.5.1 Defining a 3D object with vectors
    26. 3.5.2 Projecting to 2D
    27. 3.5.3 Orienting faces and shading
    28. 3.5.4 Exercises
    29. Summary
  10. 4 Transforming vectors and graphics
    1. 4.1 Transforming 3D objects
    2. 4.1.1 Drawing a transformed object
    3. 4.1.2 Composing vector transformations
    4. 4.1.3 Rotating an object about an axis
    5. 4.1.4 Inventing your own geometric transformations
    6. 4.1.5 Exercises
    7. 4.2 Linear transformations
    8. 4.2.1 Preserving vector arithmetic
    9. 4.2.2 Picturing linear transformations
    10. 4.2.3 Why linear transformations?
    11. 4.2.4 Computing linear transformations
    12. 4.2.5 Exercises
    13. Summary
  11. 5 Computing transformations with matrices
    1. 5.1 Representing linear transformations with matrices
    2. 5.1.1 Writing vectors and linear transformations as matrices
    3. 5.1.2 Multiplying a matrix with a vector
    4. 5.1.3 Composing linear transformations by matrix multiplication
    5. 5.1.4 Implementing matrix multiplication
    6. 5.1.5 3D animation with matrix transformations
    7. 5.1.6 Exercises
    8. 5.2 Interpreting matrices of different shapes
    9. 5.2.1 Column vectors as matrices
    10. 5.2.2 What pairs of matrices can be multiplied?
    11. 5.2.3 Viewing square and non-square matrices as vector functions
    12. 5.2.4 Projection as a linear map from 3D to 2D
    13. 5.2.5 Composing linear maps
    14. 5.2.6 Exercises
    15. 5.3 Translating vectors with matrices
    16. 5.3.1 Making plane translations linear
    17. 5.3.2 Finding a 3D matrix for a 2D translation
    18. 5.3.3 Combining translation with other linear transformations
    19. 5.3.4 Translating 3D objects in a 4D world
    20. 5.3.5 Exercises
    21. Summary
  12. 6 Generalizing to higher dimensions
    1. 6.1 Generalizing our definition of vectors
    2. 6.1.1 Creating a class for 2D coordinate vectors
    3. 6.1.2 Improving the Vec2 class
    4. 6.1.3 Repeating the process with 3D vectors
    5. 6.1.4 Building a vector base class
    6. 6.1.5 Defining vector spaces
    7. 6.1.6 Unit testing vector space classes
    8. 6.1.7 Exercises
    9. 6.2 Exploring different vector spaces
    10. 6.2.1 Enumerating all coordinate vector spaces
    11. 6.2.2 Identifying vector spaces in the wild
    12. 6.2.3 Treating functions as vectors
    13. 6.2.4 Treating matrices as vectors
    14. 6.2.5 Manipulating images with vector operations
    15. 6.2.6 Exercises
    16. 6.3 Looking for smaller vector spaces
    17. 6.3.1 Identifying subspaces
    18. 6.3.2 Starting with a single vector
    19. 6.3.3 Spanning a bigger space
    20. 6.3.4 Defining the word dimension
    21. 6.3.5 Finding subspaces of the vector space of functions
    22. 6.3.6 Subspaces of images
    23. 6.3.7 Exercises
    24. Summary
  13. 7 Solving systems of linear equations
    1. 7.1 Designing an arcade game
    2. 7.1.1 Modeling the game
    3. 7.1.2 Rendering the game
    4. 7.1.3 Shooting the laser
    5. 7.1.4 Exercises
    6. 7.2 Finding intersection points of lines
    7. 7.2.1 Choosing the right formula for a line
    8. 7.2.2 Finding the standard form equation for a line
    9. 7.2.3 Linear equations in matrix notation
    10. 7.2.4 Solving linear equations with NumPy
    11. 7.2.5 Deciding whether the laser hits an asteroid
    12. 7.2.6 Identifying unsolvable systems
    13. 7.2.7 Exercises
    14. 7.3 Generalizing linear equations to higher dimensions
    15. 7.3.1 Representing planes in 3D
    16. 7.3.2 Solving linear equations in 3D
    17. 7.3.3 Studying hyperplanes algebraically
    18. 7.3.4 Counting dimensions, equations, and solutions
    19. 7.3.5 Exercises
    20. 7.4 Changing basis by solving linear equations
    21. 7.4.1 Solving a 3D example
    22. 7.4.2 Exercises
    23. Summary
  14. Part 2. Calculus and physical simulation
  15. 8 Understanding rates of change
    1. 8.1 Calculating average flow rate from volume
    2. 8.1.1 Implementing an average_flow_rate function
    3. 8.1.2 Picturing the average flow rate with a secant line
    4. 8.1.3 Negative rates of change
    5. 8.1.4 Exercises
    6. 8.2 Plotting the average flow rate over time
    7. 8.2.1 Finding the average flow rate in different time intervals
    8. 8.2.2 Plotting the interval flow rates
    9. 8.2.3 Exercises
    10. 8.3 Approximating instantaneous flow rates
    11. 8.3.1 Finding the slope of small secant lines
    12. 8.3.2 Building the instantaneous flow rate function
    13. 8.3.3 Currying and plotting the instantaneous flow rate function
    14. 8.3.4 Exercises
    15. 8.4 Approximating the change in volume
    16. 8.4.1 Finding the change in volume for a short time interval
    17. 8.4.2 Breaking up time into smaller intervals
    18. 8.4.3 Picturing the volume change on the flow rate graph
    19. 8.4.4 Exercises
    20. 8.5 Plotting the volume over time
    21. 8.5.1 Finding the volume over time
    22. 8.5.2 Picturing Riemann sums for the volume function
    23. 8.5.3 Improving the approximation
    24. 8.5.4 Definite and indefinite integrals
    25. Summary
  16. 9 Simulating moving objects
    1. 9.1 Simulating a constant velocity motion
    2. 9.1.1 Adding velocities to the asteroids
    3. 9.1.2 Updating the game engine to move the asteroids
    4. 9.1.3 Keeping the asteroids on the screen
    5. 9.1.4 Exercises
    6. 9.2 Simulating acceleration
    7. 9.2.1 Accelerating the spaceship
    8. 9.3 Digging deeper into Euler’s method
    9. 9.3.1 Carrying out Euler’s method by hand
    10. 9.3.2 Implementing the algorithm in Python
    11. 9.4 Running Euler’s method with smaller time steps
    12. Exercises
    13. Summary
  17. 10 Working with symbolic expressions
    1. 10.1 Finding an exact derivative with a computer algebra system
    2. 10.1.1 Doing symbolic algebra in Python
    3. 10.2 Modeling algebraic expressions
    4. 10.2.1 Breaking an expression into pieces
    5. 10.2.2 Building an expression tree
    6. 10.2.3 Translating the expression tree to Python
    7. 10.2.4 Exercises
    8. 10.3 Putting a symbolic expression to work
    9. 10.3.1 Finding all the variables in an expression
    10. 10.3.2 Evaluating an expression
    11. 10.3.3 Expanding an expression
    12. 10.3.4 Exercises
    13. 10.4 Finding the derivative of a function
    14. 10.4.1 Derivatives of powers
    15. 10.4.2 Derivatives of transformed functions
    16. 10.4.3 Derivatives of some special functions
    17. 10.4.4 Derivatives of products and compositions
    18. 10.4.5 Exercises
    19. 10.5 Taking derivatives automatically
    20. 10.5.1 Implementing a derivative method for expressions
    21. 10.5.2 Implementing the product rule and chain rule
    22. 10.5.3 Implementing the power rule
    23. 10.5.4 Exercises
    24. 10.6 Integrating functions symbolically
    25. 10.6.1 Integrals as antiderivatives
    26. 10.6.2 Introducing the SymPy library
    27. 10.6.3 Exercises
    28. Summary
  18. 11 Simulating force fields
    1. 11.1 Modeling gravity with a vector field
    2. 11.1.1 Modeling gravity with a potential energy function
    3. 11.2 Modeling gravitational fields
    4. 11.2.1 Defining a vector field
    5. 11.2.2 Defining a simple force field
    6. 11.3 Adding gravity to the asteroid game
    7. 11.3.1 Making game objects feel gravity
    8. 11.3.2 Exercises
    9. 11.4 Introducing potential energy
    10. 11.4.1 Defining a potential energy scalar field
    11. 11.4.2 Plotting a scalar field as a heatmap
    12. 11.4.3 Plotting a scalar field as a contour map
    13. 11.5 Connecting energy and forces with the gradient
    14. 11.5.1 Measuring steepness with cross sections
    15. 11.5.2 Calculating partial derivatives
    16. 11.5.3 Finding the steepness of a graph with the gradient
    17. 11.5.4 Calculating force fields from potential energy with the gradient
    18. 11.5.5 Exercises
    19. Summary
  19. 12 Optimizing a physical system
    1. 12.1 Testing a projectile simulation
    2. 12.1.1 Building a simulation with Euler’s method
    3. 12.1.2 Measuring properties of the trajectory
    4. 12.1.3 Exploring different launch angles
    5. 12.1.4 Exercises
    6. 12.2 Calculating the optimal range
    7. 12.2.1 Finding the projectile range as a function of the launch angle
    8. 12.2.2 Solving for the maximum range
    9. 12.2.3 Identifying maxima and minima
    10. 12.2.4 Exercises
    11. 12.3 Enhancing our simulation
    12. 12.3.1 Adding another dimension
    13. 12.3.2 Modeling terrain around the cannon
    14. 12.3.3 Solving for the range of the projectile in 3D
    15. 12.3.4 Exercises
    16. 12.4 Optimizing range using gradient ascent
    17. 12.4.1 Plotting range versus launch parameters
    18. 12.4.2 The gradient of the range function
    19. 12.4.3 Finding the uphill direction with the gradient
    20. 12.4.4 Implementing gradient ascent
    21. 12.4.5 Exercises
    22. Summary
  20. 13 Analyzing sound waves with a Fourier series
    1. 13.1 Combining sound waves and decomposing them
    2. 13.2 Playing sound waves in Python
    3. 13.2.1 Producing our first sound
    4. 13.2.2 Playing a musical note
    5. 13.2.3 Exercises
    6. 13.3 Turning a sinusoidal wave into a sound
    7. 13.3.1 Making audio from sinusoidal functions
    8. 13.3.2 Changing the frequency of a sinusoid
    9. 13.3.3 Sampling and playing the sound wave
    10. 13.3.4 Exercises
    11. 13.4 Combining sound waves to make new ones
    12. 13.4.1 Adding sampled sound waves to build a chord
    13. 13.4.2 Picturing the sum of two sound waves
    14. 13.4.3 Building a linear combination of sinusoids
    15. 13.4.4 Building a familiar function with sinusoids
    16. 13.4.5 Exercises
    17. 13.5 Decomposing a sound wave into its Fourier series
    18. 13.5.1 Finding vector components with an inner product
    19. 13.5.2 Defining an inner product for periodic functions
    20. 13.5.3 Writing a function to find Fourier coefficients
    21. 13.5.4 Finding the Fourier coefficients for the square wave
    22. 13.5.5 Fourier coefficients for other waveforms
    23. 13.5.6 Exercises
    24. Summary
  21. Part 3. Machine learning applications
  22. 14 Fitting functions to data
    1. 14.1 Measuring the quality of fit for a function
    2. 14.1.1 Measuring distance from a function
    3. 14.1.2 Summing the squares of the errors
    4. 14.1.3 Calculating cost for car price functions
    5. 14.1.4 Exercises
    6. 14.2 Exploring spaces of functions
    7. 14.2.1 Picturing cost for lines through the origin
    8. 14.2.2 The space of all linear functions
    9. 14.2.3 Exercises
    10. 14.3 Finding the line of best fit using gradient descent
    11. 14.3.1 Rescaling the data
    12. 14.3.2 Finding and plotting the line of best fit
    13. 14.3.3 Exercises
    14. 14.4 Fitting a nonlinear function
    15. 14.4.1 Understanding the behavior of exponential functions
    16. 14.4.2 Finding the exponential function of best fit
    17. 14.4.3 Exercises
    18. Summary
  23. 15 Classifying data with logistic regression
    1. 15.1 Testing a classification function on real data
    2. 15.1.1 Loading the car data
    3. 15.1.2 Testing the classification function
    4. 15.1.3 Exercises
    5. 15.2 Picturing a decision boundary
    6. 15.2.1 Picturing the space of cars
    7. 15.2.2 Drawing a better decision boundary
    8. 15.2.3 Implementing the classification function
    9. 15.2.4 Exercises
    10. 15.3 Framing classification as a regression problem
    11. 15.3.1 Scaling the raw car data
    12. 15.3.2 Measuring the “BMWness” of a car
    13. 15.3.3 Introducing the sigmoid function
    14. 15.3.4 Composing the sigmoid function with other functions
    15. 15.3.5 Exercises
    16. 15.4 Exploring possible logistic functions
    17. 15.4.1 Parameterizing logistic functions
    18. 15.4.2 Measuring the quality of fit for a logistic function
    19. 15.4.3 Testing different logistic functions
    20. 15.4.4 Exercises
    21. 15.5 Finding the best logistic function
    22. 15.5.1 Gradient descent in three dimensions
    23. 15.5.2 Using gradient descent to find the best fit
    24. 15.5.3 Testing and understanding the best logistic classifier
    25. 15.5.4 Exercises
    26. Summary
  24. 16 Training neural networks
    1. 16.1 Classifying data with neural networks
    2. 16.2 Classifying images of handwritten digits
    3. 16.2.1 Building the 64-dimensional image vectors
    4. 16.2.2 Building a random digit classifier
    5. 16.2.3 Measuring performance of the digit classifier
    6. 16.2.4 Exercises
    7. 16.3 Designing a neural network
    8. 16.3.1 Organizing neurons and connections
    9. 16.3.2 Data flow through a neural network
    10. 16.3.3 Calculating activations
    11. 16.3.4 Calculating activations in matrix notation
    12. 16.3.5 Exercises
    13. 16.4 Building a neural network in Python
    14. 16.4.1 Implementing an MLP class in Python
    15. 16.4.2 Evaluating the MLP
    16. 16.4.3 Testing the classification performance of an MLP
    17. 16.4.4 Exercises
    18. 16.5 Training a neural network using gradient descent
    19. 16.5.1 Framing training as a minimization problem
    20. 16.5.2 Calculating gradients with backpropagation
    21. 16.5.3 Automatic training with scikit-learn
    22. 16.5.4 Exercises
    23. 16.6 Calculating gradients with backpropagation
    24. 16.6.1 Finding the cost in terms of the last layer weights
    25. 16.6.2 Calculating the partial derivatives for the last layer weights using the chain rule
    26. 16.6.3 Exercises
    27. Summary
  25. appendix A. Getting set up with Python
    1. A.1 Checking for an existing Python installation
    2. A.2 Downloading and installing Anaconda
    3. A.3 Using Python in interactive mode
    4. A.3.1 Creating and running a Python script file
    5. A.3.2 Using Jupyter notebooks
  26. appendix B. Python tips and tricks
    1. B.1 Python numbers and math
    2. B.1.1 The math module
    3. B.1.2 Random numbers
    4. B.2 Collections of data in Python
    5. B.2.1 Lists
    6. B.2.2 Other iterables
    7. B.2.3 Generators
    8. B.2.4 Tuples
    9. B.2.5 Sets
    10. B.2.6 NumPy arrays
    11. B.2.7 Dictionaries
    12. B.2.8 Useful collection functions
    13. B.3 Working with functions
    14. B.3.1 Giving functions more inputs
    15. B.3.2 Keyword arguments
    16. B.3.3 Functions as data
    17. B.3.4 Lambdas: Anonymous functions
    18. B.3.5 Applying functions to NumPy arrays
    19. B.4 Plotting data with Matplotlib
    20. B.4.1 Making a scatter plot
    21. B.4.2 Making a line chart
    22. B.4.3. More plot customizations
    23. B.5 Object-oriented programming in Python
    24. B.5.1 Defining classes
    25. B.5.2 Defining methods
    26. B.5.3 Special methods
    27. B.5.4 Operator overloading
    28. B.5.5 Class methods
    29. B.5.6 Inheritance and abstract classes
  27. appendix C. Loading and rendering 3D Models with OpenGL and PyGame
    1. C.1 Recreating the octahedron from chapter 3
    2. C.2 Changing our perspective
    3. C.3 Loading and rendering the Utah teapot
    4. C.4 Exercises
  28. index
18.219.224.103