\chapter{Assignment-Hamiltonian Equation of Motion}
\textbf{Assignment 01}
\begin{enumerate}
	\item A particle of inass $m$ moves inside a bowl under gravity. If the surface of the bowl is given by the equation $z=\frac{1}{2} a\left(x^{2}+y^{2}\right)$, where $a$ is a constant.\\
	(A) Write down Lagrangian of the system in cylindrical co-ordinate.\\
	(a) Identified the cyclic coordinate and law of conservation of momentum.\\
	(b) Write down hamiltonion of the system in cylindrical coordinate system.
	 \item A bike stuntman rides inside a well of frictionless surface given by $z=a\left(x^{2}+y^{2}\right)$, under the action of gravity acting in the negative $z$ direction i.e $\vec{g}=-g \hat{z}$. What speed should be maintain to be able to ride at a constant height $z_{0}$ without falling down?
	 \item A block of mass $M$ is suspended vertically from the cciting by a spring of spring constant $k$. A pendulum of mass $m$ is attached to the bottom of this block by a mass less rod of length $l$ (as shown in the attached figure). Assume that the block can move only vertically and that the motion of the pendulum takes place in a fixed vertical plane.\\
	 (a) Write down Lagrangian of the system in suitable generalized coordinate.\\
	 (b) write down Lagranges equation of motion.
	 \begin{figure}[H]
	 	\centering
	 	\includegraphics[height=5cm,width=2.5cm]{Assignment-HE-01}
	 \end{figure}
	 \item A particle of mass $m$ is attached to fixed point $O$ by a weightless inextensible string of length $a$. It is rotating under the gravity as shown in the figure.\\
	 (a) Write down The Lagrangian of the system in spherical co-ordinate.\\
	 (b) write down Hamiltonian of the system.
	  \begin{figure}[H]
	 	\centering
	 	\includegraphics[height=4.5cm,width=3.5cm]{Assignment-HE-02}
	 \end{figure}
	 \item Particle of mass $m$ slides under the gravity without friction along the parabotic path
	 $y=a x^{2}$ axis shown in the figure. Here $a$ is a constant\\
	 (a) Write down Lagrangian of the system .\\
	 (b) Write down Lagranges equation of motion.\\
	 (c) write down Hamiltonian of the system.
	  \begin{figure}[H]
	 	\centering
	 	\includegraphics[height=3.5cm,width=4cm]{Assignment-HE-03}
	 \end{figure}
	 \item The Lagrangian of a particle of mass $m$ moving in one dimension is $L=\exp (\alpha t)\left[\frac{m \dot{x}^{2}}{2}-\frac{k x^{2}}{2}\right]$, where $\alpha$ and $k$ are positive constants.\\
	 (a) Find the Lagranges equation of motion of the particle.\\
	 (b) Write down Hamiltonian of the system.
	 \item A simple pendulum of mass $m_{2}$, with a mass $m_{1}$ at the point of support which can move on a horizontal line lying in the plane in which $m_{2}$ moves (Figure given)\\
	 (a) Write down Lagrangian of system.\\
	 (b) Which coordinate is cyclic co-ordinate.\\
	 (c) Find the generalized momentum.
	  \begin{figure}[H]
	 	\centering
	 	\includegraphics[height=4cm,width=5cm]{Assignment-HE-04}
	 \end{figure}
	 \item The point of support of a simple pendulum of length $b$. moves on a massless rim of a radius a rotating with constant angular velocity $\omega$.\\
	 (a) Obtain the expression for the Cartesian components of the velocity and acceleration and acceleration of the mass $m$.\\
	 (b) Write down expression of Lagrangian of a system in as function of $\omega$ and $\theta$\\
	 (c) Write down equation of motion of system.
	  \begin{figure}[H]
	 	\centering
	 	\includegraphics[height=5.5cm,width=5.5cm]{Assignment-HE-05}
	 \end{figure}
	 \item As shown in figure the particle of mass $m_{2}$ moves on a vertical axis and the whole system rotates about this axis with a constant angular velocity $\omega$.
	  \begin{tasks}(1)
	 	\task[\textbf{a.}] What is degree of freedom of system
	 	\task[\textbf{b.}] Write down Lagrangian of the system in spherical polar co-ordinate.
	 	\task[\textbf{c.}]Write down Lagranges equation of the system.
	 	\task[\textbf{d.}] Write down Hamiltonian of the system.
	 \end{tasks}
  \begin{figure}[H]
 	\centering
 	\includegraphics[height=6cm,width=3.6cm]{Assignment-HE-06}
 \end{figure}
	 \item A bead of mass $m$ can slide without friction along a mass less rod kept at $45^{\circ}$ with the vertical as shown in the figure. The rod is rotating about the vertical axis with a constant angular speed $\omega$. At any instant $r$ is the distance of the bead from the origin.\\
	 (a) Find the Lagrangian of the system\\
	 (b) Find the momentum conjugate to $r$.
	  \begin{figure}[H]
	 	\centering
	 	\includegraphics[height=4.5cm,width=6.5cm]{Assignment-HE-07}
	 \end{figure}
	 \item A bead slides along a smooth wire bent in the shape of a parabola $z=c r^{2}$ (figure). The bead rotates in a circle of radius $R$ when the wire is rotating about its vertical symmetry axis with angular velocity $\omega$. Find the value of $c$.
	  \begin{figure}[H]
	 	\centering
	 	\includegraphics[height=6cm,width=5.2cm]{Assignment-HE-08}
	 \end{figure}
\end{enumerate}
\textbf{Assignment 02}
\begin{enumerate}
	\item A system is governed by the Hamiltonian
	$$
	I=\frac{1}{2}\left(p_{x}-a y\right)^{2}+\frac{1}{2}\left(p_{y}-b x\right)^{2}
	$$
	where $a$ and $b$ are constants and $p, p$, are momenta conjugate to $x$ and $y$ respectively. For what values of $a$ and $b$ will the quantities $\left(p_{x}-3 y\right)$ and $\left(p_{y}+2 x\right)$ be conserved.
	\item A particle of mass $m$ and coordinate $q$ has the Lagrangian $L=\frac{1}{2} m \dot{q}^{2}-\frac{\lambda}{2}-\dot{q}^{2}$ where $\lambda$ is a constant. Find the Hamiltonian of the system.
	\item The coordinates and momenta $x_{i}, \quad p_{i}(i=1,2,3)$ of a particle satisfy the canonical Poisson bracket relations $\left\{x_{i}, p_{j}\right\}=\delta_{i j}$. If $C_{1}=x_{2} p_{3}+x_{3} p_{2}, C_{2}=x_{1} p_{2}-x_{2} p_{1}$ and $C_{3}=x_{1} p_{3}+x_{3} p_{1} .$ Then\\
	(a) Find the value of $\left\{C_{1}, C_{2}\right\}$\\
	(b) Find the relation between $C_{1}, C_{2}$ and $C_{3}$.
	\item If Hamiltonian of the Harmonic oscillator is given by $H=\frac{p^{2}}{2 m}+\frac{1}{2} m \omega^{2} x^{2}$, the quantity defined $u(x, p, t)=\ln (p+i m \omega x)-i \omega t$ then check whether $u$ is conserve during the motion.
	\item The Hamiltonian of a particle of unit mass moving in the $x y$-plane is given to be: $H=x p_{x}-y p_{y}-\frac{1}{2} x^{2}+\frac{1}{2} y^{2}$ in suitable units. The initial values are given to be $(x(0), y(0))=(1,1)$ and $\left(p_{x}(0), p_{y}(0)\right)=\left(\frac{1}{2}, \frac{1}{2}\right)$.\\
	(a) Discuss equation of motion\\
	(b) Plot the curves traced out by the particles in the $x y$-plane and the $p_{x} p_{y}-$ plane.
	\item A mechanical system is described by the Hamiltonian $H(q, p)=\frac{p^{2}}{2 m}-\frac{1}{2} m \omega^{2} q^{2} .$ As a result of the canonical transtormation generated by $F(q, Q)=-\frac{Q}{q}$, the Hamiltonian in the new coordinate $Q$ and momentum $P$.
	\item The Hamiltonian of the system is given by $H=\frac{p_{x}^{2}}{2 m}+\frac{p_{y}^{2}}{2 m}+\frac{m \omega^{2}\left(x^{2}+y^{2}\right)}{2}$. Check whether the following quantities are conserve during the motion or not.\\
	(a) $S_{1}=\frac{1}{2}\left(x p_{y}-y p_{x}\right)$\\
	(b) $S_{2}=\frac{1}{2 m \omega}\left(p_{x} p_{1}+m^{2} \omega^{2} x y\right)$\\
	(c) $S_{3}=\frac{1}{4 m \omega}\left[p_{s}^{2}-p_{3}^{2}+m^{2} \omega^{2}\left(y^{2}-x^{2}\right)\right]$\\
	\item The transformation equations between two sets of coordinate are
	$$
	\begin{aligned}
	&Q=\log \left(1+q^{1 / 2} \cos p\right) \\
	&P=2\left(1+q^{1 / 2} \cos p\right) q^{1 / 2} \sin p
	\end{aligned}
	$$\
	(a) Show that $Q, P$ are canonical transform to $q, p$.\\
	(b) Show that the function that generates this transformation is $F_{3}=-\left(e^{Q}-1\right)^{2} \tan p$
	\item Prove that the transformation is canonical.\\
	(i) $Q_{1}=q_{1}, P_{1}=p_{1}-2 p_{2}$,\\
	(ii) $Q_{2}=p_{2}, P_{2}=-2 q_{1}-q_{2}$
	\item  The Hamiltonian of harmonic oscillator is given by $H=\frac{1}{2 m}\left(p^{2}+m^{2} \omega^{2} q^{2}\right)$\\
 	A. \begin{tasks}(1)
		\task[\textbf{a.}]Write down equation of motion
		\task[\textbf{b.}]Plot phase space for given energy $E$
		\task[\textbf{c.}] Plot $q$ vs $t$ with initial condition $t=0, q=q_{0}$ and $\dot{q}-0$ at $t=0$
		\task[\textbf{d.}] Plot $p$ vs $t$
	\end{tasks}
	B.	 \begin{tasks}(1)
		\task[\textbf{a.}] (a) If a generating function is defined as $F_{1}=\frac{m \omega q^{2}}{2} \cot Q$ then find canonical transformation.\\
		From the use of canonical transformation $H=\frac{1}{2 m}\left(p^{2}+m^{2} \omega^{2} q^{2}\right)$
		\task[\textbf{b.}]Find new Hamiltonian $K(Q, P, t)$
		\task[\textbf{c.}]Plot $Q$ vs $t$
		\task[\textbf{d.}]Plot $P$ vs $t$
		\task[\textbf{e.}]Plot phase space between $P$ and $Q$
	\end{tasks}
