\chapter{Swapping order with Lebesgue integrals}
\section{Motivating limit interchange}
\prototype{$\mathbf{1}_\QQ$ is good!}

One of the issues with the Riemann integral is
that it behaves badly with respect to convergence of functions,
and the Lebesgue integral deals with this.
This is therefore often given as a poster child
or why the Lebesgue integral has better behaviors than the Riemann one.

We technically have already seen this:
consider the indicator function $\mathbf{1}_\QQ$,
which is not Riemann integrable by \Cref{prob:1QQ}.
But we can readily compute its Lebesgue integral over $[0,1]$, as
\[ \int_{[0,1]} \mathbf{1}_\QQ \; d\mu
	= \mu\left( [0,1] \cap \QQ \right) = 0 \]
since it is countable.

This \emph{could} be thought of as a failure of convergence
for the Riemann integral.
\begin{example}
	[$\mathbf{1}_\QQ$ is a limit of finitely supported functions]
	\label{ex:1QQindicator}
	We can define the sequence of functions $g_1$, $g_2$, \dots\ by
	\[ g_n(x) = \begin{cases}
			1 & (n!)x \text{ is an integer} \\
			0 & \text{else}.
		\end{cases} \]
	Then each $g_n$ is piecewise continuous
	and hence Riemann integrable on $[0,1]$ (with integral zero),
	but $\lim_{n \to \infty} g_n = \mathbf{1}_\QQ$ is not.
\end{example}

The limit here is defined in the following sense:
\begin{definition}
	Let $f$ and $f_1, f_2, \dots \colon \Omega \to \RR$ be a sequence of functions.
	Suppose that for each $\omega \in \Omega$, the sequence
	\[ f_1(\omega), \; f_2(\omega), \; f_3(\omega), \;, \dots \]
	converges to $f(\omega)$.
	Then we say $(f_n)_n$ \vocab{converges pointwise}
	to the limit $f$, written $\lim_{n \to \infty} f_n = f$.

	We can define $\liminf_{n \to \infty} f_n$
	and $\limsup_{n \to \infty} f_n$ similarly.
