\subsection{Projection methods}

\begin{Definition}{galerkin-method}
  Let $\mata\in\Rnn$ and $\vb\vx\in\R^n$ with $\mata\vx=\vb$. Then,
  the vector $\tilde\vx\in\R^n$ is called the \define{Galerkin
    approximation} of $\vx$ in a subspace $K$ orthogonal to a subspace
  $L$, if there holds
  \begin{align}
    \tilde\vx &\in K,\\
    \vb-\mata\tilde\vx &\perp L.
                         \label{eq:krylov:1}
  \end{align}
  This type of approximation is called \define{Galerkin method}, more
  specifically \define{Ritz-Galerkin method} in the case $K=L$ and
  \define{Petrov-Galerkin method} in the case $K\neq L$.
\end{Definition}

\begin{remark}
  For the case $K=L$ we deduce from the optimization property of
  orthogonal projections that $\mata\tilde x$ in~\eqref{eq:krylov:1}
  is the vector in the subspace $\mata K$ closest to $\vb$. Thus,
  $\tilde\vx$ minimizes the \putindex{residual} $\vb-\mata\vy$ over
  all choices $\vy\in K$.

 Note that this does not hold for $K\neq L$.
\end{remark}

\begin{Definition}{projection-step}
  Given a vector $\vx^{(k)}\in\R^n$ and its residual
  $\vb-\mata\vx^{(k)}$. Then, we say that the vector
  $\vx^{(k+1)}\in\R^n$ is obtained by a \define{projection step}, if
  \begin{gather}
    \vx^{(k+1)} = \vx^{(k)} + \vy,
  \end{gather}
  where after the choice of subspaces $K$ and $L$ the update $\vy$ is
  determined by the condition
  \begin{align}
    \vy&\in K\\
    \vr^{(k)} - \mata\vy &\perp L.
  \end{align}
\end{Definition}

\begin{Example}{projection-gauss-seidel}
  The Gauss-Seidel substep is a projection step with the choice
  \begin{gather}
    K=L=\spann{\ve_i}.
  \end{gather}
\end{Example}

\begin{notation}
  We will mostly only consider a single step of the method in
  \slideref{Definition}{projection-step}. Therefore, we will consider
  the step from an initial guess $\vx^{(0)}$ to an approximate solution
  $\tilde \vx$ to simplify the notation.
\end{notation}

\begin{Definition}{projection-method-matrix}
  Let $\matv=(\vv_1,\dots,\vv_m)$ and $\matw=(\vw_1,\dots,\vw_m)$ be bases for
  the subspaces $K$ and $L$, respectively. Then, the solution
  $\tilde \vx$ to the projection step is determined by $\vy\in\R^m$ and
  \begin{align}
    \tilde\vx &= \vx^{(0)} + \matv\vy\\
    \matw^*\mata\matv \vy &= \matv^* \vr^{(0)}.
  \end{align}
  It is thus obtained by solving an $m$-by-$m$ linear system, called
  the projected system or the \define{Galerkin
    equations}. $\matw^*\mata\matv\in\R^{m\times m}$ is the
  \define{projected matrix}.
\end{Definition}

\begin{proof}
  See \cite[Section 5.1.2]{Saad00}.
\end{proof}

\begin{Theorem}{projected-invertible}
  Let one of the following conditions hold:
  \begin{enumerate}
  \item $\mata$ is symmetric, positive definite and $L=K$.
  \item $\mata$ is invertible and $L = \mata K$.
  \end{enumerate}
  Then, the projected matrix $\matw^*\mata\matv$ is invertible for any
  bases $\matv$ and $\matw$ of $K$ and $L$, respectively.
\end{Theorem}

\begin{Theorem}{projection-orthogonal-optimal}
  Let $\mata\in\Rnn$ be symmetric, positive definite. Then,
  $\tilde \vx$ is the result of the orthogonal ($L=K$) projection
  method with initial vector $\vx^{(0)}$ if and only if it minimizes
  the \putindex{A-norm} of the error over the space $\vx^{(0)}+K$. Namely, for the
  solution $\vx$ of $\mata\vx=\vb$ there holds
  \begin{gather}
    \norm{\tilde\vx-\vx}_A = \min_{\vy\in\vx^{(0)}+K} \norm{\vy-\vx}_A.
  \end{gather}
  Here, $\norm\vy_A = \sqrt{\vy^*\mata\vy}$.
\end{Theorem}

\begin{Theorem}{projection-oblique-optimal}
  Let $\mata\in\Rnn$ and $L=\mata K$. Then, $\tilde \vx$ is the result
  of the (oblique) projection method with initial vector $\vx^{(0)}$
  if and only if it minimizes the Euclidean norm of the residual
  $\vb-\mata\tilde\vx$ over the space $\vx^{(0)}+K$. Namely, there
  holds
  \begin{gather}
    \norm{\vb-\mata\tilde\vx}_2
    = \min_{\vy\in\vx^{(0)}+K} \norm{\vb-\mata\vy}_2
  \end{gather}
\end{Theorem}

\begin{Example}{projection-1d}
  A one-dimensional projection method can be characterized by two
  vectors $\vv$ and $\vw$. Then,
  \begin{gather}
    K = \spann\vv,
    \qquad L = \spann\vw.
  \end{gather}
  The new solution is
  \begin{gather}
    \tilde\vx = \vx^{(0)}+\alpha \vv,
  \end{gather}
  where
  \begin{gather}
    \alpha = \frac{\scal(\vr^{(0)},\vw)}{\scal(\mata\vv,\vw)}
  \end{gather}
  is determined from the Galerkin condition $\vr^{(0)}-\mata\vy \perp \vw$
\end{Example}

\begin{Algorithm*}{steepest-descent-algol}{The steepest descent method}
  \begin{algorithmic}[1]
    \Require $\mata\in\Rnn$ s.p.d.; $\quad\vx,\vb\in\R^n$
    \State $\vr\gets \vb-\mata\vx$
    \Repeat
    \State $\alpha\gets\frac{\scal(\vr,\vr)}{\scal(\mata\vr,\vr)}$
    \State $\vx \gets \vx + \alpha\vr$
    \State $\vr \gets \vr-\alpha\mata\vr$
    \Until convergence
  \end{algorithmic}
\end{Algorithm*}

\begin{Lemma}{steepest-descent}
  Each step of the steepest descent method computes the minimum of
  $F(\vy) = \norm{\vy-\vx}_A^2$ along the line from the current
  iterate $\vx^{(k)}$ in direction $-\nabla F(\vx^{(k)})$.
\end{Lemma}

\begin{remark}
  In this algorithm, the operation $\mata\vr$ is applied twice. Since
  in most implementations this will be the part with the most
  computational operations, we introduce an auxiliary vector
  $\vp = \mata\vr$, which can be used in lines 3 and 5. At the cost of
  one additional vector, the numerical effort per step is almost
  cut in half.
\end{remark}

\begin{Remark}{convergence-residual}
  The residual $\vr = \vb - \mata\tilde\vx$ of the current iterate
  $\tilde\vx$ measures the misfit of $\tilde\vx$ in the equation
  $\mata\vx = \vb$. Since the error $\tilde\vx-\vx$ is unknown, we can
  use it as criterion for the accuracy of the current
  solution. Indeed, there holds
  \begin{gather}
    \norm{\vx-\tilde\vx}
    = \norm*{\mata^{-1}\bigl(\mata\vx - \mata\tilde\vx\bigr)}
    = \norm*{\mata^{-1}\bigl(\vb - \mata\tilde\vx\bigr)}
    \le \norm{\mata^{-1}}\norm{\vr}.
  \end{gather}
  Therefore, the criterion for convergence of the algorithm is typically
  \begin{gather}
    \norm{\vr} \le \text{TOL},
  \end{gather}
  where TOL is a tolerance chosen by the user.

  Note, that the computation of $\vr$ is subject to roundoff
  errors. Thus, the tolerance should always be chosen
  \begin{gather}
    \text{TOL} > c\norm{\vb}\eps,
  \end{gather}
  where $\eps$ is the machine accuracy and $c$ should account for
  error accumulation in the matrix-vector product.
\end{Remark}

\begin{Algorithm*}{steepest-descent-python1}{Steepest descent in Python}
  \lstinputlisting{python/steepest-descent.py}
\end{Algorithm*}

\begin{remark}
  Depending on the quality of the compiler/interpreter, the code line
  \begin{lstlisting}[language=Python,numbers=none]
    x += alpha*r
  \end{lstlisting}
  may involve creating an auxiliary vector $\vp = \alpha \vr$ and
  adding this vector to $\vx$.  Obviously, this could be avoided by
  directly implementing
  \begin{lstlisting}[language=Python,numbers=none]
    for i in range(0,n):
      x[i] += alpha*r[i]
  \end{lstlisting}
  Since operations like this are ubuquitous in scientific computing,
  they were standardized early on in the BLAS (basic linear algebra
  subroutines) library. It contains the FORTRAN function
  \begin{lstlisting}[language=Fortran,numbers=none]
    SUBROUTINE DAXPY( n, alpha, x, incx, y, incy)
  \end{lstlisting}
  which computes $\vy\gets\vy+\alpha\vx$ for double precision vectors
  of length $n$ (the increment arguments allow to skip elements). For
  usage in Python, there is a wrapper
  \begin{lstlisting}[language=Python,numbers=none]
    scipy.linalg.blas.daxpy(x, y[, n, a, offx, incx, offy, incy])
  \end{lstlisting}
\end{remark}

\begin{Algorithm*}{steepest-descent-python2}{Steepest descent with daxpy}
  \lstinputlisting{python/steepest-descent-axpy.py}
\end{Algorithm*}

\begin{Algorithm*}{minimal-residual-algol}{The minimal residual method}
  \begin{algorithmic}[1]
    \Require $\mata\in\Rnn$; $\quad\vx,\vb\in\R^n$
    \State $\vr\gets \vb-\mata\vx$
    \Repeat
    \State $\alpha\gets\frac{\scal(\mata\vr,\vr)}{\scal(\mata\vr,\mata\vr)}$
    \State $\vx \gets \vx + \alpha\vr$
    \State $\vr \gets \vr-\alpha\mata\vr$
    \Until convergence
  \end{algorithmic}
\end{Algorithm*}

\begin{Lemma}{minimal-residual}
  Each step of the minimal method computes the minimum of
  $F(\vy) = \norm{\vb-\mata\vy}_2^2$ along the line from the current
  iterate $\vx^{(k)}$ in direction $\vr$.
\end{Lemma}

\begin{Lemma}{lucky-breakdown}
  The iteration sequences $\{\vx^{(k)}\}$ of the steppest descent and
minimal residual methods, respectively, are well defined except for
the case where $\vx^{(k)}$ is the exact solution $\vx$ and thus
$\vr^{(k)} = 0$.

We refer to this phenomenon as \define{lucky breakdown}, since the
method only fails after the exact solution has been found.
\end{Lemma}

\begin{Lemma*}{kantorovich-inequality}{Kantorovich inequality}
  Let $\mata\in\Rnn$ be symmetric, positive definite with minnimal and
  maximal eigenvalues $\lambda_{\min}$ and $\lambda_{\max}$. Then, for
  $\vx\in\R^n$ there holds
  \begin{gather}
    \frac{\scal(\mata\vx,\vx)\scal(\mata^{-1}\vx,\vx)}{\scal(\vx,\vx)^2}
    \le \frac{(\lambda_{\min}+\lambda_{\max})^2}{4\lambda_{\min}\lambda_{\max}}
  \end{gather}
\end{Lemma*}

\begin{Theorem}{steepest-descent-convergence}
  Let $\mata\in\R^nn$ be symmetric, positive definite with extremal
  eigenvalues $\lambda_{\min}$ and $\lambda_{\max}$. Then, the error
  $\ve^{(k)} = \vx-\vx^{(k)}$ of the steepest descent method admits
  the estimate
  \begin{gather}
    \norm{\ve^{(k+1)}}_A \le \rho \norm{\ve^{(k)}}_A,
  \end{gather}
  where the contraction number is
  \begin{gather}
    \rho
    = \frac{\lambda_{\max}-\lambda_{\min}}{\lambda_{\max}+\lambda_{\min}}
    = \frac{\cond_2\mata - 1}{\cond_2\mata+1}
    = 1-\frac2{\cond_2\mata+1}
  \end{gather}
\end{Theorem}

\begin{Theorem}{minimal-residual-convergence}
  Let $\mata\in\Rnn$ such that its symmetric part $(\mata+\mata^T)/2$
  is positive definite. Then, the residuals $\vr^{(k)}$ of the minimal
  residual method admit the estimate
  \begin{gather}
    \norm{\vr^{(k+1)}}_2 \le \rho \norm{\vr^{(k)}}_2,
  \end{gather}
  where the contraction number is
  \begin{gather}
    \rho = \left(1-\frac{\norm{\mata}^2}{\sigma^2}\right)^{\nicefrac12}
  \end{gather}
  and $\sigma$ is the smallest eigenvalue of $(\mata+\mata^T)/2$.
\end{Theorem}

\begin{remark}
  Let $e_k$ be some measure of the error of a fixed-point iteration
  with contraction number $\rho$ after $k$ steps. Then, there holds
  \begin{gather}
    e_k \le \rho^k e_0.
  \end{gather}
  Thus, the number of steps needed to obtain a (relative) reduction of
  the error by a prescribed relative tolerance $\epsilon$, that is, to
  achieve $e_k/e_0\le\epsilon$, is
  \begin{gather}
    \label{eq:krylov:steps-convergence}
    k \ge \frac{\log\epsilon}{\log\rho}.
  \end{gather}
  From this formula, we realize that the number of steps of such a
  method grows linearly with the logarithm of the tolerance. Further,
  it is inverse proportional to the logarithm of the contraction
  number $\rho$.

  Note that logarithms with respect to any base can be used
  in~\eqref{eq:krylov:steps-convergence}, since the quotient is
  independent of the base.
\end{remark}

\begin{Definition}{convergence-rate-logarithmic}
  The (logarithmic) \define{convergence rate} of a contraction with
  \putindex{contraction number} $\rho$ is
  \begin{gather}
    r_c = -\log_{10} \rho.
  \end{gather}

  The average \define{observed convergence rate} of $k$ steps of a
  method with error measure $e_k$ is
  \begin{gather}
    \overline{r_c} = \frac1k \log_{10}\frac{e_0}{e_k}.
  \end{gather}
\end{Definition}

\begin{remark}
  The logarithmic convergence rate allows us to directly compare two
  iterative methods. Let's say, there are two iterations $M_1$ and
  $M_2$ and the convergence rate of $M_2$ ist twice the rate of
  $M_1$. Then, given an initial vector, $M_1$ will need twice as many
  steps compared to $M_2$ to reach the same accuracy.

  This implies that $M_2$ is favorable, if the effort for each step is
  less than twice the effort for $M_1$. If it is more than twice, then
  $M_1$ is the faster method in spite of the slower convergence.
