\documentclass{article}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amssymb}
\usepackage{algorithm}
\usepackage{graphicx}
\usepackage[noend]{algpseudocode}
\usepackage{url}

\newlength\tindent
\setlength{\tindent}{\parindent}
\setlength{\parindent}{0pt}
\renewcommand{\indent}{\hspace*{\tindent}}

\newtheorem{thm}{Theorem}
\newtheorem{cor}{Corollary}[thm]
\newtheorem{lemma}{Lemma}[thm]

\title{Knapsack Problem}
\author{Daniel Braithwaite}

\begin{document}
	\pagenumbering{gobble}
	\maketitle
	\newpage
  	\pagenumbering{arabic}

	\section{Dynamic Knapsack 0-1}
		\subsection{Algorithm}

		 	Given a list of items $item_1,......item_n$, say we have a table T that in position T[i][j] stores the best solution's for packing items $item_1,....item_i$ into a bag with capacity j. We can use the following recurrence

			\begin{align}
				max
				\begin{cases}
					T[i-1][j]\\
					T[i-1][j-items[i].weight] + items[i].value
				\end{cases}
			\end{align}

			\begin{algorithm}
				\begin{algorithmic}[1]
					\Procedure{dnamicKnapsack01}{$capacity, items$}
						\State $T \gets int[items.length][capacity+1]$

						\For{$i = 0, i < items.length, i \gets i+1$}
							\For{$j = 0, j < capacity+1, j \gets j+1$}
								\State Fill out cell based on recurrence
							\EndFor
						\EndFor

						\State $solution \gets [0....0]$ (n zeros)

						\State $i \gets items.length-1$
						\State $j \gets capacity-1$

						\While{$j > 0$} \Comment{while we have available capacity}
							\State Use recurrence to figure out what action we took to get this value
							\If{We took item $i$ to get here}
								\State $j \gets items[i].weight$

								\State $solution[i] \gets 1$
							\EndIf

							\State $i \gets i - 1$
						\EndWhile

					\EndProcedure
				\end{algorithmic}
			\end{algorithm}
		\subsection{Proof}
			We want to work our way towards the recurrence we used in the dynamic programming algorithm. Say we have $n$ items we are trying to put into a bag of capacity $C$. Ether we put the $n^{th}$ item in the bag or we didn't. From this we get the following two sub problems.

			\begin{enumerate}
				\item \textbf{We took the $n^{th}$ item:} Here we are solving the sub problem where we have items $item_1,.....,item_{n-1}$ and bag with capacity $C - item_n.weight$.

				\item \textbf{Didn't take $n^{th}$ item:} Here the sub problem is the items $item_1,.....,item_{n-1}$ and bag with capacity $C$.
			\end{enumerate}

			This observation gives us our recurrence, now we need to show optimal sub structure for both of these cases

			\begin{enumerate}
				\item Say we took the $n^{th}$ item. Let $S$ be set of items taken giving the optimal solution for this problem. Assume for a contradiction that $S - \{item_n\}$ isn't the optimal solution for the problem $item_1,.....,item_{n-1}$ with a bag of capacity $C - item_n.weight$. Let $S^\prime$ be an optimal solution for this sub problem. But then we could take $S^\prime$ and add $n$ back in. This would give us a better solution than $S$ for the whole problem. A contradiction!

				\item Now say we didn't take the $n^{th}$ item. Let $S$ be the optimal selection of items. Assume for a contradiction that $S$ is not the optimal solution for the sub problem $item_1,.....,item_{n-1}$ with capacity $C$. But then we could take a better solution say $S^\prime$, not choose the $n^{th}$ item and have a solution better than $S$. A contradiction!
			\end{enumerate}

		Now we see the Knapsack 0-1 problem as optimal substructure. Now we must prove the algorithm is correct. First we look at filling out the table.\newline

		Fill out the table must be correct as we just proved the problem had optimal substructure using that recurrence. The last thing to show is that recovering the solution from the table is always correct.\newline

		To start this we see that this also uses the recurrence we defined to determine what action was taken. If an item was taken then we mark it as taken in the solution. With ether action we move back in the table to where that solution came from until we have $j$ at 0 which means that we have no more available capacity.

		\subsection{Complexity}
			\subsubsection{Filling Out Table}
				The main part of the algorithm cost is here. The table is of size $weight * numItems$ and no matter the input data the whole table has to be filed out. The cost of using the recurrence is $O(1)$. Therefore the cost for filling out the table is

				\begin{align}
					\theta(nC)
				\end{align}

			\subsubsection{Finding Solution}
				The cost of this involves moving through the table and recovering the decisions at each point. Finding what decision was made is $O(1)$. At worst case you would be moving one square in the table at a time. Therefore finding the solution has cost

				\begin{align}
					O(n + C)
				\end{align}

			\subsubsection{Overall Cost}
				Combing the cost of the previous sections we see this algorithm has the following cost

				\begin{align}
					\theta(nC)
				\end{align}

		\subsection{Testing}
			I plotted the lines for the best, average and worst case data this algorithm could encounter. I defined the worst case data to be as follows.

			\begin{enumerate}
				\item \textbf{Best Case} is when reconstructing the solution we can just do it all in one step. So for this to happen we set all the item values and weights to be the same.

				\item \textbf{Worst Case} is when we take as many steps through the table as possible. To get this we set all the weights to be 1.

				\item \textbf{Average Case} is just randomly generated data
			\end{enumerate}

			\begin{figure}[h]
				\vspace{3mm}
				\begin{center}
					\includegraphics[scale=0.4]{Dynamic.png}
				\end{center}
			\end{figure}

			\break

			No matter the input for this algorithm the dynamic programming table will have to be filled. The only thing that will cause a difference will be how long it will take to recover the solution. However this cost is minimal compared to the cost of computing the table. So for some input size $n$ if we have best, worst or average case data the number of steps will be almost identical for all of them. This is why you can only see one line, as they are all overlapping. All the lines fit perfect with the theoretical.

			\break

	\section{Brute Force Knapsack 0-N}
		\subsection{Algorithm}
			\begin{algorithm}
				\begin{algorithmic}[1]
					\Procedure{bruteforceKnapsack01}{$capacity, items$}
						\State $w, b \gets [0,.....,0]$ \Comment{number of entries equals items.length}

						\While{true}
							\State $w[0] \gets w[0] + 1$

							\For{index i in w} \Comment{Handle when we are taking to much of an item}
								\If{$w[i] > item_i.mutiplicity$}
									\State $w[i] \gets 0$
									\State $w[i+1] \gets w[i+1] + 1$
								\EndIf
							\EndFor

							\If{we have taken all of the last item} \Comment{this is true if we have enumerated all possible solutions}
								\State break
							\EndIf

							\State $weight \gets$ weight of possible solution w

							\If{$weight \leqslant capacity$}
								\State Store solution in b if the value is better
							\EndIf
						\EndWhile

						\Return b
					\EndProcedure

				\end{algorithmic}
			\end{algorithm}
		\subsection{Proof}
			The brute force algorithm works by enumerating all the possible options and storing the one with the best value if its weight is less or equal to the capacity
		\subsection{Complexity}
			This algorithm has to enumerate all the possible combinations of items. No matter what the input data is (best, worst, average case) it will still have to enumerate all the solutions as it has no way of being able to tell if it has found the best one except for checking the all the remaining ones. We get the complexity to be

			\begin{align}
				\theta(2^n)
			\end{align}

		\subsection{Testing}
			There is no best, worst or average case, as discussed in \textbf{2.3}. As such we only graphed the average case and the theoretical line. When graphing this we only ran it on very small data sets as it is a very inefficient algorithm.

			\begin{figure}[h]
				\vspace{3mm}
				\begin{center}
					\includegraphics[scale=0.4]{BruteForce.png}
				\end{center}
			\end{figure}

			As you can see in the graph the average case line matches up perfectly with the theoretical line.

	\section{Problem Conversion 0-N to 0-1}
		The following 0-N algorithms will convert there 0-N knapsack problems to 0-1 problems. Here we will detail the algorithm for doing this with the cost. Rather than actually producing a new list of items and working with that we will create a mapping. Say our item list has size $n^\prime$ and this list represents $n$ items. Then our mapping take numbers in the range 0 to n and map them to numbers between 0 and $n^\prime$.

		\break

		\subsection{Algorithm}
			\begin{algorithm}
				\begin{algorithmic}[1]
					\Procedure{createMapping}{$items$}
						\State $mapping \gets []$

						\State $i \gets 0, j \gets 1, k \gets 0$
						\While{$k < items.length$}
							\State itemMapping.map(i to k)

							\If{$j = items[k].multiplicity$}
								\State $j \gets 0$
								\State $k \gets k + 1$
							\EndIf

							\State $i \gets i + 1$
							\State $j \gets j + 1$
						\EndWhile

						\Return mapping
					\EndProcedure
				\end{algorithmic}
			\end{algorithm}
		\subsection{Proof}
			For each item passed in has a multiplicity (i.e. how many of that item we have) if we examine the algorithm we can clearly that it will create a mapping to an item item.multiplicity times. From this we see that the algorithm is correct.

		\subsection{Cost}
			We can clearly see that the loop is going to iterate exactly once for each of the $n$ items making the cost

			\begin{align}
				\theta(n)
			\end{align}

		\break

	\section{Dynamic Knapsack 0-N}
		\subsection{Algorithm}
			Here we can use our previous 0-1 knapsack algorithm to solve this one. We just need to create an algorithm to convert a 0-N knapsack problem to a 0-1 problem and then once solved convert the solution back. The key difference being that if we want to access item k then really what we are doing is accessing the item k is mapped to ($mapping[k]$).

			\begin{algorithm}
				\begin{algorithmic}[1]
					\Procedure{dnamicKnapsack0N}{$capacity, items$}
						\State $mapping \gets createMapping(items)$

						\State $T \gets int[mapping.length][capacity+1]$

						\For{$i = 0, i < mapping.length, i \gets i+1$}
							\For{$j = 0, j < capacity+1, j \gets j+1$}
								\State Fill out cell based on recurrence
							\EndFor
						\EndFor

						\State $solution \gets [0....0]$ \Comment{items.length number of 0's}

						\State $i \gets items.length-1$
						\State $j \gets capacity-1$

						\While{$j > 0$} \Comment{while we have available capacity}
							\State Use recurrence to figure out what action we took to get this value
							\If{We took item $i$ to get here}
								\State $j \gets items[mapping[i]].weight$

								\State $solution[mapping[i]]$++
							\EndIf

							\State $i \gets i - 1$
						\EndWhile
					\EndProcedure
				\end{algorithmic}
			\end{algorithm}
		\subsection{Proof}
			We see this algorithm works by converting the 0-N knapsack problem into a 0-1 knapsack problem and uses the 0-1 dynamic algorithm to solve it. The solution is then converted back to 0-N format.\newline

			We know that the conversion to 0-1 format is correct, we also know that the dynamic algorithm for solving the 0-1 knapsack is correct. So all we need prove is that the conversion of the solution back to 0-N is correct.\newline

			By inspecting the algorithm we see that our solution has an entry for each of the original items. We are using the recurrence to determine if an item was taken as we traverse the table. If an item was taken then we need to figure out what one of the original items it maps to and increment our count of that original item in the solution. We know we are able to do this correctly because our mapping is correct.
		\subsection{Complexity}
		There are two parts for this algorithm we will explore the complexity for each of them and then we can come up with an overall complexity

		\subsubsection{Solving The 0-1 Problem}
			We already know the complexity of this and it is

			\begin{align}
				\theta(nC)
			\end{align}

		\subsubsection{Recovering Solution}
			The complexity of recovering the solution is the same as it was in the 0-1 dynamic solution. We only changed how the items where accessed

			\begin{align}
				\O(n + C)
			\end{align}

		\subsubsection{Overall Complexity}
			The dominating cost is still that  of solving the 0-1 Knapsack problem giving this algorithm

			\begin{align}
				\theta(nC)
			\end{align}

			\break
		\subsection{Testing}
			\textbf{Please Note:} For the testing of all the knapsack 0-N algorithms we used data where none of the items had a multiplicity over 1 (for simplicity). This \textbf{wouldn't} effect the results as the conversion will still run and the rest of the algorithm will run for the same amount of time had there been less input items but the same total n items\newline

			We tested this data on the same type of best, average and worst case data as we did for the 0-1 dynamic knapsack and as expected got the same result.

			\begin{figure}[h]
				\vspace{3mm}
				\begin{center}
					\includegraphics[scale=0.4]{Dynamic0N.png}
				\end{center}
			\end{figure}

			We can see that all the lines match up perfectly with the theoretical data

	\break

	\section{Graph Search Knapsack 0-N}
		To start with we want to convert the 0-N problem to a 0-1 problem. This is so we can use a similar idea to the dynamic programming table\footnote{\url{http://courses.csail.mit.edu/6.006/fall11/rec/rec21_knapsack.pdf}}. Each node will be represented by the following triple $<i, j, value, previous>$ where $0 \leqslant i \leqslant numItems$ and $0 \leqslant j \leqslant capacity$ . The i and j mean the node contains the best solution for the items 0 to i-1 for a knapsack of capacity j.\newline\newline

		A node is connected to at most two others. Let $n$ be a node. The possibility's are ether we take item n.i or we don't. In the first case n is linked to a node where i = n.i + 1, and j = n.j + item[i] (as long as n.j + item[i] is $\leqslant$ capacity. In the second case n is linked to the node where i = n.i + 1, and j = n.j. As we are building the graph we want to keep track of the current best value of each node, along with that we keep track of the previous node that was used to get this value.

		For simplicity we will sometimes refer to a node as just <i,j>. Then we can refer to the value and previous node as <i,j>.value and <i,j>.previous

		\subsection{Algorithm}
			\begin{algorithm}
				\begin{algorithmic}[1]
					\Procedure{graphKnapsack0N}{$capacity, items$}
						\State $items^\prime \gets convertTo01Problem(items)$
						\State $fringe \gets []$
						\State add root node $<0, 0, 0, null>$

						\While{fringe is not empty}
							\State $current \gets node from fringe$
							\State remove current from fringe

							\If{$current.i + 1 > items^\prime$}
								\State return to top of loop
							\EndIf

							\State get node $<current.i + 1,current.j>$ if it doesn't exist add it to fringe.

							\State update $<current.i + 1,current.j>$ if our current path to the node is better

							\If{there is enough available weight to take item j}
								\State get node $<current.i + 1,current.j + items ^\prime[i].weight>$ if it doesn't exist add it to fringe
								\State update that node if there is a better path to it
							\EndIf

						\EndWhile

						\State $solution \gets [0,.....,0]$  \Comment{same length as items}
					\EndProcedure
				\end{algorithmic}
			\end{algorithm}

		\break
		\subsection{Proof}
			We will need to prove two things for this algorithm to be correct. First being that our method of generating the graph will explore all the possibility's. Second we need to show that we can recover the best path through the graph, thus giving us the optimal solution and finally show that the part of the algorithm that recovers the solution also correctly converts it back to a 0-N solution.	\newline

			We think of nodes as representing a sub problem. For example the node $<2, 5>$ is saying we have considered the items 0 and 1, and that we have used up 5 of our total capacity. From here we have two choices ether we take item 2 (taking item 2 also depends on weather we have enough spare capacity) or we don't take item $i$. Meaning there is an edge between $<2, 5>$ and the nodes $<3, 5>$ and $<3, 5 + 1>$ (assuming the weight of item 2 is 1). If we make this more general and apply it to some node $<i, j>$ then clearly we are searching all the possible combinations. So if we store the node with the best value as we are building this binary tree then by the end we clearly will have a node that holds the best possible value\newline

			Now we have a node that can tell us the best value possible when trying to put all these items into a back with a known capacity. But what we are really interested in is the combination of items. As we are building the graph when we link two nodes with an edge, say  $n$ to $n^\prime$, we examine the current value of $n^\prime$ and see if the value of $n$ (plus the value of item $n.i$ we are taking it) is greater than it. If it is then we store $n$ as the previous node in $n^\prime$. We do this for all nodes so at the end we can just step back through the tree starting from the best node giving us the solution.\newline

			Finally we look at the part of the code that recovers the solution. This starts at the node in the graph that has the best value stepping back though the nodes using the previous value. At any iteration in the loop we have a node $n$ and the one before it $n_p$. We compare $n.j$ and $n_p.j$ if they are equal then we didn't take an item so just set $n$ to $n_p$. If they are not equal then we update the solution by using our mapping $solution[mapping[n_p.i]]$++. We increment the item $n_p.i$ as moving from $n_p$ to $n$ meant we where acting on the item $n_p.i$. This clearly gives us a 0-N solution that is correct.

		\subsection{Complexity}
			This algorithm has two components that we will explore separately.

			\subsubsection{Constructing The Graph}
				We can think of constructing the graph as traversing all the nodes in the graph. Which at worst case there will be as many nodes as there are cells in the dynamic programming table. However the key thing to note is that this is at worst, there often will be less nodes than this. Giving us the following complexity where n is the number of items (that is not the number of items in the input array but the sum of there multiplicity's) and C is the total capacity of the bag

				\begin{align}
					O(nC)
				\end{align}

			\subsubsection{Finding The Solution}
				At any node we know that it is connected to at most 2 nodes, there for what we generated is a binary tree. We also know the two following things. That there is going to be at most n + 1 layers (one for every possible value of i, $0 \leqslant i \leqslant n$). The second thing we know that as we are recovering the solution we are only stepping back through the tree. Therefore at worst we will have to step though the height of the tree giving us the complexity

				\begin{align}
					O(n+1)
				\end{align}

			\subsubsection{Overall Complexity}
				The dominating cost of the algorithm is that of constructing the graph so we get the algorithm complexity to be

				\begin{align}
					O(nC)
				\end{align}

		\subsection{Testing}
			The graph below displays the best, average and worst case for the graph search algorithm, we defined these as follows.

			\begin{enumerate}
				\item \textbf{Best Case} is when we can construct the graph with only having one layer. If we set all the items to have a capacity that will consume all available space we can get this.

				\item \textbf{Worst Case} is when we set each of the items to have a different weight, so for example for a set of 3 items we have $item_1.weight = 1, item_2.weight = 2,item_3.weight = 3$. This will give us the most possible nodes without much overlap. If we chose to do the same worst case data as we did for the dynamic programming we would do better than average as there would be a lot of overlap between nodes.

				\item \textbf{Average Case} is just randomly generated data
			\end{enumerate}

			\begin{figure}[h]
				\vspace{3mm}
				\begin{center}
					\includegraphics[scale=0.4]{Graph0N.png}
				\end{center}
			\end{figure}

			As we see in the graph this is much more varied than the other algorithms. The best case is very different from the average and worst case which are quite close together. However they are all less than the theoretical line which I graphed as $n*C + n + 1$ as this is the maximum number of steps the algorithm can take.

	\section{Graph Search vs Dynamic}
		If we examine the order costs of the algorithm we would expect that the graph search would out perform the dynamic algorithm, or atleast perform the same as it. The graph search algorithm has a complexity of $O(nC)$ meaning it is bounded from above by $nC$. Compared to the complexity of the dynamic algorithm $\theta(nC)$ meaning it is bounded from above and below by $nC$.\newline

		If we examine the algorithms more closely we can also see this as noted in the dynamic algorithm complexity section the algorithm will always have to fill out the entire table no matter the input data. We also noted previously that in the graph search it was only worst case where we would have to traverse $nC$ number of nodes. \newline

		We can conclude from this that the graph search algorithm will perform at most $nC$ steps where as the dynamic algorithm will always perform $nC$ steps.\newline

		We can also examine the cost of reconstructing the solution for each of the algorithms. Even here the graph search algorithm is faster, with a complexity of $O(n + 1)$ compared to the dynamic algorithm where recovering the solution is $O(n + C)$. \newline

		With this in mind we compare the best case of the dynamic algorithm and the worst case of the graph search.

		\begin{figure}[h]
			\vspace{3mm}
			\begin{center}
				\includegraphics[scale=0.4]{DynamicGraphComparason.png}
			\end{center}
		\end{figure}

		As we can see in the graph the graph search algorithm clearly out performs the dynamic algorithm.

\end{document}


\subsection{Discussions}\label{subsec::discuss}

In this section, we discuss some potential improvements for \name.

\paragraph{Improving verification time.} As shown in the experiments above, the verification time in \name is already fast in practice compared to other systems, yet it can be further improved by 1-2 orders of magnitude.

Within the verification of \name, most of the time (more than 95\% in the evaluations above) is spent on our zkVPD protocols using bilinear pairings. In our current protocol, we use the pairing-based zkVPD both for the input layer and for the masking polynomials $g_i, R_i$ in each intermediate layer. Although the masking polynomials are small, the verification of our zkVPD still requires $O(s_i)$ pairings per layer for $g_i$, which is asymptotically the same as the input layer. For example, for the SHA256 circuit with 12 layers, the zkVPD verification of each $g_i$ is around 46ms, $\frac{1}{16}$ of the total verification time.

However, there are many zkVPD candidates for these masking polynomials. Recall that the size of $g_i$ is only $O(s_i)$, logarithmic on the size of the circuit. We could use any zkVPD with up to linear commitment size, prover time, proof size and verification time while still maintaining the asymptotic complexity of \name. The only property we need is zero knowledge. Therefore, we can replace our pairing-based zkVPD with any of the zero knowledge proof systems we compare with as a black-box. \textsf{Ligero} and \textsf{Aurora} are of particular interest as their verification requires no cryptographic operations. If we use the black-box of these two systems for the zkVPD of $g_i, R_i$, the prover time and proof size would be affected minimally, and the verification time would be improved by almost $d$ times, as only the zkVPD of the input layer requires pairings after the change. This is a 1-2 orders-of-magnitude improvement depending on the depth of the circuit. In addition, it also removes the trusted setup in the zkVPD for the masking polynomials. We plan to integrate this approach into our system when the implementations of \textsf{Ligero} and \textsf{Aurora} become available.

\paragraph{Removing trusted setup.} After the change above, the only place that requires trusted setup is the zkVPD for the input layer. However, replacing our pairing-based zkVPD with other systems without trusted setup may affect the succinctness of our verification time on structured circuits. For example, using \textsf{Ligero}, \textsf{Bulletproof} and \textsf{Aurora} as a black-box would increase the verification time to $O(n)$, and using \textsf{Hyrax} would increase the proof size and verification time to $O(\sqrt{n})$. Using \textsf{libSTARK} may keep the same complexity, as polynomial evaluation is a special function with short description, but the prover time and memory usage is high in STARK as shown in the experiments. Designing an efficient zkVPD protocol with logarithmic proof size and verification time without trusted setup is left as an interesting future work and we believe this paper serves as an important step towards the goal of efficient succinct zero knowledge proof without trusted setup.


\section{\textbf{FibTaskTest}}

\subsection{Particular Case (problem)}
The problem is calculating the sum of the first $n$ Fibonacci numbers,
in parallel. \\

Note: this exercise is exactly the same as the homonym from chapter
14; we believe the authors of the book made a mistake and ended up
with this repetition. Still we are repeating here for the sake of
completeness.

\subsection{Solution}
The solution merely follows the recursive definition of the Fibonacci
sequence, whose base cases are 1 (for first and second elements of the
sequence). Each recursive call of the function creates a new task
(\C{Future} from java concurrency library). Below the code of the main
function: \\

\begin{lstlisting}[style=numbers]
  public Integer call() {
    try {
      if (arg > 2) {
        Future<Integer> left = exec.submit(new FibTask(arg-1));
        Future<Integer> right = exec.submit(new FibTask(arg-2));
        return left.get() + right.get();
      } else {
        return 1;
      }
    } catch (Exception ex) {
      ex.printStackTrace();
      return 1;
    }
  }
\end{lstlisting}
\hfill

Note that \C{Future} class represents asynchronous calculations, and
that the \C{get} method is blocking (waits for calculation to be
complete); hence we would effectively create a  tree of tasks until we
reach the base cases and start evaluating bottom up.

\subsection{Experiment Description}
The experiment merely request the calculation of the sum up to a known
number (16), and asserts the expected result (987).

\subsection{Observations and Interpretations}
The test works as expected in both 2 and 24 cores machines. Below a
sample output: \\

\begin{verbatim}
[oraadm@gdlaa008 orig]$ junit steal.FibTaskTest
.run

Time: 0.176

OK (1 test)
\end{verbatim}
\hfill


\documentclass{article}
\usepackage{amsmath}

\usepackage{igor_macros}

%%No paragraph break on emty lines
\catcode10=10   \catcode13=10

%\def\ee{\end{equation}}

\title {Deduction of Hamilton-Jakoby equation}
\author {Igor Bukanov}
\date { 08 February 1999 }

\begin{document}

\begin{abstract}
I present strict deduction of Hamilton-Jakoby eqution and corresponding
approach for solving equation of motion. I decided to write this due to
uneasy feeling that Landau-Lifshic Mechanics left to me. In any case it
was  useful exercise.
\end{abstract}

\maketitle

\section{Definitions and deduction}
Consider the standard expression for the action of a classical system
given by
\eqgl{
\l{S-def}
S = \int_0^T L[\vec{\dot{q}}(t), \vec{q}(t), t] dt, \\
\l{init-cond}
\vec{q}(0) = \vec{q_0}, \, \vec{q}(T) = \vec{q_1},  \\
\vec{q} \equiv q_1 \dots q_N, \notag
}
where $N$ stands for the number of degree of freedom.

The corresponding Lagrange equations of motion are
\eqnl{L-eqn}{
\frac{\partial L}{\partial q^i}
    = \frac{d}{dt}\frac{\partial L}{\partial \dot{q}^i}
}

A solution of (\ref{L-eqn}) that satisfy the initial conditions
\rf{init-cond} maybe formally written in the following form,
\eqgl{
\vec{q} = \vec{q}(t, T, \vec{q}_0, \vec{q}_1), \, \notag \\
\vec{q}(0, T, \vec{q}_0, \vec{q}_1) = \vec{q}_0, \, \l{formal-solution} \\
\vec{q}(T, T, \vec{q}_0, \vec{q}_1) = \vec{q}_1 . \notag
}

The form \rf{formal-solution} provides minimal or at least extremal action,
that is a function of initial conditions \rf{init-cond} and time interval $T$,

\eqnl{S-min-def}{
S_{min} \equiv S_{min}(T, \vec{q_0}, \vec{q_1})
    = \int_0^T L[\vec{\dot{q}}(t, T, \vec{q}_0, \vec{q}_1),
                 \vec{q}(t, T, \vec{q}_0, \vec{q}_1), t] dt
}

With \rf{S-min-def} in hands I can start deduction of a equation for $S_{min}$
itself. In the following I assume $S \equiv S_{min}$.

\par
I calculate time derivative for it as a derivative of parametric
integral ,
\eqnl{der-S-a}{
\frac{\partial S}{\partial T}
    = \left.L[\vec{\dot{q}}, \vec{q}, t)\right|_{t=T}
    + \int_0^T \frac{\partial}{\partial T} L[\vec{\dot{q}}, \vec{q}, t] dt .
}

To father transform \rf{der-S-a} I consider a general expression of the form
\eqn{
F(a) =
\int_0^T dt
    \frac{\partial}{\partial a} L[\vec{\dot{q}}(t, a), \vec{q}(t, a), t]
}
with some arbitrary parameter $a$. I have
\eqn{
F(a) =
\int_0^T dt \left\lbrace
\frac{\partial L}{\partial\vec{\dot{q}}}\frac{\partial\vec{\dot{q}}}{\partial a}
+ \frac{\partial L}{\partial\vec{q}}\frac{\partial\vec{q}}{\partial a}
\right\rbrace .
}

But
\eqn{
\frac{\partial\vec{\dot{q}}}{\partial a}
    \equiv \frac{\partial}{\partial a}\frac{\partial}{\partial t}\vec{q}(t, a)
    = \frac{\partial}{\partial t}\frac{\partial}{\partial a}\vec{q}(t, a)
}
under the assumtion that $\vec{q}(t, a)$ is smoth enough to permit interchange
of the differention order. This gives
\eqn{
\begin{split}
F(a) &=
\int_0^T dt \left\lbrace
\frac{\partial L}{\partial\vec{\dot{q}}}\frac{\partial}{\partial t}
\frac{\partial\vec{q}}{\partial a}
+ \frac{\partial L}{\partial\vec{q}}\frac{\partial\vec{q}}{\partial a}
\right\rbrace . \\
&=
\int_0^T dt \left\lbrace
\frac{\partial}{\partial t}\left[
\frac{\partial L}{\partial\vec{\dot{q}}}
\frac{\partial\vec{q}}{\partial a}\right]
+ \frac{\partial L}{\partial\vec{q}}\frac{\partial\vec{q}}{\partial a}
- \frac{\partial}{\partial t}\frac{\partial L}{\partial\vec{\dot{q}}}
    \frac{\partial\vec{q}}{\partial a}
\right\rbrace . \\
&=
\left.
\frac{\partial L}{\partial\vec{\dot{q}}}
\frac{\partial\vec{q}}{\partial a}\right|_{t=0}^T
+
\int_0^T dt \frac{\partial\vec{q}}{\partial a} \left\lbrace
\frac{\partial L}{\partial\vec{q}}
- \frac{\partial}{\partial t}\frac{\partial L}{\partial\vec{\dot{q}}}
\right\rbrace . \\
\end{split}
}

Assuming now that $\vec{q}$ satisfies the Lagrange equations \rf{L-eqn},
the second term in the last expression gives 0 and I have
\eqgl{
\l{param-derivative}
\int_0^T \frac{\partial}{\partial a} L[\vec{\dot{q}}(t, a), \vec{q}(t, a), t] dt
=
\left.
\frac{\partial L}{\partial\vec{\dot{q}}}
\frac{\partial\vec{q}}{\partial a}\right|_{t=0}^T ,
\\
\vec{\dot{q}}(t, a) \equiv
\left.\frac{\partial\vec{q}}{\partial t}\right|_a \notag
}

When $a \equiv T$ the last equation gives for \rf{der-S-a}
\eqn{
\int_0^T dt
    \frac{\partial}{\partial T} L[\vec{\dot{q}}(t, T), \vec{q}(t, T), t]
=
\left.
\frac{\partial L}{\partial\vec{\dot{q}}}
\frac{\partial\vec{q}}{\partial T}\right|_{t=0}^T ,
}
and finally I have for \rf{der-S-a}
\eqnl{der-S-b}{
\frac{\partial S(T, \vec{q}_0, \vec{q}_1)}{\partial T}
    = \left. L[\vec{\dot{q}}, \vec{q}, t]\right|_{t=T}
    + \left.\frac{\partial L}{\partial\vec{\dot{q}}}
      \frac{\partial\vec{q}}{\partial T}\right|_{t=0}^T .
}

Now I take into account that
\eqnl{L-at-second-edge}{
\begin{split}
\left. L[\vec{\dot{q}}, \vec{q}, t]\right|_{t=T}
&\equiv \left. L[\vec{\dot{q}}(t, T, \vec{q}_0, \vec{q}_1),
 \vec{q}(t, T, \vec{q}_0, \vec{q}_1), t]
\right|_{t=T}
\\
&= L[\vec{\dot{q}}(T, T, \vec{q}_0, \vec{q}_1),
    \vec{q}(T, T, \vec{q}_0, \vec{q}_1), T]
\\
&= L[\vec{\dot{q}}(T, T, \vec{q}_0, \vec{q}_1), \vec{q}_1, T]
\end{split}
}

from the boundary condition \rf{formal-solution},
$\vec{q}(T, T, \vec{q}_0, \vec{q}_1) = \vec{q}_1$.
\par

From the same condition it is also follows
\eqn{
\frac{\partial}{\partial T}
\vec{q}(t=T, T, \vec{q}_0, \vec{q}_1)
=
\frac{\partial \vec{q}_1}{\partial T}
=
\left.\frac{\partial}{\partial t}
\vec{q}(t, T, \vec{q}_0, \vec{q}_1)\right|_{t=T}
+
\left.\frac{\partial}{\partial T}
\vec{q}(t, T, \vec{q}_0, \vec{q}_1)\right|_{t=T} ,
}
but $\frac{\partial \vec{q}_1}{\partial T} \equiv 0$ because
the boundary conditions themself are not depend on $T$, so

\eqn{
0 =
\left.\frac{\partial}{\partial t}
\vec{q}(t, T, \vec{q}_0, \vec{q}_1)\right|_{t=T}
+
\left.\frac{\partial}{\partial T}
\vec{q}(t, T, \vec{q}_0, \vec{q}_1)\right|_{t=T} .
}
In the same way
\eqn{
0 \equiv \frac{\partial \vec{q}_0}{\partial T}
= \frac{\partial}{\partial T}
\vec{q}(t=0, T, \vec{q}_0, \vec{q}_1)
=
\left.\frac{\partial}{\partial T}
\vec{q}(t, T, \vec{q}_0, \vec{q}_1)\right|_{t=0} .
}
This gives for \rf{der-S-b}
\eqnl{der-S-c}{
\begin{split}
\frac{\partial S(T, \vec{q}_0, \vec{q}_1)}{\partial T}
&=
L[\vec{\dot{q}}(T, T, \vec{q}_0, \vec{q}_1), \vec{q}_1, T]
- \left.\frac{\partial L}{\partial\vec{\dot{q}}}
  \frac{\partial}{\partial t}
  \vec{q}(t, T, \vec{q}_0, \vec{q}_1)\right|_{t=T}
\\ &\equiv
L[\vec{\dot{q}}(T, T, \vec{q}_0, \vec{q}_1), \vec{q}_1, T]
- \vec{\dot{q}}(T, T, \vec{q}_0, \vec{q}_1)
\frac{\partial }{\partial\vec{\dot{q}}}
L[\vec{\dot{q}}, \vec{q}_1, T]

\end{split}
}

Now I consider ${\partial S}/{\partial \vec{q}_1}$.
\eqn{
\frac{\partial S(T, \vec{q}_0, \vec{q}_1)}{\partial \vec{q}_1}
 = \int_0^T \frac{\partial}{\partial \vec{q}_1}
     L[\vec{\dot{q}}(t, T, \vec{q}_0, \vec{q}_1),
                 \vec{q}(t, T, \vec{q}_0, \vec{q}_1), t] dt

}

From \rf{param-derivative} with $a \equiv \vec{q}_1$ I get
\eqn{
\frac{\partial S(T, \vec{q}_0, \vec{q}_1)}{\partial \vec{q}_1}
=
\left.\frac{\partial L}{\partial\dot{q^i}}
\frac{\partial q^i}{\partial \vec{q}_1}\right|_{t=0}^T
\equiv
\left.\frac{\partial L}{\partial\dot{q^i}}
\frac{\partial}{\partial \vec{q}_1}q^i(t, T, \vec{q}_0, \vec{q}_1)
\right|_{t=0}^T .
}

Using the boundary conditions in \rf{formal-solution} one more time I have
\eqn{
\left.Y(t)\frac{\partial}{\partial q^j_1}q^i(t, T, \vec{q}_0, \vec{q}_1)
\right|_{t=0}^T
=
Y(T)\frac{\partial q^i_1}{\partial q^j_1}
    - Y(0)\frac{\partial q^i_0}{\partial q^j_1}
=
Y(T)\delta^i_j - 0 = Y(T)\delta^i_j .
}

Thus
\eqnl{der-S-d}{
\frac{\partial S(T, \vec{q}_0, \vec{q}_1)}{\partial \vec{q}_1}
=
\left.\frac{\partial L}{\partial\vec{\dot{q}}}\right|_{t=T} ,
}

Equations \rf{der-S-c} and \rf{der-S-d} together form a closed system
\eqnl{S-der-system}{
\left\lbrace
\begin{split}
\frac{\partial S(T, \vec{q}_0, \vec{q}_1)}{\partial T}
&=
L[\vec{\dot{q}}(T, T, \vec{q}_0, \vec{q}_1), \vec{q}_1, T]
- \dot{q^i}(T, T, \vec{q}_0, \vec{q}_1)
    \frac{\partial }{\partial\dot{q^i}}
    L[\vec{\dot{q}}, \vec{q}_1, T]

\\
\frac{\partial S(T, \vec{q}_0, \vec{q}_1)}{\partial \vec{q}_1}
&= \frac{\partial}{\partial\vec{\dot{q}}}
    L[\vec{\dot{q}}(T, T, \vec{q}_0, \vec{q}_1), \vec{q}_0, \vec{q}_1)]
\end{split}
\right.
}

or simply
\eqnl{S-der-system-b}{
\left\lbrace
\begin{split}
\frac{\partial S(T, \vec{q}_1)}{\partial T}
&=
L[\vec{\dot{q}}, \vec{q}_1, T]
- \dot{q}^i \partial_{\dot{q}^i}L [\vec{\dot{q}}, \vec{q}_1, T]
\\
\frac{\partial S(T, \vec{q}_1)}{\partial q^i_1}
&=
\partial_{\dot{q}^i}L[\vec{\dot{q}}, \vec{q}_1, T]
\end{split}
\right. ,
}
where I skiped a dependence of $S$ on $\vec{q}_0$, which reflects the fact
that a general solution of \rf{S-der-system-b} will include some constants
$C_\alpha$, that assume dependence on $\vec{q}_0$

From a pure algebraic point of view I can use the second equation
in \rf{S-der-system-b} to expres $\vec{\dot{q}}$ as a function of
$\vec{q}_1$, $T$ and $\frac{\partial S}{\partial \vec{q}_1}$:
\eqn{
\vec{\dot{q}} =
    \vec{\dot{q}}(\vec{q}_1, T, \frac{\partial S}{\partial \vec{q}_1})
}

[ Note: I skipped completely the issue about the existence of such solution.]

This gives from \rf{S-der-system-b} the single partial differencal equation
for $S$ as a function of $\vec{q}_1$ and $T$,
\eqnl{S-eqn}{
\begin{split}
\frac{\partial S(T, \vec{q}_1)}{\partial T}
= &
L[\vec{\dot{q}}(\vec{q}_1, T,
    \frac{\partial S(T, \vec{q}_1)}{\partial \vec{q}_1}),
    \vec{q}_1, T]
\\
& - \dot{q}^i(\vec{q}_1, T,
        \frac{\partial S(T, \vec{q}_1)}{\partial \vec{q}_1})
 \frac{\partial S(T, \vec{q}_1)}{\partial q^i_1}
\end{split}
}

The equation \rf{S-eqn} is called Hamilton-Jakoby equation.
It is the first order partial non-linear differential equation.
In short I can write it as
\eqnl{JH-eqn}{
\frac{\partial S(T, \vec{q})}{\partial T}
=
L[\vec{\dot{q}}, \vec{q}, T]
 - \frac{\partial S(T, \vec{q})}{\partial q^i} \dot{q}^i
}

where $\vec{\dot{q}} = \vec{\dot{q}}(\partial_{\vec{q}}S, T, q)$
can be considered as a symbol representing a function
 obtained from the solution of the algebraic system
\eqnl{JH-qdot}{
\frac{\partial S(T, \vec{q})}{\partial q^i}
=
\partial_{\dot{q}^i}L[\vec{\dot{q}}, \vec{q}, T]
}

\par

Now comes technically more difficult in my opinion part to
deduce the solution of the equation of motion \rf{L-eqn} from
the solution of the JH-equation \rf{JH-eqn}.

\par

As for a first-oder partial differncial equation for $S$ in $N+1$ space,
the general solution of \rf{JH-eqn} can be written as function
of $T$, $\vec{q}$ and $N + 1$ constants $\vec{\alpha}$ and $\alpha_0$:
\eqn{
    S = \tilde{S}(T, \vec{q}, \vec{\alpha}) + \alpha_0,
}
where $\alpha_0$ is written explicitly as additive constant.
It is possible because $S$ is present in \rf{JH-eqn} only via
its patial derivatives $\partial_T S$ and $\partial_{\vec{q}} S$.

\par
In the following the value of $\alpha_0$ is not important and for
this reason I simply assume that
\eqnl{S-is-T-q-alpha}{
    S = S(T, \vec{q}, \vec{\alpha}).
}

I define
\eqnl{beta-definition}{
    \vec{\beta}(T, \vec{q}, \vec{\alpha}) = \frac{\partial S}{\partial \alpha}.
}

This definition also can be seen as introduction of new coordinates
$T$, $\vec{\beta}$, $\vec{\alpha}$ instead of the set
$T$, $\vec{q}$, $\vec{\alpha}$. I will assume in the following that
\rf{beta-definition} can be resolved with respect to $\vec{q}$.
[Warning: under what condition this can be true?] I.e. I can assume
that
\eqn{
    \vec{q} = \vec{q}(T, \vec{\beta}, \vec{\alpha})
        \equiv \vec{q}(T, \frac{\partial S}{\partial \alpha}, \vec{\alpha}).
}

With this definitions/assumesiones in hands I differenciate \rf{JH-eqn}
with respect to $\alpha$,
\eqn{
\begin{split}
\frac{\partial^2 S}{\partial \vec{\alpha} \partial T}
 & = \frac{\partial^2 S}{\partial T \partial \vec{\alpha}}
\\ &
\equiv \frac{\partial \vec{\beta}}{\partial T}
= \frac{\partial}{\partial \vec{\alpha}}
    L[\vec{\dot{q}}(\partial_{\vec{q}}S(T, \vec{q}, \vec{\alpha}), T, q),
        \vec{q}, T]
-
\frac{\partial}{\partial \vec{\alpha}}\left\lbrace
 \frac{\partial S(T, \vec{q}, \vec{\alpha})}{\partial q^i} \dot{q}^i
\right\rbrace
\\ &
= \frac{\partial L}{\partial \dot{q}^i}
\frac{\partial \dot{q^i}}{\partial \vec{\alpha}}
-\frac{\partial^2 S}{\partial \vec{\alpha} \partial q^i}\dot{q}^i
-\frac{\partial S}{\partial q^i}
    \frac{\partial \dot{q}^i}{\partial \vec{\alpha}}

=
\frac{\partial S}{\partial q^i}
    \frac{\partial \dot{q}^i}{\partial \vec{\alpha}}
-\frac{\partial^2 S}{\partial \vec{\alpha} \partial q^i}\dot{q}^i
-\frac{\partial S}{\partial q^i}
    \frac{\partial \dot{q}^i}{\partial \vec{\alpha}}
\end{split},
}
or
\eqnl{beta-over-T}{
\left.\frac{\partial \vec{\beta}}{\partial T}\right|_{\alpha\vec{q}}
\; = \; -\frac{\partial^2 S}{\partial \vec{\alpha} \partial q^i}\dot{q}^i
\; = \;
-\left.\frac{\partial\vec\beta}{\partial q^i}
  \right|_{\alpha T}\dot{q}^i
}

where I used \rf{JH-qdot} to replace
$\partial_{\dot{q}^i}L[\vec{\dot{q}}, \vec{q}, T]$ by
$\partial_{q^i}S$.

\par

I must note here that $\dot{\vec{q}}$ in \rf{beta-over-T} is just a symbol
that denote a function defined as the solution of
\rf{JH-qdot}, i.e. it just represents some function of $T$, $\beta$,
$\alpha$:
\eqn{
\vec{\dot{q}}
 =
\vec{\dot{q}}(\partial_{\vec{q}}S(\vec{q}, T, \vec{\alpha}), T, q)
=
\vec{\dot{q}}(\partial_{\vec{q}}S(\vec{q}(T, \vec{\beta}, \vec{\alpha}),
  T, \vec{\alpha}), T, q(T, \vec{\beta}, \vec{\alpha}))
}

\par
Now I return to the interpretation of \rf{beta-definition} as a non-explicit
definition of function $\vec{q}(\vec{\beta}, T, \vec{\alpha})$. I will show
that with fixed $\vec{\beta}$, $\vec{\alpha}$ this function $\vec{q}$ as
function of $T$ satisfies the equation of motion \rf{L-eqn}. Indeed, I have
for
\eqn{
\left.\frac{\partial{q}}{\partial{T}}\right|_{\beta\alpha}
\; \equiv \;
\vec{q}_T
}
from $\vec{\beta} = \vec{\beta}(\vec{q}, T, \vec{\alpha})$:

\eqnl{q-dot-q-T-deduction}{
\begin{split}
0 & =
\left.\frac{\partial\vec{\beta}}{\partial T}
(\vec{q}, T, \vec{\alpha})\right|_{\alpha\beta}
\; = \;
\left.\frac{\partial\vec{\beta}}{\partial q^i}\right|_{\alpha T}
\left.\frac{\partial q^i}{\partial T}\right|_{\alpha\beta}
+
\left.\frac{\partial\vec{\beta}}{\partial T}
\right|_{\alpha\vec{q}}
\\ & =
\left.\frac{\partial\vec{\beta}}{\partial q^i}\right|_{\alpha T}
q^i_T
+
\left.\frac{\partial\vec{\beta}}{\partial T}
\right|_{\alpha\vec{q}}
\; = \;
\left.\frac{\partial\vec{\beta}}{\partial q^i}\right|_{\alpha T}
q^i_T
-\left.\frac{\partial\vec\beta}{\partial q^i}
  \right|_{\alpha T}q^i_T\dot{q}^i
\\ & =
\left.\frac{\partial\vec{\beta}}{\partial q^i}\right|_{\alpha T}
\left(q^i_T - \dot{q}^i\right)
\end{split}
}

\par
Assuming that the matrix
$\frac{\partial\vec{\beta}}{\partial q^i}
\equiv \frac{\partial\beta^j}{\partial q^i}$ has non-zero determinant
[ I need to refer to a proper mathematics for exact reasons ]
I multiply \rf{q-dot-q-T-deduction} by
$\left(\frac{\partial\beta^j}{\partial q^i}\right)^{-1}$ which gives
\eqnl{q-dot-is-q-T}{
\dot{q}^i = q^i_T
\equiv \left.\frac{\partial{q^i}}{\partial{T}}\right|_{\alpha\beta} .
}

That is, an abstract symbol $\dot{\vec{q}}$ is equal to the partial derivative
$\vec{q}_T$. This in turns means from \rf{JH-qdot} that $\vec{q}_T$ satisfies
\eqnl{JH-qdot-alpha}{
\frac{\partial S(T, \vec{q}, \vec{\alpha})}{\partial q^i}
=
\partial_{q_T^i}L[\vec{q}_T, \vec{q}, T].
}

I differenciate the last equation over T to get
\eqn{
\begin{split}
\frac{d}{d T}\partial_{q_T^i}L[\vec{q}_T, \vec{q}, T]
& \equiv
\left.\frac{\partial}{\partial T}\right|_{\alpha \vec{\beta}}
\partial_{q_T^i}L[\vec{q}_T, \vec{q}, T]
\; = \;
 \left.\frac{\partial}{\partial T}\right|_{\alpha\vec{\beta}}
  \left.\frac{\partial S(T, \vec{q}, \vec{\alpha})}{\partial q^i}
    \right|_{\alpha T}
\\
& = \left.\frac{\partial}{\partial T}\right|_{\alpha\vec{q}}
  \left.\frac{\partial S(T, \vec{q}, \vec{\alpha})}{\partial q^i}
    \right|_{\alpha T}
+
\left.\frac{\partial}{\partial q^j}\right|_{\alpha T}
  \left.\frac{\partial S(T, \vec{q}, \vec{\alpha})}{\partial q^i}
    \right|_{\alpha T}
  \left.\frac{\partial q^j}{\partial T}\right|_{\alpha \vec{\beta}}
\\
& =  \left.\frac{\partial}{\partial q^i}\right|_{\alpha T}
  \left\{L[\vec{\dot{q}}, \vec{q}, T]
 - \frac{\partial S(T, \vec{q}, \vec{\alpha})}{\partial q^j} \dot{q}^j
    \right\}
\\
& \phantom{ = }
+
\left.\frac{\partial}{\partial q^j}\right|_{\alpha T}
  \left.\frac{\partial S(T, \vec{q}, \vec{\alpha})}{\partial q^i}
    \right|_{\alpha T}
  q^j_T
\end{split}
}

due to \rf{JH-eqn}, thus

\eqn{
\begin{split}
\frac{d}{d T}\partial_{q_T^i}L[\vec{q}_T, \vec{q}, T]
& =  \left.\frac{\partial}{\partial q^i}\right|_{\alpha T}
  \left\{L[\vec{q_T}, \vec{q}, T]
 - \frac{\partial S(T, \vec{q}, \vec{\alpha})}{\partial q^j} q_T^j
    \right\}
\\
& \phantom{ = }

+
\left.\frac{\partial}{\partial q^j}\right|_{\alpha T}
  \left.\frac{\partial S(T, \vec{q}, \vec{\alpha})}{\partial q^i}
    \right|_{\alpha T}
  q^j_T
\\
& =  \left.\frac{\partial}{\partial q^i}\right|_{\alpha T}
  L[\vec{q_T}, \vec{q}, T]
 - \frac{\partial^2 S(T, \vec{q}, \vec{\alpha})}{\partial q^i \partial q^j}
    q_T^j
 -  \frac{\partial S(T, \vec{q}, \vec{\alpha})}{\partial q^j}
    \frac{\partial q_T^j} {\partial q^i }
\\
& \phantom{ = }
+
\frac{\partial^2 S(T, \vec{q}, \vec{\alpha})}{\partial q^j \partial q^i}
  q^j_T

\\
& =  \left.\frac{\partial}{\partial q^i}\right|_{\alpha T}
  L[\vec{q_T}, \vec{q}, T]
 -  \frac{\partial S(T, \vec{q}, \vec{\alpha})}{\partial q^j}
    \frac{\partial q_T^j} {\partial q^i }

\\
& =
\left.\frac{\partial}{\partial q^i}\right|_{\alpha \vec{q}_T}
  L[\vec{q_T}, \vec{q}, T]

+
\left.\frac{\partial}{\partial q_T^j}\right|_{\alpha \vec{q}}
  L[\vec{q_T}, \vec{q}, T]
\left.\frac{\partial q_T^j}{\partial q^i}\right|_{\alpha T}
\\
& \phantom{ = }

 -  \frac{\partial S(T, \vec{q}, \vec{\alpha})}{\partial q^j}
    \frac{\partial q_T^j} {\partial q^i }.

\end{split}
}

Using \rf{JH-qdot-alpha} one more time I finally have
\eqn{
\begin{split}
\frac{d}{d T}\partial_{q_T^i}L[\vec{q}_T, \vec{q}, T]
& =
\left.\frac{\partial}{\partial q^i}\right|_{\alpha \vec{q}_T}
  L[\vec{q_T}, \vec{q}, T]

+  \frac{\partial S(T, \vec{q}, \vec{\alpha})}{\partial q^j}
    \frac{\partial q_T^j} {\partial q^i }.
\\
& \phantom{ = }
 -  \frac{\partial S(T, \vec{q}, \vec{\alpha})}{\partial q^j}
    \frac{\partial q_T^j} {\partial q^i }.

\end{split}
}

or simply
\eqn{
\frac{d}{d T}\partial_{q_T^i}L[\vec{q}_T, \vec{q}, T]
\; = \;
\frac{\partial}{\partial q^i} L[\vec{q_T}, \vec{q}, T].
}

Thus $\vec{q}(T, \vec{\beta}, \vec{\alpha})$ defined in inexplicit way
via \rf{beta-definition} is a solution of equations of motion \rf{L-eqn}
that depends on $2N$ constants $\alpha_i$, $\beta_i$, $i=1..N$

\par

\section{Bottom line}

In short, I can describe the Hamilton-Jakoby method as following procedure.

\begin{enumerate}
\item
Given the Lagrangian $L=L(\dot{\vec{q}}, \vec{q}, T)$ express $\dot{\vec{q}}$
as a function of $\partial_{\dot{\vec{q}}}L$, $q$ and~$T$:
\eqn{
\dot{\vec{q}} = \dot{\vec{q}}
    (\frac{\partial L}{\partial \dot{\vec{q}}}, \vec{q}, T)
}

\item
Substitude $\partial_{\dot{\vec{q}}}L$ by $\partial_{\vec{q}}S(T, \vec{q})$
in the expression for $\dot{\vec{q}}$ and find a general solution
of the Hamilton-Jakoby equation \rf{JH-eqn} for $S(T, \vec{q})$:

\eqn{
\partial_T S(T, \vec{q}) =
L[\vec{\dot{q}}(\partial_{\vec{q}}S, \vec{q}, T), \vec{q}, T]
 - \partial_{q^i} S(T, \vec{q}) \dot{q}^i(\partial_{\vec{q}}S, \vec{q}, T).
}

\item
Present the general solution in the following form:
\eqn{
S = S(T, \vec{q}, \vec{alpha}) + \alpha_0,
}
where $\vec{alpha})$ and $\alpha_0$ are $N+1$ arbitrary constants.

\item

From $beta^i \equiv \partial_{\alpha_i} S(T, \vec{q}, \vec{\alpha})$ express
$\vec{q}$ as a function of $\vec{\alpha}$, $\vec{\beta}$ and~$T$:
\eqn{
\vec{q} = \vec{q}(\vec{\alpha}, \vec{\beta}, T).
}

\item

Use definitions $\vec{q_0} = \vec{q}(\vec{\alpha}, \vec{\beta}, T_0)$,
$\vec{v_0} = \partial_T \vec{q}(\vec{\alpha}, \vec{\beta}, T_0)$ to express
$q$ via initial conditions:
\eqn{
\vec{q} = \vec{q}(\vec{q_0}, \vec{v_0}, T).
}

\end{enumerate}

And that is it.

\end{document}


\documentclass[11pt]{article}
\usepackage{amsmath,amssymb,graphicx}
\usepackage{../setspace}
\addtolength{\textwidth}{1.5in}
\addtolength{\hoffset}{-1in}
\addtolength{\textheight}{1.5in}
\addtolength{\voffset}{-1in}

\title{STAT3401: Lab exercises concerning measures of distance}
\author{Paul Hewson}
\date{11th January 2007}
\usepackage{/usr/share/R/share/texmf/Sweave}
\begin{document}
\setlength{\parindent}{0pt}
\setlength{\parskip}{12pt}
\sffamily
\maketitle

\section{Mahalanobis distance and multivariate normality}

\begin{Schunk}
\begin{Sinput}
> USA.mah.dist <- mahalanobis(USArrests, center = mean(USArrests),
+     cov = var(USArrests))
> hist(USA.mah.dist, freq = FALSE)
> curve(dchisq(x, df = 4), add = TRUE, col = "red")
\end{Sinput}
\end{Schunk}

or consider $qq$ plots:

\begin{Schunk}
\begin{Sinput}
> n <- 50
> p <- 4
> qqplot(USA.mah.dist, qchisq(ppoints(n), p))
\end{Sinput}
\end{Schunk}

\begin{itemize}
\item Do you think that the USArrests data can be considered to be multivariate normal.   If so, repeat this with some other data, such as the simulated multivariate normal data you generated in week 1, or any other data we have met (e.g. iris data).
\item For any data you consider, compare your findings on multivariate normality with what you find when examining univariate normality of either the margins or the linear combinations
\end{itemize}

More advanced work: there are some who think that a beta distribution should be used.   There is a function \texttt{qqbetaM} in a file in the portal.   Copy this into your workspace, \texttt{source} the function and see whether you think it makes a difference.

\begin{Schunk}
\begin{Sinput}
> source("qqbetaM.R")
> qqbetaM(USA.mah.dist, 4)
\end{Sinput}
\end{Schunk}

Compare the qqplots you obtain from this function with qqplots from the $\chi^{2}$ based quantiles.   Do you have any data where your impression of normality may be altered by a change in the comparison?   From your reading material, when might it matter whether you use a beta distribution or a $\chi^{2}$?

\section{Gower's distance}

This is quite an important exercise!   Make sure you are happy calculating Gower's distance.  Use the \texttt{class07.csv} data in the portal, and calculate the distance between a few individuals by hand.   Then use \texttt{daisy} in \texttt{library(cluster)} to calcuate it in R (it's a slightly fiddly function to use).

\end{document}


\section{The homotopy image of a map}\label{chap:image}
\subsection{The universal property of the image of a map}

\begin{defn}
Let $f:A\to X$ and $g:B\to X$ be maps. We define
\begin{equation*}
\mathrm{hom}_X(f,g)\defeq\sm{h:A\to B}f\htpy g\circ h.
\end{equation*}
\end{defn}

\begin{rmk}
In other words, a term $(h,H):\mathrm{hom}_X(f,g)$ consists of a map $h:A\to B$ equipped with a homotopy $H:f\htpy g\circ h$ witnessing that the triangle
\begin{equation*}
\begin{tikzcd}[column sep=tiny]
A \arrow[rr,"h"] \arrow[dr,swap,"f"] & & B \arrow[dl,"g"] \\
& X
\end{tikzcd}
\end{equation*}
commutes. Recall from \cref{ex:triangle_fib} that the type $\mathrm{hom}_X(f,g)$ is equivalent to the type
\begin{equation*}
\prd{x:X}\fib{f}{x}\to\fib{g}{x}.
\end{equation*}
\end{rmk}

\begin{lem}
For any $f:A\to X$ and any embedding\index{embedding} $m:B\to X$, the type $\mathrm{hom}_X(f,m)$ is a proposition.
\end{lem}

\begin{proof}
Since propositions are closed under equivalences by \cref{lem:prop_equiv}, it suffices to show that the type
\begin{equation*}
\prd{x:X}\fib{f}{x}\to\fib{m}{x},
\end{equation*}
is a proposition. Recall from \cref{cor:prop_emb} that a map is an embedding if and only if its fibers are propositions.
Thus we see that the type $\prd{x:X}\fib{f}{x}\to\fib{m}{x}$ is a product of propositions, so it is a proposition by \cref{thm:trunc_pi}.
\end{proof}

\begin{defn}
Consider a commuting triangle
\begin{equation*}
\begin{tikzcd}[column sep=small]
A \arrow[rr,"i"] \arrow[dr,swap,"f"] & & B \arrow[dl,"m"] \\
& X
\end{tikzcd}
\end{equation*}
with $I:f\htpy m\circ i$, and where $m$ is an embedding\index{embedding}.
We say that $m$ has the \define{universal property of the image of $f$}\index{universal property!of the image|textit} if the map
\begin{equation*}
(i,I)^\ast : \mathrm{hom}_X(m,m')\to\mathrm{hom}_X(f,m')
\end{equation*}
defined by $(i,I)^\ast(h,H)\defeq (h\circ i,\ct{I}{(i\cdot H)})$,
is an equivalence for every embedding $m':B'\to X$.
\end{defn}

\begin{rmk}
Since $\mathrm{hom}_X(f,m)$ is a proposition for every $f:A\to X$ and every embedding $m:B\to X$, it follows by \cref{ex:prop_equiv} that the universal property of the image of $f$ is equivalent to the property that the implication
\begin{equation*}
\mathrm{hom}_X(f,m')\to\mathrm{hom}_X(m,m')
\end{equation*}
holds for every embedding $m':B'\to X$.
\end{rmk}

The homotopy image can be used in many important constructions. In this lecture we discuss two applications: the propositional truncation, and set quotients.

\subsection{The propositional truncation}
Note that embeddings into the unit type are just propositions. To see this, note that
\begin{align*}
\sm{A:\UU}{f:A\to\unit}\isemb(f)
& \eqvsym \sm{A:\UU}\isemb(\const_\ttt) \\
& \eqvsym \sm{A:\UU}\prd{x:\unit}\isprop(\fib{\const_\ttt}{x}) \\
& \eqvsym \sm{A:\UU}\isprop(\fib{\const_\ttt}{\ttt}) \\
& \eqvsym \sm{A:\UU}\isprop(A).
\end{align*}
Therefore, the universal property of the image of the map $A\to\unit$ is a proposition $P$ satisfying the universal property of the propositional truncation:

\begin{defn}
Let $A$ be a type, and let $P$ be a proposition that comes equipped with a map $\eta:A\to P$. We say that $\eta:A\to P$ satisfies the \define{universal property of propositional truncation}\index{universal property!of propositional truncation|textit} if for every proposition $Q$, the precomposition map
\begin{equation*}
\blank\circ\eta:(P\to Q)\to (A\to Q)
\end{equation*}
is an equivalence.
\end{defn}

\begin{thm}
Consider a commuting triangle
\begin{equation*}
\begin{tikzcd}[column sep=small]
A \arrow[rr,"i"] \arrow[dr,swap,"f"] & & B \arrow[dl,"m"] \\
& X
\end{tikzcd}
\end{equation*}
with $I:f\htpy m\circ i$, and where $m$ is an embedding. The following are equivalent:
\begin{enumerate}
\item $m$ satisfies the universal property of the image of $f$.
\item for each $x:X$, the proposition $\fib{m}{x}$ satisfies the universal property of the propositional truncation of $\fib{f}{x}$.
\end{enumerate}
\end{thm}

Note that, given a family of propositions $P$ over a type $A$, the type $\sm{a:A}P(a)$ isn't necessarily a proposition. Instead, we think of $\sm{a:A}P(a)$ of the \emph{subtype} of $A$ containing the terms that satisfies $P$. Using the propositional truncation we can assert that there \emph{exists} a term in $A$ that satisfies $P$ without requiring one to construct it.

\begin{defn}
Let $P:A\to \prop$ be a family of propositions over a type $A$. Then we define
\begin{equation*}
\exists_{(a:A)}P(a)\defeq \brck{\sm{a:A}P(a)}.
\end{equation*}
\end{defn}

Similarly, we can define the disjunction of two propositions $P$ and $Q$ to be the \emph{proposition} $\brck{P+Q}$, which clearly satisfies the universal property of disjunction\footnote{Alternatively, we have shown in \cref{ex:join_propositions} that the join $\join{P}{Q}$ also is a proposition that satisfies the universal property of disjunction.}. In \cref{table:logic} we give an overview of the logical connectives on propositions.

\begin{table}
\caption{\label{table:logic}Logic in type theory}
\begin{center}
\begin{tabular}{ll}
\toprule
\emph{Logical connective} & \emph{Interpretation in HoTT} \\
\midrule
$\top$ & $\unit$ \\
$\bot$ & $\emptyt$ \\
$P\land Q$ & $P\times Q$ \\
$P\lor Q$ & $\brck{P+Q}$ \\
$P\to Q$ & $P\to Q$ \\
$P\leftrightarrow Q$ & $\eqv{P}{Q}$ \\
$\neg P$ & $P\to\emptyt$ \\
$\forall x.P(x)$ & $\prd{x:A}P(x)$ \\
$\exists x.P(x)$ & $\brck{\sm{x:A}P(x)}$ \\
$\exists! x.P(x)$ & $\iscontr(\sm{x:A}P(x))$ \\
\bottomrule
\end{tabular}
\end{center}
\end{table}

\subsection{Constructing the propositional truncation}
Although technically it is not necessary to construct the propositional truncation before constructing the image of a map, we do so because the construction is simpler in this special case, and yet contains most of the essential ideas.

\begin{lem}\label{lem:extend_join_prop}
Suppose $f:A\to P$, where $A$ is any type, and $P$ is a proposition.
Then the map
\begin{equation*}
(\join{A}{B}\to P)\to (B\to P)
\end{equation*}
given by $h\mapsto h\circ \inr$ is an equivalence, for any type $B$.
\end{lem}

\begin{proof}
Since both types are propositions by \cref{thm:trunc_pi} it suffices to construct a map
\begin{equation*}
(B\to P)\to (\join{A}{B}\to P).
\end{equation*}
Let $g:B\to P$. Then the square
\begin{equation*}
\begin{tikzcd}
A\times B \arrow[r,"\proj 2"] \arrow[d,swap,"\proj 1"] & B \arrow[d,"g"] \\
A \arrow[r,swap,"f"] & P
\end{tikzcd}
\end{equation*}
commutes since $P$ is a proposition. Therefore we obtain a map $\join{A}{B}\to P$ by the universal property of the join.
\end{proof}

The idea of the construction of the propositional truncation is that if we are given a map $f:A\to P$, where $P$ is a proposition, then it extends uniquely along $\inr:A\to \join{A}{A}$ to a map $\join{A}{A}\to P$. This extension again extends uniquely along $\inr:\join{A}{A}\to \join{A}{(\join{A}{A})}$ to a map $\join{A}{(\join{A}{A})}\to P$ and so on, resulting in a diagram of the form
\begin{equation*}
\begin{tikzcd}
A \arrow[dr] \arrow[r,"\inr"] & \join{A}{A} \arrow[d,densely dotted] \arrow[r,"\inr"] & \join{A}{(\join{A}{A})} \arrow[dl,densely dotted] \arrow[r,"\inr"] & \cdots \arrow[dll,densely dotted,bend left=10] \\
& P
\end{tikzcd}
\end{equation*}

\begin{defn}
The \define{join powers} $A^{\ast n}$ of a type $X$ are defined by
\begin{align*}
A^{\ast 0} & \defeq \emptyt \\
A^{\ast 1} & \defeq A \\
A^{\ast (n+1)} & \defeq \join{A}{A^{\ast n}}.
\end{align*}
Furthermore, we define $A^{\ast\infty}$ to be the sequential colimit of the type sequence
\begin{equation*}
\begin{tikzcd}
A^{\ast 0} \arrow[r] & A^{\ast 1} \arrow[r,"\inr"] & A^{\ast 2} \arrow[r,"\inr"] & \cdots.
\end{tikzcd}
\end{equation*}
\end{defn}

Our goal is now to show that $A^{\ast\infty}$ is a proposition and satisfies the universal property of the propositional truncation.

\begin{lem}
Consider a type sequence
\begin{equation*}
\begin{tikzcd}
A_0 \arrow[r,"f_0"] & A_1 \arrow[r,"f_1"] & A_2 \arrow[r,"f_2"] & \cdots
\end{tikzcd}
\end{equation*}
with sequential colimit $A_\infty$, and let $P$ be a proposition. Then the map
\begin{equation*}
\seqin^\ast: (A_\infty\to P)\to \Big(\prd{n:\N}A_n\to P\Big)
\end{equation*}
given by $h\mapsto \lam{n}(h\circ \seqin_n)$ is an equivalence.
\end{lem}

\begin{proof}
By the universal property of sequential colimits established in \cref{thm:sequential_up} we obtain that $\coconemap$ is an equivalence. Note that we have a commuting triangle
\begin{equation*}
\begin{tikzcd}
& P^{A_\infty} \arrow[dl,swap,"\coconemap"] \arrow[dr,"\seqin^\ast"] \\
\cocone(P) \arrow[rr,swap,"\proj 1"] & & \Big(\prd{n:\N}A_n\to P\Big).
\end{tikzcd}
\end{equation*}
Note that for any $g:\prd{n:\N}A_n\to P$ the type
\begin{equation*}
\prd{n:\N} g_n\htpy g_{n+1}\circ f_n
\end{equation*}
is a product of contractible types, since $P$ is a proposition. Therefore it is contractible by \cref{thm:funext_wkfunext}, and it follows by \cref{ex:proj_fiber} that the projection is an equivalence. We conclude by the 3-for-2 property of equivalences (\cref{ex:3_for_2}) that $\seqin^\ast$ is an equivalence.
\end{proof}

\begin{lem}\label{lem:infjp_up}
Let $A$ be a type, and let $P$ be a proposition. Then the function
\begin{equation*}
\blank\circ \seqin_0: (A^{\ast\infty}\to P)\to (A\to P)
\end{equation*}
is an equivalence.
\end{lem}

\begin{proof}
We have the commuting triangle
\begin{equation*}
\begin{tikzcd}
&[-3em] P^{A^{\ast\infty}} \arrow[dl,swap,"\seqin^\ast"] \arrow[dr,"\blank\circ\seqin_0"] \\
\Big(\prd{n:\N}A^{\ast n} \to P\Big) \arrow[rr,swap,"\lam{h}h_0"] & & P^A.
\end{tikzcd}
\end{equation*}
Therefore it suffices to show that the bottom map is an equivalence. Since this is a map between propositions, it suffices to construct a map in the converse direction. Let $f:A\to P$. We will construct a term of type
\begin{equation*}
\prd{n:\N}A^{\ast n} \to P
\end{equation*}
by induction on $n:\N$. The base case is trivial. Given a map $g:A^{\ast n}\to P$, we obtain a map $g:A^{\ast(n+1)}\to P$ by \cref{lem:extend_join_prop}.
\end{proof}

\begin{lem}\label{lem:isprop_infjp}
The type $A^{\ast\infty}$ is a proposition for any type $A$.
\end{lem}

\begin{proof}
By \cref{cor:contr_prop} it suffices to show that $A^{\ast\infty}\to \iscontr(A^{\ast\infty})$, and by \cref{lem:infjp_up} it suffices to show that
\begin{equation*}
A\to \iscontr(A^{\ast\infty}),
\end{equation*}
because $\iscontr(A^{\ast\infty})$ is a proposition by \cref{ex:isprop_istrunc}.

Let $x:A$. To see that $A^{\ast\infty}$ is contractible it suffices by \cref{ex:seqcolim_contr} to show that $\inr:A^{\ast n}\to A^{\ast(n+1)}$ is homotopic to the constant function $\const_{\inl(x)}$. However, we get a homotopy $\const_{\inl(x)}\htpy \inr$ immediately from the path constructor $\glue$.
\end{proof}

\begin{thm}
For any type $A$ there is a type $\brck{A}$ that comes equipped with a map $\eta:A\to \brck{A}$, and satisfies the universal property of propositional truncation.
\end{thm}

\begin{proof}
Let $A$ be a type. Then we define $\brck{A}\defeq A^{\ast\infty}$, and we define $\eta\defeq \seqin_0:A\to A^{\ast\infty}$. Then $\brck{A}$ is a proposition by \cref{lem:isprop_infjp}, and $\eta:A\to \brck{A}$ satisfies the universal property of propositional truncation by \cref{lem:infjp_up}.
\end{proof}

\subsection{The construction of the image of a map}
The image of a map $f:A\to X$ can be defined using the propositional truncation:
\begin{defn}
For any map $f:A\to X$ we define the \define{image}\index{image|textbf} of $f$ to be the type
\begin{equation*}
\im(f) \defeq \sm{x:X}\brck{\fib{f}{x}}
\end{equation*}
and we define the \define{image inclusion} to be the projection $\proj 1 :\im(f)\to X$.
\end{defn}
However, the construction of the fiberwise join in \cref{ex:fib_join} suggests that we can also define the image of $f$ as the infinite join power $f^{\ast\infty}$, where we repeatedly take the fiberwise join of $f$ with itself. The reasons for defining the image in this way are twofold: we will be able to use this construction to show that the set-quotients of a small type are small, and second, we many interesting types appear in this construction.

\begin{lem}
Consider a map $f:A\to X$, an embedding $m:U\to X$, and $h:\mathrm{hom}_X(f,m)$. Then the map
\begin{equation*}
\mathrm{hom}_X(\join{f}{g},m)\to \mathrm{hom}_X(g,m)
\end{equation*}
is an equivalence for any $g:B\to X$.
\end{lem}

\begin{proof}
Note that both types are propositions, so any equivalence can be used to prove the claim. Thus, we simply calculate
\begin{align*}
\mathrm{hom}_X(\join{f}{g},m) & \eqvsym \prd{x:X}\fib{\join{f}{g}}{x}\to \fib{m}{x} \\
& \eqvsym \prd{x:X}\join{\fib{f}{x}}{\fib{g}{x}}\to\fib{m}{x} \\
& \eqvsym \prd{x:X}\fib{g}{x}\to\fib{m}{x} \\
& \eqvsym \mathrm{hom}_X(g,m).
\end{align*}
The first equivalence holds by \cref{ex:triangle_fib}; the second equivalence holds by \cref{ex:fib_join}, also using \cref{ex:equiv_precomp,lem:postcomp_equiv} where we established that that pre- and postcomposing by an equivalence is an equivalence; the third equivalence holds by \cref{lem:extend_join_prop,lem:postcomp_equiv}; the last equivalence again holds by \cref{ex:triangle_fib}.
\end{proof}

For the construction of the image of $f:A\to X$ we observe that if we are given an embedding $m:U\to X$ and a map $(i,I):\mathrm{hom}_X(f,m)$, then $(i,I)$ extends uniquely along $\inr:A\to \join[X]{A}{A}$ to a map $\mathrm{hom}_X(\join{f}{f},m)$. This extension again extends uniquely along $\inr:\join[X]{A}{A}\to \join[X]{A}{(\join[X]{A}{A})}$ to a map $\mathrm{hom}_X(\join{f}{(\join{f}{f})},m)$ and so on, resulting in a diagram of the form
\begin{equation*}
\begin{tikzcd}
A \arrow[dr] \arrow[r,"\inr"] & \join[X]{A}{A} \arrow[d,densely dotted] \arrow[r,"\inr"] & \join[X]{A}{(\join[X]{A}{A})} \arrow[dl,densely dotted] \arrow[r,"\inr"] & \cdots \arrow[dll,densely dotted,bend left=10] \\
& U
\end{tikzcd}
\end{equation*}

\begin{defn}
Suppose $f:A\to X$ is a map. Then we define the \define{fiberwise join powers}
\begin{equation*}
f^{\ast n}:A_X^{\ast n} X.
\end{equation*}
\end{defn}

\begin{constr}
Note that the operation $(B,g)\mapsto (\join[X]{A}{B},\join{f}{g})$ defines an endomorphism on the type
\begin{equation*}
\sm{B:\UU}B\to X.
\end{equation*}
We also have $(\emptyt,\ind{\emptyt})$ and $(A,f)$ of this type. For $n\geq 1$ we define
\begin{align*}
A_X^{\ast (n+1)} & \defeq \join[X]{A}{A_X^{\ast n}} \\
f^{\ast (n+1)} & \defeq \join{f}{f^{\ast n}}.\qedhere
\end{align*}
\end{constr}

\begin{defn}
We define $A_X^{\ast\infty}$ to be the sequential colimit of the type sequence
\begin{equation*}
\begin{tikzcd}
A_X^{\ast 0} \arrow[r] & A_X^{\ast 1} \arrow[r,"\inr"] & A_X^{\ast 2} \arrow[r,"\inr"] & \cdots.
\end{tikzcd}
\end{equation*}
Since we have a cocone
\begin{equation*}
\begin{tikzcd}
A_X^{\ast 0} \arrow[r] \arrow[dr,swap,"f^{\ast 0}" near start] & A_X^{\ast 1} \arrow[r,"\inr"] \arrow[d,swap,"f^{\ast 1}" near start] & A_X^{\ast 2} \arrow[r,"\inr"] \arrow[dl,swap,"f^{\ast 2}" xshift=1ex] & \cdots \arrow[dll,bend left=10] \\
& X
\end{tikzcd}
\end{equation*}
we also obtain a map $f^{\ast\infty}:A_X^{\ast\infty}\to X$ by the universal property of $A_X^{\ast\infty}$.
\end{defn}

\begin{lem}\label{lem:finfjp_up}
Let $f:A\to X$ be a map, and let $m:U\to X$ be an embedding. Then the function
\begin{equation*}
\blank\circ \seqin_0: \mathrm{hom}_X(f^{\ast\infty},m)\to \mathrm{hom}_X(f,m)
\end{equation*}
is an equivalence.
\end{lem}

\begin{thm}\label{lem:isprop_infjp}
For any map $f:A\to X$, the map $f^{\ast\infty}:A_X^{\ast\infty}\to X$ is an embedding that satisfies the universal property of the image inclusion of $f$.
\end{thm}

\subsection{Surjective maps}

Another application of the propositional truncation is the notion of surjective map.

\begin{defn}
A map $f:A\to B$ is said to be \define{surjective} if there is a term of type
\begin{equation*}
\issurj(f)\defeq \prd{y:B}\brck{\fib{f}{b}}.
\end{equation*}
\end{defn}

\begin{eg}
Any equivalence is a surjective map, and so is any map that has a section (those are sometimes called \define{split epimorphisms}). Other examples include the base point inclusion $\unit\to\sphere{n}$ for any $n\geq 1$.
\end{eg}

\begin{thm}\label{thm:surjective}
Consider a commuting triangle
\begin{equation*}
\begin{tikzcd}[column sep=tiny]
A \arrow[rr,"q"] \arrow[dr,swap,"f"] & & B \arrow[dl,"m"] \\
& X
\end{tikzcd}
\end{equation*}
in which $m$ is an embedding. Then $m$ satisfies the universal property of the image of $f$ if and only if $i:A\to B$ is surjective.
\end{thm}

\begin{thm}
Let $f:A\to B$ be a map. The following are equivalent:
\begin{enumerate}
\item $f$ is an equivalence.
\item $f$ is both surjective and an embedding.
\end{enumerate}
\end{thm}

\begin{exercises}
\item Show that
\begin{equation*}
\eqv{\brck{A}}{\prd{P:\prop}(A\to P)\to P}
\end{equation*}
for any type $A:\UU$. This is called the \define{impredicative encoding} of the propositional truncation.
\item For any $B:A\to\UU$, construct an equivalence
\begin{equation*}
\eqv{\Big(\exists_{(a:A)}\brck{B(a)}\Big)}{\brck{\sm{a:A}B(a)}}
\end{equation*}
%\item \label{also}(Mart\'in Escard\'o) For any two propositions $P$ and $Q$, define
%\begin{equation*}
%P\boxplus Q \defeq ((P\to Q)\to Q)\times ((Q\to P)\to P).
%\end{equation*}
%\begin{subexenum}
%\item Show that $P\lor Q\to P\boxplus Q$ and $P\boxplus Q\to\neg(\neg P\land \neg Q)$.
%\end{subexenum}
%\item \label{ex:brck_comp} Formulate the computation rule corresponding to the path constructor $\mu$. That is, compute the type of $\apd{\rec{\brck{\blank}}(f,g)}{\mu(x,y)}$, and find a canonical element in it.
\item Let
\begin{tikzcd}
P_0 \arrow[r] & P_1 \arrow[r] & P_2 \arrow[r] & \cdots
\end{tikzcd}
be a sequence of propositions. Show that
\begin{equation*}
\eqv{\colim_n(P_n)}{\exists_{(n:\N)} P_n}.
\end{equation*}
\item Show that the relation $x,y\mapsto\brck{x=y}$ is an equivalence relation, on any type.
\item Let $f:A\to X$ be a map. Construct an equivalence
\begin{equation*}
\eqv{\Big(\sm{y:\mathsf{join\usc{}power}_X(n,A)}f(x)=f^{\ast n}(y)\Big)}{\Big(\sm{y:A}f(x)=f(y)\Big)^{\ast n}}
\end{equation*}
for any $x:A$.
\item Let $f:A\to B$ be a map. Show that the following are equivalent:
\begin{enumerate}
\item The commuting square
\begin{equation*}
\begin{tikzcd}
A \arrow[d,swap,"f"] \arrow[r] & \brck{A} \arrow[d,"\brck{f}"] \\
B \arrow[r] & \brck{B}.
\end{tikzcd}
\end{equation*}
is a pullback square.
\item There is a term of type $A\to\isequiv(f)$.
\item The commuting square
\begin{equation*}
\begin{tikzcd}
A\times A \arrow[r,"f\times f"] \arrow[d,swap,"\proj 1"] & B \times B \arrow[d,"\proj 1"] \\
A \arrow[r,swap,"f"] & B
\end{tikzcd}
\end{equation*}
is a pullback square.
\end{enumerate}
\item Consider a pullback square
\begin{equation*}
\begin{tikzcd}
A' \arrow[d,swap,"{f'}"] \arrow[r,"p"] & A \arrow[d,"f"] \\
B' \arrow[r,swap,"q"] & B,
\end{tikzcd}
\end{equation*}
in which $q:B'\to B$ is surjective. Show that if $f':A'\to B'$ is an embedding, then so is $f:A\to B$.
\item Show that a type $A$ is a proposition if and only if the map $\inl:A\to \join{A}{A}$ is an equivalence.
\item Show that $\inl:\brck{A}\to \join{\brck{A}}{A}$ is an equivalence for any type $A$.
\item Consider a family of diagrams of the form
  \begin{equation*}
    \begin{tikzcd}
      A_i \arrow[r] \arrow[d,swap,"{f_i}"] &
        C \arrow[r] \arrow[d,"g"] & X \arrow[d,"h"] \\
      B_i \arrow[r] & D \arrow[r] & Y
    \end{tikzcd}
  \end{equation*}
  indexed by $i:I$, in which the left squares are pullback squares,
  and assume that the induced map
  \begin{equation*}
    \Big(\sm{i:I}B_i\Big)\to D
  \end{equation*}
  is surjective. Show that the following are equivalent:
  \begin{enumerate}
  \item For each $i:I$ the outer rectangle is a pullback square.
  \item The right square is a pullback square.
  \end{enumerate}
  Hint: By \cref{thm:descent-Sigma} it suffices to prove this equivalence for a single diagram of the form
    \begin{equation*}
    \begin{tikzcd}
      A \arrow[r] \arrow[d,swap,"{f}"] &
        C \arrow[r] \arrow[d,swap,"g"] & X \arrow[d,"h"] \\
      B \arrow[r] & D \arrow[r] & Y
    \end{tikzcd}
    \end{equation*}
  where the map $B \to D$ is assumed to be surjective.
\end{exercises}