New Functions from Old Functions
Complicated functions are built from simple ones in three ways: by transforming a graph (shifting, stretching, reflecting), by combining functions with arithmetic, and by composing one function inside another. Knowing how a function was built tells you its graph and its domain, and composition in particular is what the chain rule will differentiate.
For a first look, see Graph Transformations and Composition and Inverses.
Transformations
Let . Starting from the graph of :
| New function | Effect on the graph |
|---|---|
| , | shift up / down by |
| , | shift right / left by |
| stretch vertically by (compress if ) | |
| compress horizontally by (stretch if ) | |
| reflect in the -axis | |
| reflect in the -axis | |
| reflect the parts below the -axis upward |
The rule that trips people up is the horizontal one: moves the graph right. The reason is that the new function reaches the old value when , that is at . Changes inside the function act on and look "backwards"; changes outside act on and look as expected.
When several transformations are combined, apply them in the order that the expression is evaluated: inside first (horizontal), outside last (vertical). For , write it as : compress horizontally by 3, shift right , stretch vertically by 2, shift up 1.
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-np.pi, 2 * np.pi, 600)
base = np.sin(x)
new = 2 * np.sin(3 * x - np.pi) + 1
plt.figure(figsize=(6, 4))
plt.plot(x, base, label="sin x")
plt.plot(x, new, label="2 sin(3x − π) + 1")
plt.axhline(1, color="grey", lw=0.5, ls="--")
plt.legend()
print("range of new:", round(float(new.min()), 3), "to", round(float(new.max()), 3))
print("period:", round(2 * np.pi / 3, 4), " a cycle starts at x =", round(np.pi / 3, 4))range of new: -1.0 to 3.0
period: 2.0944 a cycle starts at x = 1.0472
A cycle starts where , at : there the graph crosses its midline going up, just as crosses 0 going up at .
Combinations
For functions and , the sum, difference, product and quotient are defined pointwise: , and so on. The domain is where both are defined, , and for we also remove the points where .
Composition
The composition is defined by : first apply , then . Its domain is the set of in the domain of for which lies in the domain of . Composition is not commutative: and are usually different.
import sympy as sp
x = sp.symbols('x', real=True)
f = sp.sqrt(x) # domain x >= 0
g = 2 - x**2 # domain all x
fg = f.subs(x, g) # f(g(x))
gf = g.subs(x, f) # g(f(x))
print("f∘g =", fg)
print("g∘f =", gf)
print("domain of f∘g:", sp.solve_univariate_inequality(g >= 0, x, relational=False))
print("(f∘g)(1) =", fg.subs(x, 1), " (g∘f)(1) =", gf.subs(x, 1))f∘g = sqrt(2 - x**2)
g∘f = 2 - x
domain of f∘g: Interval(-sqrt(2), sqrt(2))
(f∘g)(1) = 1 (g∘f)(1) = 1
Notice the trap in : SymPy simplifies to and prints , which looks defined everywhere. But only accepts , because must be computed first. Simplifying a formula can erase its domain, just as cancelling a factor erased a hole in the diagnostic review.
Decomposition
The reverse skill, recognising a given function as a composition, matters more in calculus than building one. To differentiate with the chain rule, you must see it as three layers:
Read the layers in the order a calculator would evaluate them: first , then the sine, then the square. A decomposition is not unique (you could merge the inner two layers into ), but the most useful one has each layer be a single basic function from the catalogue.
import math
from functools import reduce
def compose(*fs):
"""compose(f, g, k)(x) == f(g(k(x))), applied right to left."""
return reduce(lambda f, g: lambda x: f(g(x)), fs)
k = lambda x: 5 * x + 1
g = math.sin
f = lambda v: v ** 2
h = compose(f, g, k)
for x in [0, 0.3, 1]:
print(x, round(h(x), 6), round(math.sin(5 * x + 1) ** 2, 6))0 0.708073 0.708073
0.3 0.358169 0.358169
1 0.078073 0.078073
In software
Composition is the backbone of programming: function pipelines, middleware stacks, Unix pipes, map chains and, above all, neural networks, where each layer is a function and the network is their composition, . Training such a network needs the derivative of a composition, which is exactly what backpropagation computes (Backpropagation Is the Chain Rule). Transformations appear wherever a signal or curve is re-timed or re-scaled: , , in audio, animation and UI code.
Exercises
Exercise 1: Read the transformations
Describe how to get the graph of from , and give the vertex and the direction it opens.
Answer
Shift left 3, stretch vertically by 2, reflect in the -axis, shift up 1. Vertex , opens downward.
Solution
Inside: shifts the graph 3 units left, moving the vertex from to . Outside: the factor stretches by 2 and flips, so it opens downward; raises the vertex to .
Exercise 2: Domain of a quotient
For and , find the domain of .
Answer
with and , that is .
Solution
needs , so . is defined everywhere but the quotient excludes , that is . Both lie in , so both are removed.
Exercise 3: Decompose
Write as with each layer a simple function.
Answer
, , .
Solution
Evaluate as a calculator would: square first (), then take the sine and add 1 (), then the square root (). You could also split into and , giving four layers.
Exercise 4: Evaluate a composition in code
For and , compute exactly by hand, then with the compose helper using Fraction.
Answer
.
Solution
, then .
from fractions import Fraction as F
from functools import reduce
compose = lambda *fs: reduce(lambda f, g: lambda x: f(g(x)), fs)
f = lambda x: x**2 - 1
g = lambda x: 1 / (x + 1)
print(compose(f, g)(F(2)))-8/9
Exercise 5: When do they commute?
For and , find all with .
Answer
Only .
Solution
Test yourself
How is the graph of related to the graph of ?
- Shifted left 4
- Shifted right 4
- Shifted down 4
- Stretched by 4
For and , what is the domain of ?
- all real
Which decomposition of is correct?
- , ,
- , ,
- , ,