# The Science of Programming/The Simplest Things

If you have studied Chapter 4 of CME, you will have learned the power rule for differentiating simple polynomial functions, known as terms. A term has the form:
with a being known as the coefficient of the term and n being known as the exponent of the term.
Here is the power rule for terms:
As example, consider the polynomial:
If the power rule is correct, then the derivative should be:
Let's test. We start out by defining a function to represent the polynomial:
As before, the dependent variable becomes the name of the function and the independent variable becomes the formal parameter.
Before we continue, now would be a good time to learn about using Sway with files, since typing into the interpreter can be rather painful for function definitions.
Thus for the rest of this book, even though interactions with the interpreter are shown, you should be storing Sway programs in files and running Sway programs from files.
The derivative, or ${\frac {dy}{dx}}$ can be written as this function:
Is there any way to test whether or not the power rule is correct? Recall our revised ratio function from the previous chapter:
As you should remember, the ratio it computes is an approximation for the derivative at a given x. Note that we've changed the names of the formal parameters, w to x, dw to dx, and h to y, to reflect the fact that we are working with polynomials now.
We believe that ratio (for small dx) produces a number that should be close to what the function dy/dx produces.
Let's define a testing function:
Given a value for x, the test function will print out the two results. Time for some nomenclature. When we use our ratio function, we are finding the derivative numerically. That is to say, we use a small number for dx, find the resulting dy, and then compute the ratio ${\frac {dy}{dx}}$. On the other hand, using the power rule (as embodied in the function dy/dx), we find the derivative symbolically since we never actually compute an actual ratio.
For this test at least, we see that there is good agreement between our numeric and symbolic solutions.
Clearly, though, the symbolic solution is preferred because:
* it is exact
* it is easier to compute
You may be asking yourself, can we write a program to find a derivative symbolically?

## A Shortcoming to Our Approach

It certainly would be nice if we could use the symbolic approach to finding derivatives instead of our numeric approach using ratios. The problem is that our function representing a term:
has the coefficient 2 and the exponent 3 hard-wired. If we are given such a function but aren't privy to the internal details (such as the value of the hard-wired exponent), we cannot create the derivative function since we need to know the value of the exponent to do so and have no way to get at it. We need to contrive a way to retrieve the exponent from the function so we can use it to construct the derivative function.
The next section is to give you an exposure to one approach for extracting the components of a function. The approach uses objects.
If you don't know what an object is or how to create one in Sway, please read about using objects in The Sway Reference Manual now.
OK, now that you are familiar with objects, we can begin to use objects to write a program that finds a derivative symbolically.

## Polynomial Objects and the Power Rule

Instead of using a function to represent the polynomial y, we are going to use an object instead. From studying the reference manual, you know that an object is simply an environment and an environment is simply a table of variables and their values. You also know that you use a function to create an object and that a function that creates an object is conventionally known as a constructor. Finally, you know that to define a constructor function, you have that function return the predefined variable this.
Here's a constructor for a simple polynomial object. We name the constructor term:
Note that the constructor includes an internal function for computing the value of y given an x, just as before. We call that function by using the dot operator.
Indeed, $2*4^{3}$ is truly 128 and $2*5^{3}$ is truly 250.
Now comes the good part! Storing a polynomial term as an object rather than a function allows us to extract the coefficient a and the exponent n:
We can even pretty print the object y to see all its fields:
We see, among other fields, the correct values for a and n. Later on, you will understand the meaning and purpose of the predefined fields: context, dynamicContext, and constructor. The this field you already know.
We want access to a and n because the power rule needs them to compute the derivative polynomial. Now we can write an expression that allows us to compute the derivative:
Of course, we could write a function to do this task for us:
Note that the powerRule function takes a term object as an argument and returns a new term object that represents the derivative. This is because the new term has a coefficient of a * n and an exponent of n - 1, just like the power rule dictates. Let's check and make sure our power rule works as intended:
Indeed it does!
Moreover, pretty printing z shows us the correct values for a and n:
You may not have noticed it, but we have a little lack of symmetry. While the term object has an internal function for computing its value, we use an external function, powerRule to compute its derivative. When we program using objects, we use internal functions when possible. So let's rewrite the term function so that it can compute its own derivative:
We name this new internal function, diff, to indicate taking the differential of our object. Note how we have dispensed with the variables coeff and exp inside the diff function and just used a and n directly.
Finally, we add a third internal function to term. This one is used to visualize our object in a more concise way than using the pp function. By convention, we will call this function toString:
The toString function converts both the coefficient and the exponent to strings and then concatenates those strings together to form a string representation of the term:
Notice how much easier it is to pick out the coefficient and the exponent of a term by using the toString function. Another way to formulate the toString function takes advantage of the fact that if you add a string and a number (with the string on the left hand side of the plus sign, the number is automatically converted to a string. That, with the fact that Sway combines mathematical operators from left-to-right, lets us remove the string conversion of n:
If we use an empty string to start the expression, we can remove the first string conversion as well:

## A Whole System of Objects

From here on out, we are going to develop a system for finding the derivative (and eventually, the integral) of many kinds of mathematical objects (limited only by our imagination and attention span). Each object in our system will do (at least) three things. The first is to compute its value at a given spot. The second is to compute its derivative (and eventually, its integral). The third is to compute its visualization. So all our mathematical objects will have value, diff, and toString internal functions or methods.
Even the simplest real-world mathematical objects such as numbers and variables will need to be re-created as Sway objects. That way, we can always take the derivative of an object without having to ask first if taking the derivative makes sense.

## Questions

All formulas are written using Grammar School Precedence.
1. Why does the Sway implementation of $2x^{3}$ have parentheses: 2 * (x ^ 3)?
2. Using the powerRule function, can you take the derivative of a derivative? Explain.
3. Write a function that uses the power rule function to answer the following questions on p. 58 of Thompson: 1, 2, 4, 6, 7. Name your function p58. Your function should calculate, then print the answers to the questions.
4. Using pencil and paper, work exercises 3, 8, 9, and 10 on p. 58 of Thompson.

## Footnotes

* ↑ In general, it does not matter what names are used for the formal parameters, so we could have left the ratio function untouched and it would still work exactly the same.
* ↑ In the world of object-oriented programming (which is another way of saying programming with objects), these internal functions are called methods. We will use the term method from now one. Just remember, a method is simply an internal function.
← A Little of This, A Little of That · Constant Worries →