Codes

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.pyIllustration of the for-else idiom
plotex.pyBasic example of Matplotlib use
vectorfield.pyVisualizing a vector field via Matplotlib's streamplot()

Chapter 2: Numbers

kahansum.pyCompensated summation
naiveval.pyNaive evaluation of a mathematically tricky function
compexp.pyComputing the exponential function
recforw.pyForward recursion
recback.pyBackward recursion
cancel.pyExample where rounding errors cancel
chargearray.pyConstructing an array of electrical charges
legendre.pyEvaluating Legendre polynomials and their derivatives
multipole.pyMultipole expansion for point charges

Chapter 3: Derivatives

finitediff.pyFinite-difference approximations to the first derivative
richardsondiff.pyRichardson extrapolation for finite differences
psis.pyComputing the wave function for two physical scenarios
kinetic.pyEvaluating the local kinetic energy

Chapter 4: Matrices

triang.pyInfrastructure for triangular matrices and testing
gauelim.pyGaussian elimination
ludec.pyLU decomposition
gauelim_pivot.pyGaussian elimination with partial pivoting
jacobi.pyJacobi iterative method for linear systems
power.pyPower method for eigenvalue/eigenvector evaluation
invpowershift.pyInverse-power method with shifting
qrdec.pyQR decomposition
qrmet.pyQR method for eigenvalues
eig.pyAll eigenvalues and eigenvectors
svd.pySingular-value decomposition
kron.pyKronecker product of two matrices
twospins.pyTwo interacting spin-half particles
threespins.pyThree interacting spin-half particles

Chapter 5: Zeros and Minima

fixedpoint.pyFixed-point iteration
bisection.pyBisection method
secant.pySecant method
legroots.pyZeros of Legendre polynomials using Newton's method
multi_newton.pyNewton's method for multidimensional problems
golden.pyOne-dimensional minimization via golden-section search
descent.pyMultidimensional minimization via gradient descent
powell.pyMultidimensional minimization via Powell's method
action.pyDiscrete Newton method for extremizing the action

Chapter 6: Approximation

barycentric.pyLagrange interpolation using the barycentric formula
splines.pyCubic-spline interpolation
triginterp.pyTrigonometric interpolation using sines and cosines
fft.pyFast Fourier transform
linefit.pyLeast-squares straight-line fit
newnormal.pyLeast-squares fitting using the normal equations
bayes.pyBayesian linear regression
neural.pyNeural network nonlinear regression
newblack.pyNonlinear fitting for the Stefan-Boltzmann law

Chapter 7: Integrals

newtoncotes.pyNewton-Cotes quadrature
adaptive.pyAdaptive integration
romberg.pyRomberg integration
gauleg.pyGauss-Legendre quadrature
montecarlo.pyMonte Carlo quadrature with importance sampling
eloc.pyMany-particle wave function and local energy
vmc.pyVariational Monte Carlo via the Metropolis algorithm

Chapter 8: Differential Equations

ivp_one.pyInitial-value problem solvers
ivp_two.pyRunge-Kutta for simultaneous differential equations
bvp_shoot.pyBoundary-value problem via the shooting method
bvp_matrix.pyBoundary-value problem via the matrix approach
evp_shoot.pyMathieu equation via the shooting method
evp_matrix.pyMathieu equation via the matrix approach
diffusion.pyDiffusion equation via finite-difference methods
poisson.pyPoisson's equation in two dimensions via the FFT