# The inverse of a function


{{< include ../_common_code.qmd >}}

In this section we will use this add-on package:


```{julia}
using Plots
plotly();
```

---


A (univariate) mathematical function relates or associates values of $x$ to values $y$ using the notation $y=f(x)$. A key point is a given $x$ is associated with just one $y$ value, though a given $y$ value may be associated with several different $x$ values. (Graphically, this is the horizontal line test.)


We may conceptualize such a relation in many ways:

* through an algebraic rule;
* through the graph of $f$;
* through a description of what $f$ does;
* or through a table of paired values, say.

For the moment, let's consider a function as a rule that takes in a value of $x$ and outputs a value $y$. If a rule is given defining the function, the computation of $y$ is straightforward. A different question is not so easy: for a given value $y$ what value---or *values*---of $x$ (if any) produce an output of $y$? That is, what $x$ value(s) satisfy $f(x)=y$?


*If* for each $y$ in some set of values there is just one $x$ value, then this operation associates to each value $y$ a single value $x$, so it too is a function. When that is the case we call this an *inverse* function.


Why is this useful? When available, it can help us solve equations. If we can write our equation as $f(x) = y$, then we can "solve" for $x$ through $x = g(y)$, where $g$ is this inverse function.


Let's explore when we can "solve" for an inverse function.


Consider @fig-plot-2-x-showing-x-to-y-and-y-to-x showing a  graph of the function $f(x) = 2^x$.

