TExcelTAD
Home › Pre-Calculus, Geometry & Trigonometry › Composition and Inverse Functions

Composition and Inverse Functions

Functions can be combined like numbers: added, multiplied, and, most importantly, chained so that the output of one becomes the input of the next. Some functions can also be run backwards. This lesson covers composition and inverses, the two operations behind data pipelines and encode/decode pairs.

Arithmetic of functions

For functions ff and gg, define new functions point by point:

(f+g)(x)=f(x)+g(x)(f−g)(x)=f(x)−g(x)(f⋅g)(x)=f(x)⋅g(x)(f/g)(x)=f(x)/g(x),where g(x)≠0\begin{aligned} (f + g)(x) &= f(x) + g(x) & (f - g)(x) &= f(x) - g(x) \\ (f \cdot g)(x) &= f(x) \cdot g(x) & (f / g)(x) &= f(x) / g(x), \quad \text{where } g(x) \ne 0 \end{aligned}

The domain of each is the overlap of the two domains (minus the zeros of gg for the quotient).

Composition

The composition f∘gf \circ g ("ff after gg") feeds xx into gg, then feeds the result into ff:

(f∘g)(x)=f(g(x))(f \circ g)(x) = f(g(x))

Its domain is the set of xx in the domain of gg for which g(x)g(x) is in the domain of ff. Order matters: usually f∘g≠g∘ff \circ g \ne g \circ f.

Example. f(x)=xf(x) = \sqrt{x} and g(x)=x−3g(x) = x - 3.

(f∘g)(x)=x−3domain x≥3(g∘f)(x)=x−3domain x≥0\begin{aligned} (f \circ g)(x) &= \sqrt{x - 3} && \text{domain } x \ge 3 \\ (g \circ f)(x) &= \sqrt{x} - 3 && \text{domain } x \ge 0 \end{aligned}

Going the other way, decomposing a complicated function into simple steps is a key skill for the chain rule in calculus: h(x)=(2x+1)5h(x) = (2x + 1)^5 is f∘gf \circ g with g(x)=2x+1g(x) = 2x + 1 and f(u)=u5f(u) = u^5.

python · try it yourself
import sympy as sp

x = sp.Symbol("x", real=True)
f = lambda u: sp.sqrt(u)
g = lambda u: u - 3
print("f(g(x)) =", f(g(x)), "   g(f(x)) =", g(f(x)))
print("f(g(12)) =", f(g(12)), "   g(f(12)) =", g(f(12)))

def compose(*fs):
    """compose(f, g, h)(x) == f(g(h(x)))"""
    def h(v):
        for fn in reversed(fs):
            v = fn(v)
        return v
    return h

p = compose(lambda u: u**5, lambda u: 2*u + 1)
print("(2x+1)^5 at x = 1:", p(1))
f(g(x)) = sqrt(x - 3)    g(f(x)) = sqrt(x) - 3
f(g(12)) = 3    g(f(12)) = -3 + 2*sqrt(3)
(2x+1)^5 at x = 1: 243

Inverse functions

A function ff is one-to-one if different inputs always give different outputs. Graphically, every horizontal line meets the graph at most once (the horizontal line test). Exactly the one-to-one functions have an inverse f−1f^{-1}, which undoes ff:

f−1(f(x))=xfor every x in the domain of ff(f−1(y))=yfor every y in the range of fdomain of f−1=range of f,range of f−1=domain of f\begin{aligned} &f^{-1}(f(x)) = x && \text{for every } x \text{ in the domain of } f \\ &f(f^{-1}(y)) = y && \text{for every } y \text{ in the range of } f \\ &\text{domain of } f^{-1} = \text{range of } f, && \text{range of } f^{-1} = \text{domain of } f \end{aligned}

Note: f−1(x)f^{-1}(x) is not 1/f(x)1/f(x). The −1-1 means "inverse function", as in sin⁡−1\sin^{-1}.

Finding an inverse. Write y=f(x)y = f(x), solve for xx, then swap the names.

f(x)=3x−25y=3x−25⇒5y=3x−2⇒x=5y+23f−1(x)=5x+23\begin{aligned} f(x) &= \frac{3x - 2}{5} \\ y = \frac{3x - 2}{5} \quad\Rightarrow\quad 5y = 3x - 2 \quad\Rightarrow\quad x &= \frac{5y + 2}{3} \\ f^{-1}(x) &= \frac{5x + 2}{3} \end{aligned}

