\nameref{sub:array}s offer a means of storing and working with a list of values in your code. Each array has a number of elements, each of which has a value, and can be accessed using an index. Together with the \nameref{sub:for_loop}, arrays provide a means of managing multiple values in your code. The following illustrations show how these work in the computer, and should help you better understand how arrays can be used within your code.

\subsection{Understanding \texttt{Populate Array}} % (fold)
\label{sub:understanding_populate_array}

\sref{sub:designing_statistics_calculator} \nameref{sub:designing_statistics_calculator} outlined the pseudocode and flowcharts for a small statistics programs. This included a number of functions and procedures that helped divide the program's code into smaller units of work. One of these procedures was \texttt{Populate Array}, discussed in \sref{ssub:populating_the_array} \nameref{ssub:populating_the_array}. This procedure is responsible for reading values from the user and using these to populate the program's array, and the flowchart for this logic is shown in \fref{fig:populate-array-flow-understanding}.

\begin{figure}[htbp]
   \centering
   \includegraphics[width=0.5\textwidth]{./topics/arrays/diagrams/PopulateArray}
   \caption{Flowchart showing the process for \texttt{Populate Array}, from \fref{fig:populate-array-flow}}
   \label{fig:populate-array-flow-understanding}
\end{figure}

The following illustrations will show this code running to populate an array that contains three values. This will show how the array is passed by reference, and how the for loop works together with the array to populate all elements.

\clearpage
\subsubsection{Main starts, and the array is allocated space} % (fold)
\label{ssub:main_starts_and_the_array_is_allocated_space}

All local variables are allocated space on the Stack when the function or procedure they are declared in is called. In this example the \texttt{Main} procedure is executed and space is allocated for its \texttt{my\_data} variable. This variable is an \nameref{sub:array} that is used to store three \texttt{double} values. When \texttt{Main} is loaded onto the Stack there is space allocated for three \texttt{double} values associated with the \texttt{my\_data} variable.

\begin{figure}[htbp]
   \centering
   \includegraphics[width=\textwidth]{./topics/arrays/images/PopulateArray1}
   \caption{When the program starts \texttt{Main} allocates space for its local variables, including the array}
   \label{fig:populate-array-vis-1}
\end{figure}

\mynote{
\begin{itemize}
  \item In \fref{fig:populate-array-vis-1} the indicated areas show the following:
  \begin{enumerate}
    \item The program starts and \texttt{Main} is loaded onto the stack, allocating space for its local variables.
    \item The \texttt{my\_data} array is allocated space to store its values.
  \end{enumerate}
  \medskip
  \item Notice that the three values in the array are allocated next to each other.
  \item The indexes can be used to access the array's elements. The index value determines the number of elements that must be skipped to find where the value is stored.
\end{itemize}
}
% subsubsection main_starts_and_the_array_is_allocated_space (end)

\clearpage
\subsubsection{Populate array is called, and a reference to \texttt{my{\textunderscore}data} passed in} % (fold)
\label{ssub:populate_array_is_called_and_a_reference_to_my_data_passed_in}

\texttt{Populate Array} is called as the first step in \texttt{Main}. This is passed the \texttt{my\_data} variable (pass by reference), rather than being passed the values from within that variable. This gives the \texttt{data} parameter access to the memory where \texttt{my\_data} is stored.

\begin{figure}[htbp]
   \centering
   \includegraphics[width=\textwidth]{./topics/arrays/images/PopulateArray2}
   \caption{Populate array is called, and a reference to the \texttt{my\_data} array is pass to its \texttt{data} parameter}
   \label{fig:populate-array-vis-2}
\end{figure}

\mynote{
\begin{itemize}
  \item In \fref{fig:populate-array-vis-2} the indicated areas show the following:
  \begin{enumerate}
    \item When \texttt{Populate Array} is called it is loaded onto the Stack. Its \texttt{data} parameter receives the address of \texttt{my\_data} from \texttt{Main}. In C the value 3 would also be passed to the \texttt{size} parameter, as C does not keep track of the length of the array for you. At the same time space for \texttt{Populate Array}'s local variables \texttt{i} and \texttt{prompt} are allocated on the Stack.
    \item Notice that in \texttt{Populate Array} the \texttt{data} parameter only stores the address of the array, as it is passed by reference. This saves time and space, and is needed in this case as the procedure wants to store data into the variable passed to this parameter.
    \item The \texttt{prompt} local variable is also an array. It is allocated spaces on the stack as is done for all local variables.
  \end{enumerate}
  \medskip
  \item Arrays are allocated a contiguous area of memory to store its elements.
  \item A String is an array of characters.
\end{itemize}
}

% subsubsection populate_array_is_called_and_a_reference_to_my\_data_passed_in (end)

\clearpage
\subsubsection{Step 1 of \texttt{Populate Array} is run} % (fold)
\label{ssub:step_1_of_populate_array_is_run}

Step 1 of \texttt{Populate Array} initialises the for loop's control variable (\texttt{i} in this case). This variable keeps track of the times the loop body has executed, and can be used to get the \emph{current} value from the array.

\begin{figure}[htbp]
   \centering
   \includegraphics[width=\textwidth]{./topics/arrays/images/PopulateArray3}
   \caption{Step 1 of \texttt{Populate Array} is called, and the for loop sets i to the lowest index of the \texttt{data} array}
   \label{fig:populate-array-vis-3}
\end{figure}

\mynote{
\begin{itemize}
  \item In \fref{fig:populate-array-vis-3} the indicated areas show the following:
  \begin{enumerate}
    \item Step 1 of \texttt{Populate Array} is a for loop. The first action of the for loop is to initialise the value of \texttt{i} to the \emph{lowest index value} of the \texttt{data} array. The lowest index is 0, so \texttt{i} is assigned the value 0.
    \item Next the loop checks its condition, it is loop from 0 to 2, and has not passed 2 so the body of the loop will be executed. This will be checked again when the for loop ends.
  \end{enumerate}
  \medskip
  \item The first action of a for loop is to initialise the value of the control variable.
  \item When processing each element of an array the for loop should initialise the control variable to 0, the first index of the array.
\end{itemize}
}

% subsubsection step_1_of_populate_array_is_run (end)

\clearpage
\subsubsection{Step 2 constructs the prompt to be shown to the user} % (fold)
\label{ssub:step_2_constructs_the_prompt_to_be_shown_to_the_user}

The user needs to be told what to enter. The \texttt{prompt} is a string that will contain this message so that it can be passed to \texttt{Read Double}. The value for the prompt will use the loop's control variable (the counter) so that the user known which value they are up to.

\begin{figure}[htbp]
   \centering
   \includegraphics[width=\textwidth]{./topics/arrays/images/PopulateArray4}
   \caption{Step 2 builds the prompt \texttt{Enter value 1: } which will be shown to the user}
   \label{fig:populate-array-vis-4}
\end{figure}