::: {#fig-plot-2-x-showing-x-to-y-and-y-to-x}
```{julia}
#| echo: false
p = let
    gr()
	empty_style = (xaxis=([], false),
                    yaxis=([], false),
                    framestyle=:origin,
                    legend=false)
	axis_style = (arrow=true, side=:head, line=(:gray, 1))
	text_style = (10,)
	fn_style =  (;line=(:black, 3))
	fn2_style =  (;line=(:red, 4))
	mark_style = (;line=(:gray, 1, :dot))
	domain_style = (;fill=(:orange, 0.35), line=nothing)
	range_style = (; fill=(:blue, 0.35), line=nothing)

	f(x) = 2^x
	a, b = 0, 2
	plot(; empty_style...)
	xs = range(a, b, 200)
	ys = f.(xs)

	plot!( xs,  ys; fn_style...)
	plot!([a-1/4, b+.2], [0,0]; axis_style...)
	plot!([0, 0], [-.1, f(2.1)]; axis_style...)

	x = 1
	y = (f(b)+f(a))/2
	plot!([x,x,0],[0,f(x),f(x)]; line=(:black, 1, :dash), arrow=true, side=:head)
	plot!([0,log2(y), log2(y)], [y,y,0]; line=(:black,1,:dash), arrow=true, side=:head)

	annotate!([
	(x, 0, text(L"c", 10, :top)),
		(0,f(x), text(L"f(c)", 10, :right)),
		(0, y, text(L"y=f(d)", 10, :right)),
		(log2(y), 0, text(L"d",  10, :top))

	])


end
plotly()
p
```

Plot of $f(x) = 2^x$ illustrating how to graphically start from $x=c$ to find $f(c)$ and how to start from $y=f(d)$ to find $d$
:::

The graph of a function is a representation of points $(x,f(x))$, so to *find* $y = f(c)$ from the graph, we begin on the $x$ axis at $c$, move vertically to the graph (the point $(c, f(c))$), and then move horizontally to the $y$ axis, intersecting it at $y = f(c)$. This is how an $x$ is associated to a single $y$.


If we were to *reverse* the direction, starting at $y = f(d)$ on the $y$ axis and then moving horizontally to the graph, and then vertically to the $x$-axis we end up at a value $d$ with the correct value of $f(d)$. This allows solving for $x$ knowing $y$ in $y=f(x)$.

The operation described will form a function **if** the initial movement horizontally is guaranteed to find *no more than one* value on the graph. That is, to have an inverse function, there can not be two $x$ values corresponding to a given $y$ value. This observation is often visualized through the "horizontal line test"---the graph of a function with an inverse function can only intersect a horizontal line at most in one place.


More formally, a function is called *one-to-one* *if* for any two $a \neq b$, it must be that $f(a) \neq f(b)$. Many functions are one-to-one, many are not. Familiar one-to-one functions are linear functions ($f(x)=a \cdot x + b$ with $a\neq 0$), odd powers of $x$ ($f(x)=x^{2k+1}$), and functions of the form $f(x)=x^{1/n}$ for $x \geq 0$. In contrast, all *even* functions are *not* one-to-one, as $f(x) = f(-x)$ for any nonzero $x$ in the domain of $f$.


A class of functions that are guaranteed to be one-to-one are the *strictly* increasing functions (which satisfy $a < b$ implies $f(a) < f(b)$). Similarly, strictly decreasing functions are one-to-one. The term strictly *monotonic* is used to describe either strictly increasing or strictly decreasing.  By the above observations, strictly monotonic function will have inverse functions.


The function $2^x$, graphed above, is strictly increasing, so it will have an inverse function. That is we can solve for $x$ in an equation like $2^x = 9$ using the inverse function of $f(x) = 2^x$, provided we can identify the inverse function.


## How to solve for an inverse function?


If we know an inverse function exists, how can we find it?


If our function is given by a graph, the process above describes how to find the inverse function.


However, typically we have a rule describing our function. What is the process then?  A simple example helps illustrate. The *linear* function $f(x) = 9/5\cdot x + 32$ is strictly increasing, hence has an inverse function. What should it be? Let's describe the action of $f$: it multiplies $x$ by $9/5$ and then adds $32$. To "invert" this we *first* invert the adding of $32$ by subtracting $32$, then we would "invert" multiplying by $9/5$ by *dividing* by $9/5$. Hence $g(x)=(x-32)/(9/5)$. We would generally simplify this, but let's not for now. If we view a function as a composition of many actions, then we find the inverse by composing the inverse of these actions in **reverse** order. The reverse order might seem confusing, but this is how we get dressed and undressed: to dress we put on socks and then shoes. To undress we take off the shoes and then take off the socks.


When we solve algebraically for $x$ in $y=9/5 \cdot x + 32$ we do the same thing as we do verbally: we subtract $32$ from each side, and then divide by $9/5$ to isolate $x$:


$$
\begin{align*}
y &= 9/5 \cdot x + 32\\
y - 32 &= 9/5 \cdot x\\
(y-32) / (9/5) &= x.
\end{align*}
$$

From this, we have the function $g(y) = (y-32) / (9/5)$ is the inverse function of $f(x) =  9/5\cdot x + 32$.


*Usually* univariate functions are written with $x$ as the dummy variable, so it is typical to write $g(x) = (x-32) / (9/5)$ as the inverse function.


*Usually* we use the name $f^{-1}$ for the inverse function of $f$, so this would be most often [seen](http://tinyurl.com/qypbueb) as $f^{-1}(x) = (x-32)/(9/5)$ or after simplification $f^{-1}(x) = (5/9) \cdot (x-32)$.


:::{.callout-note}
## Note
The use of a negative exponent on the function name is *easily* confused for the notation for a reciprocal when it is used on a mathematical *expression*. An example might be the notation $(1/x)^{-1}$. As this is an expression this would simplify to $x$ and not the inverse of the *function* $f(x)=1/x$ (which is $f^{-1}(x) = 1/x$).

:::

##### Example


Suppose a transformation of $x$ is given by $y = f(x) = (ax + b)/(cx+d)$. This function is  invertible for most choices of the parameters. Find the inverse and describe it's domain.


From the expression $y=f(x)$ we *algebraically* solve for $x$:


$$
\begin{align*}
y &= \frac{ax +b}{cx+d}\\
y \cdot (cx + d) &= ax + b\\
ycx - ax &= b - yd\\
(cy-a) \cdot x &= b - dy\\
x &= -\frac{dy - b}{cy-a}.
\end{align*}
$$

We see that to solve for $x$ we need to divide by $cy-a$, so this expression can not be zero. So, using $x$ as the dummy variable, we have


$$
f^{-1}(x) = -\frac{dx - b}{cx-a},\quad  cx-a \neq 0.
$$

##### Example


The function $f(x) = (x-1)^5 + 2$ is strictly increasing and so will have an inverse function. Find it.


Again, we solve algebraically starting with $y=(x-1)^5 + 2$ and solving for $x$:


$$
\begin{align*}
y &= (x-1)^5 + 2\\
y - 2 &= (x-1)^5\\
(y-2)^{1/5} &= x - 1\\
(y-2)^{1/5} + 1 &= x.
\end{align*}
$$

We see that $f^{-1}(x) = (x - 2)^{1/5} + 1$. The fact that the power $5$ is an odd power is important, as this ensures a unique (real) solution to the fifth root of a value, in the above $y-2$.


In the section on  [polynomial roots](../precalc/polynomial_roots.html) we introduce the `solve` function of `SymPy`, which can  algebraically solve for inverse functions in easier cases.



##### Example


The function $f(x) = x^x,  x \geq 1/e$ is strictly increasing. However, trying to algebraically solve for an inverse function will quickly run into problems (without using specially defined functions). The existence of an inverse does not imply there will always be luck in trying to find a mathematical rule defining the inverse.


In the section on the intermediate value theorem we will see how to *numerically* solve for an inverse function.


## Functions which are not always invertible


Consider the function $f(x) = x^2$. The graph---a parabola---is clearly not *monotonic*. Hence no inverse function exists. Yet, we can solve equations $y=x^2$ quite easily: $y=\sqrt{x}$ *or* $y=-\sqrt{x}$. We know the square root undoes the squaring, but we need to be a little more careful to say the square root is the inverse of the squaring function.


The issue in this case is there are generally *two* possible answers. To avoid this, we might choose to only take the *non-negative* answer. To make this all work as above, we restrict the domain of $f(x)$ and now consider the related function $f(x)=x^2, x \geq 0$. This is now a monotonic function, so will have an inverse function. This is clearly $f^{-1}(x) = \sqrt{x}$. (The $\sqrt{x}$ being defined as the principle square root or the unique *non-negative* answer to $u^2-x=0$.)


The [inverse function theorem](https://en.wikipedia.org/wiki/Inverse_function_theorem) basically says that if $f$ is *locally* monotonic, then an inverse function will exist *locally*. By "local" we mean in a neighborhood of $c$.


##### Example


Consider the function $f(x) = (1+x^2)^{-1}$. This bell-shaped function is even (symmetric about $0$), so can not possibly be one-to-one. However, if the domain is restricted to $[0,\infty)$ it is. The restricted function is strictly decreasing and its inverse is found, as follows:


$$
\begin{align*}
y &= \frac{1}{1 + x^2}\\
1+x^2 &= \frac{1}{y}\\
x^2 &= \frac{1}{y} - 1\\
x &= \sqrt{(1-y)/y}, \quad 0 < y \leq 1.
\end{align*}
$$

Then $f^{-1}(x) = \sqrt{(1-x)/x}$ where $0 < x \leq 1$. The somewhat complicated restriction for the domain coincides with the range of $f(x)$. We shall see next that this is no coincidence.


## Formal properties of the inverse function


Consider again the graph of a monotonic function, in this case $f(x) = x^2 + 2, x \geq 0$:

::: {#fig-plot-xsquared-plus-2-domain-range}
```{julia}
#| hold: true
f(x) = x^2 + 2
plot(f, 0, 4; yticks=[2,4,8,16],
     legend=false, framestyle=:origin)
plot!([(2,0), (2, f(2)), (0, f(2))])
```

Plot of $f(x) = x^2 + 2$ over $[0,4]$
:::

The graph is shown over the interval $(0,4)$, but the *domain* of $f(x)$ is all $x \geq 0$. The *range* of $f(x)$ is clearly $2 \leq y \leq \infty$.


The lines layered on the plot show how to associate an $x$ value to a $y$ value or vice versa (as $f(x)$ is one-to-one). The domain then of the inverse function is all the $y$ values for which a corresponding $x$ value exists: this is clearly all values bigger or equal to $2$. The *range* of the inverse function can be seen to be all the images for the values of $y$, which would be all $x \geq 0$. This gives the relationship:


::: {.relationship title="The domain and range"}
The *domain* of $f^{-1}(x)$ is the *range* of $f(x)$.

The *range*  of $f^{-1}(x)$ is the *domain* of $f(x)$.
:::

From this we can see if we start at $x$, apply $f$ we get $y$, if we then apply $f^{-1}$ we will get back to $x$ so we have:


::: {.relationship title = "Composition with an inverse"}
For all $x$ in the domain of $f$: $f^{-1}(f(x)) = x$.

For all $x$ in the domain of $f^{-1}$: $f(f^{-1}(x)) = x$.

In short $f^{-1} \circ f$ and $f \circ f^{-1}$ are both identity functions, though on possibly different domains.
:::


## The graph of the inverse function


The graph of $f(x)$ is a representation of all values $(x,y)$ where $y=f(x)$. As the inverse flips around the role of $x$ and $y$ we have:

::: {.relationship title="Mirror points"}
 If $(x,y)$ is a point on the graph of $f(x)$, then $(y,x)$ will be a point on the graph of $f^{-1}(x)$.
:::


Let's see this in action. Take the function $2^x$. We can plot it by generating points to plot as follows:

::: {#fig-plot-2-to-x-and-inverse-with-xs-ys}
```{julia}
#| hold: true
f(x) = 2^x
xs = range(0, 2, length=50)
ys = f.(xs)
plot(xs, ys; aspect_ratio=:equal, framestyle=:origin,
     xlims=(0,4),
     color=:blue, label="f")
plot!(ys, xs; color=:red, label="f⁻¹") # the inverse
```

Plot of $f(x) = 2^x$ using `plot(xs, ys)` and its inverse produced with `plot!(ys, xs)`
:::

By flipping around the $x$ and $y$ values in the `plot!` command, we produce the graph of the inverse function---when viewed as a function of $x$. We can see that the domain of the inverse function (in red) is clearly different from that of the function (in blue).


The inverse function graph can be viewed as a symmetry of the graph of the function. Flipping the graph for $f(x)$ around the line $y=x$ will produce the graph of the inverse function: @fig-plot-cbrt-and-its-inverse show the graph of $f(x) = x^{1/3}$ and its inverse function:

::: {#fig-plot-cbrt-and-its-inverse}
```{julia}
#| hold: true
f(x) = cbrt(x)
xs = range(-2, 2, length=150)
ys = f.(xs)
plot(xs, ys; aspect_ratio=:equal, legend=false,
     line=(:blue,))
plot!(ys, xs; line=(:red,))
plot!(identity; line=(:green, :dash))
x = 1/4
y = f(x)
plot!([(x,y), (y,x)]; line=(:green, :dot))
```

Plot of $f(x) = x^{1/3}$ and its inverse
:::

In @fig-plot-cbrt-and-its-inverse we drew a line connecting $(1/4, f(1/4))$ to $(f(1/4),1/4)$. We can see that this line crosses the line $y=x$ perpendicularly, indicating that points are symmetric about the $y=x$ line. (The plotting argument `aspect_ratio=:equal` ensures that the $x$ and $y$ axes are on the same scale, so that this type of line will look perpendicular.)

One consequence of this symmetry, is that if $f$ is strictly increasing, then so is its inverse.

::: {.callout-note}
## `cbrt(x)` is different from `x^(1/3)`
In the above we used `cbrt(x)` and not `x^(1/3)`. The latter usage assumes that $x \geq 0$ as it isn't guaranteed that for all real exponents the answer will be a real number. The `cbrt` function knows there will always be a real answer and provides it.
:::

### Lines


The slope of $f(x) = 9/5 \cdot x + 32$ is clearly $9/5$ and the slope of the inverse function $f^{-1}(x) = 5/9 \cdot (x-32)$ is clearly $5/9$ - or the reciprocal. This makes sense, as the slope is the rise over the run, and by flipping the $x$ and $y$ values we merely flip over the rise and the run.


Now consider the graph of the *tangent line* to a function. This concept will be better defined later.


For concreteness, we consider $f(x) = \sqrt{x}$ at $c=2$. The tangent line will have slope $1/(2\sqrt{2})$ and will go through the point $(2, f(2))$. In @fig-plot-sqrt-x-its-inverse-two-tangent-lines we graph the function, its tangent line, and their inverses.

::: {#fig-plot-sqrt-x-its-inverse-two-tangent-lines}
```{julia}
#| hold: true
f(x) = sqrt(x)
c = 2
tl(x) = f(c) + 1/(2 * sqrt(2)) * (x - c)
xs = range(0, 3, length=150)
ys = f.(xs)
zs = tl.(xs)
plot(xs, ys,  color=:blue, legend=false)
plot!(xs, zs, color=:blue) # the tangent line
plot!(ys, xs, color=:red)  # the inverse function
plot!(zs, xs, color=:red)  # inverse of tangent line
scatter!([(c, f(c))], marker=(:blue, 3))
scatter!([(f(c),c)], marker=(:red, 3))
```

Plot of $f(x) = \sqrt{x}$, its inverse, and two related tangent lines
:::

What do we see? In blue, we can see the familiar square root graph along with a "tangent" line through the point $(2, f(2))$. The red graph of $f^{-1}(x) = x^2, x \geq 0$ is seen and, perhaps surprisingly, a tangent line. This is at the point $(f(2), 2)$. We know the slope of this tangent line is the reciprocal of the slope of the red tangent line. This gives this informal observation:


> If the graph of $f(x)$ has a tangent line at $(c, f(c))$ with slope $m$, then the graph of $f^{-1}(x)$ will have a tangent line at $(f(c), c)$ with slope $1/m$.



This is reminiscent of the formula for the slope of a perpendicular line, $-1/m$, but quite different, as this formula implies the two lines have either both positive slopes or both negative slopes, unlike the relationship in slopes between a line and a perpendicular line.

::: {#fig-inverse-normal layout-ncol=1}
```{julia}
#| echo: false
# inverse function slope
gr()
p1 = let
	f(x) = x^2
	df(x) = 2x

	empty_style = (xaxis=([], false),
                    yaxis=([], false),
                    framestyle=:origin,
                    legend=false)
	axis_style = (arrow=true, side=:head, line=(:gray, 1))
	text_style = (10,)
	fn_style =  (;line=(:black, 3))
	fn2_style =  (;line=(:red, 4))
	mark_style = (;line=(:gray, 1, :dot))

	plot(; aspect_ratio=:equal, empty_style...)
	xs = range(0, 1.25, 100)
	plot!(xs,f.(xs); fn_style...)
	plot!(f.(xs), xs; fn2_style...)
	plot!(identity, -1/4, 2; line=(:gray, 1, :dot))

	#plot!([-.1, 1.35],[0,0]; axis_style...)
	#plot!([0,0], [-0.1, f(1.3)]; axis_style...)

	c = .4
	m = df(c)
	tl(x) = f(c) + df(c)*(x-c)
	plot!(tl; line=(:black, 1, :dash))
	d = c + .6
	p1, p2, p3 = (c, tl(c)), (d, tl(c)), (d, tl(d))
	q1, q2, q3 = (tl(c),c), (tl(c),d), (tl(d), d)

	plot!([p1, p2, p3]; line=(:black, 1, :dot))


	tl1(x) = c + (1/m)*(x - f(c))
	plot!(tl1; line=(:red, 1, :dash))
	plot!([q1, q2, q3]; line=(:red, 1, :dot))

	annotate!([
	  ((c+d)/2, f(c), text(L"\Delta x", 10, :top, :black)),
		(d, (tl(c)+tl(d))/2, text(L"\Delta y", 10, :left, :black)),
		(f(c), (c+d)/2, text(L"\Delta x", 10, :right, :red)),
		((tl(c)+tl(d))/2, d, text(L"\Delta y", 10, :bottom, :red)),
		(d, tl(d),
		 	text(L"rise/run = $m = \Delta y / \Delta x$", 10, :top, :left,
				 rotation= rad2deg(atan(m)))),
		(tl(d), d,
		 text(L"rise/run = $\Delta x / \Delta y = 1/m$", 10, :bottom, :left,		rotation=rad2deg(atan(1/m)))),
        (1.9, 1.9, text(L"y=x", 10, :top, rotation=45))



	])
    current()
end

# normal line

p2 = let
	f(x) = 4 - (x-2)^2
	df(x) = -2*(x-2)

	empty_style = (xaxis=([], false),
                    yaxis=([], false),
                    framestyle=:origin,
                    legend=false)
	axis_style = (arrow=true, side=:head, line=(:gray, 1))
	text_style = (10,)
	fn_style =  (;line=(:black, 3))
	fn2_style =  (;line=(:red, 4))
	mark_style = (;line=(:gray, 1, :dot))

	plot(; aspect_ratio=:equal, empty_style...,
         xlims=(1, 2.5), ylims=(3, 4.5))
	xs = range(.99, 2.01, 100)
	plot!(xs,f.(xs); fn_style...)

	c = 1.5
	tl(x) = f(c) + df(c)*(x-c)
	nl(x) = f(c) - (x-c)/df(c)

	xs = range(1, 2, 10)
	plot!(xs, tl.(xs); fn2_style...)

	xs = range(.9, 2, 10)
	plot!(xs, nl.(xs); fn2_style...)

	ylims!((f(.85), nl(.95)))

	o = 1/3
	plot!([c,c+o, c+o], [tl(c), tl(c), tl(c+o)]; mark_style...)
	m = (tl(c+o) - tl(c))
	plot!([c,c,c+m], [nl(c),nl(c + m),nl(c+m)]; mark_style...)

	theta = rad2deg(atan(tl(c+o)-tl(c), o))
	annotate!([
	(c + o/2, f(c), text(L"1", :top, 10)),
	(c + o, (f(c)+f(c+o))/2, text(L"m",  :right, 10)),
	(c, (nl(c) + nl(c+m))/2, text(L"-1", :right, 10)),
	(c+m/2, nl(c+m), text(L"m", :top, 10)),
	(c + o/2, tl(c+o), text(L"rise/run $=m/1$",    10, :top,
							rotation=theta)),
	(c + 1.1*o, nl(c+1.1*o), text(L"rise/run $=(-1)/m$", 10, :bottom,
							rotation=theta-90))
	])
    current()
end

plot(p1, p2)
```

The inverse function has slope at a corresponding point that is the *reciprocal*; the "normal line" for a function at a point has slope that is the *negative reciprocal* of the "tangent line" at a point.

:::

```{julia}
#| echo: false
plotly()
nothing
```

The key here is that the shape of $f(x)$ near $x=c$ is directly related to the shape of $f^{-1}(x)$ at $f(c)$. In this case, if we use the tangent line as a fill in for how steep a function is, we see from the relationship that if $f(x)$ is "steep" at $x=c$, then $f^{-1}(x)$ will be "shallow" at $x=f(c)$.


## Questions


###### Question


Is it possible that a function have two different inverses?


```{julia}
#| hold: true
#| echo: false
choices = [L"No, for all $x$ in the domain and an inverse, the value of any inverse will be the same, hence all inverse functions would be identical.",
L"Yes, the function $f(x) = x^2, x \geq 0$ will have a different inverse than the same function $f(x) = x^2,  x \leq 0$"]
answer = 1
buttonq(choices, answer)
```

###### Question


A function takes a value $x$ adds $1$, divides by $2$, and then subtracts $1$. Is the function "one-to-one"?


```{julia}
#| hold: true
#| echo: false
choices = [L"Yes, the function is the linear function $f(x)=(x+1)/2 - 1$ and so is monotonic.",
L"No, the function is $1$ then $2$ then $1$, but not \"one-to-one\""
]
answer = 1
buttonq(choices, answer)
```

###### Question


Is the function $f(x) = x^5 - x - 1$ one-to-one?


```{julia}
#| hold: true
#| echo: false
choices=[L"Yes, a graph over $(-100, 100)$ will show this.",
L"No, a graph over $(-2,2)$ will show this."
]
answer = 2
buttonq(choices, answer)
```

###### Question


A function is given by the table


```{verbatim}
x  |   y
--------
1  |   3
2  |   4
3  |   5
4  |   3
5  |   4
6  |   5
```

Is the function one-to-one?


```{julia}
#| hold: true
#| echo: false
yesnoq(false)
```

###### Question


A function is defined by its graph.

::: {#fig-function-x-sinx-defined-by-graph}
```{julia}
#| hold: true
#| echo: false
f(x) = x - sin(x)
plot(f, 0, 6pi)
```
Plot of $f(x)$ over $[0, 6\pi]$
:::

Over the domain shown in @fig-function-x-sinx-defined-by-graph , is the function one-to-one?


```{julia}
#| hold: true
#| echo: false
yesnoq(true)
```

###### Question


Suppose $f(x) = x^{-1}$.


What is $g(x) = (f(x))^{-1}$?


```{julia}
#| hold: true
#| echo: false
choices = ["``g(x) = x``", "``g(x) = x^{-1}``"]
answer = 1
radioq(choices, answer)
```

What is $g(x) = f^{-1}(x)$?


```{julia}
#| hold: true
#| echo: false
choices = ["``g(x) = x``", "``g(x) = x^{-1}``"]
answer = 2
radioq(choices, answer)
```

###### Question


A function, $f$, is given by its graph:


```{julia}
#| echo: false
k(x) = sin(pi/4 * x)
plot(k, -2, 2)
```

What is the value of $f(1)$?


```{julia}
#| hold: true
#| echo: false
val = k(1)
numericq(val, 0.2)
```

What is the value of $f^{-1}(1)$?


```{julia}
#| hold: true
#| echo: false
val = 2
numericq(val, 0.2)
```

What is the value of $(f(1))^{-1}$?


```{julia}
#| hold: true
#| echo: false
val = 1/k(1)
numericq(val, 0.2)
```

What is the value of $f^{-1}(1/2)$?


```{julia}
#| hold: true
#| echo: false
val = 2/3
numericq(val, 0.2)
```

###### Question


A function is described as follows: for $x > 0$ it takes the square root, adds $1$ and divides by $2$.


What is the inverse of this function?


```{julia}
#| hold: true
#| echo: false
choices=[
L"The function that multiplies by $2$, subtracts $1$ and then squares the value.",
L"The function that divides by $2$, adds $1$, and then takes the square root of the value.",
L"The function that takes square of the value, then subtracts $1$, and finally multiplies by $2$."
]
answer = 1
buttonq(choices, answer)
```

###### Question


A function, $f$, is specified by a table:


```{verbatim}
x  |  y
-------
1  |  2
2  |  3
3  |  5
4  |  8
5  |  13
```

What is $f(3)$?


```{julia}
#| hold: true
#| echo: false
numericq(5)
```

What is $f^{-1}(3)$?


```{julia}
#| hold: true
#| echo: false
numericq(2)
```

What is $f(5)^{-1}$?


```{julia}
#| hold: true
#| echo: false
numericq(1/13)
```

What is $f^{-1}(5)$?


```{julia}
#| hold: true
#| echo: false
numericq(3)
```

###### Question


Find the inverse function of $f(x) = (x^3 + 4)/5$.


```{julia}
#| hold: true
#| echo: false
choices = [
"``f^{-1}(x) = (5x-4)^{1/3}``",
"``f^{-1}(x) = (5x-4)^3``",
"``f^{-1}(x) = 5/(x^3 + 4)``"
]
answer = 1
radioq(choices, answer)
```

###### Question


Find the inverse function of $f(x) = x^\pi + e,  x \geq 0$.


```{julia}
#| hold: true
#| echo: false
choices = [
raw"``f^{-1}(x) = (x-e)^{1/\pi}``",
raw"``f^{-1}(x) = (x-\pi)^{e}``",
raw"``f^{-1}(x) = (x-e)^{\pi}``"
]
answer = 1
radioq(choices, answer)
```

###### Question


What is the *domain* of the inverse function for $f(x) = x^2 + 7,  x \geq 0$?


```{julia}
#| hold: true
#| echo: false
choices = [
raw"``[7, \infty)``",
raw"``(-\infty, \infty)``",
raw"``[0, \infty)``"]
answer = 1
radioq(choices, answer)
```

###### Question


What is the *range* of the inverse function for $f(x) =  x^2 + 7,  x \geq 0$?


```{julia}
#| hold: true
#| echo: false
choices = [
raw"``[7, \infty)``",
raw"``(-\infty, \infty)``",
raw"``[0, \infty)``"]
answer = 3
radioq(choices, answer)
```

###### Question


From the plot, are blue and red inverse functions?

::: {#fig-plot-of-blue-red-inverse-maybe-1}
```{julia}
#| hold: true
#| echo: false
f(x) = x^3
xs = range(0, 2, length=100)
ys = f.(xs)
plot(xs, ys, color=:blue, legend=false)
plot!(ys, xs, color=:red)
plot!(x->x, linestyle=:dash)
```
Plot of two functions, one in blue, one in red along with the line $y=x$.
:::

```{julia}
#| hold: true
#| echo: false
yesnoq(true)
```

From the plot, are blue and red inverse functions?

::: {#fig-plot-of-blue-red-inverse-maybe-2}
```{julia}
#| hold: true
#| echo: false
f(x) = x^3 - x - 1
xs = range(-2,2, length=100)
ys = f.(xs)
plot(xs, ys, color=:blue, legend=false)
plot!(-xs, -ys, color=:red)
plot!(x->x, linestyle=:dash)
```
Plot of two functions, one in blue, one in red along with the line $y=x$.
:::

```{julia}
#| hold: true
#| echo: false
yesnoq(false)
```

###### Question


The function $f(x) = (ax + b)/(cx + d)$ is known as a [Mobius](http://tinyurl.com/oemweyj) transformation and can be expressed as a composition of $4$ functions, $f_4 \circ f_3 \circ f_2 \circ f_1$:


* where $f_1(x) = x + d/c$ is a translation,

* where $f_2(x) = x^{-1}$ is inversion and reflection,

* where $f_3(x) = ((bc-ad)/c^2) \cdot x$ is scaling,

* and $f_4(x) = x + a/c$ is a translation.


For $x=10$, $a=1$, $b=2$, $c=3$ and $d=5$, what is $f(10)$?


```{julia}
#| echo: false
𝒂,𝒃,𝒄,𝒅 = 1,2,3,5
f1(x) = x + 𝒅/𝒄; f2(x) = 1/x; f3(x) = (𝒃*𝒄-𝒂*𝒅)/𝒄^2 * x; f4(x)= x + 𝒂/𝒄
𝒇(x;a=𝒂,b=𝒃,c=𝒄,d=𝒅) = (a*x+b) / (c*x + d)
numericq(𝒇(10))
```

For $x=10$, what is $f_4(f_3(f_2(f_1(10))))$?


```{julia}
#| hold: true
#| echo: false
numericq(f4(f3(f2(f1(10)))))
```

The last two answers should be the same, why?


```{julia}
#| hold: true
#| echo: false
choices = [
    L"As $f_4(f_3(f_2(f_1(x))))=(f_4 \circ f_3 \circ f_2 \circ f_1)(x)$",
    L"As $f_4(f_3(f_2(f_1(x))))=(f_1 \circ f_2 \circ f_3 \circ f_4)(x)$",
    "As the latter is more complicated than the former."
]
answer=1
radioq(choices, answer)
```

Let $g_1$, $g_2$, $g_3$, and $g_4$ denote the inverse functions. Clearly, $g_1(x) = x- d/c$ and $g_4(x) = x - a/c$, as the inverse of adding a constant is subtracting the constant.


What is $g_2(x)=f_2^{-1}(x)$?


```{julia}
#| hold: true
#| echo: false
choices = ["``g_2(x) = x^{-1}``", "``g_2(x) = x``", "``g_2(x) = x -1``"]
answer = 1
radioq(choices, answer)
```

What is $g_3(x)=f_3^{-1}(x)$?


```{julia}
#| hold: true
#| echo: false
choices = [
    raw"``c^2/(b\cdot c - a\cdot d) \cdot  x``",
    raw"``(b\cdot c-a\cdot d)/c^2 \cdot  x``",
    raw"``c^2 x``"]
answer = 1
radioq(choices, answer)
```

Given these, what is the value of $g_4(g_3(g_2(g_1(f_4(f_3(f_2(f_1(10))))))))$?


```{julia}
#| echo: false
g1(x) = x - 𝒅/𝒄; g2(x) = 1/x; g3(x) = 1/((𝒃*𝒄-𝒂*𝒅)/𝒄^2) *x; g4(x)= x - 𝒂/𝒄
val1 = g4(g3(g2(g1(f4(f3(f2(f1(10))))))))
numericq(val1)
```

What about the value of $g_1(g_2(g_3(g_4(f_4(f_3(f_2(f_1(10))))))))$?


```{julia}
#| hold: true
#| echo: false
val = g1(g2(g3(g4(f4(f3(f2(f1(10))))))))
numericq(val)
```