\end{definition}
This is actually a fairly weak notion of convergence, for example:
\begin{exercise}
	[Witch's hat]
	Find a sequence of continuous function on $[-1,1] \to \RR$
	which converges pointwise to the function $f$ given by
	\[ f(x) = \begin{cases}
			1 & x = 0 \\
			0 & \text{otherwise}.
		\end{cases} \]
\end{exercise}
This is why when thinking about the Riemann integral
it is commonplace to work with stronger conditions like
``uniformly convergent'' and the like.
However, with the Lebesgue integral, we can mostly not think about these!

\section{Overview}
The three big-name results for exchanging
pointwise limits with Lebesgue integrals is:
\begin{itemize}
	\ii Fatou's lemma: the most general statement possible,
	for any nonnegative measurable functions.
	\ii Monotone convergence: ``increasing limits'' just work.
	\ii Dominated convergence (actually Fatou-Lebesgue):
	limits that are not too big
	(bounded by some absolutely integrable function) just work.
\end{itemize}

\section{Fatou's lemma}
Without further ado:
\begin{lemma}
	[Fatou's lemma]
	Let $f_1, f_2, \dots \colon \Omega \to [0,+\infty]$
	be a sequence of \emph{nonnegative} measurable functions.
	Then $\liminf_n \colon \Omega \to [0,+\infty]$ is measurable and
	\[ \int_\Omega \left( \liminf_{n \to \infty} f_n \right) \; d\mu
		\le \liminf_{n \to \infty} \left( \int_\Omega f_n \; d\mu \right).  \]
	Here we allow either side to be $+\infty$.
\end{lemma}
Notice that there are \emph{no extra hypothesis}
on $f_n$ other than nonnegative: which makes this quite surprisingly versatile
if you ever are trying to prove some general result.

\section{Everything else}
The big surprise is how quickly all the ``big-name''
theorem follows from Fatou's lemma.
Here is the so-called ``monotone convergence theorem''.
\begin{corollary}
	[Monotone convergence theorem]
	Let $f$ and $f_1, f_2, \dots \colon \Omega \to [0,+\infty]$
	be a sequence of \emph{nonnegative}
	measurable functions such that $\lim_n f_n = f$
	and $f_n(\omega) \le f(\omega)$ for each $n$.
	Then $f$ is measurable and
	\[ \lim_{n \to \infty} \left( \int_\Omega f_n \; d\mu \right)
		= \int_\Omega f \; d\mu. \]
	Here we allow either side to be $+\infty$.
\end{corollary}
\begin{proof}
	We have
	\begin{align*}
		\int_\Omega f \; d\mu
		&= \int_\Omega \left( \liminf_{n \to \infty} f_n \right) \; d\mu \\
		&\le \liminf_{n \to \infty} \int_\Omega f_n \; d\mu \\
		&\le \limsup_{n \to \infty} \int_\Omega f_n \; d\mu \\
		&\le \int_\Omega f \; d\mu
	\end{align*}
	where the first $\le$ is by Fatou lemma,
	and the second by the fact that
	$\int_\Omega f_n \le \int_\Omega f$ for every $n$.
	This implies all the inequalities are equalities and we are done.
\end{proof}
\begin{remark}
	[The monotone convergence theorem does not require monotonicity!]
	In the literature it is much more common
	to see the hypothesis $f_1(\omega) \le f_2(\omega) \le \dots \le f(\omega)$
	rather than just $f_n(\omega) \le f(\omega)$ for all $n$,
	which is where the theorem gets its name.
	However as we have shown this hypothesis is superfluous!
	This is pointed out in \url{https://mathoverflow.net/a/296540/70654},
	as a response to a question entitled
	``Do you know of any very important theorems that remain unknown?''.
\end{remark}

\begin{example}
	[Monotone convergence gives $\mathbf{1}_\QQ$]
	This already implies \Cref{ex:1QQindicator}.
	Letting $g_n$ be the indicator function for $\frac1{n!}\ZZ$
	as described in that example, we have $g_n \le \mathbf{1}_\QQ$
	and $\lim_{n \to \infty} g_n(x) = \mathbf{1}_\QQ(x)$,
	for each individual $x$.
	So since $\int_{[0,1]} g_n \; d\mu = 0$ for each $n$,
	this gives $\int_{[0,1]} \mathbf{1}_\QQ = 0$ as we already knew.
\end{example}

The most famous result, though is the following.
\begin{corollary}
	[Fatou–Lebesgue theorem]
	Let $f$ and $f_1, f_2, \dots \colon \Omega \to \RR$
	be a sequence of measurable functions.
	Assume that $g \colon \Omega \to \RR$ is an
	\emph{absolutely integrable} function for which
	$|f_n(\omega)| \le |g(\omega)|$ for all $\omega \in \Omega$.
	Then the inequality
	\begin{align*}
		\int_\Omega \left( \liminf_{n \to \infty} f_n \right) \; d\mu
		&\le \liminf_{n \to \infty} \left( \int_\Omega f_n \; d\mu \right) \\
		&\le \limsup_{n \to \infty} \left( \int_\Omega f_n \; d\mu \right)
		\le \int_\Omega \left( \limsup_{n \to \infty} f_n \right) \; d\mu.
	\end{align*}
\end{corollary}
\begin{proof}
	There are three inequalities:
	\begin{itemize}
		\ii The first inequality follows by Fatou on $g + f_n$ which is nonnegative.
		\ii The second inequality is just $\liminf \le \limsup$.
		(This makes the theorem statement easy to remember!)
		\ii The third inequality follows by Fatou on $g - f_n$ which is nonnegative.
		\qedhere
	\end{itemize}
\end{proof}

\begin{exercise}
	Where is the fact that $g$ is absolutely integrable used in this proof?
\end{exercise}

\begin{corollary}
	[Dominated convergence theorem]
	Let $f_1, f_2, \dots \colon \Omega \to \RR$
	be a sequence of measurable functions
	such that $f = \lim_{n \to \infty} f_n$ exists.
	Assume that $g \colon \Omega \to \RR$ is an
	\emph{absolutely integrable} function for which
	$|f_n(\omega)| \le |g(\omega)|$ for all $\omega \in \Omega$.
	Then
	\[ \int_\Omega f \; d \mu
		= \lim_{n \to \infty} \left( \int_\Omega f_n \; d\mu \right). \]
\end{corollary}
\begin{proof}
	If $f(\omega) = \lim_{n \to \infty} f_n(\omega)$,
	then $f(\omega) = \liminf_{n \to \infty} f_n(\omega)
	= \limsup_{n \to \infty} f_n(\omega)$.
	So all the inequalities in the Fatou-Lebesgue theorem
	become equalities, since the leftmost and rightmost sides are equal.
\end{proof}
Note this gives yet another way to verify \Cref{ex:1QQindicator}.
In general, the dominated convergence theorem
is a favorite clich\'{e} for undergraduate exams,
because it is easy to create questions for it.
Here is one example showing how they all look.
\begin{example}
	[The usual Lebesgue dominated convergence examples]
	Suppose one wishes to compute
	\[ \lim_{n \to \infty}
		\left( \int_{(0,1)} \frac{n\sin(n\inv x)}{\sqrt x} \right) \; dx \]
	then one starts by observing that
	the inner term is bounded by the absolutely integrable function $x^{-1/2}$.
	Therefore it equals
	\begin{align*}
		\int_{(0,1)} \lim_{n \to \infty}
			\left( \frac{n\sin(n\inv x)}{\sqrt x} \right) \; dx
		&= \int_{(0,1)} \frac{x}{\sqrt x} \; dx \\
		&= \int_{(0,1)} \sqrt{x} \; dx = \frac23.
	\end{align*}
\end{example}

\section{Fubini and Tonelli}
\todo{TO BE WRITTEN}

\section{\problemhead}
\todo{problems}


\subsection{7.1 Curvature and Special Relativity}

\subsubsection{7.1.a. Curvature of a Space Curve in $\mathbb{R}^3$}

$\mathbf{x} = \mathbf{x}(t)$ \, \quad $\left( \frac{ds}{dt} \right)^2 = v^2$ \, \quad $s(t) = \int_0^t \| \mathbf{x}(u) \| du$ \\
$\| \mathbf{v} \| = v$\[
\begin{aligned}
        & \dot{ \mathbf{x}} = \frac{d\mathbf{x}}{dt} = \mathbf{v} = \frac{d\mathbf{x}}{ds} \frac{ds}{dt} = \mathbf{T} v \\
        & \mathbf{a} = \ddot{x} = \mathbf{v} = \dot{v} \mathbf{T} + v\dot{\mathbf{T}} = \frac{d^2s}{dt^2} \mathbf{T} + v \frac{d\mathbf{T}}{ds} \frac{ds}{dt} = \dot{v} \mathbf{T} + v^2 \frac{d\mathbf{T}}{ds} \\
        & \mathbf{v} \times \mathbf{a} = v^3 \mathbf{T} \times \frac{d\mathbf{T}}{ds} = v^3 \kappa(s) \mathbf{T} \times \mathbf{n}
\end{aligned}
\]
so
\[
\text{unit tangent vector } \mathbf{T} = \frac{d\mathbf{x}}{ds} = \dot{ \mathbf{x}} \left( \frac{dt}{ds} \right) = \frac{ \mathbf{v}}{v}
\]
Note that
\[
\frac{d\mathbf{T}}{ds} \cdot \mathbf{T} = \frac{1}{2} \frac{d}{ds} \left( \mathbf{T} \cdot \mathbf{T} \right) = \frac{1}{2} \frac{d}{ds} (1) = 0
\]

Now
\[
\frac{d\mathbf{T}}{ds} = \kappa(s) \mathbf{n}(s) \quad \quad \, (7.1)
\]
where $\mathbf{n}$ principal normal, $\kappa(s) \geq 0$ curvature of $C$.
\[
\Longrightarrow \kappa = \frac{ \| \mathbf{v} \times \mathbf{a} }{v^3}
\]
\paragraph{7.1(1)}\ \\
\[
\begin{aligned}
        & x = \cos{\omega t} \\
        & y = \sin{\omega t} \\
        & z = kt
\end{aligned} %\quad \quad \dot{x} = \left( \begin{matrix} - \omega s{\omega t}  \\ \omega c{\omega t} \\ k \end{matrix} \right) \quad \quad \mathbf{a} = \left( \begin{matrix} - \omega^2 c{\omega t} \\ - \omega^2 s{\omega t} \\ 0 \end{matrix} \right) \quad \, v = \sqrt{ \omega^2 + k^2}
\]

\[
\mathbf{v} \times \mathbf{a} = \left| \begin{matrix} \mathbf{e}_x & \mathbf{e}_y & \mathbf{e}_z \\ -\omega s{\omega t} & \omega c{\omega t} & k \\ - \omega^2 c{\omega t} & - \omega^2 s{\omega t} & 0 \end{matrix} \right| = \left( \begin{matrix} k \omega^2 s{(\omega t)} \\ - k \omega^2 c{( \omega t)} \\ \omega^3 \end{matrix} \right)
\]
\[
\Longrightarrow \kappa = \frac{ \| \mathbf{v} \times \mathbf{a} \| }{v^3} = \frac{ \sqrt{ k^2 \omega^4 + \omega^6} }{ \sqrt{ ( \omega^2 + k^2)^3 } } = \frac{ \omega^2}{ \omega^2 + k^2 }
\]

\paragraph{7.1(2)}\ \\

Given $\mathbf{B} = \mathbf{T} \times \mathbf{n}$,
\[
\frac{d\mathbf{B}}{ ds } = \frac{ d\mathbf{T} }{ds} \times \mathbf{n} + \mathbf{T} \times \frac{d\mathbf{n}}{ ds} = \mathbf{T} \times \frac{d\mathbf{n}}{ ds}
\]
so
\[
\mathbf{n} \times \frac{d\mathbf{B}}{ds} = \mathbf{n} \times (\mathbf{T} \times \frac{d\mathbf{n}}{ds} ) = \left( \mathbf{n} \times \frac{d\mathbf{n}}{ds} \right) \mathbf{T} - (\mathbf{n} \times \mathbf{T}) \frac{d\mathbf{n}}{ds} = 0
\]
Indeed
\[
\begin{aligned}
        & \mathbf{T} \cdot \frac{d\mathbf{B}}{ds} = 0 \\
        & \mathbf{B} \cdot \frac{d \mathbf{B}}{ds} = \frac{d}{ds} ( \mathbf{B}\cdot \mathbf{B}) = \frac{d}{ds}(1)= 0
\end{aligned}
\]

Then $\frac{d\mathbf{B}}{ds} \parallel \mathbf{n}$. \\

Define torsion $\frac{d\mathbf{B}}{ds} = \tau(s) \mathbf{n}$\\

Using $CAB-BAC$,
\[
\begin{gathered}
        \mathbf{n} \times \mathbf{B} = \mathbf{n} \times (\mathbf{T} \times \mathbf{n} ) = \mathbf{T} \\
        \mathbf{T} \times \mathbf{B} = \mathbf{T} \times (\mathbf{T} \times \mathbf{n} ) = (\mathbf{n} \cdot \mathbf{T} ) \mathbf{T} - ( \mathbf{T} \cdot \mathbf{T} ) \mathbf{n} = - \mathbf{n} \\
        \Longrightarrow \mathbf{n} = \mathbf{B} \times \mathbf{T}
\end{gathered}
\]

So
\[
\frac{ d \mathbf{n}}{ d s} = \frac{ d\mathbf{B}}{ds} \times \mathbf{T}  + \mathbf{B} \times \frac{ d\mathbf{T}}{ ds} = \tau \mathbf{n} \times \mathbf{T} + \mathbf{B} \times \kappa \mathbf{n} = \boxed{ - \tau \mathbf{B} + - \kappa \mathbf{T}  = \frac{ d \mathbf{n}}{ds} }
\]

\subsubsection{7.2 Electromagnetism in Minkowski Space}

\paragraph{7.2(3)\quad Field strength 2-Form}\ \\
Notation: \ieq{\d x^0 = \d t}; \ieq{\d x^{ij\cdots} = \d x^i \wedge \d x^j \wedge \cdots}. The expansion for \ieq{*F} was taken from (14.20) combined with (3.41).
\beq{
	F \wedge F
		&= (E_i \d x^{i0} + B_{J=\{1,2,3\}} \d x^J) \wedge (E_k \d x^{k0} + B_{L=\{1,2,3\}} \d x^L) \\
		&= -E_iE_k \bcancel{\d x^{i0k0}} + B_JB_L \bcancel{\d x^{JL}} + E_iB_L \d x^{i0L} + B_JE_k \d x^{Jk0} \\
		&= -2 E_iB_J \d x^{0iJ} \\
		&= -2 (E_1B_{23}\d x^{0123} + E_2B_{13}\d x^{0213} + E_3B_{12}\d x^{0312}) \\
		&= -2 \underbrace{(E_1B_{23} + E_2B_{31} + E_3B_{12})}_{= \langle\vec E, \vec B\rangle}\underbrace{\d x^{0123}}_{= \vol^4} \\
		&= -2 \, \langle\vec E, \vec B\rangle \vol^4 \\
	F \wedge *F
		&= (E_i\d x^{i0}+B_{J=\{1,2,3\}}\d x^J) \wedge (-(\vec*B)_k\d x^{k0} + (\vec*E)_{L=\{1,2,3\}}\d x^L) \\
		&= - E_iB^{\vec*}_j \bcancel{\d x^{i0k0}} + B_JE^{\vec*}_L \bcancel{\d x^{JL}} + E_iE^{\vec*}_L\d x^{0iL} - B_JB^{\vec*}_k\d x^{0Jk} \\
		&= B^{\vec*}_kB_J \d x^{0kJ} - E_iE^{\vec*}_L \d x^{0iL} \\
		&= B^{\vec*}_kB_J \d x^{0kJ} - E_kE^{\vec*}_J \d x^{0kJ} \\
		&= (B^{\vec*}_kB_J - E_kE^{\vec*}_J) \d x^{0kJ} \\
			&\qquad\text{(permute k, J so their combination is in increasing order; } \\
			&\qquad\text{ permuting the double indices of B, E cancels out the minus)} \\
		&= (\|\vec B\|^2-\|\vec E\|^2) \vol^4
}

LaTeX_and_pdfs/the geometry of physics problems/07r3minkowski.tex


\documentclass[10pt]{article}
\usepackage{bm}% bold math
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{color}
\DeclareMathOperator{\sgn}{sgn}
\renewcommand{\thefootnote}{\alph{footnote}}

\begin{document}

\subsection{Ideal Gas Model}

From (McQuarrie, Statistical mechanics, 2000) the ideal gas method (IGM) for calculating an absolute free energy is outlined below.
\begin{equation}
	G  = H - TS
\end{equation}
\begin{equation}
	H = U + RT
\end{equation}
\begin{equation}
	U = E_\text{pot} + E_\text{ZPE} + E_\text{trns} + E_\text{rot} + E_\text{vib} \end{equation}
\begin{equation}
	S = S_\text{trns} + S_\text{rot} + S_\text{vib} + S_\text{elec}
\end{equation}

where $T$ is temperature, $R$ the ideal gas constant and $S_\text{elec}$ is taken to be zero for all molecules. The internal energy components are then

\begin{equation}
	E_\text{ZPE} = \frac{N_a}{2}\sum_i h \nu_i
\end{equation}
\begin{equation}
	E_\text{trns} = \frac{3}{2}RT
\end{equation}
\begin{equation}
	E_\text{rot} =
	\begin{cases}
		0 &\quad \text{if  } N = 1 \\
		RT &\quad \text{if linear} \\
		\frac{3}{2} RT &\quad \text{otherwise}
	\end{cases}
\end{equation}
\begin{equation}
	E_\text{vib} = R \sum_i \frac{\theta_i}{e^{\theta_i / T} - 1} \quad ,\quad \theta_i = h\nu_i / k_B
\end{equation}

where $N_a$ is Avogadro's's constant, $N$ is the number of atoms in the molecule, $k_B$ Boltzmann's constant, $ \nu_i$ the $i$-th harmonic frequency and $h$ is Planks constant. The entropic components are

\begin{equation}
	S_\text{trns} = R  \ln(q_\text{trns}) + \frac{5}{2}R
\end{equation}
\begin{equation}
	S_\text{rot} = \begin{cases}
		0 &\quad \text{if  } N = 1 \\
		R  \ln(q_\text{rot}) + R &\quad \text{if linear} \\
		R  \ln(q_\text{rot}) + \frac{3}{2}R &\quad \text{otherwise}
	\end{cases}
\end{equation}
\begin{equation}
	S_\text{vib}^\text{HO} = R \sum_i \frac{\theta_i}{T(e^{\theta_i / T} - 1)} - \ln(1 - e^{-\theta_i / T})
\end{equation}
\begin{equation}
	q_\text{trans} = {\Big (} \frac{2\pi m k_B T}{h^2} {\Big )}^{3/2}  V_\text{eff} \quad , \quad V_\text{eff} =  \begin{cases}
		k_B T / p^{\circ} \quad&\text{if 1 atm standard state} \\
		1 / c^\circ N_a \quad&\text{if 1 M standard state}
	\end{cases}
\end{equation}
\begin{equation}
	q_\text{rot} = \frac{T^{3/2}}{\sigma_r} \sqrt{\frac{\pi}{\omega_r}} \quad,\quad \omega_r = \prod_{k} \frac{h^2}{8 \pi^2 k_B I_k}
\end{equation}

where $q$ are molecular partition functions, $p^{\circ}$ is the standard pressure (1 atm) and $c^\circ$ the standard concentration (1 mol dm$^{-3}$), $\sigma_r$ is the rotational symmetry number for the molecule and $I_k$ a diagonal element of the moment of inertia matrix.
\\\\
Due to the vibrational entropy contribution being overestimated for low frequency modes Thrular proposed a correction, which instead of summing over frequencies in $S_\text{vib}^\text{HO}$ does so over $\max(\nu_\text{thresh},\; \nu_i)$ to shift all low frequencies to a threshold value (\emph{J. Phys. Chem. B} 2011, {\bfseries{115}}, 14556). An alternative method from Grimme (\emph{Chem. Eur. J.}, 2012, {\bfseries{18}}, 9955) uses an interpolation between a harmonic oscillator and rigid rotor to scale down the contribution from the low frequency modes as
\begin{equation}
	S_\text{vib}^\text{Grimme} = \sum_i w_i S_ \text{vib}^\text{HO}(i) + (1-w_i) {\Big (} R\ln {\Big (} \sqrt{\frac{8 \pi^3 \mu_i' k_B T}{h^2}} {\Big )} + \frac{R}{2} {\Big )}
\end{equation}
\begin{eqnarray}
	\mu_i' = \frac{\mu_i \bar{B}}{\mu_i + \bar{B}} \quad,\quad \mu_i = \frac{h}{8\pi^2 \nu_i} \quad,\quad \bar{B} = \text{Tr}[I] / 3
\end{eqnarray}
\begin{equation}
	w_i = \frac{1}{1 + (\omega_0/ \nu_i)^\alpha}
\end{equation}
where $\omega_0$ and $\alpha$ are adjustable parameters.

\end{document}


\appendixchapter{ACESproxy function derivation}{i}
\label{appendixD}

The ACESproxy 10-bit and 12-bit logarithmic encoding and decoding functions have been derived from the single mathematical function described below. A series of parameters are defined and the values for the parameters specified based on the bit depth of the encoding.

\unnumberedformat
\section{Math functions}
The following general-use math functions are defined for use within the equations.

ROUND$(\mathbf{a})$ \tabto{7em}Math function taking a floating-point value $\mathbf{a}$, and returning the integer value closest to $\mathbf{a}$.

MAX$(\mathbf{a,b})$ \tabto{7em}Math function returning the greater of $\mathbf{a}$ or $\mathbf{b}$

MIN$(\mathbf{a,b})$ \tabto{7em}Math function returning the lesser of $\mathbf{a}$ or $\mathbf{b}$

FLOAT2CV$(\mathbf{a})$ \tabto{7em}Math function returning MAX($CVmin$, MIN($CVmax$, ROUND($\mathbf{a}$)))

\section{Parameters}
The following parameters are defined for each bit-depth.

\begin{itemize}
	\item $CVmin$ is the minimum code value available for representation of ACES image data.
	\item $CVmax$ is the maximum code value available for representation of ACES image data.
	\item $StepsPerStop$ is the number of code values representing a change of 1 stop in exposure.
	\item $MidCVoffset$ is the integer code value representing the assigned midpoint of the exposure scale for a particular bit-depth encoding. (e.g. the point to which a mid-grey exposure value would be mapped)
	\item $MidLogOffset$ is the base 2 logarithmic value representing the assigned midpoint of the exposure scale in log space, [e.g. $MidLogOffset = \log_2( 2^{-2.5} ) = -2.5$ ]
\end{itemize}

\begin{center}
\begin{tabularx}{0.75\textwidth}{|l|Y|Y|}
\hline
	 & \textbf{ACESproxy 10-bit CV} & \textbf{ACESproxy 12-bit CV} \\ \hline
	$CVmin$ & 64 & 256 \\ \hline
	$CVmax$ & 940& 3760 \\ \hline
	$StepsPerStop$ & 50 & 200 \\ \hline
	$MidCVoffset$ & 425 & 1700 \\ \hline
	$MidLogOffset$ & -2.5 & -2.5 \\ \hline
\end{tabularx}
\end{center}

\note{$MidCVoffset$ is not equal to the ACESproxy value that most closely represents an ACES mid-gray value of 0.18. ACES 0.18 is most closely represented by ACESproxy 426 10-bit CV and 1705 12-bit CV.}

\newpage
\section{Encoding Function}
The following floating-point equation is used to convert linear values to integer code values.
\begin{equation}
    \resizebox{\textwidth}{!}{$
    ACESproxy = \left\{
    \begin{array}{l l}
        CVmin;    & lin \leq 2^{\left({\frac{(CVmin-MidCVoffset)}{StepsPerStop}-MidLogOffset}\right)} \\
        \mathrm{FLOAT2CV}\left[\left(\log_2(lin)-MidLogOffset\right)\times StepsPerStop+MidCVOffset\right];        & lin > 2^{\left({\frac{(CVmin-MidCVoffset)}{StepsPerStop}-MidLogOffset}\right)} \\
    \end{array} \right. $ }
\end{equation}

where $ACESproxy$ is the resulting integer code value in the range of code values from $CVmin$ to $CVmax$.

An implementation may use mathematically equivalent forms of this encoding equation.

\section{Decoding Function}
The following floating-point equation is used to convert ACESproxy integer code values to linear values.

\begin{center}
$lin = 2^{\left( \dfrac{(ACESproxy-MidCVoffset)}{StepsPerStop} + MidLogOffset\right)}$

\end{center}

The conversion to linear creates the closest value in 16-bit half precision floating-point to the floating-point result of the equation. Linear values resulting from this equation are limited to the range of values that can be encoded in ACESproxy as illustrated in Appendix \ref{appendixB}. This decoding function does not produce negative values.

An implementation may use mathematically equivalent forms of this decoding equation.


\chapter{Multiplying Polynomials}

Watch Khan Academy's \textbf{Multiplying monomials} at \url{https://youtu.be/Vm7H0VTlIco}.

To review, when you multiply two monomials, you take the product of
their coefficients and the sum of their degrees:
\begin{equation*}
  (2x^6)(5x^3) = (2)(5)(x^6)(x^3) = 10x^9
\end{equation*}
If you have a product of more than two monomials, multiply \emph{all}
the coefficients and sum \emph{all} the exponents:
\begin{equation*}
  (3x^2)(2x^3)(4x) = (3)(2)(4)(x^2)(x^3)(x^1) = 24x^6
\end{equation*}\index{multiplication!polynomials}

\begin{Exercise}[title={Multiplying monomials}, label=multmonomials]
Multiply these monomials
  \Question $(3x^2)(5x^3)$
\vspace{20mm}
  \Question $(2x)(4x^9)$
\vspace{20mm}
  \Question $(-5.5x^2)(2x^3)$
\vspace{20mm}
  \Question $(\pi)(-2x^5)$
\vspace{20mm}
  \Question $(2x)(3x^2)(5x^7)$
\vspace{20mm}
\end{Exercise}
\begin{Answer}[ref=multmonomials]
  $(3x^2)(5x^3) = 15x^5$

  $(2x)(4x^9) = 8x^{10}$

  $(-5.5x^2)(2x^3) = -11x^5$

  $(\pi)(-2x^5) = -2\pi x^5$

  $(2x)(3x^2)(5x^7) = 30x^{10}$
\end{Answer}

\section{Multiplying a monomial and a polynomial}

Watch Khan Academy's \textbf{Multiplying monomials by polynomials} at \url{https://youtu.be/pD2-H15ucNE}.

When multiplying a monomial and a polynomial, you use the the distributive property. Then it is just multiplying several pairs of monomials:
\begin{multline*}
  (3x^2)(4x^3 - 2x^2 + 3x - 7) \\
  = (3x^2)(4x^3) + (3x^2)(-2x^2) + (3x^2)(3x) + (3x^2)(-7) \\
  = 12x^5 - 6x^4 + 9x^3 -21x^2
\end{multline*}

\begin{Exercise}[title={Multiplying a monomial and a polynomial}, label=multmonopoly]
Multiply these monomials
\Question $(3x^2)(5x^3 - 2x + 3)$
\vspace{20mm}
\Question $(2x)(4x^9 - 1)$
\vspace{20mm}
\Question $(-5.5x^2)(2x^3 + 4x^2 + 6)$
\vspace{20mm}
\Question $(\pi)(-2x^5 + 3x^4 + x)$
\vspace{20mm}
\Question $(2x)(3x^2)(5x^7 + 2x)$
\end{Exercise}
\begin{Answer}[ref=multmonopoly]
  $(3x^2)(5x^3 - 2x + 3) = 15x^6 - 6x^3 + 6x^2$

  $(2x)(4x^9 - 1) = 8x^{10} - 2x$

  $(-5.5x^2)(2x^3 + 4x^2 + 6) = 11x^5 - 22x^4 + 33x^2$

  $(\pi)(-2x^5 + 3x^4 + x) = -2\pi x^5 + 3\pi x^4 + \pi x$

  $(2x)(3x^2)(5x^7 + 2x) = 30x^{10} + 12x^4$
\end{Answer}

\section{Multiplying polynomials}

Watch Khan Academy's \textbf{Multiplying binomials by polynomials} video at \url{https://youtu.be/D6mivA_8L8U}

When you are multiplying two polynomials, you will use the
distributive property several times to make it one long
polynomial. Then you will combine the terms with the same degree. For
example,
\begin{multline*}
  (2x^2 - 3)(5x^2 + 2x - 7) \\
  =   (2x^2)(5x^2 + 2x - 7) + (-3)(5x^2 + 2x - 7) \\
  =   (2x^2)(5x^2) + (2x^2)(2x) + (2x^2)(-7) + (-3)(5x^2) + (-3)(2x) + (-3)(-7) \\
  =   10^4 + 4x^3 + -14x^2 + -15x^2 + -6x + 21
  =   10^4 + 4x^3 + -29x^2 + -6x + 21
\end{multline*}

One common form that you will see is multiplying two binomials together:
\begin{multline*}
(2x + 7)(5x + 3) = (2x)(5x + 3) + (7)(5x+3) = (2x)(5x) + (7)(5x) + (2x)(3) + (7)(3)
\end{multline*}
Notice the product has become the sum of four parts: the firsts, the
inners, the outers, and the lasts. People sometimes use the mnemonic
FOIL to remember this pattern, but there is a general rule that works
for all product of polynomials, not just binomials.  Here it is: Every
term in the first will be multiplied by every term in the second, and
then just add them together.

So, for example, if you have a polynomial $s$ with three terms and you
multiply it by a polynomial $t$ with five terms, you will get a sum of
15 terms -- each term is a product of two monomials, one from $s$ and
one from $t$.  (Of course, several of those terms might have the same
degree, so they will be combined together when you simplify. Thus you
typically end up with a polynomial with less than 15 terms.)

Using this rule, here is how I would multiply $2x^2 - 3x + 1$ and
$5x^2 + 2x - 7$:
\begin{multline*}
  (2x^2 - 3x + 1)(5x^2 + 2x - 7)  = \begin{matrix}
  (2x^2)(5x^2)& + &(2x^2)(2x)& + &(2x^2)(-7)& + \\
  (-3x)(5x^2)& + &(-3x)(2x)& + &(-3x)(-7)& + \\
    (1)(5x^2)& + &(1)(2x)& + &(1)(-7)&
  \end{matrix} \\
  = 10x^4 + 4x^3 + (-14)x^2 + (-15)x^3 + (-6)x^2 + 21x +5x^2 + 2x + (-7) \\
  = 10x^4 + (4 - 15)x^3 + (-14 - 6 + 5)x^2  + (21 + 2)x + (-7) \\
  = 10x^4 - 11x^3 - 15x^2 + 23x - 7
\end{multline*}
Note that the product (before combining terms with the same degree) has
$3 \times 3 = 9$ terms -- every possible combination of a term from the
first polynomial and a term from the second polynomial.

One common source of error: losing track of the negative
signs. You will need to be really careful. I have found that it
helps to use + between all terms, and use negative coefficients to
express subtraction. For example, if the problem says $4x^2 - 5x - 3$,
you should work with that as $4x^2 + (-5)x + (-3)$

\begin{Exercise}[title={Multiplying polynomials}, label=multpolys]
  Multiply the following pairs of polynomials:
  \Question{$2x + 1$ and $3x - 2$}
  \vspace{15mm}
  \Question{$-3x^2 + 5$ and $4x -2$}
  \vspace{15mm}
  \Question{$-2x - 1$ and $-3x - \pi$}
  \vspace{15mm}
  \Question{$-2x^5 + 5x$ and $3x^5 + 2x$}
  \vspace{15mm}

\end{Exercise}
\begin{Answer}[ref=multpolys]
  $(2x + 1)(3x - 2) = 6x^2 - x - 2$

  $(-3x^2 + 5)(4x - 2) = -12x^3 + 6x^2 + 20x - 10$

  $(-2x - 1)(-3x - \pi) = 6x^2 + (4 + 2\pi)x + \pi$

  $(-2x^5 + 5x)(3x^5 + 2x) = -6x^{10} + 12x^6 + 10x^2$
\end{Answer}

\begin{Exercise}[title={Observations}, label=obsmultpoly]
  Let's say I have two polynomials, $p_1$ and $p_2$.  $p_1$ has degree
  23.  $p_2$ has degree 12.  What is the degree of their product?
\end{Exercise}
\begin{Answer}[ref=obsmultpoly]
  The degree of the product is determined by the term that is the
  product of the highest degree term in $p_1$ and the highest degree
  term in $p_2$. Thus, the product of a degree 23 polynomial and a
  degree 12 polynomial has degree 35.
\end{Answer}


%!TEX root =  ../main.tex

\objective{Find higher derivatives of functions, apply and produce their graphs}

\index{Concavity}
The first derivative of a function tell you the slope of the graph at any point.  When the
derivative function is positive, the original graph slopes up.  When it is negative the original
graph slopes down.  When it is zero, the original graph is flat.

Could you graph the original function if you were given the derivative?  Well, you wouldn't
know where to start, but once you picked a beginning, your slope would make it clear
where to go from there.  Practice staying within the graphical realm, drawing the derivative
of a given function, or a possible function given its derivative graph.

\subsection{Second Derivatives}
But what about the derivative of the derivative?
The slope of the slope is the rate of change, or acceleration.  Think about what happens when
you accelerate, even if you are traveling backwards at first: your velocity becomes more and more
positive.  On the other hand, deceleration means slowing down and quickly traveling backwards more and
more quickly.

\begin{figure}
\begin{centering}
\begin{tabular}{ |c|c|l| }
\hline
$f'(x)$ & $f''(x)$ & $f(x)$ \\ \hline \hline
\multirow{3}{*}{ + } & + & right side of a valley \\ \cline{2-3}
 & 0 & top of a peak \\ \cline{2-3}
 & - & left side of a mountain \\ \hline
\multirow{3}{*}{ 0 } & + & end of a valley, start of a peak \\ \cline{2-3}
 & 0 & plateau \\ \cline{2-3}
 & - & end of a peak, start of a valley \\ \hline
\multirow{3}{*}{ - } & + & ride side of a valley \\ \cline{2-3}
 & 0 & bottom of a valley \\ \cline{2-3}
 & - & right side of a peak \\
\hline
\end{tabular}
\caption{Relating first and second derivative signs to a graph}
\end{centering}
\end{figure}

Most of these behaviors can be seen in figure ...  The solid curve is $f(x)=\frac{1}{6}x^6
-2x^4+8x^2-20$.  The dashed curve is $f'(x)$ and the dotted is $f''(x)$.

\begin{figure}\label{derivativesigns}
\begin{centering}
\includegraphics[width=\textwidth]{\chapdir/pics/derivativesigns}
\caption{A function with inflection and turning points}
\end{centering}
\end{figure}


\section{Homotopy pushouts}

A common way in topology to construct new spaces is by attaching cells\index{attaching cells} to a given space. A $0$-cell is just a point, an $1$-cell is an interval, a $2$-cell is a disc, a $3$-cell is the solid ball, and so forth. Many spaces can be obtained by attaching cells. For example, the circle\index{circle} is obtained by attaching a $1$-cell to a $0$-cell, so that both end-points of the interval are mapped to the point. More generally, an $n$-sphere is obtained by attaching an $n$-disc to the point, so that its entire boundary gets mapped to the point.

In type theory we can also consider a notion of $n$-cells. Just as in topology, a $0$-cell is just a point (i.e., a term). A $1$-cell, however, is in type theory an identification, i.e., a term of the identity type. A $1$-cell is then an identification of identifications, and so forth. Then we can attach cells to a type by taking a pushout, which is a process dual to taking a pullback.

The idea of pushouts is to glue two types $A$ and $B$ together using a mediating type $S$ and maps $f:S\to A$ and $g:S\to B$. In other words, we start with a diagram of the form
\begin{equation*}
\begin{tikzcd}
A & S \arrow[l,swap,"f"] \arrow[r,"g"] & B.
\end{tikzcd}
\end{equation*}
We call such a triple $\mathcal{S}\jdeq (S,f,g)$ a \define{span}\index{span} from $A$ to $B$.
A span from $A$ to $B$ can be thought of as a relation\index{relation} from $A$ to $B$, relating $f(s)$ to $g(s)$ for any $s:S$.

Unlike pullbacks, however, it is not automatically the case that pushouts always exist, and there are several equivalent characterizations of pushouts. Two such characterizations are presented in this lecture, establishing the duality between pullbacks and pushouts. Other characterizations, including the induction principle of pushouts, and the \emph{dependent universal property} of pushouts, are studied in \cref{chap:descent}.

\subsection{The universal property of pushouts}

\begin{defn}
Consider a span $\mathcal{S}\jdeq (S,f,g)$ from $A$ to $B$, and let $X$ be a type.
A \define{cocone}\index{cocone|textbf} with vertex $X$ on $\mathcal{S}$ is a triple $(i,j,H)$ consisting of maps $i:A\to X$ and $j:B\to X$, and a homotopy $H:i\circ f\htpy j\circ g$ witnessing that the square
\begin{equation*}
\begin{tikzcd}
S \arrow[r,"g"] \arrow[d,swap,"f"] & B \arrow[d,"j"] \\
A \arrow[r,swap,"i"] & X
\end{tikzcd}
\end{equation*}
commutes.
We write $\mathsf{cocone}_{\mathcal{S}}(X)$\index{cocone_S(X)@{$\mathsf{cocone}_{\mathcal{S}}(X)$}|textbf} for the type of cocones on $\mathcal{S}$ with vertex $X$.
\end{defn}

\begin{rmk}\label{rmk:htpy-cocone}
  Given two cocones $(i,j,H)$ and $(i',j',H')$ with vertex $C$, the type of identifications $(i,j,H)=(i',j',H')$ in $\mathsf{cocone}_{\mathcal{S}}(C)$ is equivalent to the type of triples $(K,L,M)$ consisting of
  \begin{align*}
    K : i\htpy i' \\
    L : j\htpy j',
  \end{align*}
  and a homotopy $M$ witnessing that the square
  \begin{equation*}
    \begin{tikzcd}
      %  ((pr2 (pr2 c)) ∙h (L ·r g)) ~ ((K ·r f) ∙h (pr2 (pr2 c')))
      i\circ f \arrow[d,swap,"H"] \arrow[r,"K\cdot f"] & i' \circ f \arrow[d,"{H'}"] \\
      j\circ g \arrow[r,swap,"K_B\cdot g"] & j'\circ g
    \end{tikzcd}
  \end{equation*}
  of homotopies commutes.
\end{rmk}

\begin{defn}
Consider a cocone $(i,j,H)$ with vertex $X$ on the span $\mathcal{S}\jdeq (S,f,g)$, as indicated in the following commuting square
\begin{equation*}
\begin{tikzcd}
S \arrow[r,"g"] \arrow[d,swap,"f"] & B \arrow[d,"j"] \\
A \arrow[r,swap,"i"] & X.
\end{tikzcd}
\end{equation*}
For every type $Y$, we define the map\index{cocone_map@{$\mathsf{cocone\usc{}map}$}|textbf}
\begin{equation*}
\mathsf{cocone\usc{}map}(i,j,H):(X\to Y)\to \mathsf{cocone}(Y)
\end{equation*}
by $h\mapsto (h\circ i,h\circ j,h\cdot H)$.
\end{defn}

\begin{defn}
  A commuting square
  \begin{equation*}
    \begin{tikzcd}
      S \arrow[r,"g"] \arrow[d,swap,"f"] & B \arrow[d,"j"] \\
      A \arrow[r,swap,"i"] & X.
    \end{tikzcd}
  \end{equation*}
  with $H:i\circ f \htpy j\circ g$ is said to be a \define{(homotopy) pushout square}\index{pushout square} if the cocone $(i,j,H)$ with vertex $X$ on the span $\mathcal{S}\jdeq (S,f,g)$
  satisfies the \define{universal property of pushouts}\index{universal property!of pushouts|textbf}, which asserts that the map
  \begin{equation*}
    \mathsf{cocone\usc{}map}(i,j,H):(X\to Y)\to \mathsf{cocone}(Y)
  \end{equation*}
  is an equivalence for any type $Y$. Sometimes pushout squares are also called \define{cocartesian squares}\index{cocartesian square|textbf}.
\end{defn}

\begin{lem}\label{lem:unique-mapping-property-pushout}
  Consider a pushout square
  \begin{equation*}
    \begin{tikzcd}
      S \arrow[r,"g"] \arrow[d,swap,"f"] & B \arrow[d,"j"] \\
      A \arrow[r,swap,"i"] & X.
    \end{tikzcd}
  \end{equation*}
  with $H:i\circ f \htpy j\circ g$, and consider a commuting square
  \begin{equation*}
    \begin{tikzcd}
      S \arrow[r,"g"] \arrow[d,swap,"f"] & B \arrow[d,"{j'}"] \\
      A \arrow[r,swap,"{i'}"] & X'.
    \end{tikzcd}
  \end{equation*}
  with $H':i'\circ f \htpy j'\circ g$. Then the type of maps $h:X\to X'$ equipped with homotopies
  \begin{align*}
    K & : h\circ i \htpy i' \\
    L & : h\circ j \htpy j'
  \end{align*}
  and a homotopy $M$ witnessing that the square
  \begin{equation*}
    \begin{tikzcd}
      h\circ i\circ f \arrow[r,"K\cdot f"] \arrow[d,swap,"h\cdot H"] & i' \circ f \arrow[d,"{H'}"] \\
      h\circ j\circ g \arrow[r,swap,"L\cdot g"] & j'\circ g
    \end{tikzcd}
  \end{equation*}
  commutes, is contractible.
\end{lem}

\begin{proof}
  For any map $h:X\to X$', the type of triples $(K,L,M)$ as in the statement of the lemma is equivalent to the type of identifications
  \begin{equation*}
    \mathsf{cocone\usc{}map}((i,j,H),h)=(i',j',H'),
  \end{equation*}
  by \cref{rmk:htpy-cocone}. Therefore it follows that the type of quadruples $(h,K,L,M)$ is equivalent to the fiber of $\mathsf{cocone\usc{}map}(i,j,H)$ at $(i',j',H')$. Since we have assumed that the cocone $(i,j,H)$ satisfies the universal property of the pushout of $\mathcal{S}$, the map $\mathsf{cocone\usc{}map}(i,j,H)$ is an equivalence, and therefore it has contractible fibers by \cref{thm:contr_equiv}.
\end{proof}

\begin{thm}\label{thm:3-for-2-pushout}
  Consider two cocones
  \begin{equation*}
    \begin{tikzcd}
      S \arrow[r,"g"] \arrow[d,swap,"f"] & B \arrow[d,"j"]
      & & S \arrow[r,"g"] \arrow[d,swap,"f"] & B \arrow[d,"{j'}"] \\
      A \arrow[r,swap,"i"] & C
      & & A \arrow[r,swap,"{i'}"] & C'
    \end{tikzcd}
  \end{equation*}
  on a span $\mathcal{S}\jdeq(S,f,g)$, and let $h:C\to C'$ be a map equipped with homotopies
  \begin{align*}
    K & : h\circ i \htpy i' \\
    L & : h\circ j \htpy j'
  \end{align*}
  and a homotopy $M$ witnessing that the square
  \begin{equation*}
    \begin{tikzcd}
      h\circ i\circ f \arrow[r,"K\cdot f"] \arrow[d,swap,"h\cdot H"] & i' \circ f \arrow[d,"{H'}"] \\
      h\circ j\circ g \arrow[r,swap,"L\cdot g"] & j'\circ g
    \end{tikzcd}
  \end{equation*}
  commutes. Then if any two of the following three statements hold, so does the third:
  \begin{enumerate}
  \item The cocone $(i,j,H)$ satisfies the universal property of the pushout of $\mathcal{S}$.
  \item The cocone $(i',j',H')$ satisfies the universal property of the pushotu of $\mathcal{S}$.
  \item The map $h$ is an equivalence.
  \end{enumerate}
\end{thm}

\begin{proof}
  First we observe that we have a commuting triangle
  \begin{equation*}
    \begin{tikzcd}[column sep=0]
      (X'\to Y) \arrow[rr,"\blank\circ h"]
      \arrow[dr,swap,"{\mathsf{cocone\usc{}map}(i',j',H')}"]
      & & (X\to Y) \arrow[dl,"{\mathsf{cocone\usc{}map}(i,j,H)}"] \\
      & \mathsf{cocone}_{\mathcal{S}}(Y) & \phantom{(X'\to Y)}
    \end{tikzcd}
  \end{equation*}
  for any type $Y$. Therefore it follows from the 3-for-2 property of equivalences that if any two of the maps in this triangle is an equivalence, so is the third. Now the claim follows from the observation in \cref{ex:equiv_precomp} that $h$ is an equivalence if and only if the map $\blank\circ h:(X'\to Y)\to (X\to Y)$ is an equivalence for any type $Y$.
\end{proof}

In the following corollary we establish the fact that pushouts are \emph{uniquely unique}.

\begin{cor}
  Consider two pushouts
  \begin{equation*}
    \begin{tikzcd}
      S \arrow[r,"g"] \arrow[d,swap,"f"] & B \arrow[d,"j"]
      & & S \arrow[r,"g"] \arrow[d,swap,"f"] & B \arrow[d,"{j'}"] \\
      A \arrow[r,swap,"i"] & C
      & & A \arrow[r,swap,"{i'}"] & C'
    \end{tikzcd}
  \end{equation*}
  of a given span $\mathcal{S}\jdeq (S,f,g)$. Then the type of equivalences $e:\eqv{C}{C'}$ equipped with homotopies
  \begin{align*}
    K & : h\circ i \htpy i' \\
    L & : h\circ j \htpy j'
  \end{align*}
  and a homotopy $M$ witnessing that the square
  \begin{equation*}
    \begin{tikzcd}
      h\circ i\circ f \arrow[r,"K\cdot f"] \arrow[d,swap,"h\cdot H"] & i' \circ f \arrow[d,"{H'}"] \\
      h\circ j\circ g \arrow[r,swap,"L\cdot g"] & j'\circ g
    \end{tikzcd}
  \end{equation*}
  commutes, is contractible.
\end{cor}

\begin{proof}
  This follows from combining \cref{lem:unique-mapping-property-pushout,thm:3-for-2-pushout}.
\end{proof}

\subsection{Suspensions}
A particularly important class of examples of pushouts are suspensions.

\begin{defn}
  Let $X$ be a type. A \define{suspension}\index{suspension|textbf} of $X$ is a type $\susp X$ equipped with a \define{north pole} $\mathsf{N}:\susp X$, a \define{south pole} $\mathsf{S}:\susp X$, and a \define{meridian}
  \begin{equation*}
    \mathsf{merid} : X \to (\mathsf{N}=\mathsf{S}),
  \end{equation*}
  such that the commuting square
  \begin{equation*}
    \begin{tikzcd}
      X \arrow[r,"\mathsf{const}_\ttt"] \arrow[d,swap,"\mathsf{const}_\ttt"] & \unit \arrow[d,"{\mathsf{const}_\south}"] \\
      \unit \arrow[r,swap,"\mathsf{const}_\north"] & \susp X
    \end{tikzcd}
  \end{equation*}
  is a pushout square.
\end{defn}

We can use suspensions to present the spheres in type theory. The $2$-sphere is a space which, like the surface of the earth, has a north pole and a south pole. Moreover, for each point of the equator there is a meridian that connects the north pole to the south pool. Of course, the equator is a circle, so we see that the $2$-sphere is just the suspension of the circle.

Similarly we can see that the $(n+1)$-sphere must be the suspension of the $n$-sphere. The $(n+1)$-sphere is the unit sphere in the vector space $\mathbb{R}^{n+2}$. This vector space has an orthogonal basis $e_1,\ldots,e_{n+2}$. Then the north and the south pole are given by $e_{n+2}$ and $-e_{n+2}$, respectively, and for each unit vector in $\mathbb{R}^{n+1}\subseteq\mathbb{R}^{n+2}$ we have a meridian connecting the north pole with the south pole. The unit sphere in $\mathbb{R}^{n+1}$ is of course the $n$-sphere, so we see that the $(n+1)$-sphere must be a suspension of the $n$-sphere.

These observations suggest that we can define the spheres by recursion on $n$. Note that the spheres in type theory are defined entirely synthetically, i.e., without reference to the ambient topological space $\mathbb{R}^{n+1}$. Indeed, from a homotopical point of view each space $\mathbb{R}^{n}$ is contractible, so in type theory it is just presented as the unit type\footnote{It is an entirely different matter to define the \emph{set} $\mathbb{R}$ rather than the homotopy type of $\mathbb{R}$. See Chapter 11 of \cite{hottbook} for definitions of the Dedekind reals and the Cauchy reals.}.

\begin{defn}
We define the \define{$n$-sphere}\index{n-sphere@{$n$-sphere}|textbf} $\sphere{n}$\index{Sn@{$\sphere{n}$}|textbf} for any $n:\N$ by induction on $n$, by taking
\begin{align*}
\sphere{0} & \defeq \bool \\
\sphere{n+1} & \defeq \susp{\sphere{n}}.
\end{align*}
\end{defn}

\begin{rmk}
  Note that this recursive definition of the spheres only goes through in type theory if we have (or assume) a universe that is closed under suspensions.
\end{rmk}

In the following lemma we give a slight simplification of the universal property of suspensions, making it just a little easier to work with them.

\begin{lem}
Let $X$ and $Y$ be types, and let $\susp X$ be a suspension of $X$. Then the map\index{universal property!of suspensions|textit}
\begin{equation*}
(\susp{X}\to Y)\to \sm{y,y':Y} X\to (y=y')
\end{equation*}
given by $f\mapsto (f(\north),f(\south),f\cdot\merid)$ is an equivalence.
\end{lem}

\begin{proof}
  Note that we have a commuting triangle
  \begin{equation*}
    \begin{tikzcd}[column sep=-1em]
      \phantom{\sm{y,y':Y}X\to (y=y')} & (\susp X \to Y) \arrow[dl,swap,"\mathsf{cocone\usc{}map}"] \arrow[dr,"{f\mapsto (f(\north),f(\south),f\cdot\merid)}"] &
      \phantom{\mathsf{cocone}_{\mathcal{S}}(Y)} \\
      \mathsf{cocone}_{\mathcal{S}}(Y) \arrow[rr] & & \sm{y,y':Y}X\to (y=y')
    \end{tikzcd}
  \end{equation*}
  where $\mathcal{S}$ is the span $\unit \leftarrow X \rightarrow \unit$. The bottom map is given by $(i,j,H)\mapsto (i(\ttt),j(\ttt),H)$. This map is an equivalence, and the map on the left is an equivalence by the assumption that $\susp X$ is a suspension of $X$. Therefore the claim follows by the 3-for-2 property of equivalences.
\end{proof}

A slightly slicker version of the universal property of suspensions can be obtained in the case where $X$ is a pointed type. Recall that a pointed type is a pair $(X,x_0)$ consisting of a type $X$ and a point $x_0:X$. The point $x_0:X$ is called the \define{base point} of the pointed type. In the case of the suspension, we define the base point to be the north pole.

\begin{defn}
  Given two pointed types $X$ and $Y$, a pointed map from $X$ to $Y$ is a pair $(f,p)$ consisting of a map $f:X\to Y$ and a path $p:f(x_0)=y_0$ witnessing that $f$ preserves the base point. We write
  \begin{equation*}
    X\to_\ast Y
  \end{equation*}
  for the type of \define{pointed maps} from $X$ to $Y$. The type $X\to_\ast Y$ is itself a pointed type, with base point $(\mathsf{const}_{y_0},\refl{y_0})$.
\end{defn}

Now suppose that we have a pointed map $f:\susp X\to_\ast Y$ with $p:f(x_0)=y_0$. Then we obtain a map $X\to\loopspace{Y}$ that maps $x:X$ to the concatenation
\begin{equation*}
  \begin{tikzcd}[column sep=6em]
    f(\north) \arrow[r,equals,"\ap{f}{\merid(x)}"] & f(\south) \arrow[r,equals,"\ap{f}{\merid(x_0)}^{-1}"] & f(\north).
  \end{tikzcd}
\end{equation*}
Note that the function $x\mapsto\ct{\ap{f}{\merid(x)}}{\ap{f}{\merid(x_0)}^{-1}}$ is a pointed map by the right inverse law of path concatenation. Therefore we obtain a map
\begin{equation*}
  \tau_{X,Y} :(\susp X \to_\ast Y) \to (X \to_\ast \loopspace{Y}).
\end{equation*}
It is not hard to see that also $\tau_{X,Y}$ is pointed. We leave this to the reader. The following theorem is also called the adjointness of the suspension and loop space functors. This is an extremely important relation that pops up in many calculations of homotopy groups.

\begin{thm}
  Let $X$ and $Y$ be pointed types. Then the pointed map
  \begin{equation*}
    \tau_{X,Y} : (\susp X \to_\ast Y) \to_\ast (X\to_\ast \loopspace Y)
  \end{equation*}
  is an equivalence. Moreover, $\tau$ is pointedly natural in $X$ and $Y$.
\end{thm}

\subsection{The duality of pullbacks and pushouts}
\begin{lem}\label{lem:cocone_pb}
For any span $\mathcal{S}\jdeq (S,f,g)$ from $A$ to $B$, and any type $X$ the square\index{cocone_S(X)@{$\mathsf{cocone}_{\mathcal{S}}(X)$}!as a pullback|textit}
\begin{equation*}
\begin{tikzcd}
\mathsf{cocone}_{\mathcal{S}}(X) \arrow[r,"\pi_2"] \arrow[d,swap,"\pi_1"] & X^B \arrow[d,"\blank\circ g"] \\
X^A \arrow[r,swap,"\blank\circ f"] & X^S,
\end{tikzcd}
\end{equation*}
which commutes by the homotopy $\pi_3' \defeq\lam{(i,j,H)} \mathsf{eq\usc{}htpy}(H)$, is a pullback square.
\end{lem}

\begin{proof}
The gap map $\mathsf{cocone}_{\mathcal{S}}(X)\to X^A\times_{X^S} X^B$ is the function
\begin{equation*}
\lam{(i,j,H)}(i,j,\mathsf{eq\usc{}htpy}(H)).
\end{equation*}
This is an equivalence by \cref{thm:fib_equiv}, since it is the induced map on total spaces of the family of equivalences $\mathsf{eq\usc{}htpy}$. Therefore, the square is a pullback square by \cref{thm:is_pullback}.
\end{proof}

In the following theorem we establish the duality between pullbacks and pushouts.

\begin{thm}\label{thm:pushout_up}
Consider a commuting square\index{universal property!of pushouts|textit}
\begin{equation*}
\begin{tikzcd}
S \arrow[r,"g"] \arrow[d,swap,"f"] & B \arrow[d,"j"] \\
A \arrow[r,swap,"i"] & X,
\end{tikzcd}
\end{equation*}
with $H:i\circ f\htpy j\circ g$. The following are equivalent:
\begin{enumerate}
\item The square is a pushout square.
\item The square
\begin{equation*}
\begin{tikzcd}
T^X \arrow[r,"\blank\circ j"] \arrow[d,swap,"\blank\circ i"] & T^B \arrow[d,"\blank\circ g"] \\
T^A \arrow[r,swap,"\blank\circ f"] & T^S
\end{tikzcd}
\end{equation*}
which commutes by the homotopy
\begin{equation*}
\lam{h} \mathsf{eq\usc{}htpy}(h\cdot H)
\end{equation*}
is a pullback square, for every type $T$.
%\item The type $X$ satisfies \define{span induction} for the span $A\leftarrow S \rightarrow B$, in the sense that for any type family $P$ over $X$, the map
%\begin{equation*}
%\Big(\prd{x:X}P(x)\Big)\to \Big(\sm{i':\prd{a:A}P(i(a))}{j':\prd{b:B}P(j(b))} i'\htpy_H j'\Big)
%\end{equation*}
%given by $s\mapsto (s\circ i,s\circ j,s\cdot H)$ has a section.
\end{enumerate}
\end{thm}

\begin{proof}
It is straightforward to verify that the triangle
\begin{equation*}
\begin{tikzcd}[column sep=3em]
& T^X \arrow[dl,swap,"{\mathsf{cocone\usc{}map}(i,j,H)}"] \arrow[dr,"{\mathsf{gap}(\blank\circ i,\blank\circ j, \mathsf{eq\usc{}htpy}(\blank\cdot H))}"] \\
\mathsf{cocone}(T) \arrow[rr,swap,"{\mathsf{gap}(i,j,\mathsf{eq\usc{}htpy}(H))}"] & & T^A \times_{T^S} T^B
\end{tikzcd}
\end{equation*}
commutes. Since the bottom map is an equivalence by \cref{lem:cocone_pb}, it follows that if either one of the remaining maps is an equivalence, so is the other. The claim now follows by \cref{thm:is_pullback}.
\end{proof}

\begin{eg}\label{eg:circle_pushout}
By \cref{ex:circle_up_pushout} and the second characterization of pushouts in \cref{thm:pushout_up} it follows that the circle is a pushout\index{circle!S1 equiv susp 2@{$\eqv{\sphere{1}}{\susp\bool}$}|textit}
\begin{equation*}
\begin{tikzcd}
\bool \arrow[r] \arrow[d] & \unit \arrow[d] \\
\unit \arrow[r] & \sphere{1}.
\end{tikzcd}
\end{equation*}
In other words, $\eqv{\sphere{1}}{\susp{\bool}}$.
\end{eg}

\begin{thm}\label{thm:pushout_pasting}
Consider the following configuration of commuting squares:\index{pushout!pasting property|textit}\index{pasting property!for pushouts|textit}
\begin{equation*}
\begin{tikzcd}
A \arrow[r,"i"] \arrow[d,swap,"f"] & B \arrow[r,"k"] \arrow[d,swap,"g"] & C \arrow[d,"h"] \\
X \arrow[r,swap,"j"] & Y \arrow[r,swap,"l"] & Z
\end{tikzcd}
\end{equation*}
with homotopies $H:j\circ f\htpy g\circ i$ and $K:l\circ g\htpy h\circ k$, and suppose that the square on the left is a pushout square.
Then the square on the right is a pushout square if and only if the outer rectangle is a pushout square.
\end{thm}

\begin{proof}
Let $T$ be a type. Taking the exponent $T^{(\blank)}$ of the entire diagram of the statement of the theorem, we obtain the following commuting diagram
\begin{equation*}
\begin{tikzcd}
T^Z \arrow[r,"\blank\circ l"] \arrow[d,swap,"\blank\circ h"] & T^Y \arrow[d,swap,"\blank\circ g"] \arrow[r,"\blank\circ j"] & T^X \arrow[d,"\blank\circ f"] \\
T^C \arrow[r,swap,"\blank\circ k"] & T^B \arrow[r,swap,"\blank\circ i"] & T^A.
\end{tikzcd}
\end{equation*}
By the assumption that $Y$ is the pushout of $B\leftarrow A \rightarrow X$, it follows that the square on the right is a pullback square. It follows by \cref{thm:pb_pasting} that the rectangle on the left is a pullback if and only if the outer rectangle is a pullback. Thus the statement follows by the second characterization in \cref{thm:pushout_up}.
\end{proof}

\begin{lem}
Consider a map $f:A\to B$. Then the cofiber of the map $\inr:B\to \mathsf{cofib}_f$ is equivalent to the suspension $\susp{A}$ of $A$.
\end{lem}

\subsection{Fiber sequences and cofiber sequences}

\begin{defn}
Given a map $f:A\to B$, we define the \define{cofiber}\index{cofiber|textbf} $\mathsf{cofib}_f$\index{cofib_f@{$\mathsf{cofib}_f$}|textbf} of $f$ as the pushout
\begin{equation*}
\begin{tikzcd}
A \arrow[r,"f"] \arrow[d] & B \arrow[d,"\inr"] \\
\unit \arrow[r,swap,"\inl"] & \mathsf{cofib}_f.
\end{tikzcd}
\end{equation*}
The cofiber of a map is sometimes also called the \define{mapping cone}\index{mapping cone|textbf}.
\end{defn}

\begin{eg}
The suspension $\susp X$ of $X$ is the cofiber of the map $X\to \unit$.\index{suspension!as cofiber}
\end{eg}

\subsection{Further examples of pushouts}

\begin{defn}
We define the \define{join}\index{join} $\join{X}{Y}$\index{join X Y@{$\join{X}{Y}$}|textbf} of $X$ and $Y$ to be the pushout
\begin{equation*}
\begin{tikzcd}
X\times Y \arrow[r,"\proj 2"] \arrow[d,swap,"\proj 1"] & Y \arrow[d,"\inr"] \\
X \arrow[r,swap,"\inl"] & X \ast Y.
\end{tikzcd}
\end{equation*}
\end{defn}

\begin{defn}
Suppose $A$ and $B$ are pointed types, with base points $a_0$ and $b_0$, respectively. The \define{(binary) wedge}\index{wedge@(binary) wedge|textbf} $A\vee B$ of $A$ and $B$ is defined as the pushout
\begin{equation*}
\begin{tikzcd}
\bool \arrow[r] \arrow[d] & A+B \arrow[d] \\
\unit \arrow[r] & A\vee B.
\end{tikzcd}
\end{equation*}
\end{defn}

\begin{defn}
Given a type $I$, and a family of pointed types $A$ over $i$, with base points $a_0(i)$. We define the \define{(indexed) wedge}\index{wedge@{(indexed) wedge}|textbf} $\bigvee_{(i:I)}A_i$ as the pushout
\begin{equation*}
\begin{tikzcd}[column sep=huge]
I \arrow[d] \arrow[r,"{\lam{i}(i,a_0(i))}"] & \sm{i:I}A_i \arrow[d] \\
\unit \arrow[r] & \bigvee_{(i:I)} A_i.
\end{tikzcd}
\end{equation*}
\end{defn}

\begin{defn}
Let $X$ and $Y$ be types with base points $x_0$ and $y_0$, respectively.
We define the \define{wedge} $X\lor Y$ of $X$ and $Y$ to be the pushout
\begin{equation*}
\begin{tikzcd}[column sep=8em]
\bool \arrow[r,"{\ind{\bool}(\inl(x_0),\inr(y_0))}"] \arrow[d,swap,"\mathsf{const}_\ttt"] & X+Y \arrow[d,"\inr"] \\
\unit \arrow[r,swap,"\inl"] & X\lor Y
\end{tikzcd}
\end{equation*}
\end{defn}

\begin{defn}
Let $X$ and $Y$ be types with base points $x_0$ and $y_0$, respectively.
We define a map
\begin{equation*}
\mathsf{wedge\usc{}incl} : X \lor Y \to X\times Y.
\end{equation*}
as the unique map obtained from the commutative square
\begin{equation*}
\begin{tikzcd}[column sep=8em]
\bool \arrow[r,"{\ind{\bool}(\inl(x_0),\inr(y_0))}"] \arrow[d,swap,"\mathsf{const}_\ttt"] & X+Y \arrow[d,"{\ind{X+Y}(\lam{x}\pairr{x,y_0},\lam{y}\pairr{x_0,y})}"] \\
\unit \arrow[r,swap,"\lam{t}\pairr{x_0,y_0}"] & X\times Y.
\end{tikzcd}
\end{equation*}
\end{defn}

\begin{defn}
We define the \define{smash product} $X\wedge Y$ of $X$ and $Y$ to be the pushout
\begin{equation*}
\begin{tikzcd}[column sep=huge]
X\lor Y \arrow[r,"\mathsf{wedge\usc{}incl}"] \arrow[d,swap,"\mathsf{const}_\ttt"] & X\times Y \arrow[d,"\inr"] \\
\unit \arrow[r,swap,"\inl"] & X\wedge Y.
\end{tikzcd}
\end{equation*}
\end{defn}

\begin{exercises}
\item \label{ex:span_rel}Use \cref{thm:choice,thm:fam_proj,cor:times_up_out} to show that the type
\begin{equation*}
\mathsf{span}(A,B)\defeq \sm{S:\UU} (S\to A)\times (S\to B)
\end{equation*}
of small spans from $A$ to $B$ is equivalent to the type $A\to (B\to\UU)$ of small relations from $A$ to $B$.
\item \label{ex:pushout_equiv}Use \cref{thm:pushout_up,cor:pb_equiv,ex:equiv_precomp} to show that for any commuting square
\begin{equation*}
\begin{tikzcd}
S \arrow[r,"g"] \arrow[d,swap,"f","{\eqvsym}"'] & B \arrow[d,"j"] \\
A \arrow[r,swap,"i"] & C
\end{tikzcd}
\end{equation*}
where $f$ is an equivalence, the square is a pushout square if and only if $j:B\to C$ is an equivalence.
Use this observation to conclude the following:
\begin{enumerate}
\item If $X$ is contractible, then $\susp X$ is contractible.
\item The cofiber of any equivalence is contractible.
\item The cofiber of a point in $B$ (i.e., of a map of the type $\unit\to B$) is equivalent to $B$.
\item There is an equivalence $\eqv{X}{\join{\emptyt}{X}}$.
\item If $X$ is contractible, then $\join{X}{Y}$ is contractible.
\item If $A$ is contractible, then there is an equivalence $\eqv{A\vee B}{B}$ for any pointed type $B$.
\end{enumerate}
\item \label{ex:join_propositions}Let $P$ and $Q$ be propositions.
\begin{subexenum}
\item Show that $\join{P}{Q}$ satisfies the \emph{universal property of disjunction}, i.e., that for any proposition $R$, the map
\begin{equation*}
(\join{P}{Q}\to R)\to (P\to R)\times (Q\to R)
\end{equation*}
given by $f\mapsto (f\circ \inl,f\circ \inr)$, is an equivalence.
\item Use the proposition $R\defeq\iscontr(\join{P}{Q})$ to show that $\join{P}{Q}$ is again a proposition.
\end{subexenum}
\item Let $Q$ be a proposition, and let $A$ be a type. Show that the following are equivalent:
\begin{subexenum}
\item The map $(Q\to A)\to(\emptyt\to A)$ is an equivalence.
\item The type $A^Q$ is contractible.
\item There is a term of type $Q\to\iscontr(A)$.
\item The map $\inr:A\to \join{Q}{A}$ is an equivalence.
\end{subexenum}
\item Let $P$ be a proposition. Show that $\susp P$ is a set, with an equivalence
\begin{equation*}
\eqv{\Big(\inl(\ttt)=\inr(\ttt)\Big)}{P}.
\end{equation*}
\item Show that $\eqv{A\sqcup^{\mathcal{S}} B}{B\sqcup^{\mathcal{S}^{\mathsf{op}}} A}$, where $\mathcal{S^{\mathsf{op}}}\defeq (S,g,f)$ is the \define{opposite span} of $\mathcal{S}$.
\item Use \cref{ex:pb_pi} to show that if
\begin{equation*}
\begin{tikzcd}
S \arrow[r] \arrow[d] & Y \arrow[d] \\
X \arrow[r] & Z
\end{tikzcd}
\end{equation*}
is a pushout square, then so is
\begin{equation*}
\begin{tikzcd}
A\times S \arrow[r] \arrow[d] & A\times Y \arrow[d] \\
A\times X \arrow[r] & A\times Z
\end{tikzcd}
\end{equation*}
for any type $A$.
\item Use \cref{ex:pb_prod} to show that if
\begin{equation*}
\begin{tikzcd}
S_1 \arrow[r] \arrow[d] & Y_1 \arrow[d] & S_2 \arrow[r] \arrow[d] & Y_2 \arrow[d] \\
X_1 \arrow[r] & Z_1 & X_2 \arrow[r] & Z_2
\end{tikzcd}
\end{equation*}
are pushout squares, then so is
\begin{equation*}
\begin{tikzcd}
S_1+S_2 \arrow[r] \arrow[d] & Y_1+ Y_2 \arrow[d] \\
X_1 +X_2 \arrow[r] & Z_1+Z_2.
\end{tikzcd}
\end{equation*}
\item
\begin{subexenum}
\item Consider a span $(S,f,g)$ from $A$ to $B$. Use \cref{ex:pb_diagonal} to show that the square
\begin{equation*}
\begin{tikzcd}[column sep=large]
S+S \arrow[d,swap,"{f+g}"] \arrow[r,"{[\idfunc,\idfunc]}"] & S \arrow[d,"{\inr\circ g}"] \\
A+B \arrow[r,swap,"{[\inl,\inr]}"] & A\sqcup^\mathcal{S} B
\end{tikzcd}
\end{equation*}
is again a pushout square.
\item Show that $\eqv{\susp X}{\join{\bool}{X}}$.
\end{subexenum}
\item Consider a commuting triangle
\begin{equation*}
\begin{tikzcd}[column sep=tiny]
A \arrow[rr,"h"] \arrow[dr,swap,"f"] & & B \arrow[dl,"g"] \\
& X
\end{tikzcd}
\end{equation*}
with $H:f\htpy g\circ h$.
\begin{subexenum}
\item Construct a map $\mathsf{cofib}_{(h,H)}: \mathsf{cofib}_{g}\to \mathsf{cofib}_f$.
\item Use \cref{ex:pb_fib} to show that $\eqv{\mathsf{cofib}_{\mathsf{cofib}(h,H)}}{\mathsf{cofib}_h}$.
\end{subexenum}
\item \label{ex:sphere_null}Use \cref{ex:circle_connected} to show that for $n\geq 0$, $X$ is an $n$-type if and only if the map
\begin{equation*}
\lam{x}\mathsf{const}_x : X \to (\sphere{n+1}\to X)
\end{equation*}
is an equivalence.
\item
\begin{subexenum}
\item Construct for every $f:X\to Y$ a function
\begin{equation*}
\susp f : \susp X\to \susp Y.
\end{equation*}
\item Show that if $f\htpy g$, then $\susp f \htpy \susp g$.
\item Show that $\susp \idfunc[X]\htpy\idfunc[\susp X]$
\item Show that
\begin{equation*}
\susp(g\circ f)\htpy (\susp g)\circ (\susp f).
\end{equation*}
for any $f:X\to Y$ and $g:Y\to Z$.
\end{subexenum}
\item Consider a commuting diagram of the form
\begin{equation*}
\begin{tikzcd}
A_0 & B_0 \arrow[l] \arrow[r] & C_0 \\
A_1 \arrow[u] \arrow[d] & B_1 \arrow[u] \arrow[r] \arrow[d] \arrow[l] & C_1 \arrow[u] \arrow[d] \\
A_2 & B_2 \arrow[l] \arrow[r] & C_2
\end{tikzcd}
\end{equation*}
with homotopies filling the (small) squares. Use \cref{ex:pb_3by3} to construct an equivalence
\begin{align*}
& (A_0\sqcup^{B_0} C_0) \sqcup^{(A_1\sqcup^{B_1} C_1)} (A_2 \sqcup^{B_2} C_2) \\
& \qquad \eqvsym (A_0 \sqcup^{A_1} A_2) \sqcup^{(B_0\sqcup^{B_1} B_2)} (C_0\sqcup^{C_1} C_2).
\end{align*}
This is known as the \define{3-by-3 lemma}\index{3-by-3 lemma!for pushouts} for pushouts.
\item
\begin{subexenum}
\item Let $I$ be a type, and let $A$ be a family over $I$. Construct an equivalence
\begin{equation*}
\eqv{\Big(\bigvee\nolimits_{(i:I)}\susp A_i\Big)}{\susp\Big(\bigvee\nolimits_{(i:I)}A_i\Big)}.
\end{equation*}
\item Show that for any type $X$ there is an equivalence
\begin{equation*}
\eqv{\Big(\bigvee\nolimits_{(x:X)}\bool\Big)}{X+\unit}.
\end{equation*}
\item Construct an equivalence
\begin{equation*}
\eqv{\susp(\mathsf{Fin}(n+1))}{\bigvee\nolimits_{(i:\mathsf{Fin}(n))}\sphere{1}}.
\end{equation*}
\end{subexenum}
\item Show that $\eqv{\join{\mathsf{Fin}(n+1)}{\mathsf{Fin}(m+1)}}{\bigvee\nolimits_{(i:\mathsf{Fin}(n\cdot m))}\sphere{1}}$, for any $n,m:\N$.
\item For any pointed set $X$, show that the squares
  \begin{equation*}
    \begin{tikzcd}
      \sphere{1} \arrow[r] \arrow[d] & \unit \arrow[d] \\
      \bigvee_{(x:X)}\sphere{1} \arrow[r] & \susp{X}
    \end{tikzcd}
    \qquad\text{and}\qquad
    \begin{tikzcd}
      X\times \sphere{1} \arrow[d] \arrow[r] & \unit \arrow[d] \\
      \bigvee_{(x:X)}\sphere{1} \arrow[r] & \susp{X}
    \end{tikzcd}
  \end{equation*}
  are pushout squares.
\item Show that the square
  \begin{equation*}
    \begin{tikzcd}
      \sphere{1} \arrow[r] \arrow[d] & \unit \arrow[d] \\
      \sphere{1}\times\sphere{1} \arrow[r] & \sphere{2}\vee\sphere{1}
    \end{tikzcd}
  \end{equation*}
  is a pushout square.
\item For any type $X$, show that the mapping cone of the fold map $X+X\to X$ is the suspension of $X+\unit$, i.e.~show that the following square
  \begin{equation*}
    \begin{tikzcd}
      X+X \arrow[d] \arrow[r] & \unit \arrow[d] \\
      X \arrow[r] & \susp{X+\unit}
    \end{tikzcd}
  \end{equation*}
  is a pushout square.
\end{exercises}


\documentclass{article}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{tikz}
\usepackage[all]{xy}
\usetikzlibrary{positioning,chains,fit,shapes,calc}

\begin{document}

\title{Homework 11}
\author{Josh Cai}

\maketitle
\section*{Section 8.1}

\noindent\textbf{2a)} $a_n = n \times a_{n-1}$, because there are $n$ places to insert the $a_n$th digit into the existing permutation.

\noindent\textbf{2b)} $a_n = n \times a_{n-1} = n \times a_{n-1} \times a_{n-2} =  n \times a_{n-1} \times a_{n-2} \times . . . \times 1 = n!$.
\\\\\noindent\textbf{4)} The number of ways to pay a bill of $n$ pesos is $a_n = a_{n-1} + a_{n-2}+2a_{n-5}+2a_{n-10}+a_{n-20}+a_{n-50}+a_{n-100}$.
\\\\\noindent\textbf{8a)} The number of bit strings of length $n$ with three consecutive zeros is $a_{n}=a_{n-1}+a_{n-2}+a_{n-3}+2^{n-3}$, since each ends in either 1 (number is $a_{n-1}$), 10 (number is $a_{n-2}$), 100 (number is $a_{n-3}$), or 000 (number is $2^{n-3}$).

\\\noindent\textbf{8b)} $a_1 = a_2 = 0, a_3 = 1$

\\\noindent\textbf{8c)} There are 47 different bit strings of length 7 with three consecutive zeros.
\\\\\noindent\textbf{14a)} The number of ternary strings with two consecutive zeros is $a_n = 2a_{n-1}+2a_{n-2}+3^{n-2}$.

\\\noindent\textbf{14b)} $a_1 = 0, a_2 = 1$

\\\noindent\textbf{14c)} There are 281 different ternary strings of length 6 with two consecutive zeros.
\\\\\noindent\textbf{30a)}

\\\noindent\textbf{30b)} $C_2 = C_0C_1+C_1C_0 = 1+1 = 2$. $C_3 = C_0C_2+C_1C_1+C_2C_0 = 2+1+2 = 5$. $C_4 = C_0C_3+C_1C_2+C_2C_1+C_3C_0 = 5+2+2+5 = 14$.

\\\noindent\textbf{30c)} $C_4 = \frac{1}{n+1} \binom{8}{4} = \frac{1}{5} \times \frac{8!}{4!4!} = 14$.

\section*{Section 8.2}
\noindent\textbf{2a)} Is a linear homogeneous recurrence relation with constant coefficients, degree 2.

\noindent\textbf{2b)} Is not homogeneous.

\noindent\textbf{2c)} Is not linear.

\noindent\textbf{2d)} Is a linear homogeneous recurrence relation with constant coefficients, degree 3.
\\\\\noindent\textbf{4b)} Is not homogeneous.

