Physicists routinely use numerical methods to carry out tasks like interpolation, integration, or root-finding in their daily work. To implement such methods in their computer programs, practicing scientists either “roll their own” or opt for “canned programs” (i.e., solutions for common tasks which somebody else has provided, typically as part of a library). When the original code is unavailable (or never looked at), canned programs are also known as “black boxes”.
In my recently published textbook, Numerical Methods in Physics with Python, I opt against the use of black boxes. Instead, I show students how to mathematically derive numerical techniques, how to implement them in the popular programming language Python, as well as how to use them to study problems that show up in physics.
there’s something vaguely aristocratic about the admonition “no need to trouble your head with that”
Much of my book is intentionally dedicated to reinventing the wheel. This may give rise to the question: what’s wrong with students taking standard methods for granted and, instead, spending all their time learning how to apply them to physics? In what follows, I go over several interrelated arguments for why doing things “from scratch” is a good idea. While I have computational physics in mind, many of the points I make below may also be of interest to those working in other areas of STEM.
- What’s wrong with focusing on the physics instead of getting distracted by programming? This brings to mind the trend of de-emphasizing deltas and epsilons when teaching calculus. Of course, even hard-core anti-rigorists wouldn’t dream of telling beginners that, e.g., integrals are a mathematical tool, so physics students don’t need to trouble themselves with mastering the concept. Similarly, one shouldn’t let students tackle complicated numerical integration problems without first exposing them to the standard techniques. Having a solid grounding in math and programming makes it easier (not harder) to focus on the physics.
- What’s wrong with students using a code they found on the web? In today’s climate, with fake news being a constant cause for concern, it is surprising to see students sometimes being willing to blindly trust programs of unknown provenance (as long the relevant website is official-looking). An article I recently wrote with a colleague discusses examples where standard textbooks themselves cannot always be trusted, so random preprints or websites are clearly all the more suspect.
- What’s wrong with reading the documentation of one’s favorite library to decide which technique to use? It is true that excellent libraries do exist (e.g., NumPy and SciPy in the Python world) and it’s perfectly OK for an experienced user to employ their functionality. However, even if you trust the source, knowing which specific numerical method to use is not so trivial: a library’s documentation, typically made up of a list of functions, cannot provide much qualitative insight. As an example, a web search for libraries on interpolation will nudge you toward the use of splines (i.e., you likely wouldn’t even learn that Lagrange interpolation is a good idea). Other times, the documentation merely states that the library provides a wrapper to another library’s functionality.
- What’s wrong with using a library’s documentation to see what to do when the algorithm fails? This is certainly possible. However, it’s common for the documentation to lag behind the functionality contained in the code itself. This is often the case for numerical methods: the documentation typically doesn’t provide any equations, so there are technical aspects (e.g., analytical manipulations that could help, or related techniques with different convergence properties) that the reader wouldn’t even know to look for.
- What’s wrong with reading the actual code for a function in an open-source library? The short answer: you could, but you likely won’t. Even if you do read it, you’re almost certainly not going to like what you see. Libraries are designed to be efficient and general; as a result, they don’t typically make for good bedtime reading. Even so, it is important for beginners to learn how to structure their own programs well, so reading well-written (and intelligible) example code is valuable, just like reading good prose can help with one’s writing.
- What’s wrong with acknowledging that most people are users, not library authors, so they shouldn’t have to do things the hard way? First, all library authors started out as users; they were lucky enough to get good foundations that allowed them to progress. Second, there’s something vaguely aristocratic about the admonition “no need to trouble your head with that”. Third, even though the typical physics student is not going to go on to rewrite NumPy from scratch, students may end up writing widely used programs in a lab, in the industry, in finance, or in the software sector.
- What’s wrong with just taking somebody else’s word for it? The very question goes against the culture associated with modern science; trying to figure things out for oneself is a crucial aspect of doing physics. (Not for nothing is Nullius in verba the Royal Society’s motto.) The personality traits that make one uncomfortable with a code they cannot read/understand are the same as those that make one uncomfortable with a recipe-type approach to numerical methods, namely a code that implements a formula which drops from the sky. In Descartes’ famous image, when you’re walking alone in the dark, you would be well-advised to move slowly: you might not get very far, but at least you would be guarding against the possibility of falling.
The majority of Numerical Methods in Physics with Python is devoted to deriving numerical methods from scratch; the 57 computer programs (also found at the companion website https://numphyspy.org) are a crucial component of the book. Of course, deriving and implementing everything from scratch is merely a noble ideal; I had to make some tough decisions about where to start and where to stop. As per another memorable image (due to Neurath), we are like sailors who must rebuild their ship on the open sea, so we cannot start afresh from the bottom.