\item If generating function is given by $F(q, P)=q^{2} P$ and Hamiltonian of the system is given by $H(q, p)=\frac{p^{2}}{2 \alpha q^{2}}+\frac{\beta q^{4}}{4}$ where $\alpha$ and $\beta$ are constants, then find the equation of motion in term of $Q, P$.
\item If Lagrangian of the system is given by $L=\frac{1}{2} m \dot{x}^{2}+m\left(\dot{y}^{2}+\dot{z}^{2}\right)-\frac{1}{2} k x^{2}-\frac{1}{2} k(y+z)^{2}$
 \begin{tasks}(2)
	\task[\textbf{a.}]Write down Hamiltonian of system.
	\task[\textbf{b.}]If $L_{z}=x p_{y}-y p_{x}$, find $\frac{d L_{z}}{d t}$
	\task[\textbf{c.}]If $L_{x}=y p_{z}-z p_{y}$, find $\frac{d L_{x}}{d t}$
	\task[\textbf{d.}] If $L_{y}=z p_{x}-x p_{z}$, find $\frac{d L_{y}}{d t}$
	\task[\textbf{e.}]Find $\frac{d p_{x}}{d t}$ and $\frac{d p_{y}}{d t}$
\end{tasks}

\end{enumerate}

Classical Mechanics  -CSIR/chapter/Assignments/Assignment-Hamilltonian Equation of Motion.tex


\documentclass[12pt]{cdblatex}
\usepackage{pgfplots}
\usepackage{fancyhdr}
\usepackage{footer}

\begin{document}

% =======================================================================================
\section*{ADM and BSSN variables for the Kasner metric}

A standard form of the Kasner metric is given by
\begin{equation*}
   ds^2 = - dt^2 + t^{2p_1} dx^2 + t^{2p_2} dy^2 + t^{2p_3} dz^2
\end{equation*}
where $p_1$, $p_2$ and $p_3$ are constants subject to
\begin{align*}
   1 &= p_1 + p_2 + p_3\\
   1 &= p^1_1 + p^2_2 + p^3_2
\end{align*}

The following Cadabra codes compute various quantities defined in the ADM and BSSN
formulations of the Einstein equations.

All of the results are exactly as expected (what else could it give?).

None of this is new -- the main point of this whole exercise is to use a familar metric
to explore how standard computations can be implemented using Cadabra.

None of these results are used by the main evolution codes (in the directories {\tt
adm} and {\tt bssn}) other than to set the initial data (at $t=1$).

The code that sets the initial data was written by hand (as opposed to the Cadabra
codes that generates the Ada procedures used in the evolution codes).

\clearpage

