Before I show you how to use literate programming, let’s look at what it replaces: scripts. Scripts are files of code that are meant to be run on their own. They may produce results, or format data and save it somewhere, or scrape data from the web – scripts can do just about anything.
Scripts can even have documentation within the file, using # characters (at least, in R and python) at the beginning of a line. # indicates a comment – that is, that the line does not contain code and should be ignored by the computer when the program is run. Comments are incredibly useful to help humans understand what the code does and why it does it.
This code will use concepts we have not yet introduced - feel free to tinker with it if you want, but know that you’re not responsible for being able to write this code yet. You just need to read it and get a sense for what it does. I have heavily commented it to help with this process.
# Define the angle of the spiral (polar coords)# go around two full times (2*pi = one revolution)theta <-seq(0, 4*pi, .01) # Define the distance from the origin of the spiral# Needs to have the same length as thetar <-seq(0, 5, length.out =length(theta))# Now define x and y in cartesian coordinatesx <- r *cos(theta)y <- r *sin(theta)plot(x, y, type ="l")
Figure 1: A Cartesian Spiral in R
We can also plot a logarithmic spiral in python using the same essential steps. Notice how the fundamentals are the same, but things like how to reference the value of \(\pi\) or the length of a vector are slightly different.
import numpy as npimport matplotlib.pyplot as plt# Define the angle of the spiral (polar coords)# go around two full times (2*pi = one revolution)theta = np.arange(0, 4* np.pi, 0.01)# Define the distance from the origin of the spiral# Needs to have the same length as theta # (get length of theta with theta.size, # and then divide 5 by that to get the increment)r = np.arange(0, 5, 5/theta.size)# Now define x and y in cartesian coordinatesx = r * np.cos(theta)y = r * np.sin(theta)# Define the axesfig, ax = plt.subplots()# Plot the lineax.plot(x, y)plt.show()