\noindent\textbf{4d)} Is not linear.

\noindent\textbf{4f)} Is a linear homogeneous recurrence relation with constant coefficients, degree 3.
\\\\\noindent\textbf{6)} Recurrence relation is $a_n = 2a_{n-1}+a_{n-2}$. Characteristic equation is $r^2 -2r-1$.

\end{document}


\subsection{KENN} \label{kenn_architecture}
As anticipated in section~\ref{nsi_approaches}, KENN is the Neuro-Symbolic Integration approach chosen for this study. It adopts a restricted FOL language with fuzzy semantics and injects the logical knowledge directly into the model structure by adding a special layer. This new layer acts to increase the satisfaction of the logical knowledge by modifying the final predictions of the neural network.

In this section we go deeper into the details of the framework of KENN by focusing on the most important aspects to better understand its role in this thesis.

\subsubsection{Language and semantics}
We can start by introducing more details about the logical language supported by KENN. This framework allows imposing constraints on predictions by providing a set of logical clauses that constitute our knowledge base. For the sake of brevity, we will refer to a logical knowledge base as KB. Each clause of the KB represents a logical formula generated from a subset of the FOL with the following restrictions:
\begin{itemize}
    \item \textit{the only operators allowed are disjunction and negation}
    \item \textit{parentheses are not allowed}
    \item \textit{variables are assumed to be universally quantified}
    \item \textit{only unary and binary predicates are allowed}
    \item \textit{functions are not allowed}
