\input{SingleAssignmentSetup.tex}
\input{../WeekTitles.tex}
\usepackage{bbding} % for Checkmarkbold
\begin{document}

\newcommand{\ub}{\underbrace}

\begin{center}
\subsection*{MNTC P01 - Week \#9 - \WeekTitleNine}
\end{center}

% % ******************** PENDULUM ********************************
\subsection*{Pendulum}
\begin{enumerate}[1.]

\item
  \begin{Question}
    \begin{minipage}[h]{0.6\linewidth}
\vspace{0pt}
Consider the motion of a frictionless pendulum.
\begin{align*}
  \mbox{Newton's Second Law: }   m  L^2 \theta'' & = T_g \\
  & = - m L g \sin(\theta)  \\
  \mbox{Solving for $\theta''$: }\theta'' & = - \frac{g}{L} \sin(\theta)
\end{align*}

{\bf Without} simulating the actual motion of the pendulum, we can
compute the period, $T$, using the formula below:
\begin{align*}
  T = 4 \sqrt{L/g} \int_0^{\pi/2} \frac{dx}{\sqrt{1 - k^2 \sin^2 x}}
\end{align*}
where $k = \sin\left(\frac{1}{2} \theta_0\right)$ and $g$ is the
acceleration due to gravity, $9.8 $ m/s.
    \end{minipage} \hfill
    \begin{minipage}[h]{0.25\linewidth}
     \includegraphics[width=1.0\linewidth]{graphics/Week09_Pendulum/pendulum_diagram}
    \end{minipage}

For each set of values for $L$ and $\theta_0$ given below,
\begin{enumerate}[(a)]
\item Use the MATLAB \verb#integral# function to find the period of
  the pendulum oscillations by evaluating the integral given above,
  and
\item confirm the period by using \verb#ode45# to simulate the motion
  of the pendulum for exactly that length of time, and plot a graph of
  the angular {\bf velocity} against time.  The velocity should just
  reach zero at the end of one cycle.
\end{enumerate}

Do this with the following sets of $L$ and $\theta_0$ values.
\begin{enumerate}[(i)]
\item $L = 2$ m, $\theta_0 = 40^o$,
\item $L =2.5$ m, $\theta_0 = 20^o$.
\item $L =5.0$ m, $\theta_0 = 90^o$.
\end{enumerate}
  \end{Question}

\begin{Solution}

Link to the MATLAB code: \\
\href{http://www.mast.queensu.ca/~apsc171/MNTCP01/PracticeProblems/MATLAB/W09Pendulum1.m}{W09Pendulum1.m} \\
\href{http://www.mast.queensu.ca/~apsc171/MNTCP01/PracticeProblems/MATLAB/pendulumDE.m}{pendulumDE.m} \\

Note that we simply re-used the \verb#pendulumDE.m# from the lectures,
and set the friction coefficient $\mu = 0$.
  \begin{enumerate}[(i)]
  \item $L = 2$ m, $\theta_0 = 40^o$:  {\bf T = 2.9274} seconds.
  \item $L =2.5$ m, $\theta_0 = 20^o$: {\bf T = 3.1978} seconds.
  \item $L =5.0$ m, $\theta_0 = 90^o$: {\bf T = 5.2974} seconds.
  \end{enumerate}

Plots: \\
\includegraphics[width=0.33\linewidth]{graphics/Week09_Pendulum/pendulum_nofriction_1}
\includegraphics[width=0.33\linewidth]{graphics/Week09_Pendulum/pendulum_nofriction_2}
\includegraphics[width=0.33\linewidth]{graphics/Week09_Pendulum/pendulum_nofriction_3}
\end{Solution}

In all the graphs, we see that that the velocity returns to 0 at the
end of the cycle, indicating that our integral calculation of the
period matches the period from the \verb#ode45# simulation.

%*******************************
\item
\begin{Question}
    \begin{minipage}[t]{0.7\linewidth}
\vspace{0pt}
Consider the motion of a pendulum, this time {\bf with} friction.
\begin{align*}
  \mbox{Newton's }& \mbox{ Second Law: } \\
   m  L^2 \theta'' & = T_g + T_f  \\
  & = - m L g \sin(\theta) - (\mu L^2 m) \theta' \\[3ex]
  \mbox{Solving for $\theta''$: }\theta'' & = - \frac{g}{L} \sin(\theta) - \mu
  \theta'
\end{align*}
\begin{enumerate}[(a)]
\item Write a MATLAB function for the differential equation, and a
  script that will simulate the scenario for $L = 1.5$ m, $g = 9.8$
  m/s$^2$, and $\mu = 0.2$.  Use an initial condition of
  $\theta_0 = \frac{7\pi}{8}$, which is close to vertical.
\item Experiment with the initial {\bf angular velocity} of the
  pendulum and find the smallest {\bf positive} initial velocity that
  will result in the pendulum passing over the top of the axis of
  rotation.  Find the value to the nearest 0.1 rad/s.

  Have MATLAB generate a plot of the angle vs time graph for both the
  initial velocity that achieves this result, and for the initial velocity 0.1 rad/s smaller, which does {\em not} go `over the top'.
\item Repeat the analysis in part (b), but this time using a {\bf
    negative} initial velocity.
\end{enumerate}
\end{minipage} \hfill
\begin{minipage}[t]{0.25\linewidth}
\vspace{0pt}
  \includegraphics[width=1.0\linewidth]{graphics/Week09_Pendulum/pendulum_diagram}
\end{minipage}

\end{Question}

\begin{Solution}

\begin{enumerate}[(a)]
\item
Link to the MATLAB code: \\
\href{http://www.mast.queensu.ca/~apsc171/MNTCP01/PracticeProblems/MATLAB/W09Pendulum2.m}{W09Pendulum2.m} \\
\href{http://www.mast.queensu.ca/~apsc171/MNTCP01/PracticeProblems/MATLAB/pendulumDE.m}{pendulumDE.m} \\

Here is the graph of the angle over time for the pendulum, when it has no initial velocity.

\includegraphics[width=0.45\linewidth]{graphics/Week09_Pendulum/pendulum_overtop_1}

\item With some experimentation, we find that an initial angular
  velocity of $\theta'(0) = 1.1$ rad/s will be enough to push the
  pendulum over the top of the axis of rotation. Comparing
  $\theta'(0) = 1.1$ and 1.0, we obtain the following graph of angle
  against time.

\includegraphics[width=0.5\linewidth]{graphics/Week09_Pendulum/pendulum_overtop_2}

\item With further experimentation, we find that using negative
  initial angular velocities requires a higher initial velocity
  compared to positive initial velocities, because friction eats away
  at the effect of that first push when we are going down first and
  then over the top.  Still, a value of $\theta'(0) = -3.3$ rad/s will
  be enough to push the pendulum over the top of the axis. Comparing
  $\theta'(0) = -3.3$ and -3.2, we obtain the following graph of angle
  against time.

\includegraphics[width=0.5\linewidth]{graphics/Week09_Pendulum/pendulum_overtop_3}
\end{enumerate}

\end{Solution}

\subsection*{Single Tank Problems}

%********************
\item
\begin{Question}
  An aquarium pool has volume $2 \times 10^6$ liters.  The pool
  initially contains pure fresh water. At $t=0$ minutes, water
  containing 10 grams/liter of salt is poured into the pool at a rate
  of 60 liters/minute. The salt water instantly mixes with the fresh
  water, and the excess mixture is drained out of the pool at the same
  rate (60 liters/minute).

   \begin{enumerate}[(a)]

   \item Write a differential equation for $S(t)$, the mass of
     salt in the pool at time $t$.

   \item Use MATLAB solve the differential equation to predict $S(t)$
     over time.

   \item Based on the graph of the solution, what happens to $S(t)$ as
     $t \to \infty$?

   \item Find this same value using only the information about the
     volume and the concentration of the incoming salt solution.

   \end{enumerate}

\end{Question}

\begin{Solution}
\begin{enumerate}
\item
\begin{align*}
  \text{Rate of change of salt amount (g/min)} & = \text{ Rate in } - \text{ Rate out }  \\
  \text{Rate in (g/min)} & = \text{Flow rate $\times$ Concentration} \\
  & = (60 \text{ liters/min}) \times (10 \text{ g/liter}) = 600 \text{ g/min} \\
  \text{Rate out (g/min)} & = \text{Flow rate $\times$ Concentration}  \\
  & =\text{Flow rate $\times$ amount (g) / Pool volume (liters)} \\
  & = (60 \text{ liters/min}) (S(t) \text{ grams}) / (2 \times 10^6 \text{ liters}) \\
  & = (3 \times 10^{-5}) S(t) \\
  \text{Finally, we get our DE: } ~~~~~ \ddt{S} &= 600 - (3 \times 10^{-5}) S \\
\end{align*}

\item Note: to see anything interesting in this simulation, you have
  to simulate for a {\bf long} simulation time, i.e. a long
  \verb#tspan#.  Here are two graphs of the simulation results, one
  with 1000 minutes, and then with 1 million minutes.

\includegraphics[width=0.45\linewidth]{graphics/Week09_Pendulum/single_tank_1a} \hfill
\includegraphics[width=0.45\linewidth]{graphics/Week09_Pendulum/single_tank_1b}

The reason a long simulation time is needed is because adding 60 L/min
to a 2 million liter pool takes a long time to have an effect.
\item As $t \to \infty$, we see the graph of $S(t)$ plateau at $S \to 2 \times 10^7$ grams.

\item We expect that the salt in the aquarium will tend to the same
  {\bf concentration} as the incoming water, as all of the original
  water is replaced with the new inflow solution.  At a concentration
  of 10 g/liter, in a volume of $ 2 \times 10^6$ liters, we expect to
  see eventually $S = C\times V = (10)(2 \times 10^6) = 2 \times 10^7$
  grams of salt in the aquarium, which matches our graphical results.

\end{enumerate}
\end{Solution}

% ****************************************
\item
\begin{Question}
  A 150 liter tank initially contains 60 liters of water with 0.5 kgs
  of salt dissolved in it.  Water enters the tank at a rate of 0.9
  liters/hr and the water entering the tank has a salt concentration
  of $\frac{1}{5}(1 + \cos (t))$ kgs/liter.
  \begin{enumerate}[(a)]
  \item Draw a diagram of the inflow and outflow for this scenario.
  \item Build a formula for the volume of water in the tank over time.
  \item Find out how long it will be until the tank overflows.
  \item Write a differential equation that describes the rate of
    change of the {\bf amount of salt} in the tank.
  \item Use MATLAB to generate a graph of the amount of salt in the
    tank over time, up until the tank overflows.
  \item How much salt is in the tank when it overflows?
  \end{enumerate}

\end{Question}

\begin{Solution}
  \begin{enumerate}[(a)]
  \item Here is a diagram of the system.

\includegraphics[width=0.45\linewidth]{graphics/Week09_SingleTanks/Tank1}

  \item Since the tank has a volume of 150 L, is gaining 0.9 - 0.6 = 0.3
  L/hour, and starts at 60 L, we obtain the volume expression $V(t) = 60 + 0.3 t$.

\item Solving $V(t) = 150$ for $t$, gives us $ 60 + 0.3 t = 150$, or
  $t = 300$ hours until the tank overflows.

\item The differential equation will be the same ``rate in - rate
  out'' form.
\begin{align*}
  \text{Rate of change of salt amount (kg/hr)} & = \text{ Rate in } - \text{ Rate out }
\end{align*}
\begin{align*}
  \text{Rate in (kg/hr)} & = \text{Flow rate $\times$ Concentration} \\
                         & = (0.9 \text{ liters/hr}) \times (\frac{1}{5} (1 + \cos(t)) \text{ kg/liter}) \\
                         & = 0.18(1 + \cos(t)) \text{kg/hr} \\
  \text{Rate out (kg/hr)} & = \text{Flow rate $\times$ Concentration}  \\
                         & =\text{Flow rate $\times$ amount (g) / Pool volume (liters)} \\
                         & = (0.6 \text{ liters/hr}) (S(t) \text{ kg}) / (60 + 0.3 t) \text{ liters}) \\
  \text{Finally, we get our DE: } ~~~~~ \ddt{S} &= 0.18 (1 + \cos(t)) - \frac{0.6}{60 + 0.3t} S
\end{align*}

\item Here is the graph of the predicted amount of salt in the tank over time.

  \includegraphics[width=0.75\linewidth]{graphics/Week09_SingleTanks/single_tank_2a}

  Note that the $\cos(t)$ effect has a short period ($2 \pi \approx $ 6 hours)
  relative to the 300 hours of the simulation time, which is why the
  graph looks like it has the high-frequency oscillations in it.

\item By either zooming in, or typing \verb#S# at the MATLAB command
  line to show all the \verb#S# values coming out of the simulation
  and grabbing the last one, we find at the end of 300 hours that
  there is $S(300) \approx 27.6212$ kg of salt in the tank.

  \end{enumerate}

\end{Solution}

% ******************** Fish Population ********************************
\subsection*{Other First Order Models}
\item
  \begin{Question}

Differential equations are not only well-suited for physics
  applications: they are are also widely used in biology, particularly
  in population models.

  Consider the fish population model below, based on a standard
  limited-resource population growth, minus a function of harvesting.

$$\frac{dP}{dt} =\underbrace{ [(10 -P)\cdot P]}_{\mbox{natural population growth rate}} -\underbrace{h(t)}_{\mbox{harvesting rate}}$$
where
\begin{itemize}
\item $P$  = population of fish (in thousands), and
\item $\frac{dP}{dt}$  = rate of population change, in thousands per
year
\item $h(t)$ is the harvesting rate (in thousands of fish per
year)
\end{itemize}

We want to study the impact of two harvesting models:
\begin{itemize}
\item $h_1 = k_1$; constant harvesting
\item $h_2(t) = k_2 (\sin(\pi t) + 1)$; seasonal model where the
  harvesting has a yearly cycle.
\end{itemize}
\begin{enumerate}[(a)]

\item Generate a prediction of the population over time, starting at
  initial populations of $P(0) = 15$ for each model.  Use $k_1 = k_2 =
  5$. Produce a graph showing the predicted population over time on
  the same graph, over a long enough time interval to show the
  long-term behaviour of both solutions.

\vspace{0.2in}

One question that arises in such harvesting models is which fishing
strategy permits a higher average harvesting rate can be maintained:
seasonal harvesting, or constant harvesting?  To decide this, we note
that the average harvest rate for $h_1$ is $k_1$, and for $h_2$ is
$k_2$, so whichever value of $k_1$ and $k_2$ is larger indicates the
strategy with the greater average harvesting rate.

We will define the {\em maximum sustainable harvest rate} for both
models as the {\em highest harvest rate for which the population is
  not driven to zero.}

\item Find and report the maximum sustainable harvest level $k_1$ for
  the constant harvesting model (to the nearest integer).  (Use trial
  and error if necessary, though more insightful DE-related ways are
  possible.)  Indicate how you found the cut-off level.

  {\em NOTE: during this process, your model will predict a population
    of zero, which will then lead to large negative populations.  This
    clearly makes no sense, so limit your plots with the command
    \verb#ylim([0, P0])#.  This same problem will also trigger
    warnings in ode45 about error tolerances; you can safely ignore
    those warnings.}

\item Generate a plot showing the population over time, using the same
  initial value used earlier, but using both the $k_1$ value just
  above, and just below the extinction level. (One line should remain
  positive, while the other should crash to zero at some point on the
  graph.)

\item Use trial and error (theory isn't much help here) to find the
  maximum sustainable harvest level $k_2$ for the cyclic harvesting
  model (to the nearest integer).  Include a plot showing the
  population over time with this harvesting level.

\item Based on your experiments, can constant harvesting or cyclic
  harvesting sustain a greater average harvest in the long run?
  Explain your reasoning.
\end{enumerate}
  \end{Question}

\begin{Solution}
\begin{enumerate}[(a)]
\item  The code in the link below generates the basic graph of the populations over time.  It can be adapted to help answer the later sections.

  Link to the MATLAB code: \\
  \href{http://www.mast.queensu.ca/~apsc171/MNTCP01/PracticeProblems/MATLAB/W09PopulationModel1.m}{W09PopulationModel1.m} \\

\includegraphics[width=3in]{graphics/Week09_PopulationModels/population_harvesting_1a}

\item Through trial and error, we find that there is a transition in
  behaviour between $k_1 = 25$ and $k_1 = 26$.
\begin{center}
\includegraphics[width=3in]{graphics/Week09_PopulationModels/population_harvesting_1b}
\end{center}

Notice that the \verb#ylim([0, P0])# line in the script is very
important, because once the population $P$ drops below zero, the
differential equation solution will keep going down forever.  Here's
the graph {\em without} the \verb#ylim# command:
\begin{center}
\includegraphics[width=3in]{graphics/Week09_PopulationModels/population_harvesting_1c}
\end{center}
Notice the vertical scales shows $10^{13}$, indicating that the
negative values predicted by the model are huge, and much larger than
the initial population of $P_0 = 15$.  However, all those negative
values are essentially meaningless for populations, so using the
\verb#ylim# command to focus the attention on the relevant population
range is a reasonable tactic to make the graphs useful.

\item In the seasonal harvest scenario, we experiment with values of
  $k_2$, and find that between $k_2 = 16$ and $k_2 = 17$, we see our
  seasonal pattern stop repeating and start reaching extinction:

\begin{center}
\includegraphics[width=3in]{graphics/Week09_PopulationModels/population_harvesting_1d}
\end{center}

\item Based on these experiments, it seems that seasonal harvesting
  leads to extinction at lower average harvesting levels, because a
  lower average rate of harvest ($k_2$ = 17 thousand fish per year)
  leads to extinction, compared to the constant harvest case (where
  $k_1$ = 26 thousand fish per year can be harvested).
\end{enumerate}
\end{Solution}

% ********************** Tank Systems ****************
\subsection*{Multi-Tank Systems}

\item \begin{Question}
Consider two interconnected tanks.  Tank A initially contains $100 \; \text{L}$
of water and $200 \; \text{g}$ of salt, and tank $B$ initially contains $200 \;
\text{L}$ of water and $900 \; \text{g}$ of salt.  The liquid inside each tank is
kept well stirred.
\begin{itemize}
\item Liquid flows from tank $A$ to tank $B$ at a rate of $3 \;
\text{L} \cdot \text{min}^{-1}$;
\item liquid flows from tank $B$ to tank $A$ at rate of $2 \; \text{L} \cdot \text{min}^{-1}$.
\item A salt brine with concentration $7 \; \text{g} \cdot \text{L}^{-1}$ of salt flows into tank $A$ at a rate of $5 \; \text{L} \cdot \text{min}^{-1}$; an outflow pipe drains out of the Tank A at $4 \; \text{L} \cdot \text{min}^{-1}$.
\item Moreover, a salt brine with concentration
  $3 \; \text{g} \cdot \text{L}^{-1}$ of salt flows into tank $B$ at a
  rate of $7 \; \text{L} \cdot \text{min}^{-1}$; the solution is drained
  out at $8 \; \text{L} \cdot \text{min}^{-1}$.
\end{itemize}
\begin{enumerate}
\item Draw a diagram for the flows and concentrations in this scenario.
\item Write a set of differential equations for the {\bf amount} of
  salt in each tank.
\item Use MATLAB to simulate the concentration of salt in each tank
  over time.  Generate two separate graphs, one for Tank A and one for
  Tank B, and use the \verb#title# command to label each one.
\item Use the solution generated by MATLAB to estimate when Tank B
  reaches its lowest salt amount, and what that lowest amount is.
\end{enumerate}

\end{Question}

\begin{Solution}
  \begin{enumerate}
  \item Here is a diagram with all the information added.
\begin{center}
\includegraphics[width=0.6\linewidth]{graphics/Week09_TankSystems/TankDiagram1}
\end{center}
\item   Let $S_A(t)$ and $S_B(t)$ denote the amount (in grams) of salt in tanks $A$
  and $B$ respectively at time $t$ (in minutes).  It follows that
  \begin{align*}
    \frac{dS_A}{dt} &= \text{input rate} - \text{output rate} = \left( 7
      \frac{\text{g}}{\text{L}} \right) \left( 5 \frac{\text{L}}{\text{min}}
    \right) + \left( \frac{S_B \; \text{g}}{200 \; \text{L}} \right) \left( 2
      \tfrac{\text{L}}{\text{min}} \right) - \left( \frac{S_A \; \text{g}}{100
        \; \text{L}} \right) \left( 4 + 3 \frac{\text{L}}{\text{min}} \right) \\
    &= - \frac{7}{100} S_A + \frac{2}{200} S_B + 35 \\
    \frac{dS_B}{dt} &= \left( 3 \frac{\text{g}}{\text{L}} \right) \left( 7
      \frac{\text{L}}{\text{min}} \right) + \left( \frac{S_A \; \text{g}}{100 \;
        \text{L}} \right) \left( 3 \frac{\text{L}}{\text{min}} \right) - \left(
      \frac{S_B \; \text{g}}{200 \; \text{L}} \right) \left( 2+8
      \tfrac{\text{L}}{\text{min}} \right) \\
    &=  \frac{3}{100} S_A - \frac{10}{200} S_B + 21 \, ,
  \end{align*}
  The coefficients in these expressions could be simplified
  (e.g. $\frac{10}{200} = \frac{1}{20} = 0.05$) but keeping the
  origins of each element of the coefficient is sometimes helpful for
  identifying any errors.  E.g. $\frac{2}{200}S_B$ comes from a flow
  rate of $2$ L/min, a volume of 200 L in Tank B, and the amount of
  salt $S_B$.

\item To implement this system of equations in MATLAB, we define a
  vector $\vec{w} = [S_A, S_B]$, and then write the differential
  equation system in a separate MATLAB function file, here called \\
\href{http://www.mast.queensu.ca/~apsc171/MNTCP01/PracticeProblems/MATLAB/tankSystem1.m}{tankSystem1.m}

The main script that sets up the initial conditions and then uses \verb#ode45# to solve the equations is in
\href{http://www.mast.queensu.ca/~apsc171/MNTCP01/PracticeProblems/MATLAB/W09TankSystem1.m}{W09TankSystem1.m}

The solution produces the two simulated salt concentrations shown
below.
\begin{center}
\includegraphics[width=0.45\linewidth]{graphics/Week09_TankSystems/tank_system_1a} \hfill
\includegraphics[width=0.45\linewidth]{graphics/Week09_TankSystems/tank_system_1b}
\end{center}
\item Tank B reaches its minimum salt amount at approximately
  $t = 27-28$ minutes, and has a level of 750 g at that moment.
  \end{enumerate}
\end{Solution}

%*******************************
\item
\begin{Question}
  Consider the 3-tank system shown below.
\begin{center}
\includegraphics[width=0.65\linewidth]{graphics/Week09_TankSystems/TankDiagram2}
\end{center}
\begin{enumerate}
\item Write the set of differential equations that governs the {\bf
    amount} of salt in each tank, $S_A$, $S_B$ and $S_C$.
\item Convert your answer from part (a) into a set of differential
  equations for the {\bf concentrations} in each tank, $C_A$, $C_B$
  and $C_C$.
\item Using the first line \\
  \verb#function dw_dt = tankSystem2(t, w, k, VA, VB, VC)# and the
  definition that $\vec{w} = [C_A, C_B, C_C]$ to group the three
  dependent variables, write a MATLAB function file that implements
  the differential equation system from part (b).
\item Write a script that simulates the changes in concentration over
  time, using the volumes shown in the diagram, a flow rate of $k = 2$
  L/min for each connection, and a time span of 250 minutes.

\item Use your knowledge of chemistry to explain the fact that all the
  tanks converge to a common concentration of 2.5 g/L.
\end{enumerate}
\end{Question}

\begin{Solution}
  \begin{enumerate}[(a)]
  \item The differential equations are all based on the ``net rate =
    rate in - rate out''.  The new feature is that {\bf every}
    concentration will of the form $\ds \frac{S}{V}$.
  \begin{align*}
    \frac{dS_A}{dt} &  \left(\frac{\text{g}}{\text{min}} \right) = \text{input rate} - \text{output rate} =
 \left( k \frac{\text{L}}{\text{min}} \right)
\left( \frac{S_C}{V_C} \frac{\text{g}}{\text{L}} \right)
- \left( k \frac{\text{L}}{\text{min}} \right)
\left( \frac{S_A}{V_A} \frac{\text{g}}{\text{L}} \right)  \\
& = k \frac{S_C}{V_C} - k \frac{S_A}{V_A}  \\
    \frac{dS_B}{dt} &  \left(\frac{\text{g}}{\text{min}} \right) = \text{input rate} - \text{output rate} =
 \left( k \frac{\text{L}}{\text{min}} \right)
\left( \frac{S_A}{V_A} \frac{\text{g}}{\text{L}} \right)
- \left( k \frac{\text{L}}{\text{min}} \right)
\left( \frac{S_B}{V_B} \frac{\text{g}}{\text{L}} \right)  \\
& = k \frac{S_A}{V_A} - k \frac{S_B}{V_B}  \\
    \frac{dS_C}{dt} &  \left(\frac{\text{g}}{\text{min}} \right) = \text{input rate} - \text{output rate} =
 \left( k \frac{\text{L}}{\text{min}} \right)
\left( \frac{S_B}{V_B} \frac{\text{g}}{\text{L}} \right)
- \left( k \frac{\text{L}}{\text{min}} \right)
\left( \frac{S_C}{V_C} \frac{\text{g}}{\text{L}} \right)  \\
& = k \frac{S_B}{V_B} - k \frac{S_C}{V_C}
  \end{align*}
\item Converting each of these differential equations to concentrations requires building off the two relationships \\
$\ds  C = \frac{S}{V}$ and $\ds \frac{dC}{dt} = \frac{1}{V} \frac{dS}{dt}$.
\begin{align*}
    \frac{dC_A}{dt}
& = \frac{1}{V_A} \frac{dS_A}{dt}
 = \frac{k}{V_A} C_C - \frac{k}{V_A} C_A \\
    \frac{dC_B}{dt}
& = \frac{1}{V_B} \frac{dS_B}{dt}
 = \frac{k}{V_B} C_A - \frac{k}{V_B} C_B \\
    \frac{dC_C}{dt}
& = \frac{1}{V_C} \frac{dS_C}{dt}
 = \frac{k}{V_C} C_B - \frac{k}{V_C} C_C
\end{align*}

\item You can find the MATLAB function for this simulation at
\href{http://www.mast.queensu.ca/~apsc171/MNTCP01/PracticeProblems/MATLAB/tankSystem2.m}{tankSystem2.m}.

\item You can find the MATLAB script that runs the simulation at
  \href{http://www.mast.queensu.ca/~apsc171/MNTCP01/PracticeProblems/MATLAB/W09TankSystem2.m}{W09TankSystem2.m}.

  Here is the graph of the simulated concentration of salt in the 3
  tanks.
\begin{center}
\includegraphics[width=0.65\linewidth]{graphics/Week09_TankSystems/tank_system_2a}
\end{center}

\item With the continuous mixing, eventually all three tanks will
  reach the same concentration.  We can predict that concentration by
  noting that the only salt is the salt in Tank A at the start.  That
  amount of salt is
  $S_A(0) = C_A(0) V_A = (10 \text{ g/L})(100 \text{ L}) = 1000 \text{
    g}$

  After all the mixing the 1000 g of salt will be evenly distributed
  over the whole volume which is
  $V_A + V_B + V_C = 100 + 200 + 100 = 400$ L, so a final
  concentration of $ \ds\frac{1000 \text{ g}}{400 \text{ L}} = 2.5$
  g/L in every tank.
  \end{enumerate}
\end{Solution}

\end{enumerate}
\end{document}


\subsection{Support}

\subsubsection{Support}

We define the support of an itemset as the proportion of transactions which contain the itemset.

\(supp(A)=\dfrac{\sum A }{n}\).

We can also consider this as:

\(supp(A)=P(A)\)


\subsection{Binary functions}

\subsubsection{Properties of binary functions}

Binary functions can be written as:

\(f(a,b)=a\oplus b\)

A function is commutative if:

\(x\oplus y = y\oplus x\)

A function is associative if:

\((x\oplus y)\oplus z = x\oplus (y\oplus z)\)

A function \(\otimes \) is left distributive over \(\oplus \) if:

\(x\otimes (y\oplus z)=(x\otimes y) \oplus (x\otimes z)\)

Alternatively, function \(\otimes \) is right distributive over \(\oplus \) if:
\((x\oplus y)\otimes z=(x\otimes z) \oplus (y\oplus z)\)

A function is distributive over another function if it both left and right distributive over it.


\section{Geometric interpretation of random variables}\label{rvs}

It is fundamental to define the geometric properties of random
variables using linear algebra.

Consider the vector space which consists of all random variables with finite
mean and variance.
We will regard each point in this space (or vector that correponds to that point
in terms of linear algebra) as a random variable.
We define the scalar product of two random variables $X$ and $Y$ to be
\[
\langle X, Y \rangle = \Cov(X,Y).
\]
It is not difficult to check that the definition satisfies the properties
of a scalar product assuming that $X$ and $Y$ are the same random variables
if there is a constant $a$ such that $\P(X = Y + a)=1$.

\begin{marginfigure}
\includegraphics{figures/01_corr_def.pdf}
\caption{Geometric representation of random variables.}
\label{fig:corr_def}
\end{marginfigure}

Having defined the scalar product, we are now able to introduce the squared
length of a random variable $X$ which is
\[
\lVert X \rVert^2 = \langle X, X \rangle = \Cov(X,X) = \Var(X),
\]
so the~standard
deviation of $X$ ($\sigma_X$) is the length.

Recall that for any non-random vectors $a$ and $b$ the angle
between them is calculated with the formula
\[
\cos(a, b) = \frac{\langle a,  a\rangle}{|a| |b|}.
\]
The same applies for the random variables and it is already clear that
two random variables are uncorrelated iff their scalar product
equals $0$. Additionally, it means that these two random variables
are orthogonal in the vector space.

The analogue for $\cos(a, b)$ in the vector space of all the random
variables is the correlation between two of them:
\[
\Corr(X,Y) = \frac{\Cov(X,Y)}{\sqrt{\Var(X)\Var(Y)}} = \frac{\langle X, Y \rangle}{\sqrt{\lVert X \rVert^2 \lVert Y \rVert^2}}.
\]
From the equivalence of $\Corr(X,Y)$ to $\cos(a, b)$ it
automatically follows that the correlation coefficient can range from $-1$ to $1$.

% proofreaded up to this line

\begin{marginfigure}[10\baselineskip]
\includegraphics{figures/01_pythagorean_theorem.pdf}
\caption{The Pythagorean theorem for random variables $X$ and $Y$.}
\label{fig:rv_pyth}
\end{marginfigure}

A useful property of the geoemtry of random variables is that all the
geometric theorems still hold. For instance, the Pythagorean theorem can
be formulated as follows: if the ranadom variables $X$ and $Y$ are uncorrelated
(which implies that they are orthogonal), then the variance of their sum equals
the sum of their variances:
\[
\Var(X + Y) = \sigma^2_{X+Y} = \sigma^2_{X} + \sigma^2_{Y} = \Var(X) + \Var(Y).
\]
Translated to the non-random language, assumption of uncorrelatedness correspnds
to the right triangle setting, the variance of the sum of two random variables
stands for the hypotenuse squared and the sum of the variances is the sum of
the legs squared.

Another important geometric tool is projection.
Recall that for any two vectors the scalar product $\langle a, b \rangle$
can be interpreted as the length of projected $b$ multiplied by the length of $a$.
The projection itself is $\cos(a, b) b$.
Same holds for the random variables.
The projection of such a random variable $Y$ onto $\{cX| c \in \mathbb{R}\}$ is
$\hat Y = \Corr(X,Y) \cdot Y$.

\begin{marginfigure}
\includegraphics{figures/01_basic_projection.pdf}
\caption{The projection of a random variable $Y$ onto the line spanned by
a random variable $X$.}
\label{fig:rv_proj}
\end{marginfigure}

Note that the squared lengths of the leg adjacent to $\varphi$ and the
hypotenuse are $\Var(\hat Y)$ and $\Var(Y)$.
So, the Figure~\ref{fig:rv_proj} gives a useful expression for the correlation
coefficient squared:
\[
\Corr^2(X,Y) = \frac{\Var(\hat Y)}{\Var(Y)}.
\]

\subsection{The law of iterated expectations}

\marginnote{
Here is the proof for the case when $X$ and $Y$ are both discrete. Let $\E(Y|X) = g(X)$.
\begin{align*}
&\E(g(X)) = \sum_x g(x) \P(X=x) \\
&= \sum_x \left( \sum_y y \P(Y=y|X=x) \right) \P(X=x) \\
&= \sum_x \sum_y y  \P(X=x) \P(Y=y|X=x)  \\
&= \sum_y y \sum_x \P(X=x, Y=y) \\
&= \sum_y y \P(Y=y) = \E(Y)
\end{align*}
The proof in case of continous random variables is absolutely analogous.
}

\begin{theorem}
For any random variable $X$ and $Y$,
\[
\E(\E(Y|X)) = \E(Y).
\]
\end{theorem}

\begin{proof}

Consider a vector space of all the random variables.  The random variables
which can be described as functions of $X$ form a subspace of that vector
space, represented as a plane $\alpha$ in Figure~\ref{fig:adams}.
Another subspace is a subspace of constants, denoted as a vector $\mathbf{1} \in \alpha$.

In order to obtain $\E(Y|X)$, first, we need to project $Y$ onto the subspace
of all the functions of $X$. As a result of this step, we get $\E(Y|X)$ — the function
of $X$ that predicts $Y$ the best (the function which gives the lowest MSE).
Next, projecting $\E(Y|X)$ onto the space of all constants, we obtain $E(Y)$.

Notice that the vector $Y - \E(Y|X)$ (which is also called the residual) is
perpendicular to the plane $\alpha$. Particularly, the vecotr $\E(Y|X) - E(Y)$ is
perpendicular to the vector of constants $\mathbf{1}$. Thus, we can apply
the theorem of three perpendiculars and conclude that the vector $Y - \E(Y)$ is
also perpendicular to the vector of constants $\mathbf{1}$.

So, we showed that the expectation of the random variable $Y$ can be obtained either
in two steps or by its direct projection onto the subpace of constans.

\begin{figure}[h!]
\begin{center}
\includegraphics[width=0.6\linewidth]{figures/01_law_of_iterated_expectations.pdf}
\caption{The law of iterated expectations. Equivalence of the two-step projecttion
and direct projection of $Y$ onto $\mathbf{1}$.}
\label{fig:adams}
\end{center}
\end{figure}

\end{proof}

\subsection{MSE decomposition}

\begin{theorem}
The mean squared error of an estimator $\hat \theta$ with respect to an unknown
parameter $\theta$ defined as $MSE(\hat \theta) = \E((\hat \theta - \theta)^2)$
can be decomposed into the sum of the variance of the estimator and its squared bias:
\[
MSE(\hat \theta) = \Var(\hat \theta) + \E \left[\left( \E(\hat \theta) - \theta  \right)^2 \right]
\]
\end{theorem}

\marginnote{
\begin{align*}
&MSE(\hat \theta) = \E((\hat \theta - \theta)^2) \\
&= \E \left[ \left( \hat \theta - \E(\hat \theta) +  \E(\hat \theta) - \theta \right)^2 \right] \\
&= \E \left[ \left( \hat \theta - \E(\hat \theta) \right)^2 + 2  (\hat \theta - \E(\hat \theta)) (\E(\hat \theta) - \theta )  \right. \\
&+ \left. \left( \E(\hat \theta) - \theta  \right)^2 \right] \\
&= \E \left[\left( \hat \theta - \E(\hat \theta) \right)^2 \right] + 2 \E (\hat \theta - \E(\hat \theta)) (\E(\hat \theta) - \theta ) \\
&+ \E \left[\left( \E(\hat \theta) - \theta  \right)^2 \right] \\
&= \E \left[\left( \hat \theta - \E(\hat \theta) \right)^2 \right] \\
&+ 2 \E(\hat \theta - \E(\hat \theta)) \E(\hat \theta - \E(\hat \theta)) \\
&+ \E \left[\left( \E(\hat \theta) - \theta  \right)^2 \right] \\
&= \E \left[\left( \hat \theta - \E(\hat \theta) \right)^2 \right] + \E \left[\left( \E(\hat \theta) - \theta  \right)^2 \right] \\
&= \Var(\hat \theta) + \E \left[\left( \E(\hat \theta) - \theta  \right)^2 \right]
\end{align*}
}

\begin{proof}
We start with a random variable $\theta$ and its estimate $\hat \theta$ in the
vector space. We know that an unbiased estimator's projection would be exactly
the vector representing $\theta$. However, in general it does not have to and
Figure~\ref{fig:mse_decomposed} illustrates this case: the projection of the estimator falls onto
the line spanned by the vector $\theta$.

Connecting vectors $\theta$ and $\hat \theta$, we obtain the right triangle which
legs are $\hat \theta - \E(\hat\theta)$, $\E(\hat\theta) - \theta$ and the
hypotenuse $\hat \theta - \theta$.
Applying the Pythagorean theorem, we finish the proof:
\begin{align*}
\lVert \hat \theta - \theta \rVert^2 &= \lVert \hat \theta - \E(\hat \theta) \rVert^2  + \lVert \E(\hat \theta) - \theta \rVert^2 \\
\E((\hat \theta - \theta)^2) &= \E((\hat \theta - \E(\hat \theta))^2) + \E((\E(\hat \theta) - \theta)^2) \\
MSE(\hat \theta) &= \Var(\hat \theta) + \E((\E(\hat \theta) - \theta)^2)
\end{align*}

\begin{figure}[h!]
\begin{center}
\includegraphics[width=0.6\linewidth]{figures/01_mse_decomposition.pdf}
\caption{Decomposition of mean squred error into the variance and the bias squared
$\left(a = \sqrt{\lVert\hat\theta - \E(\hat\theta)\rVert^2} \right.$, $b = \sqrt{\lVert \theta - \E(\hat\theta) \rVert^2}$, $\left. c = \sqrt{\lVert \hat\theta - \theta \rVert^2} \right)$}
\label{fig:mse_decomposed}
\setfloatalignment{b}
\end{center}
\end{figure}

\end{proof}


\subsubsection{Curl \& Conservative Vector Fields}
\begin{theorem}
	$\vec{F}$ is conservative $\Leftrightarrow\nabla\times\vec{F} = \vec{0}$
\end{theorem}
\begin{proof}[Partial Proof]
	We will prove the 2D case and provide intuition for higher dimensions.\\
	Let $\vec{F}(x,y) = \langle P(x,y), Q(x,y)\rangle$ be a conservative vector field.\\
	Since $\vec{F}$ is conservative, $P_y = Q_x$.\\
	So, $\nabla \times \vec{F}=Q_x-P_y=0$.
\end{proof}

\noindent
Thinking back to what it means for a vector field to be conservative, a vector field must have path independence between all points to have a potential function and be conservative. If in some plane there is a net rotation, there cannot be path independence because one can choose one path that goes “with” the field and another that goes “against” the field.

[INSERT IMAGE]


\documentclass{article}

\input{../homework.sty}

\title{Homework 1}
\author{Austin Gill}

\begin{document}
\maketitle

\section*{The Fibonacci Bug}
    It's really hard to parallelize when the sequence definition is a recurrence relation. There is a strong dependence of the $n$th term on the $(n-1)$th and $(n-2)$th terms. This doesn't parallelize because there is an inherent order requirement, essentially forcing a sequential execution.

    Now, there \textit{is} a way to make the code parallel.

   \begin{quote}
       Given a homogeneous linear recurrence relation with constant coefficients (a discrete case of a differential equation)

       \[ a_0 x(n) + a_1 x(n - 1) + a_2 x(n - 2) + \cdots + a_m x(n - m) = 0 \]

       We assume a solution of the form $x(n) = C\lambda^n$ and substitute:

       \[ a_0 C\lambda^n + a_1 C \lambda^{n - 1} + \cdots + a_m C \lambda^{n - m} = 0 \]
       \[ a_0 \lambda^n + a_1 \lambda^{n - 1} + \cdots + a_m \lambda^{n - m} = 0 \]
       \[ \lambda^{n - m} \underbrace{( a_0 \lambda^m + a_1 \lambda^{m - 1} + \cdots + a_m \lambda^0 )}_{\text{Characteristic equation with roots as solutions}} = 0 \]

       In the case of no repeated roots, we have

       \[ x(n) = c_1 \lambda_1^n + c_2 \lambda_2^n + \cdots + c_m \lambda_m^n \]

       where each $\lambda_i$ is an eigenvalue, and each $c_i$ is found from the initial conditions. We say that the dominant eigenvalue is the eigenvalue with the greatest absolute value.

       \[ \lambda_d = \max_i\{ \vert \lambda_i \vert \} \]
   \end{quote}

   Now returning to the Fibonacci sequence, a second order recurrence relation (requiring two initial conditions to solve)

   \[ x(n) - x(n - 1) - x(n - 2) = 0 \]
   \[ x(n) = C\lambda^n \rightsquigarrow C\lambda^n - C \lambda^{n - 1} - C\lambda^{n - 2} = 0 \]

   Factoring out $C\lambda^{n - 2}$ gives us the characteristic equation

   \[ \lambda^2 - \lambda - 1 = 0 \]

   which has roots

   \[ \lambda = \frac{1 \pm \sqrt{1 - 4(-1)(1)}}{2} = \frac{1 \pm \sqrt{5}}{2} \]

   where $\frac{1 + \sqrt{5}}{2}$, a.k.a., the golden ratio $\phi$, is the dominant eigenvalue. So then the recurrence relation has the solution

   \[ x(n) = c_1 {\left(\frac{1 + \sqrt{5}}{2}\right)}^n + c_2 {\left(\frac{1 - \sqrt{5}}{2}\right)}^n \]

   where we solve for $c_1$ and $c_2$ using the initial conditions $x(0) = 1$ and $x(1) = 1$.

   \[ x(0) = 1 = c_1 + c_2 \]
   \[ x(1) = 1 = c_1 \left( \frac{1 + \sqrt{5}}{2} \right) + c_2 \left( \frac{1 - \sqrt{5}}{2} \right) \]

   which gives $c_1 = \frac{1}{\sqrt{5}}$ and $c_2 = - \frac{1}{\sqrt{5}}$.

   So all together then, the closed form solution of the homogeneous Fibonacci linear recurrence relation is

   \[x(n) = \frac{1}{\sqrt{5}} \left( {\left( \frac{1 + \sqrt{5}}{2} \right)}^n - {\left( \frac{1 - \sqrt{5}}{2} \right)}^n \right)\]

   Note that this closed form has no history dependence, so it \textit{will} parallelize quite nicely, ignoring the increased number of floating point operations required to compute each $x(n)$ value.

\section{}
    \begin{quote}
        Recall that OpenMP creates private variables for reduction variables, and these private variables are initialized to the identity element for the reduction operator. For example, if the operator is addition, the private variables are initialized to 0, while if the operator is multiplication, the private variables are initialized to 1.

        What are the identity values for these operators: \mintinline{c}{&&}, \mintinline{c}{||}, \mintinline{c}{|}, and \mintinline{c}{^}?
    \end{quote}

    The mathematical identity for $\land$ is $T$, because $x \land T \equiv x$, thus the identity for the \mintinline{c}{&&} operator is \mintinline{cpp}{true}. Similarly, the identity for the \mintinline{c}{||} operator is \mintinline{cpp}{false}. The identities for the bitwise operations \mintinline{c}{|} and \mintinline{c}{^} are both \mintinline{c}{0x0}.

\section{}
    \begin{quote}
        Suppose OpenMP did not have the \mintinline{c}{reduction} clause. Show how to implement an \textit{efficient} parallel reduction by adding a private variable and using the \mintinline{c}{critical} pragma.
    \end{quote}

    Consider the following three solutions.

    \begin{enumerate}
        \item Normal reduction: \begin{minted}{c}
            size_t sum1 = 0;
            #pragma omp parallel for num_threads( 8 ) reduction(+:sum1)
            for( size_t i = 0; i < n; ++i )
            {
                sum1 += i;
            }
        \end{minted}

        \item Naive reduction, which is even worse than sequential: \begin{minted}{c}
            size_t sum2 = 0;
            size_t local_sum = 0;
            #pragma omp parallel for num_threads( 8 ) private( local_sum )
            for( size_t i = 0; i < n; ++i )
            {
                local_sum = i;

                // This is wrong. It's essentially sequential with a lot of overhead.
                #pragma omp critical
                sum2 += local_sum;
            }
        \end{minted}

        \item Better reduction, which performs slightly \textit{better} than using \mintinline{c}{reduction}: \begin{minted}{c}
            size_t sum3 = 0;
            #pragma omp parallel num_threads( 8 )
            {
                size_t local_sum = 0;
                #pragma omp for
                for(size_t i = 0; i < n; ++i )
                {
                    local_sum += i;
                }

                #pragma omp critical
                sum3 += local_sum;
            }
        \end{minted}
    \end{enumerate}

    All together, with timing code and \mintinline{c}{n = 1000000} we get the following results.

    \begin{minted}{text}
        reduced sum:  499999500000
        elapsed time: 13.664000ms
        naive sum:    499999500000
        elapsed time: 76.582000ms
        critical sum: 499999500000
        elapsed time: 0.291000ms
    \end{minted}

\section{}
    \begin{quote}
        For each of the following code segments, use OpenMP pragmas to make the loop parallel, or explain why the code segment is not suitable for parallel execution.

        % \begin{enumerate}
        %     \item \begin{minted}{c}
        %         for(i = 0; i < (int) sqrt(x); i++)
        %         {
        %             a[i] = 2.3 * i;
        %             if(i < 10)
        %                 b[i] = a[i];
        %         }
        %     \end{minted}

        %     \item \begin{minted}{c}
        %         flag = 0;
        %         for(i = 0; i < n && !flag; i++)
        %         {
        %             a[i] = 2.3 * i;
        %             if(a[i] < b[i])
        %                 flag = 1;
        %         }
        %     \end{minted}

        %     \item \begin{minted}{c}
        %         for(i = 0; i < n; i++)
        %         {
        %             a[i] = foo(i);
        %         }
        %     \end{minted}

        %     \item \begin{minted}{c}
        %         for(i = 0; i < n; i++)
        %         {
        %             a[i] = foo(i);
        %             if(a[i] > b[i])
        %                 a[i] = b[i];
        %         }
        %     \end{minted}

        %     \item \begin{minted}{c}
        %         for(i = 0; i < n; i++)
        %         {
        %             a[i] = foo(i);
        %             if(a[i] < b[i])
        %                 break;
        %         }
        %     \end{minted}

        %     \item \begin{minted}{c}
        %         dotp = 0;
        %         for(i = 0; i < n; i++)
        %         {
        %             dotp += a[i] * b[i];
        %         }
        %     \end{minted}

        %     \item \begin{minted}{c}
        %         for(i = k; i < 2 * k; i++)
        %         {
        %             a[i] = a[i] + a[i-k];
        %         }
        %     \end{minted}

        %     \item \begin{minted}{c}
        %         for(i = k; i < n; i++)
        %         {
        %             a[i] = b * a[i - k];
        %         }
        %     \end{minted}
        % \end{enumerate}
    \end{quote}

    \begin{enumerate}
        \item \begin{minted}{c}
            #pragma omp parallel for num_threads( 8 )
            for(i = 0; i < (int) sqrt(x); i++)
            {
                a[i] = 2.3 * i;
                if(i < 10)
                    b[i] = a[i];
            }
        \end{minted}

        \item Cannot use OpenMP to parallelize because the loop is not fixed in size.
        \begin{minted}{text}
            prob3.c: In function ‘main’:
            prob3.c:50:23: error: invalid controlling predicate
                 for(size_t i = 0; i < 9 && !flag; i++)
                                   ^
        \end{minted}

        \item \begin{minted}{c}
            #pragma omp parallel for num_threads( 8 )
            for(i = 0; i < n; i++)
            {
                a[i] = foo(i);
            }
        \end{minted}

        \item \begin{minted}{c}
            #pragma omp parallel for num_threads( 8 )
            for(i = 0; i < n; i++)
            {
                a[i] = foo(i);
                if(a[i] > b[i])
                    a[i] = b[i];
            }
        \end{minted}

        \item Cannot use OpenMP to parallelize because the loop is not fixed in size.

        \item \begin{minted}{c}
            dotp = 0;
            #pragma omp parallel for num_threads( 8 ) reduce(+: dotp)
            for(i = 0; i < n; i++)
            {
                dotp += a[i] * b[i];
            }
        \end{minted}

        \item Cannot parallelize because you cannot easily guarantee \mintinline{c}{a[i - k]} will be properly computed \textit{before} \mintinline{c}{a[i]}, as in the Fibonacci sequence assigned over the weekend. Since \mintinline{c}{k} is fixed, there might be a way to force the loop division over the threads to play nicely, but this would be too much work for too little reward, and would have the added benefit of confusing the hell out of anyone who tried to understand your code.

        \item Same as above.
    \end{enumerate}

\section{}
    \begin{quote}
        Given a task that can be divided into $m$ subtasks, each requiring one unit of time, how much time is needed for an $m$-stage pipeline to process $n$ tasks?
    \end{quote}

    Assuming that each task is broken into $m$ strictly ordered sequential subtasks, we have a few choices. You can use $n$ workers, each performing the full task. Then the amount of time will be on the order of however long it takes to complete a single task. However, this doesn't scale well; we normally have more tasks than each task has subtasks.

    We can use $m$ workers, each performing a single sub task in an offset manner so that worker 1 completes subtask 1 for task 1 and begins subtask 1 for task 2 as worker 2 begins subtask 2 for task 1 and so on. This will complete in $\sim \mathcal O(n)$ time.

    We can use $jm$ workers and have $j$ copies of the pipeline running side-by-side. This also completes in $\sim \mathcal O(n)$ time.

\section{}
    \begin{quote}
        If the address of the nodes in a hypercube has $n$ bits, at most how many nodes can there be, and how many edges does each node have?

        Give an algorithm that routes a message from node $u$ to node $v$ in this $k$-node hypercube in no more than $\log k$ steps.
    \end{quote}

    There will be $2^n$ nodes with $n$ edges per node.

    \begin{minted}{python}
        intermediate_address = u
        # Iterate over the bits of the address.
        for i, u_b, v_b in enumerate(zip(u, v)):
            # Flip the differing bits of the src address one-by-one
            if u_b != v_b:
                intermediate_address[i] = v_b
                mailman.send(msg, intermediate_address)
    \end{minted}

\section{}
    \begin{quote}
        Research the \textit{shuffle-exchange} network topology. Draw the network with 16 processor nodes (numbering each node in binary, showing shuffle and exchange links). If there are $k$ bits in the address, how many nodes are there? With $n$ nodes, what is the diameter and bisection width of the network? How many edges per node are there?
    \end{quote}

    In a shuffle-exchange network with $2^n$ nodes addressed with $n$ bit addresses, there is a directed edge between two nodes if one of the following holds.

    \begin{description}
        \item[shuffle] One address is a 1-bit cyclic left shift of the other. This forms a directed shuffle edge.
        \item[exchange] The addresses differ only in the last bit. This forms a directed exchange edge.
    \end{description}

    Each node has two outgoing edges and two incoming edges, one each of shuffle and exchange. The network has a bisection width of $2^{n - 1}$ and a diameter of $2n - 1$.

    So the 4 node network shown in~\autoref{fig:shuffle4}.

    \begin{figure}
        \centering
        \begin{tikzpicture}[>=stealth, node distance=2cm]
            \tikzset{vertex/.style = {shape=circle, draw, minimum size=1em}}

            \node[vertex] (00) {$00$};
            \node[vertex, right of=00] (01) {$01$};
            \node[vertex, right of=01] (10) {$10$};
            \node[vertex, right of=10] (11) {$11$};

            \draw[->, loop left] (00) edge node[left]{S} (00);
            \draw[->, loop right] (11) edge node[right]{S} (11);

            \draw[->, bend left] (01) edge node[above]{S} (10);
            \draw[->, bend left] (10) edge node[below]{S} (01);

            \draw[->, bend left] (00) edge node[above]{E} (01);
            \draw[->, bend left] (01) edge node[below]{E} (00);

            \draw[->, bend left] (10) edge node[above]{E} (11);
            \draw[->, bend left] (11) edge node[below]{E} (10);
        \end{tikzpicture}
        \caption{A 4 node (2 bit) shuffle-exchange network}\label{fig:shuffle4}
    \end{figure}

    Now consider the following Python script \inputminted{python}{prob6.py}

    which produces, in part the network shown in~\autoref{fig:shuffle16}.

    \afterpage{%
    \thispagestyle{empty}
    \newgeometry{margin=0cm}
    \begin{figure}
        \centering
        \begin{tikzpicture}[>=stealth, node distance=1.4cm]
            \tikzset{vertex/.style = {shape=circle, draw, minimum size=1em}}

            \node[vertex] (0000) {$0000$};
            \node[vertex, below of=0000] (0001) {$0001$};
            \node[vertex, below of=0001] (0010) {$0010$};
            \node[vertex, below of=0010] (0011) {$0011$};

            \node[vertex, below of=0011] (0100) {$0100$};
            \node[vertex, below of=0100] (0101) {$0101$};
            \node[vertex, below of=0101] (0110) {$0110$};
            \node[vertex, below of=0110] (0111) {$0111$};

            \node[vertex, below of=0111] (1000) {$1000$};
            \node[vertex, below of=1000] (1001) {$1001$};
            \node[vertex, below of=1001] (1010) {$1010$};
            \node[vertex, below of=1010] (1011) {$1011$};

            \node[vertex, below of=1011] (1100) {$1100$};
            \node[vertex, below of=1100] (1101) {$1101$};
            \node[vertex, below of=1101] (1110) {$1110$};
            \node[vertex, below of=1110] (1111) {$1111$};

            \draw[->, color=blue!70, bend left] (0000) edge (0001);
            \draw[->, color=blue!70, bend left] (0001) edge (0000);
            \draw[->, color=blue!70, bend left] (0010) edge (0011);
            \draw[->, color=blue!70, bend left] (0011) edge (0010);
            \draw[->, color=blue!70, bend left] (0100) edge (0101);
            \draw[->, color=blue!70, bend left] (0101) edge (0100);
            \draw[->, color=blue!70, bend left] (0110) edge (0111);
            \draw[->, color=blue!70, bend left] (0111) edge (0110);
            \draw[->, color=blue!70, bend left] (1000) edge (1001);
            \draw[->, color=blue!70, bend left] (1001) edge (1000);
            \draw[->, color=blue!70, bend left] (1010) edge (1011);
            \draw[->, color=blue!70, bend left] (1011) edge (1010);
            \draw[->, color=blue!70, bend left] (1100) edge (1101);
            \draw[->, color=blue!70, bend left] (1101) edge (1100);
            \draw[->, color=blue!70, bend left] (1110) edge (1111);
            \draw[->, color=blue!70, bend left] (1111) edge (1110);

            \draw[->, color=red!70, loop above] (0000) edge (0000);
            \draw[->, color=red!70, bend left=45] (0001) edge (0010);
            \draw[->, color=red!70, bend left=45] (0010) edge (0100);
            \draw[->, color=red!70, bend left=45] (0011) edge (0110);
            \draw[->, color=red!70, bend left=45] (0100) edge (1000);
            \draw[->, color=red!70, bend left=45] (0101) edge (1010);
            \draw[->, color=red!70, bend left=45] (0110) edge (1100);
            \draw[->, color=red!70, bend left=45] (0111) edge (1110);
            \draw[->, color=red!70, bend left=45] (1000) edge (0001);
            \draw[->, color=red!70, bend left=45] (1001) edge (0011);
            \draw[->, color=red!70, bend left=45] (1010) edge (0101);
            \draw[->, color=red!70, bend left=45] (1011) edge (0111);
            \draw[->, color=red!70, bend left=45] (1100) edge (1001);
            \draw[->, color=red!70, bend left=45] (1101) edge (1011);
            \draw[->, color=red!70, bend left=45] (1110) edge (1101);
            \draw[->, color=red!70, loop below] (1111) edge (1111);
        \end{tikzpicture}
        \caption{A 16 node (4-bit) {\color{red!70}shuffle}-{\color{blue!70}exchange} network}\label{fig:shuffle16}
    \end{figure}
    \restoregeometry{}
    } % end afterpage

\end{document}


\subsection{Singular value decomposition}

The singular value decomposition of \(m\times n\) matrix \(M\) is:

\(M=U\Sigma V^* \)

Where:

\begin{itemize}
\item \(U\) is a unitary matrix (\(m\times m\))
\item \(\Sigma \) is a diagonal matrix with non-negative real numbers (\(m\times n\))
\item \(V\) is a unitary matrix (\(n\times n\))
\end{itemize}

\(\Sigma \) is unique. \(U\) and \(V\) are not.

\subsubsection{Properties}

\(M^*M=U\Sigma^2 U^*\)

\((M^*M)^{-1}=V\Sigma^{-2} V^*\)

\subsubsection{Calculating the SVD}

The SVD is generally calculated iteratively.


\section{Data Clustering \& Mixture Models}
\subsection*{KMeans}
% ZT?
\textbf{Target:} $\min_{\mathbf{U}, \mathbf{Z}} J(\mathbf{U}, \mathbf{Z}) = \|\mathbf{X} - \mathbf{U} \mathbf{Z}\|_F^2$\\
$= \sum_{n=1}^N \sum_{k=1}^K \mathbf{z}_{k,n} \|\mathbf{x}_n - \mathbf{u}_k\|_2^2$\\
1. \textbf{Initiate:} choose $K$ centroids $\mathbf{U} = [\mathbf{u}_1, \ldots, \mathbf{u}_K]$\\
2. \textbf{Cluster Assign:} data points to clusters. $k^\star(\mathbf{x}_n) = \argmin_k \{ \|\mathbf{x}_n - \mathbf{u}_k\|_2 \}$ returns cluster $k^\star$, whose centroid $\mathbf{u}_{k^\star}$ is closest to data point $\mathbf{x}_n$. Set $\mathbf{z}_{k^\star,n} = 1$, and for $ l \neq k^\star~ \mathbf{z}_{l,n}=0$.\\
3. \textbf{Update centroids}: $\mathbf{u}_k = \frac{\sum_{n=1}^N z_{k,n} \mathbf{x}_n}{\sum_{n=1}^N z_{k,n}}$.\\
4. Repeat from step 2, stops if $\|\mathbf{Z} - \mathbf{Z}^\text{new}\|_0 = \|\mathbf{Z} - \mathbf{Z}^\text{new}\|^2_F = 0$.\\
Computational cost: $O(k\cdot n \cdot d)$

\subsection*{Gaussian Mixture Models (GMM)}
Gaussian $p(x)=\frac{1}{\sqrt{2\pi}\sigma}\mathit{exp}(-\frac{(x-\mu)^2}{2\sigma^2})$
Multivariate $p(x;\mu;\Sigma)=\frac{1}{|\Sigma|^{\frac{1}{2}}(2\pi)^{\frac{D}{2}}}\mathit{exp}[-\frac{1}{2}(x-\mu)^T \Sigma^{-1} (x-\mu)]$ \\
For GMM let $\boldsymbol{\theta}_k = (\boldsymbol{\mu}_k, \boldsymbol{\Sigma}_k)$; $p_{\theta_k}(\mathbf{x}) = \mathcal{N}(\mathbf{x} | \boldsymbol{\mu}_k, \Sigma_k)$\\
\textbf{Mixture Models:} $p_\theta(\mathbf{x}) = \sum_{k=1}^K \pi_k p_{\theta_k}(\mathbf{x})$\\
\textbf{Assignment variable (generative model):} $z_{ij} \in \{0, 1\}$, $\sum_{j=1}^k z_{ij} = 1$\\
$\operatorname{Pr}(z_k = 1) = \pi_k \Leftrightarrow p(\mathbf{z}) = \prod_{k=1}^K \pi_k^{z_k}$\\
\textbf{Complete data distribution:}\\
$p_\theta(\mathbf{x}, \mathbf{z}) = \prod_{k=1}^K \left( \boldsymbol{\pi}_k p_{\theta_k}(\mathbf{x})\right)^{z_k}$\\
\textbf{Posterior Probabilities:} $\\\operatorname{Pr}(z_k = 1 | \mathbf{x}) = \frac{\operatorname{Pr}(z_k = 1) p(\mathbf{x} | z_k = 1)}{\sum_{l=1}^K \operatorname{Pr}(z_l = 1) p(\mathbf{x} | z_l = 1)} = \frac{\boldsymbol{\pi}_k p_{\theta_k}(\mathbf{x})}{\sum_{l=1}^K \boldsymbol{\pi}_l p_{\theta_l}(\mathbf{x})}$\\
$\text{posterior} p(A|B)=\frac{\text{prior} p(A)\times\text{likelihood} p(B|A)}{\text{evidence} p(B)}$\\
\textbf{Likelihood of observed data $\mathbf{X}$:}\\
$p_\theta(\mathbf{X}) = \prod_{n=1}^N p_\theta(\mathbf{x}_n) = \prod_{n=1}^N \left(\sum_{k=1}^K \pi_k p_{\theta_k}(\mathbf{x}_n)\right)$
\textbf{Max. Likelihood Estimation (MLE):}\\
$\argmax_\theta\sum_{n=1}^N \log \left( \sum_{k=1}^K \pi_k p_{\theta_k}(\mathbf{x}_n)\right)\\
\ge \sum_{n=1^N} \sum_{k=1}^K{q_k[\log p_{\theta_k}(\mathbf{x}_n) + \log \pi_k - \log q_k]}$\\
with $\sum_{k=1}^K{q_k} = 1$ by Jensen Inequality.

\textbf{Generative Model}\\
1. sample cluster index $j \sim Categorical(\pi)$\\
2. given $j$, sample data $x \sim \text{Normal}(\mu_j, \Sigma_j)$

\textbf{Expectation-Maximization (EM) for GMM}\\
E-Step:
Pr$[z_{k,n} = 1 | \mathbf{x}_n] = q_{k, n} = \frac{\boldsymbol{\pi}_k^{(t-1)} \mathcal{N}(\mathbf{x}_n | \boldsymbol{\mu}_k^{(t-1)}, \boldsymbol{\Sigma}_k^{(t-1)})}{\sum_{j=1}^K \boldsymbol{\pi}_j^{(t-1)} \mathcal{N}(\mathbf{x}_n | \boldsymbol{\mu}_j^{(t-1)}, \boldsymbol{\Sigma}_j^{(t-1)})}$\\
M-Step:
$\boldsymbol{\mu}_k^{(t)} := \frac{\sum_{n=1}^N q_{k,n} \mathbf{x}_n}{\sum_{n=1}^N q_{k,n}}$
$, \boldsymbol{\pi}_k^{(t)} := \frac{1}{N} \sum_{n=1}^N q_{k,n}$\\
$\Sigma_k^{(t)} = \frac{\sum_{n=1}^N q_{k, n} (\mathbf{x}_n - \boldsymbol{\mu}_k^{(t)})(\mathbf{x}_n - \boldsymbol{\mu}_k^{(t)})^\top}{\sum_{n=1}^N q_{k,n}}$

\textbf{Discussion K-means vs. EM}\\
hard assignment vs soft. spherical clusters shapes vs covariance matrix. fast vs slow and more iteration. K-means can be used as initialization for EM.\\
K-means as a special case of GMM with covariances $\Sigma_j = \sigma^2 I$. in the limit of $\sigma \rightarrow 0$, recover K-means (hard assignments).

\textbf{Model Order Selection (AIC / BIC for GMM)}\\
Trade-off between data fit (i.e. likelihood $p(\mathbf{X} | \theta)$) and complexity (i.e. \# of free parameters $\kappa(\cdot)$). For choosing $K$:\\
Akaike Information Criterion: $\operatorname{AIC}(\theta | \mathbf{X}) = -\log p_\theta(\mathbf{X}) + \kappa(\theta)$\\
Bayesian Information Criterion: $\operatorname{BIC}(\theta | \mathbf{X}) = -\log p_\theta(\mathbf{X}) + \frac{1}{2} \kappa(\theta) \log N$\\
\# of free params, fixed covariance matrix: $\kappa(\theta) = K \cdot D + (K - 1)$ ($K$: \# clusters, $D$: $\mathsf{dim}\text{(data)}=\mathsf{dim}(\mu_i)$, $K-1$: $\pi$ of \# free clusters), full covariance matrix: $\kappa(\theta) = K(D + \frac{D(D+1)}{2}) + (K - 1)$.\\
Compare AIC/BIC for different $K$ -- the smaller the better. BIC penalizes complexity more.


\subsection{Lie algebra of \(SU(n)\)}

\subsubsection{\(SU(n)\) forms a Lie group}

\subsubsection{Lie algebra of \(SU(n)\)}

The Lie algebra of \(SU(n)\) is defined as:

\(\mathfrak{su}(n)=\{X\in \mathbb {C}^{n\times n}|e^{tX}\in SU(n) \forall t\in \mathbb{R}\}\)

This is satisfied by the skew-Hermitian matrices where \(M=-M^*\) and the trace is \(0\). Note that this means the diagonals are all \(0\) or pure imaginary.


\subsection{Symbols}
As we saw earlier, symbols are defined using an equals sign.

\begin{Verbatim}[formatcom=\color{blue}]
N = 212^17
\end{Verbatim}

No result is printed when a symbol is defined.
To see the value of a symbol, just evaluate it.

\begin{Verbatim}[formatcom=\color{blue}]
N
\end{Verbatim}

$\displaystyle N=3529471145760275132301897342055866171392$

Symbols can have more that one letter.
Everything after the first letter is displayed as a subscript.

\begin{Verbatim}[formatcom=\color{blue}]
NA = 6.02214*10^23
NA
\end{Verbatim}

$\displaystyle N_A=6.02214\times10^{23}$

A symbol can be the name of a Greek letter.

\begin{Verbatim}[formatcom=\color{blue}]
xi = 1/2
xi
\end{Verbatim}

$\displaystyle \xi=\frac{1}{2}$

Greek letters can appear in subscripts.

\begin{Verbatim}[formatcom=\color{blue}]
Amu = 2.0
Amu
\end{Verbatim}

$\displaystyle A_\mu=2.0$

The following example shows how
Eigenmath scans the entire symbol to find Greek letters.

\begin{Verbatim}[formatcom=\color{blue}]
alphamunu = 1
alphamunu
\end{Verbatim}

$\displaystyle \alpha_{\mu\nu}=1$

When a symbolic chain is defined,
Eigenmath follows the chain as far as possible.
The following example sets $A=B$ followed by $B=C$.
Then when $A$ is evaluated, the result is $C$.

\begin{Verbatim}[formatcom=\color{blue}]
A = B
B = C
A
\end{Verbatim}

$\displaystyle A=C$

Although $A=C$ is printed,
inside the program the binding of $A$ is still $B$, as can be seen with
the $binding$ function.

\begin{Verbatim}[formatcom=\color{blue}]
binding(A)
\end{Verbatim}

$\displaystyle B$

The {\it quote} function returns its argument unevaluated
and can be used to clear a symbol.
The following example clears $A$ so that its evaluation goes back to
being $A$ instead of $C$.

\begin{Verbatim}[formatcom=\color{blue}]
A = quote(A)
A
\end{Verbatim}

$\displaystyle A$

\subsection{User-defined functions}
The following example shows
a user-defined function with a single argument.

\begin{Verbatim}[formatcom=\color{blue}]
f(x) = sin(x)/x
f(pi/2)
\end{Verbatim}

$\displaystyle \frac{2}{\pi}$

The following example defines a function with two arguments.

\begin{Verbatim}[formatcom=\color{blue}]
g(x,y) = abs(x) + abs(y)
g(1,-2)
\end{Verbatim}

$\displaystyle 3$

User-defined functions can be evaluated without an argument list.
The binding of the function name is returned when there is no
argument list.

\begin{Verbatim}[formatcom=\color{blue}]
f(x) = sin(x)/x
f
\end{Verbatim}

$\displaystyle f=\frac{\sin(x)}{x}$

Normally a function body is not evaluated when a function is defined.
However, in some cases it is required that the function body be the
result of something.
The $eval$ function is used to accomplish this.
For example, the following code causes the function body to be a sixth order Taylor series expansion of $\cos x$.

\begin{Verbatim}[formatcom=\color{blue}]
f(x) = eval(taylor(cos(x),x,6))
f
\end{Verbatim}

$\displaystyle f=-\frac{1}{720}x^6+\frac{1}{24}x^4-\frac{1}{2}x^2+1$

When a function body is evaluated the function arguments
are passed to symbol definitions.
The symbol definitions can appear either before or after
the function definition.

\begin{Verbatim}[formatcom=\color{blue}]
f(x) = A + B
A = a x
B = b x
f(2)
\end{Verbatim}

$\displaystyle 2a+2b$


\chapter{Monte Carlo integration versus Monte Carlo ray tracing}\label{chap:appraytracing}
Although Monte Carlo (MC)	 integration and MC ray tracing are two different methods, they are based on the same idea. To fully comprehend MC ray tracing it is necessary to first understand the basics of MC integration. The aim of this appendix is to point out the similarities and the differences between the two methods. In the next section we give an introduction to MC integration for the two-dimensional case. In Section \ref{app:MCRT} we explain MC ray tracing.
\section{Monte Carlo integration}
Let us consider a set $\mbox{\insieme{D}} = [\vect{a}, \vect{b}]= [a_1, b_1] \times [a_2, b_2]$ with $\vect{a} = (a_1, a_2)$ and $\vect{b} = (b_1, b_2)\in\mathbb{R}^2$. Consider a function $f:[\vect{a},\vect{b}]\subset \mathbb{R}^2 \rightarrow \mathbb{R}$ and a random variable $\vect{Y}$ with values in \insieme{D} and probability density function $\rho(\vect{y})$, where $\vect{y}$ are the values of $\vect{Y}$ in \insieme{D}. We indicate the random variables with capital letters and the corresponding deterministic values with lowercase letters. The expected value of $f(\vect{Y})$ with respect of $\rho(\vect{Y})$ is:
\begin{equation}
\mathbb{E}[f(\vect{Y})] =\int_{\textup{D}}f(\vect{y})\rho(\vect{y}) \textrm{d}\vect{y}.
\end{equation}
If $\rho$ is a uniform probability density function, the expected value is given by:
\begin{equation}\label{eq:expected_value}
\mathbb{E}[f(\vect{Y})] =\frac{1}{\lambda([\vect{a},\vect{b}])} \int_{\textup{D}}f(\vect{y}) \textrm{d}\vect{y},
\end{equation}
where $\lambda([\vect{a},\vect{b}]) = (b_1-a_1)\cdot(b_2-a_2)$.
%In this case, MC methods approximate the integral in Equation (\ref{eq:expected_value}) by the sum:
Let $\{\vect{Y}_\variabile{i}\}_{\variabile{i} = 1, \cdots, N}$ be independent samples with probability density function $\rho$.
Indicating with $S_{N}[f(\vect{Y})]$ the average:
\begin{equation}
S_{N}[f(\vect{Y})] = \frac{1}{N} \sum_{\variabile{i}=1}^{N} f(\vect{Y}_\variabile{i}),
\end{equation}
and with $\const{Pr}(E)$ the probability that an event $E$ happens,
%\begin{equation}\label{eq:approx_MC_QMC}
%\frac{1}{\lambda([\vect{a},\vect{b}])}\int_{\textup{D}}f(\vect{y}) \textrm{d}\vect{y}\approx\frac{1}{N} \sum_{\variabile{i}=1}^{N} f(\vect{Y}_\variabile{i}).
%\end{equation}
the strong law of large numbers states that
\begin{equation}
\const{Pr}\Big(\lim_{N\rightarrow\infty}S_{N}[f(\vect{Y})] = \mathbb{E}[f(\vect{Y})] \Big) = 1
\end{equation}
\cite{grinstead2012introduction}. This implies that the empirical mean is an unbiased estimator of the expectation value:
\begin{equation}\label{eq:approx_MC_QMC}
\mathbb{E}[f(\vect{Y})] \approx \frac{1}{N} \sum_{\variabile{i}=1}^{N} f(\vect{Y}_\variabile{i}).
\end{equation}
From the linearity of the expected value (see \cite{grinstead2012introduction})
%\footnote{Given a set of independent random variables $\{X_\variabile{i}\}_{\variabile{i}=1, \cdots, N}$ and a real number $a$, the expected value satisfies:
%$\mathbb{E}\big[\sum_{\variabile{i}=1}^{N}X_{\variabile{i}}\big] = \sum_{\variabile{i}=1}^{N}\mathbb{E}[X_{\variabile{i}}]$ and $\mathbb{E}[a\,X_\variabile{i}] = a\mathbb{E}[X_{\variabile{i}}]$.},
 it follows that
\begin{equation}\label{eq:linearity}
\mathbb{E}[S_{N}[f(\vect{Y})]] = \frac{1}{N}\sum_{\variabile{i}=1}^{\textrm{N}}\mathbb{E}[f(\vect{Y}_{\variabile{i}})] = \mathbb{E}[f(\vect{Y})],
\end{equation}
where the second equality holds as
$f(\vect{Y}_{\variabile{i}})_{\variabile{i}=1, \cdots, N}$ are random variables with probability density function $\rho(\vect{y})$, because $\big(\vect{Y}_{\variabile{i}}\big)_{\variabile{i}=1, \cdots, N}$ are independent samples with probability density function $\rho(\vect{y})$. This implies that $\mathbb{E}[f(\vect{Y}_{\variabile{i}})] = \mathbb{E}[f(\vect{Y})]$ for every $\variabile{i}=1, \cdots, N$. Indicating with $\textrm{Var}[f(\vect{Y})]$ the variance of $f(\vect{Y})$:
\begin{equation}\textrm{Var}[f(\vect{Y})]=\mathbb{E}[(f(\vect{Y})-\mathbb{E}[f(\vect{Y})])^2] = \sigma^2[f(\vect{Y})],
\end{equation}
the Bienaym\'e formula (see \cite{grinstead2012introduction}) can be applied because the random variables $\{\vect{Y}_\variabile{i}\}_{i = 1, \cdots, N}$ are uncorrelated, therefore
%\footnote{Given a set of \textit{independent} random variables $\{X_\variabile{i}\}_{\variabile{i}=1, \cdots, N}$ and a real number $a$, the variance satisfies:
%$\textrm{Var}\big[\sum_{\variabile{i}=1}^{N}X_{\variabile{i}}\big] = \sum_{\variabile{i}=1}^{N}\textrm[X_{\variabile{i}}]$ and $\textrm{Var}[a\,X_\variabile{i}] = a^2\textrm{Var}[X_{\variabile{i}}]$.}
\begin{equation}\label{eq:variance}
\textrm{Var}[S_{N}[f(\vect{Y})]]= \textrm{Var}\Bigg[\frac{1}{N}\sum_{\variabile{i}=1}^{N}f(\vect{Y}_\variabile{i})\Bigg] =
 \frac{1}{N^2}\sum_{\variabile{i}=1}^{N}\textrm{Var}\big[f(\vect{Y}_\variabile{i})\big] = \frac{1}{N}\textrm{Var}[f(\vect{Y})],
\end{equation}
where the last equality holds as $\textrm{Var}[f(\vect{Y}_{\variabile{i}})] = \textrm{Var}[f(\vect{Y})]$.
Equation (\ref{eq:linearity}) gives
\begin{equation}\label{eq:variance2}
\begin{aligned}
\textrm{Var}[S_N[f(\vect{Y})]] &= \mathbb{E}[(S_{N}[f(\vect{Y})]-\mathbb{E}[S_{N}[f(\vect{Y})]])^2]\\ &= \mathbb{E}[(S_{N}[f(\vect{Y})]-\mathbb{E}[f(\vect{Y})])^2].
\end{aligned}
\end{equation}
Combining (\ref{eq:variance}) with (\ref{eq:variance2}) we obtain:
\begin{equation}\label{eq:variance3}
\mathbb{E}[(S_{N}[f(\vect{Y})]-\mathbb{E}[f(\vect{Y})])^2] = \sigma^2[f(\vect{Y})]/N.
\end{equation}
Let us denote the integration error with:
\begin{equation}\label{eq:error1}
\const{err}(f, S_{N}[f(\vect{Y})]) =\int_{\textup{D}}f(\vect{y})\rho(\vect{y}) \textrm{d}\vect{y}-S_{N}[f] = \mathbb{E}[f(\vect{Y})]-S_N[f(\vect{Y})].
\end{equation}
Considering the convex function $g:\variabile{x}\mapsto\variabile{x}^2$ we can write:
\begin{equation}
\begin{aligned}
\mathbb{E}[|\const{err}(f, S_{N})|] &= \sqrt{g\big(\mathbb{E}[|\const{err}(f, S_{N})|]\big)} \\&\leq\sqrt{\mathbb{E}[g(|\const{err}(f, S_{N})|)]} = \sqrt{\mathbb{E}[\const{err}^2(f, S_{N})]},
\end{aligned}
%\begin{aligned}
%\mathbb{E}\big[|\const{err}(f, S_{\const{N}})|\big] &= \frac{1}{\const{N}}\sqrt{\Bigg(\sum_{\variabile{1}=1}^\const{N}|\const{err}(f, S_{\const{N}})|\Bigg)^2}  \leq \frac{1}{\const{N}}\sqrt{\const{N}\sum_{\variabile{1}=1}^\const{N}\big(\const{err}(f, S_{\const{N}})\big)^2}  \\
%& =\sqrt{\frac{1}{\const{N}}\sum_{\variabile{1}=1}^\const{N}\big(\const{err}(f, S_{\const{N}})\big)^2}= \sqrt{\mathbb{E}\big[\const{err}(f, S_{\const{N}})^2\big]}
%\end{aligned}
\end{equation}
where the inequality follows from Jensen's inequality (\cite{williams1991probability} Chapter $6$).
Using the previous relation and Equations (\ref{eq:variance3}) and (\ref{eq:error1}), we obtain:
\begin{equation}\label{eq:mean_error}
\mathbb{E}\big[|\const{err}(f, S_{N})|\big]\leq
\sqrt{\mathbb{E}\big[\const{err}^2(f, S_{N})\big]} = \frac{\sigma[f]}{\sqrt{N}}.
\end{equation}
Hence, the absolute value of the integration error is, on average, bounded by $\sigma[f(\vect{Y})]/\sqrt{N}$, where $\sigma[f(\vect{Y})]$ is the standard deviation of $f$ \cite{leobacher2014introduction}. It is important to note that $\const{err}(f, S_{N})$ does not depend on the dimension of the set \insieme{D}.
\section{Monte Carlo ray tracing}\label{app:MCRT}
The MC technique can be combined with ray tracing in order to compute the intensity distribution at the target of an optical system.
In MC ray tracing the position and the direction of every ray are chosen randomly at the source.

In the two-dimensional case, for every ray we need to choose one position coordinate $\variabile{x}_1$ and one angular coordinate $\optangle_1$. These coordinates are chosen randomly and such that they are uniformly distributed at the source.
Using ray tracing, the rays with those random initial coordinates are traced from \point{S} to \point{T} and, the corresponding random variables $\vect{Z}_{\variabile{i}}=(\variabile{x}^{\variabile{i}},\optangle^{\variabile{i}})$ at the target are obtained, where $\variabile{x}^{\variabile{i}}$ and $\variabile{t}^{\variabile{i}}$ are the position and direction coordinates of the $\variabile{i}$-th ray at the target. Note that the rays at the target are most likely non-uniformly distributed. Once the target variables are computed, we can apply the idea of MC simulation to approximate the target intensity $I(\optangle)$ defined in Chapter \ref{chap:raytracing}. Since the intensity only depends on the angular coordinate $\optangle$ we use MC approximation in one dimension. Hence, we approximate the expected value $\mathbb{E}[I]$ by a sum as described in Equation (\ref{eq:approx_MC_QMC}).

To clarify the connection between MC integration and MC ray tracing, in Table \ref{tab:MCcomparison} we report the correspondence between the functions and the variables used in the previous section and in Chapter \ref{chap:raytracing}.
\begin{table}[h] \label{tab:MCcomparison}
\centering
\caption{\bf Comparison between MC integration and MC ray tracing}
\begin{tabular}{|l|l|l|}
 \hline   MC integration & MC ray tracing \\
  \hline
 \insieme{D} $= [\vect{a}, \vect{b}]\subset \mathbb{R}^2$  & $[\optangle_{\variabile{j}-1}, \optangle_{\variabile{j}})\subset \mathbb{R}$ \\ [0.9ex]
 $f:\mbox{\insieme{D}}\rightarrow \mathbb{R}^2$ & $\chi_{\variabile{j}}: [\optangle_{\variabile{j}-1}, \optangle_{\variabile{j}})\rightarrow \{0,1\}$ \\ [0.9ex]
 $\vect{Y}$   & $\optangle$ \\ [0.9ex]
 $N$   & $\nrays$ \\ [0.9ex]
$\{\vect{Y}_{\variabile{j}}\}_{\variabile{j}=1, \cdots, N}$
& $\{\optangle^{\variabile{k}}\}_{\variabile{k}=1, \cdots, \nrays}$  \\ [0.9ex]
 $S_{N}[f(\vect{Y})] = \sum_{\variabile{i}=1}^N \frac{f(\vect{Y}_{\variabile{i}})}{N}$ &
 $\hat{I}_{\textrm{MC}}  = \frac{\nrays[\optangle_{\variabile{j}-1}, \optangle_{\variabile{j}})}{\nrays[-\pi/2, \pi/2]}= \frac{1}{\nrays}\sum_{\variabile{k}=1}^{\nrays}\chi_{\variabile{j}}(\optangle^{\variabile{k}})$ \\ [0.9ex]
 $\mathbb{E}\big[S_N[f(\vect{Y})]\big]=\mathbb{E}[f(\vect{Y})]$
& $\mathbb{E}[\hat{I}_{\textrm{MC}}]=\const{P}_{\variabile{j}, \Delta \optangle}$ \\ [0.9ex]
 $\textrm{Var}\big[S_N[f(\vect{Y})]\big] = \frac{\textrm{Var}[f(\vect{Y})]}{N} = \frac{\sigma^2}{N}$  & $\textrm{Var}[\hat{I}_{\textrm{MC}}] = \frac{\const{P}_{\variabile{j}, \Delta \optangle}(1-\const{P}_{\variabile{j}, \Delta \optangle})}{\nrays} = \sigma_{\variabile{j}}^2$ \\ [0.9ex]
 \hline
 \end{tabular}
\label{tab:MCcomparison}
 \end{table}

The main difference between the two methods is that in MC integration we know the probability density distribution $\rho$ of the random variables $\vect{Y}_{\variabile{i}}$ which is the uniform distribution. Because of this, the expected value of the function $f$ can be approximated by (\ref{eq:approx_MC_QMC}). On the contrary, in MC ray tracing we consider random variables equally distributed with a uniform distribution $\rho$ at the source but, applying ray tracing to these variables, we do not know the target probability distribution anymore. Therefore the expected value of the \textit{target} MC intensity $\hat{I}_{\textrm{MC}}$ cannot be approximated by the sum in (\ref{eq:approx_MC_QMC}). Thus, the target is divided into intervals (for the one dimensional case) of the same length, i.e., bins, and the intensity is approximated by the sum of the characteristic function $\chi_{\variabile{j}}$ of the $\variabile{j}$-th interval as explained in Chapter \ref{chap:raytracing} (see Equation (\ref{eq:approssimazione_MCraytracing})). Because of this, the error convergence also depends on to the number of bins for MC ray tracing (see Equation (\ref{eq:final_MC_error})).


\subsection{Utility functions}

We have \(U=f(\mathbf x)\).

Throughout this we will be using a Cobb-Douglas utility function, and discuss the properties of other utility functions at the end.

We have \(U=\prod_i x_i^{\alpha_i}\).

\subsection{Marginal utility}

The marginal utility of product \(x_1\) is:

\(\dfrac{\delta }{\delta x_1}f(\mathbf x)\)

For Cobb-Douglas:

\(U=\sum_i x_i^{\alpha_i})\).

\(\dfrac{\delta }{\delta x_1}f(\mathbf x) = \dfrac{\delta}{\delta x_1} \prod_i x_i^{\alpha_i}\)

\(\dfrac{\delta }{\delta x_1}f(\mathbf x) = \dfrac{1}{x_1}\alpha_1\prod_i x_i^{\alpha_i}\)


\section{Linear functions}
\subsection{Linear functions}
$f : R^n \to R$ means f is a function mapping n-vectors to numbers\\
\textit{Superposition \& linearity}:
$f(\alpha x + \beta y) = \alpha f(x) + \beta f(y)$\\
\textbullet $ f(\alpha_1 x_1 +...+ \alpha_k x_k) = \alpha_1 f(x_1)+...+ \alpha_k f(x_k)$\\
A function that satisfies superposition is called \textit{linear}\\
\textbf{Linear function satisfies}\\
\textbullet \textit{Homogeneity}: For any n-vector x and any scalar $\alpha, f(\alpha x) = \alpha f(x)$ \\
\textbullet \textit{Additivity}: For any n-vectors x and y, $f(x + y) = f(x) + f(y)$\\
\textbf{Affine functions}
$f:R_n \to R$ is affine if and only if it can be expressed as $f(x) = a^Tx + b$ for some n-vector a and scalar b, which is sometimes called the \textit{offset}\\
\textbullet Any \textit{affine} scalar-valued function satisfies the following variation on the super-position property:
$f(\alpha x + \beta y) = \alpha f(x) + \beta f(y)$, where $\alpha + \beta = 1$

\subsection{Taylor approximation}
The (first-order) Taylor approximation of f near (or at) the point z:\\
\begin{scriptsize}
$\hat{f}(x)= f(z) + \frac{\partial f}{\partial x_1}(z)(x_1 - z_1) + ... +  \frac{\partial f}{\partial x_n}(z)(x_n - z_n)$
\end{scriptsize}\\
Alternatively,
$\hat{f}(x)=f(z)+ \nabla f(z)^T(x-z)$
\subsection{Regression model}
\textit{Regression model} is (the affine function of x) $\hat{y} = x^T \beta + v$


\section{Brief primer on tensors}\label{sec:tensor}
The material here is based on \cite{lee-book-2000,dullemond-1991-tensor}.

Let \(V\) be an \(n\)-dimensional vector space over the reals. A {\em covector} on \(V\) is a real-valued linear functional \(\omega:V\rightarrow \Re\). The space of all covectors is itself a real vector space  under pointwise addition and multiplication. It is written as \(V^{\star}\) and called the {\em dual space} to \(V\).

\begin{proposition}[\cite{lee-book-2000} Proposition 4.1] For \(V\) an \(n\)-dimensional vector space and \(E_1, \ldots, E_n\) a basis for \(V\), the covectors \(\epsilon^1, \ldots, \epsilon^n\), defined by:
  \[
  \epsilon^i(E_j) = \delta^i_j = \left\{
  \begin{array}{ll}
    1 & \mbox{if \(i =j\)} \\
    0 & \mbox{if \(i\not=j\)}
    \end{array}\right.
    \]
\noindent form a basis for \(V^{\star}\), called the {\em dual basis} to \((E_i)\).
\end{proposition}
%% TODO: Define canonical isomorphism.
Note that it follows that the dimensionality of \(V^{\star}\) is the same as that of \(V\). It is also the case that \(V^{\star\star}\) is canonically isomorphic to \(V\).

Tensors can be represented in one of three equivalent ways -- as multi-dimensional arrays that are invariant under basis change, as multilinear functions of \(V\) and \(V^{\star}\) and as elements of tensor products of \(V\) and \(V^{\star}\). Here we choose the last alternative.

\begin{definition}[Tensor] For $V$ a finite-dimensional real vector space, a {\em tensor} of rank \((k,l)\) over $V$ is an element of the tensor space \(V\otimes \ldots \otimes V \otimes V^{\star}\otimes \ldots \otimes V^{\star}\) where the first product has \(k\) elements and the second \(l\). \(k\) is said to be its {\em contravariant} rank, and \(l\) its {\em covariant} rank. The {\em rank} of the tensor is \(k+l\).
\end{definition}
Denote the set of all \((k,l)\)-tensors over \(V\) by \(T^k_l\).
$T^k_l(V)$ is a vector space under point-wise addition and scalar multiplication. Specifically:
\[
\begin{array}{l}
  (\alpha T)(X_1, \ldots, X_k, Y_1, \ldots, Y_l) = \alpha(T(X_1, \ldots, X_k, Y_1, \ldots, Y_l))\\
  (S+T)(X_1, \ldots, X_k, Y_1, \ldots, Y_l)=S(X_1, \ldots, X_K, Y_1, \ldots, Y_k) + T(X_1, \ldots, X_k, Y_1, \ldots, Y_l)
\end{array}
\]
Note that a matrix \(M\) is a \((1,1)\)-tensor, a vector \(x\) a \((1,0)\)-tensor, and a covector \{a\} a \((0,1)\)-tensor. \((k,l)\)-tensors are in a 1-1 correspondence with multilinear maps \(V^{\star}\times \ldots \times V^{\star} \times V \times \ldots \times V \rightarrow \Re\), where the first product has \(k\) elements and the second \(l\).

The meaning of the indices of a tensor is usually decided beforehand; it is conventional to write a tensor with its contravariant (upper) indices first and the covariant (lower) indices last, e.g.{} \({T^{ij}}_k\). For instance, a matrix \(M\) is a \((1,1)\)-tensor, and we can write \({M^j}_i\) to make its indices explicit. This is the same as \({M^j}_k\) -- the identity of indices does not matter. Also, basis vectors will be written with a lower index, and components of a vector with respect to this basis with an upper index; similarly, basis covectors will be written with an upper index and components of a covector with a lower index.

In the following we shall adopt the {\em Einstein summation convention}:
\begin{quotation}
  If the same index name appears twice in any term, once as an upper index and once as a lower index, that term is understood to be summed over all possible values of that index, generally from \(1\) to the dimension of the space in question. If the indices belong to different tensors, their tensor product is taken first.
\end{quotation}
Thus for instance we can rewrite:
\[
\begin{array}{lcl}
  \Sigma_{\nu=1}^n A_{\mu\nu}v^{\nu} & \rightarrow &   A_{\mu\nu}v_{\nu}\\
  \Sigma_{\beta=1}^n \Sigma_{\gamma=1}^n A _{\alpha \beta}B^{\beta \gamma}C_{\gamma \delta} & \rightarrow & A_{\alpha \beta}B^{\beta \gamma}C_{\gamma \delta}
\end{array}
\]

Let \(\{{\bf e}_j\}\) be a basis for \(V\), with canonical cobasis \(\{\epsilon^i\}\) for \(V^{\star}\).
A \((k,l)\)-tensor \(T\) can be associated with an array with \(k+l\) dimensions, whose \((i_1, \ldots, i_k, j_1, \ldots, j_l)\)'th element, written \(T^{i_1\ldots i_k}_{j_1, \ldots, j_l}\), is given by the coefficient of \(T\) for the basis element \({\bf e}_{i_1}\otimes \ldots \otimes {\bf e}_{i_k}\otimes \epsilon^{j_1}\otimes \ldots \otimes \epsilon^{j_l}\). Thus:
\[ T = T^{i_1 \ldots i_k}_{j_1 \ldots j_l} {\bf e}_{i_1}\otimes \ldots \otimes {\bf e}_{i_k}\otimes \epsilon^{j_1}\otimes \ldots \otimes \epsilon^{j_l}
\]

\subsection{Tensor Contraction}\label{sec:summation-convention}

The {\em contraction} of a tensor is obtained by setting one upper and one lower index equal, thus indicating a summation per the convention above. The result of contracting a \((k,l)\)-tensor  is a \((k-1,l-1)\) tensor.

The contraction operation is invariant under coordinate changes.

\subsection{Working with tensors}

Tensors can be ``partially'' evaluated. For \(T\) a tensor of rank \(k\) representing a predicate \(p\), and \(a\) a vector representing the value of argument \(i\), \(T_{1\ldots k}a^i\) represents the predicate \(\lambda x_1, \ldots, x_{i-1},x_{i+1},\ldots x_k. p(x_1, \ldots, x_{i-1}, a, x_{i+1}, \ldots, x_k)\). It can be thought of as {\em contracting} \(T\) and \(a\) on index \(i\).

\begin{example}\label{ex:tc-1}
  Consider the tensor representation of the boolean polynomial \(p(x,y) = x(1-y)\). Because \(x,y\in \{0,1\}\) with the two values independent of each other, we will embed them in the two dimensional vector space \(U=\{0,1\}^2\), with ``one hot'' basis vectors \({\bf e}_1=(1,0)\) and \({\bf e}_2=(0,1)\) representing \(0\) and \(1\) respectively. Now the tensor \(T_{i,j}\) representing the binary predicate \(\lambda x,y. p(x,y)\) is represented by the table:
  \[
  \begin{array}{lll}
    {T_{1,1}}&=(0)(1-0)&=0\\
    {T_{1,2}}&=(0)(1-1)&=0\\
    {T_{2,1}}&=(1)(1-0)&=1\\
    {T_{2,2}}&=(1)(0)&=0
  \end{array}
  \]
  We can use this representation to evaluate the predicate at different points through tensor contraction. For instance, \(p(0,1)\) is given, for \(a={\bf e}_1,b={\bf e}_2\) by \(T_{i,j}a^i b^j\), which through the Einstein convention expands out to:\footnote{Recall that for a vector \(x\), \(x^k\) represents its \(k\)th coordinate. Note that
    in the first  term \(T_{i,j}a^ib^j\) the super-scripts of \(a\) and \(b\) are formal (unbound) indices, whereas in  the second term \(T_{i,j}a^i b^j \), \(i\) and \(j\) are bound, and the super-scripts of \(a\) and \(b\) represent component selections.}
  \[\begin{array}{ll}
  T_{i,j}a^i b^j &= \Sigma_{j=1}^2 \Sigma_{i=1}^2 a^i b^j T_{i,j}\\
  &= (1)(0) T_{1,1} + (1)(1) T_{1,2}
      + (0)(0) T_{2,1} + (0)(1) T_{2,2}\\
  & =T_{1,2}\\
  & = 0
  \end{array}
  \]

  Similarly, the term \(T_{i,j} a^i\) gives us a \((1,0)\) tensor \(S_j\) representing \(\lambda x.p(0,x)\):
  \[
  \begin{array}{l}
    S_1 = \Sigma_{i=1}^2 a^i T_{i,1} =a^1 T_{1,1} + a^2  T_{2,1} = T_{1,1} = 0\\
    S_2 = \Sigma_{i=1}^2 a^i T_{i,2} =a^1 T_{1,2} + a^2  T_{2,2} =  T_{1,2} = 0
  \end{array}\]

  The term \(T_{i,j} a^j\) gives us a \((1,0)\) tensor \(R_i\) representing \(\lambda x. p(x,0)\):
  \[\begin{array}{l}
    R_1 = \Sigma_{i=1}^2 a^i T_{1,i} =a^1 T_{1,1} + a^2  T_{1,2} = T_{1,1} = 0\\
    R_2 = \Sigma_{i=1}^2 a^i T_{2,i} =a^1 T_{2,1} + a^2  T_{2,2} =  T_{2,1} = 1
  \end{array}\]

    Applying \(R_i\) to \(b\), i.e. computing \(R_ib^i\) gives \(R_1 b^1 + R_2 b^2=(0)(0) + (1)(1)=1\), whereas applying it to \(a\)  gives  \(R_1 a^1 + r_2 b^2 = (0)(1)+(0)(0) = 0\). The results in both case agree with \((\lambda x.p(x,0))(1)\).

\end{example}

More generally, we can compute tensor contraction symbolically. Let \(x,y\) be  unknown vectors in \(U\), and let \(S\) be a rank-2 tensor over \(U\). Then if \(S\) represents the predicate \(q\), the predication \(q(x,y)\) is represented by \(S_{i,j}x^i y^j\) which expands out to:
\[\begin{array}{ll}
  [S(x,y)]&\defeq  S_{i,j}x^i y^j\\
  & = \Sigma_{j=1}^2 \Sigma_{i=1}^2 x^i y^j [S_{i,j}] \\
  & =   x^1 y^1  [S_{1,1}] + x^1 y^2 [S_{1,2}]
      + x^2 y^1 [S_{2,1}]  + x^2 y^2 [S_{2,2}]
  \end{array}
\]

\begin{example}[Example~\ref{ex:tc-1} contd]
  Taking the value of \(S\) above to be \(T\), we get:
  \[\begin{array}{ll}
      [T(x,y)] & = x^2y^1\\
      & = [x=1][y=0]
  \end{array}
  \]
\end{example}

The representation of an arbitrary $k$-ary predicate is similar, a tensor \(T_k\) represented by \(n^k\) numbers, for \(U=\{0,1\}^n\).


\subsection{Properties of the inverse}

The following are some algebraic properties of matrix inverses.

\begin{proposition}{Properties of the inverse}{inverse-properties}
  Let $A$ and $B$ be $n \times n$-matrices, $I$ the $n\times
  n$-identity matrix. Then the following hold.
  \begin{enumerate}
  \item $I$ is invertible and $I^{-1} = I$.
  \item If $A$ and $B$ are invertible then $AB$ is invertible, and
    $(AB)^{-1} = B^{-1}A^{-1}$.
  \item If $A$ is invertible then so is $A^{-1}$, and
    $(A^{-1})^{-1} = A$.
  \item If $A$ is invertible then so is $A^k$, and
    $(A^k)^{-1} = (A^{-1})^k$.
  \item If $A$ is invertible and $p$ is a non-zero scalar, then $pA$
    is invertible and $(pA)^{-1} = \frac{1}{p}A^{-1}$.
  \end{enumerate}
\end{proposition}


\subsection{Vector potential}

Given a vector field \(\mathbf F\) we may be able to identify another vector field \(A\) such that:

\(\mathbf F =\nabla \times \mathbf A\)

Existence:

We know that the divergence of the curl for any vector field is \(0\), so this applies to \(A\):

\(\nabla . (\nabla \times \mathbf A)=0\)

Therefore:

\(\nabla . \mathbf F= 0\)

This means that if there is a vector potential of \(\mathbf F\), then \(\mathbf F\) has no divergence.

\subsection{Non-uniqueness of vector potentials}

Vector potentials are not unique.

If \(\mathbf A\) is a vector potential of \(\mathbf F\), then so is \(\mathbf A + \nabla c\), where \(c\) is a scalar field and \(\nabla c\) is its gradient.

\subsection{Conservative vector fields}

Not all vector fields have scalar potentials. Those that do are conservative.

For example if a vector field is the gradient of a scalar height function, then the height is a scalar potential.

If a vector field is the rotation of water, there will not be a scalar potential.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% CS624: Analysis of Algorithms
% Copyright 2015 Pejman Ghorbanzade <pejman@ghorbanzade.com>
% Creative Commons Attribution-ShareAlike 4.0 International License
% More info: https://github.com/ghorbanzade/beacon
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section*{Question 4}

Prove that if $f = O(g)$ and $g = O(h)$ then $f = O(h)$.

\subsection*{Solution}
Since $f = O(g)$, there are constants $c$ and $x_0$ such that
\begin{equation}
f(x) \leq c \times g(x)
\label{eq9}
\end{equation}
for any $x \geq x_0$.

Since $g = O(h)$, there are constants $d$ and $x_1$ such that
\begin{equation}
g(x) \leq d \times h(x)
\end{equation}
for any $x \geq x_1$.

We take $x_2 = max(x_0,x_1)$ and $e = c \times d$ as constants with which the following holds true for $x \geq x_2$.

\begin{equation}
f(x) \leq c \times d \times h(x) = e \times h(x)
\end{equation}

By definition of Big-Oh, $f = \mathcal{O}(h)$ is proved.