3D graphics, machine learning, and simulations with Python Paul Orland M A N N I N G Mathematical Notation Reference 3 Mathematical Notation Reference Notation example Name Defined in section (x,y) Coordinate(cid:1)vector(cid:1)in(cid:1)2D 2.1.1 sin(x) Trigonometric(cid:1)sine(cid:1)function 2.3.1 cos(x) Trigonometric(cid:1)cosine(cid:1)function 2.3.1 θ Greek(cid:1)letter(cid:1)theta,(cid:1)commonly(cid:1)representing(cid:1)angle(cid:1)measure(cid:1) 2.3.1 π Greek(cid:1)letter(cid:1)pi,(cid:1)representing(cid:1)the(cid:1)number(cid:1)3.14159(cid:1).(cid:1).(cid:1). 2.3.2 (x,y,z) Coordinate(cid:1)vector(cid:1)in(cid:1)3D 3.1.1 u·v Dot(cid:1)product(cid:1)of(cid:1)two(cid:1)vectors(cid:1)u(cid:1)and(cid:1)v 3.3 u×v Cross(cid:1)product(cid:1)of(cid:1)two(cid:1)vectors(cid:1)u(cid:1)and(cid:1)v 3.4 f(g(x)) Composition(cid:1)of(cid:1)functions 4.1.2 f(x,y)versusf(x)(y) See(cid:1)discussion(cid:1)of(cid:1)currying. 4.1.2 au+bv Linear(cid:1)combination(cid:1)(cid:80)(cid:71)(cid:1)two(cid:1)vectors(cid:1)u(cid:1)and(cid:1)v 4.2.3 e1,e2,e3,... Standard(cid:1)basis(cid:1)vectors 4.2.4 ⎛ ⎞ 1 v=⎝2⎠ Column(cid:1)vector 5.1.1 3 ⎛ ⎞ 1 2 3 Matrix 5.1.1 A=⎝4 5 6⎠ 7 8 9 Rn Real(cid:1)coordinate(cid:1)vector(cid:1)space(cid:1)of(cid:1)dimension(cid:1)n 6.2.1 (f+g)(x)orf(x)+g(x) Adding(cid:1)two(cid:1)functions 6.2.3 c·f(x) Scalar(cid:1)multiplication(cid:1)of(cid:1)a(cid:1)function 6.2.3 span({u,v,w}) The(cid:1)span(cid:1)of(cid:1)a(cid:1)set(cid:1)of(cid:1)vectors 6.3.3 f(x)=ax+b Linear(cid:1)function 6.3.5 f(x)=a0+a1x+···+anxn Polynomial(cid:1)function 6.3.5 (continues on inside back cover) Math for Programmers 3D graphics, machine learning and simulations with Python ii Math for Programmers 3D GRAPHICS, MACHINE LEARNING AND SIMULATIONS WITH PYTHON PAUL ORLAND MANNING SHELTER ISLAND For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 761 Shelter Island, NY 11964 Email: [email protected] ©2020 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine. Manning Publications Co. Development editor: Jenny Stout 20 Baldwin Road Technical development editor: Kris Athi PO Box 761 Review editor: Aleks Dragosavljević Shelter Island, NY 11964 Production editor: Lori Weidert Copy editor: Frances Buran Proofreader: Jason Everett Technical proofreader: Mike Shepard Typesetter and cover designer: Marija Tudor ISBN 9781617295355 Printed in the United States of America To my first math teacher and my first programming teacher—Dad. vi brief contents 1 ■ Learning math with code 1 PART 1 VECTORS AND GRAPHICS ............................................... 19 2 ■ Drawing with 2D vectors 21 3 ■ Ascending to the 3D world 75 4 ■ Transforming vectors and graphics 121 5 ■ Computing transformations with matrices 158 6 ■ Generalizing to higher dimensions 205 7 ■ Solving systems of linear equations 257 PART 2 CALCULUS AND PHYSICAL SIMULATION ........................ 301 8 ■ Understanding rates of change 303 9 ■ Simulating moving objects 337 10 ■ Working with symbolic expressions 354 11 ■ Simulating force fields 392 12 ■ Optimizing a physical system 422 13 ■ Analyzing sound waves with a Fourier series 463 PART 3 MACHINE LEARNING APPLICATIONS ............................. 497 14 ■ Fitting functions to data 499 15 ■ Classifying data with logistic regression 526 16 ■ Training neural networks 559 vii viii BRIEF CONTENTS