\end{remark}

\subsection{Krylov spaces}

\begin{Definition}{krylov-space}
  For a matrix $\mata\in\Rnn$ and a vector $\vv\in\R^n$, we define the
  \define{Krylov space}
  \begin{gather}
    \krylov_m = \krylov_m(\mata,\vv)
    = \spann{\vv,\mata\vv,\dots,\mata^{m-1}\vv}.
  \end{gather}
\end{Definition}

\begin{Definition}{grade-of-v}
  For a matrix $\mata\in\Rnn$ and a vector $\vv\in\R^n$ we define the
  \define{grade} of $\vv$, more precise the grade of $\vv$ with
  respect to $\mata$ as the minimal grade of a polynomial $p$ such
  that
  \begin{gather}
    p(\mata)\vv = 0
  \end{gather}
\end{Definition}

\begin{Lemma}{krylov-invariant}
\end{Lemma}
\begin{Lemma}{krylov-polynomial}
  The Krylov space $\krylov_m(\mata,\vv)$ is isomorphic to $\P_{m-1}$
  if and only if the grade of $\vv$ with respect to $\mata$ is at
  least $m$. In particular, for any $\vw\in\krylov_m(\mata,\vv)$ there
  is a unique polynomial $p\in\P_{m-1}$ such that
  \begin{gather}
    \vw = p(\mata)\vv.
  \end{gather}

  It is invariant under the action of $\mata$ if and only if the grade
  of $\vv$ does not exceed $m$.
\end{Lemma}

\begin{proof}
  See \cite[Propositions 6.1 \& 6.2]{Saad00}.
\end{proof}

\begin{Lemma}{krylov-projector}
  Let $\matq_m$ be a projector onto $\krylov_m$ and define
  \begin{align}
    \mata_m\colon \krylov_m &\to \krylov_m\\
    \vv&\mapsto \matq_m \mata.
  \end{align}
  Then, for any polynomial $q\in\P_{m-1}$ there holds
  \begin{gather}
    q(\mata)\vv = q(\mata_m)\vv.
  \end{gather}
  For any polynomial $p\in\P_m$, there holds
  \begin{gather}
    \matq_m p(\mata)\vv = p(\mata_m)\vv
  \end{gather}
\end{Lemma}

\begin{proof}
  See \cite[Proposition 6.3]{Saad00}.
\end{proof}

\subsection{The Arnoldi procedure}

\begin{Algorithm*}{arnoldi-1}{Arnoldi I}
  \algtext*{EndIf}
  \begin{algorithmic}[1]
    \Require $\mata\in\Rnn$; $\quad\vv_1\in\R^n, \norm{\vv_1}_2 = 1$
    \For{$j=1,\dots,m$}
    \For{$i=1,\dots,j$}
    \State $h_{ij} \gets \scal(\mata\vv_j,\vv_i)$
    \EndFor
    \State $\vw_j \gets \mata \vv_j-\sum_{i=1}^j h_{ij}\vv_i$
    \State $h_{j+1,j} \gets \norm{\vw_j}_2$
    \If{$h_{j+1,j}=0$} \textbf{stop}\EndIf
    \State $\vv_{j+1} = \nicefrac{\vw_j}{h_{j+1,j}}$
    \EndFor
  \end{algorithmic}
\end{Algorithm*}

\begin{Lemma}{arnoldi-krylov}
  Assume that the Arnoldi algorithm does not stop before step
  $m$. Then, the vectors $\vv_1,\dots,\vv_m$ form an orthonormal basis
  of $\krylov_m(\mata,\vv_1)$.
\end{Lemma}

\begin{proof}
  See \cite[Proposition 6.4]{Saad00}.
\end{proof}

\begin{Theorem}{arnoldi-projection}
  Let $\matv_m = (\vv_1,\dots,\vv_m)$ be the matrix of basis vectors
  generated by the Arnoldi method and let
  $\overline{\matH}_m\in\R^{m+1\times m}$ be the Hessenberg matrix of
  entries $h_{ij}$ computed in the algorithm. Let further
  $\mathH_m\in\R^{m\times m}$ be the same matrix without the last
  row. Then, there holds
  \begin{align}
    \mata\matv_m &= \matv_m\matH_m+\vw_m\ve_m^T\\
                 &= \matv_{m+1}\overline{\matH}_m\\
  \end{align}
  and
  \begin{gather}
    \matH_m = \matv_m^T\mata\matv.
  \end{gather}
\end{Theorem}

\begin{proof}
  See \cite[Proposition 6.5]{Saad00}.
\end{proof}
%%% Local Variables:
%%% mode: latex
%%% TeX-master: "main"
%%% End:


