# The Science of Programming/Auld Lang Sine

You have, in all likelihood, previously made acquaintance with sines and cosines. Sinusoids (which include sines and cosines) are periodic functions. Periodic functions have the following property:
for all x and certain values of p. The smallest value of p for which the above equation holds is known as the period of the function.
If you not familiar with sines and cosine functions, this is what a sine wave looks like:
Note that for sine waves without phase and frequency shifts (as above), when x is zero, the amplitude (y-value) of the sine wave is zero (with a rising slope). The wave reaches zero again (with a rising slope) x = 2π. Contrast this with the cosine wave:
Unlike the sine wave, which has zero-crossings at multiples of π, the cosine wave has peaks and troughs at multiples of π. As with the sine wave, the period, or peak to peak length, is 2π.

## The general sine wave

If you look closely at the two waves, you'll see that the cosine is just the sine wave shifted ${\frac {\pi }{2}}$ units to the left. So, we can define cosine in terms of sine:
Note that mathematicians often abbreviate sine to sin and cosine to cos. The above illustrates one of the common ways to modify a sine wave: shifting the phase of the wave, for which the symbol $\theta$ is often used:
Another modification is to change the amplitude of a sine wave or, on other words, to change how high and how low the peaks range. The variable a is used to indicate amplitude, so the formula for a sine wave that allows amplitude modification is:
Finally, it is common to change the frequency of sine waves, or how many peaks (or troughs) are in a given region. The variable $\omega$ is often used for this task:
If we set $\omega$ to 2, we will get twice as many peaks (or troughs) within a given region. The inverse of frequency is period, so setting $\omega$ to 2 will shorten the peak to peak distance by half. In the particular case of frequency 2, the period of the sine wave would be π.
Here's a neat trick. If you want to find the location of the 'first' zero crossing, of a general sign wave of this form, change the sign of both the phase shift and the operator combining it with x and then factor out $\omega$. For cosine, with unit amplitude and frequency doubled, we have:
This version of cosine has its first zero crossing at $x=-{\frac {\pi }{4}}$.
In other words, to find the first zero crossing, divide the phase shift by the frequency and negate it (assuming that the phase shift is being added in).

## Implementing the general sine wave

Sway has the functions sin and cos built-in, but these functions assume amplitude = 1, frequency = 1, and phase shift = 0.
To implement sine and cosine, we will take our usual object approach:
Note how we make cosine a wrapper for sine as cosine is just sine with a phase shift.
Let's take that little trick we learned in the previous section for finding the 'first' zero crossing and implement it:
Let's see if it works for cosine with frequency 2:
Good. Let's also check that the value of the sine wave is indeed zero at that point:
Bingo!

## Differentiating sine and cosine

As SPT points out in Chapter XV in CMT, the derivative of sine is cosine and the derivative of cosine is the negative of sine.
Using the first rule, we can add a diff function to the sine constructor. Of course, in keeping with our unassuming differentiation system, we need to pass in independent and with-respect-to variables, as appropriate:
Of course, this implementation assumes that the independent variable and the with-respect-to variable are one and the same. It also assumes that the independent variable is just a symbol. As such, we could not construct a sine wave of the form;
To do so, we will have to take the same approach as for terms, by allowing the independent variable to be a differentiable object. Recall the term constructor:
Recall also how the diff function implements the chain rule. We will need to follow the same strategy for our sine constructor:
All that's left for us to do is implement our visualization for sine (and, of course, test):

## What is -sin?

According to CMT, the derivative of sine is cosine and the derivative of cosine is -sin. Therefore, the second derivative of sine is -sine.

## Questions

1. What is the difference between the function $\sin(x+{\frac {\pi }{2}})$ and the function $\sin(x)+{\frac {\pi }{2}}$?
2. Why can't we name sine and cosine constructors sin and cos?
3. Add the following simplification to the sine constructor. If the phase shift is equal to or greater than 2 π, subract off 2 π.
4. Explain why the previous simplification is mathematically valid.
5. Simplify the construction of sine objects so that a zero object is generated if the amplitude is zero.
6. Simplify the construction of sine objects so that a constant term object is generated if the frequency is zero.
7. Simplify the visualization of sine objects so that the amplitude, frequency, and phase shift are omitted if they are 1, 1, and 0, respectively.
8. Find and add other visualization mods. Hint: what should your visualization look like if the frequency is != 1 but the independent variable is a term with a coefficient != 1?

## Footnotes

* ↑ Contrast this with the cosine wave with unit amplitude and frequency (shown at the beginning of this chapter), which has a zero crossing at $x=-{\frac {\pi }{2}}$.
* ↑ There is also a generalized cosine function: $y=a\cos(\omega x+\theta )$. Of course, this is equal to $a\sin(\omega x+{\frac {\pi }{2}}+\theta )$.
← The Rich Get Richer · Do What You Can →