Shift And Scale: Revisiting Confusing High School Math

Posted in math

When I took high school trigonometry, we spent a lot of time on function transformations. As useless and boring as they seemed at the time, understanding them is crucial to higher-level math. They’re especially useful when modeling signals (electronic or other) as functions of time and manipulating them in various ways.

There was one part of transformations I never managed to intuit, though: the rules for shifting and scaling the function by operating on its argument, like $\sin(5x-3)$. It may seem obvious what exactly that means; I thought I was the only one who didn’t get it until the subject turned into a 15-minute discussion and multiple mistakes in a 2nd year Stanford EE lecture. Try for yourself: without plugging in a value, which of the following two curves is $\sin(2x - \frac \pi 2)$?

Did you hesitate? I couldn’t find any resource giving an intuitive explanation how exactly these transformations work, so let’s return to the basics for a moment, and try to fully wrap our heads around some confusing high school math. We’ll return the two sine waves at the end.

It seems be easy. Imagine a function $f(x)$, say $f(x) = x^3 -x$ (shown in black).

  1. A factor in front of the argument, like $f(ax)$, makes the function “faster” by a factor of $a$ (shown in blue). We’ll call this a “scale” along the x-axis. For $|a| < 1$, this “expands” or “dilates” the function, and for $|a| > 1$ it “compresses” or “contracts” the function.
  2. An offset, like $f(x - d)$ shifts the function $d$ units to the right (shown in red). If $f$ is a function of time, this can be thought of as delaying it $d$ seconds.

Composing these two transformations is easy, just use the order of operations. $f(ax - d)$ (shown in red) is the same as scaling $f$ by $a$ (giving the blue curve), then shifting that by $d$ (giving the final, green curve). This is graphed below for $a=0.5$, $d=1.5$.

Wait, it’s not the same. Open the desmos link if you want to double-check what’s what or play with the numbers.

This brings us to the confusing part. With $g(x) = 5 + 3f(x)$, $g(x)$ is just the result of stretching $f(x)$ by a factor of $3$ vertically and then moving it $5$ units up. But, unlike operations on the output of a function, operations on the input do not obey the order of operations in this way. Of course, you respect the order of operations when evaluating them, but the operations you do on the argument $x$ do not directly correspond to the visible transformations on $f(x)$.

It’s at this point that most teachers, including my EE lecturer, will tell you to plug in numbers. Let’s try it. Define and graph $h(x)$:

$$h(x) = f(ax - d)$$

Scaling the function by $a$, and then shifting it by $d$, we’d expect the value at $x=0$ to be unchanged by the scale, and moved $d$ units to the right by the shift, ending up at $x=d$. In symbols, that’s $(0, f(0)) \rightarrow (d, f(0))$, or $h(d) = f(0)$. Let’s plug it in:

$$h(d) = f(ad - d) = f(d(a-1))$$

Not exactly what we wanted.

Where does the value of $f(0)$ actually end up in $h$? Solve for it:

$$f(0) = h(x) \implies f(0) = f(ax-d) \implies 0 = ax - d \implies x = \frac d a$$

Clearly, something is wrong with our initial hypothesis.

Confusion over this makes what should be a simple mental step into a potential landmine. As you may have had drilled into you by your high school math teacher, the function we want for a scale-then-shift is:

$$h(x) = f(a(x-d))$$

Try the plugging in the values again and show yourself that it works. This begins to reveal something I was never explicitly taught about function transformations: Operations on the argument manifest visually in reverse order of operations. Though PEMDAS tells us that we multiply by $a$ after subtracting $d$ from $x$, the transformations are performed as if the reverse were true; $h(x)$ is $f(x)$ scaled (multiply by $a$) then shifted (subtract $d$). Why? Let’s think about function transformations using symbols instead of graphs for a moment.

Let $\tau_d$ denote translation (or delay, if you like), by $d$:

$$(\tau_d f)(x) = f(x-d)$$

Let $\psi_a$ denote scaling the argument by $a$:

$$(\psi_a f)(x) = f(ax)$$