\section{Electricity}
  \subsection{Electrostatics}
  Electric charge is a property of some objects that allow such objects to feel an electric force. The units for charge are coulombs (C). The smallest amount of charge an object can have is the elementary charge: $e=1.602*10^{-19}C$. For an electron, this is -e, while for a proton it is +e. As everyone knows, like charges repel, and opposite charges attract each other.\par
  Any material we use is made of atoms, which themselves are made of charge. There are two different types of materials, insulators and conductors. For insulators, atomic/molecular electrons are stuck in place with their parent atom/molecule and their electrons are immobile. For conductors, the atomic/molecular electrons are shared among the material (these are also known as mobile electrons). Our typical insulators are wood, plastic, and printer paper, while typical conductors are metal, water, and humans.\par
  \begin{align*}
    \text{The force that charge } q_1 \text{ exerts on charge} q_2 \text{ is: (Coulombs law)}\\
    \vec{F_{1\to2}}(r_{1\to2})=\frac{1}{4\pi \epsilon_0}\frac{q_1q_2}{r_{1\to2}^2}\hat{r_{1\to2}}\\
    \text{Where } \epsilon_0=8.85*10^{-12}\frac{C^2}{Nm^2}
  \end{align*}
  \subsubsection{Example}
  Find the net force on $q_c$ due to $q_a$ and $q_b$ where $q_a+q_b$ are fixed in space. Remeber that forces are vectors.
  \begin{align*}
    \vec{F_{net_c}}&=\vec{F_{bc}}+\vec{F_{ac}}\\
    &=\frac{1}{4\pi\epsilon_0}\frac{q_bq_c}{r_{bc}^2}\hat{r_{bc}}+\frac{1}{4\pi\epsilon_0}\frac{q_bq_c}{r_{ac}^2}\hat{r_{ac}}\\
    &=\frac{1}{4\pi\epsilon_0}\frac{q_bq_c}{X_0^2}(\hat{+i})+\frac{1}{4\pi\epsilon_0}\frac{q_aq_c}{Y_0^2}(\hat{+j})\\
    &=\frac{1}{4\pi\epsilon_0}\left(\frac{q_bq_c}{x_0^2}\hat{i}+\frac{q_bq_c}{y_0^2}\hat{j}\right)\\
    \text{if } q_b=q_a \text{ and } x_0=y_0 \text{, then }\\
    \vec{F_{net_c}}&=\frac{q_aq_c}{4\pi\epsilon_0x_0^2}(\hat{i}+\hat{j})\\
  \end{align*}\
  Coulombs law works with super position:
  \begin{equation*}
    \vec{F_{1\to2}}=\frac{1}{4\pi\epsilon_0}\frac{q_1q_2}{r_{1\to2}^2}\hat{r_{1\to2}}
  \end{equation*}
  Find the force on charge $Q_A$ due to other stationary charges:
  \begin{align*}
    \vec{F_{ba}}&=\frac{1}{4\pi\epsilon_0}\frac{q_aq_b}{r_{ba}}\hat{r_{ba}}\\
    &=\frac{1}{4\pi\epsilon_0}\frac{q_aq_b}{y_0^2}\hat{j}\\
    \vec{F_{ca}}&=\frac{1}{4\pi\epsilon_0}\frac{q_aq_c}{r_{ca}^2}\hat{r_{ca}}\\
    &=\frac{q_cq_a}{4\pi\epsilon_0}\left[\frac{1}{r_{ca}^2}sin(\theta)(\hat{i})+\frac{1}{r_{ca}^2}cos(\theta)(-\hat{j})\right]\\
    &=\frac{q_cq_a}{r\pi\epsilon_0}\left[\frac{1}{x_0^2+y_0^2}\frac{x_0}{\sqrt{x_0^2+y_0^2}}\hat{i}-\frac{1}{\sqrt{x_0^2+y_0^2}}\frac{y_0}{\sqrt{x_0^2+y_0^2}}\hat{j}\right]\\
    &=\frac{q_cq_a}{4\pi\epsilon_0}\frac{1}{(x_0^2+y_0^2)^{\frac{3}{2}}}(x_0\hat{i}-y_0\hat{j})\\
    \text{Now find }f_{net_a}&=F_{ca}+F_{ba}\\
    &=(F_{ba_x}+F_{ca_x})\hat{i}+(F_{ba_y}+F_{ca_y})\hat{j}\\
    F_{net_a}&=\frac{1}{4\pi\epsilon_0}\left[\frac{q_cq_a}{(x_0^2+y_0^2)^{\frac{3}{2}}}x_0\hat{i}+\left(\frac{q_bq_a}{y_0}-\frac{q_cq_a}{(x_0^2+y_0^2)^{\frac{3}{2}}}y_o\right)\hat{j}\right]\\
  \end{align*}
  \subsection{Electric Fields}
  A field is a function that has different values throughout space that can be changed in time. Temperature is a scalar field when looking at the temperature around a room. Wind patterns are a vector field on a weather map. We will study the electric field $\vec{E}$. This field helps us find the force exerted on any charge Q.
  \begin{equation*}
    \vec{F}=Q\vec{E}
  \end{equation*}
  The field $\vec{E}$ created by a charge q is:
  \begin{equation*}
    \vec{E}=\frac{1}{4\pi\epsilon_0}\frac{q}{r^2}\hat{r}\\
  \end{equation*}
  This field acts on another charge Q such that:
  \begin{align*}
    \vec{F}&=Q\vec{E}\\
    &=Q\frac{1}{4\pi\epsilon_0}\frac{q}{r^2}\hat{r}\\
    \vec{F}&=\frac{1}{4\pi\epsilon_0}\frac{qQ}{r^2}\hat{r} \text{ (Coulombs Law)}\\
  \end{align*}
  $\vec{E}$ Fields point away from positive cherges and towards negative ones. Since $\vec{E}$ is a vector, if two or more static charges each create a field, the net field is their vector sum (superposition).
  \begin{equation*}
    \vec{E_{net}}=\sum_{i=1}^n\vec{E_i}
  \end{equation*}
  \subsubsection{Example 1}
  Two identical charges $q_1=q_2=q$ are equidistant from the origin on the x-axis. Find $\vec{E}$ anywhere on the x-axis.
  \begin{align*}
    \vec{E}&=2\left(\frac{1}{4\pi\epsilon_0}\frac{q_2}{r_2^2}cos\theta\right)\hat{k}\\
    &=2\frac{1}{4\pi\epsilon_0}\frac{2qz}{\left(z^2+\left(\frac{L}{2}\right)^2\right)^{\frac{3}{2}}}\hat{k}\\
    \vec{E}(z)&=\frac{1}{4\pi\epsilon_0}\frac{2qz}{\left(z^2+\left(\frac{L}{2}\right)^{\frac{3}{2}}\right)}\hat{K}
  \end{align*}
  What if the charges that make a field are grouped together closely? Then we will describe those charges as a continuous distribution. Linear charge density is $\lambda=\frac{dq}{dl}\to dq=\lambda dl$. Recall,
  \begin{align*}
    \vec{E}&=\frac{1}{4\pi\epsilon_0}\frac{q}{r^2}\hat{r}\\
    &=\sum_{i=1}^N\frac{1}{4\pi\epsilon_0}\frac{q_i}{r_i^2}\hat{r_i}\to \vec{E}=\frac{1}{4\pi\epsilon_0}\int\frac{dq}{r^2}\hat{r}\\
    \text{For this problem: }\\
    E&=\frac{1}{r\pi\epsilon_0}\int_{\frac{-L}{2}}^{\frac{L}{2}}\frac{\lambda dx}{z^2+x^2}\frac{z}{\sqrt{z^2+x^2}}\hat{k}\\
    &=\frac{1}{r\pi\epsilon_0}\lambda z \int_{\frac{-L}{2}}^{\frac{L}{2}}\frac{dx}{(z^2+x^2)^{\frac{3}{2}}}\hat{k}\\
    &=\frac{1}{4\pi\epsilon_0}\lambda z \left(\frac{x}{z^2\sqrt{x^2+z^2}}\right)\Big|_{\frac{-L}{2}}^{\frac{L}{2}}\hat{k}\\
    &=\frac{1}{4\pi\epsilon_0}\lambda z \left[\frac{\frac{L}{2}-\frac{-L}{2}}{z^2\sqrt{\frac{L}{2}^2+z^2}}\right]\hat{k}\\
    &=\frac{1}{4\pi\epsilon_0}2\lambda z\frac{\frac{L}{2}}{z^2\sqrt{\frac{L}{2}^2+z^2}}\hat{k}
  \end{align*}
  If the line of charge is $\inf$ in length, then the only change would be the bounds of the integral.
  \subsubsection{Example 2}
  Find $\vec{E}$ where the midpoint of a uniform line of charge as shown. The X components cancel.
  \begin{align*}
    d\vec{E}&=\frac{1}{4\pi\epsilon_0}\frac{dq}{r^2}\hat{r}\\
    \lambda&=\frac{dq}{dl}=\frac{dq}{dx}\\
    dq&=\lambda dx\\
    \text{We see that } E_x=0 \text{ while } E_z\neq 0 \text{ then, }\\
    d\vec{E}&=\frac{1}{4\pi\epsilon_0}\frac{\lambda dx}{x^2+z^2}\frac{z}{\sqrt{x^2+z^2}}\hat{k}\\
    &=\frac{1}{4\pi\epsilon_0}\frac{\lambda dxz}{(x^2+z^2)^{\frac{3}{2}}}\hat{k}\\
    E&=\int_{\frac{-L}{2}}^{\frac{L}{2}}\frac{1}{4\pi\epsilon_0}\frac{\lambda dxz}{(x^2+z^2)^{\frac{3}{2}}}\\
    &=\frac{\lambda z}{4\pi\epsilon_0}\int_{\frac{-L}{2}}^{\frac{L}{2}}(x^2+z^2)^{\frac{-3}{2}}dx\\
    \vec{E}(z)&=\frac{2\lambda z}{4\pi\epsilon_0}\frac{\frac{L}{2}}{z^2\sqrt{\frac{L}{2}^2+z^2}}\hat{k}
  \end{align*}
  \subsubsection{Example 3}
  Find $\vec{E}$ for the sitution shown:
  \begin{align*}
    d\vec{E}&=\frac{1}{4\pi\epsilon_0}\frac{dq}{r^2}\hat{r}\\
    &=\frac{1}{4\pi\epsilon_0}\frac{\lambda dx}{r^2}\left(cos\theta\hat{i}+sin\theta\hat{k}\right)\\
    &=\frac{\lambda}{4\pi\epsilon_0}\left[\int_{0}^{L}\frac{dx(x-L)}{(z^2+(x-L))^{\frac{3}{2}}}\hat{i}+\int_0^L\frac{dxz}{(z^2+(x-L))^{\frac{3}{2}}}  \right]\\
    \shortintertext{We will let x'=x-l } x=0\to x'=0-L=-L\\
    x=L\to x'=0\\
    dx'=dx-dL \text{ because } L=const\\
    &=\frac{\lambda}{4\pi\epsilon_0}\left[\int_{-L}^0\frac{dx'x'}{(z^2+x'^2)^{\frac{3}{2}}}\hat{i}+\int_{-L}^0\frac{dx'z}{(z^2+x'^2)^{\frac{3}{2}}}\hat{k}\right]\\
    \vec{E}&=\frac{\lambda}{4\pi\epsilon_0}\left[\frac{x'}{x'^2\sqrt{x^2+z^2}}\Big|_{x'=L}^{x'=0}\hat{i}+\frac{-1}{\sqrt{x^2+z^2}}\Big|_{x'=-L}^{x'=0}\hat{k}\right]
  \end{align*}
  \subsection{Electric Field Lines}
  We can draw field lines to represent how an electric field looks in space. Recall that for a point charge, $\vec{E}=\frac{1}{4\pi\epsilon_0}\frac{q}{r^2}\hat{r}$
  \newline
  Let's find $\vec{E}$ above the midpoint of two opposite charges +q and -q which are a distance d apart.
  \begin{align*}
    r^2&=z^2+\left(\frac{d}{2}\right)^2\\
    \vec{E}&=\vec{E}_-\vec{E}_+\\
    &=\frac{1}{4\pi\epsilon_0}\frac{q}{z^2+\frac{d}{2}}\left(-\frac{\frac{d}{2}}{\sqrt{z^2+\frac{d}{2}^2}}\hat{i}-\frac{z}{\sqrt{z^2+\frac{d}{2}^2}}\hat{k}\right)+\frac{1}{4\pi\epsilon_0}\frac{q}{z^2+\frac{d}{2}}\left(-\frac{\frac{d}{2}}{z^2}\hat{i}+\frac{z}{\sqrt{z^2+\frac{d}{2}^2}}\hat{k}\right)\\
    \vec{E}&=-\frac{1}{4\pi\epsilon_0}\frac{qd}{\left(z^2+\frac{d}{2}^2\right)^{\frac{3}{2}}}\hat{i}\\
    \shortintertext{Consider $\vec{p}=q\vec{d}$. This is an electric dipole moment.}\\
    \vec{E}&=-\frac{1}{r\pi\epsilon_0}\frac{\vec{p}}{\left(z^2+\frac{d}{2}^2\right)^{\frac{3}{2}}}\\
    \shortintertext{As you get further from the dipole, z$>>$d:}\\
    \vec{E}&\cong-\frac{1}{4\pi\epsilon_0}\frac{\vec{p}}{(z^2)^{\frac{3}{2}}}\\
    \shortintertext{If you get very far away: $z\to\infty$ or $\frac{d}{2}\to\infty$}\\
    \vec{E}&=\frac{-1}{4\pi\epsilon_0}\frac{qd}{\left(z^2+\frac{d}{2}\right)^{\frac{3}{2}}}\hat{i} =\frac{-1}{4\pi\epsilon_0}\frac{q\left(\frac{d}{z}\right)}{\left(1+\frac{d}{2}\right)^{\frac{3}{2}}}\hat{i}\\
  \end{align*}
  For dipoles in an external field, $\vec{p}=q\vec{d}$. The net displacement force on $\vec{p}$ is 0, but it will have torque.
  \begin{align*}
    \tau=\vec{r}\times\vec{F}=\left(\vec{\frac{d}{2}}\times\vec{F_+}\right)+\left(\vec{\frac{d}{2}}\times\vec{F_-}\right)&=\left(\vec{\frac{d}{2}}\times q\vec{E_{ext}}\right)+\left(-\vec{\frac{d}{2}}\times q\vec{E_{ext}}\right)\\
    &=2\left(\vec{\frac{d}{2}}\times q\vec{E_{ext}}\right)\\
    \vec{\tau}&=(\vec{q}d\times \vec{E_{ext}})\\
    \alignedbox{\vec{\tau}}{=\vec{P}\times\vec{E_{ext}}}\\
  \end{align*}
  \subsubsection{Example 1.}
  The field $\vec{E}$ is above the center of a uniformly charged ring is $\vec{E}=\frac{1}{r\pi\epsilon_0}\frac{Qz}{(z^2+k^2)^{\frac{3}{2}}}\hat{k}$. Now let the ring isntead, be a disk with toal charge Q that is uniformly distributed.
  \begin{align*}
    \shortintertext{Total charge Q over a total area of $\pi R^2$}\\
    \frac{Q}{\pi R^2}&=\sigma\\
    dQ&=\sigma2\pi rdr\\
    dE&=\frac{1}{4\pi\epsilon_0}\frac{dQ}{r^2}\hat{r}\\
    dQ&=\sigma 2\pi r'dr'\\
    \shortintertext{All of the x and y components vanish, which leaves us with:}\\
    \int dE&=\int\frac{1}{4\pi\epsilon_0}\frac{\sigma 2\pi r'dr'}{r'^2+z^2}\frac{z}{\sqrt{r'^2+z^2}}\hat{k}\\
    \vec{E}&=\frac{1}{4\pi\epsilon_0}2\pi\sigma z \int_0^R\frac{r'dr'}{(r'^2+z^2)^{\frac{3}{2}}}\hat{k}\\
    &=\frac{1}{4\pi\epsilon_0}2\pi\sigma z \left(-\frac{1}{\sqrt{r'^2+z^2}}\right)\Big|_{r'=0}^{r'=R}\\
    &=\frac{1}{4\pi\epsilon_0}2\pi\sigma z \left[-\frac{1}{R^2+z^2}-\frac{1}{z}\right]\hat{k}\\
    \alignedbox{\vec{E}}{=\frac{1}{4\pi\epsilon_0}\frac{2Q}{R^2}z\left(\frac{1}{z}-\frac{1}{\sqrt{R^2+z^2}}\right)\hat{k}}
  \end{align*}
  \subsection{Flux and Gauss's Law}
  Flux is the amount of flow of a material or substance through some region. Area vectors point out of the plane of the area itself. Flux is denoted by $\Phi$, and the equation for flux is:
  \begin{equation*}
    \Phi=\int\vec{f}\cdot d\vec{a}
  \end{equation*}
  \subsubsection{Example 1}
  Find the flux $\Phi$ of $\vec{E}=\epsilon_0\frac{z}{z_0}\vec{i}$ through an are $y_0+z_0$ in the y-z plane.
  \begin{align*}
    \Phi&=\int\vec{E}\cdot d\vec{a}\\
    &=\int Eda\\
    &=\int_0^{z_0}\int_0^{y_0}Eda\\
    &=\int_0^{z_0}\int_0^{y_0}\epsilon_0\frac{z}{z_0}dydz\\
    &=\epsilon_0\frac{y_0}{z_0}\int_0^{z_0}dz\\
    &=\epsilon_0\frac{y_0}{z_0}\left(\frac{1}{2}z_0^2-\frac{1}{2}0^2\right)\\
    \alignedbox{\Phi}{=\frac{1}{2}\epsilon_0y_0z_0}\\
    \shortintertext{First, a single charge Q. We must apply a Gaussian surface to the point charge:}\\
    \Phi&=\int\vec{E}\cdot d\vec{a}=\int(E\hat{r})\cdot(da\hat{r})\\
    &=\int\vec{E}\cdot d\vec{a}\\
    &=\int\frac{1}{4\pi\epsilon_0}\frac{1}{R^2}da\\
    &=\frac{1}{4\pi\epsilon_0}\int da\\
    &=\frac{q}{\epsilon_0}\\
    \intertext{More generally, we write:}\\
    \alignedbox{\Phi_E}{=\oint\vec{E}\cdot d\vec{a}=\frac{q}{\epsilon_0}}
  \end{align*}
  This brings us to Gauss's law. This law is always true but it is only useful in certain situations. 1) When $\vec{E}\cdot d\vec{a}$ is an easy dot product (they are parallel vectors). 2) When E is constant in magnatude across the surface. Then we can pull E through the integral. 3) When the total surface area is known. For example, we want the equations to be able to do the following:
  \begin{align*}
    &\oint\vec{E}\cdot d\vec{a}\\
    &\oint Eda\\
    &E\oint da /to E_a=\frac{q_{inside}}{E_0}
  \end{align*}
  \subsubsection{Example 2}
  A sphere has a uniform charge Q throughout its volume and radius, R. Find E everywhere.
  \begin{align*}
    \shortintertext{Outside:}\\
    \oint\vec{E}\cdot d\vec{a}&=\frac{q_{enc}}{\epsilon_0}\\
    \oint Eda &= \frac{Q}{\epsilon_0}\\
    E\oint da &= \frac{Q}{\epsilon_0}\\
    E4\pi r^2 &=\frac{Q}{\epsilon_0}\\
    \alignedbox{\vec{E}}{=\frac{1}{4\pi\epsilon_0}\frac{Q}{r^2}\hat{r}\text{ for }r>R}
    \shortintertext{Now for the inside:}\\
    \int dq_{enc}&=\int\rho dv\\
    q_{enc}&=\rho\int dv_{gauss}\\
    &=\rho\frac{4}{3}\pi r^3\\
    \text{Also } \rho&=\frac{Q}{\frac{4}{3}\pi R^3}\\
    E4\pi r^2&=\frac{q_{enc}}{\epsilon_0}\\
    E&=\frac{1}{4\pi r^2}\frac{1}{\epsilon_0}Q\frac{r^3}{R^3}\\
    \alignedbox{E}{=\frac{1}{4\pi\epsilon_0}\frac{Qr}{R^3}\hat{r} \text{ for }r<R}
  \end{align*}
  \subsubsection{Example 3}
  Find $\vec{E}$ everywhere for a very long line of charge with a charge density $\lambda$ (constant and uniform charge).
  \begin{align*}
    \oint\vec{E}\cdot d\vec{a}&=\frac{q_{enc}}{\epsilon_0}\\
    \int\vec{E}\cdot d\vec{a}_{curve}+\int\vec{E}\cdot d\vec{a}_{left} +\int\vec{E}\cdot d\vec{a}_{right}&=\int\vec{E}\cdot d\vec{a}_{curve}=\frac{\lambda l}{\epsilon_0}\\
    \int\vec{E}\cdot d\vec{a}_{curve}&=\frac{\lambda l}{\epsilon_0}\\
    E\int d\vec{a}_{curve}&=\frac{\lambda L}{\epsilon_0}\\
    E(2\pi rl)&=\frac{\lambda l}{\epsilon_0}\\
    \alignedbox{\vec{E}}{=\frac{\lambda}{2\pi\epsilon_0r}\hat{r}}\\
  \end{align*}
  \subsubsection{Example 4}
  A sphere has a non-uniform charge density of $\rho=\rho_0\frac{r}{R}$ for a sphere with a radius, R. Find $\vec{E}$ everywhere.
  \begin{align*}
    \int\int\int dxdydz \to \int\int\int r^2sin\theta d\theta dr d\phi\\
    \shortintertext{First, the outside: } r>R\\
    \oint\vec{E}\cdot d\vec{a}&=\frac{q_{enc}}{\epsilon_0}\\
    \oint Eda&=\frac{q_{enc}}{\epsilon_0}\\
    E\oint da&=\frac{q_{enc}}{\epsilon_0}\\
    E(4\pi r^2)=\frac{q_{enc}}{\epsilon_0}\\
    \int dq_{enc} &= \int\rho dV_{enc}\\
    q_{enc}&=\int \rho_0\frac{r}{R}dV\\
    &=\frac{\rho_0}{R}\int_0^{2\pi}\int_0^\pi\int_0^Rrr^2sin\theta dr d\theta d\phi\\
    &=\frac{\rho_0}{R}4\pi\int_0^Rr^3dr\\
    &=\rho_0\frac{1}{R}4\pi\frac{1}{4}R^4\\
    q_{enc}&=\pi\rho_0R^3\\
    E(4\pi r^2)=\frac{1}{\epsilon_0}\pi\rho_0R^3\\
    \alignedbox{\vec{E}}{=\frac{1}{4\epsilon_0}\frac{\rho_0R^3}{r^2}\hat{r}\text{ for }r>R}\\
    \shortintertext{What about for $r<R$? Becuase of the boundary, we will have to seperately find $\vec{E}$ for $r<R$:}\\
    \oint\vec{E}\cdot d\vec{a}&=\frac{q_{enc}}{\epsilon_0}\\
    \oint Eda &=\frac{q_enc}{\epsilon_0}\\
    E\oint da&=\frac{q_{enc}}{\epsilon_0}\\
    E(4\pi r^2)&=\frac{q_{enc}}{\epsilon_0}\\
    \int dq_{enc}&=\int \rho dV_{gauss}\\
    q_{enc}&=\int\rho_0\frac{r}{R}dV_{gauss}=\frac{\rho_0}{R}\int_0^{2\pi}\int_0^\pi\int_0^rrr^2sin\theta dr d\theta d\phi\\
    &=4\pi\frac{\rho_0}{R}\int_0^rr^3dr\\
    q_{enc}&=\pi\frac{\rho}{R}r^4\\
    \to \text{Gauss's law } E(4\pi r^2)&=\frac{1}{\epsilon_0}\pi\rho_0\frac{r^4}{R}\\
    E&=\frac{1}{4\epsilon_0R}\rho_0r^2\\
    \alignedbox{E}{=\frac{1}{4\epsilon_0}\rho_0\frac{r^2}{R}}\\
  \end{align*}
  The equations agree at $r=R$, therefore when greather than or equal to and less than and equal to, the answer also works as $r\to0$ or $r\to\infty$.
  \subsubsection{Example 4 (Gauss's Law with Conductors)}:
  Let's consider 2 large conductivity plates that are side by side. Lets put $\pm Q$ on both of the conductivity plates.
  \begin{align*}
    \oint\vec{E}\cdot d\vec{a}&=\frac{q_{enc}}{\epsilon_0}\\
    \int\vec{E}d\vec{a}_{right}+\int\vec{E}d\vec{a}_{left}+\int\vec{E}d\vec{a}_{top}&+\int\vec{E}d\vec{a}_{bottom}+\int\vec{E}d\vec{a}_{top}+\int\vec{E}d\vec{a}_front\\
    \int\vec{E}d\vec{a}_{right}+\int\vec{E}d\vec{a}_{left}&=\frac{q_{enc}}{\epsilon_0}\\
    E_a+E_a&=\frac{q_{enc}}{\epsilon_0} \text{ Surface charge density}\\
    2E_a&=\frac{\sigma a}{\epsilon_0}\hat{i} \text{ Between plates}\\
    \vec{E}&=\frac{\sigma}{2\epsilon_0}\hat{i} \text{ left of the plates}\\
    \shortintertext{Now for the right plate:}\\
  \end{align*}

  % Notes from 10.05.2020
  \newpage
  \subsection{Electric Potential}
  \subsubsection{Example 1}
  Find $\vec{E}$ above a disk of charge distribution that is uniform. the disk has radius (R) and total charge (Q). Let's find V then $\vec{E}$
  \begin{align*}
    dV&=\frac{1}{4\pi\epsilon_0}\frac{dq}{r}\\
    \shortintertext{The charge distribution is $\sigma=\frac{Q}{\pi R^2}$}\\
    dq&=\sigma dA\\
    &=\sigma 2\pi rdr\\
    \shortintertext{This r is not the same r as before, so we will call them r'}\\
    &=\sigma 2\pi r'dr'\\
    dV&=\frac{1}{4\pi\epsilon_0}\frac{\sigma 2\pi r'dr'}{\sqrt{z^2+r'^2}}\\
    V&=\frac{1}{4\pi\epsilon_0}2\pi\sigma\int_0^R\frac{r'dr'}{\sqrt{z^2+r'^2}}\\
    V&=\frac{1}{4\pi\epsilon_0}2\pi\sigma\sqrt{z^2+r'^2}\Big|_{r'=0}^{r'=R}\\
    \alignedbox{V}{=\frac{1}{4\pi\epsilon_0}2\pi\sigma\left[\sqrt{z^2+R^2}-z\right]}
    \shortintertext{Now let's find $\vec{E}$}\\
    E_x&=-\frac{\partial}{\partial x}V=0\\
    E_y&=\frac{-\partial}{\partial y}V=0\\
    E_z&=\frac{-\partial}{\partial z}V = -\frac{1}{4\pi\epsilon_0}2\pi\sigma\left[\frac{1}{2}(z^2+R^2)^{\frac{-1}{2}}2z-1\right]\\
    \alignedbox{\vec{E}}{=\frac{1}{4\pi\epsilon_0}2\pi\sigma\left[1-\frac{z}{\sqrt{z^2+R^2}}\hat{K}\right]}\\
  \end{align*}
  What does it mean to have potential charge? Potential doesn't have a proper value. Defining a reference point for V. $E_s = -\frac{\partial}{\partial s}V$. Consider V and $V+V_0$ where $V_0$ is a constant. We get the same $\vec{E}$ value. $E_s=-\frac{\partial}{\partial s}V=\frac{-\partial}{\partial s}(V+v_0)$. This is analogous to choosing an origin. For example, if you are going to calculate the velocity of a marker hitting the ground, you have to keep track of position so you must choose an origin. This is the same way that V is chosen when doing these equations. We need to choose where $V=0$. We choose $V=0$ at infinity. Recall that $V=\frac{1}{4\pi\epsilon_0}\frac{q}{r}$.
  \subsection{Electrostatic Potential Energy}
   Electric forces are conservative. This doesn't mean not progressive, it means that they conserve energy. We can do these calculations using energy alone, similar to gravity or $MgH=\frac{1}{2}mv^2$. The law of energy conservation is $\Delta K+\Delta U=0$. Energy is not always conserved though. One physics 103 example is friction (drag). Disipative forces are also a good example of ways energy is not fully conserved. Energy is not lost, it is just converted from translational energy to heat energy. Macroscopically, the energy is not conserved, but at a microscopic level, the energy is fully conserved. We will not consider disipative forces in this course. This means that we can use the law of energy conservation ($\Delta K+\Delta U=0$). We've said previously that the $U=QV$. Very similar to $\vec{F}=Q\vec{E}$.
  \subsubsection{Example 1}
  Charge $Q_1=6\mu C$ and $Q_2=4\mu C$ are released from rest at a distance apart of $l=10cm$. Find their final speeds $v_1$ and $v_2$. The forces of point $Q_1$ and $Q_2$ are going to exert forces away from each other. It is possible to take a force approach to this problem, but as they move apart, their acceleration changes. This makes the problem much more difficult, but you can do an energy approach because the stages in between do not matter within this approach. We will solve this using energy.
  \begin{align*}
    \Delta K+\Delta U &=0\\
    K_1-K_0+U_1-U_2&=0\\
    \shortintertext{We have to change either $Q_1$ and $Q_2$ or change $K_0$ and $K_1$ because they don't mean the same thing. We are going to change Q to be $Q_A$ and $Q_B$. Initially, their kinetic energy is zero:}
    K_1+U_1-U_0&=0\\
    \left(\frac{1}{2}m_Av_a^2+\frac{1}{2}m_bv_b^2\right)+\frac{1}{4\pi\epsilon_0}\frac{Q_AQ_B}{r_{AB}}-\frac{1}{4\pi\epsilon_0}\frac{Q_A}{l}&=0\\
    \shortintertext{Interaction energy is a better term than potential energy. Potential energy goes in pairs. It is how two charges interact, not how a single charge exists. No matter how far apart they are, they will exert a force on each other. $\frac{Q_A}{Q_B}\to0$ because $r_{AB}\to 0$}\\
    \frac{1}{2}m_Av_A^2+\frac{1}{2}m_Bv_B^2&=\frac{1}{4\pi\epsilon_0}\frac{Q_AQ_B}{l}\\
    \shortintertext{Let $M_A=M_B=15g$}\\
    \shortintertext{In this system, $\vec{F_{AB}}=-\vec{F_{AB}}$ Also there are no external forces acting so,}\\
    \vec{F}_{net}&=\vec{F}_{AB}+\vec{F}_{BA}=0=\frac{d}{dt}\vec{P}\\
    \Delta P_{01}&=0=P_1-P_0\\
    0&=m_Av_A+m_Bv_B\\
    \to v_A^2&=V_B^2\frac{m_B^2}{m_A^2}\\
    \to \frac{1}{2}m_A\left[v_B^2\frac{m_B^2}{m_A^2}\right]+\frac{1}{2}m_Bv_B^2&=\frac{1}{4\pi\epsilon_0}\frac{Q_AQ_B}{l}\\
    \frac{1}{2}v_B^2\left[\frac{m_B^2}{m_A}+m_B\right]&=\frac{1}{4\pi\epsilon_0}\frac{Q_AQ_B}{l}\\
    m_bv_B^2&=\frac{1}{4\pi\epsilon_0}\frac{Q_AQ_B}{l}\\
    \alignedbox{v_b}{=\sqrt{\frac{1}{m_b}\frac{1}{4\pi\epsilon_0}\frac{Q_AQ_B}{l}}}\\
    v_a&=-v_B\frac{m_B}{m_A}\\
    \alignedbox{v_A}{=-v_B}\\
  \end{align*}
  \newpage


\documentclass{article}

\usepackage{amsmath}
\usepackage{numprint}

\author{Daniel Fernandes Martins (danielfmt)}
\title{Question \#3 Solution}

\begin{document}

\maketitle

\section{Conditional Probability}

\subsection{Calculating $P(B)$}

First we need to calculate the probability of picking a black ball on the
first try:

\begin{equation*}
P(B) = \frac{1}{2} \cdot 1 + \left( \frac{1}{2} \right)^2 = \frac{3}{4}
\end{equation*}

\subsection{Calculating $P(A|B)$}

For the second ball to be also black, then we must have picked the first bag
(bag $A$):

\begin{equation*}
P(A|B) = P(A \cap B) \div P(B) = \left( \frac{1}{2} \cdot 1 \right) \div \frac{3}{4} = \frac{2}{3}
\end{equation*}

\end{document}


\documentclass[11pt,twoside]{article}

\usepackage[headings]{fullpage}
\usepackage[utopia]{mathdesign}

\pagestyle{myheadings}
\markboth{Be rational}{Be rational}

\input{../../fncextra}

\begin{document}

\begin{center}
    \bf Let's be rational about this
\end{center}

Our go-to form of interpolating function is the polynomial,
\begin{equation}
 p(x) = c_0 + c_1 x + c_2 x^2 + \cdots + c_{n-1} x^{n-1}.
\end{equation}
When the monomials are evaluated at $m$ nodes, we get the Vandermonde matrix
\begin{equation}
\mV =
 \begin{bmatrix}
      1 & t_0 & t_0^{2} & \cdots & t_0^{m-1} \\[1mm]
      1 & t_1 & t_1^{2} & \cdots & t_1^{m-1}   \\[1mm]
      1 & t_2 & t_2^{2} & \cdots & t_2^{m-1} \\[1mm]
      \vdots & \vdots & \vdots  &  & \vdots \\
      1 & t_{m-1} & t_{m-1}^{2} & \cdots & t_{m-1}^{m-1}
 \end{bmatrix} .
\end{equation}
Given a function $f$, setting $y_i=f(t_i)$ and solving $\mV \bfc = \bfy$ produces the coefficients of the interpolating polynomial.

Polynomials can, in principle, converge pointwise to any continuous function. However, they are not equally efficient at doing so for all functions. One way to obtain faster convergence in some cases is to turn to a different category of functions. An interesting choice are the \textbf{rational functions},
\begin{equation}
\label{ratdef}
 r(x) = \frac{a_0 + a_1 x + \cdots + a_{n-1} x^{n-1}}{b_0 + b_1 x + \cdots + b_{n-1} x^{n-1}+ b_nx^n}.
\end{equation}
Because they can have zeros in the denominator, rational functions are often superior for approximating functions that blow up or have steep gradients.

Observe that in~\eqref{ratdef}, multiplying all of the coefficients by the same constant leaves $r$ unchanged. Thus we make a normalization, $b_n=1$. This leaves a total of $n+n=2n$ coefficients to be determined in $r$, so we require $2n$ interpolation nodes. From~\eqref{ratdef}, setting $r(t_i)=f(t_i)=y_i$, for $i=0,\ldots,2n-1$ and clearing the denominator, we eventually obtain
\begin{equation}
\label{eq:sys1}
\m{W} \bfa - \m{Y} \m{W} \bfb = \m{Y}
\begin{bmatrix}
 t_0^n \\ t_1^n \\ \vdots \\ t_{2n-1}^n
\end{bmatrix},
\end{equation}
where $\m{W}$ is a $2n\times n$ Vandermonde-style matrix,
$\m{Y}=\diag(y_0,\ldots,y_{2n-1})$, and $\bfa$ and $\bfb$ collect the polynomial coefficients in the numerator and denominator respectively. This equation is actually a square linear system,
\begin{equation}
  \label{eq:sys2}
\begin{bmatrix}
  \m{W} & - \m{Y} \m{W}
\end{bmatrix}
\begin{bmatrix}
\bfa \\ \bfb
\end{bmatrix}
= \m{Y}
\begin{bmatrix}
t_0^n \\ t_1^n \\ \vdots \\ t_{2n-1}^n
\end{bmatrix},
\end{equation}
easily solved for the vector $\bfc = \bigl[ \bfa\,; \, \bfb \bigr]$.

\subsection*{Goals}

You will compute a rational interpolant and compare it to a polynomial interpolant for the same points.

\subsection*{Preparation}

Read section 9.1 and answer the following questions.

\begin{enumerate}
    \item Derive~\eqref{eq:sys1}.
    \item Write out the linear system~\eqref{eq:sys2} for the rational interpolant to the four points $(-1,0)$, $(0,-1)$, $(1,1)$, $(2,1)$.
\end{enumerate}

\subsection*{Procedure}

Download templates for the script and for the function \texttt{ratinterp.m}.

\begin{enumerate}
    \item Complete the function \texttt{ratinterp} that computes a rational interpolant to given data using the algorithm outlined above.
    \item Define the function $g(x) = \tanh(10x) + 2x^2$ and plot it over the interval $[-1,1]$.
    \item To your plot add the polynomial interpolant using $m=18$ equally spaced nodes in $[1,1]$. (It will not be a good result.)
    \item Find the rational interpolant with $n=9$ and the same nodes. Start a new plot and plot the error $g(x)-r(x)$ over $[-1,1]$. It should be fairly small over the whole interval.
\end{enumerate}

\subsection*{Discussion}

Returning to the $n=9$ rational interpolant of $g$, find the poles of $r$ (i.e., the roots of the denominator). Several of them are conjugate pairs that lie essentially on the imaginary axis of the complex plane. Referring to a property of $g$, explain the two poles closest to the origin. You may want to prove and then use the identity $\cosh(x)=\cos(ix)$.

\end{document}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:


\chapter{Frames}

(Wikipedia says frame (ordered basis) for what we say basis. Should we follow?)

\section{A frame is an origin point and a basis}

With a basis, we can describe every vector with tuples.

With a frame, we can describe every point with tuples.

A frame \(S\) is an origin point \( O \) and a basis \( E \).

We can describe a point \( P \) as \( O + OP \).
We then state \(OP\) as a linear combination of the vectors in \(E\).
Suppose that the linear combination is \(OP = x_1 e_1 + \ldots + x_n e_n\).
We call the tuple \( (x_1,\ldots,x_n) \) the \emph{\(S\)-coordinates} of \(P\).

\emph{Coordinate system} is another name for \emph{frame}.

\footnote{\url{https://en.wikipedia.org/wiki/Frame\#Mathematics}}

\section{Some coordinate systems}

A \emph{coordinate system} maps a coordinate tuple to a vector.

\subsection{The rectangular coordinate system}

Orthonormal basis.

\(R(x,y) = x e_1 + y e_2\).

\(R(x) = x_1 e_1 + x_2 e_2\).

In this system, the coordinates are the scalar coefficients in the linear combination of basis vectors.
The coordinates describe how the basis vectors should be linearly combined to form the described vector.

Let \(T : V^2 \to V^2\) be a linear transformation.
Then \(T(R(x)) = T(x_1 e_1 + x_2 e_2) = x_1 \cdot T(e_1) + x_2 \cdot T(e_2) = x_1 e_1' + x_2 e_2' = R'(x) \).

\subsection{The polar coordinate system}

\(P(r,t) = r e_1 \text{ rotated } t \text{ radians counterclockwise}\).

\section{Coordinate system transformations}

\subsection{Converting polar coordinate tuples to rectangular coordinate tuples}

Both the rectangular coordinate $(r\cos\theta, r\sin\theta)$ and the polar coordinate $(r,\theta)$
describe the same point in two-dimensional Euclidean space.
\[
R(r\cos\theta, r\sin\theta) = P(r,\theta)
\]

A point in a space can have different coordinates in different coordinate systems.

\section{Using coordinate systems to apply analysis to geometry}

Coordinate systems bridge synthetic geometry and analytic geometry.

A coordinate system transformation is a function taking a coordinate system and giving another coordinate system.

\section{Describing vectors with numbers without drawing}

Earlier in \ChapterRef{chp:vector},
we defined a vector as something telling us
how to go from an origin point to a destination point.

We can define three directions: right, forward, and up.
We can describe \emph{any} point in space by saying that the point is
\(x\) meters right, \(y\) meters forward, and \(z\) meters up from where we are standing.
We have just described a \emph{coordinate system}:
a mapping between a \emph{coordinate tuple} and a point in space.

A tuple and a coordinate system can represent a vector.

\( v = x_1 e_1 + x_2 e_2 \).

Describing a vector using bases:

\[
v = \sum_k x_k e_k
\]
where each \(x_k:\Real\) and each \(e_k:\Real^n\).

For example:

Let \( e_1 \) be a unit vector pointing right.

Let \( e_2 \) be a unit vector pointing up.

Then, \( 1 e_1 + 2 e_2 \) is a vector.

"The numbers in the list depend on the choice of coordinate system."%
\footnote{\url{https://en.wikipedia.org/wiki/Covariance_and_contravariance_of_vectors}}

\subsection{Basis vectors and coordinate axes}

An orthonormal basis makes good coordinate axes.%
\footnote{\url{https://en.wikipedia.org/wiki/Orthonormal_basis}}

The same point has different coordinates in different frames.

\section{Coordinate systems}

We can describe a point by a \emph{coordinate system} and a \emph{coordinate tuple}.

\index{definitions!coordinate}%
A \emph{coordinate} is a number.

\index{definitions!coordinate tuple}%
A \emph{coordinate tuple} is a tuple of \emph{coordinates}.
Example: \((1,2)\).

\index{definitions!coordinate system}%
A \emph{coordinate system} maps a coordinate tuple to a point in a space.

\index{definitions!\(n\)-dimensional real coordinate space}%
The \emph{n-dimensional real coordinate space} \( \Real^n \)
is the set of all \(n\)-tuples where every component is a real number.
It can \emph{represent} the \(n\)-dimensional Euclidean space.

The \(n\)-dimensional Cartesian coordinate system uses \(n\) basis vectors
to map \( \Real^n \) to \(n\)-dimensional Euclidean space.

\subsection{Drawing the standard two-dimensional Cartesian coordinate axes}

\subsection{Transforming the vector}

Also known as \emph{active transformation}.
This changes the vector.

We write \(v = E(x)\) to mean that \emph{the vector \(v\) is described by the coordinate tuple \(x\) under coordinate system \(E\)}.

Let \(v = E(x)\).
The vector \(v\) is described by the tuple \(x\) under coordinate system \(E\).

If we transform the vector to \(T(v)\),
then we will also transform the coordinate tuple to \(T(E(x))\),
which we can also write as \((T \circ E)(x)\) using function composition notation.

Thus, if \(v = E(x)\), then \( T(v) = (T \circ E)(x) \).

Therefore, transforming the vector by \(T\) has the same effect as
transforming the coordinate system (not the coordinate tuple) by \(T\).

\subsection{Transforming the basis}

\emph{Passive transformation}.
Changing the basis while still referring to the same point.
This does not change the vector.

Every vector can be stated as a linear combination of the basis vectors.

\index{definitions!coordinate transformation}%
A \emph{coordinate transformation} is a map from a coordinate system to a coordinate system.

There are many points in an Euclidean space, but none is special.
However, we can pick a point in Euclidean space, and call it \( O \), short for \emph{origin}.

We can describe the same vector using different coordinate tuples in different coordinate systems.
For example, the same vector \(v\) has coordinate tuples \(x\) in coordinate system \(E\)
and has coordinate tuples \(x'\) in coordinate system \(E'\).
\[
    v = E(x) = E'(x')
\]

\footnote{\url{https://en.wikipedia.org/wiki/Active_and_passive_transformation}}

Let \(T\) be a linear transformation.

If \(E'\) happens to be \(T \circ E\), then

\begin{align*}
    E(x) &= (T \circ E)(x') & \text{assumed}
    \\ E(x) &= T(E(x')) & \text{by definition of composition}
    \\ T^{-1}(E(x)) &= T^{-1}(T(E(x'))) & \text{applying \(T^{-1}\) to both sides}
    \\ (T^{-1}\circ E)(x) &= E(x')
\end{align*}

\subsection{Changing basis}

A basis transformation is a coordinate system transformation.
Such transformation has the type
$(\mathbb{R}^n \to \mathbb{R}^n) \to (\mathbb{R}^n \to \mathbb{R}^n)$.
Such basis transformation $E' = T~E$ can be written out
as $\vec{e}'_k = T_k~(\vec{e}_1, \ldots, \vec{e}_n)$,
which can be written out even more in the greatest detail as
\[
(\vec{e}'_k)_i = (T_k)_i~(\vec{e}_1, \ldots, \vec{e}_n)
\]
which are actually $n^2$ equations,
and each $(T_k)_i$ is a function that takes $n^2$ real numbers.
If the transformation is linear,
we can write the transformation as matrix multiplication:
\[
E' = ET
\]
which expands into $n$ equations each:
\[
\vec{e}_i' = \sum_{k=1}^n T_{ik} \vec{e}_k
\]
which, in turn, expands into $n$ equations again each:
\[
e_{ij}' = \sum_{k=1}^n T_{ik} \cdot e_{kj}
\]

Sometimes we want to use another basis to locate the same point.
That is, if we have a vector $x$ in basis $E$, and a vector $y$ in basis $TE$,
we want $Ex = TEy$.
We want to change the basis from $E$ to $TE$,
so old coordinate $x$ become $y$,
but we want $y$ in $TE$ such that $TEy$ is still $Ex$,
which we can state mathematically:
\begin{align*}
TEy &= Ex
\\ T^{-1}TEy &= T^{-1}Ex
\\ Ey &= T^{-1}Ex
\end{align*}
which says that the change of coordinate goes against the change of basis,
so we say that the vector $x$ is *contravariant* to the basis transformation $T$.
This means that if we move the origin of the coordinate system 3 units to the right,
then we will have to move the coordinates 3 units to the left
if we want the new coordinate to locate the same point.

What about covectors?
Covector $f$.
\[
f(TEy) = f(Ex)
\]
Due to the linearity of covectors:
\begin{align*}
f(Ex) &= f\left(\sum_i x_i \vec{e}_i\right) = \sum_i f(x_i \vec{e}_i)
\\ f(TEy) &= f\left(\sum_i y_i T\vec{e}_i \right) = \sum_i f(y_i T\vec{e}_i)
\\ \vec{e}'_k = \sum_i T_{ki} \vec{e}_i
\end{align*}

(todo show that covector is covariant)

\section{Coordinate transformation}

Let's say we have one space $S$,
and two coordinate systems $J : M \to S$ and $K : N \to S$.
A coordinate transformation from $J$ to $K$ (we name this transformation $T : M \to N$)
transforms a $J$-coordinate
to an $K$-coordinate describing the same point.
This transformation relates both coordinate systems as
$J~x = K~(T~x)$, which means that if $x$ is a coordinate in $J$, then $T~x$ is a coordinate in $K$ locating the same point.
We can also write the equation as $J = K \circ T$.

To consider each component of the coordinate separately,
we write
$T~(x_1,\ldots,x_m) = (y_1,\ldots,y_n)$
where
\begin{align*}
y_1 &= t_1~(x_1,\ldots,x_m)
\\ &\vdots
\\ y_n &= t_n~(x_1,\ldots,x_m).
\end{align*}
Thus we can think of $T$ as an $n$-tuple $(t_1,\ldots,t_n)$
whose each component $t_k$ takes an $m$-tuple.
We can extend the definition of function application so that it works on tuples:
\[
(t_1,\ldots,t_n)~\vec{x} = (t_1~\vec{x}, \ldots, t_n~\vec{x})
\]
but why would we?

If we define that, we can see how a change in a tuple component translates
to a change in the other tuple component.
\[
\frac{\partial y_i}{\partial x_j} = pd~j~t_i
\]

A space mapping maps the underlying space $M : R \to S$.

The transformation $T~x = 2\cdot x$ can be viewed as two things:
as a space transformation, it makes everything bigger;
as a coordinate transformation, it makes everything smaller.
Coordinate transformation works in reverse.
You can move the point, or you can change the coordinate.

Covariance.

Distance-preserving transformation.
$d~(T~x)~(T~y) = d~x~y$.

$T$-symmetry.
$f~(T~x) = T~(f~x)$?

Inverse transformation.
Composition of transformations.

We can use several coordinate systems on the same space.
Some coordinate systems are more convenient to work with.
To specify a place on Earth, we can use the
geographic coordinate system (latitudes and longitudes).
% https://en.wikipedia.org/wiki/Geographic_coordinate_system
A coordinate transformation does not move the point.

If a coordinate system is a bijection,
then it describes an isomorphism between
its coordinate space and the space it describes.
This means we can pick any of them we find most convenient,
and whatever works with it will work with the other.

Let's say we have two spaces $R$ and $S$.
$T : R \to S$.

Embedding and projection are mappings between spaces.
Embedding maps a lower-dimensional space to a higher-dimensional space;
projection maps a higher-dimensional space to a lower-dimensional space.

\subsection{Picking a frame: The standard basis vectors}

The standard basis vectors of $\mathbb{R}^3$
is $\{e_1,e_2,e_3\}$
where $e_1 = (1,0,0)$, $e_2 = (0,1,0)$, and $e_3 = (0,0,1)$,
which can also be written as the matrix
\[
    \Matrix{ e_1 & e_2 & e_3 }
    = \Matrix{ 1&0&0\\0&1&0\\0&0&1 }
\]
where each basis vector becomes a column in the matrix.

A coordinate system transformation multiplies the basis with the transformation matrix.

\subsection{Curvilinear coordinate system: polar coordinate system}

\section{Understanding a covector as a linear function}

Every linear endofunction of an $n$-dimensional vector space
can be written as a vector of $n$ of inner products.

\section{Understanding how cobasis is to covector as basis is to vector}

We can derive a basis for $V^*$ from a basis for $V$.
Let us write the basis for $V$ as \(E = \{ e_1, \ldots, e_n\}\).
We then define $G = \{ g_1, \ldots, g_n \}$ where \(g_k(x) = x \cdot e_k\).
If such $G$ also spans the entire $V^*$,
then $G$ is a basis for $V^*$,
and we call such $G$ the dual basis of $E$.

\section{Multiplying a covector and a vector}

The product between a covector $f : V^*$ and vector $\vec{x} : V$
is simply $f~\vec{x}$ (function application)?

\section{Eigenvalues, eigenvectors, and fixpoints of transformations? Why are we talking about this?}

We say that a point is a fixpoint (invariant)
of a transformation iff the transformation maps it to itself.
$f(x) = x$.
We can also say that $f$ is a fixtransform of $x$.

We can embed a plane on a sphere.

To describe an embedding,
we can use words, or we can use algebra:
we pick a coordinate system for each space,
and describe the embedding of the coordinates.
An embedding $E : R \to S$ is a mapping from a space $R$ to (a subset of) another space $S$.
Let $J : M \to R$ and $K : N \to S$ be coordinate systems for those spaces, respectively.
Let $T : M \to N$ be the coordinate transformation that corresponds to that embedding.
Let $x$ be a $J$-coordinate.
Then we have:
\[
K~(T~x) = E~(J~x)
\]
which can also be written
\[
K \circ T = E \circ J.
\]
Spherical coordinate system
use three: radius,: $(r,\theta,\phi)$. Here the radius is fixed so we can use $(\theta,\phi)$.

\section{Moving points around}

\subsection{Translating a point}

We can translate a point \(P\) by a vector \(v\).
The result is the point \(P + v\).

\subsection{Rotating a vector}

Rotation is always done with respect to the origin.
If you need otherwise, then translate, rotate, and inverse-translate.

Two-dimensional rotation is simple because the axis of rotation is a point.
Three-dimensional rotation is more complex because the axis of rotation is a line.

Let \(x\) be the vector that we want to rotate.

Let \(a\) be the axis of rotation.

Let \(n\) be orthogonal to \(a\).
Let \(a + n = x\).
Thus, \(a\), \(x\), and \(n\) form a right triangle, with \(x\) as the hypothenuse.

Let \(\theta\) be the angle of rotation.

Let \(A(x,y)\) be the angle from \(x\) to \(y\).

The result of rotating a vector \(x\) about the axis \(a\) by angle \(\theta\) is the vector \(y\).

\(\norm{x} = \norm{y}\)

\(A(a,x) = A(a,y)\).

\(A(n,x) + \theta \equiv A(n,y)\).

\subsection{Applying a rotation matrix to a vector}

This matrix%
\footnote{\url{https://en.wikipedia.org/wiki/Rotation_matrix}}
describes
two-dimensional rotation by a counterclockwise angle of \(\theta\):
\Formula{
    \Matrix{
        \cos\theta & -\sin\theta
        \\ \sin\theta & \cos\theta
    }.
}
Let that matrix be \(R\).
Let \(v\) be a vector.
Then \(Rv\) is \(v\) rotated by \(\theta\).

\section{Moving frames around without moving points}

\subsection{Locating the same point with different coordinate systems}

Example of coordinate transformation:
The same point in the same two-dimensional Euclidean space
is described by
both the polar coordinates \( (r,\theta) \)
and the rectangular coordinates \( (r \cos \theta, r \sin \theta) \).
The transformation is \( (r,\theta) \to (r \cos \theta, r \sin \theta) \).

% https://en.wikipedia.org/wiki/Real_coordinate_space

A \emph{coordinate system} $M : C \to S$ is a surjective mapping from
\emph{coordinate space} $C$ to \emph{target space} $S$.

A \emph{coordinate} is a point in \(C\).
The coordinate system tells us how to get to a point.

The \(n\)-dimensional real coordinate space is $\mathbb{R}^n$.
% https://en.wikipedia.org/wiki/Real_coordinate_space
It is also called the real $n$-space.
A point in the real $n$-space is an $n$-tuple of real numbers $(x_1,\ldots,x_n)$.

$(x,y)$ is the tuple of coordinates,
$x$ is the x-coordinate, and $y$ is the y-coordinate.

Coordinate systems unify geometry and
% https://en.wikipedia.org/wiki/Mathematical_analysis
mathematical analysis.
With coordinates,
we can solve geometric problems by
numbers, calculus, and algebra,
so that computers can
find the intersection of geometric objects
by solving the corresponding system of equations,
and find the size of a geometric object by solving the corresponding integral.

\section{Fields}

A field is a function that assigns something to each point in space.

A vector field is a function \( f : P \to V \).

We need two bases to coordinatefully describe a vector field.
One basis to turn \( P \) to \(\Real^m\).
Another basis to turn \(V\) to \(\Real^n\).

The coordinateful description of a field \(f\) under basis \(?,?\) is \(f_? : \Real^m \to \Real^n\).

A force field assigns a force vector to each point in space.

A scalar field assigns a scalar to each point in space.
An example of a scalar field is a temperature field.%
\footnote{\url{https://en.wikipedia.org/wiki/Temperature_gradient}}

Let \(T(P)\) be the temperature at point \(P\).
Because \(P = O + \sum_k x_k e_k\), we have \(T(O + \sum_k x_k e_k)\).

Mathematics and physics use the same term \enquote{field} to mean different things.

\footnote{\url{https://en.wikipedia.org/wiki/Field_(physics)}}


%================================
\section{Separation Axioms. From $T_0$ to Hausdorff}
%================================

% links

% https://en.wikipedia.org/wiki/Separation_axiom

% https://en.wikipedia.org/wiki/Separated_sets

% https://proofwiki.org/wiki/Definition:Tychonoff_Separation_Axioms

%--------------------------------
\begin{definition}
	[saperated]
	\label{def: seperated}
	In a topological space, two sets are said to be \textit{separated} iff each is disjoint from other's closure.
\end{definition}
%--------------------------------

%--------------------------------
\begin{definition}
	[separated by neighbourhoods]
	\label{def: separated by neighbourhoods}
	In a topological space $(X, \mathcal T)$, two sets $A$ and $B$ are said to be \textit{separated by neighbourhood} iff there are neighbourhoods $N_A$ of $A$ and $N_B$ of $B$ such that $N_A$ and $N_B$ are disjoint.
\end{definition}
%--------------------------------

%--------------------------------
\begin{definition}
	[topologically indistinguishable]
	\label{def: topologically indistinguishable}
	Let $(X, \mathcal T)$ be a topological space. Two points $x,y \in X$ are said to be \textit{topologically indistinguishable} iff they share all their neighbourhoods. That is, let $\mathcal N_x$ be the family of all neighbourhoods of $x$ and let $\mathcal N_y$ be the family of all neighbourhoods of $y$, we have
	$$
	\mathcal N_x = \mathcal N_y.
	$$

	Respectively, $x,y$ are said to be \textit{topologically distinguishable} iff they are not topologically distinguishable; i.e.,
	$$
	\mathcal N_x \ne \mathcal N_y.
	$$
\end{definition}
%--------------------------------

%--------------------------------
\begin{example}
	In an indiscrete topological space, all distinct points are topologically indistinguishable.
\end{example}
%--------------------------------

\centering{
	\subsection*{$T_0$ Spaces}
}

%--------------------------------
\begin{definition}
	[$T_0$ spaces]
	\label{def: T_1 spaces}
	A topological space $(X, \mathcal T)$ is said to be $T_0$ or \textit{Kolmogorov}, iff all distinct points $x,y \in X$ are topologically distinguishable.
\end{definition}
%--------------------------------

%--------------------------------
\begin{example}
	Let $X$ be any set and let $\mathcal T$ be the indiscrete topology on $X$. $(X, \mathcal T)$ is $T_0$ iff $|X| \in \{0, 1\}$.
\end{example}
%--------------------------------

\centering{
	\subsection*{$T_1$ Spaces}
}

%--------------------------------
\begin{definition}
	[$R_0$ spaces]
	\label{def: R_0 spaces}
	A topological space $(X, \mathcal T)$ is said to be $R_0$ iff any two topologically distinguishable points in $X$ are separated.
\end{definition}
%--------------------------------

%--------------------------------
\begin{definition}
	[$T_1$ Spaces]
	\label{def: T_1 spaces}
	A topological space $(X, \mathcal T)$ is said to be $T_1$ or \textit{Fr\'echet} iff it is $T_0$ and $R_0$; i.e., all distinct pionts $x,y \in X$ are separated.
\end{definition}
%-------------------------------

%--------------------------------
\begin{example}
	[$R_0$ but not $T_0$]
	Let $\mathcal T$ be a countable family of disjoint proper intervals on $\mathbb R^n$, and $\bigcup \mathcal T = \mathbb R^n$. $(X, \mathcal T)$ is $R_0$, but not $T_0$.
\end{example}
%--------------------------------

%--------------------------------
\begin{example}
	[$T_0$ but not $R_0$]
	\label{eg: T_0 but not T_1}
	Let $(\mathbb R_{\ge 0}, \mathcal T)$ be a topological space with
	$$
	\mathcal T = \left\{ U \subseteq \mathbb R : \forall i \in \mathbb R_{\ge 0}, \ U_i = [0, i)  \right\},
	$$
	Then for all $x,y \in (\mathbb R_{\ge 0}, \mathcal T)$, if $x \ne y$, then there are $|y - x|$ neighbourhoods $N_x$ of $x$ do not contain $y$. Thus, it is $T_0$.

	On the other hand, it is not $R_0$, because for all $x, y \in (\mathbb R_{\ge 0}, \mathcal T)$ with $x < y$, $x \in \overline{\{y\}} = [0, y]$.
\end{example}
%--------------------------------

%--------------------------------
\begin{example}
	[$R_0$ but not $T_1$]
	\label{eg: R_0 but not T_1}

	Let $X$ be any set with $|X| \ge 3$, let $U \subsetneq X$ with $|U| \ge 2$, let $\mathcal T_{X \setminus U}$ be a $T_1$ topology on $X \setminus U$, and let $\mathcal T$
	$$
	\mathcal T = \mathcal T_{X \setminus U} \cup \{X, U\}.
	$$

	For all $x,y \in X$, if $x \ne y$, then they are separated. Thus, the space is $R_0$.

	But $(X, \mathcal T)$ is not $T_1$, because all $\{u\} \in U$ share the same closure which is $U$ itself.
\end{example}
%--------------------------------

%--------------------------------
\begin{corollary}
	[alternative definitions of $R_0$ spaces]
	\label{prop: alternative definitions of R_0 spaces}
	Let $(X, \mathcal T)$ be $R_0$, then the following conditions are equivalent.
	\begin{enumerate}[(i)]
		\item
		The closure of all singletons in $X$ are not $T_0$ subspace.

		\item
		For any two points $x,y \in X$, $x \in \overline{ \{y\} }$ iff $y \in \overline{ \{x\} }$.

		\item
		Every open set is the union of closed sets.
	\end{enumerate}

	\begin{proof}
		\
		\begin{enumerate}[(i)]
			\item
			By Definition \ref{def: R_0 spaces}, if $y$ and $x$ are topologically distinguishable, by Definition \ref{def: R_0 spaces}, $x$ and $y$ are separated; i.e., $x \notin \overline {\{y\}}$ and $y \notin \overline{\{x\}}$.

			\item
			By Definition \ref{def: R_0 spaces}, for all $x,y \in X$, $x,y$ are not separated only if they are topologically indistinguishable. By Definition \ref{def: topologically indistinguishable}, they share all their neighbourhoods, thus they have the same closure; i.e., $\overline{\{x\}} = \overline{\{y\}}$.

			\item
			For any $U \in \mathcal T$,
			$$
			U = \bigcup_{x \in U} \{x\}.
			$$
			If $(X, \mathcal T)$ is $T_1$, then we are done. Suppose $(X, \mathcal T)$ is not $T_1$, then there exists $A \in \mathcal T$ with $|A| > 1$, and for all $B \subsetneq A$, $B \notin \mathcal T$ (proof omitted). For such $A$, $X \setminus A$ is open, for $X \setminus A = \bigcup (\mathcal T \setminus \{A\})$, thus $A$ is also closed.

			Suppose for any such $A$ with $A \cap U \ne \emptyset$, $A \subseteq U$. Suppose it fails, i.e., $A \cap U \ne A$, then we have $A \cap U \subsetneq A$ and $A \cap U \in \mathcal T$, which is contradicted to the condition of $A$. Now we have
			$$
			U = \bigcup \mathcal A \cup \bigcup_{x \in I} \{x\}
			$$
			where $\mathcal A$ is the family of such $A$, and $I$ is the union of all closed singletons in $U$. Thus $U$ is open.
		\end{enumerate}
	\end{proof}
\end{corollary}
%--------------------------------

%--------------------------------
\begin{corollary}
	[alternative definitions of $T_1$ spaces]
	\label{prop: alternative definitions of T_1 spaces}
	Let $(X, \mathcal T)$ be $T_1$, then the following conditions are equivalent.
	\begin{enumerate}[(i)]
		\item All singletons in $X$ are closed.
		\item Every subset of $X$ is the intersection of all open sets containing it.
		\item Every cofinite subset of $X$ is open.
	\end{enumerate}

	\begin{proof} \
		\begin{enumerate}[(i)]
			\item
			Suppose there exists $\{x\} \subseteq X$ with $\overline{\{x\}} \ne \{x\}$, then there exists $y \in \overline{\{x\}}$ with $x \ne y$. By Definition \ref{def: T_1 spaces}, this is impossible.

			\item
			For any $A \subseteq X$,
			$$
			A = \bigcup_{x \in A} \{x\}.
			$$
			Let $B = X \setminus A$. By De Morgan's law,
			$$
			B = \bigcap_{x \in A} X \setminus \{x\}.
			$$
			$(X, \mathcal T)$ is $T_1$ iff all $\{x\}$ are closed, in which case, $B$ is the intersection of all open sets $X \setminus \{x\} \supseteq B$.

			\item
			Let $A$ be a cofinite subset of $X$. $X \setminus A$ is a finite union of singletons. As $(X, \mathcal T)$ is $T_1$, any singletons in $X$ is closed. By Proposition \ref{prop: dark side of topology}, $X \setminus A$ is closed. By Definition \ref{def: closed sets}, $A$ is open.�
		\end{enumerate}
	\end{proof}
\end{corollary}
%--------------------------------

\centering{
	\subsection*{Hausdorff Spaces}
}

%--------------------------------
\begin{definition}
	[$R_1$ spaces]
	\label{def: R_1 spaces}
	A topological space $(X, \mathcal T)$ is said to be $R_1$ iff any two topological distinguishable points in $X$ are separated by neighbourhoods.
\end{definition}
%--------------------------------

%--------------------------------
\begin{definition}
	[Hausdorff Spaces]
	\label{def: Hausdorff spaces}
	A topological space $(X, \mathcal T)$ is said to be \textit{Hausdorff} or $T_2$ iff it is $T_0$ and $R_1$; i.e., all distinct points $x, y \in X$ are separated by neighbourhoods.
\end{definition}
%--------------------------------

%--------------------------------
\begin{theorem}
	All metrizable spaces are Hausdorff

	\begin{proof}
		Let $(X, \mathcal T)$ be a metrizable space. There exists a metric $\rho$ on $X$ that induces $\mathcal T$. Given distinct points $x,y \in X$, suppose for all $\varepsilon \in \mathbb R_{> 0}$, there exists $z \in B(x, \varepsilon) \cap B(y, \varepsilon)$. Then $\rho(x, z) < \varepsilon$ and $\rho(y, z) < \varepsilon$. Now we have
		$$
		\rho(x, z) + \rho(y, z) < 2\varepsilon.
		$$

		Put $\rho(x,y) > 2\varepsilon$ as $x$ and $y$ are arbitrarily given. Then we have
		$$
		\rho(x, z) + \rho(y,z) < \rho(x,y),
		$$
		which implies that $\rho$ is not a metric on $X$. Hence, $(X, \mathcal T)$ is not metrizable which is contradicted to the condition.
	\end{proof}
\end{theorem}
%--------------------------------

%--------------------------------
\begin{theorem}
	\label{prop: T2 implies T1}
	All singletons in a Hausdorff space are closed.

	\begin{proof}
		Let $(X, \mathcal T)$ be a Hausdorff space, and let $x \in X$. For all $y \in X$ with $x \ne y$, there is a open neighbourhood $U_y$ of $y$ such that $x \notin U_y$. Then, for all such $U_y$, we have
		$$
		\forall y \in X, \; x \in X \setminus U_y = \{x\} \iff x \in \bigcap_{y \in X \setminus \{x\}} X \setminus U_y  = \{x\}.
		$$
		As all $X \setminus U_y$ are closed, their intersection $\{x\}$ is closed.
	\end{proof}
\end{theorem}
%--------------------------------

%--------------------------------
\begin{example}
	[$T_1$ but not Hausdorff]
	\label{eg: T_1 but not T_2}
	Let $X$ be a nonempty set, let $p \in X$, let $\mathcal T'$ be a Hausdorff topology on $X \setminus \{p\}$, and let
	$$
	\mathcal T = \{X\} \cup \mathcal T'.
	$$
	Then, all $x \in (X, \mathcal T)$ are closed, thus $(X, \mathcal T)$ is Fr\'echet. But the only neighbourhood of $p$ is $X$, so its closure is $X$. Then, for any $x \in X \setminus \{p\}$, $x$ and $p$ are not separated, in which case $(X, \mathcal T)$ is not $R_0$. Thus, $(X, \mathcal T)$ is not Hausdorff.
\end{example}
%--------------------------------


\section{Relations and Functions}\label{sec:relations_and_functions}
\subsection{Basics}
The Cartesian product of two sets can be viewed as describing all possible connections between the elements of the first set to the elements of the second set, and thus any subset of a Cartesian product forms a specific \emph{relation} between the sets.

\begin{example}{Relations as subsets of Cartesian products}{relation}
	Given the following two sets:
	\[
		A=\{1,2,3,4\},\ B=\{\alpha,\beta,\gamma\},
	\]
	then
	\begin{align*}
		A\times B = \left\{ (1,\alpha),\ (1,\beta),\ (1,\gamma), \right.\\
												(2,\alpha),\ (2,\beta),\ (2,\gamma),\\
												(3,\alpha),\ (3,\beta),\ (3,\gamma),\\
							  \left.	(4,\alpha),\ (4,\beta),\ (4,\gamma) \right\}.
	\end{align*}

	We can choose the following pairs to form a subset of $A\times B$:
	\[
		R = \left\{ (1,\beta),\ (2,\alpha),\ (3,\alpha),\ (3,\beta), (4,\gamma)  \right\}.
	\]
	$R$ is thus a relation between $A$ and $B$. We can graphically illustrate $R$ as follows:
	\begin{figure}[H]
		\centering
		\begin{tikzpicture}
			\Large

			\draw[thick, xred, fill=xred!20] (0,0) ellipse (0.5cm and 2cm) node[above, yshift=2cm] (Albl) {$A$};
			\node (1) at (0,1.5cm) {$1$};
			\node (2) at (0,0.5cm) {$2$};
			\node (3) at (0,-0.5cm) {$3$};
			\node (4) at (0,-1.5cm) {$4$};

			\draw[thick, xgreen, fill=xgreen!20] (2.5cm,0) ellipse (0.5cm and 1.5cm) node[above, yshift=2cm] (Blbl) {$B$};
			\node (alpha) at (2.5cm,1cm) {$\alpha$};
			\node (beta) at (2.5cm,0cm) {$\beta$};
			\node (gamma) at (2.5cm,-1cm) {$\gamma$};

			\draw[arrow] (1) -- (beta);
			\draw[arrow] (2) -- (alpha);
			\draw[arrow] (3) -- (alpha);
			\draw[arrow] (3) -- (gamma);
			\draw[arrow] (4) -- (beta);

			\draw[arrow] (Albl) -- (Blbl) node[midway, above] {$R$};
		\end{tikzpicture}
	\end{figure}
\end{example}

Relations can be inverted by reversing the order of each of its pairs.

\begin{example}{Inverse relation}{inv_relation}
	The inverse relation to the relation in \autoref{example:relation} is
	\[
		R^{-1} = \left\{ (\beta,1),\ (\alpha,2),\ (\alpha,3),\ (\beta,3), (\gamma,4)  \right\}.
	\]
	Graphically:
	\begin{figure}[H]
		\centering
		\begin{tikzpicture}
			\Large

			\draw[thick, xred, fill=xred!20] (0,0) ellipse (0.5cm and 2cm) node[above, yshift=2cm] (Albl) {$A$};
			\node (1) at (0,1.5cm) {$1$};
			\node (2) at (0,0.5cm) {$2$};
			\node (3) at (0,-0.5cm) {$3$};
			\node (4) at (0,-1.5cm) {$4$};

			\draw[thick, xgreen, fill=xgreen!20] (2.5cm,0) ellipse (0.5cm and 1.5cm) node[above, yshift=2cm] (Blbl) {$B$};
			\node (alpha) at (2.5cm,1cm) {$\alpha$};
			\node (beta) at (2.5cm,0cm) {$\beta$};
			\node (gamma) at (2.5cm,-1cm) {$\gamma$};

			\draw[arrow] (beta) -- (1);
			\draw[arrow] (alpha) -- (2);
			\draw[arrow] (alpha) -- (3);
			\draw[arrow] (gamma) -- (3);
			\draw[arrow] (beta) -- (4);

			\draw[arrow] (Blbl) -- (Albl) node[midway, above] {$R^{-1}$};
		\end{tikzpicture}
	\end{figure}
\end{example}

A \emph{function} $f$ from a set $A$ to a set $B$ is a relation for which any element in $A$ is connected to a single element in $B$.

\begin{example}{Functions}{functions}
	The following are two functions from the set $A$ to the set $B$ defined in \autoref{example:relation}:
	\begin{figure}[H]
		\centering
		\begin{tikzpicture}
			\Large
			% f
			\draw[thick, xred, fill=xred!20] (0,0) ellipse (0.5 and 2) node[above, yshift=2cm] (Albl) {$A$};
			\node (1f) at (0,1.5) {$1$};
			\node (2f) at (0,0.5) {$2$};
			\node (3f) at (0,-0.5) {$3$};
			\node (4f) at (0,-1.5) {$4$};

			\draw[thick, xgreen, fill=xgreen!20] (2.5,0) ellipse (0.5 and 1.5) node[above, yshift=2cm] (Blbl) {$B$};
			\node (alpha_f) at (2.5,1) {$\alpha$};
			\node (beta_f) at (2.5,0) {$\beta$};
			\node (gamma_f) at (2.5,-1) {$\gamma$};

			\draw[arrow] (1f) -- (alpha_f);
			\draw[arrow] (2f) -- (beta_f);
			\draw[arrow] (3f) -- (gamma_f);
			\draw[arrow] (4f) -- (gamma_f);

			\draw[arrow] (Albl) -- (Blbl) node[midway, above] {$f$};

			% g
			\draw[thick, xred, fill=xred!20] (6,0) ellipse (0.5 and 2) node[above, yshift=2cm] (Albl) {$A$};
			\node (1g) at (6,1.5)  {$1$};
			\node (2g) at (6,0.5)  {$2$};
			\node (3g) at (6,-0.5) {$3$};
			\node (4g) at (6,-1.5) {$4$};

			\draw[thick, xgreen, fill=xgreen!20] (8.5,0) ellipse (0.5 and 1.5) node[above, yshift=2cm] (Blbl) {$B$};
			\node (alpha_g) at (8.5,1) {$\alpha$};
			\node (beta_g) at  (8.5,0) {$\beta$};
			\node (gamma_g) at (8.5,-1) {$\gamma$};

			\draw[arrow] (1g) -- (alpha_g);
			\draw[arrow] (2g) -- (gamma_g);
			\draw[arrow] (3g) -- (beta_g);
			\draw[arrow] (4g) -- (alpha_g);

			\draw[arrow] (Albl) -- (Blbl) node[midway, above] {$g$};
		\end{tikzpicture}
	\end{figure}

	The pairs making up $f$ are $(1,\alpha),\ (2,\beta),\ (3,\gamma)$ and $(4,\gamma)$, and the pairs making up $g$ are $(1,\alpha),\ (2,\gamma),\ (3,\beta)$ and $(4,\alpha)$.
\end{example}

\begin{note}{Relations which are not functions}{}
	Note that the relation in \autoref{example:relation} is \textbf{not} a function, since the element $3\in A$ is connected to more than one element in $B$, namely $\alpha$ and $\gamma$.
\end{note}

Different names are used in some branches of mathematics to describe functions, such as \emph{maps} and \emph{transformations}. Barring context, they all mean the same thing.

A common way to denote that a function $f$ is connecting elements in $A$ to elements in $B$ is
\begin{equation}
	f: A\to B.
	\label{eq:function_basics}
\end{equation}
$A$ is called the \emph{domain} of $f$, and $B$ its \emph{image}. In this book and many other sources, the following notation is used: $f(x)=y$, which means that when we apply the function $f$ to an element $x\in A$, the result is the element it is connected to, i.e. $y\in B$. We write this as $x\mapsto y$ (the special symbol $\mapsto$ is called a \emph{mapping notation}).

\begin{example}{Value $\mapsto$ value notation for functions}{}
	For the functions $f,g$ as defined in \autoref{example:functions}:
	\begin{align*}
		&f(1)=\alpha,\ f(2)=\beta,\ f(3)=f(4)=\gamma.\\
		&g(1)=g(4)=\alpha,\ g(2)=\gamma,\ g(3)=\beta.
	\end{align*}
\end{example}

\subsection{Injective, surjective and bijective functions}
A function is \emph{injective} if each of the elements in its \textbf{image} is connected to by at most a single element in its \textbf{domain}. An injective function is also known as an \emph{injection}.

\begin{example}{Injective function}{injective}
	\centering
	\begin{tikzpicture}[scale=0.9]
% Injective function
		\fill[xred!20, draw=xred, thick] (0,0) circle (0.75cm and 2cm) node[above, yshift=2cm, text=xred] (A) {$A$};
		\node (A1) at (0,1.5) {$1$};
		\node (A2) at (0,0.5) {$2$};
		\node (A3) at (0,-0.5) {$3$};
		\node (A4) at (0,-1.5) {$4$};

		\fill[xgreen!20, draw=xgreen, thick] (2,0) circle (0.75cm and 1.75cm) node[above, yshift=2cm, text=xgreen] (B) {$B$};
		\node (B1) at (2,1.2) {$\alpha$};
		\node (B2) at (2,0.4) {$\beta$};
		\node (B3) at (2,-0.4) {$\gamma$};
		\node (B4) at (2,-1.2) {$\delta$};

		\draw[arrow] (A1) -- (B2);
		\draw[arrow] (A2) -- (B3);
		\draw[arrow] (A3) -- (B1);
		\draw[arrow] (A4) -- (B4);

		\draw[arrow] (A) to node[midway, above, yshift=2mm] {Injective} (B);

% Non injective function
		\fill[xred!20, draw=xred, thick] (6,0) circle (0.75cm and 2cm) node[above, yshift=2cm, text=xred] (A) {$A$};
		\node (A1b) at (6,1.5) {$1$};
		\node (A2b) at (6,0.75) {$2$};
		\node (A3b) at (6,0) {$3$};
		\node (A4b) at (6,-0.75) {$4$};
		\node (A5b) at (6,-1.5) {$5$};

		\fill[xgreen!20, draw=xgreen, thick] (8,0) circle (0.75cm and 1.75cm) node[above, yshift=2cm, text=xgreen] (B) {$B$};
		\node (B1b) at (8,1.2) {$\alpha$};
		\node (B2b) at (8,0.4) {$\beta$};
		\node (B3b) at (8,-0.4) {$\gamma$};
		\node (B4b) at (8,-1.2) {$\delta$};

		\draw[arrow] (A1b) -- (B1b);
		\draw[arrow, red] (A2b) -- (B2b);
		\draw[arrow, red] (A3b) -- (B2b);
		\draw[arrow] (A4b) -- (B3b);
		\draw[arrow] (A5b) -- (B4b);

		\draw[arrow] (A) to node[midway, above, yshift=2mm] {Non-injective} (B);
	\end{tikzpicture}

	\flushleft
	The function on the right in non-injective because the element $\beta\in B$ is connected to by two elements in $A$ ($2$ and $3$, red arrows).
\end{example}

A function is \emph{surjective} if every element in its image is connected to by at least a single element in its domain (see \autoref{example:surjective}). As with injective functions, a surjective function is also known as a \emph{surjection}. A non surjective function can be made into surjective function by excluding from its image any element that is not connected to by any element from its domain (see \autoref{example:surjectification}).

A function $f:A\to B$ that is both surjective and bijective is called a \emph{bijective function} (also a \emph{bijection}). All elements in the image of a bijection are connected to by exactly a single element in its domain. This means that the direction of the connections can be flipped, yielding the \emph{inverse} of the original function (denoted $f^{-1}$).

The reason only bijective functions have inverses is as follows: Given a function $f:A\to B$,
\begin{itemize}
	\item if $f$ is non-injective, then there is at least one element $y_{1}\in B$ which is connected to by at least two elements from $A$. We can name these elements $x_{1}$ and $x_{2}$. When inverted, $f^{-1}:B\to A$ has an element $y_{1}\in B$ (note that for $f^{-1}$, $B$ is its domain), which is connected to two or more elements in $A$, the image of $f^{-1}$. These are of course $x_{1},x_{2}$. This fact disqualifies $f^{-1}$ from being a function.
	\item If $f$ is non-surjective, then there exists at least one element $y_{2}\in B$ that is not connected to by any element from $A$. When inverted, $y_{2}$ in the domain $B$ of $f^{-1}$ is not connected to any element in its image $A$. This fact disqualifies $f^{-1}$ from being a function.
\end{itemize}

 \blindtext

\begin{example}{Surjective function}{surjective}
	\centering
	\begin{tikzpicture}[scale=0.9]
		% Surjection
		\fill[xred!20, draw=xred, thick] (0,0) circle (0.75cm and 2cm) node[above, yshift=2cm, text=xred] (A) {$A$};
		\node (A1) at (0,1.5) {$1$};
		\node (A2) at (0,0.5) {$2$};
		\node (A3) at (0,-0.5) {$3$};
		\node (A4) at (0,-1.5) {$4$};

		\fill[xgreen!20, draw=xgreen, thick] (2,0) circle (0.75cm and 1.75cm) node[above, yshift=2cm, text=xgreen] (B) {$B$};
		\node (B1) at (2,1.2) {$\alpha$};
		\node (B2) at (2,0.4) {$\beta$};
		\node (B3) at (2,-0.4) {$\gamma$};
		\node (B4) at (2,-1.2) {$\delta$};

		\draw[arrow] (A1) -- (B2);
		\draw[arrow] (A2) -- (B3);
		\draw[arrow] (A3) -- (B1);
		\draw[arrow] (A4) -- (B4);

		\draw[arrow] (A) to node[midway, above, yshift=2mm] {Surjective} (B);

		% Non surjection
		\fill[xred!20, draw=xred, thick] (6,0) circle (0.75cm and 2cm) node[above, yshift=2cm, text=xred] (A) {$A$};
		\node (A1b) at (6,1.5) {$1$};
		\node (A2b) at (6,0.75) {$2$};
		\node (A3b) at (6,0) {$3$};
		\node (A4b) at (6,-0.75) {$4$};
		\node (A5b) at (6,-1.5) {$5$};

		\fill[xgreen!20, draw=xgreen, thick] (8,0) circle (0.75cm and 1.75cm) node[above, yshift=2cm, text=xgreen] (B) {$B$};
		\node (B1b) at (8,1.2) {$\alpha$};
		\node (B2b) at (8,0.4) {$\beta$};
		\node (B3b) at (8,-0.4) {$\gamma$};
		\node (B4b) at (8,-1.2) {$\delta$};

		\draw[arrow] (A1b) -- (B1b);
		\draw[arrow, red] (A2b) -- (B2b);
		\draw[arrow, red] (A3b) -- (B2b);
		\draw[arrow] (A4b) -- (B3b);
		\draw[arrow] (A5b) -- (B4b);

		\draw[arrow] (A) to node[midway, above, yshift=2mm] {Non-surjective} (B);
	\end{tikzpicture}
\end{example}

\begin{example}{Making a non-surjective function into a surjection}{surjectification}
	Given the two sets $A=\{1,2,3,4\}$ and $B=\{\alpha,\beta,\gamma,\delta\}$, the following non-surjective function $f:A\to B$ is defined:
	\[
		f = \left\{ (1,\alpha),\ (2,\beta),\ (3,\gamma),\ (4,\gamma) \right\}.
	\]

	By removing $\delta$ from $B$, the function $f$ becomes surjective (though it remains non-injective).
\end{example}

\begin{example}{Cross examples}{}
	\centering
	\begin{tikzpicture}[scale=0.9]
		% Injection not surjection
		\fill[xred!20, draw=xred, thick] (0,0) circle (0.75cm and 2cm) node[above, yshift=2cm, text=xred] (A) {$A$};
		\node (A1) at (0,1) {$1$};
		\node (A2) at (0,0)	{$2$};
		\node (A3) at (0,-1) {$3$};

		\fill[xgreen!20, draw=xgreen, thick] (2,0) circle (0.75cm and 1.75cm) node[above, yshift=2cm, text=xgreen] (B) {$B$};
		\node (B1) at (2,1.05) {$\alpha$};
		\node (B2) at (2,0.35) {$\beta$};
		\node (B3) at (2,-0.35) {$\gamma$};
		\node (B3) at (2,-1.05) {$\delta$};

		\draw[arrow] (A1) -- (B1);
		\draw[arrow] (A2) -- (B2);
		\draw[arrow] (A3) -- (B3);

		\draw[arrow] (A) to node[midway, above, yshift=2mm] {Injective, non surjective} (B);

		% Surjection not injection
		\fill[xred!20, draw=xred, thick] (6,0) circle (0.75cm and 2cm) node[above, yshift=2cm, text=xred] (A) {$A$};
		\node (A1) at (6,1.5) {$1$};
		\node (A2) at (6,0.5)	{$2$};
		\node (A3) at (6,-0.5) {$3$};
		\node (A4) at (6,-1.5) {$4$};

		\fill[xgreen!20, draw=xgreen, thick] (8,0) circle (0.75cm and 1.75cm) node[above, yshift=2cm, text=xgreen] (B) {$B$};
		\node (B1) at (8,1.05) {$\alpha$};
		\node (B2) at (8,0.35) {$\beta$};
		\node (B3) at (8,-0.35) {$\gamma$};
		\node (B4) at (8,-1.05) {$\delta$};

		\draw[arrow] (A1) -- (B1);
		\draw[arrow] (A2) -- (B2);
		\draw[arrow] (A3) -- (B3);
		\draw[arrow] (A4) -- (B3);

		\draw[arrow] (A) to node[midway, above, yshift=2mm] {Injective, non surjective} (B);
	\end{tikzpicture}

	\vspace{1em}
	\begin{tikzpicture}[scale=0.9]
		% Injection and surjection
		\fill[xred!20, draw=xred, thick] (0,0) circle (0.75cm and 2cm) node[above, yshift=2cm, text=xred] (A) {$A$};
		\node (A1) at (0,1.5) {$1$};
		\node (A2) at (0,0.5)	{$2$};
		\node (A3) at (0,-0.5) {$3$};
		\node (A4) at (0,-1.5) {$4$};

		\fill[xgreen!20, draw=xgreen, thick] (2,0) circle (0.75cm and 1.75cm) node[above, yshift=2cm, text=xgreen] (B) {$B$};
		\node (B1) at (2,1.05) {$\alpha$};
		\node (B2) at (2,0.35) {$\beta$};
		\node (B3) at (2,-0.35) {$\gamma$};
		\node (B4) at (2,-1.05) {$\delta$};

		\draw[arrow] (A1) -- (B1);
		\draw[arrow] (A2) -- (B3);
		\draw[arrow] (A3) -- (B2);
		\draw[arrow] (A4) -- (B4);

		\draw[arrow] (A) to node[midway, above, yshift=2mm] {Injective and surjective} (B);

		% Surjection not injection
		\fill[xred!20, draw=xred, thick] (6,0) circle (0.75cm and 2cm) node[above, yshift=2cm, text=xred] (A) {$A$};
		\node (A1) at (6,1.5) {$1$};
		\node (A2) at (6,0.5)	{$2$};
		\node (A3) at (6,-0.5) {$3$};
		\node (A4) at (6,-1.5) {$4$};

		\fill[xgreen!20, draw=xgreen, thick] (8,0) circle (0.75cm and 1.75cm) node[above, yshift=2cm, text=xgreen] (B) {$B$};
		\node (B1) at (8,1.05) {$\alpha$};
		\node (B2) at (8,0.35) {$\beta$};
		\node (B3) at (8,-0.35) {$\gamma$};
		\node (B4) at (8,-1.05) {$\delta$};

		\draw[arrow] (A1) -- (B1);
		\draw[arrow] (A2) -- (B1);
		\draw[arrow] (A3) -- (B2);
		\draw[arrow] (A4) -- (B3);

		\draw[arrow] (A) to node[midway, above, yshift=2mm] {Neither injective nor surjective} (B);
	\end{tikzpicture}
\end{example}

\vspace{2em}
\begin{note}{Other names for bijections}{bijections}
	Bijections are also called \emph{one-to-one correspondences} and \emph{invertible functions}.
\end{note}

\subsection{Real functions}
In suitable cases, a function is defined via a general mapping rule. This should be very familiar to anyone who learned mathematics in high school, where many times functions are defined this way, e.g.
\begin{equation}
	f(x) = x^{2}+3x-4.
	\label{eq:function_by_formula}
\end{equation}

In mapping notation we can write \autoref{eq:function_by_formula} as $f:x\mapsto x^{2}+3x-4$. In high school mathematics, both the domain and image of such functions is $\mathbb{R}$, although it is almost never specified explicitly. Such functions are commonly referred to as \emph{real functions}, a convention used in this book as well.

\begin{example}{Functions defined using a mapping rule}{}
	The following are real functions:
	\[
		f_{1}(x) = 2x^{2}-5,\quad f_{2}(x)=\sin\left( \frac{x}{3} \right),\quad f_{3}(x)=\frac{1}{\sqrt{2\pi}}\eu^{-\frac{(x-\mu)^{2}}{\sigma^{2}}}.
	\]

	Note that these functions can also be defined using different sets, for example $f_{1}:\mathbb{N}\to\mathbb{Z},\quad f_{2}:\mathbb{N}\to[-1,1],\quad$ etc.
\end{example}

Real functions can be easily plotted in a \emph{Cartesian coordinate system} by drawing all the points $\left( x,f(x) \right)$ (i.e. all the points $\left( x,y \right)$, where $x,y\in\mathbb{R}$ and $x\mapsto y$). We call these points the \emph{graph} of $f$ over $\mathbb{R}$.

\begin{example}{Graphs of real functions}{graph_of_functions}
	The following two functions are plotted on the domain $\left[ -9,9 \right]$:

	\begin{minipage}{0.35\textwidth}
		\begin{itemize}
			\item \textcolor{xred}{$\bm{f(x)=x^{2}-2x-3}$},
			\item \textcolor{xgreen}{$\bm{g(x)=4\eu^{x}/\left( \eu^{x}+1 \right)}$}.
		\end{itemize}
	\end{minipage}%
	\begin{minipage}[c]{0.65\textwidth}
		\centering
		\begin{tikzpicture}
			\begin{axis}[
					graph2d,
					width=9cm, height=6cm,
					xmin=-9, xmax=9,
					ymin=-5, ymax=5,
					domain=-9:9,
					restrict y to domain=-5:5,
					declare function={f(\x)=\x^2-2*\x-3;},
					declare function={g(\x)=4*exp(\x)/(1+exp(\x));},
				]
				\addplot[function, color=xred] {f(x)};
				\addplot[function, color=xgreen] {g(x)};
			\end{axis}
		\end{tikzpicture}
	\end{minipage}
\end{example}

In \autoref{example:graph_of_functions}, the function $g(x)$ always increases in value from left to right. Let's give this notion a more formal tone: a function $f$ is said to be \emph{increasing} on an interval $I$ if for any $x_{1},x_{2}\in I$, if $x_{2}>x_{1}$ then $f\left( x_{2} \right) > f\left( x_{1} \right)$. We can similarily define the idea of \emph{decreasing} on an interval.

A property of some functions which is visually easy to depict is symmetry. A real function $f$ is said to be \emph{symmetric} if for any $x\in\mathbb{R},\ f(-x)=f(x)$. This essentially means that the $y$-axis mirrors the function's plot. If for any $x\in\mathbb{R},\ f(-x)=-f(x)$, we say that the function is \emph{anti-symmetric}. A function can be neither, but there's only a single function which is both: the zero function, i.e. $f(x)=0$.

\begin{example}{Symmetric and anti-symmetric functions}{function_symmetry}
	In the following graphs, the function on the top is symmetric, while the function on the bottom is anti-symmetric:
	\begin{figure}[H]
		\centering
		\begin{tikzpicture}
			\begin{axis}[
					graph2d,
					width=13cm, height=8cm,
					xmin=-5, xmax=5,
					ymin=-1.5, ymax=1.5,
					domain=-6:6,
					restrict y to domain=-1.5:1.5,
				]
				\addplot[function, xred] {(2*\x^2-2)*exp(-1/\x^2)/\x^4};
				%\addplot[function, xblue] {(2*\x^2-2)*exp(-1/\x^2)/\x^5};

					%\left(2x^{2}-2\right)\cdot\frac{\exp\left(-\frac{1}{x^{2}}\right)}{x^{4}}
			\end{axis}
		\end{tikzpicture}

		\begin{tikzpicture}
			\begin{axis}[
					graph2d,
					width=13cm, height=8cm,
					xmin=-4, xmax=4,
					ymin=-2, ymax=2,
					domain=-4:4,
					restrict y to domain=-2:2,
				]
				\addplot[function, xblue] {(2*\x^2-2)*exp(-1/\x^2)/\x^5};
			\end{axis}
		\end{tikzpicture}
	\end{figure}
\end{example}

(injections/surjections of real functions?)

A real function is said to be \emph{periodic} if it repeats its values exactly over and over with increasing $x$. In more precise terms we define a real function $f$ to be periodic if for any integer value $k$,
\begin{equation}
	f(x+kT) = f(x).
	\label{eq:periodic function}
\end{equation}
where $T=[a,b]$ is a finite interval of $\mathbb{R}$ which we call the \emph{period} of the function.

\begin{example}{A periodic function}{periodic function}
	The following graph depicts a periodic function $f$, with its period $T$ shown. Notice that for any $x\ f(x+T)=f(x)$, i.e. you can move the period measure left and right along the $x$-axis and the values of $f(x)$ in both its edges would always be the equal.

	\centering
	\begin{tikzpicture}
		\begin{axis}[
				graph2d,
				width=14cm, height=6cm,
				xmin=-15, xmax=15,
				ymin=-5, ymax=5,
				xticklabels={},
				yticklabels={},
				domain=-15:15,
				restrict y to domain=-6:6,
				samples=350,
			]
			\addplot[function, xgreen] {0.8*sin(deg(2*\x))+1.5*sin(deg(\x))};
			\draw[|-|, thick] (1,4) -- node[midway, below] {$T$} ({2*pi+1},4);
		\end{axis}
	\end{tikzpicture}
\end{example}

Two additional measures that arise from a period $T$ are the \emph{frequency} $f=\frac{1}{T}$, and the \emph{angular frequency} $\omega=2\pi f=\frac{2\pi}{T}$. We will use these measures later in the book.

\vspace{2em}
\begin{note}{Units of period and frequency}{}
	In a periodic function such as the one in the above example, the units for the period are the same one used for the horizontal axis, while the units of both frequency and angular frequency are both 1 over the unit used for the horizontal axis/period. For example, if the unit of the horizontal axis is that of seconds, then the frequency units are 1/seconds, i.e. Hertz (SI symbol: \si{Hz}).
\end{note}

\subsection{Composition of functions}
Functions can be \emph{composed} together, generating new functions. Given two functions $f:A\to B$ and $g:B\to C$, their composition is denoted as $f\circ g$. For the composition to be well defined, the \textbf{image} of $f$ must be the same as the \textbf{domain} of $g$, and the resulting composition would have $A$ as its domain and $C$ as its image, i.e. $f\circ g:A\to C$.

\begin{example}{Composition of functions}{composition}
	Consider the functions
	\[
		f(x)=x^{2},\quad g(x)=\sin(x).
	\]
	Using these functions, the two possible compositions are
	\begin{itemize}
		\item $f\circ g = f\left( g(x) \right) = \left[ \sin(x) \right]^{2}$, and
		\item $g\circ f = g\left( f(x) \right) = \sin\left( x^{2} \right)$.
	\end{itemize}
\end{example}

\begin{example}{Graphical representation of function composition}{}
	A graphical representation of composing two functions:
	\[
		f:\{1,2,3,4\}\to\{\alpha,\beta,\gamma,\delta\},\quad g:\{\alpha,\beta,\gamma,\delta\}\to\{a,b,c\}.
	\]
	\begin{figure}[H]
		\centering
		\begin{tikzpicture}[scale=0.9]
			\fill[xred!20, draw=xred, thick] (0,0) circle (0.75cm and 2cm);
			\node (A1) at (0,1.5) {$1$};
			\node (A2) at (0,0.5) {$2$};
			\node (A3) at (0,-0.5) {$3$};
			\node (A4) at (0,-1.5) {$4$};

			\fill[xpurple!20, draw=xpurple, thick] (3,0) circle (0.75cm and 2cm);
			\node (B1) at (3,1.2) {$\alpha$};
			\node (B2) at (3,0.4) {$\beta$};
			\node (B3) at (3,-0.4) {$\gamma$};
			\node (B4) at (3,-1.2) {$\delta$};

			\draw[arrow, thick] (1,2) -- node [midway, above] {$f$} ++(1,0);
			\draw[arrow, thick] (4,2) -- node [midway, above] {$g$} ++(1,0);

			\fill[xgreen!20, draw=xgreen, thick] (6,0) circle (0.75cm and 1.75cm);
			\node (C1) at (6,1) {$a$};
			\node (C2) at (6,0) {$b$};
			\node (C3) at (6,-1) {$c$};

			\draw[arrow] (A1) -- (B2);
			\draw[arrow] (A2) -- (B4);
			\draw[arrow] (A3) -- (B1);
			\draw[arrow] (A4) -- (B3);
			\draw[arrow] (B1) -- (C2);
			\draw[arrow] (B2) -- (C3);
			\draw[arrow] (B3) -- (C1);
			\draw[arrow] (B4) -- (C3);
		\end{tikzpicture}
	\end{figure}

	The composition results in the following function
	\[
		f\circ g:\{1,2,3,4\}\to\{a,b,c,\}.
	\]
	\begin{figure}[H]
		\centering
		\begin{tikzpicture}[scale=0.9]
			\fill[xred!20, draw=xred, thick] (0,0) circle (0.75cm and 2cm);
			\node (A1) at (0,1.5) {$1$};
			\node (A2) at (0,0.5) {$2$};
			\node (A3) at (0,-0.5) {$3$};
			\node (A4) at (0,-1.5) {$4$};

			\draw[arrow, thick] (1,2) -- node [midway, above] {$f\circ g$} ++(1,0);

			\fill[xgreen!20, draw=xgreen, thick] (3,0) circle (0.75cm and 1.75cm);
			\node (C1) at (3,1) {$a$};
			\node (C2) at (3,0) {$b$};
			\node (C3) at (3,-1) {$c$};

			\draw[arrow] (A1) -- (C3);
			\draw[arrow] (A2) -- (C3);
			\draw[arrow] (A3) -- (C2);
			\draw[arrow] (A4) -- (C1);
		\end{tikzpicture}
	\end{figure}
\end{example}