Here are all the code listings from the book, bundled together into a zipped directory. Alternatively, you can clone the git repo.
Below, you can browse through the same codes, chapter by chapter. These programs are discussed in detail in the main text so, to avoid duplication, they don’t contain comments.
Chapter 1: Idiomatic Python
forelse.py | Illustration of the for-else idiom |
plotex.py | Basic example of Matplotlib use |
vectorfield.py | Visualizing a vector field via Matplotlib's streamplot() |
Chapter 2: Numbers
kahansum.py | Compensated summation |
naiveval.py | Naive evaluation of a mathematically tricky function |
compexp.py | Computing the exponential function |
recforw.py | Forward recursion |
recback.py | Backward recursion |
cancel.py | Example where rounding errors cancel |
chargearray.py | Constructing an array of electrical charges |
legendre.py | Evaluating Legendre polynomials and their derivatives |
multipole.py | Multipole expansion for point charges |
Chapter 3: Derivatives
finitediff.py | Finite-difference approximations to the first derivative |
richardsondiff.py | Richardson extrapolation for finite differences |
psis.py | Computing the wave function for two physical scenarios |
kinetic.py | Evaluating the local kinetic energy |
Chapter 4: Matrices
triang.py | Infrastructure for triangular matrices and testing |
gauelim.py | Gaussian elimination |
ludec.py | LU decomposition |
gauelim_pivot.py | Gaussian elimination with partial pivoting |
jacobi.py | Jacobi iterative method for linear systems |
power.py | Power method for eigenvalue/eigenvector evaluation |
invpowershift.py | Inverse-power method with shifting |
qrdec.py | QR decomposition |
qrmet.py | QR method for eigenvalues |
eig.py | All eigenvalues and eigenvectors |
svd.py | Singular-value decomposition |
kron.py | Kronecker product of two matrices |
twospins.py | Two interacting spin-half particles |
threespins.py | Three interacting spin-half particles |
Chapter 5: Zeros and Minima
fixedpoint.py | Fixed-point iteration |
bisection.py | Bisection method |
secant.py | Secant method |
legroots.py | Zeros of Legendre polynomials using Newton's method |
multi_newton.py | Newton's method for multidimensional problems |
golden.py | One-dimensional minimization via golden-section search |
descent.py | Multidimensional minimization via gradient descent |
powell.py | Multidimensional minimization via Powell's method |
action.py | Discrete Newton method for extremizing the action |
Chapter 6: Approximation
barycentric.py | Lagrange interpolation using the barycentric formula |
splines.py | Cubic-spline interpolation |
triginterp.py | Trigonometric interpolation using sines and cosines |
fft.py | Fast Fourier transform |
linefit.py | Least-squares straight-line fit |
newnormal.py | Least-squares fitting using the normal equations |
bayes.py | Bayesian linear regression |
neural.py | Neural network nonlinear regression |
newblack.py | Nonlinear fitting for the Stefan-Boltzmann law |
Chapter 7: Integrals
newtoncotes.py | Newton-Cotes quadrature |
adaptive.py | Adaptive integration |
romberg.py | Romberg integration |
gauleg.py | Gauss-Legendre quadrature |
montecarlo.py | Monte Carlo quadrature with importance sampling |
eloc.py | Many-particle wave function and local energy |
vmc.py | Variational Monte Carlo via the Metropolis algorithm |
Chapter 8: Differential Equations
ivp_one.py | Initial-value problem solvers |
ivp_two.py | Runge-Kutta for simultaneous differential equations |
bvp_shoot.py | Boundary-value problem via the shooting method |
bvp_matrix.py | Boundary-value problem via the matrix approach |
evp_shoot.py | Mathieu equation via the shooting method |
evp_matrix.py | Mathieu equation via the matrix approach |
diffusion.py | Diffusion equation via finite-difference methods |
poisson.py | Poisson's equation in two dimensions via the FFT |