preface |
|
xvii | |
acknowledgments |
|
xxi | |
About this book |
|
xxiii | |
About the author |
|
xxviii | |
About the cover illustration |
|
xxix | |
|
1 Learning math with code |
|
|
1 | (18) |
|
1.1 Solving lucrative problems with math and software |
|
|
2 | (9) |
|
Predicting financial market movements |
|
|
2 | (3) |
|
|
5 | (2) |
|
Building 3D graphics and animations |
|
|
7 | (2) |
|
Modeling the physical world |
|
|
9 | (2) |
|
1.2 How not to learn math |
|
|
11 | (2) |
|
Jane wants to learn some math |
|
|
12 | (1) |
|
Slogging through math textbooks |
|
|
13 | (1) |
|
1.3 Using your well-trained left brain |
|
|
13 | (6) |
|
|
14 | (1) |
|
Build your own calculator |
|
|
15 | (1) |
|
Building abstractions with functions |
|
|
16 | (3) |
|
Part 1 Vectors and graphics |
|
|
19 | (282) |
|
2 Drawing with 2D vectors |
|
|
21 | (54) |
|
|
22 | (10) |
|
|
24 | (2) |
|
|
26 | (3) |
|
|
29 | (3) |
|
2.2 Plane vector arithmetic |
|
|
32 | (19) |
|
Vector components and lengths |
|
|
35 | (2) |
|
Multiplying vectors by numbers |
|
|
37 | (2) |
|
Subtraction, displacement, and distance |
|
|
39 | (3) |
|
|
42 | (9) |
|
2.3 Angles and trigonometry in the plane |
|
|
51 | (16) |
|
From angles to components |
|
|
52 | (4) |
|
Radians and trigonometry in Python |
|
|
56 | (1) |
|
From components back to angles |
|
|
57 | (3) |
|
|
60 | (7) |
|
2.4 Transforming collections of vectors |
|
|
67 | (5) |
|
Combining vector transformations |
|
|
69 | (1) |
|
|
70 | (2) |
|
2.5 Drawing with Matplotlib |
|
|
72 | (3) |
|
3 Ascending to the 3D world |
|
|
75 | (46) |
|
3.1 Picturing vectors in 3D space |
|
|
77 | (6) |
|
Representing 3D vectors with coordinates |
|
|
79 | (1) |
|
|
80 | (2) |
|
|
82 | (1) |
|
3.2 Vector arithmetic in 3D |
|
|
83 | (9) |
|
|
83 | (2) |
|
Scalar multiplication in 3D |
|
|
85 | (1) |
|
|
85 | (1) |
|
Computing lengths and distances |
|
|
86 | (1) |
|
Computing angles and directions |
|
|
87 | (2) |
|
|
89 | (3) |
|
3.3 The dot product: Measuring vector alignment |
|
|
92 | (11) |
|
Picturing the dot product |
|
|
93 | (2) |
|
Computing the dot product |
|
|
95 | (2) |
|
|
97 | (1) |
|
Measuring angles with the dot product |
|
|
97 | (3) |
|
|
100 | (3) |
|
3.4 The cross product: Measuring oriented area |
|
|
103 | (11) |
|
Orienting ourselves in 3D |
|
|
103 | (3) |
|
Finding the direction of the cross product |
|
|
106 | (2) |
|
Finding the length of the cross product |
|
|
108 | (1) |
|
Computing the cross product of 3D vectors |
|
|
109 | (1) |
|
|
110 | (4) |
|
3.5 Rendering a 3D object in 2D |
|
|
114 | (7) |
|
Defining a 3D object with vectors |
|
|
114 | (2) |
|
|
116 | (1) |
|
Orienting faces and shading |
|
|
116 | (3) |
|
|
119 | (2) |
|
4 Transforming vectors and graphics |
|
|
121 | (37) |
|
4.1 Transforming 3D objects |
|
|
123 | (15) |
|
Drawing a transformed object |
|
|
124 | (2) |
|
Composing vector transformations |
|
|
126 | (3) |
|
Rotating an object about an axis |
|
|
129 | (2) |
|
Inventing your own geometric transformations |
|
|
131 | (3) |
|
|
134 | (4) |
|
4.2 Linear transformations |
|
|
138 | (20) |
|
Preserving vector arithmetic |
|
|
138 | (2) |
|
Picturing linear transformations |
|
|
140 | (2) |
|
Why linear transformations? |
|
|
142 | (4) |
|
Computing linear transformations |
|
|
146 | (3) |
|
|
149 | (9) |
|
5 Computing transformations with matrices |
|
|
158 | (47) |
|
5.1 Representing linear transformations with matrices |
|
|
159 | (16) |
|
Writing vectors and linear transformations as matrices |
|
|
159 | (2) |
|
Multiplying a matrix with a vector |
|
|
161 | (2) |
|
Composing linear transformations by matrix multiplication |
|
|
163 | (3) |
|
Implementing matrix multiplication |
|
|
166 | (1) |
|
3D animation with matrix transformations |
|
|
166 | (3) |
|
|
169 | (6) |
|
5.2 Interpreting matrices of different shapes |
|
|
175 | (16) |
|
Column vectors as matrices |
|
|
176 | (2) |
|
What pairs of matrices can be multiplied? |
|
|
178 | (2) |
|
Viewing square and non-square matrices as vector functions |
|
|
180 | (1) |
|
Projection as a linear map from 3D to 2D |
|
|
181 | (3) |
|
|
184 | (2) |
|
|
186 | (5) |
|
5.3 Translating vectors with matrices |
|
|
191 | (14) |
|
Making plane translations linear |
|
|
191 | (3) |
|
Finding a 3D matrix for a 2D translation |
|
|
194 | (1) |
|
Combining translation with other linear transformations |
|
|
195 | (1) |
|
Translating 3D objects in a 4D world |
|
|
196 | (3) |
|
|
199 | (6) |
|
6 Generalizing to higher dimensions |
|
|
205 | (96) |
|
6.1 Generalizing our definition of vectors |
|
|
206 | (13) |
|
Creating a class for 2D coordinate vectors |
|
|
207 | (1) |
|
|
208 | (1) |
|
Repeating the process with 3D vectors |
|
|
209 | (1) |
|
Building a vector base class |
|
|
210 | (2) |
|
|
212 | (2) |
|
Unit testing vector space classes |
|
|
214 | (2) |
|
|
216 | (3) |
|
6.2 Exploring different vector spaces |
|
|
219 | (18) |
|
Enumerating all coordinate vector spaces |
|
|
219 | (2) |
|
Identifying vector spaces in the wild |
|
|
221 | (2) |
|
Treating functions as vectors |
|
|
223 | (3) |
|
Treating matrices as vectors |
|
|
226 | (1) |
|
Manipulating images with vector operations |
|
|
227 | (3) |
|
|
230 | (7) |
|
6.3 Looking for smaller vector spaces |
|
|
237 | (21) |
|
|
238 | (2) |
|
Starting with a single vector |
|
|
240 | (1) |
|
|
240 | (3) |
|
Defining the word dimension |
|
|
243 | (1) |
|
Finding subspaces of the vector space of functions |
|
|
244 | (1) |
|
|
245 | (3) |
|
|
248 | (9) |
|
Solving systems of linear equations |
|
|
257 | (1) |
|
7.1 Designing an arcade game |
|
|
258 | (5) |
|
|
259 | (1) |
|
|
260 | (1) |
|
|
261 | (1) |
|
|
262 | (1) |
|
7.2 Finding intersection points of lines |
|
|
263 | (15) |
|
Choosing the right formula for a line |
|
|
263 | (2) |
|
Finding the standard form equation for a line |
|
|
265 | (2) |
|
Linear equations in matrix notation |
|
|
267 | (1) |
|
Solving linear equations with NumPy |
|
|
268 | (2) |
|
Deciding whether the laser hits an asteroid |
|
|
270 | (1) |
|
Identifying unsolvable systems |
|
|
271 | (2) |
|
|
273 | (5) |
|
7.3 Generalizing linear equations to higher dimensions |
|
|
278 | (16) |
|
Representing planes in 3D |
|
|
278 | (2) |
|
Solving linear equations in 3D |
|
|
280 | (2) |
|
Studying hyperplanes algebraically |
|
|
282 | (1) |
|
Counting dimensions, equations, and solutions |
|
|
283 | (2) |
|
|
285 | (9) |
|
7.4 Changing basis by solving linear equations |
|
|
294 | (7) |
|
|
296 | (1) |
|
|
297 | (4) |
|
Part 2 Calculus and physical simulation |
|
|
301 | (196) |
|
8 Understanding rates of change |
|
|
303 | (34) |
|
8.1 Calculating average flow rate from volume |
|
|
305 | (5) |
|
Implementing an average_flow_rate function |
|
|
305 | (1) |
|
Picturing the average flow rate with a secant line |
|
|
306 | (2) |
|
|
308 | (1) |
|
|
309 | (1) |
|
8.2 Plotting the average flow rate over time |
|
|
310 | (5) |
|
Finding the average flow rate in different time intervals |
|
|
310 | (1) |
|
Plotting the interval flow rates |
|
|
311 | (2) |
|
|
313 | (2) |
|
8.3 Approximating instantaneous flow rates |
|
|
315 | (8) |
|
Finding the slope of small secant lines |
|
|
315 | (3) |
|
Building the instantaneous flow rate function |
|
|
318 | (2) |
|
Currying and plotting the instantaneous flow rate function |
|
|
320 | (2) |
|
|
322 | (1) |
|
8.4 Approximating the change in volume |
|
|
323 | (5) |
|
Finding the change in volume for a short time interval |
|
|
323 | (1) |
|
Breaking up time into smaller intervals |
|
|
324 | (1) |
|
Picturing the volume change on the flow rate graph |
|
|
325 | (3) |
|
|
328 | (1) |
|
8.5 Plotting the volume over time |
|
|
328 | (9) |
|
Finding the volume over time |
|
|
328 | (1) |
|
Picturing Riemann sums for the volume function |
|
|
329 | (3) |
|
Improving the approximation |
|
|
332 | (2) |
|
Definite and indefinite integrals |
|
|
334 | (3) |
|
9 Simulating moving objects |
|
|
337 | (17) |
|
9.1 Simulating a constant velocity motion |
|
|
338 | (4) |
|
Adding velocities to the asteroids |
|
|
339 | (1) |
|
Updating the game engine to move the asteroids |
|
|
339 | (1) |
|
Keeping the asteroids on the screen |
|
|
340 | (2) |
|
|
342 | (1) |
|
9.2 Simulating acceleration |
|
|
342 | (2) |
|
Accelerating the spaceship |
|
|
343 | (1) |
|
9.3 Digging deeper into Euler's method |
|
|
344 | (4) |
|
Carrying out Euler's method by hand |
|
|
344 | (2) |
|
Implementing the algorithm in Python |
|
|
346 | (2) |
|
9.4 Running Euler's method with smaller time steps |
|
|
348 | (6) |
|
|
349 | (5) |
|
10 Working with symbolic expressions |
|
|
354 | (38) |
|
10.1 Finding an exact derivative with a computer algebra system |
|
|
355 | (3) |
|
Doing symbolic algebra in Python |
|
|
356 | (2) |
|
10.2 Modeling algebraic expressions |
|
|
358 | (7) |
|
Breaking an expression into pieces |
|
|
358 | (1) |
|
Building an expression tree |
|
|
359 | (1) |
|
Translating the expression tree to Python |
|
|
360 | (2) |
|
|
362 | (3) |
|
10.3 Putting a symbolic expression to work |
|
|
365 | (9) |
|
Finding all the variables in an expression |
|
|
365 | (1) |
|
|
366 | (3) |
|
|
369 | (3) |
|
|
372 | (2) |
|
10.4 Finding the derivative of a function |
|
|
374 | (7) |
|
|
374 | (1) |
|
Derivatives of transformed functions |
|
|
375 | (2) |
|
Derivatives of some special functions |
|
|
377 | (1) |
|
Derivatives of products and compositions |
|
|
378 | (1) |
|
|
379 | (2) |
|
10.5 Taking derivatives automatically |
|
|
381 | (6) |
|
Implementing a derivative method for expressions |
|
|
382 | (1) |
|
Implementing the product rule and chain rule |
|
|
383 | (1) |
|
Implementing the power rule |
|
|
384 | (2) |
|
|
386 | (1) |
|
10.6 Integrating functions symbolically |
|
|
387 | (5) |
|
Integrals as antiderivatives |
|
|
387 | (1) |
|
Introducing the SymPy library |
|
|
388 | (1) |
|
|
389 | (3) |
|
11 Simulating force fields |
|
|
392 | (30) |
|
11.1 Modeling gravity with a vector field |
|
|
393 | (3) |
|
Modeling gravity with a potential energy function |
|
|
394 | (2) |
|
11.2 Modeling gravitational fields |
|
|
396 | (3) |
|
|
396 | (2) |
|
Defining a simple force field |
|
|
398 | (1) |
|
11.3 Adding gravity to the asteroid game |
|
|
399 | (5) |
|
Making game objects feel gravity |
|
|
400 | (3) |
|
|
403 | (1) |
|
11.4 Introducing potential energy |
|
|
404 | (4) |
|
Defining a potential energy scalar field |
|
|
405 | (2) |
|
Plotting a scalar field as a heatmap |
|
|
407 | (1) |
|
Plotting a scalar field as a contour map |
|
|
407 | (1) |
|
11.5 Connecting energy and forces with the gradient |
|
|
408 | (14) |
|
Measuring steepness with cross sections |
|
|
409 | (2) |
|
Calculating partial derivatives |
|
|
411 | (2) |
|
Finding the steepness of a graph with the gradient |
|
|
413 | (2) |
|
Calculating force fields from potential energy with the gradient |
|
|
415 | (3) |
|
|
418 | (4) |
|
12 Optimizing a physical system |
|
|
422 | (41) |
|
12.1 Testing a projectile simulation |
|
|
425 | (7) |
|
Building a simulation with Euler's method |
|
|
426 | (1) |
|
Measuring properties of the trajectory |
|
|
427 | (1) |
|
Exploring different launch angles |
|
|
428 | (1) |
|
|
429 | (3) |
|
12.2 Calculating the optimal range |
|
|
432 | (8) |
|
Finding the projectile range as a function of the launch angle |
|
|
432 | (3) |
|
Solving for the maximum range |
|
|
435 | (2) |
|
Identifying maxima and minima |
|
|
437 | (2) |
|
|
439 | (1) |
|
12.3 Enhancing our simulation |
|
|
440 | (9) |
|
|
441 | (1) |
|
Modeling terrain around the cannon |
|
|
442 | (1) |
|
Solving for the range of the projectile in 3D |
|
|
443 | (4) |
|
|
447 | (2) |
|
12.4 Optimizing range using gradient ascent |
|
|
449 | (14) |
|
Plotting range versus launch parameters |
|
|
449 | (1) |
|
The gradient of the range function |
|
|
450 | (1) |
|
Finding the uphill direction with the gradient |
|
|
451 | (2) |
|
Implementing gradient ascent |
|
|
453 | (4) |
|
|
457 | (6) |
|
13 Analyzing sound waves with a Fourier series |
|
|
463 | (34) |
|
13.1 Combining sound waves and decomposing them |
|
|
465 | (1) |
|
13.2 Playing sound waves in Python |
|
|
466 | (5) |
|
Producing our first sound |
|
|
467 | (2) |
|
|
469 | (2) |
|
|
471 | (1) |
|
13.3 Turning a sinusoidal wave into a sound |
|
|
471 | (7) |
|
Making audio from sinusoidal functions |
|
|
471 | (2) |
|
Changing the frequency of a sinusoid |
|
|
473 | (2) |
|
Sampling and playing the sound wave |
|
|
475 | (2) |
|
|
477 | (1) |
|
13.4 Combining sound waves to make new ones |
|
|
478 | (8) |
|
Adding sampled sound waves to build a chord |
|
|
478 | (1) |
|
Picturing the sum of two sound waves |
|
|
479 | (2) |
|
Building a linear combination of sinusoids |
|
|
481 | (2) |
|
Building a familiar function with sinusoids |
|
|
483 | (3) |
|
|
486 | (1) |
|
13.5 Decomposing a sound wave into its Fourier series |
|
|
486 | (11) |
|
Finding vector components with an inner product |
|
|
487 | (1) |
|
Defining an inner product for periodic functions |
|
|
488 | (2) |
|
Writing a function to find Fourier coefficients |
|
|
490 | (1) |
|
Finding the Fourier coefficients for the square wave |
|
|
491 | (1) |
|
Fourier coefficients for other waveforms |
|
|
492 | (2) |
|
|
494 | (3) |
|
Part 3 Machine learning applications |
|
|
497 | (98) |
|
14 Fitting functions to data |
|
|
499 | (27) |
|
14.1 Measuring the quality of fit for a function |
|
|
502 | (9) |
|
Measuring distance from a function |
|
|
503 | (2) |
|
Summing the squares of the errors |
|
|
505 | (2) |
|
Calculating cost for car price functions |
|
|
507 | (3) |
|
|
510 | (1) |
|
14.2 Exploring spaces of functions |
|
|
511 | (4) |
|
Picturing cost for lines through the origin |
|
|
512 | (2) |
|
The space of all linear functions |
|
|
514 | (1) |
|
|
515 | (1) |
|
14.3 Finding the line of best fit using gradient descent |
|
|
515 | (4) |
|
|
516 | (1) |
|
Finding and plotting the line of best fit |
|
|
516 | (2) |
|
|
518 | (1) |
|
14.4 Fitting a nonlinear function |
|
|
519 | (7) |
|
Understanding the behavior of exponential functions |
|
|
519 | (2) |
|
Finding the exponential function of best fit |
|
|
521 | (2) |
|
|
523 | (3) |
|
15 Classifying data with logistic regression |
|
|
526 | (33) |
|
15.1 Testing a classification function on real data |
|
|
528 | (4) |
|
|
529 | (1) |
|
Testing the classification function |
|
|
529 | (1) |
|
|
530 | (2) |
|
15.2 Picturing a decision boundary |
|
|
532 | (4) |
|
Picturing the space of cars |
|
|
532 | (1) |
|
Drawing a better decision boundary |
|
|
533 | (1) |
|
Implementing the classification function |
|
|
534 | (1) |
|
|
535 | (1) |
|
15.3 Framing classification as a regression problem |
|
|
536 | (8) |
|
|
536 | (2) |
|
Measuring the "BMWness" of a car |
|
|
538 | (2) |
|
Introducing the sigmoid function |
|
|
540 | (1) |
|
Composing the sigmoid function with other functions |
|
|
541 | (2) |
|
|
543 | (1) |
|
15.4 Exploring possible logistic functions |
|
|
544 | (7) |
|
Parameterizing logistic functions |
|
|
545 | (1) |
|
Measuring the quality of fit for a logistic function |
|
|
546 | (2) |
|
Testing different logistic functions |
|
|
548 | (1) |
|
|
549 | (2) |
|
15.5 Finding the best logistic function |
|
|
551 | (8) |
|
Gradient descent in three dimensions |
|
|
551 | (1) |
|
Using gradient descent to find the best fit |
|
|
552 | (2) |
|
Testing and understanding the best logistic classifier |
|
|
554 | (1) |
|
|
555 | (4) |
|
16 Training neural networks |
|
|
559 | (36) |
|
16.1 Classifying data with neural networks |
|
|
561 | (1) |
|
16.2 Classifying images of handwritten digits |
|
|
562 | (6) |
|
Building the 64-dimensional image vectors |
|
|
563 | (2) |
|
Building a random digit classifier |
|
|
565 | (1) |
|
Measuring performance of the digit classifier |
|
|
566 | (1) |
|
|
567 | (1) |
|
16.3 Designing a neural network |
|
|
568 | (9) |
|
Organizing neurons and connections |
|
|
568 | (1) |
|
Data flow through a neural network |
|
|
569 | (3) |
|
|
572 | (2) |
|
Calculating activations in matrix notation |
|
|
574 | (2) |
|
|
576 | (1) |
|
16.4 Building a neural network in Python |
|
|
577 | (5) |
|
Implementing an MLP class in Python |
|
|
578 | (2) |
|
|
580 | (1) |
|
Testing the classification performance of an MLP |
|
|
581 | (1) |
|
|
582 | (1) |
|
16.5 Training a neural network using gradient descent |
|
|
582 | (6) |
|
Framing training as a minimization problem |
|
|
582 | (2) |
|
Calculating gradients with backpropagation |
|
|
584 | (1) |
|
Automatic training with scikit-learn |
|
|
585 | (1) |
|
|
586 | (2) |
|
16.6 Calculating gradients with backpropagation |
|
|
588 | (7) |
|
Finding the cost in terms of the last layer weights |
|
|
589 | (1) |
|
Calculating the partial derivatives for the last layer weights using the chain rule |
|
|
590 | (1) |
|
|
591 | (4) |
Appendix A Getting set up with Python |
|
595 | (12) |
Appendix B Python tips and tricks |
|
607 | (28) |
Appendix C Loading and rendering 3D Models with OpenGL and PyGame |
|
635 | (10) |
index |
|
645 | |