Check: f−1(f(4))=f−1(2)=10+23=4f^{-1}(f(4)) = f^{-1}(2) = \frac{10 + 2}{3} = 4. ✓

Graph. Swapping xx and yy reflects the graph across the line y=xy = x. If (a,b)(a, b) is on ff, then (b,a)(b, a) is on f−1f^{-1}.

Restricting the domain. f(x)=x2f(x) = x^2 is not one-to-one on R\mathbb{R}, but on [0,∞)[0, \infty) it is, and its inverse there is x\sqrt{x}. This is exactly how arcsin⁡\arcsin and arccos⁡\arccos were defined in Inverse Trigonometric Functions, and how ln⁡\ln is the inverse of exe^x in the next lessons.

python · try it yourself
import sympy as sp

x, y = sp.symbols("x y", real=True)
f = (3*x - 2) / 5
inv = sp.solve(sp.Eq(y, f), x)[0].subs(y, x)
print("f⁻¹(x) =", inv)
print("f⁻¹(f(x)) =", sp.simplify(inv.subs(x, f)))

g = x**3 + 1
roots = sp.solve(sp.Eq(y, g), x)
print("inverse of x³+1:", roots[0], f"(plus {len(roots) - 1} complex roots)")
print("inverse of x² (two branches):", sp.solve(sp.Eq(y, x**2), x))
f⁻¹(x) = 5*x/3 + 2/3
f⁻¹(f(x)) = x
inverse of x³+1: (y - 1)**(1/3) (plus 2 complex roots)
inverse of x² (two branches): [-sqrt(y), sqrt(y)]

For x3+1x^3 + 1, solve finds all three complex cube roots; the real one, printed as (y - 1)**(1/3), is the inverse y−13\sqrt[3]{y - 1}. (SymPy writes it with a fractional power, which it only treats as real for y≥1y \ge 1; mathematically y−13\sqrt[3]{y - 1} works for every yy.) For x2x^2 it returns two branches, which is SymPy telling you that x2x^2 is not one-to-one.

In software

  • Pipelines are compositions. A Unix pipe cat log | grep ERROR | wc -l, a pandas method chain, middleware in a web server and the layers of a neural network are all f∘g∘hf \circ g \circ h. Keeping each step pure makes the whole chain predictable. The order matters just as in math: normalising then rounding is not rounding then normalising.
  • Encode/decode pairs are inverse functions. base64 encode/decode, JSON serialise/parse, compression/decompression and encryption/decryption must satisfy decode(encode(x)) == x. That equation is the standard round-trip test.
  • Not everything is invertible. A hash function is deliberately not one-to-one in a recoverable way, and lossy compression (JPEG, MP3) throws information away, so no exact inverse exists.
  • Unit conversions like °C ↔ °F or pixels ↔ world coordinates are inverse pairs (screen to world).
python · try it yourself
import base64
import json

def caesar(shift):
    return lambda s: "".join(chr((ord(c) - 97 + shift) % 26 + 97) if c.islower() else c for c in s)

encode, decode = caesar(3), caesar(-3)
msg = "attack at dawn"
print(encode(msg), "->", decode(encode(msg)))

b = base64.b64encode(msg.encode())
print(b.decode(), "->", base64.b64decode(b).decode())

data = {"id": 7, "tags": ["a", "b"]}
print("round trip ok:", json.loads(json.dumps(data)) == data)

c_to_f = lambda c: c * 9 / 5 + 32
f_to_c = lambda f: (f - 32) * 5 / 9
print("37 °C =", c_to_f(37), "°F, back:", f_to_c(c_to_f(37)))
dwwdfn dw gdzq -> attack at dawn
YXR0YWNrIGF0IGRhd24= -> attack at dawn
round trip ok: True
37 °C = 98.6 °F, back: 37.0

Exercises

Exercise 1: Compose

With f(x)=2x+3f(x) = 2x + 3 and g(x)=x2g(x) = x^2, find (f∘g)(x)(f \circ g)(x), (g∘f)(x)(g \circ f)(x) and both at x=−2x = -2.

Answer

(f∘g)(x)=2x2+3(f \circ g)(x) = 2x^2 + 3, (g∘f)(x)=(2x+3)2=4x2+12x+9(g \circ f)(x) = (2x + 3)^2 = 4x^2 + 12x + 9; at −2-2: 11 and 1.