\end{itemize}
Even if we can use only the operators of negation and disjunction, it is possible to use logical equivalence to introduce some other operators into KENN's language. Considering classical logic for simplicity, two examples of logically equivalent formulas are:
\begin{itemize}
    \item  $ A \to B = \neg A \vee B $
    \item  $ \neg ( A \wedge B) = \neg A \vee \neg B $ (De Morgan's law)
\end{itemize}
The mapping between FOL and KENN's restricted language is quite trivial. If we consider the example of logical implication, we can schematize the translation as follows:
\begin{enumerate}
    \item Starting from the FOL formula:
    \begin{gather*}
        \forall X, A(X) \to B(X)
    \end{gather*}
    \item Remove quantifier (universal assumption):
    \begin{gather*}
        A(X) \to B(X)
    \end{gather*}
    \item Remove variables\footnote{we can remove variables because we are dealing only with unary predicates referred to the same variable}:
    \begin{gather*}
        A \to B
    \end{gather*}
    \item Use logical equivalence (when possible) to convert unsupported operators:
    \begin{gather*}
        \neg A \vee B
    \end{gather*}
\end{enumerate}
Now that we transformed the FOL formula into a clause supported by KENN, we have to convert it using the syntax required by the parser:
\begin{enumerate}
    \item Use ``n" as negation symbol and ``," to represent disjunctions:
    \begin{gather*}
        nA,B
    \end{gather*}
    \item Add a positive clause weight; use ``\_" in case you want to set it as learnable parameter:
    \begin{gather*}
        0.5:nA,B
    \end{gather*}
\end{enumerate}

The semantics of KENN's logic is fuzzy. The authors propose the \textit{Gödel’s t-conorm} as fuzzy operator to compute the truth degree of a formula. Given a clause $ c $ representing a disjunction of $ n $ literals, the value of the t-conorm is computed by:
\begin{equation*} \label{eq:godel}
    \bot(c) = \max\limits_{i=1}^{n}(c_{i})
\end{equation*}
According to the equation, we can remark that the satisfaction of a clause depends exclusively on the value of the highest literal appearing in it.

\subsubsection{Architecture}
Moving on to KENN's architecture, we already saw in section \ref{nsi_approaches} that it differs from most of its competitors that inject knowledge through the loss function. The idea behind this approach is to have an additional layer that is responsible for injecting knowledge. This layer is simply put on top of an arbitrary neural network and becomes an integral part of it, thus influencing both the learning process and the predictions at inference time according to the provided logical knowledge. The architecture of KENN is represented in Figure~\ref{fig:kenn_architecture} at the maximum abstraction level.

\begin{figure}[H]
    \centering
    \includegraphics[width=.7\linewidth]{kenn_architecture.jpg}
    \caption{ Architecture of KENN - Features $ x $ are given in input to a neural network (NN), the predictions $ y $ are modified by the Knowledge Enhancer (KE) to satisfy a logical KB ($ \kappa $), thus obtaining the final predictions $ y'$. (Source:~\cite{kenn})}
    \label{fig:kenn_architecture}
\end{figure}

\paragraphn{Components}
As we can see from the Figure~\ref{fig:kenn_architecture}, the main component of the architecture is the Knowledge Enhancer, which has a specific role and is composed of a set of subcomponents that cooperate:
\begin{itemize}
    \item \textbf{Knowledge Enhancer (KE):} The KE is the layer responsible for injecting logical knowledge into the model. Given a logical KB $K$ composed of $n$ clauses, the KE contains $n$ independent \textit{Clause Enhancers} that produce $n$ variations to modify $y$ accordingly to $K$. These variations, also called deltas, are aggregated by the KE to obtain the enhanced output $y'$.
    \item \textbf{Clause Enhancer (CE):} The CE is the unit responsible for increasing the satisfaction of a clause by applying a \textit{boost function}, which acts on the predictions of the labels related to the literals of the clause. Each CE is associated to a clause weight $w_c$ that regulates the influence of the clause during the enhancement process.
    \item \textbf{T-conorm boost function (TBF):} The TBF is the function applied by each CE to increase the value of the Gödel t-conorm of its grounded clause. The TBF produces the deltas that will be aggregated by the KE to obtain the final predictions. Since the TBF is based on the Gödel t-conorm, which is non-differentiable, the authors propose the following soft differentiable approximation to compute the deltas:
    \begin{equation} \label{eq:tbf}
    	\delta _{w_{c}}(v)_{i} = w_{c} \cdot softmax(v)_{i}
    \end{equation}
    where $ w_{c} $ is the weight of the clause $ c $, $ v $ is the preactivation vector (i.e., the output of the base neural network) of the literals belonging to $c$, and $ i $ refers to the \textit{i-th} literal of the clause.
\end{itemize}

\paragraphn{Clause Enhancer details}
Now that the high-level architecture has been presented, we can go deeper into the details of the clause enhancement mechanism. Referring to Figure~\ref{fig:clause_enhancer}, the functioning of a CE can be summarized with the following steps:
\begin{enumerate}
    \item Receive the preactivations $ z $ of all the grounded literals (i.e., the output $y$ of the base neural network).
    \item Apply a pre-elaboration step $ \phi $ to filter the preactivations: keep only the preactivations of the literals belonging to the clause $ c $ and change the sign of the preactivations of negated literals.
    \item Apply the TBF (Equation \ref{eq:tbf}) to the resulting preactivations to produce the deltas.
    \item Apply a post-elaboration step $ \phi' $ to convert the TBF’s results into changes to be applied to the original preactivations $ z $: expand back the dimensionality by filling with 0s the positions of the previously filtered out preactivations, then change back the sign of the deltas of negated literals.
\end{enumerate}
An example of clause enhancement that may help to better comprehend the mechanism is shown in Figure~\ref{fig:example_enhancement}.
\begin{figure}[H]
    \centering
    \includegraphics[width=.35\linewidth]{clause_enhancer.jpg}
    \caption{Clause Enhancer for $ A \vee \neg B $. (Source:~\cite{kenn})}
    \label{fig:clause_enhancer}
\end{figure}

\begin{figure}[H]
    \centering
    \includegraphics[width=.75\linewidth]{example_enhancement.png}
    \caption{Example of clause enhancement for $ A \vee \neg B $, with $w_{c}=2.0$}
    \label{fig:example_enhancement}
\end{figure}

\begin{figure}
    \centering
    \includegraphics[width=.5\linewidth]{knowledge_enhancer.jpg}
    \caption{Knowledge Enhancer producing enhanced predictions. The deltas of each CE are summed to the preactivations of the base neural network. (Source:~\cite{kenn})}
    \label{fig:knowledge_enhancer}
\end{figure}

\paragraphn{Aggregation step}
Once understood the enhancement mechanism for a single clause, it is necessary to explain how the outputs of each CE are combined. An illustration of this step is available in Figure~\ref{fig:knowledge_enhancer}. As we can see, the KE aggregates the initial preactivations to the deltas coming from each CE. The aggregation consists of a simple sum and the resulting value is passed through an activation function to obtain the final predictions $y'$. The authors of KENN motivate the choice of the sum as it leads to faster learning and inference, thus resulting in higher scalability. However, this kind of aggregation may lead to the so-called \textit{conflicts}.

A conflict occurs when the same literal appears with different signs in two or more clauses, thus leading the CEs to produce for the same literal both positive and negative deltas. The natural consequence is that there could be side effects when performing the aggregation step since the benefits of a clause could be mitigated or completely overwhelmed by the effects of another clause. To better understand this particular situation, imagine having two clauses $ c_{1}: \neg A \vee B $ and $ c_{2}: \neg B \vee C \vee D $, where $B$ is the literal involved in possible conflicts. Let us assume that starting from an arbitrary preactivation vector $ z $ the CEs generate $ \delta (z_{c_{1}})_{B} = 0.7 $ and $ \delta (z_{c_{2}})_{B} = -0.5 $. At this point, we can aggregate the deltas obtaining $\delta _{B} = 0.7 - 0.5 = 0.2$. The result is that the final change on $ B $ will reflect the effect of the stronger grounded clause lowered by the effect of the weaker one. It is important to underline that the more literals involved in a clause, the less the probability to encounter relevant conflicts, since the same literal must be dominant in both clauses~\cite{kenn}.

\paragraphn{Enhancement of a logical implication}
An important aspect to remark and keep in mind is that since the goal of KENN is to increase the Gödel t-conorm of a grounded clause, the action of a CE is always the following: \textit{produce a positive boost to the preactivations of positive literals and a negative boost to the preactivations of negative literals}. In other words, the sign of a delta computed by the CE reflects the sign of the related literal of the clause. Thus, reminding that a logical implication must be expressed in KENN by using the logical equivalence $ A \to B = \neg A \vee B $, the effect of a CE always results in a decrease of the preactivation of $A$ and an increase of the preactivation of $B$. This means that a violated grounded clause (i.e., $ 1 \to 0 $) is always pushed by KENN towards its satisfiability (i.e., $ 1 \to 1 $, $ 0 \to 0 $, or $ 0 \to 1 $).

If we consider again the example in Figure~\ref{fig:example_enhancement}, which is based on the clause $A \vee \neg B $ that can be seen as $ B \to A $, we can analyze how the knowledge enhancement of an implication would modify the initial predictions. Table~\ref{tab:example_tbf_values} reports the values obtained at each step of the CE, plus the prediction on $A$ and $B$ before and after the enhancement. If we consider $ threshold = 0.5 $ to assign 1 and 0 to the grounded literals and we compare the columns $ \sigma (z) $ and $ \sigma (\delta _{w_{c}}(z)+ z)$, we can observe that KENN changed $1 \to 0$ into $1 \to 1$.

\begin{table}[h]
\centering
\caption{Numeric example of the action of KENN for $ A \vee \neg B $}
\label{tab:example_tbf_values}
\begin{tabular}{c|c|c|c|c|c|c|c|}
\cline{2-8}
                                 & $ z $ & $ \sigma (z) $       & $ z_{c} $ & $ \delta _{w_{c}}(z_{c}) $ & $ \delta _{w_{c}}(z) $ & $ \delta _{w_{c}}(z) + z $ & $ \sigma (\delta _{w_{c}}(z)+ z)$ \\ \hline
\multicolumn{1}{|c|}{$ A $}      & -0.7  & 0.33 ($ \approx 0 $) & -0.7      & 1.8                        & 1.8                    & 1.1                        & 0.75 ($ \approx 1 $)              \\ \hline
\multicolumn{1}{|c|}{$ B $}      & 2.9   & 0.95 ($ \approx 1 $) & -         & -                          & -0.2                   & 2.7                        & 0.94 ($ \approx 1 $)              \\ \hline
\multicolumn{1}{|c|}{$ \neg B $} & -     & -                    & -2.9      & 0.2                        & -                      & -                          & -                                 \\ \hline
\end{tabular}
\end{table}

\paragraphn{Considerations on the clause weight}
In the example above, KENN with its intervention changed the final predictions leading to a satisfied clause. This is not always the case, since the choice of a different clause weight could lead to another outcome. In Figure~\ref{fig:sigmoid_example} we can see two examples that start from the same preactivations, but use different clause weights. The examples show in a graphical way the changes introduced by KENN to the initial prediction from the perspective of the sigmoid activation function. Considering that a classification threshold of 0.5 on post-sigmoid values is equivalent to using a 0 threshold directly on preactivations, we can see that the delta produced in Figure~\ref{fig:sigmoid_example_w2} is not enough to push the prediction over the threshold. On the contrary, Figure~\ref{fig:sigmoid_example_w1} shows that the prediction changes its sign thanks to a higher clause weight.

\begin{figure}[h]
     \centering
     \begin{subfigure}{0.47\textwidth}
         \centering
         \includegraphics[width=\textwidth]{figures/ex_enhancement_w2.JPG}
          \caption{$w=2$,  $\delta_{Y} = 1.6$,  $Y'=-0.4$}
         \label{fig:sigmoid_example_w2}
     \end{subfigure}
     \hfill
     \begin{subfigure}{0.47\textwidth}
         \centering
         \includegraphics[width=\textwidth]{figures/ex_enhancement_w5.JPG}
         \caption{$w=5$,  $\delta_{Y} = 4$,  $Y'=2$}
         \label{fig:sigmoid_example_w1}
     \end{subfigure}
        \caption{Example of enhancement given $Y = -2$ and $softmax_{Y} = 0.8$, from the perspective of the sigmoid activation function}
        \label{fig:sigmoid_example}
\end{figure}

% \begin{table}
% \centering
% \caption{Truth table of the clause $ A \vee \neg B $}
% \label{tab:example_tbf_tt}
% \begin{tabular}{c|c|c|c|}
% \cline{2-4}
%                         & $ A $ & $ B $ & $ A \vee \neg B $ \\ \hline
% \multicolumn{1}{|c|}{1} & 0     & 0     & 1                 \\ \hline
% \multicolumn{1}{|c|}{2} & 0     & 1     & 0                 \\ \hline
% \multicolumn{1}{|c|}{3} & 1     & 0     & 1                 \\ \hline
% \multicolumn{1}{|c|}{4} & 1     & 1     & 1                 \\ \hline
% \end{tabular}
% \end{table}
\paragraphn{Relational KENN}
The authors of KENN propose also an extended version of the framework to use logical knowledge in relational domains. In that version, it is possible to specify relations between examples in contexts like collective classification by using binary predicates. The architecture of the relational version is slightly different and requires more sophisticated steps to inject logical knowledge into the neural network. The details can be found in~\cite{daniele2021neural} and will not be discussed here since this thesis focuses on the use of unary predicates.


% !TeX root = ../main.tex

\begin{survey}
\label{cha:survey}

\title{Title of the Survey}
\maketitle

写出至少 5000 外文印刷字符的调研阅读报告或者书面翻译 1-2 篇(不少于 2 万外文印刷符)。

\tableofcontents

\section{Single-Objective Programming}

It is impossible to cover in a single chapter every concept of mathematical
programming\cite{tex}. This chapter introduces only the basic concepts and techniques of
mathematical programming such that readers gain an understanding of them
throughout the book.~\cite{abrahams99tex,salomon1995advanced}.

The general form of single-objective programming (SOP) is written
as follows,
\begin{equation*} % 如果附录中的公式不想让它出现在公式索引中，那就请
                             % 用 equation*
\left\{\begin{array}{l}
\max \,\,f(x)\\%[0.1 cm]
\mbox{subject to:} \\%[0.1 cm]
\qquad g_j(x)\le 0,\quad j=1,2,\cdots,p
\end{array}\right.
\end{equation*}
which maximizes a real-valued function $f$ of
$x=(x_1,x_2,\cdots,x_n)$ subject to a set of constraints.

\newcommand\Real{\mathbf{R}}
\newtheorem{mpdef}{Definition}[chapter]
\begin{mpdef}
In SOP, we call $x$ a decision vector, and
$x_1,x_2,\cdots,x_n$ decision variables. The function
$f$ is called the objective function. The set
\begin{equation*}
S=\left\{x\in\Real^n\bigm|g_j(x)\le 0,\,j=1,2,\cdots,p\right\}
\end{equation*}
is called the feasible set. An element $x$ in $S$ is called a
feasible solution.
\end{mpdef}

\newtheorem{mpdefop}[mpdef]{Definition}
\begin{mpdefop}
A feasible solution $x^*$ is called the optimal
solution of SOP if and only if
\begin{equation}
f(x^*)\ge f(x)
\end{equation}
for any feasible solution $x$.
\end{mpdefop}

One of the outstanding contributions to mathematical programming was known as
the Kuhn-Tucker conditions~\eqref{eq:ktc}. In order to introduce them, let us give
some definitions. An inequality constraint $g_j(x)\le 0$ is said to be active at
a point $x^*$ if $g_j(x^*)=0$. A point $x^*$ satisfying $g_j(x^*)\le 0$ is said
to be regular if the gradient vectors $\nabla g_j(x)$ of all active constraints
are linearly independent.

Let $x^*$ be a regular point of the constraints of SOP and assume that all the
functions $f(x)$ and $g_j(x),j=1,2,\cdots,p$ are differentiable. If $x^*$ is a
local optimal solution, then there exist Lagrange multipliers
$\lambda_j,j=1,2,\cdots,p$ such that the following Kuhn-Tucker conditions hold,
\begin{equation}
\label{eq:ktc}
\left\{\begin{array}{l}
    \nabla f(x^*)-\sum\limits_{j=1}^p\lambda_j\nabla g_j(x^*)=0\\%[0.3cm]
    \lambda_jg_j(x^*)=0,\quad j=1,2,\cdots,p\\%[0.2cm]
    \lambda_j\ge 0,\quad j=1,2,\cdots,p.
\end{array}\right.
\end{equation}
If all the functions $f(x)$ and $g_j(x),j=1,2,\cdots,p$ are convex and
differentiable, and the point $x^*$ satisfies the Kuhn-Tucker conditions
\eqref{eq:ktc}, then it has been proved that the point $x^*$ is a global optimal
solution of SOP.

\subsection{Linear Programming}
\label{sec:lp}

If the functions $f(x),g_j(x),j=1,2,\cdots,p$ are all linear, then SOP is called
a \emph{linear programming}.

The feasible set of linear is always convex. A point $x$ is called an extreme
point of convex set $S$ if $x\in S$ and $x$ cannot be expressed as a convex
combination of two points in $S$. It has been shown that the optimal solution to
linear programming corresponds to an extreme point of its feasible set provided
that the feasible set $S$ is bounded. This fact is the basis of the \emph{simplex
  algorithm} which was developed by Dantzig as a very efficient method for
solving linear programming.
\begin{table}[ht]
\centering
  \centering
  \caption{This is an example for table}
  \label{tab:badtabular2}
  \begin{tabular}[c]{|m{1.5cm}|c|c|c|c|c|c|}\hline
    \multicolumn{2}{|c|}{Network Topology} & \# of nodes &
    \multicolumn{3}{c|}{\# of clients} & Server \\\hline
    GT-ITM & Waxman Transit-Stub & 600 &
    \multirow{2}{2em}{2\%}&
    \multirow{2}{2em}{10\%}&
    \multirow{2}{2em}{50\%}&
    \multirow{2}{1.2in}{Max. Connectivity}\\\cline{1-3}
    \multicolumn{2}{|c|}{Inet-2.1} & 6000 & & & &\\\hline
    \multirow{2}{1.5cm}{Xue} & Rui  & Ni &\multicolumn{4}{c|}{\multirow{2}*{\thuthesis}}\\\cline{2-3}
    & \multicolumn{2}{c|}{ABCDEF} &\multicolumn{4}{c|}{} \\\hline
\end{tabular}
\end{table}

Roughly speaking, the simplex algorithm examines only the extreme points of the
feasible set, rather than all feasible points. At first, the simplex algorithm
selects an extreme point as the initial point. The successive extreme point is
selected so as to improve the objective function value. The procedure is
repeated until no improvement in objective function value can be made. The last
extreme point is the optimal solution.

\subsection{Nonlinear Programming}

If at least one of the functions $f(x),g_j(x),j=1,2,\cdots,p$ is nonlinear, then
SOP is called a \emph{nonlinear programming}.

A large number of classical optimization methods have been developed to treat
special-structural nonlinear programming based on the mathematical theory
concerned with analyzing the structure of problems.
\begin{figure}[h]
  \centering
  \includegraphics{thu-lib-logo.pdf}
  \caption{This is an example for figure.}
  \label{tab:badfigure2}
\end{figure}

Now we consider a nonlinear programming which is confronted solely with
maximizing a real-valued function with domain $\Real^n$.  Whether derivatives are
available or not, the usual strategy is first to select a point in $\Real^n$ which
is thought to be the most likely place where the maximum exists. If there is no
information available on which to base such a selection, a point is chosen at
random. From this first point an attempt is made to construct a sequence of
points, each of which yields an improved objective function value over its
predecessor. The next point to be added to the sequence is chosen by analyzing
the behavior of the function at the previous points. This construction continues
until some termination criterion is met. Methods based upon this strategy are
called \emph{ascent methods}, which can be classified as \emph{direct methods},
\emph{gradient methods}, and \emph{Hessian methods} according to the information
about the behavior of objective function $f$. Direct methods require only that
the function can be evaluated at each point. Gradient methods require the
evaluation of first derivatives of $f$. Hessian methods require the evaluation
of second derivatives. In fact, there is no superior method for all
problems. The efficiency of a method is very much dependent upon the objective
function.

\subsection{Integer Programming}

\emph{Integer programming} is a special mathematical programming in which all of
the variables are assumed to be only integer values. When there are not only
integer variables but also conventional continuous variables, we call it \emph{
  mixed integer programming}. If all the variables are assumed either 0 or 1,
then the problem is termed a \emph{zero-one programming}. Although integer
programming can be solved by an \emph{exhaustive enumeration} theoretically, it
is impractical to solve realistically sized integer programming problems. The
most successful algorithm so far found to solve integer programming is called
the \emph{branch-and-bound enumeration} developed by Balas (1965) and Dakin
(1965). The other technique to integer programming is the \emph{cutting plane
  method} developed by Gomory (1959).

\hfill\textit{Uncertain Programming\/}\quad(\textsl{BaoDing Liu, 2006.2})

\bibliographystyle{plainnat}
\bibliography{ref/refs,ref/appendix}

\end{survey}


\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage{color}
\newcommand{\comm}{\textcolor{red}}  % for making comments
\renewcommand{\v}{\mathbf}
\usepackage{amsfonts}
\newcommand{\Z}{\mathbb{Z}}
\newcommand{\R}{\mathbb{R}}
\setlength{\parindent}{0.5in}
\title{How to Raytrace}
\begin{document}
\maketitle

\emph{Throughout the following, a color is an element of $[0,1]^3$, with
addition and multiplication defined componentwise; if, after all other
calculations, a component of a color is greater than 1, it is set to 1.
Moreover, $\epsilon$ represents an arbitrarily small quantity, used to avoid
immediately hitting the current surface at the current point again.}

\vspace{2ex}

Given a ``camera'' at point $\v{o}$ and a ``screen,'' for every point $\v{s}$
on the screen, let $\v{v} = (\v{s}-\v{o})/\|\v{s}-\v{o}\|$; the color of the
pixel at $\v{s}$ is then found by finding the first object that the ray
$\v{o}+t\v{v}$ ($t\geq 0$) intersects:

\begin{itemize}
\item If it hits nothing, the color of the pixel is black.
\item If it hits a light, the color of the pixel is white.
\item If it hits a non-light object $Q$, let $\v{p}$ be the point of intersection and $\v{n}$ be the unit vector normal to the surface of $Q$ at $\v{p}$.  The color of the pixel is $C = (0, 0, 0)$ with values added to it as follows:
 \begin{itemize}
 \item For every light source in the scene, let $\v{\ell}$ be its center and let $\v{m}=(\v{\ell}-\v{p})/\|\v{\ell}-\v{p}\|$.  If the ray $(\v{p}+\epsilon\v{m})+t\v{m}$ intersects a non-light primitive before intersecting $\ell$, skip the rest of this step for this light.  Otherwise:
  \begin{itemize}
  \item If $\v{n}\bullet\v{m}$ and the diffusion of $Q$'s material are both positive, multiply their product by the product of the colors of $Q$ and the light source and add the result to $C$.
  \item If $\v{v}\bullet(\v{m} - 2(\v{m}\bullet\v{n})\v{n})$ and the specularity of $Q$'s material are both positive, calculate the former to the power of 20\comm{?} multiplied by the product of the latter and the color of the light source, and add the result to $C$.
  \end{itemize}
 \item If the reflectiveness of $Q$'s material is greater than zero, let $\v{r} = \v{v} - 2(\v{v}\bullet\v{n})\v{n}$ (the reflection of $\v{p}$ after hitting $Q$) and add to $C$ the color corresponding to the ray $(\v{p}+\epsilon\v{r})+t\v{r}$ multiplied by $Q$'s reflectiveness, multiplied by $Q$'s color.  (The depth of this recursion should be limited to some fixed value.)
 \item If $Q$'s material permits refraction, let $i$ be the index of refraction of the medium in which $\v{o}$ is located divided by the index of refraction on the other side of the surface of $Q$, let $c = \v{v}\bullet\v{n}$, and let $s = i^2(1-c^2)$.  If $s$ is less than or equal to 1, let $\v{t} = i(\v{v} - (c+\sqrt{1-s})\v{n})$ (the refraction of $\v{v}$ through the surface of $Q$) and add to $C$ the color corresponding to the ray $(\v{p}+\epsilon\v{t})+t\v{t}$.  (If $s>1$, then total internal reflection occurs, which \comm{does not need to be handled???})
 \end{itemize}
\end{itemize}

\section*{Formul\ae{} for Intersections}
\begin{itemize}

\item Given a sphere of radius $r$ centered at $\v{c}$, the values of $t$ for
which a vector $\v{o}+t\v{v}$ intersects it are all of the solutions of
$\|\v{v}\|^2 t^2 + 2(\v{v}\bullet(\v{o}-\v{c})) t + \|\v{o}-\v{c}\|^2 = r^2$.

\item Given a plane with normal vector $\v{n}$ and containing the point
$d\v{n}$ for some $d\in\R^\geq$, the value of $t$ for which a (non-coplanar)
vector $\v{o}+t\v{v}$ intersects it is $((d\v{n}-\v{o})\bullet\v{n}) /
(\v{v}\bullet\v{n})$.

\item Given an indexed set of points $\{\v{x}_i\}_{i=0}^n$ forming a polygon
such that each $\v{x}_i$ is adjacent to both $\v{x}_{(i-1)\bmod(n+1)}$ and
$\v{x}_{(i+1)\bmod(n+1)}$, the values of $t$ for which a vector $\v{o}+t\v{v}$
intersects the interior (i.e., not the edges) of this polygon are all the $t$'s
such that $\v{p} = \v{o}+t\v{v}$ is in the plane containing the polygon and
such that the values in $\{((\v{x}_{(i+1)\bmod(n+1)} - \v{x}_i)\times(\v{p} -
\v{x}_i))\bullet\v{n} : i\in[0,n]\cap\Z\}$ (where $\v{n}$ is a vector normal to
the plane) are either all positive or all negative.  \comm{Does this only work
for convex polygons?}

\end{itemize}

\end{document}

TO ADD:
 - formulae for normals
 - formulae for cylindrical shells
 - descriptions of the relevant properties of materials (color, diffusion,
   specularity, reflectiveness, index of refraction (in (0, \infty)?), etc.)
  - In Bikker's raytracer{1,2} (but not raytracer3), specularity was equal to 1
    minus the diffusion; look into this.
 - Sources:
  - <http://www.devmaster.net/articles/raytracing_series/part1.php>
  - <http://www.devmaster.net/articles/raytracing_series/Reflections%20and%20Refractions%20in%20Raytracing.pdf>
  - <http://www.itu.dk/courses/IM/Projects/Raytracer/RayNotes.pdf>

Alternative algorithm for checking whether a given point p is inside a polygon:
Pick a point known to be outside the polygon and consider the line segment
between it and p; p is then inside the polygon iff this line segment intersects
an odd number of edges of the polygon.