\mynote{
\begin{itemize}
  \item In \fref{fig:populate-array-vis-4} the indicated areas show the following:
  \begin{enumerate}
    \item Step 2 of \texttt{Populate Array} builds the prompt by concatenating `\texttt{Enter Value}' with \texttt{i + 1} and `\texttt{: }'.
    \item To achieve this \texttt{i + 1} must be converted from an Integer to a String.
    \item The result is stored in the \texttt{prompt} variable.
  \end{enumerate}
  \medskip
  \item This action will be performed each time the loop executes. In this case the value stored in \texttt{prompt} will be `\texttt{Enter value  1: }'.
  \item The small square shown at the end of the \texttt{prompt} represents the overhead. In C this is the \emph{sentinel} value, in Pascal it is the length\footnote{Pascal actually stores the length at the start of the string, but the idea is the same.} of the array.
\end{itemize}
}

% subsubsection step_2_constructs_the_prompt_to_be_shown_to_the_user (end)

\clearpage
\subsubsection{Step 3 reads a value from the user and stores it in the array at index 0} % (fold)
\label{ssub:step_3_reads_a_value_from_the_user_and_stores_it_in_the_array_at_index_0}

The next step calls the \texttt{Read Double} function. This is responsible for reading a value from the user, and returning it to the caller. The value returned is then stored in an element of the array. The \texttt{i} variable is read to determine the position where this value should be stored. This means that you can think of \texttt{i} as referring to the \emph{current} element of the array.

\begin{figure}[htbp]
   \centering
   \includegraphics[width=\textwidth]{./topics/arrays/images/PopulateArray5}
   \caption{Step 3 reads a \texttt{double} value from the user and stores it in \texttt{data[i]}}
   \label{fig:populate-array-vis-5}
\end{figure}

\mynote{
\begin{itemize}
  \item In \fref{fig:populate-array-vis-5} the indicated areas show the following:
  \begin{enumerate}
    \item Step 3 of \texttt{Populate Array} calls \texttt{Read Double}, passing across the prompt to be shown to the user.
    \item Within \texttt{Read Double} the value is read from the Terminal. This includes some validation to make sure that the value entered is a number.
    \item To determine where the value is stored the computer needs to evaluate the expression used to index the array. In this case that is the value of the \texttt{i} variable.
    \item The \texttt{data} reference is followed, and \texttt{0} elements skipped, to find the location where the data should be stored.
  \end{enumerate}
  \medskip
  \item Notice that this has stored the value in \texttt{my\_data}, as \texttt{data} is passed by reference.
\end{itemize}
}

% subsubsection step_3_reads_a_value_from_the_user_and_stores_it_in_the_array_at_index_0 (end)

\clearpage
\subsubsection{Control returns to Step 1 as the loop body has ended} % (fold)
\label{ssub:control_returns_to_step_1_as_the_loop_body_has_ended}

At the end of the loop body the for loop performs two actions. It has finished the first pass through the loop, so its control variable (the counter) needs to be incremented to 1. Then it needs to jump back to check its condition. This will determine if the loop's body is executed again or skipped. In this case \texttt{i} is still in the defined range so the loop's body is run again.

\begin{figure}[htbp]
   \centering
   \includegraphics[width=\textwidth]{./topics/arrays/images/PopulateArray6}
   \caption{At the end of the loop body \emph{i} is incremented and control jumps back to check the loop's condition}
   \label{fig:populate-array-vis-6}
\end{figure}

\mynote{
\begin{itemize}
  \item In \fref{fig:populate-array-vis-6} the indicated areas show the following:
  \begin{enumerate}
    \item The end of the loop body indicates that two things needs to occur. Firstly the value of \texttt{i} needs to be incremented, and then control needs to jump back to check the condition of the loop (step 1).
    \item The loop's condition checks if \texttt{i} is still in range (\texttt{i} is in the range \texttt{0..2}, coded as \texttt{i < size} in C). As it is still in range the loop's body will execute again.
  \end{enumerate}
  \medskip
\end{itemize}
}

% subsubsection control_returns_to_step_1_as_the_loop_body_has_ended (end)

\clearpage
\subsubsection{Second prompt is built asking the user to enter value 2} % (fold)
\label{ssub:second_prompt_is_built_asking_the_user_to_enter_value_2}

Back at step 2 again, the prompt needs to be recreated. This time its message will be `\texttt{Enter value 2: }'. The process to create this is the same, with the value of \texttt{i + 1} being converted to a String, and the three parts concatenated together and stored in \texttt{prompt}. This overrides the details in the existing array, reusing the same memory to store these values.

\begin{figure}[htbp]
   \centering
   \includegraphics[width=\textwidth]{./topics/arrays/images/PopulateArray7}
   \caption{Step 2 builds the prompt \texttt{Enter value 2: } which will be shown to the user}
   \label{fig:populate-array-vis-7}
\end{figure}

\mynote{
\begin{itemize}
  \item In \fref{fig:populate-array-vis-7} the indicated areas show the following:
  \begin{enumerate}
    \item Step 2 of \texttt{Populate Array} builds the prompt by concatenating `\texttt{Enter Value}' with \texttt{i + 1} and `\texttt{: }'.
    \item To achieve this \texttt{i + 1} must be converted from an Integer to a String.
    \item The result is stored in the \texttt{prompt} variable.
  \end{enumerate}
  \medskip
  \item Notice that this is using the same location to store its data.
  \item The small square shown at the end of the \texttt{prompt} represents the overhead. In C this is the \emph{sentinel} value, in Pascal it is the length\footnote{Pascal actually stores the length at the start of the string, but the idea is the same.} of the array.
\end{itemize}
}

% subsubsection second_prompt_is_built_asking_the_user_to_enter_value_2 (end)

\clearpage
\subsubsection{\texttt{Populate Array} stores the second value read into \texttt{data[1]}} % (fold)
\label{ssub:populate_array_stores_the_second_value_read_into_data[1]}

Step 3 uses \texttt{Read Double} again to get the value to store in the second element of the array. To find where this should be stored the computer calculates the value of the index, reading this from the \texttt{i} variable. The value returned from \texttt{Read Double} is then stored in the array referred to by \texttt{data}, at index \texttt{1}.

\begin{figure}[htbp]
   \centering
   \includegraphics[width=\textwidth]{./topics/arrays/images/PopulateArray8}
   \caption{The second value read is stored in \texttt{data[1]}}
   \label{fig:populate-array-vis-8}
\end{figure}

\mynote{
\begin{itemize}
  \item In \fref{fig:populate-array-vis-8} the indicated areas show the following:
  \begin{enumerate}
    \item Step 3 of \texttt{Populate Array} calls \texttt{Read Double}, passing across the prompt to be shown to the user.
    \item Within \texttt{Read Double} the value is read from the Terminal. This includes some validation to make sure that the value entered is a number.
    \item The value of the index is now \texttt{1}, as read from variable \texttt{i}.
    \item The \texttt{data} reference is followed, and \texttt{1} element is skipped, to find the location where the data should be stored.
  \end{enumerate}
  \medskip
\end{itemize}
}

% subsubsection populate_array_stores_the_second_value_read_into_data[1] (end)

\clearpage
\subsubsection{\texttt{i} is incremented again, and control returns to Step 1 to determine if loop runs again} % (fold)
\label{ssub:i_is_incremented_again_and_control_returns_to_step_1_to_determine_if_loop_runs_again}

The end of the for loop's body has been reached again, so it performs two actions: it increments its control variable (\texttt{i} in this case) and jumps back to check its condition (step 1). The condition then determines if the loop's body is to be executed again or skipped. In this case \texttt{i} is still in the defined range so the loop's body is run a third time.

\begin{figure}[htbp]
   \centering
   \includegraphics[width=0.98\textwidth]{./topics/arrays/images/PopulateArray9}
   \caption{At the end of the loop body \emph{i} is incremented and control jumps back to check the loop's condition}
   \label{fig:populate-array-vis-9}
\end{figure}

\mynote{
\begin{itemize}
  \item In \fref{fig:populate-array-vis-9} the indicated areas show the following:
  \begin{enumerate}
    \item The end of the loop body indicates that two things needs to occur: the value of \texttt{i} is incremented, and control jump back to check the condition of the loop.
    \item The loop's condition checks if \texttt{i} is still in range (\texttt{i} is in the range \texttt{0..2}, coded as \texttt{i < size} in C). As it is still in range the loop's body is execute again.
  \end{enumerate}
  \medskip
  \item These same actions always occur at the end of the for loop. It increments its control variable, and jumps back to check its condition.
\end{itemize}
}

\csection{The C for loop can be used for more than just counting. At the end of the loop the instructions from the \nameref{sub:c_for_loop}'s \emph{increment} occur, typically something like \texttt{i++}.}

% subsubsection i_is_incremented_again_and_control_returns_to_step_1_to_determine_if_loop_runs_again (end)

\clearpage
\subsubsection{Third prompt is built asking the user to enter value 3} % (fold)
\label{ssub:third_prompt_is_built_asking_the_user_to_enter_value_3}

Back at step 2 for the third time. This step recreates the prompt, this time with the message `\texttt{Enter value 3: }'. The process to create this is the same as before, with the value of \texttt{i + 1} being converted to a String, and the three parts concatenated together and stored in \texttt{prompt}. Remember that this overrides the data currently in the \texttt{prompt} array.

\begin{figure}[htbp]
   \centering
   \includegraphics[width=\textwidth]{./topics/arrays/images/PopulateArray10}
   \caption{Step 2 builds the prompt \texttt{Enter value 3: } which will be shown to the user}
   \label{fig:populate-array-vis-10}
\end{figure}

\mynote{
\begin{itemize}
  \item In \fref{fig:populate-array-vis-10} the indicated areas show the following:
  \begin{enumerate}
    \item Step 2 of \texttt{Populate Array} builds the prompt by concatenating `\texttt{Enter Value}' with \texttt{i + 1} and `\texttt{: }'.
    \item To achieve this \texttt{i + 1} must be converted from an Integer to a String.
    \item The result is stored in the \texttt{prompt} variable.
  \end{enumerate}
  \medskip
\end{itemize}
}

% subsubsection third_prompt_is_built_asking_the_user_to_enter_value_3 (end)

\clearpage
\subsubsection{\texttt{Populate Array} stores the third value read into \texttt{data[2]}} % (fold)
\label{ssub:populate_array_stores_the_third_value_read_into_data[2]}

Once again, step 3 uses \texttt{Read Double} get the value to store in the array. In this case \texttt{i} indicates that this should be stored in the element at index 2 (skipping 2 elements, so storing the value in the third).

\begin{figure}[htbp]
   \centering
   \includegraphics[width=\textwidth]{./topics/arrays/images/PopulateArray11}
   \caption{The second value read is stored in \texttt{data[1]}}
   \label{fig:populate-array-vis-11}
\end{figure}

\mynote{
\begin{itemize}
  \item In \fref{fig:populate-array-vis-11} the indicated areas show the following:
  \begin{enumerate}
    \item Step 3 of \texttt{Populate Array} calls \texttt{Read Double} again.
    \item Within \texttt{Read Double} the value is read from the Terminal.
    \item The value of the index is now \texttt{2}, as read from variable \texttt{i}.
    \item The \texttt{data} reference is followed, and \texttt{2} elements are skipped to find the location where the data should be stored.
  \end{enumerate}
  \medskip
  \item Notice how over these three loops \texttt{i} has been used to determine which element is the \emph{current element}. The loop's processing is the same, but changing \texttt{i} means that this action is applied to each element in the array one at a time.
  \item The loop's body determines the action to apply to the \texttt{$i^{th}$} element of the array.
  \item The for loop then updates \texttt{i} so that the body is applied to \emph{each} element of the array.
\end{itemize}
}

% subsubsection populate_array_stores_the_third_value_read_into_data[2] (end)

\clearpage
\subsubsection{\texttt{i} is incremented again, and control jumps back to check the condition a fourth time} % (fold)
\label{ssub:i_is_incremented_again_and_control_jumps_back_to_check_the_condition_a_fourth_time}

The end of the for loop's body has been reached again, so it performs two actions: it increments its control variable (\texttt{i} in this case) and jumps back to check its condition (step 1). This time the value of \texttt{i} is outside the range of the indexes for \texttt{data} (\texttt{0..2}, it is no longer less than 3). This means that the loop's body should not be run again, and control will jump past the body to the next step. As there are no more steps in \texttt{Populate Array} it will end.

\begin{figure}[htbp]
   \centering
   \includegraphics[width=\textwidth]{./topics/arrays/images/PopulateArray12}
   \caption{At the end of the loop body \emph{i} is incremented and control jumps back to check the loop's condition}
   \label{fig:populate-array-vis-12}
\end{figure}

\mynote{
\begin{itemize}
  \item In \fref{fig:populate-array-vis-12} the indicated areas show the following:
  \begin{enumerate}
    \item The end of the loop body causes the value of \texttt{i} to be incremented, and control jump back to check the condition of the loop.
    \item The loop's condition checks if \texttt{i} is still in range (\texttt{i} is in the range \texttt{0..2}, coded as \texttt{i < size} in C).
    \item \texttt{i} is \textbf{not} in the range \texttt{0..2}, so the loop body is skipped. As there are no more steps in this procedure it ends.
  \end{enumerate}
  \medskip
  \item The condition in the for loop is responsible for determining if the loop runs again. This time the condition indicates that the loop is not to be run again, so control will jump past the end of the loop.
  \item The for loop works like a while loop, with additional logic to keep a counter.
\end{itemize}
}

% subsubsection i_is_incremented_again_and_control_jumps_back_to_check_the_condition_a_fourth_time (end)

\clearpage
\subsubsection{\texttt{Populate Array} ends, and has populated \texttt{Main}'s \texttt{my{\textunderscore}data} array} % (fold)
\label{ssub:populate_array_ends_and_has_populated_main_s_my_data_array}

When \texttt{Populate Array} ends its space on the stack is released so that it can be used again, and control returns to \texttt{Main}. \texttt{Populate Array} was responsible for reading values from the user and storing these in the array passed to it, and if you look at \fref{fig:populate-array-vis-13} you can see that this has been achieved.

The instructions in \texttt{Populate Array} commanded the computer to read a value from the user and store it in the current element (the $i^{th}$ element) of the array. These actions were then repeated by the \nameref{sub:for_loop} \emph{for each} index of the array. Together the for loop and its body allow you to define actions that must be performed on all elements in an array.

\begin{figure}[htbp]
   \centering
   \includegraphics[width=\textwidth]{./topics/arrays/images/PopulateArray13}
   \caption{Control returns to \texttt{Main}, and its \texttt{my\_data} array has been populated}
   \label{fig:populate-array-vis-13}
\end{figure}

\mynote{
\begin{itemize}
  \item In \fref{fig:populate-array-vis-13} the indicated areas show the following:
  \begin{enumerate}
    \item The last step in \texttt{Populate Array} has been completed, so it ends and control returns to \texttt{Main}.
    \item The values in \texttt{my\_data} have been updated. Passing this array by reference to \texttt{Populate Array} allowed it to make changes to this array's values.
  \end{enumerate}
  \medskip
  \item Notice that the data associated with the \texttt{Populate Array} procedure has been released, including the data used for the \texttt{prompt} array.
  \item \texttt{Populate Array} is responsible for filling the array passed to it with values from the user, and that is what it has done.

\end{itemize}
}

% subsubsection populate_array_ends_and_has_populated_main_s_my_data_array (end)
% subsection understanding_populate_array (end)

\clearpage
\subsection{Understanding \texttt{Sum}} % (fold)
\label{sub:understanding_sum}

\fref{fig:sum-understanding} shows the flowchart of the \texttt{Sum} function from the Statistics Calculator program. This algorithm was developed in the \sref{ssub:calculating_sum}, and its Pseudocode is shown in Listing \ref{plst:sum}. The \texttt{Sum} function is responsible for calculating the sum of all of the values in the array passed to it. This is achieved by having a \texttt{total} variable that is initialised to 0, and then has the value of \emph{each element} from the array added to it.

\begin{figure}[htbp]
   \centering
   \includegraphics[width=0.5\textwidth]{./topics/arrays/diagrams/SumFlow}
   \caption{Flowchart showing the process for \texttt{Sum}}
   \label{fig:sum-understanding}
\end{figure}

The following code will show how this function is executed on an array with three values in it. This will continue the execution from \sref{sub:understanding_populate_array} \nameref{sub:understanding_populate_array}, though the same process would occur for any array values.

\mynote{
The most important thing to pay attention to in this illustration is the interactions between the \nameref{sub:for_loop} and the \nameref{sub:array}. Make sure you can see how this combination allows you to specify the actions to be performed on an element (in the loop's body), and then to have this run for each element of the array (controlled by the loop condition).
}

\clearpage
\subsubsection{\texttt{Sum} is called, and passed the \texttt{my data} array} % (fold)
\label{ssub:sum_is_called_and_passed_the_values_in_my_data}

When \texttt{Sum} is called it is passed the array to read its values from. This is passed in the same way as was done in \texttt{Populate Array}. The difference here is that this is passed using a \texttt{const} reference, to indicate that \texttt{Sum} is not allowed to change the data in the array. This means that \texttt{Sum} will not compile if you update values in this array, and provide a guarantee to the caller that their data will not change when given to the \texttt{Sum} function.

\begin{figure}[htbp]
   \centering
   \includegraphics[width=\textwidth]{./topics/arrays/images/Sum1}
   \caption{\texttt{Sum} is called, and it is passed the array to get its values from}
   \label{fig:sum-array-vis-1}
\end{figure}

\mynote{
\begin{itemize}
  \item In \fref{fig:sum-array-vis-1} the indicated areas show the following:
  \begin{enumerate}
    \item Step 1 of \texttt{Main} has populated the array with values.
    \item Step 2 calls the \texttt{Sum} function, passing in the \texttt{my\_data} array.
    \item When sum starts it is allocated space on the heap. Its \texttt{data} parameter is passed the address of \texttt{my\_data}. In C the \texttt{size} parameter will be passed the value 3, this isn't needed in Pascal as the language takes care of these details for you. Space is also allocated for the local variables \texttt{i} and \texttt{total}.
  \end{enumerate}
  \medskip
  \item Passing \texttt{my\_data} by reference means that sum gets a reference that \emph{points} to the start of the array.
  \item \texttt{Sum} is responsible for calculating the sum value of the values in the array.

\end{itemize}
}
% subsubsection sum_is_called_and_passed_the_values_in_my_data (end)

\clearpage
\subsubsection{\texttt{total} is initialised to 0} % (fold)
\label{ssub:total_is_initialised_to_0}

The first action in \texttt{Sum} is to set the value of \texttt{total} to 0. \texttt{total} will be used to store the running total of the array, and it must start at 0. Remember that the space on the stack was used before, and therefore these variables get seemingly random values initially. It is important to remember to always initialise the variables you are using.

\begin{figure}[htbp]
   \centering
   \includegraphics[width=\textwidth]{./topics/arrays/images/Sum2}
   \caption{\texttt{Total} is initialised, having its value set to 0}
   \label{fig:sum-array-vis-2}
\end{figure}

\mynote{
\begin{itemize}
  \item In \fref{fig:sum-array-vis-2} the indicated areas show the following:
  \begin{enumerate}
    \item The \texttt{total} variable is assigned the value 0.
  \end{enumerate}
  \medskip
  \item Local variables do not get a default value when the Function or Procedure starts. You need to make sure you initialise these to appropriate values before you use them.
  \item \texttt{total} will keep a running total of the elements in the array, it must start at the value 0.
\end{itemize}
}

% subsubsection total_is_initialised_to_0 (end)

\clearpage
\subsubsection{For loop initialises \texttt{i}} % (fold)
\label{ssub:for_loop_initialises_i}

Step 2 of \texttt{Sum} starts the for loop that will iterate over the elements of the \texttt{data} array. The for loop's control variable, \texttt{i}, is set to the first index of the array and the condition checks if the loop's body should run. As \texttt{i} is in the range \texttt{0..2}, it is less than 3, control will jump into the loop's body making step 3 the next instruction.

\begin{figure}[htbp]
   \centering
   \includegraphics[width=\textwidth]{./topics/arrays/images/Sum3}
   \caption{\texttt{i} is initialised by the for loop, and control jumps to the loop's body}
   \label{fig:sum-array-vis-3}
\end{figure}

\mynote{
\begin{itemize}
  \item In \fref{fig:sum-array-vis-3} the indicated areas show the following:
  \begin{enumerate}
    \item The for loop initialises its control variable \texttt{i}, assigning it the value 0.
    \item The condition of the loop is checked to see if the body should execute. As \texttt{i} is still in the range \texttt{0..2}, it is less than 3, the loop's body will execute.
  \end{enumerate}
  \medskip
  \item The first action of a for loop is always to initialise its control variable. After this it checks if the body of the loop should run.
\end{itemize}
}

% subsubsection for_loop_initialises_i (end)

\clearpage
\subsubsection{\texttt{total} is increased by the value in \texttt{data[0]}} % (fold)
\label{ssub:total_is_incremented_by_the_value_in_data[0]}

The body of the for loop reads the \emph{current} value from the \texttt{data} array, \texttt{data[i]}. As \texttt{i} is currently 0 this reads the first element of the array. This reads the value 10.0, which is added to the current \texttt{total}, 0. The resulting value is then stored back into the \texttt{total} variable, giving it the value 10.0, calculated from the expression \texttt{total + data[0]}, which is \texttt{0 + 10.0}.

\begin{figure}[htbp]
   \centering
   \includegraphics[width=\textwidth]{./topics/arrays/images/Sum4}
   \caption{Total is increased by the value in \texttt{data[0]}}
   \label{fig:sum-array-vis-4}
\end{figure}

\mynote{
\begin{itemize}
  \item In \fref{fig:sum-array-vis-4} the indicated areas show the following:
  \begin{enumerate}
    \item The value of \texttt{data[i]} must be read in this expression. At this point \texttt{i} is 0, so \texttt{data[0]} must be read.
    \item \texttt{data[0]} is found in the array referred to by \texttt{data}, after skipping \texttt{0} elements. This reads the value of the first array element.
    \item The expression evaluates \texttt{total\footnote{Remember that at this point \texttt{total} has not been changed, so it has the value \texttt{0.0} as shown in \fref{fig:sum-array-vis-3}.} + data[i]}, giving \texttt{total + data[0]}, which is \texttt{0 + 10.0}, with the final value being \texttt{10.0}.
    \item The value 10.0, calculated above, is then stored in \texttt{total}.
  \end{enumerate}
  \medskip
  \item The body of the for loop provides the instructions that are run for each element of the array.
\end{itemize}
}

% subsubsection total_is_incremented_by_the_value_in_data[0] (end)

\clearpage
\subsubsection{For loop increases the value of i and runs the loop body a second time} % (fold)
\label{ssub:for_loop_increases_the_value_of_i_and_runs_the_loop_body_a_second_time}

At the end of the for loop it increments the value of its control variable, assigning \texttt{i} the value 1, and then jumps back to check its condition. As \texttt{i} is still in the range 0..2 the loop body will be run again, making step 3 the next action.

\begin{figure}[htbp]
   \centering
   \includegraphics[width=0.95\textwidth]{./topics/arrays/images/Sum5}
   \caption{At the end of the loop body \texttt{i} is incremented, and control jumps back to check the condition}
   \label{fig:sum-array-vis-5}
\end{figure}

\mynote{
\begin{itemize}
  \item In \fref{fig:sum-array-vis-5} the indicated areas show the following:
  \begin{enumerate}
    \item The end of the loop has been reached. The for loop increments its control variable, and jumps back to check its condition.
    \item In this case \texttt{i} is the control variable, so its value is increased to 1.
    \item The condition of the loop is checked to see if the body should execute. As \texttt{i} is still in the range \texttt{0..2} (it is less than 3) the loop's body will execute, making step 3 the next action.
  \end{enumerate}
  \medskip
  \item The end of each for loop always performs these steps. Increasing the value of its control variable, and jumping back to check its condition.
\end{itemize}
}

\csection{Remember C can do more than just counting in the for loop, though this is its main use.}

% subsubsection for_loop_increases_the_value_of_i_and_runs_the_loop_body_a_second_time (end)

\clearpage
\subsubsection{The value of \texttt{total} is increased by the value in \texttt{data[1]}} % (fold)
\label{ssub:the_value_of_total_is_increased_by_the_value_in_data[1]}

The body of the for loop reads the \emph{current} value from the \texttt{data} array, \texttt{data[i]}. Now \texttt{i} has the value 1 it reads the second element of the array. This reads the value -5, which is added to the current \texttt{total}, 10.0. The resulting value is then stored back into the \texttt{total} variable, giving it the value 5.0, calculated from the expression \texttt{total + data[1]}, which is \texttt{10.0 + -5.0}.

\begin{figure}[htbp]
   \centering
   \includegraphics[width=\textwidth]{./topics/arrays/images/Sum6}
   \caption{Total is increased by the value in \texttt{data[1]}}
   \label{fig:sum-array-vis-6}
\end{figure}

\mynote{
\begin{itemize}
  \item In \fref{fig:sum-array-vis-6} the indicated areas show the following:
  \begin{enumerate}
    \item The value of \texttt{data[i]} must be read in this expression. At this point \texttt{i} is 1, so \texttt{data[1]} must be read.
    \item \texttt{data[1]} is found in the array referred to by \texttt{data}, after skipping \texttt{1} element. This reads the value of the second array element.
    \item The expression evaluates \texttt{total\footnote{As before, \texttt{total} has not been changed at this point, so it has the value \texttt{10.0} as shown in \fref{fig:sum-array-vis-5}.} + data[i]}, giving \texttt{total + data[1]}, which is \texttt{10.0 + -5.0}, with the final value being \texttt{5.0}.
    \item The value 5.0, calculated above, is then stored in \texttt{total}.
  \end{enumerate}
  \medskip
  \item Notice that this is performing the same task, just using the next element from the array.
\end{itemize}
}

% subsubsection the_value_of_total_is_increased_by_the_value_in_data[1] (end)

\clearpage
\subsubsection{For loop increases the value of i and runs the loop body a third time} % (fold)
\label{ssub:for_loop_increases_the_value_of_i_and_runs_the_loop_body_a_third_time}

The end of the for loop has been reached again, so it increments the value of its control variable, assigning \texttt{i} the value 2, and then jumps back to check its condition. As \texttt{i} is still in the range 0..2 the loop body will run a third time, making step 3 the next action.

\begin{figure}[htbp]
   \centering
   \includegraphics[width=0.95\textwidth]{./topics/arrays/images/Sum7}
   \caption{At the end of the loop body \texttt{i} is incremented, and control jumps back to check the condition}
   \label{fig:sum-array-vis-7}
\end{figure}

\mynote{
\begin{itemize}
  \item In \fref{fig:sum-array-vis-7} the indicated areas show the following:
  \begin{enumerate}
    \item The end of the loop has been reached. The for loop increments its control variable, and jumps back to check its condition.
    \item In this case \texttt{i} is the control variable, so its value is increased to 2.
    \item The condition of the loop is checked to see if the body should execute. As \texttt{i} is still in the range \texttt{0..2} (it is less than 3) the loop's body will execute, making step 3 the next action.
  \end{enumerate}
  \medskip
  \item The end of each for loop always performs these steps. Increasing the value of its control variable, and jumping back to check its condition.
\end{itemize}
}

% subsubsection for_loop_increases_the_value_of_i_and_runs_the_loop_body_a_third_time (end)

\clearpage
\subsubsection{The value of \texttt{total} is increased by the value in \texttt{data[2]}} % (fold)
\label{ssub:the_value_of_total_is_increased_by_the_value_in_data[2]}

The body of the for loop reads the \emph{current} value from the \texttt{data} array, \texttt{data[i]}. Now \texttt{i} has the value 2 it reads the second element of the array. This reads the value 17.21, which is added to the current \texttt{total}, 5.0. The resulting value is then stored back into the \texttt{total} variable, giving it the value 22.21, calculated from the expression \texttt{total + data[2]}, which is \texttt{5.0 + 17.21}.

\begin{figure}[htbp]
   \centering
   \includegraphics[width=\textwidth]{./topics/arrays/images/Sum8}
   \caption{Total is increased by the value in \texttt{data[1]}}
   \label{fig:sum-array-vis-8}
\end{figure}

\mynote{
\begin{itemize}
  \item In \fref{fig:sum-array-vis-8} the indicated areas show the following:
  \begin{enumerate}
    \item The value of \texttt{data[i]} must be read in this expression. At this point \texttt{i} is 2, so \texttt{data[2]} must be read.
    \item \texttt{data[2]} is found in the array referred to by \texttt{data}, after skipping \texttt{2} elements. This reads the value of the third array element.
    \item The expression evaluates \texttt{total\footnote{As before, \texttt{total} has not been changed at this point, so it has the value \texttt{5.0} as shown in \fref{fig:sum-array-vis-7}.} + data[i]}, giving \texttt{total + data[2]}, which is \texttt{5.0 + 17.21}, with the final value being \texttt{22.21}.
    \item The value 5.0, calculated above, is then stored in \texttt{total}.
  \end{enumerate}
  \medskip
  \item Notice that this is performing the same task, just using the next element from the array.
\end{itemize}
}

% subsubsection the_value_of_total_is_increased_by_the_value_in_data[2] (end)

\clearpage
\subsubsection{For loop increases the value of \texttt{i}, and this time the loop finishes} % (fold)
\label{ssub:for_loop_increases_the_value_of_i_and_this_time_the_loop_finishes}

The end of the for loop has been reached again, so it increments the value of its control variable, assigning \texttt{i} the value 3, and then jumps back to check its condition. This time \texttt{i} is no longer in the range 0..2 (it is not less than 3), so the loop body will now be skipped, making step 4 the next action.

\begin{figure}[htbp]
   \centering
   \includegraphics[width=0.95\textwidth]{./topics/arrays/images/Sum9}
   \caption{At the end of the loop body \texttt{i} is incremented, and control jumps back to check the condition}
   \label{fig:sum-array-vis-9}
\end{figure}

\mynote{
\begin{itemize}
  \item In \fref{fig:sum-array-vis-9} the indicated areas show the following:
  \begin{enumerate}
    \item The end of the loop has been reached. The for loop increments its control variable, and jumps back to check its condition.
    \item In this case \texttt{i} is the control variable, so its value is increased to 3.
    \item The condition of the loop is checked to see if the body should execute. \texttt{i} is no longer in the range \texttt{0..2} (it is not less than 3), so the loop's body will be skipped, making step 4 the next action.
  \end{enumerate}
  \medskip
  \item The for works just like a while loop, checking its condition each loop and skipping the body when the condition is not met (when it is false).
\end{itemize}
}

% subsubsection for_loop_increases_the_value_of_i_and_this_time_the_loop_finishes (end)

\clearpage
\subsubsection{\texttt{Sum} function returns the \texttt{total} to the expression in \texttt{Main}} % (fold)
\label{ssub:sum_function_returns_the_total_to_the_expression_in_main}

The end of the for loop has been reached again, so it increments the value of its control variable, assigning \texttt{i} the value 3, and then jumps back to check its condition. This time \texttt{i} is no longer in the range 0..2 (it is not less than 3), so the loop body will now be skipped, making step 4 the next action.

\begin{figure}[htbp]
   \centering
   \includegraphics[width=0.95\textwidth]{./topics/arrays/images/Sum10}
   \caption{Step 4 indicates that the value in \texttt{total} is returned to \texttt{Main}}
   \label{fig:sum-array-vis-10}
\end{figure}

\mynote{
\begin{itemize}
  \item In \fref{fig:sum-array-vis-10} the indicated areas show the following:
  \begin{enumerate}
    \item Step 4 indicates that the value in the \texttt{total} variable should be returned to the caller.
    \item The result returned by the \texttt{Sum} function is used in the expression in \texttt{Main}.
    \item As this is the end of the function its space on the stack is released, allowing control to return to \texttt{Main}.
  \end{enumerate}
  \medskip
  \item The for works just like a while loop, checking its condition each loop and skipping the body when the condition is not met (when it is false).
\end{itemize}
}

% subsubsection sum_function_returns_the_total_to_the_expression_in_main (end)

\clearpage
\subsubsection{\texttt{Main} outputs the sum to the Terminal} % (fold)
\label{ssub:main_outputs_the_sum_to_the_terminal}

\texttt{Sum} returns its value to be used in step 2 of \texttt{Main}. This step outputs the value returned to the Terminal. So by the end of Step 2 in \texttt{Main} the sum has been calculated, and written to the Terminal.

\begin{figure}[htbp]
   \centering
   \includegraphics[width=0.95\textwidth]{./topics/arrays/images/Sum11}
   \caption{Step 4 indicates that the value in \texttt{total} is returned to \texttt{Main}}
   \label{fig:sum-array-vis-11}
\end{figure}

\mynote{
\begin{itemize}
  \item In \fref{fig:sum-array-vis-11} the indicated areas show the following:
  \begin{enumerate}
    \item Back in \texttt{Main}, the sum is output to the Terminal.
    \item The value that is output is the value returned from the \texttt{Sum} function.
  \end{enumerate}
\end{itemize}
}

% subsubsection main_outputs_the_sum_to_the_terminal (end)

% subsection understanding_sum (end)


\documentclass[journal]{IEEEtran}

\usepackage[style=ieee]{biblatex}
\usepackage{amsmath}
\usepackage{url}
\usepackage{graphicx}
\usepackage{float}

\usepackage{blindtext, subfig}
\usepackage{dblfloatfix}

% \usepackage[caption=false]{subfig}
\usepackage{todonotes}

\bibliography{ParticleSizeDistribution.bib}

\begin{document}

\title{Investigating Cross-Sectional Size Distribution of Randomly Distributed
       3D Spheres}

\author{Sai Pandian, ID:\@ 29899923}%

% The report headers
\markboth{PHYS6017 Computer Techniques in Physics Report 2, May 2020}
{Shell \MakeLowercase{\textit{et al.}}: Bare Demo of IEEEtran.cls for IEEE Journals}

\maketitle

\begin{abstract}
  In this paper we present a 3-dimensional numerical simulation demonstrating
  that it is possible to reproduce the known planar cross-section distributions
  for Wicksell's Corpuscle Problem, thereby recovering 3D particle size
  distributions from 2D cross-sections. We investigate the effects of different
  particle sizes on the experimental data, and show this method is accurate for
  2 different starting 3D size distributions. We also present our findings on
  how sensitive this method is to experimental data and discuss its efficacy in
  a real world application such as identifying a material using a planar
  cross-section image from a microscope.
\end{abstract}

\section{Introduction}

\IEEEPARstart{I}{n} the world of Material Science and Crystallography, it is
often the case that a material is identified and classified using the
distribution of its constituent particle sizes. However, direct observation of
the particles is often impossible. Using a modern microscope or laser imaging
techniques, it is possible to observe particles in a cross-section of
material. But the observed radii of particles in this cross-section do not
necessarily correspond directly to the radii of the particles in the material,
since the plane is not oriented such that it passes through the centre of all
observed particles. Thus, it becomes difficult to relate the observed
distribution of particle sizes in the plane to the real distribution of particle
sizes in the material.

Mathematically, determining the distribution of spherical particles from planar
sections is a well known problem called Wicksell's Corpuscle Problem
\cite{DWicksell}, named after S. D. Wicksell, who solved the problem in
1925. With this solution, it is possible to determine the distribution of
particle sizes in a solid from its observed cross-section, allowing us to
identify and classify materials better \cite{Cuzzi2016}.

In this paper we aim to show that it is possible to reproduce theoretical
results for cross-sectional distributions using numerical simulation, and it is
thus possible to solve Wicksell's Corpuscle Problem in this manner.

\section{Method}

The numerical simulation of the 3D particle distribution was done using
Python. It looked at many spheres of different radii, distributed randomly in
space with a given "box of length 20. The spheres were not allowed to overlap,
which we ensured by placing the spheres such that if one sphere has vector
position $\overrightarrow{\textbf{a}}$ and radius $r_{1}$ and the other has
vector position $\overrightarrow{\textbf{b}}$ and radius $r_{2}$, the spheres
must have positions such that:
\begin{equation*}
|\overrightarrow{\textbf{a}} - \overrightarrow{\textbf{b}}| > (r_{1} + r_{2})
\end{equation*}
Random coordinates for a sphere were drawn from a uniform distribution and
checked against every existing sphere until a set of coordinates was drawn that
ensures no overlap, at which point the sphere was placed in space and the process
was repeated until all the necessary spheres have been placed.

\begin{figure}%[H]%[!ht]
\begin{center}
\includegraphics[width=0.45\textwidth]{images/box3d.pdf}
\caption{$750$ spheres of radius 0.7 distributed randomly in a cube of length 20,
  such that no sphere is touching or overlapping another sphere. A horizontal
  plane, in blue, passes through the box and ``cuts'' through the
  spheres.}\label{fig:3dplot_plane}
\end{center}
\end{figure}

We then placed a plane into the cube, with a random orientation, which
``cuts'' through many of the spheres in the cube. This is shown in
Figure~\ref{fig:3dplot_plane}, where we see a plane (in blue) intersecting 750
spheres of constant radius, distributed at random in a box.

In order to obtain the planar cross-section, we employed the spherical cap
method \cite{Sykes1922}. If we consider a sphere with centre
$\overrightarrow{\textbf{c}_0} = (x_0, y_0, z_0)$ and radius $R$, and a plane
with equation $Ax + By + Cz = D$ such that $\overrightarrow{\textbf{n}} = (A, B,
C)$ is the normal vector to the plane, then the distance from the centre of the
circle to the closest point on the plane $\overrightarrow{\textbf{p}_0}$ is given by $\rho$:
\begin{equation*}
\rho = \frac{(\overrightarrow{\textbf{c}_0} - \overrightarrow{\textbf{p}_0}) \cdot{}
  \overrightarrow{\textbf{n}}}{|\overrightarrow{\textbf{n}}|} = \frac{Ax_0 +
  By_0 + Cz_0 - D}{\sqrt{A^2 + B^2 + C^2}}
\end{equation*}
If $|\rho| < R$, then there is an intersection of the plane and the sphere. The
radius of the circle $r$ in the planar cross-section is given by:
\begin{equation*}
r = \sqrt{R^2 - \rho^2}
\end{equation*}
And we then binned this radius to produce a histogram of the distribution of
planar circle radii. It is also possible to recover the centre
$\overrightarrow{\textbf{c}}$ of the circle visible on the plane using:
\begin{equation*}
\overrightarrow{\textbf{c}} = \overrightarrow{\textbf{c}_0} +
\rho\frac{\overrightarrow{\textbf{n}}}{|\overrightarrow{\textbf{c}}|} = (x_0,
y_0, z_0) + \rho\frac{(A, B, C)}{\sqrt{A^2 + B^2 + C^2}}
\end{equation*}

\begin{figure}%[H]%[!ht]
\begin{center}
\includegraphics[width=0.45\textwidth]{images/circles.pdf}
\caption{Planar cross-section constructed using mock data. Spheres cut by the
  plane appear as circles on the plane, with radius dependent on the relative
  position of the spheres to the plane. This distribution of the radii of the
  circles can be modelled.}\label{fig:circles}
\end{center}
\end{figure}

Figure~\ref{fig:circles} presents a planar cross-section generated using this
method and mock data. As we see, there are a range of circular radii observed
since even though the spheres all had the same radius, they were at different
distances from the plane, and were thus cut at different points. The many
observed circular radii are binned to produce a distribution.

If $f(R)dR$ is the number of spheres per unit volume with radii in interval $[R,
R + dR]$, then we can define a distribution $\phi(x)dx$ which represents the
number of circles per unit area on the plane with radii in interval $[x,
x+dx]$ \cite{Kiderlen2011}. This distribution is given by:
\begin{equation}
\phi(x) = \int_{x}^{\infty}\frac{xf(R)}{\sqrt{R^2 - x^2}}
\label{eq:phi_OG}
\end{equation}
We can compute this integral to obtain a theoretical planar distribution for
different starting spherical radius distributions.

We first considered the case in which all the spheres had the same radius
$R_0$. In this case, the spherical radius distribution is simply a Dirac delta
function centred on the radius $R_0$.

If we compute the integral in Equation~\ref{eq:phi_OG}, replacing $f(R)$ with
$\delta(R-R_0)$ we find:
\begin{equation}
\phi(x) = \int_{x}^{\infty}\frac{x\delta(R-R_0)}{\sqrt{R^2 - x^2}} =
\frac{xH(R_0-x)}{\sqrt{R_0^2-x^2}}
\label{eq:phi_constant}
\end{equation}
where $H(R_0 - x)$ is the Heaviside function, which ``turns on'' the
distribution only for $x < R_0$ as it is not possible to have a circular radius
$x$ larger than the spherical radius. So we aim to see our experimental data
reproduce this distribution \cite{Kong2005}. We also investigated how changing
the size of the spheres used in the simulation affects the experimental data.

We next considered the case in which each sphere has a radius $R$ uniformly
distributed in a range $R_1 < R < R_2$. In this case, we modify $f(R)$ in
Equation~\ref{eq:phi_OG} to be:
\begin{equation*}
  f(R) =
  \begin{cases}
    1,& \text{if } R_1 < R < R_2\\
    0,& \text{} Otherwise
  \end{cases}
\end{equation*}
In this case the integral in Equation~\ref{eq:phi_OG} does not need to be
computed with the limits shown, but instead with limits between $R_1$ and $R_2$
since $f(R)$ is 0 otherwise:

\begin{equation}
\begin{split}
  \phi(x) & = \int_{R_1}^{R_2}\frac{xf(R)}{\sqrt{R^2 - x^2}} \\
  = x\ln\left(\sqrt{R_2^2 - x^2}+R_2\right) & - x\ln\left(\sqrt{R_1^2 - x^2}+R_1\right)
\end{split}
\end{equation}
Since this distribution produces complex numbers for $ x < R_1$, we only
consider the real part of this distribution, which appears to produce a peak at
$x = R_1$.

\section{Results and Discussion}

We found that for both the case of the spheres of constant radius, and the case
of the spheres of varying radius, the experimental data reproduced a
distribution in the same shape as the theoretical distribution.

We first investigated the case where we placed $750$ spheres of radius $0.1$
each in a cube of length $20$. In this case we found that a plane in a random
orientation intersects very few spheres, since the spheres are very small. The
planar distribution is presented in Figure~\ref{fig:size1}.

\begin{figure}%[H]%[!ht]
\begin{center}
\includegraphics[width=0.45\textwidth]{images/750_01.pdf}
\caption{Distribution of circle radii on plane for $750$ spheres of radius $0.1$
  each. There are relatively few points since the spheres are small, so there is
  relatively small chance that they will be cut by the plane. So only a few
  circles are visible on the plane. The theoretical distribution is shown, with
  the experimental data matching this distribution, showing a slow increase in
  number or circles as radius increases.}\label{fig:size1}
\end{center}
\end{figure}

We observe relatively few experimental data points, as there were relatively few
intersections with the plane. This was likely because the spheres' radii were
very small compared to box length and there were relatively few spheres. While
the theoretical distribution shows a strong increase near $0.1$, this is not
evident from the experimental data. We also see that there is a lot of variance
between the points so the slow rise evident in the theoretical distribution is
not clear in the experimental data. It is thus necessary that we have more
spheres that intersect with the plane, in order to obtain a larger data-set.

We found that increasing the spheres' size or placing more spheres in the box,
or making the box length smaller, are all good methods of increasing the number
of intersections we observe, as they all increase the likelihood of intersection
by increasing the ratio of sphere occupied space to total box space. We thus
next used $750$ spheres of radius $0.3$ each. The planar cross-sectional
distribution found is presented in Figure~\ref{fig:size3}.

\begin{figure}%[H]%[!ht]
\begin{center}
\includegraphics[width=0.45\textwidth]{images/750_03.pdf}
\caption{Distribution of circle radii on plane for $750$ spheres of radius $0.3$
  each. Compared to Figure~\ref{fig:size1}, there are many more experimental data
  points, since it is more likely that the plane will intersect these larger
  spheres. The experimental data more clearly resembles the theoretical
  distribution, showing a strong increase in number of circles as radius
  increases towards the maximum of 0.3.}\label{fig:size3}
\end{center}
\end{figure}

In Figure~\ref{fig:size3} we see that there are clearly more experimental data
points. This is because more spheres had intersections with the plane. Compared
to Figure~\ref{fig:1noise}, the slow rise in the theoretical distribution is
more closely followed by the experimental data. However, we still encounter
significant variance and fluctuation in lower radius bins, even though the
theoretical data is a smooth gradual increase. This is likely due to the
relatively few number of circles populating these bins, causing greater relative
difference between neighbouring bins. One method to solve this would be to use
fewer bins, which would have the effect of ``summing and averaging''
neighbouring bins so the experimental data has a smoother trend. However, this
would also reduce the number of data points so it was not implemented in our
investigation.

The sharp rise in the theoretical distribution close to the maximum radius is
also clear in the experimental data. It can be understood intuitively that we
expect to see more cross-sections with radius close to radius of the spheres
since there is a greater probability of intersection with the centre of a sphere
than any other point on the sphere.

\begin{figure}%[H]%[!ht]
\begin{center}
\includegraphics[width=0.45\textwidth]{images/750_07.pdf}
\caption{Distribution of circle radii on plane for $750$ spheres of radius $0.7$
  each. We do not observe much difference in how well the experimental data
  resembles the theoretical distribution between this figure and
  Figure~\ref{fig:size3}. With larger sized spheres, the simulation becomes harder
  to run as it is more difficult to randomly place spheres in a finite space
  without overlap.}\label{fig:size7}
\end{center}
\end{figure}

\begin{figure}[H]%[!ht]
\begin{center}
\includegraphics[width=0.45\textwidth]{images/750_07_random.pdf}
\caption{Distribution of circle radii on plane for $750$ spheres of radius $R$
  where $0.65 < R < 0.75$. The theoretical distribution is now different,
  although the experimental data still resembles this new distribution, showing
  a maximum around $R = 0.7$ and a sharp decrease after this. This is likely
  because there are more spheres with radii such that an overlap between $0$ and
  $0.7$ is possible, and relatively few spheres with radii such that a larger
  overlap is possible.}
\label{fig:random}
\end{center}
\end{figure}

\begin{figure*}[ht!]
  \centering
  \subfloat[Noise 2 orders of magnitude smaller than radius]
  {\includegraphics[width=0.45\textwidth]{images/750_07_Noise_100.pdf}
  \label{fig:2noise}}
  \centering
  \subfloat[Noise 1 order of magnitude smaller than radius]
  {\includegraphics[width=0.45\textwidth]{images/750_07_Noise_10.pdf}
  \label{fig:1noise}}
  \caption{Distribution of circle radii on plane for $750$ spheres of radius $0.7$
    each. Each recorded radius has a random noise added to it in order to
    determine how sensitive the data is to experimental
    error. Figure~\ref{fig:2noise} shows the distribution for an added noise 2
    orders of magnitude smaller than the recorded radius, while
    Figure~\ref{fig:1noise} is the same for noise 1 order of magnitude smaller. As
    we can see, the experimental data very resembles the theoretical
    distribution when noise of 2 orders of magnitude smaller are added. But this
    is not true when noise of 1 order of magnitude smaller is added, implying
    the data is very sensitive to experimental data.}
  \label{fig:noiseplots}
\end{figure*}

Increasing the size of the spheres further to a radius of $0.7$, we might
initially expect the experimental data to resemble the theoretical distribution
even more closely, but this is not what we see. Figure~\ref{fig:size7} presents
the planar distribution for $750$ spheres of radius $0.7$ each.

In Figure~\ref{fig:size7}, we see a similar experimental distribution to that of
Figure~\ref{fig:size3}, as opposed to a distribution that more closely resembles
the theoretical distribution. While initially this may seem contrary to the
notion that increasing size of spheres would increase the number of
intersections and thus give a better distribution, this may simply be due to the
fact that increasing the size of the spheres beyond a certain radius does not
provide continually better results. We observe a similar number of intersections
as the case with spheres of radius $0.3$, and thus it is possible to conclude
that the best experimental distribution is found for a spherical size that
ensures a significant number of intersections. Though continually increasing
spherical size provides diminishing returns.

Indeed, we found that making the sphere sizes much larger made the placing of
the spheres in a finite space more difficult, and exponentially increased the
time complexity of the placing algorithm. This is because if the placed spheres
are too large, while it is initially easy to place spheres, as more are placed,
more coordinates must be randomly drawn before one if found for the next sphere
to be placed into. It is also possible that the algorithm never finds a possible
coordinate for later spheres. One possible solution to this may be to use a
better placing algorithm. Choosing a tight packing algorithm \cite{Chen2001} would
maximise the number of spheres of a given radius that can be placed. However,
this would likely result in a less than random distribution of spheres if the
spheres are all of identical sizes. This would likely cause some systematic
error in the data, and so it was not investigated here. If we randomised the
sizes of the spheres as we do in the next part of the investigation, the problem
become significantly more complicated. Tight packing algorithms for irregular
shaped objects are very difficult to create and computationally expensive
\cite{Ma2018}.

Figure~\ref{fig:random} presents the experimental data for 750 spheres with each
sphere having a radius $0.65 < R < 0.75$. As we can see, the theoretical
distribution has a very different shape to the case of constant sphere
radius. We see that the theoretical distribution has a sharp peak centred around
(but not precisely at) $0.65$, the lower limit of the uniform range the radii
were drawn from, and a sharp decrease after this point. This is likely due to
the higher number of spheres available in the box which could be intersected so
as to show a planar cross-section with radius $\approx 0.65$. In a range of
radii between $0.65 < R < 0.75$, all the spheres in the box can be intersected
so as to produce a circle of radius $0.65$, but there will be fewer spheres
which can be intersected to produce a circle of radius greater than this. So a
peak at this point and then a sharp decrease is expected.

The experimental data resembles the theoretical distribution, clearly showing a
peak and sharp decrease after. However, we also see that there is significant
variance between bins, and there is a large scatter around the theoretical
data. As explained for Figure~\ref{fig:size3}, it would be possible to reduce
this fluctuation by using fewer bins, at the cost of having fewer data
points. With a more powerful computer, or if the simulation is optimised and
written with a compiled language, there is also scope for significantly
increasing the size and number of spheres used, which would provide better
experimental data.

While there is significant fluctuation and scatter in all of the simulations
presented so far, the shapes of the distributions were mostly clear, and they
resembled the theoretical distribution. However, if the system is very sensitive
to experimental error, it may be difficult to identify distribution shapes, and
thus prevent us from properly identifying the starting distribution of spherical
radii. We therefore introduced some additional random error into the data. After
the radii of the circles on the plane are calculated, a small random number is
added to each radius. The random number is drawn from a uniform distribution in
the range $[\frac{-r}{n}, \frac{r}{n}]$, where r is the circle's radius and n is
the number of orders of magnitude we wish to choose the errors. We tested the
system with errors 2 orders of magnitude smaller than the radii and 1 order of
magnitude smaller than the radii. This is presented in
Figure~\ref{fig:noiseplots}.

Figure~\ref{fig:2noise} presents the distribution for $750$ spheres or radius
$0.7$ each, with an added ``noise'' that is 2 orders of magnitude smaller than
each measured radius. We can see that the experimental data looks very similar
to the data presented in Figure~\ref{fig:size7}. The shape of the distribution
is also very clear and resembles the theoretical distribution.

Figure~\ref{fig:1noise} presents the same simulation but with an added noise
that is 1 order of magnitude smaller than each measured radius. Here we can see
that the experimental data is unrecognisable, and the shape of the distribution
is no longer clear. We thus conclude that the system is very sensitive to noise
greater than 2 orders of magnitude compared to the measured data.

However, it would be more prudent to use a Gaussian distribution to draw the
values of the noise. This is because it is likely that the many sources of error
possible in an experimental measurement of particle cross-sections in a material
will be convoluted together into a Gaussian distribution of errors due to the
central limit theorem \cite{Islam2018}. Thus, it would be necessary for any
experimentalist attempting to carry out a similar investigation with real
materials, perhaps to determine porosity \cite{Lekakh}, to carefully consider
all errors in measurements.

\section{Conclusions}
In this investigation we showed that it is possible to reproduce expected planar
distributions for the Corpuscle Problem with a 3-dimensional numerical
simulation of spheres distributed randomly in space, where a plane oriented
randomly intersects the spheres. We investigated how effective this method of
simulation is for different particle sizes, and showed that while initially
larger particles provide better data since they allow for more intersections,
continually increasing particle size becomes ineffective and computationally
expensive.

We showed it was possible to reproduce the expected planar distributions for two
starting spherical radii distributions: a Dirac delta distribution, in which all
the spheres have the same radius, and a bounded uniform distribution, in which
all the spheres' radii are drawn randomly from a uniform distribution. Both
produce different shaped planar distributions but both provide data that
resembles the theoretical distributions.

We also investigated how sensitive this method is to experimental error. We
introduced some random noise into the system from a random uniform distribution
and demonstrated that the system is very sensitive to noise greater than two
orders of magnitude relative to measured data. Thus it is important for any
experimentalist making measurements of particle cross-sections in a material to
be very careful with error handling, as any error larger than 2 orders of
magnitude compared to measured data will result in great difficulty in
reproducing the spherical radii distributions.

We therefore showed in this investigation that it is possible to use the known
solutions to Wicksell's Corpuscle Problem to better identify and classify
materials by using planar measurements to gain information about 3D particle
distributions.

\printbibliography

\end{document}


\chapter{Advanced topics in {R}}
\label{chap:R_II}

In this chapter, you will learn some additional topics in R to
\begin{itemize}
	\item Make data wrangling, analyses, and simulations
	more efficient using vectorization and tools such as {\tt plyr}

	\item Use some advanced tools for control flows and looping

	\item Generate random numbers for statistical simulations and looping

	\item Find and fix errors in R code using debugging

	\item Become aware of some additional tools and topics in R
	(accessing databases, building your own packages, etc.).

\end{itemize}

\section{Vectorization}
R is very slow at running cycles ({\tt for} and {\tt while} loops).
This is because R is a ``nimble'' language: at execution time R does
not know what you'are going to perform until it ``reads'' the code to
perform. Compiled languages such as {\tt C}, know exactly what the flow
of the program is, as the code is ``compiled'' before execution.

As a metaphor, {\tt C} is a musician playing a score she has seen
before -- optimizing each passage, while R is playing it ``a prima
vista'' (i.e., at first sight) -- this can slow code execution and
operations down. Let's see an example that illustrates this point.

\begin{compactitem}[$\quad\star$]
		\item Type (save in {\tt Code}) as {\tt Vectorize1.R} the following
		script, and run it (it sums all elements of a matrix):
\end{compactitem}

\lstinputlisting[language=R]{Practicals/Code/Vectorize1.R}

Note the {\tt system.time} R function --- it calculates how much time
your code takes.

Both {\tt SumAllElements()} and {\tt sum()} approaches are correct, and
will give you the right answer. However, the inbuilt function {\tt
sum()}  is 100 times faster than the other, because it is uses
vectorization that avoids the amount of looping that {\tt
SumAllElements()} uses!

\begin{tipbox}
	In R, even if you should try to avoid loops, in practice, it is often
	much easier to throw in a {\tt for} loop, and {\it then} ``optimize''
	the code to avoid the loop if the running time is not
	satisfactory. Therefore, it won't hurt you to become really familiar
	with loops and looping as you learned in Chapter \ref{chap:RI}
\end{tipbox}

Fortunately, R has several functions that can operate on entire vectors
and matrices without requiring looping (Vectorization). That is,
vectorizing a computer program means you write it such that as many
operations as possible are applied to whole data structure (vectors,
matrices, dataframes, lists, etc) at one go, instead of its individual
elements.

Let's learn about some important R functions that allow vectorization
in the following sections.

\subsection{The {\tt *apply} family of functions}

There are a family of functions called {\tt *apply} in R that vectorize
your code for you. These functions are described in the help files (e.g.
{\tt ?apply}).

For example, {\tt apply} can be used when you want to apply a function
to the rows or columns of a matrix (and higher-dimensional analogues --
remember arrays!). This is not generally advisable for data frames as
it will first need to coerce the data frame to a matrix first.

\begin{compactitem}[$\quad\star$]
\item Type the following in a script file called {\tt apply1.R}, save
it to your {\tt Code} directory, and run it:
\end{compactitem}

\lstinputlisting[language=R]{Practicals/Code/apply1.R}

That was using apply on some of R's inbuilt functions. You can use
apply to define your own functions. Let's try it.
\begin{compactitem}[$\quad\star$]
\item Type the following in a script file called {\tt apply2.R}, save
it to your {\tt Code} directory, and run it:
\end{compactitem}

\lstinputlisting[language=R]{Practicals/Code/apply2.R}

There are many other methods: {\tt lapply}, {\tt sapply}, {\tt eapply},
etc. Each is best for a given data type. For example, {\tt lapply} is
best for R lists. Have a look at {\scriptsize
\url{https://stackoverflow.com/questions/3505701/grouping-functions-tapply-by-aggregate-and-the-apply-family}}
for some guidelines.

\subsubsection{The {\tt tapply} function}
We will look at {\tt tapply}, which is particularly useful because it
allows you to apply a function to subsets of a vector in a dataframe,
with the subsets defined by some other vector in the same dataframe,
usually a factor (this could be useful for your Chapter \ref{chap:R_data}
pound hill data analysis, for example!).

This makes it a bit of a different member of the {\tt *apply} family.
Try this:
\begin{lstlisting}
> x <- 1:20 # a vector

# A factor (of the same length) defining groups:
> y <- factor(rep(letters[1:5], each = 4))

# Add up the values in x within each subgroup defined by y:
> tapply(x, y, sum)
 a  b  c  d  e
10 26 42 58 74
\end{lstlisting}

\subsection{Using {\tt by}}

You can also do something similar to {\tt tapply} with the {\tt by}
function, i.e., apply a function to a dataframe using some factor to
define the subsets. Try this:

\begin{lstlisting}
## import some data
attach(iris)
print (iris)

## use colMeans (as it is better for dataframes)
by(iris[,1:2], iris$Species, colMeans)
by(iris[,1:2], iris$Petal.Width, colMeans)
\end{lstlisting}

\subsection{Using {\tt replicate}}

The {\tt replicate} function is useful to avoid a loop for function
that typically involves random number generation (more on this below).
For example:

\begin{lstlisting}
> print(replicate(10, runif(5)))
\end{lstlisting}
This generates a 10 $\times$ 5 matrix of uniformly distributed random
numbers.

\subsection{Using {\tt plyr} and {\tt ddply}}

The {\tt plyr} package combines the functionality of the {\tt *apply}
family, into a few handy functions. Look up
\url{http://plyr.had.co.nz/}.

In particular, {\tt ddply} is very useful, because for each subset of a
data frame, it applies a function and then combines results into
another data frame (very useful for Practical \ref{sec:PPPrac2} in Chapter
\ref{chap:R_data}!). In other words, ``ddply '' means: take a data frame,
split it up, do something to it, and return a data frame.

Look up \url{http://seananderson.ca/2013/12/01/plyr.html} and
\url{https://www.r-bloggers.com/transforming-subsets-of-data-in-r-with-by-ddply-and-data-table/}
for examples.	There you will also see a comparison of speed of {\tt
ddply} vs {\tt by} at the latter web page--- {\tt ddply} is actually
slower than other vectorized methods, as it trades-off compactness of
use for some of the speed of vectorization! Indeed, overall functions
in {\tt plyr} can be slow if you are working with very large datasets
that involve a lot of subsetting (analyses by many groups or grouping
variables).

\begin{tipbox}
The base {\tt *apply} functions remain useful and worth
knowing even if you do get into {\tt plyr} or better still, {\tt
dplyr} (see Chapter \ref{chap:R_data})
\end{tipbox}

\section{Some more control flow tools}

Let's look at some more control tools, which we first learned about in
Chapter \ref{chap:RI}.

\subsection{{\tt break}ing out of loops}
Often it is useful (or necessary) to {\tt break} out of a loop when
some condition is met. Use {\tt break} (like in pretty much any other
programming language, like {\tt python}) in situations when you cannot
set a target number of iterations, as you would with a {\tt while} loop
(Chapter 1). Try this (type into {\tt break.R} and save in {\tt Code}):

\lstinputlisting[language=R]{Practicals/Code/break.R}

\subsection{Using {\tt next}}
You can also skip to next iteration of a loop. Both {\tt next} and {\tt
break} can be used within other loops ({\tt while}, {\tt for}). Try
this (type into {\tt next.R} and save in {\tt Code}):

\lstinputlisting[language=R]{Practicals/Code/next.R}

This script checks if a number is odd using the ``modulo''
operation and prints if it is.

\begin{tipbox}
{\bf Reminder}: Indent your code! Indentation helps you see
the flow of the logic, rather than flattened version, which is hard for
you and everybody else to read. I recommend using the {\it tab} key to
indent.
\end{tipbox}

\section{Practicals}

\begin{enumerate}

\item {\bf A vectorization challenge}

The Ricker model is a classic discrete population model which was
introduced in 1954 by Ricker to model recruitment of stock in fisheries.
It gives the expected number (or density) $N_{t+1}$ of individuals in
generation $t + 1$ as a function of the number of individuals in the
previous generation $t$:
\begin{equation}
N_{t+1} = N_t e^{r\left(1-\frac{N_t}{k}\right)}
\end{equation}
Here $r$ is intrinsic growth rate and $k$ as the
carrying capacity of the environment. Try this script that runs it:

\lstinputlisting[language=R]{Practicals/Code/Ricker.R}

Now open and run the script {\tt Vectorize2.R} (available on the
bitbucket repository). This is the stochastic Ricker model (compare
with the above script to see where the stochasticity (random error)
enters). Now modify the script to complete the exercise given.

{\it You will be marked on this one based upon how much faster your solution
is compared to mine!}

\item {\bf Extra Credit:} Implement the python versions of {\tt
Vectorize1.R} and {\tt Vectorize2.R} (call them {\tt
Vectorize1.py} and {\tt Vectorize2.py} respectively). Then write a bash
script that compares the computational speed of the four scripts. the
bash script should display meaningful summary of the results in the
terminal.

\end{enumerate}

\section{Generating Random Numbers}

You will probably need to generate random numbers at some point in your
journey towards becoming a proper data analyst or quantitative
biologist.

R has many routines for generating random samples from various
probability distributions --- we have already used {\tt runif()}, {\tt
rnorm()}. There are a number of random number distributions that you can sample
or generate random numbers from:

\begin{tabular}{p{5.4cm} p{9cm}}
	{\tt rnorm(10, m=0, sd=1)} & Draw 10 normal random numbers with mean 0 and s.d. 1\\
	{\tt dnorm(x, m=0, sd=1)} & Density function\\
	{\tt qnorm(x, m=0, sd=1)} & Cumulative density function\\
	{\tt runif(20, min=0, max=2)} & Twenty random numbers from uniform
			[0,2]\\
	{\tt rpois(20, lambda=10)} & Twenty random numbers from
			Poisson($\lambda$)\\
\end{tabular}\\

\subsection{``Seeding'' random number generators}
Before proceeding further, note that computers don't really generate mathematically random numbers, but
instead a sequence of numbers that are close to random: ``pseudo-random
numbers''. They are generated based on some iterative formula:

\[ x_{new} = f( x_{old}) \mod N \]

where modulo operation provides the ``remainder'' division.

So to generate the first random number, you need a {\bf seed}. Setting
the seed allows you to reliably generate the same sequence of numbers,
which can be useful when debugging programs (next section).

Now, try this:

\begin{lstlisting}
> set.seed(1234567)
> rnorm(1)
 0.1567038
\end{lstlisting}

What happened?! If this were truly a random number, how would everybody
get the same answer? Now try {\tt rnorm(10)} and compare the results
with your neighbour. Thus "random" numbers generated in R and in any
other software are in fact "deterministic", but from a very complex
formula that yields numbers with properties like random numbers.

Effectively, {\tt rnorm} has an enormous list that it cycles through.
The random seed starts the process, i.e., indicates where in the list
to start. This is usually taken from the clock when you start R.

But why bother with this? Well, for debugging (next section). Bugs in
code can be hard to find --- harder still if you are generating random
numbers, so repeat runs of your code may or may not all trigger the
same behaviour. You can set the seed once at the beginning of the code
--- ensuring repeatability, retaining (pseudo) randomness. Once
debugged, if you want, you can remove the set seed line.

To try out how sampling works, type the following into {\tt sample.R}
and save in {\tt Code}:

\lstinputlisting[language=R]{Practicals/Code/sample.R}

\section{Errors and Debugging}

\subsection{"Catching" errors}

Often, you don't know if a simulation or a R function will work on a
particular data or variable, or a value of a variable (can happen in
many stats functions).

Indeed, as most of you must have already experienced by now, there can
be frustrating, puzzling bugs in programs that lead to mysterious
errors. Often, the error and warning messages you get are
un-understandable, especially in R!

Rather than having R throw you out of the code, you would rather catch
the error and keep going. This can be done using {\tt try}. Modify {\tt
sample.R} as follows the
following into {\tt try.R} and save in {\tt Code} (what does this
script do?):

\lstinputlisting[language=R]{Practicals/Code/try.R}

Note the functions {\tt sample} and {\tt stop} in the above script.
Also check out {\tt tryCatch}.

\subsection{Debugging}

Once you have found an error, you would like to fix it. This is called
debugging. Here are some useful debugging functions in R :

\begin{itemize}\itemsep2pt

\item Warnings vs Errors; converting warnings to errors: {\tt
stopifnot()} --- a bit like {\tt try}

\item What to do when you get an error: {\tt traceback()}

\item Simple {\tt print} commands in the right places can be useful
for testing (but not strongly recommended)

\item Use of {\tt browser()} at key points in code --- my favourite option
(also look up  {\tt recover()})

\item {\tt debug(fn)}, {\tt undebug(fn)} : More technical approach to
debugging --- explore them
\end{itemize}

Let's look at an example using {\tt browser()}. {\tt browser()} is
handy because it will allow you to "single-step" through your code.
Place it within your function at the point you want to examine (e.g.)
local variables.

Here's an  example usage of {\tt browser()} (type in {\tt browse.R} and
save in {\tt Code}):

\lstinputlisting[language=R]{Practicals/Code/browse.R}

Now, within the browser, you can enter expressions as
normal, or you can use a few particularly useful debug commands:
\begin{itemize}
\item {\tt n}: single-step
\item {\tt c}: exit browser and continue
\item {\tt Q}: exit browser and abort, return to top-level.
\end{itemize}

\section{Building your own R packages}

You can packaging up your code, data sets and documentation to make a
{\it bona fide} R package. You may wish to do this for particularly
large projects that you think will be useful for others. Read {\it
Writing R Extensions}
(\url{cran.r-project.org/doc/manuals/r-release/R-exts.html} manual and
see {\it package.skeleton} to get started. The R tool set EcoDataTools
(\url{https://github.com/DomBennett/EcoDataTools}) and the package
{\tt cheddar} were written by Silwood Grad Students!

\section{Sweave and knitr}

Sweave and knitr are tools that allows you to write your Dissertation
Report or some other document such that it can be updated automatically
if data or R analysis change. Instead of inserting a prefabricated
graph or table into the report, the master document contains the R code
necessary to obtain it. When run through R, all data analysis output
(tables, graphs, etc.) is created on the fly and inserted into a final
document, which can be written using \LaTeX, LyX, HTML, or Markdown.
The report can be automatically updated if data or analysis change,
which allows for truly reproducible research. Check out
\url{https://support.rstudio.com/hc/en-us/articles/200552056-Using-Sweave-and-knitr} and
\url{http://yihui.name/knitr/}.

\section{Practicals}

\begin{enumerate}

\item Autocorrelation in weather (this Practical may make more sense
once you have done the R and Stats week where you will learn about
correlation coefficients and p-values).

\begin{compactitem}[$\quad\star$]

\item Make a new script named {\tt TAutoCorr.R}, and save in {\tt Code}
directory

\item At the start of the script, load and examine and plot \\ {\tt
KeyWestAnnualMeanTemperature.Rdata}, using {\tt load()} --- This is the
temperature in Key West, Florida for the 20th century.

\item {\bf The question this script will help answer is}: Is the
temperature of one year significantly correlated with the next year
(successive years), {\it across the years}. That us you will be
calculating the correlation between $n-1$ pairs of years, where $n$ is
the total number of years. However, you can't use the standard p-value
calculated for a correlation coefficient (using {\tt R}'s {\tt cor()}
function -- see below) because measurements of climatic variables in
successive time-points in a time series (successive seconds, minutes,
hours, months, years, etc.) are {\it not independent}.

\item Therefore,
\begin{enumerate}\itemsep0pt
\item Compute the appropriate correlation coefficient between successive
years and store it (look at the help file for {\tt cor()}
\item Then repeat this calculation 10000 times by\\
-- randomly permuting the time series (Hint: you can use the {\tt
sample} function that we learned about in this Chapter --- read the
help file for this function and experiment with it),\\
-- then computing the correlation coefficient for each randomly permuted
year sequence and storing it\\
\end{enumerate}

\item Then calculate what fraction of the correlation coefficients from
step 2 were greater than that from step 1 (this is your approximate
p-value).

\item {\it How do you interpret these results? Why? Present your
results and their interpretation in a pdf document written in \LaTeX
(please include the the document's source code as well).}

\end{compactitem}

\item{Mapping} (Extra Credit)

Your project may not really need GIS, but you may still like/need to do
some mapping. You you can do it in R using the {\tt maps} package. In
this practical, you will map the Global Population Dynamics Database
(\url{https://www.imperial.ac.uk/cpb/gpdd/gpdd.aspx}) (GPDD). This is a
freely available database that was developed at Silwood).

If any of you are interested in doing a project around this database,
please contact David Orme or Samraat Pawar! It is a gold mine of as yet
under-utilized information. Note that the Living Planet Index
(\url{http://livingplanetindex.org/home/index}) is based upon these
data.

\begin{compactitem}[$\quad\star$]
\item Use {\tt load()} from {\tt GPDDFiltered.RData} that is available
on the bitbucket git repository --- have a look at the database field headers and
contents.
\item What you need is latitude and longitude information for a bunch of
species for which population time series are available in the GPDD
\item Now use {\tt install.packages()} to install the package {\tt
maps}, as you did with {\tt ggplot2} --- hopefully without any
problems!
\item Now create a script (saved under a sensible name in a sensible
location --- hint hint!) that:
	\begin{enumerate}\itemsep0pt
		\item Loads the maps package
		\item Loads the GPDD data
		\item Creates a world map (use the map function, read its help
		file, also google examples using {\item maps})
		\item  Superimposes on the map all the locations from which we have
		data in the GPDD dataframe
		\item Compare your map with a fellow student to check
	\end{enumerate}

\item {\it Based on this map, what biases might you expect in any analysis
based on the data represented? --- {\it include your answer as a comment at
the end of your R script}. }
\end{compactitem}

\end{enumerate}

\section{R Module Wrap up}

 \subsection{Some comments and suggestions}

Thanks for enduring through the week! Learning to program in R or any
other language, especially if it's your first-ever effort to learn
programming, demands perseverance. Y'all have shown admirable
quantities of this necessary quality. Keep going! I believe most if not
all of you have climbed a significant part of a steep learning curve.
Here are some things to keep in mind:

\begin{itemize}\itemsep2pt

\item There are many R nerds at Silwood that you can talk to --- {\it
They walk among us!}

\item There is a Silwood R list that you can subscribe to:
\url{https://mailman.ic.ac.uk/mailman/listinfo/silwood-r}

\item However, post questions only as a last resort! Google it first,
and even before that, make sure you revise this week's (and stats
week's) work.

\item Solutions to this weeks Practicals will become available by 15th
Nov.

\end{itemize}

\section{Practicals wrap-up}

  \begin{enumerate}

	\item Review and make sure you can run all the commands, code
	fragments, and named scripts we have built till now and get the
	expected outputs.

	\item Annotate/comment your code lines as much and as often as
	necessary using \#.

	\item Keep all code, data and results files organized in the {\tt
	Week3/} directory

   \end{enumerate}

\begin{center}
	\it {\tt git add}, {\tt commit} and {\tt push} all your code and data
	from this chapter to your git repository by Wednesday, Nov 2, 5PM.
\end{center}

\section{Readings}

\begin{itemize}

	\item See {\bf An introduction to the Interactive Debugging Tools in R,
	Roger D Peng} for detailed usage.
	\url{http://www.biostat.jhsph.edu/~rpeng/docs/R-debug-tools.pdf}

	\item Friedrich Leisch. (2002) Sweave: Dynamic generation of statistical
	reports using literate data analysis. Proceedings in Computational
	Statistics, pages 575-580. Physica Verlag, Heidelberg, 2002.
	\url{http://www.statistik.lmu.de/~leisch/Sweave}

	\item Remember, R packages come with pdf guides/documentation!

\end{itemize}


\documentclass[12pt]{article}
\usepackage[usenames]{color} %used for font color
\usepackage{amsmath, amssymb, amsthm}
\usepackage{wasysym}
\usepackage[utf8]{inputenc} %useful to type directly diacritic characters
\usepackage{graphicx}
\usepackage [english]{babel}
\usepackage [autostyle, english = american]{csquotes}
\MakeOuterQuote{"}
\graphicspath{ {./} }
\newcommand{\Z}{\mathbb{Z}}
\newcommand{\N}{\mathbb{N}}
\newcommand{\R}{\mathbb{R}}
\newcommand{\Q}{\mathbb{Q}}
\newcommand{\prob}{\mathbb{P}}
\newcommand{\degrees}{^{\circ}}

\author{Tianshuang (Ethan) Qiu}
\begin{document}
\title{Math 74, Week 4}
\maketitle

\section{Lec Mon, 1c}
\subsection{a}
Since each term is the product of $x^a, y^b, z^c$, and $a+b+c = 2020$, we can simplify this problem into dogs and biscuits with 2020 biscuits and 3 dogs: $\binom{2020+3-1}{3-1} = \binom{2022}{2}$

\subsection{b}
Before combining, we expand each term by picking one variable from each of the 2020 $(x+y+z)$ multiplied together. So we have $3^{2020}$.

\subsection{c}
This is a "no dogs go hungry" problem. We can just simply pick $x,y,z$ from the first three brackets, feeding each dog a biscuit. Now we have 2017 biscuits with 3 dogs $\binom{2017+3-1}{3-1} = \binom{2019}{2}$.

\subsubsection{Alternate solution}
We can reach the same result by subtracting the amount where there is only $x$, or only $y$, or only $z$, or $xy$, $xz$, $yz$.
\newline
For the first three, there is only 1 way for that to happen since that variable has to be raised to 2020. For $xz$, we have $a+b = 2020$, feeding 2018 biscuits to 2 dogs. We need to subtract 2 since we have already counted having only one term. Therefore $\binom{2018+2-1}{2-1} = 2019$.
\newline
Adding them together we have $1 \times 3 + 2019 \times 3 = 6060$. Now we subtract it from what we had in part (a): $\binom{2022}{2}-6060$ which is the same as $\binom{2019}{2}$
\newpage

\section{Dis Mon, 1a}
LHS is the amount of ways to choose a team with $k$ people and a captain from a group with $n$ people. It chooses the team first: $\binom{n}{k}$. Then from that team we choose a captain with $k$ ways to do it.
\newline
RHS calculates the amount of ways to choose a captain first: $n$, then the rest of the team: $\binom{n-1}{k-1}$. Both sides calculate the same thing. Therefore LHS = RHS.
\newline
Q.E.D.

\section{Dis Mon, 4}
$$x+\frac{1}{x}=7$$
$$(x+\frac{1}{x})^2=49$$
$$x^2+\frac{1}{x^2}+2=49$$
$$x^2+\frac{1}{x^2}=47$$
\newpage

\section{Lec Wed, 1a}

\begin{figure}[h]
    \includegraphics{GRAPH1}
\end{figure}
The sum of these angles is 90$\degrees$.
\newline
Proof: We attempt to move all three angles to $\angle HBC$, since a square has 4 right angles, we can see that $\angle HBC = 90\degrees$.
\newline
We construct a row of 3 identical squares with $A'M' = AM, M'H' = MH, etc$, and connect $DH', H'B$
\newline
Consider $\triangle DAM$, it is a right isosceles triangle, since it is in a square ($DA = AM, DA \perp AM$). Now consider $\triangle DA'H', \triangle H'B'B$. Since all the squares have the same side length, we have $DA' = H'B', A'H'=B'B$. Furthermore, since these are all squares, we have $\angle DA'H' = \angle H'B'B = 90\degrees$.
\newline
Therefore $\triangle DA'H' \cong \triangle H'B'B$ by SAS property. $DH' = H'B, \angle H'B'B = \angle DH'A$. Since the sum of the three angles in a triangle is $180\degrees$ and $\angle H'B'B = DA'H' = 90\degrees$, we can see that $\angle B'H'B + \angle AH'D = 90\degrees$. Finally, since M'H'B' forms a line $\angle M'H'B' = 180 \degrees$, we have $\angle DH'B = \angle DAM = 90 \degrees$.
\newline
Now we have $\triangle DH'B \sim \triangle DAM$ by RAR property for similarity, therefore $\angle AMD = \angle H'BD$.
\newline
By essentially the same logic as $\triangle DA'H' \cong \triangle H'B'B$, we can prove that $\triangle DAH \cong \triangle H'B'B$, so $\angle DHA = \angle B'BH$.
\newline
We have proven that $\angle AMD = \angle H'BD$, $\angle DHA = \angle B'BH$. Since the sum these two and $\angle DBA$ is a right angle, we have proven our claim.
\newpage

\section{Lec Wed, 1b}
\begin{figure}[h]
    \includegraphics[width = 100mm]{GRAPH2.png}
\end{figure}
We reflect F over $AD$ to $F'$, and connect $F'C$. Now we first show that B is the optimal point.
\newline
By the definiton of reflection, we can see that if we were to reflect $F'$ back over $AD$ it will overlap with our original point. This "overlap" causes all three vertices of $\triangle ABF$ to overlap with those of $\triangle ABF'$. By Euclid's definition they are congruent.
\newline
Consider a point on $AD$ that is not $B: B'$. By similar reasoning we can also show that $\triangle AB'F \cong \triangle AB'F'$.
\newline
From these congruencies we see that $B'F = B'F', BF = BF'$. The farmers route can then be converted into $F'B+B'C$ and $F'B + BC$. Furthermore, since F'B'C forms a triangle, we have $F'B+B'C > F'B + BC$ by the triangle inequality, thus proving that B gives the shortest route to the cow.
\newline
Since $FF' \perp AD$ and $CD \perp AD$, we have $FF' \parallel CD$. By the property of alternate interior angles, $\angle FF'C = \angle F'CD$.
\newline
$\angle ABF' + \angle ABC = 180 \degrees$ since F'C is a line. By the same logic, $\angle CBD + \angle ABC  = 180 \degrees$. Therefore $\angle ABF' = DBC$. We have now shown $\triangle ABF' \sim \triangle CBD$ by AAA property.
\newline
$AF = AF' = 2$. Let $AB = x$, $BD$ would then equal $4-x$. By property of similarity we have $\frac{CD}{F'A} = \frac{DB}{AB}$
$$\frac{6}{2} = \frac{4-x}{x}$$
Solving the above equation yields $x=1$.
\newpage

\section{Lec Wed, 2b}
\begin{figure}[h]
    \includegraphics[width = 100mm]{GRAPH3.png}
\end{figure}
Let $AD = CD, CE = EB$, the two segments intersect at O. Connect $BD$, $AE$, $DE$, $CO$, and extend $CO$ to intersect $AB$ at $F$.
\newline
This assumes that 2a ($DE \parallel AB$, $DE = 0.5AB$) has already been proven.
\newline
Since $DE \parallel AB$, the alternate interior angles are equal, $\angle EDB = \angle DBA$, $\angle DEA = EAB$. Since the sum of all the angles of a triangle is $180 \degrees$, the third angle must also be the same. Therefore $\triangle DEO \sim \triangle BAO$ by AAA similarity.
\newline
Because $DE = 0.5AB$, $DO = 0.5BO, EO = 0.5 AO$ by the principles of similarity.
\newline
Using $DE \parallel AB$ again, we can see that $\angle EGF = \angle AFG$. By the same reasoning as above we can see that $\triangle GOE \sim \triangle FOA$ by AAA similarity. Therefore $$\frac{GE}{AF} = \frac{EO}{OA} = \frac{1}{2}$$
Since $DE \parallel AB$, we can show that $\angle CGE = \angle CFB$, $\angle CEG = \angle CBF$ due to the corresponding angles being equal. $\triangle CGE \sim \triangle CFB$ by AAA similarity. Since E is the midpoint of BC, $CE = 0.5 CB$, then by the principle of similarity, $GE = 0.5FB$
\newline
Since $GE = \frac{1}{2}FB = \frac{1}{2}AF$, $AF = FB$. Therefore all three medians intersect at point $O$.
\newpage

\section{Dis Wed, 1a}
\begin{figure}[h]
    \includegraphics[width = 100mm]{GRAPH7.png}
\end{figure}
Since these are all squares we have $DA = AM = BC = CJ$. And since $DM$ connects the diagonal of a square, $\angle DMA = \angle ADM = 45 \degrees$.
\newline
Let $DA$ have length $x$, then $DM$ would have length $\sqrt 2 x$. Since $\angle DMA = 45 \degrees$, its compliment $\angle DMH = 135 \degrees$. $MB$ has length $2x$, and $DM$ has length $x$.
\newline
$\frac{MH}{DM} = \frac{DM}{MB} = \frac{1}{\sqrt2}$, and since $\triangle DMH, \triangle BMD$ both have angle DMH, $\triangle DMH \sim \triangle BMD$ (RAR similarity), and $\angle DHM = \angle BDM$.
\newline
Now we prove a congruency between two right triangles. From the squares we have $AD = CB = x$,  $CD = AB = 3x$, and $\angle DAB = \angle BCD = 90 \degrees$, so $\triangle DAB \cong \triangle BCD$ by SAS congruency. Therefore $\angle ABD = \angle BDC$
\newline
Thus we have moved all three angles into the right angle $\angle ADC$, proving that the sum of these three angles is $90 \degrees$. Q.E.D.
\newpage

\section{Dis Wed, 3b}
\begin{figure}[h]
    \includegraphics[width = 100mm]{GRAPH6.png}
\end{figure}
Let $\triangle ABC$ be an arbitrary triangle, extend AC to E, construct $a \parallel AB$.
\newline
For this proof I need the axiom that a straight line is $180 \degrees$, and that the corresponding angles and alternate interior angles between two parallel lines are equal.
\newline
Since $a \parallel AB$, we have $\angle BAC = \angle DCE$, and $\angle ABC = \angle BCD$. Since the three angles form $\angle ACE$, which is a straight line, the 3 angles add up to $180 \degrees$.
\newpage

\section{Lec Fri, 3}
If the 5th postulate was indeed redundant (provable from others), then the 4 postulates alone must always define the Euclidean space, and that there should be no other spaces that could follow the first 4 postulates but not the fifth.
\newline
Hyperbolic geometry satisfies the first 4 with its own definitions of lines, circles, and right angles. The resulting space does not follow Euclid's 5th postulate: two lines whose sum of the inner angles on one side is less than $180 \degrees$ can still curve away from each other and never intersect.
\newline
This shows that we need Euclid's 5th postulate to properly define a Euclidean space.
\newpage

\section{Lec Fri, 4b}
Let us label "There is at most 1 parallel line to a given line $l$ through a given point $P$" "Statement A", and "two lines that are parallel to  the same line are also parallel to eachother" "Statement B".
\newline
For this problem we need to prove that $A \iff B$. We will begin by proving $A \implies B$.
\newline
\begin{figure}[h]
    \includegraphics[width = 100mm]{GRAPH5.png}
\end{figure}
\paragraph{1}
Assume that statement B is false, so the two lines are not parallel to each other. Then let $a \parallel c, b \parallel c$, and $a, c$ intersect at point $P$. Then at $P$, there are two different lines that are both parallel to $c$. \lightning
\newline
Therefore our initial assumption is incorrect, statement B is true.
\paragraph{2}
Now we try to prove that $B \implies A$. Assume that statement A is false, so there are at least 2 different lines that go through that point. Then let $a \parallel c, b \parallel c$, $a,b$ go through the same point $P$.
Then by statement B, $a \parallel b$, but $a, b$ both contain $P$, so they have to be the same line. \lightning
\newline
Therefore there exists at most one line through $P$ parallel to $c$.

\end{document}