These two transformations can be thought of as taking a function, called $f$ here, and giving us a new function. If we think of these transformations “functions”, which they are, they are called “higher order functions,” because they are functions that operate on functions. Here’s a quick example: $\tau_{\frac \pi 2} \cos(x) = \cos(x - \frac \pi 2) = \sin(x)$.

The transformation that we want—a scale, then a shift—is clearly $\tau_d(\psi_a f(x))$, which we can readily evaluate, parenthesis first like normal:

$$\tau_d(\psi_a f(x)) = \tau_d(f(ax)) = f(a(x-d))$$

If that’s confusing, it can be helpful to define an intermediate function, $g(x) = f(ax)$.

$$\tau_d(\psi_a f(x)) = \tau_d(f(ax)) = \tau_d(g(x)) = g(x-d) = f(a(x-d))$$

This also reveals that our original expression, $f(ax-d)$ is the result transformations applied in the reverse order, a shift-then-scale.

$$\psi_a (\tau_d f(x)) = \psi_a f(x-d) = f(ax - d)$$

This made me pause for a moment when I wrote it. It’s helpful to remember that function evaluation is basically literal substitution into an expression, in this case, $ax$ for the $x$ in $x-d$. A shift-then-scale and a scale-then-shift do not give the same results, as we showed earlier by plugging in points. These two transformations don’t commute because scaling the argument stretches or compresses the function around $x=0$, not where $(0, f(0))$ is now.

Evaluating the shift-then-scale $f(ax-d)$, the value of $f(0)$ doesn’t end up at $x=d$, as it would in a scale-then-shift. It ends up there after the shift, only to move to $x=\frac d a$ after scaling occurs, as shown above. To see this effect visually, open up this graph, slide $d$ to 1.5, then play around with $a$. Or, watch this animation doing just that:

Animation showing the aforementioned effect

Thinking about transformations as higher-order function application reveals why the order of operations is reversed. Function application can be thought of as literal substitution. Substituting $x-d$, the argument of a shift, into $ax$, the argument of a scale, gives $a(x-d)$. This is equivalent to applying a shift to a scale. The first operation on $x$ is the last substitution we performed and thus corresponds to the last transformation we applied. This seems counter-intuitive on the surface. If I ask what $f(g(h(x)))$ is, you evaluate $h$ then $g$ then $f$, but if I ask what transformation $f(g(h(x)))$ represents on $f(x)$, it’s the transformation represented by $g$ then the transformation represented by $h$.

Let’s prove our understanding by applying it to a less common case. Say we want to perform more operations than just shifting and scaling. What about, say, cubing and shifting? This is slightly harder because it’s more difficult to picture what cubing the argument to a function looks like. Let’s get a feel for it using $\sin(x)$ and $\sin(x^3)$:

The sine wave is expanded in the middle, but compressed further out outside. Give a moment of thought to the reason why. Think about what $x^3$ looks like near $x=0$ and $x=1$. Click to reveal:

If we then imagine shifting the orange function, the result is what you’d expect (shown in blue).

Let’s do the transformations in the opposite order:

This seems intuitive. When you expand the shifted function, shown in red, it’s the part around $x=0$ that gets expanded. Now try to picture the results in your head using the expressions plotted to make the graph:

\begin{align} &f\left(x\right)=\sin\left(\frac{\pi}{5}x\right) \\
&f((x-d)^3) \\
&f\left(x^3 - d \right) \end{align}

Which is expression is which graph? If it helps, consider this form that includes the intermediate transforms:

\begin{align} &f\left(x\right)=\sin\left(\frac{\pi}{5}x\right) \\
&g\left(x\right)=f\left(x^3\right) \\
&g\left(x-d\right) \\
&h\left(x\right)=f\left(x-d\right) \\
&h\left(x^3\right) \end{align}

Remember, function application is like a literal substitution, meaning the order of operations is reversed. .

Now let’s look at the original two sine waves again. Which of these is $\sin(2x - \frac \pi 2)$?

Hopefully this cleared up any remaining confusion you had about these transformations. It certainly helped me. If not, then hopefully it helped you remember how counterintuitive even basic math can be sometimes. In the words of Grant Sanderson, “It’s extremely hard to empathize with what it feels like not to understand something.”