\begin{cadabra}
   {t,x,y,z}::Coordinate.
   {a,b,c,d,e,f,i,j,k,l,m,n,o,p,q,r,s,u#}::Indices(position=independent,values={t,x,y,z}).

   \partial{#}::PartialDerivative;

   {p1,p2,p3}::Symbol.

   p1::LaTeXForm("p_1").
   p2::LaTeXForm("p_2").
   p3::LaTeXForm("p_3").

   gBar{#}::LaTeXForm("{\bar g}").
   ABar{#}::LaTeXForm("{\bar A}").
   Aab{#}::LaTeXForm("{A}").
   phi::LaTeXForm("{\phi}").

   g_{a b}::Metric.
   g^{a b}::InverseMetric.

   g_{a b}::Depends(\partial{#}).

   # -----------------------------------------------------------------
   # rules used when evaluating components

   DtRule := {D^{t} -> 1}.      # components of d/dt, zero shift & unit lapse

   gabRule := { g_{t t} = gtt,
                g_{x x} = gxx,
                g_{y y} = gyy,
                g_{z z} = gzz }.

   # -----------------------------------------------------------------
   # the Kasner metric

   gab := { gtt -> -1,
            gxx -> t**(2*p1),
            gyy -> t**(2*p2),
            gzz -> t**(2*p3),
            gxy -> 0,
            gxz -> 0,
            gyz -> 0,
            gtx -> 0,
            gty -> 0,
            gtz -> 0}.

   # -----------------------------------------------------------------
   # standard definitions

   Detg := g ->  gxx gyy gzz - gxx gyz gyz
               - gxy gxy gzz + gxy gxz gyz
               + gxz gxy gyz - gxz gxz gyy.

   Gamma := \Gamma^{a}_{b c} ->
            (1/2) g^{a e} (   \partial_{b}{g_{e c}}
                            + \partial_{c}{g_{b e}}
                            - \partial_{e}{g_{b c}}).

   Rabcd := R^{a}_{b c d} ->
              \partial_{c}{\Gamma^{a}_{b d}} + \Gamma^{a}_{e c} \Gamma^{e}_{b d}
            - \partial_{d}{\Gamma^{a}_{b c}} - \Gamma^{a}_{e d} \Gamma^{e}_{b c}.

   Rab := R_{a b} -> R^{c}_{a c b}.

   Kab := K_{a b} -> - (1/2) D^{c} \partial_{c}{g_{a b}} / N.

   # -----------------------------------------------------------------
   # the BSSN variables

   trK   := K          -> g^{a b} K_{a b}.
   Aab   := Aab_{a b}  -> K_{a b} - (1/3) g_{a b} K.
   gBar  := gBar_{a b} -> g_{a b} / (g**(1/3)).
   ABar  := ABar_{a b} -> (K_{a b} - (1/3) g_{a b} K) / (g**(1/3)).
   phi   := phi        -> (1/12) \log(g).

   # -----------------------------------------------------------------
   # basic objects

   substitute (gabRule, gab)
   substitute (Detg,    gab)

   complete   (gabRule, $g^{a b}$)                                      # cdb(gabRule,gabRule)

   substitute (Rabcd,   Gamma)
   substitute (Rab,     Rabcd)

   # -----------------------------------------------------------------
   # convert to BSSN

   substitute (gBar,  Detg)                                             # cdb (gBar.01,gBar)

   substitute (Aab,   trK)                                              # cdb (Aab.01,Aab)
   substitute (Aab,   Kab)                                              # cdb (Aab.02,Aab)

   substitute (ABar,  trK)                                              # cdb (ABar.01,ABar)
   substitute (ABar,  Kab)                                              # cdb (ABar.02,ABar)
   substitute (ABar,  Detg)                                             # cdb (ABar.03,ABar)

   substitute (phi,   Detg)                                             # cdb (phi.01,phi)

   # -----------------------------------------------------------------
   # now evaluate the components

   evaluate   (gab,   gabRule+DtRule, rhsonly=True)                     # cdb (gab,gab)
   evaluate   (Gamma, gabRule+DtRule, rhsonly=True)                     # cdb (Gamma,Gamma)
   evaluate   (Rabcd, gabRule+DtRule, rhsonly=True)                     # cdb (Rabcd,Rabcd)
   evaluate   (Rab,   gabRule+DtRule, rhsonly=True)                     # cdb (Rab,Rab)
   evaluate   (Kab,   gabRule+DtRule, rhsonly=True)                     # cdb (Kab,Kab)
   evaluate   (trK,   gabRule+DtRule, rhsonly=True)                     # cdb (trK,trK)

   evaluate   (gBar,  gabRule+DtRule, rhsonly=True)                     # cdb (gBar.02,gBar)
   evaluate   (Aab,   gabRule+DtRule, rhsonly=True)                     # cdb (Aab.03,Aab)
   evaluate   (ABar,  gabRule+DtRule, rhsonly=True)                     # cdb (ABar.04,ABar)

   evaluate   (phi,   gabRule+DtRule, rhsonly=True)                     # cdb (phi.02,phi)

\end{cadabra}

\begin{dgroup*}
   \begin{dmath*} \Cdb*{gab}   \end{dmath*}
   \begin{dmath*} \Cdb*{Gamma} \end{dmath*}
   \begin{dmath*} \Cdb*{Rabcd} \end{dmath*}
   \begin{dmath*} \Cdb*{Rab}   \end{dmath*}
   \begin{dmath*} \Cdb*{Kab}   \end{dmath*}
   \begin{dmath*} \Cdb*{trK}   \end{dmath*}
\end{dgroup*}

% \clearpage

\begin{dgroup*}
   \begin{dmath*} \Cdb*{gBar.01} \end{dmath*}
   \begin{dmath*} \Cdb*{gBar.02} \end{dmath*}
\end{dgroup*}

\begin{dgroup*}
   \begin{dmath*} \Cdb*{phi.01} \end{dmath*}
   \begin{dmath*} \Cdb*{phi.02} \end{dmath*}
\end{dgroup*}

% \clearpage

\begin{dgroup*}
   \begin{dmath*} \Cdb*{Aab.01}  \end{dmath*}
   \begin{dmath*} \Cdb*{Aab.02}  \end{dmath*}
   \begin{dmath*} \Cdb*{Aab.03}  \end{dmath*}
\end{dgroup*}

\begin{dgroup*}
   \begin{dmath*} \Cdb*{ABar.01} \end{dmath*}
   \begin{dmath*} \Cdb*{ABar.02} \end{dmath*}
   \begin{dmath*} \Cdb*{ABar.03} \end{dmath*}
   \begin{dmath*} \Cdb*{ABar.04} \end{dmath*}
\end{dgroup*}

\end{document}


\subsection{Jinchao's summary}

Some ideas:

\begin{enumerate}
\item Given structure of DNN or CNN, sampling of the parameters can be
  viewed a dual sampling of the data variable.  This is because any
  DNN function can be written as a combination or composition of the
  functions of the following form
$$
\sigma(\omega\cdot x+b)
$$

\item CNN can be considered to be a multigrid algorithm for data
  variables.  Given the observation above, it is also a ``dual''
  multigrid algorithm for the parameter variables.   But somehow, in
  CNN, only small subspace of the dual subspace are used because the
  convolutional operation is a very special and low dimensional dual
  operation.   In order to increase the dimension of the dual, more
  layers and especially more channels have to be introduced.

\item Data space variables are not really independent variables.  For
  a  2-D images, each image is a function of two variable which
  represents the pixel locations.

\item Any 2D image function must have certain regularity. Thus a high
  resolution image is intrinsically low dimensional.   There are some
  underlying dimensions of the image data.  The different covolutions
  and different channels are introduced to capture these low
  dimensions.

\item When a random initialization, it has been observed that the
  resulting DNN function is actually a relatively smooth function with
  respect to data variables.
  But the finally trained NN model has to be high frequence functions
  for data variable as it is very sensitive to data pertubation.

\item
\end{enumerate}


\documentclass[../main.tex]{subfiles}
\begin{document}
In this chapter, we extend the data structure learned from the first part with more advanced data structures. These data structures are not as widely used as the basic data structures, however, they can be often seen to implement more advanced algorithms or they can be more efficient compared with algorithms that relies on a more basic version.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%% Monotonic Stack
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Monotone Stack}
\label{section_mono_stack}
\textit{A monotone Stack is a data structure the elements from the front to the end is strictly either increasing or decreasing. } For example, there is an line at the hair salo, and you would naturally start from the end of the line. However, if you are allowed to kick out any person that you can win at a fight, if every one follows the rule, then the line would start with the most powerful man and end up with the weakest one. This is an example of monotonic decreasing stack.
\begin{itemize}
    \item Monotonically Increasing Stack: to push an element $e$, starts from the rear element, we pop out element $r>=e$ (violation);
    \item Monotonically Decreasing Stack: we pop out element $r<=e$ (violation). T
\end{itemize}
The process of the monotone decresing stack is shown in Fig.~\ref{fig:mono_stack}.
\begin{figure}[h!]
    \centering
    \includegraphics[width=0.9\columnwidth]{fig/monotone_stack_fig.png}
    \caption{The process of decreasing monotone stack}
    \label{fig:mono_stack}
\end{figure}
\textit{Sometimes, we can relax the strict monotonic condition, and can allow the stack or queue have repeat value. }

To get the feature of the monotonic queue, with $[5, 3, 1, 2, 4]$ as example, if it is increasing:
\begin{lstlisting}[numbers=none]
index   v   Increasing stack        Decreasing stack
1       5   [5]                     [5]
2       3   [3] 3 kick out 5        [5, 3] #3->5
3       1   [1] 1 kick out 3        [5, 3, 1] #1->3
4       2   [1, 2] #2->1            [5, 3, 2] 2 kick out 1
5       4   [1, 2, 4] #4->2         [5,4] 4 kick out 2, 3
\end{lstlisting}
By observing the above process, what features we can get?
\begin{itemize}
    \item Pushing in to get smaller/larger item to the left: When we push an element in, if there exists one element right in front of it, 1) for increasing stack, we find the \textbf{nearest smaller item to the left} of current item, 2) for decreasing stack, we find the \textbf{nearest larger item} to the left instead. In this case, we get [-1, -1, -1, 1, 2], and [-1, 5, 3, 3, 5] respectively.
    \item Popping out to get smaller/larger item to the right: when we pop one element out, for the kicked out item, such as in step of 2, increasing stack, 3 forced 5 to be popped out, for 5, 3 is the first smaller item to the right. Therefore, if one item is popped out, for this item, the current item that is about to be push in is 1) for increasing stack, \textbf{the nearest smaller item to its right}, 2) for decreasing stack, \textbf{the nearest larger item to  its right}.  In this case, we get [3,1, -1, -1, -1], and [-1, 4, 2, 4, -1] respectively.
\end{itemize}
The conclusion is with monotone stack, we can search for smaller/larger items of current item either to its left/right.

\paragraph{Basic Implementation}

This monotonic queue is actually a data structure that needed to add/remove element from the end. In some application we might further need to remove element from the front. Thus Deque from collections fits well to implement this data structure. Now, we set up the example data:
\begin{lstlisting}[language=Python]
A = [5, 3, 1, 2, 4]
import collections
\end{lstlisting}

\paragraph{Increasing Stack} We can find first smaller item to left/right.

\begin{lstlisting}[language=Python]
def increasingStack(A):
    stack = collections.deque()
    firstSmallerToLeft = [-1]*len(A)
    firstSmallerToRight = [-1]*len(A)
    for i,v in enumerate(A):
        while stack and A[stack[-1]] >= v: # right is from the popping out
            firstSmallerToRight[stack.pop()] = v  # A[stack[-1]] >= v
        if stack:  #left is from the pushing in, A[stack[-1]] < v
            firstSmallerToLeft[i] = A[stack[-1]]
        stack.append(i)
    return firstSmallerToLeft, firstSmallerToRight, stack
\end{lstlisting}
Now, run the above example with code:
\begin{lstlisting}[language=Python]
firstSmallerToLeft, firstSmallerToRight, stack = increasingQueue(A)
for i in stack:
    print(A[i], end = ' ')
print('\n')
print(firstSmallerToLeft)
print(firstSmallerToRight)
\end{lstlisting}
The output is:
\begin{lstlisting}
1 2 4

[-1, -1, -1, 1, 2]
[3, 1, -1, -1, -1]
\end{lstlisting}

\paragraph{Decreasing Stack} We can find first larger item to left/right.

\begin{lstlisting}[language=Python]
def decreasingStack(A):
    stack = collections.deque()
    firstLargerToLeft = [-1]*len(A)
    firstLargerToRight = [-1]*len(A)
    for i,v in enumerate(A):
        while stack and A[stack[-1]] <= v:
            firstLargerToRight[stack.pop()] = v

        if stack:
            firstLargerToLeft[i] = A[stack[-1]]
        stack.append(i)
    return firstLargerToLeft, firstLargerToRight, stack
\end{lstlisting}
Similarily, the output is:
\begin{lstlisting}
5 4

[-1, 5, 3, 3, 5]
[-1, 4, 2, 4, -1]
\end{lstlisting}
For the above problem, If we do it with brute force, then use one for loop to point at the current element, and another embedding for loop to look for the first element that is larger than current, which gives us $O(n^2)$ time complexity. If we think about the BCR, and try to trade space for efficiency, and use monotonic queue instead, we gain $O(n)$ linear time and $O(n)$ space complexity.
% Let us look at an example,
% \begin{lstlisting}
% Given an array [5, 3, 1, 2, 4], our target is to return an array of the same size that each element denotes the relative index we need to move to the right to find the first element that is larger than the current element, if we can not find, then we use -1. For this example the return would be  [-1, 3, 1, 1, -1].
% \end{lstlisting}

% Solution: If we do it with brute force, then use one for loop to point at the current element, and another embedding for loop to look for the first element that is larger than current, which gives us $O(n^2)$ time complexity. If we think about the BCR, and try to trade space for efficiency, we can use a decreasing monotonic queue. The first elment to the right that is larger than current is equaivalent to find the first element in the left that is smaller than the element(this means we need to use decreasing queue). First we have $[5]$, then $[5, 3]$, $[5, 3 ,1]$, then when $2$ comes in, we need to kick out $1$, so for $1$ the first larger element to its right size is $2$, we record $index(2)-index(1) = 1$. Then we have $4$, which could kick out $2$, so $4$ is the required one, then we set $r[index(2)] = index(4)-index(2) = 1$, then $r[index(3)] = index(4)-index(3) = 3$, Finally there would only $[5, 4]$, so we set them to be $-1$.
% \begin{lstlisting}
% index   v   decreasing queue
% 1       5   [5]
% 2       3   [5,3]
% 3       1   [5,3,1]
% 4       2   [5, 3, 2], kick out 1, we found the first larger number to the right of 1, which is 2
% 5       4   [5,4], kick out 2, for 2, we found 4, kick out 3, for 3 we found 4
% \end{lstlisting}
% \begin{lstlisting}[language = Python]
% a = [5, 3, 1, 2, 4]
% def firstLagerNumToRight(num):
%   if not num:
%     return []
%   monoStack = [] #decreasing monotonic stack
%   rst = [-1]*len(num)
%   for i, v in enumerate(num):
%     while  monoStack and v >= num[monoStack[-1]]:
%         index = monoStack.pop()
%         rst[index] = i-index
%     monoStack.append(i)
%   return rst
% print(firstLagerNumToRight(a))
% # [-1, 3, 1, 1, -1]
% \end{lstlisting}
Monotone stack is especially useful in the problem of subarray where we need to find smaller/larger item to left/right side of an item in the array. To better understand the features and applications of monotone stack, let us look at some examples. First, we recommend the audience to practice on these obvious applications shown in LeetCode Problem Section before moving to the examples:

There is one problem that is pretty interesting:

\paragraph{Sliding Window Maximum/Minimum } Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position. Return the max sliding window. (LeetCode Probelm: 239. Sliding Window Maximum (hard))
\begin{lstlisting}[numbers=none]
Example:

Input: nums = [1,3,-1,-3,5,3,6,7], and k = 3
Output: [3,3,5,5,6,7]
Explanation:

Window position                Max
---------------               -----
[1  3  -1] -3  5  3  6  7       3
 1 [3  -1  -3] 5  3  6  7       3
 1  3 [-1  -3  5] 3  6  7       5
 1  3  -1 [-3  5  3] 6  7       5
 1  3  -1  -3 [5  3  6] 7       6
 1  3  -1  -3  5 [3  6  7]      7
\end{lstlisting}

\textbf{Analysis:} In the process of moving the window, any item that is smaller than its predecessor will not affect the max result anymore, therefore, we can use decrese stack to remove any trough. If the window size is the same as of the array, then the maximum value is the first element in the stack (bottom). With the sliding window, we record the max each iteration when the window size is the same as k. At each iteration, if need to remove the out of window item from the stack. For example of [5, 3, 1, 2, 4] with k = 3, we get [5, 3, 4]. At step 3, we get 5, at step 4, we remove 5 friom the stack, and we get 3. At step 5, we remove 3 if it is in the stack, and we get 4. With the monotone stack, we decrease the time complexity from $O(kn)$ to $O(n)$.
\begin{lstlisting}[language=Python]
import collections

def maxSlidingWindow(self, nums, k):
    ds = collections.deque()
    ans = []
    for i in range(len(nums)):
        while ds and nums[i] >= nums[ds[-1]]: indices.pop()
        ds.append(i)
        if i >= k - 1: ans.append(nums[ds[0]]) #append the current maximum
        if i - k + 1 == ds[0]: ds.popleft() #if the first also the maximum number is out of window, pop it out
    return ans
\end{lstlisting}

\begin{examples}[resume]
\item \textbf{907. Sum of Subarray Minimums (medium).} Given an array of integers A, find the sum of min(B), where B ranges over every (contiguous) subarray of A. Since the answer may be large, return the answer modulo $10^9 + 7$. \textit{Note: 1 <= A.length <= 30000, 1 <= A[i] <= 30000.}
\begin{lstlisting}[numbers=none]
Example 1:

Input: [3,1,2,4]
Output: 17
Explanation: Subarrays are [3], [1], [2], [4], [3,1], [1,2], [2,4], [3,1,2], [1,2,4], [3,1,2,4].
Minimums are 3, 1, 2, 4, 1, 1, 2, 1, 1, 1.  Sum is 17.
\end{lstlisting}

\textbf{Analysis:} For this problem, using naive solution to enumerate all possible subarries, we end up with $n^2$ subarray and the time complexity would be $O(n^2)$, and we will receive LTE. For this problem, we just need to sum over the minimum in each subarray. Try to consider the problem from another angel, what if we can figure out how many times each item is used as minimum value corresponding subarry? Then res = sum(A[i]*f(i)). If there is no duplicate in the array, then To get f(i), we need to find out:
\begin{itemize}
    \item left[i], the length of strict bigger numbers on the left of A[i],
    \item right[i], the length of strict bigger numbers on the right of A[i].
\end{itemize}
For the given examples, if A[i] = 1, then the left item is 3, and the right item is 4, we add 1*(left\_len*right\_len) to the result. However, if there is duplicate such as [3, 1, 4, 1], for the first 1, we need [3,1], [1], [1,4], [1, 4,1] with subarries, and for the second 1, we need [4,1], [1] instead. Therefore, we set the right length to find the >= item. Now, the problem in converted to the first smaller item on the left side and the first smaller or equal item on the right side. From the feature we draw above, we need to use increasing stack, as we know, from the pushing in, we find the first smaller item, and from the popping out, for the popped out item, the current item is the first smaller item on the right side. The code is as:
\begin{lstlisting}[language=Python]
def sumSubarrayMins(self, A):
    n, mod = len(A), 10**9 + 7
    left, s1 = [1] * n, []
    right = [n-i for i in range(n)]
    for i in range(n): # find first smaller to the left from pushing in
        while s1 and A[s1[-1]] > A[i]: # can be equal
            index = s1.pop()
            right[index] = i-index # kicked out
        if s1:
            left[i] = i-s1[-1]
        else:
            left[i] = i+1
        s1.append(i)
    return sum(a * l * r for a, l, r in zip(A, left, right)) % mod
\end{lstlisting}
The above code, we can do a simple improvement, by adding 0 to each side of the array. Then eventually there will only have [0, 0] in the stack. All of the items originally in the array they will be popped out, each popping, we can sum up the result directely:
\begin{lstlisting}[language=Python]
def sumSubarrayMins(self, A):
    res = 0
    s = []
    A = [0] + A + [0]
    for i, x in enumerate(A):
        while s and A[s[-1]] > x:
            j = s.pop()
            k = s[-1]
            res += A[j] * (i - j) * (j - k)
        s.append(i)
    return res % (10**9 + 7)
\end{lstlisting}
\end{examples}
\section{Disjoint Set}
\textbf{Disjoint-set data structure} (aka union-find data structure or merge-find set) maintains a collection $S = \{S_1, S_2, ..., S_k\}$ of disjoint \textit{dynamic} sets by partitioning a set of elements. We identify each set by a \textbf{representative}, which is some member of the set. It does matter which member is used only if we get the same answer both times if we ask for the representative twice without modifying the set. Choosing the smallest member in a set as representative is an examplary prespecified rule. According to its typical applications such as implementing Kruskal's minimum spanning tree algorithm and tracking connected components dynamically, disjoint-set should support the following operations:
\begin{enumerate}
    \item \texttt{make\_set(x)}: create a new set whose only member is $x$. To keep these sets to be disjoint, this member should not already be in some existent sets.
    \item \texttt{union(x, y)}: unites the two dynamic sets that contain $x$ and $y$, say $S_x \cup  S_y$ into a new set that is the union of these two sets. In practice, we merge one set into the other say $S_y$ into $S_x$, we then remove/destroy $S_y$. This will be more efficient than create a new one that unions and destroy the other two.
    \item \texttt{find\_set(x)}: returns a pointer to the representative of the set that contains $x$.

\end{enumerate}
\paragraph{Applications} Disjoint sets are applied to implement union-find algorithm where perfoms \texttt{find\_set} and \texttt{union}. Union-find algorithms can be used into some basic graph algorithms, such as cycle detection, tracking connected components in the graph dynamically,  ~\footnote{where new edge will be added and the search based algorithm each time will be rerun to find them again}, Krauskal's MST algorithm, and Dijkstra's Shortest path algorithm.

\paragraph{Connected Component} Before we move to the implementation, let us first see how disjoint set can be applied to connected components.
\begin{figure}[h]
    \centering
    \includegraphics[width=0.98\columnwidth] {fig/disjoint_set.png}
    \caption{The connected components using disjoint set.}
    \label{fig:cc_undirected_disjoint_set}
\end{figure}
At first, we assign a set id for each vertex in the graph. Then we traverse each edge, and if the two endpoints of the edge belongs to different set, then we union the two sets. As shown in the process, first vertex 0 and 1 has different set id, then we update 1's id to 0. For edge (1, 2), we update 2's id to 0. For edge(0, 2), they are already in the same set, no update needed. We apply the same process with edge (2, 4), (3, 4), and (5, 6).

\subsection{Basic Implementation with Linked-list or List}
Before we head off to more efficient and complex implementation, we first implement a baseline for the convenience of comparison. The key for the implementation is two dictionaries named \texttt{item\_set} (saves the mapping between item and its set id, which will only be one to one) and \texttt{set\_item} (the value of the key will be a list, because one set will have one to multiple relation).

If our coding is right, each item must have an item when \texttt{find\_set} function is called, if not we will call \texttt{make\_set}. For each existing \texttt{set}, it will have at least one item. For function \texttt{union}, we choose the set that has less items to merge to the one that with more items.

\begin{lstlisting}[language=Python]
class DisjointSet():
  '''Implement a basic disjoint set'''
  def __init__(self, items):
    self.n = len(items)
    self.item_set = dict(zip(items, [i for i in range(self.n)])) # first each set only has one item [i], this can be one->multiple match
    self.set_item = dict(zip([i for i in range(self.n)], [[item] for item in items])) # each item will always belong to one set

  def make_set(self, item):
    '''make set for new incoming set'''
    if item  in self.item_set:
      return

    self.item_set[item] = self.n
    self.n += 1

  def find_set(self, item):
    if item in self.item_set:
      return self.item_set[item]
    else:
      print('not in the set yet: ', item)
      return None

  def union(self, x, y):
    id_x = self.find_set(x)
    id_y = self.find_set(y)
    if id_x == id_y:
      return

    sid, lid = id_x, id_y
    if len(self.set_item[id_x]) > len(self.set_item[id_y]):
      sid, lid = id_y, id_x
    # merge items in sid to lid
    for item in self.set_item[sid]:
      self.item_set[item] = lid
    self.set_item[lid] += self.set_item[sid]
    del self.set_item[sid]
    return
\end{lstlisting}

\paragraph{Complexity} For $n$ items, we spend $O(n)$ time to initialize the two hashmaps. With the help of hashmap, function \texttt{find\_set} tasks only $O(1)$ time, accumulating it will give us $O(n)$. For function \texttt{union}, it takes more effort to analyze. From another angle, for one item $x$, it will only update its item id when we are unioning it to another set $x_1$. The first time, the resulting set $x_1$ will have at least two items. The second update will be union $x_1$ to $x_2$. Because the merged one will have smaller length, thus the resulting items in $x_2$ will at least be 4. Then it is the third, ..., up to $k$ updates. Because a resulting set will at most has $n$ in size, so for each item, at most $\log n$ updates will be needed. For $n$ items, this makes the upper bound for \texttt{union} to be $n\log n$.

However, for our implementation, we has additional cost, which is in \texttt{union}, where we merge the list. This cost can be easily limited to constant by using linked list. However, even with \texttt{list}, there are different ways to concatenate one list to another:

\begin{enumerate}
\item Use $+$ operator: The time complexity of the concat operation for two lists, A and B, is O(A + B). This is because you aren't adding to one list, but instead are creating a whole new list and populating it with elements from both A and B, requiring you to iterate through both.

\item \texttt{extend(lst)}: Use extend which doesn't create a new list but adds to the original. The time complexity should only be $O(1)$. On the other hand \texttt{l += [i]} modifies the original list and behaves like extend.
\end{enumerate}

\subsection{Implementation with Disjoint-set Forests}
Instead of using linear linked list, we use tree structure. Different with trees we have introduced before that a node points to its children, an item here will only points to its parent. A tree represents a set, and the root node is the representative and it points to itself. The straightforward algorithms that use this structure are not faster than the linked-list version. By introducing two heuristics--``Union by rank'' and ``path compression"--we can achieve asympotically optimal disjoint-set data structure.
\begin{figure}[h]
    \centering
    \includegraphics[width=0.98\columnwidth] {fig/disjoint_set.png}
    \caption{A disjoint forest}
    \label{fig:disjoint_forest_1}
\end{figure}
\subsubsection{Naive Version} We first need to create a \texttt{Node} class which stores \texttt{item} and another parent pointer \texttt{parent}. An \texttt{item} can be any immutable data structure with necessary information represents a node.
\begin{lstlisting}[language=Python]
class Node:
  def __init__(self, item):
    self.item = item # save node information
    self.parent = None
\end{lstlisting}

We need one dict data structure \texttt{item\_finder} and one set data structure \texttt{sets} to track nodes and set. From \texttt{item\_finder} we can do (item, node) map to find node, and then from the node further we can find its set representative node or execute \texttt{union} operation. \texttt{sets} is used to track all the representative nodes. When we union two sets, the one merged to the other will be deleted in \texttt{sets}. At the easy version, \texttt{make\_set} will create  tree with only one node. \texttt{find\_set} will start from the node and traverse all the way back to its final parent which is when \texttt{node.parent==node}. And a \texttt{union} operation will simply point one tree's root node to the root of another through \texttt{parent}. The code is as follows:
\begin{lstlisting}[language=Python]
class DisjointSet():
  '''Implement with disjoint-set forest'''
  def __init__(self, items):
    self.n = len(items)
    self.item_finder = dict()
    self.sets = set() # sets will have only the parent node

    for item in items:
      node = Node(item)
      node.parent = node
      self.item_finder[item] = node # from item we can find the node
      self.sets.add(node)

  def make_set(self, item):
    '''make set for new incoming set'''
    if item  in self.item_finder:
      return

    node = Node(item)
    node.parent = node
    self.item_finder[item] = node
    self.sets.add(node)
    self.n += 1

  def find_set(self, item):
    # from item->node->parent to set representative
    if item not in self.item_finder:
      print('not in the set yet: ', item)
      return None
    node = self.item_finder[item]
    while node.parent != node:
      node = node.parent
    return node

  def union(self, x, y):
    node_x = self.find_set(x)
    node_y = self.find_set(y)
    if node_x.item == node_y.item:
      return

    #the root of one tree to point to the root of the other
    # merge x to y
    node_x.parent = node_y
    #remove one set
    self.sets.remove(node_x)
    return

  def __str__(self):
    ans = ''
    for root in self.sets:
      ans += 'set: '+ str(root.item) + '\n'
    return ans

  def print_set(self, item):
    if item in self.item_finder:
      node = self.item_finder[item]
      print(node.item, '->', end='')
      while node.parent != node:
        node = node.parent
        print(node.item, '->', end='')
\end{lstlisting}
Let's run an example:
\begin{lstlisting}[language=Python]
ds = DisjointSet(items=[i for i in range(5)])
ds.union(0,1)
ds.union(1,2)
ds.union(2,3)
ds.union(3, 4)
print(ds)
for item in ds.item_finder.keys():
  ds.print_set(item)
  print(' ')
\end{lstlisting}
The output is:
\begin{lstlisting}[numbers=none]
set: 4

0 ->1 ->2 ->3 ->4 ->
1 ->2 ->3 ->4 ->
2 ->3 ->4 ->
3 ->4 ->
4 ->
\end{lstlisting}
The above implementation, both \texttt{make\_set} and \texttt{union} takes $O(1)$ time complexity. The main time complexity is incurred at \texttt{find\_set}, which traverse a path from node to root. If we assume each tree in the disjoint-set forest is balanced, the upper bound of this operation will be $O(\log n)$. However, if the tree is as worse as a linear linked list, the time complexity will goes to $O(n)$. This makes the total time complexity from $O(n\log n)$ to $O(n^2)$.

\subsubsection{Heuristics}
\paragraph{Union by Rank}
As we have seen from the above example, A sequence of $n-1$ \texttt{union} operations may create a tree that is just a linear chain of $n$ nodes. Union by rank, which is similar to the weighted-union heuristic we used with the linked list implementation, is applied to avoid the worst case.  For each node, other than the parent pointer, it adds \texttt{rank} to track the upper bound of the height of the associated node (the number of edges in the longest simple path between the node and a descendant leaf). In union by rank, we make the root with smaller rank point to the root with larger rank.

In the initialization, and \texttt{make\_set} operation, a single noded tree has an initial rank of 0. In \texttt{union(x, y)}, there will exist three cases:
\begin{lstlisting}[numbers=none]
Case 1 x.rank == y.rank:
        join x to y
        y.rank += 1
Case 2: x.rank < y.rank:
       join y to x
       x.rank += 1
Case 3: x.rank > y.rank:
        join y to x
        x's rank stay unchanged
\end{lstlisting}
Now, with adding \texttt{rank} to the node. We modify the naive implementation:
\begin{lstlisting}[language=Python]
class Node:
  def __init__(self, item):
    self.item = item # save node information
    self.parent = None
    self.rank = 0
\end{lstlisting}
The updated implementation of \texttt{union}:
\begin{lstlisting}[language=Python]
  def union(self, x, y):
    node_x = self.find_set(x)
    node_y = self.find_set(y)
    if node_x.item == node_y.item:
      return

    # link
    if node_x.rank > node_y.rank:
      node_y.parent = node_x
      #remove one set
      self.sets.remove(node_y)
    elif node_x.rank < node_y.rank:
      node_x.parent = node_y
      self.sets.remove(node_x)
    else:
      node_x.parent = node_y
      node_y.rank += 1
      self.sets.remove(node_x)
    return
\end{lstlisting}

\paragraph{Path Compression}
In our naive implementation, \texttt{find\_set} took the most time. With path compression, during the process of \texttt{find\_set}, it simply make each node on the find path point directly to its root. Path Compression wont affect the rank of each node. Now, we modify this function:
\begin{lstlisting}[language=Python]
  def _find_parent(self, node):
    while node.parent != node:
      node = node.parent
    return node

  def find_set(self, item):
    '''modified to do path compression'''
    # from item->node->parent to set representative
    if item not in self.item_finder:
      print('not in the set yet: ', item)
      return None
    node = self.item_finder[item]
    node.parent = self._find_parent(node) # change node's parent to the root node
    return node.parent
\end{lstlisting}
The same example, the output will be:
\begin{lstlisting}[numbers=none]
set: 1

0 ->1 ->
1 ->
2 ->1 ->
3 ->1 ->
4 ->1 ->
\end{lstlisting}
\begin{lstlisting}[language=Python]
import time, random
t0 =  time.time()
n = 100000
ds = DisjointSet(items=[i for i in range(n)])
for _ in range(n):
  i, j = random.randint(0, n-1), random.randint(0, n-1) #[0,n]
  ds.union(i, j)
print('time: ', time.time()-t0)
\end{lstlisting}
\begin{bclogo}[couleur = blue!30, arrondi=0.1,logo=\bccrayon,ombre=true]{Experiment to the running time of Linked-list VS naive forest VS heuristic forest} We run the disjoint set with n=100,000, and with n times of union:
\begin{lstlisting}[language=Python]
import time, random
t0 =  time.time()
n = 100000
ds = DisjointSet(items=[i for i in range(n)])
for _ in range(n):
  i, j = random.randint(0, n-1), random.randint(0, n-1) #[0,n]
  ds.union(i, j)
print('time: ', time.time()-t0)
\end{lstlisting}
The resulting time is: 1.09s, 50.4s, 1.19s
\end{bclogo}
\paragraph{Note} As we see, in our implementation, we have never removed any item from disjoint-set structure. Also, from the above implementation, we know the sets of the nodes, but we cant track items from the root node. How can we further improve this?
\section{Fibonacci Heap}
\section{Exercises}
\subsection{Knowledge Check}
\subsection{Coding Practice}
\paragraph{Disjoint Set}
\begin{enumerate}
    \item 305. Number of Islands II (hard)
\end{enumerate}
\end{document}


\documentclass{article}

\usepackage{amsmath,amssymb,siunitx,graphicx}
\usepackage[margin=1in]{geometry}
\DeclareSIUnit\ergs{ergs}
\DeclareSIUnit\yr{yr}
\DeclareSIUnit\AU{AU}
\DeclareSIUnit\msun{\ensuremath{\mathrm{M}_{\odot}}}

\title{Why Stars are Parsecs Apart}
\author{Matthias J. Raives}

\begin{document}

  \maketitle{}

  \section{Gravitational Capture}
  Suppose the distances between stars was set by the limits of gravitational capture; i.e., stars are as close together as they could be without forming binary systems.  In this case, we would expect the characteristic interstellar distance to be
  \begin{equation}
    d_{\ast} \sim \frac{2GM_{\ast}}{\sigma_{\ast}^{2}} = \SI{2.6e12}{\cm}\:\left(\frac{M_{\ast}}{\si{\msun}}\right)\left(\frac{\sigma_{\ast}}{\SI{100}{\km\per\second}}\right)^{-2} = \SI{0.2}{\AU}\:\left(\frac{M_{\ast}}{\si{\msun}}\right)\left(\frac{\sigma_{\ast}}{\SI{100}{\km\per\second}}\right)^{-2},
  \end{equation}
  where $M_{\ast}$ is the characteristic stellar mass and $\sigma_{\ast}$ is the stellar velocity dispersion.  We see that even for massive stars (say, \SI{100}{\msun}) moving slowly (say, \SI{10}{\km\per\second}), we only have a characteristic separation of $d_{\ast}\sim\SI{200}{\AU}$, far short of the parsec scale stellar separation we observe.  Thus, we can likely rule out the gravitational interactions of the stars as the mechanism behind the characteristic interstellar distance.

  \section{Galactic Astrophysics}
  Let us then suppose that the distances between stars is set by the physics of their host galaxies.  It behooves us to write:
  \begin{equation}
    f_{\ast}\rho_{disk} \sim M_{\ast}d^{-3}_{\ast}\Longrightarrow d_{\ast} \sim \left(\frac{M_{\ast}}{f_{\ast}\rho_{disk}}\right)^{1/3},
  \end{equation}
  where $f_{\ast}$ is the fraction of the disk mass in the form of stars.  The disk density can be determined from the surface density as:
  \begin{equation}
    \Sigma_{disk} \sim \rho_{disk}H \sim \rho_{disk}\sigma_{gas}t_{dyn} \sim \sigma_{gas}\left(\frac{\rho_{disk}}{G}\right)^{1/2}\Longrightarrow\rho_{disk} \sim G\left(\frac{\Sigma_{disk}}{\sigma_{gas}}\right)^{2},
  \end{equation}
  where $H$ is the scale height of the disk, $\sigma_{gas}$ is the velocity dispersion of the gas, and $t_{dyn}$ is the dynamical time.  We can appeal to Toomre stability to determine $\sigma_{gas}$:
  \begin{equation}
    Q_{T} \sim \frac{\sigma_{gas}v_{vir}}{G\Sigma_{disk}R_{disk}}\Longrightarrow \sigma_{gas}\sim Q_{T}G\Sigma_{disk}R_{disk}v_{vir}^{-1},
  \end{equation}
  where $v_{vir}$ is the virial velocity.  Thus we see:
  \begin{equation}
    \rho_{disk} \sim \frac{v_{vir}^{2}}{Q_{T}^{2}GR_{disk}^{2}}.
  \end{equation}
  Using $v_{vir}^{2}=GM/R_{vir}$ and $R_{disk}=\lambda R_{vir}$, where $\lambda$ is the spin parameter and $M$ is the halo mass:
  \begin{equation}
    \rho_{disk} \sim \frac{M}{Q_{T}^{2}R_{vir}^{3}\lambda^{2}} \sim \rho_{vir}Q_{T}^{-2}\lambda^{-2}
  \end{equation}
  Thus, we have:
  \begin{equation}
    d_{\ast} = M_{\ast}^{1/3}f_{\ast}^{-1/3}\lambda^{2/3}Q_{T}^{2/3}\rho_{vir}^{-1/3}
  \end{equation}
  The virial density at $z=0$ is defined as:
  \begin{equation}
    \rho_{vir}\sim\Delta_{c}\rho_{c,0}\sim 200\rho_{c,0} = 200\frac{3H_{0}^{2}}{8\pi G}
  \end{equation}
  Thus, we have:
  \begin{equation}
    d_{\ast} \sim \SI{3.8}{pc}\:\left(\frac{M_{\ast}}{\si{\msun}}\right)^{1/3}\left(\frac{\lambda}{0.04}\right)^{2/3}f_{\ast}^{-1/3}Q_{T}^{2/3}
  \end{equation}
  Stars make up about $\sim90\%$ of the baryonic mass of the Milky way, and the gas is maintained at marginal stability by stellar feedback (i.e., the gas collapses and forms stars when perturbed, but generally not spontaneously), so $Q_{T}\sim f_{\ast} \sim 1$ is justified.

\end{document}


\chapter{Objects and morphisms}
\label{ch:cats}
I can't possibly hope to do category theory any justice in these few chapters;
thus I'll just give a very high-level overview of how many of the concepts we've
encountered so far can be re-cast into categorical terms.
So I'll say what a category is, give some examples,
then talk about a few things that categories can do.
For my examples, I'll be drawing from all the previous chapters;
feel free to skip over the examples corresponding to things you haven't seen.

If you're interested in category theory (like I was!), perhaps in
what surprising results are true for general categories, I strongly recommend \cite{ref:msci}.

\section{Motivation: isomorphisms}
From earlier chapters let's recall the definition of an \emph{isomorphism} of two objects:
\begin{itemize}
	\ii Two groups $G$ and $H$ are isomorphic if there was a bijective homomorphism:
	equivalently, we wanted homomorphisms $\phi : G \to H$ and $\psi : H \to G$
	which were mutual inverses, meaning $\phi \circ \psi = \id_H$ and $\psi \circ \phi = \id_G$.
	\ii Two metric (or topological) spaces $X$ and $Y$ are isomorphic
	if there is a continuous bijection $f : X \to Y$ such that $f\inv$ is also continuous.
	\ii Two vector spaces $V$ and $W$ are isomorphic if there is a bijection $T : V \to W$
	which is a linear map.
	Again, this can be re-cast as saying that $T$ and $T\inv$ are linear maps.
	\ii Two rings $R$ and $S$ are isomorphic if there is a bijective ring homomorphism $\phi$;
	again, we can re-cast this as two mutually inverse ring homomorphisms.
\end{itemize}

In each case we have some collections of objects and some maps,
and the isomorphisms can be viewed as just maps.
Let's use this to motivate the definition of a general \emph{category}.

\section{Categories, and examples thereof}
\prototype{$\catname{Grp}$ is possibly the most natural example.}
\begin{definition}
	A \vocab{category} $\AA$ consists of:
	\begin{itemize}
		\ii A class of \vocab{objects}, denoted $\obj(\AA)$.
		\ii For any two objects $A_1, A_2 \in \obj(\AA)$,
		a class of \vocab{arrows} (also called \vocab{morphisms} or \vocab{maps}) between them.
		We'll denote the set of these arrows by $\Hom_\AA(A_1, A_2)$.
		\ii For any $A_1, A_2, A_3 \in \obj(\AA)$,
		if $f : A_1 \to A_2$ is an arrow and $g : A_2 \to A_3$ is an arrow, we can compose
		these arrows to get an arrow $g \circ f : A_1 \to A_3$.

		We can represent this in a \vocab{commutative diagram}
		\begin{diagram}
			A_1 & \rTo^f & A_2 \\
			& \rdDashed^h & \dTo_g \\
			&& A_3
		\end{diagram}
		where $h = g \circ f$.
		The composition operation $\circ$ is part of the data of $\AA$;
		it must be associative.
		In the diagram above we say that $h$ \vocab{factors} through $A_2$.

		\ii Finally, every object $A \in \obj(\AA)$ has a special \vocab{identity arrow} $\id_A$;
		you can guess what it does.\footnote{To be painfully explicit: if $f : A' \to A$ is an arrow then $\id_A \circ f = f$;
		similarly, if $g : A \to A'$ is an arrow then $g \circ \id_A = g$.}
	\end{itemize}
\end{definition}
\begin{abuse}
	From now on, by $A \in \AA$ we'll mean $A \in \obj(\AA)$.
\end{abuse}
\begin{abuse}
	You can think of ``class'' as just ``set''.
	The reason we can't use the word ``set'' is
	because of some paradoxical issues with
	collections which are too large;
	Cantor's Paradox says there is no set of all sets.
	So referring to these by ``class'' is a way of sidestepping these issues.

	Now and forever I'll be sloppy and assume all my categories
	are \vocab{locally small}, meaning that $\Hom_{\AA} (A_1, A_2)$
	is a set for any $A_1, A_2 \in \AA$.
	So elements of $\AA$ may not form a set,
	but the set of morphisms between
	two \emph{given} objects will always assumed to be a set.
\end{abuse}

Let's formalize the motivation we began with.
\begin{example}
	[Basic examples of categories]
	\listhack
	\label{example:basic_categories}
	\begin{enumerate}[(a)]
		\ii There is a category of groups $\catname{Grp}$. The data is
		\begin{itemize}
			\ii The objects of $\catname{Grp}$ are the groups.
			\ii The arrows of $\catname{Grp}$ are the homomorphisms between these groups.
			\ii The composition $\circ$ in $\catname{Grp}$ is function composition.
		\end{itemize}
		\ii In the same way we can conceive a category $\catname{CRing}$ of (commutative) rings.
		\ii Similarly, there is a category $\catname{Top}$ of topological spaces,
		whose arrows are the continuous maps.
		\ii There is a category $\catname{Top}_\ast$ of topological spaces with a \emph{distinguished basepoint};
		that is, a pair $(X, x_0)$ where $x_0 \in X$.
		Arrows are continuous maps $f : X \to Y$ with $f(x_0) = y_0$.
		\ii Similarly, there is a category $\catname{Vect}_k$ of
		vector spaces (possibly infinite-dimensional) over a field $k$,
		whose arrows are the linear maps.
		There is even a category $\catname{FDVect}_k$ of
		\emph{finite-dimensional} vector spaces.
		\ii We have a category $\catname{Set}$ of sets,
		where the arrows are \emph{any} maps.
	\end{enumerate}
\end{example}
And of course, we can now define what an isomorphism is!
\begin{definition}
	An arrow $A_1 \taking{f} A_2$ is an \vocab{isomorphism}
	if there exists $A_2 \taking{g} A_1$ such that $f \circ g = \id_{A_2}$
	and $g \circ f = \id_{A_1}$.
	In that case we say $A_1$ and $A_2$ are \vocab{isomorphic}, hence $A_1 \cong A_2$.
\end{definition}
\begin{remark}
	Note that in $\catname{Set}$, $X \cong Y
	\iff \left\lvert X \right\rvert = \left\lvert Y \right\rvert$.
\end{remark}
\begin{ques}
	Check that every object in a category is isomorphic to itself.
	(This is offensively easy.)
\end{ques}
More importantly, this definition should strike you as a little impressive.
We're able to define whether two groups (rings, spaces, etc.) are isomorphic
solely by the functions between the objects.
Indeed, one of the key themes in category theory (and even algebra) is that
\begin{moral}
	One can learn about objects by the functions between them.
	Category theory takes this to the extreme by \emph{only} looking at arrows,
	and ignoring what the objects themselves are.
\end{moral}

But there are some trickier interesting examples of categories.
\begin{example}
	[Posets are categories]
	Let $\mathcal P$ be a partially ordered set.
	We can construct a category $P$ for it as follows:
	\begin{itemize}
		\ii The objects of $P$ are going to be the elements of $\mathcal P$.
		\ii The arrows of $P$ are defined as follows:
		\begin{itemize}
			\ii For every object $p \in P$, we add an identity arrow $\id_p$, and
			\ii For any pair of distinct objects $p \le q$, we add a single arrow $p \to q$.
		\end{itemize}
		There are no other arrows.
		\ii There's only one way to do the composition. What is it?
	\end{itemize}
\end{example}
For example, for the poset $\mathcal P$ on four objects $\{a,b,c,d\}$ with $a \le b$ and $a \le c \le d$, we get:
\begin{center}
\begin{tikzpicture}[scale=3.5]
	\SetVertexMath
	\Vertices{square}{d,c,a,b}
	\Edge[style={->}, label={$a \le b$}](a)(b)
	\Edge[style={->}, label={$a \le c$}](a)(c)
	\Edge[style={->}, label={$a \le d$}](a)(d)
	\Edge[style={->}, label={$c \le d$}](c)(d)
	\Loop[dist=8, dir=NO, label={$\id_a$}, labelstyle={above=1pt}](a)
	\Loop[dist=8, dir=WE, label={$\id_b$}, labelstyle={left=1pt}](b)
	\Loop[dist=8, dir=EA, label={$\id_c$}, labelstyle={right=1pt}](c)
	\Loop[dist=8, dir=WE, label={$\id_d$}, labelstyle={left=1pt}](d)
\end{tikzpicture}
\end{center}

This illustrates the point that
\begin{moral}
	The arrows of a category can be totally different from functions.
\end{moral}
In fact, in a way that can be made precise, the term ``concrete category'' refers
to one where the arrows really are ``structure-preserving maps between sets'',
like $\catname{Grp}$, $\catname{Top}$, or $\catname{CRing}$.

\begin{ques}
	Check that no two distinct objects of a poset are isomorphic.
\end{ques}

Here's a second quite important example of a non-concrete category.
\begin{example}
	[Important: groups are one-Object categories]
	A group $G$ can be interpreted as a category $\mathcal G$ with one object $\ast$,
	all of whose arrows are isomorphisms.

	\begin{center}
	\begin{tikzpicture}[scale=5.5]
		\Vertex[x=0,y=0,L={$\ast$}]{a}
		\Loop[dist=8, dir=NO, label={$1 = \id_a$}, labelstyle={above=1pt}](a)
		\Loop[dist=7, dir=WE, label={$g_2$}, labelstyle={left=1pt}](a)
		\Loop[dist=9, dir=SO, label={$g_3$}, labelstyle={below=1pt}](a)
		\Loop[dist=8, dir=EA, label={$g_4$}, labelstyle={right=1pt}](a)
	\end{tikzpicture}
	\end{center}

	As \cite{ref:msci} says:

	\begin{quote}
	The first time you meet the idea that a group is a kind of category,
	it's tempting to dismiss it as a coincidence or a trick.
	It's not: there's real content.
	To see this, suppose your education had been shuffled and you took a course
	on category theory before ever learning what a group was.
	Someone comes to you and says:

	``There are these structures called `groups', and the idea is this:
	a group is what you get when you collect together all the symmetries
	of a given thing.''

	``What do you mean by a `symmetry'?'' you ask.

	``Well, a symmetry of an object $X$ is a way of transforming $X$ or mapping
	$X$ into itself, in an invertible way.''

	``Oh,'' you reply, ``that's a special case of an idea I've met before.
	A category is the structure formed by \emph{lots} of objects and mappings
	between them -- not necessarily invertible. A group's just the very special case
	where you've only got one object, and all the maps happen to be invertible.''
	\end{quote}
\end{example}

\begin{exercise}
	Verify the above!
	That is, show that the data of a one-object category with all isomorphisms
	is the same as the data of a group.
\end{exercise}

Finally, here are some examples of categories you can make from other categories.
\begin{example}
	[Deriving categories]
	\listhack
	\begin{enumerate}[(a)]
		\ii Given a category $\AA$, we can construct the \vocab{opposite category}
		$\AA\op$, which is the same as $\AA$ but with all arrows reversed.
		\ii Given categories $\AA$ and $\BB$, we can construct the \vocab{product category} $\AA \times \BB$
		as follows: the objects are pairs $(A, B)$ for $A \in \AA$ and $B \in \BB$,
		and the arrows from $(A_1, B_1)$ to $(A_2, B_2)$
		are pairs \[ \left( A_1 \taking{f} A_2, B_1 \taking{g} B_2 \right). \]
		What do you think the composition and identities are?
	\end{enumerate}
\end{example}

\section{Special objects in categories}
\prototype{$\catname{Set}$ has initial object $\varnothing$ and final object $\{\ast\}$. An element of $S$ corresponds to a map $\{\ast\} \to S$.}
Certain objects in categories have special properties.
Here are a couple examples.
\begin{example}
	[Initial object]
	An \vocab{initial object} of $\AA$ is an object
	$A_{\text{init}} \in \AA$ such that for any $A \in \AA$ (possibly $A = A_{\text{init}}$),
	there is exactly one arrow from $A_{\text{init}}$ to $A$.
	For example,
	\begin{enumerate}[(a)]
		\ii The initial object of $\catname{Set}$ is the empty set $\varnothing$.
		\ii The initial object of $\catname{Grp}$ is the trivial group $\{1\}$.
		\ii The initial object of $\catname{CRing}$ is the ring $\ZZ$
		(recall that ring homomorphisms $R \to S$ map $1_R$ to $1_S$).
		\ii The initial object of $\catname{Top}$ is the empty space.
		\ii The initial object of a partially ordered set is its smallest element, if one exists.
	\end{enumerate}
\end{example}

We will usually refer to ``the'' initial object of a category, since:
\begin{exercise}
	[Important!]
	Show that any two initial objects $A_1$, $A_2$ of $\AA$ are \emph{uniquely isomorphic}
	meaning there is a unique isomorphism between them.
\end{exercise}

\begin{remark}
	In mathematics, we usually neither know nor care if two objects are actually equal
	or whether they are isomorphic.
	For example, there are many competing ways to define $\RR$,
	but we still just refer to it as ``the'' real numbers.

	Thus when we define categorical notions, we would like to check they are
	unique up to isomorphism.
	This is really clean in the language of categories, and definitions
	often cause objects to be unique up to isomorphism for elegant reasons like the above.
\end{remark}

One can take the ``dual'' notion, a terminal object.
\begin{example}
	[Terminal object]
	A \vocab{terminal object} of $\AA$ is an object
	$A_{\text{final}} \in \AA$ such that for any $A \in \AA$ (possibly $A = A_{\text{final}}$),
	there is exactly one arrow from $A$ to $A_{\text{final}}$.
	For example,
	\begin{enumerate}[(a)]
		\ii The terminal object of $\catname{Set}$ is the singleton set $\{\ast\}$.
		(There are many singleton sets, of course, but \emph{as sets} they are all isomorphic!)
		\ii The terminal object of $\catname{Grp}$ is the trivial group $\{1\}$.
		\ii The terminal object of $\catname{CRing}$ is the zero ring $0$.
		(Recall that ring homomorphisms $R \to S$ must map $1_R$ to $1_S$).
		\ii The terminal object of $\catname{Top}$ is the single-point space.
		\ii The terminal object of a partially ordered set is its maximal element, if one exists.
	\end{enumerate}
\end{example}

Again, terminal objects are unique up to isomorphism.
The reader is invited to repeat the proof from the preceding exercise here.
However, we can illustrate more strongly the notion of duality to give a short proof.
\begin{ques}
	Verify that terminal objects of $\AA$ are equivalent to initial objects of $\AA\op$.
	Thus terminal objects of $\AA$ are unique up to isomorphism.
\end{ques}
In general, one can consider in this way the dual of \emph{any} categorical notion:
properties of $\AA$ can all be translated to dual properties of $\AA\op$
(often by adding the prefix ``co'' in front).

One last neat construction: suppose we're working in a concrete category,
meaning (loosely) that the objects are ``sets with additional structure''.
Now suppose you're sick of maps and just want to think about elements of these sets.
Well, I won't let you do that since you're reading a category theory chapter,
but I will offer you some advice:
\begin{itemize}
	\ii In $\catname{Set}$, arrows from $\{\ast\}$ to $S$ correspond to elements of $S$.
	\ii In $\catname{Top}$, arrows from $\{\ast\}$ to $X$ correspond to points of $X$.
	\ii In $\catname{Grp}$, arrows from $\ZZ$ to $G$ correspond to elements of $G$.
	\ii In $\catname{CRing}$, arrows from $\ZZ[x]$ to $R$ correspond to elements of $R$.
\end{itemize}
and so on.
So in most concrete categories, you can think of elements as functions from special sets to the set in question.
In each of these cases we call the object in question a \vocab{free object}.

\section{Binary products}
\prototype{$X \times Y$ in most concrete categories is the set-theoretic product.}
The ``universal property'' is a way of describing objects in terms of maps
in such a way that it defines the object up to unique isomorphism
(much the same as the initial and terminal objects).

To show how this works in general, let me give a concrete example.
Suppose I'm in a category -- let's say $\catname{Set}$ for now.
I have two sets $X$ and $Y$, and I want to construct the Cartesian product $X \times Y$ as we know it.
The philosophy of category theory dictates that I should talk about maps only,
and avoid referring to anything about the sets themselves.
How might I do this?

Well, let's think about maps into $X \times Y$.
The key observation is that
\begin{moral}
A function $A \taking f X \times Y$
amounts to a pair of functions $(A \taking g X, A \taking h Y)$.
\end{moral}
Put another way, there is a natural projection map $X \times Y \surjto X$ and $X \times Y \surjto Y$:
\begin{diagram}
	&& X \\
	X \times Y & \ruSurj(2,1)^{\pi_X} & \\
	& \rdSurj(2,1)_{\pi_Y} & Y
\end{diagram}
(We have to do this in terms of projection maps rather than elements,
because category theory forces us to talk about arrows.)
Now how do I add $A$ to this diagram?
The point is that there is a bijection between functions $A \taking f X \times Y$
and pairs $(g,h)$ of functions.
Thus for every pair $A \taking g X$ and $A \taking h Y$ there is a \emph{unique} function
$A \taking f X \times Y$.

But $X \times Y$ is special in that it is ``universal'':
for any \emph{other} set $A$, if you give me functions $A \to X$ and $A \to Y$, I can use it
build a \emph{unique} function $A \to X \times Y$.
Picture:
\begin{diagram}
	&& && X \\
	A & \rDotted~{\exists! f} & X \times Y & \ruTo(4,1)^g \ruSurj(2,1)_{\pi_X} & \\
	& \rdTo(4,1)_h && \rdProj(2,1)^{\pi_Y} & Y
\end{diagram}
We can do this in any general category, defining a so-called product.
\begin{definition}
	Let $X$ and $Y$ be objects in any category $\AA$.
	The \vocab{product} consists of an object $X \times Y$
	and arrows $\pi_X$, $\pi_Y$ to $X$ and $Y$ (thought of as projection).
	We require that for any object $A$ and arrows $A \taking g X$, $A \taking h Y$, there
	is a \emph{unique} function $A \taking f X \times Y$ such that the diagram
	\begin{diagram}
		&& && X \\
		A & \rDotted~{\exists! f} & X \times Y & \ruTo(4,1)^g \ruProj(2,1)_{\pi_X} & \\
		& \rdTo(4,1)_h && \rdProj(2,1)^{\pi_Y} & Y
	\end{diagram}
	commutes.
\end{definition}
\begin{abuse}
	Strictly speaking, the product should consist of \emph{both}
	the object $X \times Y$
	and the projection maps $\pi_X$ and $\pi_Y$.
	However, if $\pi_X$ and $\pi_Y$ are understood,
	then we often use $X \times Y$ to refer to the object,
	and refer to it also as the product.
	\label{abuse:object}
\end{abuse}

Products do not always exist; for example,
take a category with just two objects and no non-identity morphisms.
Nonetheless:
\begin{proposition}[Uniqueness of products]
	When they exist, products are unique up to isomorphism:
	given two products $P_1$ and $P_2$ of $X$ and $Y$
	there is an isomorphism between the two objects.
\end{proposition}
\begin{proof}
	This is very similar to the proof that initial objects are unique up to unique isomorphism.
	Consider two such objects $P_1$ and $P_2$, and the associated projection maps.
	So, we have a diagram
	\begin{diagram}
		&& X && \\
		& \ruProj^{\pi_X^1} & \uProj_{\pi_X^2} & \luProj^{\pi_X^1} & \\
		P_1 & \rTo~f & P_2 & \rTo~g & P_1 \\
		& \rdProj_{\pi_Y^1} & \dProj_{\pi_Y^2} & \ldProj_{\pi_Y^1} & \\
		&& Y &&
	\end{diagram}
	There are unique morphisms $f$ and $g$ between $P_1$ and $P_2$ that
	make the entire diagram commute, according to the universal property.

	On the other hand, look at $g \circ f$ and focus on just the outer square.
	Observe that $g \circ f$ is a map which makes the outer square commute,
	so by the universal property of $P_1$ it is the only one.
	But $\id_{P_1}$ works as well.
	Thus $\id_{P_1} = g \circ f$.
	Similarly, $f \circ g = \id_{P_2}$ so $f$ and $g$ are isomorphisms.
\end{proof}
\begin{abuse}
	Actually, this is not really the morally correct theorem;
	since we've only showed the objects $P_1$ and $P_2$ are isomorphic
	and have not made any assertion about the projection maps.
	But I haven't (and won't) define isomorphism of the entire product,
	and so in what follows if I say ``$P_1$ and $P_2$ are isomorphic''
	I really just mean the objects are isomorphic.
\end{abuse}
\begin{exercise}
	In fact, show the products are unique up to \emph{unique} isomorphism:
	the $f$ and $g$ above are the only isomorphisms between
	the objects $P_1$ and $P_2$.
\end{exercise}

The nice fact about this ``universal property'' mindset
is that we don't have to give explicit constructions; assuming existence,
the ``universal property'' allows us to bypass all this work by saying
``the object with these properties is unique up to unique isomorphism'',
thus we don't need to understand the internal workings of the object
to use its properties.

Of course, that's not to say we can't give concrete examples.
\begin{example}
	[Examples of products]
	\listhack
	\begin{enumerate}[(a)]
		\ii In $\catname{Set}$, the product of two sets
		$X$ and $Y$ is their Cartesian product $X \times Y$.
		\ii In $\catname{Grp}$, the product of $G$, $H$
		is the group product $G \times H$.
		\ii In $\catname{Vect}_k$, the product
		of $V$ and $W$ is $V \oplus W$.
		\ii In $\catname{CRing}$, the product
		of $R$ and $S$ is appropriately the ring product $R \times S$.
		\ii Let $\mathcal P$ be a poset interpreted as a category.
		Then the product of two objects $x$ and $y$
		is the \vocab{greatest lower bound}; for example,
		\begin{itemize}
			\ii If the poset is $(\RR, \le)$ then it's $\min\{x,y\}$.
			\ii If the poset is the subsets
			of a finite set by inclusion,
			then it's $x \cap y$.
			\ii If the poset is the positive integers ordered by division,
			then it's $\gcd(x,y)$.
		\end{itemize}
	\end{enumerate}
\end{example}

Of course, we can define products of more than just one object.
Consider a set of objects $(X_i)_{i \in I}$ in a category $\AA$.
We define a \vocab{cone} on the $X_i$ to be an object $A$
with some ``projection'' maps to each $X_i$.
Then the \vocab{product} is a cone $P$ which is ``universal'' in the same sense as before:
given any other cone $A$ there is a unique map $A \to P$ making the diagram commute.
In short, a product is a ``universal cone''.

The picture of this is
\begin{diagram}
	&& A && \\
	& \ldProj(2,4) \ldProj(1,4) & \dTo~{!\exists f} & \rdProj(2,4) \rdProj(1,4)& \\
	&& P && \\
	& \ldProj \ldProj(1,2) && \rdProj \rdProj(1,2) & \\
	X_1 & X_2 && X_3 & X_4
\end{diagram}
See also \Cref{prob:associative_product}.

One can also do the dual construction to get a \vocab{coproduct}:
given $X$ and $Y$, it's the object $X+Y$
together with maps $X \taking{\iota_X} X+Y$ and $Y \taking{\iota_Y} X+Y$
(that's Greek iota, think inclusion)
such that for any object $A$ and maps $X \taking g A$, $Y \taking h A$
there is a unique $f$ for which
\begin{diagram}
	X &&&& \\
	& \rdTo(1,2)_{\iota_X} \rdTo(3,2)^g && \\
	& X+Y & \rTo~{!\exists f} & A \\
	\ruTo(1,2)^{\iota^Y} && \ruTo(3,2)_h & \\
	Y &&&&
\end{diagram}
commutes.
We'll leave some of the concrete examples as an exercise this time,
for example:
\begin{exercise}
	Describe the coproduct in $\catname{Set}$.
\end{exercise}
Predictable terminology: a coproduct is a universal \vocab{cocone}.

Spoiler alert later on:
this construction can be generalized vastly to so-called ``limits'',
and we'll do so later on.

\section{Monic and epic maps}
The notion of ``injective'' doesn't make sense
in an arbitrary category since arrows need not be functions.
The correct categorical notion is:
\begin{definition}
	A map $X \taking f Y$ is \vocab{monic}
	(or a monomorphism) if for any commutative diagram
	\begin{diagram}
		A & \pile{\rTo^g \\ \rTo_h} & X & \rTo^f Y
	\end{diagram}
	we must have $g = h$.
	In other words, $f \circ g = f \circ h \implies g = h$.
\end{definition}
\begin{ques}
	Verify that in a \emph{concrete} category, injective $\implies$ monic.
\end{ques}
\begin{ques}
	Show that the composition of two monic maps is monic.
\end{ques}

In most but not all situations, the converse is also true.
\begin{exercise}
	Show that in $\catname{Set}$, $\catname{Grp}$, $\catname{CRing}$,
	monic implies injective. (Take $A = \{\ast\}$, $A = \ZZ$, $A = \ZZ[x]$.)
\end{exercise}
More generally, as we said before there are many categories
with a ``free'' object that you can use to think of as elements.
An element of a set is a function $1 \to S$,
and element of a ring is a function $\ZZ[x] \to R$, et cetera.
In all these categories,
the definition of monic literally reads
``$f$ is injective on $\Hom_\AA(A, X)$''.
So in these categories, ``monic'' and ``injective'' coincide.

That said, here is the standard counterexample.
An additive abelian group $G = (G,+)$ is called \emph{divisible}
if for every $x \in G$ and $n \in \ZZ$ there exists $y \in G$ with $ny = x$.
Let $\catname{DivAbGrp}$ be the category of such groups.
\begin{exercise}
	Show that the projection $\QQ \to \QQ/\ZZ$ is monic but not injective.
\end{exercise}

Of course, we can also take the dual notion.
\begin{definition}
	A map $X \taking f Y$ is \vocab{epic}
	(or an epimorphism) if for any commutative diagram
	\begin{diagram}
		X & \rTo^f & Y & \pile{\rTo^g \\ \rTo_h} & A
	\end{diagram}
	we must have $g = h$.
	In other words, $g \circ f = h \circ f \implies g = h$.
\end{definition}

This is kind of like surjectivity, although it's a little farther than last time.
Note that in concrete categories, surjective $\implies$ epic.
\begin{exercise}
	Show that in $\catname{Set}$, $\catname{Grp}$, $\catname{Ab}$, $\catname{Vect}_k$, $\catname{Top}$,
	the notions of epic and surjective coincide.
	(For $\catname{Set}$, take $A = \{0, 1\}$.)
\end{exercise}
However, there are more cases where it fails.
Most notably:
\begin{example}
	[Epic but not surjective]
	\listhack
	\begin{enumerate}[(a)]
		\ii In $\catname{CRing}$, for instance, the inclusion $\ZZ \injto \QQ$ is epic
		(and not surjective)..
		Indeed, if two homomorphisms $\QQ \to A$ agree on
		every integer then they agree everywhere (why?),
		\ii In the category of \emph{Hausdorff} topological spaces
		(every two points have disjoint open neighborhoods),
		in fact epic $\iff$ dense image (like $\QQ \injto \RR$).
	\end{enumerate}
	Thus failures arise when a function $f : X \to Y$ can be determined by just some of the points of $X$.
\end{example}

\section\problemhead

\begin{problem}
	In the category $\catname{Vect}_k$ of $k$-vector spaces
	(for a field $k$),
	what are the initial and terminal objects?
\end{problem}

\begin{dproblem}
	What is the coproduct $X+Y$ in the categories
	$\catname{Set}$, $\catname{Vect}_k$, and a poset?
\end{dproblem}

\begin{problem}
	In any category $\AA$ where all products exist,
	show that \[ (X \times Y) \times Z \cong X \times (Y \times Z) \]
	where $X$, $Y$, $Z$ are arbitrary objects.
	(Here both sides refer to the objects, as in \Cref{abuse:object}.)
	\label{prob:associative_product}
\end{problem}

\begin{problem}
	\gim
	Consider a category $\AA$ with a \vocab{zero object},
	meaning an object which is both initial and terminal.
	Given objects $X$ and $Y$ in $A$,
	prove that the projection $X \times Y \to X$ is epic.
\end{problem}


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{xcolor,listings}
\usepackage[a4paper, total={7in, 10in}]{geometry}
\usepackage[spanish]{babel}

\lstset{
  language=Python,
  backgroundcolor = \color{lightgray},
  showspaces=false,
  numbers=left,
  numberstyle=\tiny,
  commentstyle=\color{gray},
  keywordstyle=\color{red}\ttfamily,
  basicstyle=\ttfamily,
  columns=fullflexible,
  frame=single,
  breaklines=true,
  postbreak=\mbox{\textcolor{black}{$\hookrightarrow$}\space},
}

\begin{document}
\title{%
		\LARGE \textbf{388} \\
        }
\author{
	Nicolas Novalic
}

\section*{A more or less formal solution}

The problem asks to find out x digit numbers for x = (3, .. ,7) whose last 3 digits are 388 (and count them).
\vskip 0.2in
So for every number $x$ it's easy to see that in the result of: $$n \times 10^{(number \ of \ digits \ in \ n)} + n$$ \vskip 0.0in ends with the number n and this result is a multiple by $n$.
\vskip 0.2in
\textbf{\textit{i.e:}} \\

\noindent\fbox{%
    \parbox{\textwidth}{%
      $$8 \times (10 + 1) = 8 \times 10 + 8 \times 1 = 80 + 8 = 88$$
      $$76 \times 101 = 7600 + 76 = 7676$$
	}
}\vskip 0.2in

In particular: $$388 \times 1001 = 388388$$

\vskip 0.2in
So we can formulate it like this:
$$ 388 \times k < 10000000 $$
\vskip 0.2in
From where we need all the $k$'s that verify that $388 \times k$ ends in 388.
\vskip 0.2in
If we divide by 388 and round up we can obtain the range of values that $k$ will take. The result of the division is 25773, so $388 \times k < 10000000$ if $k < 25773$.

\vskip 0.2in
Now, as the prime decomposition of 388 is $2 \times 2 \times 97$, we can write 388 as $4 \times 97$.
\vskip 0.2in
So decomposing 388 and 1000 we can reformulate $388 \times 1000$ = $(4 \times 97) \times (4 \times 250)$ so every 250 $k$'s we have a multiple of 388 that ends with three zeros, and if we add 388 to that number, we have found one of the numbers we are looking for!.
\vskip 0.2in
So now we see how many of this numbers are in the valid range:
$$\frac{25773}{250} = 103$$

So $388 \times (250 \times i)$ for $i = (1, ... , 103)$ are all valid numbers.
\vskip 0.2in
To end up we need to count the solution obtained by: $$388 \times (250 \times 0) = 388$$ \vskip 0.0in that we weren't counting (Note that $388 \times (250 \times 1) = 97000$).
\vskip 0.2in
Adding up, in total theres \textbf{104} numbers that are seven digits or less whose last three digits are 388 in that order.

\section*{Python code to check the solution}

To check out the number obtained before I've written this piece of code as concise as possible:
\vskip 0.2in
\begin{lstlisting}
print (len([ x for x in \
             map(lambda n: n[-3:], \
                 map(str, \
                     [y * 388 for y in range(1, int(10000000/388))])) \
             if '388' in x ]))
\end{lstlisting}
\vskip 0.2in
Basically what I'm doing is:
\begin{itemize}
\item [-] As the smallest 8 number digit is 10000000 we can find out how many multiples of 388 that are less than 10000000 exist by dividing both numbers (line 4).
\item [-] Creating a list of all multiples of 388 in the range 388 and 10000000 (line 4).
\item [-] Using the map function to convert all the elements of this list (which are integers) to strings (line 3).
\item [-] Defining a lambda function which given an element, returns it's last 3 characters (line 2).
\item [-] Using the map function with the previous mentioned lambda function on the list resulting of applying the first map function mentioned before (line 2).
\item [-] Extracting elements from the resulting list of the second map function application, which satisfy that the string '388' is a part of this element (line 1).
\item [-] We can easily note that this returns a list of elements which are all completely equal strings '388'. So we count how many elements are on this list and print the result (line 1).
\end{itemize}
\vskip 0.2in
This is actually just one line of code.

\end{document}


\documentclass[12pt, letterpaper]{report}
\usepackage[margin=1in]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{float}
\usepackage{subfig}
\graphicspath{ {./img/} }
\setlength\parindent{0pt}
\renewcommand\thesection{\Roman{section}.}
\renewcommand{\thesubsection}{\alph{subsection}.}

\title{CS1675 - Assignment 3}
\author{Zachary M. Mattis}

\begin{document}

\maketitle

\section{Problem 1 - Bernoulli Trials}

% A
\subsection{ML Estimate}

\[ \hat{\theta}(x) =  0.65 \]

% B
\subsection{$Beta(\theta | 1,1)$}

\begin{figure}[H]
	\centering
	\includegraphics[width=0.7\columnwidth]{p1b.png}
	\caption{Prior = 1,1}
\end{figure}

% C
\subsection{MAP Estimate}

\[ \textrm{MAP Estimate of } \theta = 0.65 \]

% D
\subsection{$Beta(\theta | 4,2)$}

\begin{figure}[H]
	\centering
	\includegraphics[width=0.7\columnwidth]{p1d.png}
	\caption{Prior = 4,2}
\end{figure}

\[ \textrm{MAP Estimate of } \theta = 0.6538 \]

\section{Problem 2 - Multivariate Gaussian}

% A
\subsection{Gaussian Scatter Plot}

\begin{figure}[H]
	\centering
	\includegraphics[width=0.7\columnwidth]{p2a.png}
	\caption{Gaussian Scatter Plot}
\end{figure}

% B
\subsection{ML Estimation}

\begin{table}[H]
	\centering
	\begin{tabular}{ |l|l| }
		\hline
		3.6377 & 7.8506 \\
		\hline
	\end{tabular}
	\caption{Mean}
\end{table}

\begin{table}[H]
	\centering
	\begin{tabular}{ |l|l| }
		\hline
		3.6414 & 1.0779 \\
		\hline
		1.0779 & 3.7831 \\
		\hline
	\end{tabular}
	\caption{Covariance}
\end{table}

\begin{figure}[H]
	\centering
	\includegraphics[width=0.7\columnwidth]{p2b.png}
	\caption{Gaussian 3-D}
\end{figure}

% C
\subsection{Individual Gaussian}

\begin{figure}[H]
	\centering
	\includegraphics[width=0.7\columnwidth]{p2c.png}
	\caption{Individual Gaussian}
\end{figure}

The first column data of ``gaussian txt" is in blue, while the second is in red.  The corresponding means are 3.6377 and 7.8506.  The corresponding variances are 3.6414 and 3.7831.

% D
\subsection{Multivariate vs. Univariate}

I believe multivariate Gaussian models are a better model than two separate univariate models. Given a multivariate model, it is much easier to view any correlations between the data points as they are directly displayed together. Univariate models need to be interpolated side-by-side and any correlation must be interpreted by the viewer.

\section{Problem 3 - Exponential Distribution}

% A
\subsection{Density Function}

\begin{figure}[H]
	\centering
	\includegraphics[width=0.7\columnwidth]{p3a.png}
	\caption{Exponential Distribution}
\end{figure}

\subsection{ML Estimate}

\[ f(x;\theta)=\frac{1}{\theta}{e}^{\frac{-x}{\theta}}, 0<x<\infty, \theta\in{[0,\infty}] \]
\\
\[ L(\theta)=L\left(\theta;{x}_{1},{x}_{2}...{x}_{n} \right)=\left(\frac{1}{\theta}{e}^{\frac{{-x}_{1}}{\theta}}\right)\left(\frac{1}{\theta}{e}^{\frac{{-x}_{2}}{\theta}}\right)...\left(\frac{1}{\theta}{e}^{\frac{{-x}_{n}}{\theta}} \right)=\frac{1}{{\theta}^{n}}exp\left(\frac{-\sum_{1}^{n}{x}_{i}}{\theta} \right) \]
\\
\[lnL\left(\theta\right)=-\left(n \right)ln\left(\theta\right) -\frac{1}{\theta}\sum_{1}^{n}{x}_{i}, 0<\theta<\infty\]
\\
\[\frac{d\left[lnL\left(\theta\right) \right]}{d\theta}=\frac{-\left(n \right)}{\left(\theta\right)} +\frac{1}{{\theta}^{2}}\sum_{1}^{n}{x}_{i}=0\]
\\
\[\theta=\frac{\sum_{1}^{n}{x}_{i}}{n}x`\]
\\
\[\Theta=\frac{\sum_{1}^{n}{X}_{i}}{n}\]

\end{document}