First edition

Codes (first edition)

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
kron.pyKronecker product of two matrices
twospins.pyTwo interacting spin-half particles
threespins.pyThree interacting spin-half particles

Chapter 5: Roots

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
descent.pyMinimization via the gradient descent 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
normalfit.pyLeast-squares fitting using the normal equations
blackbody.pyNon-linear 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
poisson.pyPoisson's equation in two dimensions via the FFT

Errata (first edition)

  • p. 124 For elimination (third bullet point) not to impact the determinant, this should refer (not to a linear combination, but) to “the sum of that row/equation with a multiple of any other row/equation”.
  • p. 306 Problem 11 should say “Taylor expand f(x^{(k-1)}) and f(x^{(k-2)})“. Then, it should say “Plug these relations”.
  • p. 363 “Observe that in \partial p(x)” –> “Observe that in \partial p(x_j)
  • p. 390 Problem 29 should be referring to polynomials with 2, 3, and 4 parameters (and “third degree” –> “three-parameter case” and “fourth-degree” –> “four-parameter case”)
  • p. 436 In Eq. (7.120), there should be no product symbol
  • p. 437 In Eq. (7.128), the sum should be over k
  • p. 459 In fn 42, the integrand should be using x’
  • p. 490 In problem 26, it would be more appropriate to say that I_4 can be given in terms of the first and second complete elliptic integrals
  • p. 491 In problem 31, Y_0 = (X_0 - \mu)/\sigma^2 –> Y_0 = (X_0 - \mu)/\sigma
  • p. 491 In problem 34, you’re supposed to use Gauss-Legendre for “n = 5-30
  • p. 493 Problem 43: “the fact that H_1(x) = 2x” –> “the needed Hermite polynomials”
  • p. 556 Problem 25, w(0)=1 –> w(0)=0