\documentclass[12pt,a4paper]{article}
\usepackage{microtype}
\usepackage{amsfonts}
\usepackage{amsthm}
\usepackage{graphicx}

\newcommand{\dpar}[1]{\left(#1\right)}

\newcommand{\N}{\mathbb{N}}
\newcommand{\R}{\mathbb{R}}

\theoremstyle{definition}
\newtheorem{ex}{Exercise}[section]

\title{Physics 1: Class 4}
\author{Max Jauregui}

\begin{document}
\maketitle

\section{Acceleration}

Let us consider a particle that moves along a straight line and let us
suppose that we know the velocities of the particle at the instants
$t_1$ and $t_2$. We define the \emph{average acceleration} of the
particle bewteen $t_1$ and $t_2$ as
$$\overline{a}=\frac{v(t_2)-v(t_1)}{t_2-t_1}\,.$$
In addition, we define the \emph{instantaneous acceleration} of the
particle at an instant $t$ by
$$a(t)=\lim_{\Delta t\to 0}\frac{v(t+\Delta t)-v(t)}{\Delta t}=\frac{dv}{dt}\,.$$
Since $v(t)=dx/dt$, it follows from the last equation that
$$a(t)=\frac{d}{dt}\dpar{\frac{dx}{dt}}=\frac{d^2x}{dt^2}\,,$$
where the last term is called the second derivative of the function
$x$ at the point $t$.

Since $a(t)$ is the derivative of the velocity at the instant $t$, in
a $v$ vs $t$ graph, $a(t)$ will be given by the slope of the line that
is tangent to the curve at the instant $t$. On the other hand, since
$a(t)$ is the second derivative of the position at the instant $t$, in
an $x$ vs $t$ graph, $a(t)$ will be given by the curvature of the
curve at the instant $t$. Basically, a curve has positive curvature at
a point if it has the form $\smile$ and negative curvature if it has
the form $\frown$. An inflection point of a curve is a point where the
curvature changes its sign. The curvature of the curve at this point
is zero.

\begin{ex}
  Let $x(t)=5t^3-10t+2$ be the position of a particle in meters, where
  $t$ is measured in seconds. Find the acceleration of the particle at
  the instant $1\,\mathrm{s}$. \emph{Answer:}
  $a(1)=30\,\mathrm{m/s^2}$.
\end{ex}

\begin{ex}
  Considering the $x$ vs $t$ graph given in Fig.~\ref{fig:xvst},
  answer the following:
  \begin{enumerate}
  \item[(i)] What are the signs of the acceleration between the
    instants $1\,\mathrm{s}$ and $2\,\mathrm{s}$?
  \item[(ii)] Is the acceleration negative in some instant between
    $4\,\mathrm{s}$ and $6\,\mathrm{s}$?
  \item[(iii)] What is the sign of the acceleration when the velocity
    of the particle attains its maximum value?
  \item[(iv)] Is there an instant where the acceleration is zero?
  \end{enumerate}
  \begin{figure}[ht]
    \centering
    \includegraphics[width=0.5\textwidth,keepaspectratio]{figures/xvst.pdf}
    \caption{$x$ vs $t$ graph.}
    \label{fig:xvst}
  \end{figure}
\end{ex}

\section{Constant acceleration}

Let us consider a particle that moves in a straight line with constant
acceleration $a_0$, i.e., $a(t)=a_0$ for all $t\ge 0$. We will be
interested in obtaining the expression of the position of the particle
at an arbitrary instant $t$.

We begin by observing that, since $a(t)$ is the derivative of the
velocity at the instant $t$, in an analogous way to the case when we
obtained the position of a particle from the expression of its
velocity, we will have
$$v(t)-v(t_0)=\int_{t_0}^{t}a(t')\,dt'=a_0(t-t_0)\,.$$
Then,
\begin{equation}
  \label{eq:1}
  v(t)=v_0+a_0(t-t_0)\,,
\end{equation}
where $v_0=v(t_0)$.  Now, the displacement of the particle between two
instants $t_0$ and $t$ is given by
$$x(t)-x(t_0)=\int_{t_0}^{t}v(t')\,dt'=v_0(t-t_0)+\frac{a_0}{2}(t^2-t_0^2)-a_0t_0(t-t_0)\,.$$
Hence,
\begin{equation}
  \label{eq:2}
  x(t)=x_0+v_0(t-t_0)+\frac{a_0}{2}(t-t_0)^2\,,
\end{equation}
where $x_0=x(t_0)$.

From Eqs.~(\ref{eq:1}) and~(\ref{eq:2}) we can obtain other useful
equations. For instance, it follows from Eq.~(\ref{eq:1}) that
$$t-t_0=\frac{v(t)-v_0}{a_0}\,.$$
Using this in Eq.~(\ref{eq:2}), we can obtain that
$$v^2(t)-v_0^2=2a_0[x(t)-x_0]\,.$$
Moreover, using the expression of $a_0$, obtained from
Eq.~(\ref{eq:1}), we obtain
$$x(t)-x_0=\dpar{\frac{v(t)+v_0}{2}}(t-t_0)\,.$$

\end{document}


% ***********************************************************************************
% Pure LaTeX part to be inserted in a document (be careful of depencies of packages & commands
% Prepared by XXX and YYY under the supervision of Arnaud de La Fortelle
% Fall 2017
% 2D wave propagation subsection of the modeling part
% ***********************************************************************************

\subgroup{2}{Ruitong Zhu and Qingan Zhao}

\paragraph{Model presentation}
This part is to simulate the vibrating string (i.e., 1D wave equation) and present its displacement with a time-varying image. The string held stationary at both ends and free to vibrate transversely subject only to the restoring forces due to tension in the string. $Figure 1$ shows coordinates and definies symbols for the transverse vibrating string.

\begin{figure}[htb]
	\centering
	\includegraphics[width=10cm]{string.jpg}
	\caption{Vibrating String}
\end{figure}

The partial differential equation (PDE) of this problem is given as follow:

\begin{equation}
	\frac{\partial^2 h}{\partial t^2}=a^2\left(\frac{\partial^2 h}{\partial x^2}\right)
\end{equation}

where $h$ is the wave function $h(x,t)$, representing the displacement of the string at position $x$ and time $t$; $a$ is the wave speed which equals to $\sqrt{E/\rho}$.

\paragraph{Implementation}

The simluation is based on Finite Difference Method (FDM). Using second-order central difference at time $t_n$ and position $x_i$, we can get the recurrence equation as follow:

\begin{equation}
\frac{u_{i}^{N+1}-2u_{i}^{N}+u_{i}^{N-1}}{\Delta t^2} = a^2\frac{u_{i+1}^{N}-2u_{i}^{N}+u_{i-1}^{N}}{\Delta x^2}
\end{equation}

Assume the wave speed $a = 1$; the length of the string $L$ is $2$; the maxium time for this simulation is $4$; stepsize $\Delta x$ and $\Delta t$ are both equal to $0.01$. The initial condition and the boundary condition are described as follows:
\begin{eqnarray}
h(x,0)&=&sin(\pi x)\\
\frac{\partial h}{\partial t}\bigg |_{(x,0)}&=&0\\
h(0,t)&=&h(2,t)=0
\end{eqnarray}

Here we offer the implementation in Python:

\begin{python}
	## parameter
	a = 1  ## a coefficient of stiffness
	L = 2  ## The string is constrained at x=0 and x=L.
	T = 4  ## maxium time for this simulation.
	dx = 0.01  ## time step
	dt = 0.01  ## distance step
	N = int(L/dx);
	M = int(T/dt);
	r = (a*dt/dx)**2  ## a parameter

	## initial shape of the string
	def initial(x):
	tmp = math.sin(math.pi*x)
	return tmp

	## initial speed of the string
	def speed(x):
	tmp = 0
	return tmp

	## Define an array and a blank matrix for later use.
	x = [0]
	h = np.zeros((M+1, N+1))

	## t=0, initial condition
	for i in range(N):
	x.append(x[i] + dx)  ## x axis
	h[0,i+1] = initial(x[i+1])  ## displacement of the string

	## t=dt, the first itertaion
	for i in range(N-1):
	h[1,i+1] = h[0,i+1] + r* (h[0,i] + h[0,i+2] - 2*h[0,i+1])/2 + dx*speed(x[i+1])
	## displacement of the string

	## t=n*dt where n>1
	for j in range(1, M):
	for i in range(N-1):
	h[j+1,i+1] = (h[j,i+2]+h[j,i]-2*h[j,i+1])*r-h[j-1,i+1]+2*h[j,i+1]
	## displacement of the string

	t = [0]
	for j in range(M):
	t.append(t[j] + dt)  ## t axis

	## Plot the 3D figure.
	fig = plt.figure()
	ax = Axes3D(fig)
	X, T= np.meshgrid(x,t)
	ax.plot_surface(X, T, h, cmap='rainbow')
	ax.set_xlabel('X')
	ax.set_ylabel('T')
	ax.set_zlabel('h')
	plt.show()

	## Plot the shape of string at different time
	for i in range(4):
	plt.xlabel(u'x',fontsize=14)
	plt.ylabel(u'h',fontsize=14)
	plt.show()
\end{python}

\paragraph{Results}

The dynamic change of the string is shown in $Figure 2$, ranging from $0\sim 4$.

\begin{figure}[htb]
	\centering
	\includegraphics[width=10cm]{string3d.png}
	\caption{Time-varying Image of the string}
\end{figure}

More specifically, the shapes of the string at different times are shown below:

\begin{figure}[ht]
	\centering
	\begin{minipage}{8cm}
		\includegraphics[width=7cm]{string0.png}
		\caption*{t=0}
		\end{minipage}
	\begin{minipage}{8cm}
		\includegraphics[width=7cm]{string0_5.png}
		\caption*{t=0.5}
	\end{minipage}

    \begin{minipage}{8cm}
    	\includegraphics[width=7cm]{string1.png}
    	\caption*{t=1}
        \end{minipage}
    \begin{minipage}{8cm}
    	\includegraphics[width=7cm]{string1_5.png}
    	\caption*{t=1.5}
    \end{minipage}
	\caption{Shape of the string at time t}
\end{figure}

\paragraph{Interpretation}

The results are consistent with our expectation. The shape of the string depends on the initial and boundary conditions. In this case, the string is in the shape of a sine wave that changes periodically.

The initial and boundary conditions can be changed easily at the code by modifying the function $initial()$ and $speed()$. Readers can plot more complicated image with different initial displacement and speed function.

\paragraph{Conclusion}
Finite Difference Method is a practical method to obtain the numerical solution of a Partial Differential Equation. We can learn that it is applicable for one dimentional problems like 1D wave equation and 1D heat equation. It's not difficult to understand but there are some places worth special attention while writing the code. For example, the first iteration needs to be distinguished from other iterations since $t_{N-1}$ is not defined.


\chapter{Multiscale Methods on the Sphere}
\label{ch_mms}

\section{Wavelet Transform on the Sphere}
\label{sect_wts}

\subsection{Isotropic Undecimated Wavelet Transform on the Sphere (UWTS) }
\index{wavelet!undecimated wavelet transform}

There are clearly many different possible implementations of a wavelet transform on the sphere and their performances depend on the application.
We describe here an undecimated isotropic transform which is similar in many respects to the \emph{ \`a trous} algorithm, and is therefore a good
candidate for restoration applications. Its isotropy is a favorable property when analyzing a statistically isotropic Gaussian field such as the
CMB, or data sets such as maps of galaxy clusters, which contain only isotropic features~\cite{starck:book98}. Our isotropic transform is obtained
using a scaling function $\phi_{l_c}(\vartheta, \varphi)$ with cut-off frequency $l_c$ and azimuthal symmetry, meaning that $\phi_{l_c}$ does not
depend on the azimuth $\varphi$. Hence the spherical harmonic coefficients $\hat \phi_{l_c} (l,m)$ of $\phi_{l_c}$ vanish when $m \ne 0$ so that :
\begin{eqnarray}
\phi_{l_c}(\vartheta, \varphi)= \phi_{l_c}(\vartheta) = \sum_{l = 0}^{l = l_c} \hat \phi_{l_c} (l,0) Y_{l,0}(\vartheta, \varphi)
\end{eqnarray}
where the $Y_{l,m}$ are the spherical harmonic basis functions. Then, convolving a map $f(\vartheta, \varphi)$ with $\phi_{l_c}$ is greatly simplified
and the spherical harmonic coefficients $\hat c_{0}(l,m)$ of the resulting map $c_0(\vartheta, \varphi)$ are readily given by \cite{bogdanova}:
\begin{eqnarray}\label{eq:conv}
 \hat c_{0}(l,m) = \widehat{\phi_{l_c} * f} (l,m) = \sqrt{\frac{4\pi}{2l+1} } \hat \phi_{l_c} (l,0) \hat f(l,m)
\end{eqnarray}
where $*$ stands for convolution.

\subsubsection*{From one resolution to the next one}

A sequence of smoother approximations of $f$ on a dyadic resolution scale can be obtained using the scaling function $\phi_{l_c}$ as follows
\begin{eqnarray}
c_0   & = &  \phi_{ l_{c} }  * f    \nonumber    \\
c_1   & = &  \phi_{2^{-1} l_{c} }   * f    \nonumber	   \\
&\ldots&\nonumber\\
c_j    &=&   \phi_{2^{-j}  l_{c}  }  * f  \nonumber    \\
\end{eqnarray}
where $\phi_{2^{-j} l_{c} }$ is a rescaled version of $\phi_{l_{c}}$ with cut-off frequency $2^{-j} l_{c}$. The above multi-resolution sequence
can actually be obtained recursively. Define a low pass filter $h_{j}$ for each scale $j$ by
%\begin{eqnarray}
 %\hat h_{j}(l,m) = \left\{
%  \begin{array}{ll}
 % \frac {   \hat \phi_{\frac{l_{c}}{2^{j+1}} }(l,m)   }   {  \hat  \phi_{  \frac{l_{c}}{2^{j}} }(l,m)   } & \mbox{if }  l  < \frac{ l_{c}} {2^{j+1}} \\
%0 & \mbox{otherwise } \
 % \end{array}
  %\right.
%\end{eqnarray}
\begin{eqnarray}
 \hat{H}_{j}(l,m) = \sqrt{\frac{4\pi}{2l+1} }  \hat h_{j}(l,m) = \left\{
  \begin{array}{ll}
  \frac {   \hat \phi_{\frac{l_{c}}{2^{j+1}} }(l,m)   }   {  \hat  \phi_{  \frac{l_{c}}{2^{j}} }(l,m)   } & \mbox{if }  l  < \frac{ l_{c}} {2^{j+1}} \quad \textrm{and}\quad m = 0\\
0 & \mbox{otherwise } \
  \end{array}
  \right.
\end{eqnarray}
It is then easily shown that $c_{j+1}$ derives from $c_j$ by convolution with $h_j$:  $c_{j+1} = c_{j} * h_j$.

\subsubsection*{The wavelet coefficients}

Given an asymmetrical wavelet function $\psi_{l_c}$, we can derive in the same way a high pass filter $g_j$ on each scale $j$:
%\begin{eqnarray}
%\hat{g}_{j}(l,m) = \left\{
%  \begin{array}{ll}
%  \frac{\hat{\psi}_{2l_{c}}}{\hat{\phi}_{l_{c}}} & \mbox{if }  l  < l_{c} \\
%1 & \mbox{otherwise } \
%  \end{array}
%  \right.
%\end{eqnarray}
\begin{eqnarray}
 \hat{G}_{j}(l,m) = \sqrt{\frac{4\pi}{2l+1} }  \hat{g}_{j}(l,m) = \left\{
  \begin{array}{ll}
 \frac {   \hat \psi_{\frac{l_{c}}{2^{j+1}} }(l,m)   }   {  \hat  \phi_{  \frac{l_{c}}{2^{j}} }(l,m)   } & \mbox{if }  l  < \frac{ l_{c}} {2^{j+1}} \quad \textrm{and}\quad m = 0\\
1 &\mbox{if }  l  \ge \frac{ l_{c}} {2^{j+1}} \quad \textrm{and}\quad m = 0\\
0&  \mbox{otherwise }\
  \end{array}
  \right.
\end{eqnarray}
Using these, the wavelet coefficients $w_{j+1} $ at scale $j+1$ are obtained from the previous resolution by a simple convolution: $w_{j+1} = c_{j} * g_j$.\\

Just as with the \emph{\`a trous} algorithm, the wavelet coefficients can be defined as the difference between two consecutive resolutions,
$w_{j+1}(\vartheta, \varphi) = c_{j}(\vartheta, \varphi) - c_{j+1}(\vartheta, \varphi)$, which in fact corresponds to making the following
specific choice for the wavelet function $\psi_{l_c}$:
\index{wavelet!\`a trous}
%\begin{eqnarray}
%\hat \psi(l,m) = \hat \phi_{l_c} (l,m)  - \hat \phi_{l_c/2}(l,m)
%\end{eqnarray}
\begin{eqnarray}
\hat \psi_{\frac{l_c}{2^{j}}}(l,m) = \hat \phi_{\frac{l_c}{2^{j-1}}} (l,m)  - \hat \phi_{\frac{l_c}{2^{j}}}(l,m)
\end{eqnarray}
The high pass filters $g_j$ defined above are, in this particular case, expressed as:
%\begin{eqnarray}
%\hat{g}_{j}(l,m) =  1 - \hat{h}_j(l,m)
%\end{eqnarray}
\begin{eqnarray}
 \hat{G}_{j}(l,m) = \sqrt{\frac{4\pi}{2l+1} } \hat{g}_{j}(l,m) =  1 - \sqrt{\frac{4\pi}{2l+1} } \hat{h}_j(l,m)   =   1 - \hat{H}_j(l,m)
\end{eqnarray}
Obviously, other wavelet functions could be used just as well.

\subsubsection*{Choice of the scaling function}
\index{scaling function}
Any function with a cut-off frequency is a possible candidate. We retained here a B-spline function of order 3. It is quite similar to a
Gaussian function and converges rapidly to $0$:
%\begin{eqnarray}
%\hat \phi_{l_c} (l,m) =\frac{3}{2} B_{3\ \frac{l}{l_{c}}m}
%\end{eqnarray}
\begin{eqnarray}
\hat \phi_{l_c} (l,m = 0) =\frac{3}{2} B_{3}( \frac{2 l}{l_{c} })  % \quad \textrm{where} \quad   B_3(x) = \frac{1}{12}({\mid{x-2}\mid}^3 - 4 {\mid{x-1}\mid}^3 + 6 {\mid{x}\mid}^3 - 4 {\mid{x+1}\mid}^3 + {\mid{x+2}\mid}^3)
\end{eqnarray}
where $B(x) = \frac{1}{12}({\mid{x-2}\mid}^3 - 4 {\mid{x-1}\mid}^3 + 6 {\mid{x}\mid}^3 - 4 {\mid{x+1}\mid}^3 + {\mid{x+2}\mid}^3)$.

\begin{figure*}[htb]
\centerline{
\hbox{
% \psfig{figure=ch1_diff_uv_phi_psi.ps,bbllx=0.5cm,bblly=13.5cm,bburx=20.5cm,bbury=27cm,height=5cm,width=14.5cm,clip=}
\psfig{figure=fig_sphere_filterbank.pdf,bbllx=2cm,bblly=21cm,bburx=20cm,bbury=26cm,height=7cm,width=14.5cm,clip=}
}}
\caption{On the left, the scaling function $\hat{\phi}$ and, on the
right, the wavelet function $\hat{\psi}$.}
\label{fig_diff_uv_phi_psi}
\end{figure*}

In Fig.~\ref{fig_diff_uv_phi_psi} the chosen scaling function derived from a $B$-spline of degree 3, and its resulting wavelet function, are plotted in frequency space.
\index{B-spline}

\begin{center}
\begin{tabular}{|c|} \hline
\begin{minipage}[b]{5.3in}
\vspace{0.1in}

\small{
\textsf{1. Compute the $B_3$-spline scaling function and derive $\psi$, $h$ and $g$ numerically.}

\textsf{2. Compute the corresponding Spherical Harmonics of image $c_0$. We get ${\hat c}_0$.}

\textsf{3. Set $j$ to $0$. Iterate:}

\hspace{0.3in} \textsf{4. Multiply  $\hat{c}_j$ by $\widehat H_{j}$. We get the array $\hat{c}_{j+1}$.}

\hspace{0.33in} \textsf{Its inverse Spherical Harmonics Transform gives the image at scale $j+1$.}

\hspace{0.3in} \textsf{5. Multiply $\hat{c}_j$ by $\widehat G_{j}$. We get the complex array
$\hat{w}_{j+1}$.}

\hspace{0.33in} \textsf{ The inverse Spherical Harmonics transform of $\hat{w}_{j+1}$
gives the wavelet coefficients $w_{j+1}$ at scale $j+1$.}

\hspace{0.3in} \textsf{6. j=j+1 and if $j \leq  J$, return to Step 4.}

\textsf{9. The set $\{w_1, w_2, \dots, w_{J}, c_{J}\}$ describes the wavelet transform on the sphere of $c_0$.}}

\vspace{0.05in}
\end{minipage}
\\\hline
\end{tabular}
\\ \vspace{0.1in}
The numerical algorithm for the undecimated wavelet transform on the sphere.
\end{center}
\linespread{1.3}
\index{algorithm!undecimated wavelet transform}

If the wavelet is the difference between two resolutions, Step 5 in the above UWTS algorithm can be replaced by the following simple subtraction $w_{j+1} = c_{j} - c_{j+1}$.

\subsubsection*{Reconstruction}
\index{wavelet!undecimated wavelet reconstruction}
When the wavelet is the difference between two resolutions, the reconstruction of an image from its wavelet coefficients ${\cal W} = \{w_1,\dots, w_{J}, c_{J}\}$ is straightforward:
\begin{eqnarray}
 c_{0}(\theta, \phi) = c_{J}(\theta, \phi) + \sum_{j=1}^J  w_j(\theta, \phi)
\end{eqnarray}
This is the same reconstruction formula as in the \emph{\`a trous} algorithm: the simple sum of all scales reproduces the original data.
Actually, since the present decomposition is redundant, the procedure for reconstructing an image from its coefficients is not unique and
in fact this can profitably be used to impose additional constraints on the synthesis functions (\emph{e.g.} smoothness, positivity) used
in the reconstruction. Here for instance, using the relations:
\begin{eqnarray}
\hat c_{j+1}(l,m) = \widehat H_{j} (l,m)  \hat c_{j} (l,m) \nonumber \\
\hat w_{j+1}(l,m) = \widehat G_{j} (l,m) \hat c_{j} (l,m)
\end{eqnarray}
a least squares estimate of $c_j$ from $c_{j+1}$ and $w_{j+1}$ gives:
\begin{eqnarray}
%c_{j}   = c_{j+1}  * {\tilde h}_{2^{j}l_{c}}   + w_{j+1}  * {\tilde g}_{2^{j}l_{c}}
\hat{c}_{j}   = \hat{c}_{j+1}  {\widehat {\tilde H}}_{j}   + \hat{w}_{j+1}  {\widehat {\tilde G}}_{j}
\end{eqnarray}
where the conjugate filters $ {\widehat {\tilde H}}_j $ and $ {\widehat {\tilde G}}_j$ have the expression:
\begin{eqnarray}
 {\widehat {\tilde H}}_j =  \sqrt{\frac{4\pi}{2l+1} } {\hat {\tilde h}}_j & = {\widehat H}_{j}^* /
(\mid {\widehat H}_{j}\mid^2 + \mid {\widehat G}_j\mid^2) \label{eqnht} \\
 {\widehat {\tilde G}}_j =  \sqrt{\frac{4\pi}{2l+1} } {\hat {\tilde g}}_j & = {\widehat G}_{j}^* /
(\mid {\widehat H}_j \mid^2 + \mid {\widehat G}_j \mid^2)
\label{eqngt}
\end{eqnarray}
%\begin{eqnarray}
%{\hat {\tilde h}} & = {\hat h^* /
%(\mid \hat h\mid^2 + \mid \hat g\mid^2}) \label{eqnht} \\
%{\hat {\tilde g}} & = {\hat g^* /
%(\mid \hat h \mid^2 + \mid \hat g \mid^2})
%\label{eqngt}
%\end{eqnarray}
and the reconstruction algorithm is:
\begin{center}
\begin{tabular}{|c|} \hline
\begin{minipage}[b]{5.3in}
\vspace{0.1in}

\small{
\textsf{1. Compute the $B_3$-spline scaling function and derive $\hat \psi$, $\hat h$, $\hat g$, ${\hat {\tilde h}}$, ${\hat {\tilde g}}$ numerically.}

\textsf{2. Compute the corresponding Spherical Harmonics of the image at the low resolution $c_J$. We get ${\hat c}_J$.}

\textsf{3. Set $j$ to $J-1$. Iterate:}

\hspace{0.3in} \textsf{4. Compute the Spherical Harmonics transform of the wavelet coefficients $w_{j+1}$ at scale $j+1$. We get  $\hat{w}_{j+1}$.}

\hspace{0.3in} \textsf{5. Multiply  $\hat{c}_{j+1}$ by ${\widehat {\tilde H}}_j $.}

\hspace{0.3in} \textsf{6. Multiply $\hat{w}_{j+1}$ by $ {\widehat {\tilde G}}_j $.}

\hspace{0.3in} \textsf{7. Add the results of steps 6 and 7. We get $\hat  c_j$.}

\hspace{0.3in} \textsf{8. j=j-1 and if $j \ge  0$, return to Step 4.}

\textsf{9. Compute The inverse Spherical Harmonic transform of $\hat  c_0$}}

\vspace{0.05in}
\end{minipage}
\\\hline
\end{tabular}
\\ \vspace{0.1in}
% The numerical algorithm for the inverse wavelet transform on the sphere.
\end{center}
\linespread{1.3}
\index{algorithm!undecimated wavelet reconstruction}
The synthesis low pass and high pass filters $\hat{\tilde h}$ and $\hat{\tilde g}$ are plotted in Fig.~\ref{fig_diff_uv_ht_gt}.

\begin{figure*}[htb]
\centerline{
\hbox{
% \psfig{figure=ch1_diff_uv_ht_gt.pdf,bbllx=0.5cm,bblly=13.5cm,bburx=20.5cm,bbury=27cm,height=5cm,width=14.5cm,clip=}
\psfig{figure=fig_sphere_filterbank.pdf,bbllx=2cm,bblly=12.5cm,bburx=20cm,bbury=16.7cm,height=7cm,width=14.5cm,clip=}
}}
\caption{On the left, the filter $\hat{\tilde{h}}$, and on the right the
filter $\hat{\tilde{g}}$.}
\label{fig_diff_uv_ht_gt}
\end{figure*}

\begin{figure*}
\vbox{
\centerline{
\hbox{
 % \psfig{figure=fig_uwt_sphere.ps,bbllx=1cm,bblly=7cm,bburx=17cm,bbury=22cm,height=9cm,width=12cm,clip=}
\psfig{figure=fig_wmap_bw.pdf,bbllx=0.5cm,bblly=9.5cm,bburx=20.5cm,bbury=20cm,width=8cm,height=4.5cm,clip=}
\psfig{figure=fig_wmap_scale1_bw.pdf,bbllx=0.5cm,bblly=9.5cm,bburx=20cm,bbury=20cm,width=8cm,height=4.5cm,clip=}
}}
\centerline{
\hbox{
\psfig{figure=fig_wmap_scale2_bw.pdf,bbllx=0.5cm,bblly=9.5cm,bburx=20cm,bbury=20cm,width=8cm,height=4.5cm,clip=}
\psfig{figure=fig_wmap_scale3_bw.pdf,bbllx=0.5cm,bblly=9.5cm,bburx=20cm,bbury=20cm,width=8cm,height=4.5cm,clip=}
}}
\centerline{
\hbox{
\psfig{figure=fig_wmap_scale4_bw.pdf,bbllx=0.5cm,bblly=9.5cm,bburx=20cm,bbury=20cm,width=8cm,height=4.5cm,clip=}
\psfig{figure=fig_wmap_scale5_bw.pdf,bbllx=0.5cm,bblly=9.5cm,bburx=20cm,bbury=20cm,width=8cm,height=4.5cm,clip=}
}}
}
\caption{{ WMAP Data and its wavelet transform on the Sphere using five resolution levels (4 wavelet scales and the coarse scale).
The sum of these five maps reproduces exactly the original data (top left). Top,original data and the first wavelet scale. Middle,
the second and third wavelet scales. Bottom, the fourth wavelet scale and the last smoothed array.}}
\label{Figure:UWTS}
\end{figure*}

\begin{figure}
\centerline{
\hbox{
% \centering
% \includegraphics[height=8truecm,width=6truecm]{fig_uwt_sphere.pdf}
% \includegraphics[height = 6 in]{fig_backwt_sphere.pdf}
\psfig{figure=fig_backwt_sphere.pdf,bbllx=0.5cm,bblly=6.5cm,bburx=20.5cm,bbury=21.5cm,height=7.5cm,width=10cm,clip=}
}}
\caption{{ Backprojection of a wavelet coefficient at different scales. Each map is obtained by setting all wavelet coefficients
to zero but one, and applying an inverse wavelet transform. Depending on the scale and the position of the non zero wavelet coefficient,
the reconstructed image presents an isotropic feature with a given size.}}
\label{Figure:back_wt}
\end{figure}

{ Figure~\ref{Figure:UWTS} shows the WMAP data (top left) and its undecimated wavelet decomposition on the sphere using
five resolution levels. Figures~\ref{Figure:UWTS} top right, middle left, middle right and bottom left show respectively
the four wavelet scales. Figure~\ref{Figure:UWTS} bottom right shows the last smoothed array. Figure~\ref{Figure:back_wt}
shows the backprojection of a wavelet coefficient at different scales and positions.}

\subsection{Isotropic Pyramidal Wavelet Transform on the Sphere (PWTS) }
\index{wavelet!pyramidal wavelet transform}
\index{wavelet!pyramidal wavelet reconstruction}

\begin{figure*}
\vbox{
\centerline{
\hbox{
 % \psfig{figure=fig_pyrwt_sphere.ps,bbllx=1cm,bblly=7cm,bburx=17cm,bbury=22cm,height=9cm,width=12cm,clip=}
\psfig{figure=fig_wmap_bw.pdf,bbllx=0.5cm,bblly=9.5cm,bburx=20.5cm,bbury=20cm,width=8cm,height=4.5cm,clip=}
\psfig{figure=fig_wmap_pscale1_bw.pdf,bbllx=0.5cm,bblly=9.5cm,bburx=20cm,bbury=20cm,width=8cm,height=4.5cm,clip=}
}}
\centerline{
\hbox{
\psfig{figure=fig_wmap_pscale2_bw.pdf,bbllx=0.5cm,bblly=9.5cm,bburx=20cm,bbury=20cm,width=8cm,height=4.5cm,clip=}
\psfig{figure=fig_wmap_pscale3_bw.pdf,bbllx=0.5cm,bblly=9.5cm,bburx=20cm,bbury=20cm,width=8cm,height=4.5cm,clip=}
}}
\centerline{
\hbox{
\psfig{figure=fig_wmap_pscale4_bw.pdf,bbllx=0.5cm,bblly=9.5cm,bburx=20cm,bbury=20cm,width=8cm,height=4.5cm,clip=}
\psfig{figure=fig_wmap_pscale5_bw.pdf,bbllx=0.5cm,bblly=9.5cm,bburx=20cm,bbury=20cm,width=8cm,height=4.5cm,clip=}
}}
}
\caption{{ WMAP Data (top left) and its pyramidal wavelet transform on the Sphere using five resolution levels (4 wavelet scales
and the coarse scale). The original map can be reconstructed exactly from the pyramidal wavelet coefficients.Top, original data
and the first wavelet scale. Middle, the second and third wavelet scales. Bottom, the fourth wavelet scale and the last smoothed
array. The number of pixels is divided by four at each resolution level, which can be helpful when the data set are large.}}
\label{Figure:PWTS}
\end{figure*}

In the previous algorithm, no downsampling is performed and each scale of the wavelet decomposition has the same number of pixels as
the original data set. Therefore, the number of pixels in the decomposition is equal to the number of pixels in the data multiplied
by the number of scales. For applications such as PLANCK data restoration, we may prefer to introduce a decimation in the decomposition
so as to reduce the required memory size and the computation time. This can be done easily by using a specific property of the chosen
scaling function. Indeed, since we are considering here a scaling function with an initial cut-off $l_c$ in spherical harmonic multipole
number $l$, and since the actual cut-off is reduced by a factor of two at each step, the number of significant spherical harmonic
coefficients is then reduced by a factor of four after each convolution with the low pass filter $h$. Therefore, we need less pixels
in the direct space when we compute the inverse spherical harmonic transform. Using the Healpix pixelization scheme \cite{healpix},
this can be done easily by dividing by 2 the {\it nside} parameter when calling to the inverse spherical harmonic transform routine.

{ Figure~\ref{Figure:PWTS} shows WMAP data (top left) and its pyramidal wavelet transform using five scales. As the scale number increases
(i.e. the resolution decreases), the pixel size becomes larger. Figures~\ref{Figure:PWTS} top right, middle left, middle right and bottom
left show respectively the four wavelet scales. Figure~\ref{Figure:PWTS} bottom right shows the last smoothed array.}

\subsection{Mexican hat wavelet transform on the sphere}
\index{wavelet!mexican hat}

The 1D Euclidean mexican hat wavelet is defined as the second derivative of a Gaussian. Its isotropic extension in 2D is expressed as :
\begin{equation}
\psi(r) = \frac{1}{\sqrt{2 \pi}} \Big( 2 - \Big( \frac{r}{R} \Big)^2 \Big) e^{- \frac{r^2}{2R^2}}
\end{equation}
where $R$ is the scale factor and $r$ measures the distance to the center of the wavelet. This function has been used to implement
a \emph{continuous} wavelet transform resulting in a powerful tool for data analysis purposes and especially for the detection of
mostly isotropic features embedded in noise.\\

Using the inverse stereographic projection of the plane unto the sphere, it is possible to design an extension of the continuous
Mexican Hat wavelet transform to the sphere, as motivated and explained in \cite{wave:antoine99,wave:tenerio99,wave:cayon01,wave:holschneider96}.
This leads to the following expression :
\begin{equation}
\psi_s (\delta ) = \frac{1}{\sqrt{2 \pi} N_R} \Big( 1 +  \Big( \frac{\delta}{2} \Big)^2 \Big)^2   \Big( 2 - \Big( \frac{\delta}{R} \Big)^2 \Big) e^{- \frac{\delta^2}{2R^2}}
\end{equation}
where $R$ is the scale factor, $N_R$ is a normalization factor :
\begin{equation}
N_R = R \Big( 1 + \frac{R^2}{2} + \frac{R^4}{4} \Big)^{\frac{1}{2}}
\end{equation}
and $\delta$ is the distance to the tangency point in the tangent plane which is related to the polar angle $\theta$ through the stereographic projection by :
\begin{equation}
\delta = 2 \textrm{tan} \frac{\theta}{2}
\end{equation}
The resulting wavelet function on the sphere is clearly zonal so that one can move to a spherical harmonics representation and resort
to equation~\ref{eq:conv} to compute the wavelet coefficients. This transform was used successfully for the detection of point sources
in maps of the Cosmic Microwave Background and an application to the detection of non-gaussianity in  CMB is reported in \cite{wave:vielva04}.
However, although it is profitable for data analysis, this continuous transform lacks an inverse and hence is clearly not suitable for restoration purposes.

\section{Ridgelet and Curvelet Transform on the Sphere (CTS) }
\label{sect_cur}
\subsection{Introduction.}

The 2D curvelet transform, proposed in \cite{cur:donoho99,starck:sta01_3,starck:sta02_3}, enables the directional analysis of an image
in different scales. The fundamental property of the curvelet transform is to analyze the data with functions of length about $2^{-j/2}$
for the $j^{\textrm{th}}$ sub-band $[2^j, 2^{j+1}]$ of the two dimensional wavelet transform. Following the implementation described
in \cite{starck:sta01_3,starck:sta02_3}, the data first undergoes an Isotropic Undecimated Wavelet Transform (i.e. \emph{\`a trous} algorithm).
Each scale $j$ is then decomposed into smoothly overlapping blocks of side-length $B_j$ pixels in such a way that the overlap between two
vertically adjacent blocks is a rectangular array of size $B_j \times B_j/2$. And finally, the ridgelet transform \cite{cur:candes99_1} is
applied on each individual block. Recall that the ridgelet transform precisely amounts to applying a 1-dimensional wavelet transform to the
slices of the Radon transform. More details on the implementation of the digital curvelet transform can be found in Starck et al\cite*{starck:sta01_3,starck:sta02_3}.
It has been shown that the curvelet transform could be very useful for the detection and the discrimination of non-Gaussianity in CMB \cite{starck:sta02_4}.
The curvelet transform is also redundant, with a redundancy factor of $16J+1$ whenever $J$ scales are employed. Its complexity scales like
that of the ridgelet transform that is as $O(n^2 \log_2n)$. This method is best for the detection of anisotropic structures and smooth
curves and edges of different lengths.

\subsection{Ridgelets and Curvelets on the Sphere.}
\index{curvelet transform}
The Curvelet transform on the sphere (CTS) can be similar to the 2D digital curvelet transform, but replacing the \`a trous algorithm
by the Isotropic Wavelet Transform on the Sphere previously described. The CTS algorithm consists in the following three steps which
we describe in more details next.
\begin{itemize}
\item {\it Isotropic Wavelet Transform on the Sphere.}
\item {\it Partitioning.} Each scale is decomposed into blocks of an appropriate scale (of side-length $\sim2^{-s}$), thanks to the Healpix pixelization.
\item {\it Ridgelet Analysis.} Each square is analyzed via the discrete ridgelet transform.
\end{itemize}

\subsubsection*{Partitioning using the Healpix representation.}
The Healpix representation is a curvilinear partition of the sphere into quadrilateral pixels of exactly equal area but with varying shape. The base
resolution divides the sphere into 12 quadrilateral faces of equal area placed on three rings around the poles and equator. Each face is subsequently
divided into $nside^{2}$ pixels following a hierarchical quadrilateral tree structure. The geometry of the Healpix sampling grid makes it easy to
partition a spherical map into blocks of a specified size $2^n$. We first extract the twelve base-resolution faces, and each face is then decomposed
into overlapping blocks as in the 2D digital curvelet transform. With this scheme however, there is no overlapping between blocks belonging to different
base-resolution faces. This may result in blocking effects for instance in denoising experiments \emph{via} non linear filtering. A simple way around
this difficulty is to work with various rotations of the data with respect to the sampling grid.
%The Healpix representation divides the sphere into 12 quadrilateral faces of equal area.
%Each face is subsequently divided into $nside^{2}$  pixels . Using this pixelization scheme, we can apply a partitioning
%of the data on the sphere in the following way.
%We extract first the twelve faces, and each face is then decomposed into
%overlapping blocks. Because the blocks are not very large, we readily neglect the effect of curvature on each block.

\subsubsection*{Ridgelet transform}
\index{ridgelet transform}
\index{Radon transform}
Once the partitioning is performed, the standard 2D ridgelet transform described in \cite{starck:sta02_3} is applied in each individual block :
\begin{enumerate}
\item Compute the 2D Fourier transform.
\item Extract lines going through the origin in the frequency plane.
\item Compute the 1D inverse Fourier transform of each line. We get the Radon transform.
\item Compute the 1D wavelet transform of the lines of the Radon transform.
\end{enumerate}
The first three steps correspond to a Radon transform method called the {\it linogram}. Other implementations of the Radon transform, such as
the {\it Slant Stack Radon Transform} \cite{cur:donoho_02}, can be used as well, as long as they offer an exact reconstruction.

Figure~\ref{Figure:rid_sphere} shows the flowgraph of the ridgelet transform on the sphere and Figure~\ref{Figure:back_rid}
shows the backprojection of a ridgelet coefficient at different scales and orientations.

\begin{figure*}
% \centering
%  \includegraphics[height = 5 in]{fig_flowgraph_ridgelet_sphere.pdf}
\centerline{
\hbox{
\psfig{figure=fig_flowgraph_ridgelet_sphere.pdf,bbllx=1.5cm,bblly=9cm,bburx=18.5cm,bbury=19cm,height=8cm,width=13.6cm,clip=}
}}
\caption{Flowgraph of the Ridgelet Transform on the Sphere.}
\label{Figure:rid_sphere}
\end{figure*}

\begin{figure*}
% \centering
% \includegraphics[height = 5 in]{fig_back_rid_sphere.pdf}
\centerline{
\hbox{
% \psfig{figure=fig_back_rid_sphere.ps,bbllx=0.5cm,bblly=7.5cm,bburx=21.5cm,bbury=19.5cm,height=6cm,width=9.5cm,clip=}
\psfig{figure=fig_ridssr.pdf,bbllx=0.5cm,bblly=7.5cm,bburx=21.5cm,bbury=20cm,height=6cm,width=9.5cm,clip=}
}}
\caption{Backprojection of a ridgelet coefficient at different scales and orientations.
{ Each map is obtained by setting all ridgelet coefficients to zero but one, and applying an inverse ridgelet transform. Depending on
the scale and the position of the non zero ridgelet coefficient, the reconstructed image presents a feature with a given width and a
given orientation.}}
\label{Figure:back_rid}
\end{figure*}

\subsection{Algorithm}
The curvelet transform algorithm on the sphere is as follows:
\begin{enumerate}
\item apply the isotropic wavelet transform on the sphere with $J$ scales,
\item set the block size $B_1 = B_{min}$,
\item for $j = 1, \ldots, J$ do,
\begin{itemize}
\item partition the subband $w_j$ with a block size $B_j$ and apply the digital ridgelet transform to each block,
\item if $j \mbox{ modulo } 2 = 1$ then $B_{j+1} = 2 B_{j}$,
\item else $B_{j+1} = B_{j}$.
\end{itemize}
\end{enumerate}
The sidelength of the localizing windows is doubled {\em at every other} dyadic subband, hence maintaining the fundamental property of
the curvelet transform which says that elements of length about $2^{-j/2}$ serve for the analysis and synthesis of the $j$-th subband
$[2^j, 2^{j+1}]$. We used the default value $B_{min} = 16$ pixels in our implementation. Finally, Figure~\ref{Figure:cur_sphere} gives
an overview of the organization of the algorithm.
\begin{figure*}
 % \includegraphics[height=8truecm,width=6truecm]{fig_uwt_sphere.pdf}
% \includegraphics[height = 7 in]{fig_flowgraph_curvelet_sphere.pdf}
\centerline{
\hbox{
\psfig{figure=fig_flowgraph_curvelet_sphere.pdf,bbllx=1cm,bblly=7cm,bburx=20cm,bbury=21cm,height=9cm,width=12cm,clip=}
}}
\caption{Flowgraph of the Curvelet Transform on the Sphere.}
\label{Figure:cur_sphere}
\end{figure*}
\begin{figure*}
% \includegraphics[height=8truecm,width=6truecm]{fig_uwt_sphere.pdf}
% \includegraphics[height = 5 in]{fig_back_cur_sphere.pdf}
\centerline{
\hbox{
\psfig{figure=fig_back_cur_sphere.pdf,bbllx=1cm,bblly=7cm,bburx=20cm,bbury=21cm,height=9cm,width=12cm,clip=}
}}
\caption{Backprojection of a curvelet coefficient at different scales and orientations.
{ Each map is obtained by setting all curvelet coefficients to zero but one, and applying an inverse curvelet transform.
Depending on the scale and the position of the non zero curvelet coefficient, the reconstructed image presents a feature
with a given width, length and orientation.}}
\label{Figure:back_cur}
\end{figure*}
Figure~\ref{Figure:back_cur} shows the backprojection of  curvelet coefficients at different scales and orientations.
\index{algorithm!curvelet transform}

\subsection{Pyramidal Curvelet Transform on the Sphere (PCTS)}
\index{curvelet}
\index{curvelet!pyramidal transform}

The CTS is very redundant, which may be a problem for handling huge data sets such as the future PLANCK data. The redundancy
can be reduced by substituting, in the curvelet transform algorithm, the pyramidal wavelet transform to the undecimated wavelet
transform. The second step which consists in applying the ridgelet transform on the wavelet scale is unchanged. The pyramidal
curvelet transform algorithm is:
\begin{enumerate}
\item apply the pyramidal wavelet transform on the sphere with $J$ scales,
\item set the block size $B_1 = B_{min}$,
\item for $j = 1, \ldots, J$ do,
\begin{itemize}
\item partition the subband $w_j$ with a block size $B_j$ and apply the digital ridgelet transform to each block,
\item if $j \mbox{ modulo } 2 = 2$ then $B_{j+1} = B_{j} / 2$,
\item else $B_{j+1} = B_{j}$.
\end{itemize}
\end{enumerate}
In the next section, it is shown how the pyramidal curvelet transform can be used for image filtering.


\section{Recursion} % (fold)
\label{sec:recursion}

% go slower, add more examples

\begin{frame}\frametitle{Back to control flow}
    \framesubtitle{}

    To execute repetitive code, we have relied on \texttt{for} and \texttt{while}
    loops.
    \vfill
    Furthermore, we used \texttt{if} statements to handle conditional statements.
    \vfill
    These statements are rather straightforward and easy to understand.

\end{frame}

\begin{frame}\frametitle{Recursion}
    \framesubtitle{A new type of functions}

    Recursive function solve problems by reducing them to smaller problems
    of the same form.

    \vfill

    This allows recursive functions to call themselves...

    \pause\vfill

    \begin{itemize}
        \item New paradigm
        \item Powerful tool
        \item Divide-and-conquer
        \item Beautiful solutions
    \end{itemize}

\end{frame}

\begin{frame}\frametitle{First example}
    \framesubtitle{Add numbers}

    Let's consider a trivial problem:

    Suppose we want to add two positive numbers $a$ and $b$, but we can only add/subtract one.

\end{frame}

\begin{frame}\frametitle{First example}
    \framesubtitle{Add numbers}

    Non-recursive solution:
    \codeblock{code/rec_nr_add.py}

\end{frame}

\begin{frame}\frametitle{First example}
    \framesubtitle{Add numbers}

    Recursive solution:

    \begin{itemize}
        \item Simple case: b = 0, return a
        \item Else, we can return 1 + add(a, b-1)
    \end{itemize}
    \pause
    \codeblock{code/rec_add.py}

\end{frame}

\begin{frame}\frametitle{Base case and recursive steps}
    \framesubtitle{Two parts of any recursive function}

    Recursive functions consist of two parts:

    \begin{description}
        \item[base case] The base case is the trivial case that can be dealt with
            easily.
        \item[recursive step] The recursive step brings us slightly closer to
            the base case and calls the function itself again.
    \end{description}

\end{frame}

\begin{frame}\frametitle{Reversing a list}

    How can we recursively reverse a list ([1, 2, 3] $\to$ [3, 2, 1]).

    \begin{itemize}
        \item If list is empty or has one element, the reverse is itself
        \item Otherwise, reverse elements 2 to $n$, and append the first
    \end{itemize}

    \pause

    \codeblock{code/rec_reverse.py}

\end{frame}

\begin{frame}\frametitle{Palindromes}
    \framesubtitle{Another example}

    A palindrome is a word that reads the same from both ways, such as
    \emph{radar} or \emph{level}.
    \vfill
    Let's write a function that checks whether a given word is a palindrome.

\end{frame}

\begin{frame}\frametitle{The recursive idea}
    \framesubtitle{}

    Given a word, such as \emph{level}, we check:
    \begin{itemize}
        \item whether the first and last character are the same
        \item whether the string with first and last character removed are the same
    \end{itemize}

\end{frame}

\begin{frame}\frametitle{Base case}
    \framesubtitle{}

    What's the base case in this case?

    \pause
    \begin{itemize}
        \item The empty string is a palindrome
        \item Any 1 letter string is a palindrome
    \end{itemize}

\end{frame}

\begin{frame}\frametitle{Implementation}
    \framesubtitle{}

    \codeblock{code/rec_palindrome.py}

    \pause

    What is an iterative solution?

\end{frame}

\begin{frame}\frametitle{Numerical integration}
    \framesubtitle{}

    Suppose we want to numerically integrate some function $f$:

    \[
        A = \int_a^b f(x) dx
    \]

    \pause\vfill

    Trapezoid rule:

    \begin{align*}
        A &= \int_a^b f(x) dx\\
         &= \int_a^{r_1} f(x) dx + \int_{r_1}^{r_2} f(x) dx + \ldots + \int_{r_{n-1}}^{b} f(x) dx\\
         &\approx \frac{h}{2}\left((f(a) + f(r_1)) +  h(f(r_1) + f(r_2)) + \ldots + (f(b) + f(r_{n-1}))\right)\\
         &= \frac{h}{2} (f(a) + f(b)) \quad+\quad h (f(r_1) + f(r_2) +\ldots+ f(r_{n-1}))
    \end{align*}
\end{frame}

\begin{frame}\frametitle{Trapezoid rule}
    \framesubtitle{Implementation}

    \codeblock{code/rec_trapezoid.py}

    Forget the math / code:
    This function approximates the area under f between a and b using N points.

\end{frame}

\begin{frame}\frametitle{Key point}

    \begin{itemize}
        \item If function is flat, then we don't need many points.
        \item If function is very wiggly, we need a lot of points.
    \end{itemize}

    So:

    \begin{itemize}
        \item How many points do we need?
        \item What if function is flat in some areas, wiggly in others?
    \end{itemize}

\end{frame}

\begin{frame}\frametitle{Adaptive integration}
    \framesubtitle{Recursive implementation}

    Idea: Adaptively space points based on local curvature of function.

    Areas where function is flat: few points, areas where function is wiggly: many points.

    \pause

    \codeblock{code/rec_adaptint.py}

    Note: we do not need to use trapezoid rule.

\end{frame}

\begin{frame}\frametitle{Pitfalls}
    \framesubtitle{}

    Recursion can be very powerful, but there are some pitfalls:

    \begin{itemize}
        \item Have to ensure you always reach the base case.
        \item Each successive call of the algorithm must be solving a simpler problem
        \item The number of function calls shouldn't explode. (see exercises)
        \item An iterative algorithm is always faster due to overhead of function calls.
         (However, the iterative solution might be much more complex)
    \end{itemize}

\end{frame}

% section recursion (end)


\section{Calculus with Parametric Equations}\label{sec:Calculus with Parametric Equations}
We have already seen how to compute slopes of curves given by
parametric equations---it is how we computed slopes in polar
coordinates.

\begin{example}{Slope of Cycloid}{cycloidslope}
 Find the slope of the cycloid $x=t-\sin t$, $y=1-\cos t$.
\end{example}

\begin{solution}
We compute $x'=1-\cos t$, $y'=\sin t$, so
$${dy\over dx} ={\sin t\over  1-\cos t}.$$
Note that when $t$ is an odd multiple of $\pi$, like $\pi$ or $3\pi$,
this is $(0/2)=0$, so there is a horizontal tangent line, in agreement
with figure~\ref{fig:cycloid}. At even multiples of $\pi$, the
fraction is $0/0$, which is undefined. The figure shows that
there is no tangent line at such points.
\end{solution}

Areas can be a bit trickier with parametric equations, depending on
the curve and the area desired. We can potentially compute areas
between the curve and the $x$-axis quite easily.

\begin{example}{Area Under Cycloid Arch}{areaundercycloidarch}
 Find the area under one arch of the cycloid
$x=t-\sin t$, $y=1-\cos t$.
\end{example}

\begin{solution}
We would like to compute
$$\int_0^{2\pi} y\;dx,$$
but we do not know $y$ in terms of $x$. However, the parametric
equations allow us to make a substitution: use $y=1-\cos t$
to replace $y$, and compute $dx=(1-\cos t)\;dt$. Then the integral
becomes
$$\int_0^{2\pi} (1-\cos t)(1-\cos t)\;dt=3\pi.$$

Note that we need to convert the original $x$ limits to $t$ limits
using $x=t-\sin t$. When $x=0$, $t=\sin t$, which happens only when
$t=0$. Likewise, when $x=2\pi$, $t-2\pi=\sin t$ and
$t=2\pi$. Alternately, because we understand how the cycloid is
produced, we can see directly that one arch is generated by
$0\le t\le 2\pi$. In general, of course, the $t$ limits will be
different than the $x$ limits.
\end{solution}

This technique will allow us to compute some quite interesting areas,
as illustrated by the exercises.

As a final example, we see how to compute the length of a curve given
by parametric equations. %Section~\ref{sec:arc length} investigates
The arc length for functions given as $y$ in terms of $x$ is
the formula:
$$\int_a^b \sqrt{1+\left({dy\over dx}\right)^2}\;dx.$$
Using some properties of derivatives, including the chain rule, we can
convert this to use parametric equations $x=f(t)$, $y=g(t)$:
\begin{eqnarray*}
  \ds\int_a^b \sqrt{1+\left({dy\over dx}\right)^2}\;dx&=
  \ds\int_a^b \sqrt{\left({dx\over dt}\right)^2
  +\left({dx\over dt}\right)^2\left({dy\over dx}\right)^2}\;{dt\over dx}\;dx\cr
  \\
  &=\ds\int_u^v \sqrt{\left({dx\over dt}\right)^2+
    \left({dy\over dt}\right)^2}\;dt\cr
    \\
  &=\ds\int_u^v \sqrt{(f'(t))^2+(g'(t))^2}\;dt.\cr
\end{eqnarray*}
Here $u$ and $v$ are the $t$ limits corresponding to the $x$ limits
$a$ and $b$.

\begin{example}{Length of Cycloid Arch}{lengthofcycloidarch}
 Find the length of one arch of the cycloid.
\end{example}

\begin{solution}
From $x=t-\sin t$, $y=1-\cos t$, we get the derivatives
$f'=1-\cos t$ and $g'=\sin t$, so the length is
$$
  \int_0^{2\pi} \sqrt{(1-\cos t)^2+\sin^2 t}\;dt=
  \int_0^{2\pi} \sqrt{2-2\cos t}\;dt.
$$
Now we use the formula $\ds \sin^2(t/2)=(1-\cos(t))/2$ or
$\ds 4\sin^2(t/2)=2-2\cos t$ to get
$$\int_0^{2\pi} \sqrt{4\sin^2(t/2)}\;dt.$$
Since $0\le t\le2\pi$, $\sin(t/2)\ge 0$, so we can rewrite this as
$$\int_0^{2\pi} 2\sin(t/2)\;dt = 8.$$
\end{solution}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Opensolutionfile{solutions}[ex]
\section*{Exercises for \ref{sec:Calculus with Parametric Equations}}

\begin{enumialphparenastyle}

%%%%%%%%%%
\begin{ex}
 Consider the curve of ~\ref{exer:pseudo cycloid} in
section~\ref{sec:Parametric Equations}. Find all values of
$t$ for which the curve has a horizontal tangent line.
\begin{sol}
 There is a horizontal tangent at all multiples of $\pi$.
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
 Consider the curve of ~\ref{exer:pseudo cycloid} in
section~\ref{sec:Parametric Equations}. Find the area under
one arch of the curve.
\begin{sol}
 $9\pi/4$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
 Consider the curve of  \ref{exer:pseudo cycloid} in
section~\ref{sec:Parametric Equations}. Set up an integral
for the length of one arch of the curve.
\begin{sol}
 $\ds \int_0^{2\pi}{1\over2} \sqrt{5-4\cos t}\;dt$
\end{sol}
\end{ex}

%
%%%%%%%%%%%
%\\begin{ex}
% Consider the hypercycloid of
%%%%%%%%%%%
%\\begin{ex}
%~\ref{exer:hypercycloid} in
%section~\ref{sec:parametric equations}. Find all points at
%which the curve has a horizontal tangent line.
%\begin{sol}
% Four points:\hfill\break
%$\ds \left({-3-3\sqrt5\over4},
%\pm\sqrt{5-\sqrt5\over8}\right)$,\hfill\break
%$\ds \left({-3+3\sqrt5\over4},
%\pm\sqrt{5+\sqrt5\over8}\right)$
%\end{sol}
%\end{ex}
%
%%%%%%%%%%%
%\\begin{ex}
% Consider the hypercycloid of
%%%%%%%%%%%
%\\begin{ex}
%~\ref{exer:hypercycloid} in
%section~\ref{sec:parametric equations}. Find the area between the
%large circle and
%one arch of the curve.
%\begin{sol}
% $11\pi/3$
%\end{sol}
%\end{ex}
%
%%%%%%%%%%%
%\\begin{ex}
% Consider the hypercycloid of
%%%%%%%%%%%
%\\begin{ex}
%~\ref{exer:hypercycloid} in section~\ref{sec:parametric
%  equations}. Find the length of one arch of the curve.
%\begin{sol}
% $32/3$
%\end{sol}
%\end{ex}
%
%%%%%%%%%%%
%\\begin{ex}
% Consider the hypocycloid of
%%%%%%%%%%%
%\\begin{ex}
%~\ref{exer:hypocycloid} in
%section~\ref{sec:parametric equations}. Find the area inside the curve.
%\begin{sol}
% $2\pi$
%\end{sol}
%\end{ex}
%
%%%%%%%%%%%
%\\begin{ex}
% Consider the hypocycloid of
%%%%%%%%%%%
%\\begin{ex}
%~\ref{exer:hypocycloid} in section~\ref{sec:parametric
%  equations}. Find the length of one arch of the curve.
%\begin{sol}
% $16/3$
%\end{sol}
%\end{ex}
%
%%%%%%%%%%%
%\\begin{ex}
% Recall the involute of a circle from
%%%%%%%%%%%
%\\begin{ex}
%~\ref{exer:involute of a circle} in
%section~\ref{sec:parametric equations}. Find the point in the first
%quadrant in figure~\ref{fig:involute}
%at which the tangent line is vertical.
%\begin{sol}
% $(\pi/2,1)$
%\end{sol}
%\end{ex}
%
%%%%%%%%%%%
%\\begin{ex}
% Recall the involute of a circle from
%%%%%%%%%%%
%\\begin{ex}
%~\ref{exer:involute of a circle} in
%section~\ref{sec:parametric equations}. Instead of an infinite
%string, suppose we have a string of length $\pi$ attached to the unit
%circle at $(-1,0)$, and initially laid around the top of the circle
%with its end at $(1,0)$. If we grasp the end of the string and begin
%to unwind it, we get a piece of the involute, until the string is
%vertical. If we then keep the string taut and continue to rotate it
%counter-clockwise, the end traces out a semi-circle with center at
%$(-1,0)$, until the string is vertical again. Continuing, the end of
%the string traces out the mirror image of the initial portion of the
%curve; see figure~\ref{fig:involute plus semicircle}. Find the area
%of the region inside this curve and outside the unit circle.
%\begin{sol}
% $\ds 5\pi^3/6$
%\end{sol}
%\end{ex}
%
%%%%%%%%%%%
%\\begin{ex}
% Find the length of the curve from the previous %%%%%%%%%%
%\\begin{ex}
%,
%shown in figure~\ref{fig:involute plus semicircle}.
%\begin{sol}
% $\ds 2\pi^2$
%\end{sol}
%\end{ex}
%
%%%%%%%%%%%
%\\begin{ex}
% Find the length of the spiral of Archimedes
%(figure~\ref{fig:area inside spiral}) for $0\le\theta\le2\pi$.
%\begin{sol}
% $\ds(2\pi\sqrt{4\pi^2+1}+\ln(2\pi+\sqrt{4\pi^2+1}))/2$
%\end{sol}
%\end{ex}
%
%\figure
%\vbox{\beginpicture
%\normalgraphs
%\sevenpoint
%\setcoordinatesystem units <7truemm,7truemm>
%\setplotarea x from -5 to 2, y from -4  to 3.5
%\axis left shiftedto x=0 /
%\axis bottom shiftedto y=0 /
%\circulararc 360 degrees from 0 1 center at 0 0
%\setquadratic
%\textRed
%\plot 1.000 0.000 1.005 0.000 1.022 0.003 1.048 0.010 1.084 0.024
%1.128 0.047 1.178 0.079 1.234 0.124 1.292 0.183 1.350 0.255
%1.407 0.342 1.459 0.445 1.504 0.563 1.540 0.695 1.563 0.841
%1.571 1.000 1.562 1.170 1.533 1.348 1.484 1.534 1.411 1.723
%1.314 1.913 1.191 2.102 1.043 2.285 0.868 2.459 0.668 2.621
%0.443 2.767 0.194 2.894 -0.077 2.998 -0.369 3.076 -0.677 3.125
%-1.000 3.142 /
%\plot -1.000 -3.142 -0.677 -3.125 -0.369 -3.076 -0.077 -2.998 0.194 -2.894
%0.443 -2.767 0.668 -2.621 0.868 -2.459 1.043 -2.285 1.191 -2.102
%1.314 -1.913 1.411 -1.723 1.484 -1.534 1.533 -1.348 1.562 -1.170
%1.571 -1.000 1.563 -0.841 1.540 -0.695 1.504 -0.563 1.459 -0.445
%1.407 -0.342 1.350 -0.255 1.292 -0.183 1.234 -0.124 1.178 -0.079
%1.128 -0.047 1.084 -0.024 1.048 -0.010 1.022 -0.003 1.005 -0.000
%1.000 0.000 /
%\circulararc 180 degrees from -1.000 3.142 center at -1 0
%\textBlack
%\setlinear\setdashes <2pt>
%\plot -1 -3.142 -1 3.142 /
%\plot -1 0 -3.221 2.221 /
%\plot -0.42 0.91 1.4 1.74 /
%\endpicture}
%\label{fig:involute plus semicircle}
%\endfigure{A region formed by the end of a string.}
%
%\end%%%%%%%%%%
%\\begin{ex}
%s

\end{enumialphparenastyle}