Solution
f(g(x))=2x2+3f(g(−2))=2⋅4+3=11g(f(x))=(2x+3)2g(f(−2))=(−1)2=1\begin{aligned} f(g(x)) &= 2x^2 + 3 & f(g(-2)) &= 2 \cdot 4 + 3 = 11 \\ g(f(x)) &= (2x + 3)^2 & g(f(-2)) &= (-1)^2 = 1 \end{aligned}

Exercise 2: Domain of a composition

Let f(x)=1/xf(x) = 1/x and g(x)=x−4g(x) = x - 4. Find f∘gf \circ g and its domain, and g∘fg \circ f and its domain.

Answer

(f∘g)(x)=1x−4(f \circ g)(x) = \frac{1}{x - 4}, x≠4x \ne 4. (g∘f)(x)=1x−4(g \circ f)(x) = \frac{1}{x} - 4, x≠0x \ne 0.

Solution

f∘gf \circ g needs g(x)≠0g(x) \ne 0, i.e. x≠4x \ne 4. g∘fg \circ f needs xx in the domain of ff, i.e. x≠0x \ne 0; gg accepts every real.

Exercise 3: Find an inverse

Find the inverse of f(x)=x+1x−2f(x) = \frac{x + 1}{x - 2}, x≠2x \ne 2, and state its domain.

Answer

f−1(x)=2x+1x−1f^{-1}(x) = \frac{2x + 1}{x - 1}, domain x≠1x \ne 1.

Solution
y=x+1x−2y(x−2)=x+1xy−2y=x+1xy−x=2y+1x(y−1)=2y+1⇒x=2y+1y−1\begin{aligned} y &= \frac{x + 1}{x - 2} \\ y(x - 2) &= x + 1 \\ xy - 2y &= x + 1 \\ xy - x &= 2y + 1 \\ x(y - 1) &= 2y + 1 \quad\Rightarrow\quad x = \frac{2y + 1}{y - 1} \end{aligned}

The range of ff excludes 1 (f(x)=1f(x) = 1 would need x+1=x−2x + 1 = x - 2), so f−1f^{-1} has domain x≠1x \ne 1.

python · try it yourself
f = lambda x: (x + 1) / (x - 2)
finv = lambda x: (2 * x + 1) / (x - 1)
print([round(finv(f(v)), 12) + 0.0 for v in (-3, 0, 5, 10)])
[-3.0, 0.0, 5.0, 10.0]

Exercise 4: Restrict to invert

f(x)=(x−3)2+1f(x) = (x - 3)^2 + 1 is not one-to-one. Restrict it to a domain where it is, and find the inverse there.

Answer

On [3,∞)[3, \infty): f−1(x)=3+x−1f^{-1}(x) = 3 + \sqrt{x - 1}, for x≥1x \ge 1.

Solution

The vertex is at x=3x = 3, so the right half [3,∞)[3, \infty) is increasing.

y=(x−3)2+1⇒x−3=y−1(positive root, since x≥3)f−1(x)=3+x−1,x≥1\begin{aligned} y = (x - 3)^2 + 1 \quad&\Rightarrow\quad x - 3 = \sqrt{y - 1} && \text{(positive root, since } x \ge 3\text{)} \\ f^{-1}(x) &= 3 + \sqrt{x - 1}, \quad x \ge 1 \end{aligned}

Exercise 5: Pipeline and round trip

Write pipeline = compose(str.strip, str.lower)-style code that normalises a username (strip spaces, lowercase, then replace spaces with "_"), apply it to " Ada Lovelace ", and explain why this pipeline has no inverse.

Answer

Result: ada_lovelace. It is not one-to-one: "ADA lovelace" and " ada lovelace" give the same output, so the original cannot be recovered.

Solution
python · try it yourself
def compose(*fs):
    def h(v):
        for fn in reversed(fs):
            v = fn(v)
        return v
    return h

normalise = compose(lambda s: s.replace(" ", "_"), str.lower, str.strip)
print(normalise("  Ada Lovelace "), normalise("ADA lovelace"))
ada_lovelace ada_lovelace

Test yourself

If f(x)=x+1f(x) = x + 1 and g(x)=3xg(x) = 3x, what is (f∘g)(2)(f \circ g)(2)?

  1. 6
  2. 7
  3. 9
  4. 12

Which function has an inverse on all of R\mathbb{R}?

  1. x2x^2
  2. ∣x∣\lvert x \rvert
  3. x3x^3
  4. cos⁡x\cos x

A test checks decode(encode(x)) == x for many x. In math terms it checks that…

  1. encode is even
  2. decode is a left inverse of encode
  3. encode∘decode\text{encode} \circ \text{decode} is constant
  4. encode is periodic