# The Science of Programming/Lather, Rinse, Repeat

As CME, Chapter 7 notes, suppose:
That is to say, y is some function of x. If that is so, then:
is sometimes denoted:
with the apostrophe indicating differentiation.
Because of Sway's liberal views on naming variables, we can use the same notation. Consider defining a line:
where the line constructor is just a wrapper for a call to the plus constructor:
Then, we can differentiate our line thusly:
Since f represents a line and the differentiation of a line yields the slope of a line and the slope of the line is always a constant m, we would expect that evaluating $f\,'$ at different points would always yield the slope:
What happened? The problem is the second term of f'. We end up evaluating that term with x = 0. What is x ^ -1 when x is zero? It's ${\frac {1}{0}}$. That's what caused the divide-by-zero error. What should we do about it? Whenever a term's exponent goes to zero, we should force the exponent to stay at zero, when differentiating.
Here's a new version of diff for term:
Does it work? After remaking, f and f', we have
we see that it does.
If we repeat the differentiation process again, we find out how fast the slope is changing at any given point. For a line, the slope never changes so we would expect the derivative of the derivative (also known as the second derivative) always to yield zero.
Confirmed.

## More on visualization

We really need to do something about our visualization. It's printing out terms that we really don't need to see. Let's simplify the output by making term's toString method more complicated. Please implement the following rules for term's toString method:
* if the coefficient is zero, toString should return "0"
* if the exponent is zero, toString should return "" + a
* if both the coefficient and the exponent are 1, toString should return "x"
* if the exponent is one, toString should return "" + a + "x"
* if the coefficient is one, toString should return "x^" + n
* otherwise, toString should return "" + a + "x^" + n
Earlier rules take precedence over later rules. After implementing these rules and remaking f, f', and f'', we get the following visualizations:
Somewhat better. Now we need to improve plus to throw away constant terms with a value of zero. We can do this in the main body of the plus constructor:
* if the first argument is equivalent to zero, have plus return the second argument
* if the second argument is equivalent to zero, have plus return the first argument
* otherwise, have plus return this
Your logic should look like this:
How do we determine if an argument to plus is equivalent to zero? Since we are representing zero as a term with a zero coefficient, we could use logic like this:
Now our hack of using a term to represent zero is starting to raise its ugly head. Our code is becoming non-obvious to someone who might be reading our code (for maintenance purposes, perhaps) without benefit of reading this text. Zero is a constant, in that it doesn't ever change; zero is always zero. A term, on the other hand, often changes, depending on the value of the independent variable. This leads to a cognitive dissonance that increases the difficulty of understanding our code.
Let's solve this problem by building a 'constant' constructor. This constructor will have all the methods of term, but will better represent a value that never changes. Here is an attempt:
The implementations of diff and y are left as an exercise and should implement the following logic:
* the differential of a constant is a constant zero
* the y value of a constant is always the value of the constant, regardless of the value of the independent variable
Now, we can test for a zero object in plus like this:
See how this code is much more understandable. We now update our diff method in term to take advantage of our new constant constructor.
With these changes and after remaking f, f', and f'' with the new versions of plus, term and constant, we get the following visualizations:
Much better.

## Successive Differentiations of Complex Polynomials

It's rather uneventful to repeatedly differentiate a line. Higher-order polynomials are a little more interesting. Rather than build up a high order polynomial of many terms piece by piece, as in:
We can build it up en masse:
or since this whole scale building can be hard to read, build y using infix operator syntax:
No matter how you make y (it's all a matter of preference), we can differentiate it successively:
To check numerically, let's evaluate y'' at x = 1 and 2.
The object y''' should represent the constant 24:
Everything looks good.

## Questions

1. Justify mathematically each of the simplifying visualization rules for a term.
2. Add simplification to the minus constructor. The simplification is similar to plus but is a tiny bit trickier.
3. Add simplification to the times constructor. You should return constant zero if either argument is zero and if one of the arguments is constant one, return the other. Otherwise, return this.
4. Add simplification to the div constructor. You should return constant zero if the numerator (the first argument) is constant zero. You should return the numerator if the denominator (the second argument) is constant one. Otherwise, you should return this.
5. Represent and plot $y=2x^{2}$ in sway using gnuplot. What is $dy/dx$? Plot it versus x. What does it represent? What is $d^{2}y/dx^{2}$? Plot it versus x. What does it represent? What is $d^{3}y/dx^{3}$? Plot it versus x.
6. Use both sway or pencil and paper to do 1-3 on p. 82.

## Footnotes

* ↑ The higher the order of a polynomial, the larger the largest exponent in the terms making up the polynomial.
← Guzzintas and other Cipherin' · As Time Goes By →