# The Science of Programming/Working on the Chain Gang

Sometimes, as SPT in CME, Chapter IX, points out, you find yourself puzzling over how to differentiate something complicated like:
The approach is to make the expression simpler by abstracting away the detail. Let a be the polynomial:
We can represent this using a sum of terms:
Now, y can be rewritten as:
To find the derivative of y with respect to x, we use the chain rule:
In other words, the differential of y with respect to x is equal to the differential of (the rewritten) y with respect to (the new) a multiplied by the differential of (the new) a with respect to x.
Programmatically, we have:
Looking at what dy/dx represents, we have:
Doing the final substitution by hand ($a=x^{2}+17$), we get the final answer for dx/dy:

## Implementing the chain rule

It would be nice to have the chain rule substitution step automatically done for us, but doing so requires a bit of work, both conceptually and programmatically. We begin by extending the idea of abstracting the variable of a term. Recall that, at first, we hard-wired the term variable as x. Next, we allowed the caller of the term constructor to pass in the independent variable as a Sway symbol. The next step in the abstraction is to allow the term variable to be a term (or sum of terms or whatever) itself. If we did so, then a term's diff method would become the chain rule:
Obviously, the independent variable iv can no longer be a symbol, but must be instead an object with a diff method. So, to represent a term of the form:
we would need to use an object to represent the variable x. The constructor for such a variable object would look similar to term and plus constructors. That is, it must have value, toString, and diff methods:
The rule for finding the derivative of a simple variable is: if the with-respect-to variable matches, the result is one. If not, the result is zero. We will use constants to represent the numbers zero and one; in this way, every item in our system, including numbers, has toString, value, and diff methods.
To make our lives simpler, we can add the following logic to the body of the term constructor. If a symbol is passed in as the independent variable, we will convert it into a variable object. In this way, we can pass in a symbol as before. Here is a mock-up of the new term constructor:
We will also need to modify term's toString method to call iv's visualization. Here is the new non-simplifying version:
Let's test our modified system:
It seems to be working so far for simple variables. Now let's try our original problem:
First, we make our polynomial:
Now, we visualize it:
Ouch! What did we do wrong? We need to parenthesize the visualization of iv:
Remaking a and y with term's new visualization yields:
If you use a simplifying toString method for terms, you should get:
exactly as desired! Now let's differentiate y (you will need your times constructor up and running):
Nice!
We still have two little problems. The first is that the above result is not in its simplest form. Unfortunately, producing the simplest form is rather a complex process (compounded by the fact that is is not always clear which form is the simplest). So we will stop at this point and be happy.
The other little problem occurs when we visualize a:
We've gone overboard with the parentheses. Clearly, when the independent variable is a complex object, we want to use parens. When it is a simple variable, however, we should eschew parens. This task is left as an exercise.

## Questions

1. Explain why the diff method for terms no longer needs to test whether or not the with-respect-to variable matches the independent variable.
2. Implement the one function.
3. Modify the simplifying toString method for terms to print out parentheses only when the independent variable is complex and ether the coefficient or the exponent is not equal to one. Hint: Create a term method that adds parentheses around iv's visualization if it is complex but simply returns iv's visualization if it is not. Call this method from toString where appropriate.
4. CME p. 100, 1, 8 using sway
5. CME p. 100, 2, 3, 5, 8 using pencil and paper

## Footnotes

* ↑ This is the heart of the object-oriented approach to programming: related objects have the same methods, but the methods are customized for the particular object.
* ↑ This little trick illustrates an important principle in the design of the computer programs: do as much for the user of your code as possible. We could force the user to pass in a variable object or we could allow the user to pass in a symbol, as before, and do the work ourselves.
← As Time Goes By · Slippery Slopes →