\chapter{Method}
\label{chap:method}

In this chapter, I first describe techniques that lie at the core of the proposed solution.
They then followed by a detailed explanation of the proposed solution.
Finally, I give the implementation details and settings.

\section{Neural Radiance Fields}

The Neural Radiance Fields approach (\cite{mildenhall2020nerf}) lies directly in the heart of the proposed solutions.
The continuous 5D function

\begin{equation}
    \label{eq:nerf_function}
    F : (p,v) \xrightarrow{} (c, \sigma)
\end{equation}

is required to be achieved in order to solve the novel view synthesis problem.
Given the spatial location $p \in \mathbb{R}^3$ and the 2D viewing direction $(\theta, \phi)$ (or its equivalent 3D Cartesian representation $v \in \mathbb{R}^3$),
one has to obtain the RGB color value $c$ and the volume density $\sigma$ at this exact point $p$ under the viewing direction $v$.
To obtain the image of the scene, one can take a pin-hole camera at position $p_0$ and cast rays to the scene: $p(z) = p_0 + z\cdot v$.
The final visible color value $C(p,v)$ can then be evaluated using volume rendering approach \cite{niemeyer2020differentiable, Novak18volumeSTAR} along the ray $p$:
\begin{equation}
    C(p,v) = \int_{0}^{\infty} \omega(p(z)) \cdot c(p(z),v) dz,
    \label{eq:rendering_equation}
\end{equation}
$\omega(p(z))$ is a probability weight function
\begin{equation}
    \omega(p(z)) = \tau_c(p(z)) \sigma(p(z)),
\end{equation}
where $\tau_c(p(z)) = e^{\int_0^z \sigma(p(s)) ds}$ denotes the accumulated transmittance along the ray up to the point $p(z)$, $\sigma(p(z))$ is the volume density at point $p(z)$ and the probability property is held: $\int_0^\infty \omega(p(z))dz = 1$

\begin{figure}[!htb]
    \centering
    \includegraphics[width=0.9\textwidth]{figures/mlp_nerf.png}
    \caption{MLP used in NeRF \cite{mildenhall2020nerf} Dark blue boxes represent hidden layers. Black arrows indicate FC-layers with sigmoid activation, orange arrows - FC-layers without activation. Green boxes are inputs: $p$ is a 3D sample point, $v$ is a 3D vector of viewing direction. Red boxes are the outputs: $\sigma$ is 1D volume density, $c$ is a 3D color value. $\zeta(.)$ stands for positional encoding function that maps inputs into higher dimensional space $\mathbb{R}^{2L}$ (more in \Cref{subsec:pos_enc}}
    \label{fig:mlp_nerf}
\end{figure}

\cite{mildenhall2020nerf} propose to use a Multi-Layer Perceptron (MLP) as a representation
of an implicit function $F = F_\Theta$ with parameters $\Theta$.
The scheme structure of the original NeRF's MLP is outlined in \Cref{fig:mlp_nerf}.
The position $p(z)$ is first encoded using position encoding function $\zeta(\cdot)$ (positional encoding is described in section \ref{subsec:pos_enc}) and then is processed with 8 fully-connected neural layers with ReLU activations.
The resulting feature vector is then concatenated with positionally encoded vector direction $v$
before the volume density $\sigma(p(z))$ and the color value $c(p(z), v)$ is output.
The important concept here is that $\sigma$ only depends on point $p(z)$
whilst the color value $c$ is view dependant as well.
This setting is required to encourage the MLP to be multi-view consistent.

In order to perform the volume rendering, the continuous integral from equation \Cref{eq:rendering_equation} has to be solved.
One can estimate this integral using a discrete set of densely sampled points as proposed by \cite{mildenhall2020nerf}.
Separating each ray into $N$ bins and drawing random samples from each bin
makes the representation able to learn continuous function
while only using a finite number of samples.
The estimate for the integral $C(p, v)$ will have a form (\cite{mildenhall2020nerf, max1995optical}):
\begin{equation}
    \label{eq:integral_estimation}
    C(p, v) \approx \sum_{i=1}^{N} \tau_c(p_i) (1 - \exp (-\sigma(p_i) \delta_i)) c(p_i, v),
\end{equation}
where $p_i = p(z_i)$ for brevity, $\tau_c(p_i) = \exp (-\sum_{j=0}^i \sigma(p_j) \delta_j )$ is the accumulated transmittance and $\delta_i = z_{i+1} - z_i$ is the distance between adjacent sample points.

\subsection{Positional encoding}
\label{subsec:pos_enc}

Neural networks are known as a highly representative class of functions (\cite{hornik1989multilayer}).
However, recent works (\cite{rahaman2019spectral, tancik2020fourfeat}) demonstrate the tendency of
biasing towards low-frequency functions during the training of deep neural networks.
The results can be smoothed and blurry as frequency-dependent learning speed
is much slower for high-frequency parts of the scene.
\cite{rahaman2019spectral} show that one can highly improve the quality of the MLP outputs
by using an embedding, which maps inputs to a higher dimensional space before passing them to the MLP.

The positional encoding function $\zeta(p) : \mathbb{R} \xrightarrow{} \mathbb{R}^{2L}$ as proposed by \cite{mildenhall2020nerf, vaswani2017attention} is represented by Fourier series in the following form:
\begin{equation}
    \label{eq:positional_encoding}
    \zeta(x) = (\sin(2^0\pi x), \cos(2^0\pi x), ..., \sin(2^{L-1}\pi x), \cos(2^{L-1}\pi x))
\end{equation}
The 3D Cartesian coordinates of $p$ and $v$ are then separately transformed using $\zeta(\cdot)$ and concatenated.

\subsection{Hierarchical sampling}
\label{subsec:hierarchy_sampling}

The proposed approach is ineffective in a way that it tends to generate a lot of unimportant samples
that lie in free space and do not contribute much.
To solve that authors propose the hierarchical volume sampling algorithm (similar to \cite{levoy1990efficient})
when another "coarse" neural network is used in order to roughly estimate volume densities along the ray.
This insight is then used to perform a more informed sampling of the main ("fine") network.
The "coarse" network is being optimized along with the main network, which can be considered as an overhead.
However, taking into account the increase of training convergence the overall training gets more efficient.

\subsection{Optimization}

The fully differentiable formulation of the problem makes it possible
to optimize network parameters $\Theta$ without any 3D supervision.
This is done using the back-propagation approach from the differences
between rendered predictions and ground truth images.
This task, therefore, is reduced to the minimization problem of the loss function.
The original method by \cite{mildenhall2020nerf} for loss function uses the following formulation:
\begin{equation}
    \label{eq:loss_func}
    \mathcal{L} = \sum_{(p_0, v) \in \mathcal{B}} \norm{ C_c(p, v) - C^*(p, v) }^2_2 + \norm{ C_f(p, v) - C^*(p, v) }^2_2,
\end{equation}
where $\mathcal{B}$ is the set of sampled rays in each batch,
$C^*$ is the target color value of the view ray $v$ and
$C_c(p, v)$, $C_f(p, v)$ - are the predicted colors for the view ray $v$ of the coarse and fine networks respectively.

Another regularization term can be added to this loss function as proposed by \cite{Lombardi_2019}.
The beta-distribution regularizer $\Omega(\cdot)$
forces the ray transmittance from the fine network $\tau_c$ to be close to $0$ or $1$:
\begin{equation}
    \label{eq:beta_regularizer}
    \Omega(\tau_c(p_i)) = \log(\tau_c(p_i)) + \log(1 - \tau_c(p_i))
\end{equation}
This helps the network to better handle background colors and get cleaner rendering after all.
This regularization term is used in NSVF work (\cite{liu2021neural})
that is described in \Cref{subsec:NSVF}
and NRF work (\cite{bi2020neural}) that is described in \Cref{subsec:NRF}

\subsection{Sparse Voxels Octree}
\label{subsec:NSVF}
The described approach is already able to represent scenes with high accuracy.
However, the very inefficient way of sampling points along rays
makes the training very slow and tough,
since the model has to be repeatedly evaluated on unimportant samples
of free space or occluded regions that do not make a worthwhile contribution to the finally rendered color value.
The \textit{hierarchical sampling} strategy alleviates the problem
but does not solve it completely.
The method in its formulation, therefore, is still barely practical.

\cite{liu2021neural} propose another framework
that is able to increase the efficiency of this method
and make the training time up to an order of magnitude faster.
The main contribution is the usage of sparse voxel octrees (\cite{laine2011EfficientSV}) to divide space into voxels
that are efficiently used for training MLPs with shared parameters.

This approach requires a slight elaboration on the problem formulation.
Let the whole scene be fully contained in a set of $K$ sparse voxels: $\mathbb{V} = \{V_k\}_{k=1}^K$.
The scene can then be represented as a set of voxel-bounded implicit functions
\begin{equation}
    F(p, v) = F_\Theta^i(g_i(p), \zeta(v)), \forall p \in V_i,
\end{equation}
where each function $F_\Theta^i$ is modeled with an MLP with shared parameters $\Theta$:
\begin{equation}
    F_\Theta^i: (g_i(p), \zeta(v)) \xrightarrow{} (c, \sigma), \forall p \in V_i
\end{equation}
Here $g_i(p)$ is the representation of $p$ that is defined as:
\begin{equation}
    g_i(p) = \zeta ( \chi ( \{\Tilde{g_i}(p_j^*)\}_{j=1}^8 ) ),
\end{equation}
where $\{p_j^*\}_{j=1}^8, p_j^* \in \mathbb{R}^3$ is the set of eight vertices of voxel $V_i$,
$\Tilde{g_i} \in \mathbb{R}^d$ represent the feature vectors that are stored for each vertex of voxel $V_i$,
$\chi(\cdot)$ stands for trilinear interpolation,
and $\zeta(\cdot)$ is a post-processing function,
which corresponds to positional encoding function explained in \ref{subsec:pos_enc}.

\begin{figure}[!htb]
    \centering
    \includegraphics[width=0.9\textwidth]{figures/vanilla_nsvf.png}
    \caption{Network structure of the NSVF approach
(the figure is adapted from \cite{liu2021neural}).
Same notation as for \Cref{fig:mlp_nerf} is used.
Dotted lines denote semantic parts of the presented structure:
pink, green and blue dotted lines stand for encoder, density predictor and texture predictor respectively.
\textit{Trilinear interpolation} block denotes interpolation of the sample point $p$ between voxel embeddings.}
    \label{fig:nsvf_structure}
\end{figure}

The proposed network structure is outlined in \Cref{fig:nsvf_structure}.
The encoder part that is denoted with a pink dotted line
consists of only one layer comparing to the original NRF network
where the encoder part consists of 5 deep layers.
The density predictor (green dotted line) remains the same.
The texture predictor (blue dotted line) is deeper comparing to the NeRF model.
The overall model structure is slightly shallower comparing to the original NeRF network,
which is still sufficient due to the distributed way of storing voxel embeddings in the voxel vertices.
Without voxel embeddings, the model contains approximately 0.5M learnable parameters,
which is lower than in the original NeRF model.

The volumetric rendering itself does not undergo any significant changes
except for some additional steps that have to be performed.
The sampling of the ray is only performed at the intervals of intersection with voxels $V_i$.
This allows rejecting a decent amount of unimportant samples.
A very efficient Axis Aligned Bounding Box (AABB) ray intersection algorithm (\cite{haines1989essential}) is applied here
to extract the above-mentioned intervals.
Another step is \textit{early termination}, which reduces the number of samples that have to be processed.
Since solid surfaces do not imply color values being dispersed along the ray,
the sample points that are behind the surface would be needless.
The technique here is to straightforwardly stop evaluating points,
that have an accumulated transmittance lower than some threshold.

\subsection{Voxels pruning and refinement}
\label{subsec:pruning}

The training on the scene begins with the octree,
which consists of $N=N^3_0$ voxels that fit in the bounding box of the whole scene
and cover all parts of the scene.
One can get rid of non-essential voxels by performing a \textit{self-pruning} procedure.
This technique leverages the property of neural radiance fields
to extract some coarse scene geometry during early training iterations.
The decision if voxel is going to be pruned is made by uniformly sampling $G$ points inside the voxel
and comparing the minimum transmittance value with some threshold value $\gamma$ or more formally when:
\begin{equation}
    \underset{j=1 \dots G}{min} {\exp (- \sigma (g_i(p_j)))} > \gamma, p_j \in V_i, V_i \in \mathbb{V},
\end{equation}
where $\sigma (g_i(p_j))$ is the model prediction for $\sigma$ when processing $g_i(p_j)$.
The pruning procedure has to be performed regularly as the accuracy of the model increases as it gets more and more trained,
which means that more voxels can be pruned and therefore rejected from processing.

As the model learns a more detailed representation of the scene it becomes necessary to refine the voxel structure.
During the refinement procedure, each voxel is being subdivided into $2^3$ subvoxels by halving the voxel size.
Ray sampling steps are also being halved at this point in order to correspond new voxel size value.
The feature representation vectors $\Tilde{g_i}$ of the new vertices are initialized similarly to calculating $G_i(p)$,
namely using trilinear interpolation function $\chi(\cdot)$.
This scheme allows to achieve more detailed scene representation
and also increases the model capacity.

\subsection{Neural Reflectance Fields}
\label{subsec:NRF}

Although the neural radiance fields approach is able to produce realistic renderings under novel view conditions,
one of the main limitations of this method is the inability to handle any kind of visual effects related to dynamic light conditions.
The core assumption of these approaches is static illumination,
which is implicitly presented in the color values of the rendered images.
This in turn does not allow to change illumination on the rendering phase.

\cite{bi2020neural} propose the framework for eliminating this exact limitation.
Authors introduce the neural reflectance field (NRF), which is the neural scene representation
that in addition to scene geometry also encodes normal and reflectance properties of the scene.
This approach allows the evaluation of an explicit reflectance model at the selected point after all.
Although \cite{bi2020neural} focus on the microfacet bidirectional reflectance distribution function (BRDF) (\cite{walter2007microfacet}),
any other reflectance model can be used within this method as well
(i.e. authors also show some results of furry objects using hair reflectance model \cite{kajiya1989fur}).

This method uses the same rendering framework as proposed by \cite{mildenhall2020nerf}.
Assuming that the volume containing the scene to be only scattering volume (i.e. there are no emission or absorption processes),
the corresponding rendering equation \Cref{eq:rendering_equation} (\cite{Novak18volumeSTAR}) looks similarly
except for operating with radiance values $L$ instead of color values $c$:
\begin{equation}
    L(p, v) = \int_0^\infty \omega(p(z)) \cdot L_s(p(z), v) dz = \int_0^\infty \tau_c(p(z)) \sigma(p(z)) \cdot L_s(p(z), v)dz,
\end{equation}
where $L_s(p(z), v)$ denotes the scattered light at point $p(z)$ along direction $v$.

In general case $L_s$ is calculated by integrating incident radiance along the whole unit sphere $\mathbb{S}$:
\begin{equation}
    \label{eq:radiance_Li}
    L_s(p, v) = \int_\mathbb{S} f_p(p, v, l) L_i(p, l)dl,
\end{equation}
where $l$ is the 3D Cartesian representation of directions of the incident radiance $L_i(p, l)$.
$f_p(p, v, l)$ is the phase function, which controls
how the light that comes from $l$ scatters at point $p$ if viewing from $v$.

\cite{bi2020neural} only assume single light source setting
and the reflectance function as the phase function,
which allows simplifying \Cref{eq:radiance_Li}:
\begin{equation}
    L_s(p, v) = f_r(p, v, l, n(p), R(p)) L_i(p, l),
\end{equation}
where $f_r$ denotes a differentiable reflectance function
with parameters $R$.
Normal vector $n$ is also required for evaluation of reflectance model $f_r$.
$L_i(p, l)$ is the radiance that arrives from direction $l$ and can be calculated using light intensity $L_l(p)$
and the transmittance value $\tau_l(p, l)$,
which indicates the loss of light from the light source to the sample point $p$ along light ray $l$, as follows:
$L_i(p, l) = \tau_l(p, l) L_l(p, l)$.
The light attenuation coefficient $f_{att} = \frac{1}{k_c + k_l d + k_q d^2}$ \cite{madams2011attenuation, madams2011improved} is considered inside the $L_l$ term:
$L_l(p, l) = f_{att} \cdot I$,
where $k_c$, $k_l$ and $k_q$ are attenuation factors,
$I$ is the point light intensity.
% where $d$ is distance from sample point $p$ to the light source
% and $r$ is the light's radius (assuming light source to be an emissive sphere with radius $r$) and
In my experiments the attenuation coefficients $k_c = k_l = 0$ and $k_q = 1$ were used,
so the attenuation coefficient is inverse proportional to squared distance between light sample point and light source:
$f_{att} = \frac{1}{d^2}$

Combining these equations and applying the same technique for numerical integral estimation
as used by \cite{mildenhall2020nerf} and \cite{liu2021neural} gives the following formulation of \Cref{eq:integral_estimation} for NRF:
\begin{equation}
    \label{eq:integral_estimation_nrf}
    L(p, v, l) \approx \sum_{i=1}^N \tau_l(p_i, l) L_l(p_i, l) f_r(p_i, v, l, n(p_i), R(p_i)) \tau_c(p_i) (1 - \exp (-\sigma(p_i)\delta_i))
\end{equation}
Here $\tau_l$ plays the same role for light ray $l$
as $\tau_c$ operates for view ray $v$.
It is evaluated similarly along the light ray $l$:
$\tau_l(p'_i, l) = \exp (-\sum_{j=1}^M \sigma(p'_j) \delta'_j)$,
where $p'(z') = p'_0 + z' \cdot l$ are sample points that are additionally sampled along the light ray $l$ and $\delta'_j = z'_{j=1} - z'_j$.

\begin{figure}[!htb]
    \centering
    \includegraphics[width=0.9\textwidth]{figures/vanilla_nrf.png}
    \caption{MLP structure of the NRF \cite{bi2020neural} approach.
The overall network structure echoes the original NeRF model.
14 deep layers are used (instead of 10 in NeRF).
The appearance model block represents BRDF $f_r$
that is evaluated for each sample point $p$.
To this end network outputs normal $n \in \mathbb{R}^3$
and BRDF parameters $R \in \mathbb{R}^m$ ($m=4$ for microfacet BRDF).
$L_l$ denotes light intensity with consideration to distance attenuation.
}
    \label{fig:nrf_structure}
\end{figure}

The architecture of the MLP is outlined in \Cref{fig:nrf_structure}.
The deeper structure is used as the model capacity has to be increased
due to the higher complexity of the task.
For each view sample point $p$ the model outputs both normal vector $n$ and parameters $R$,
which are required by the selected BRDF model.
For the microfacet BRDF (\cite{walter2007microfacet}) $R$ consists of a 3-dimensional albedo value
and 1-dimensional roughness value.

This formulation covers the general case when the light source can be located at an arbitrary position ('arbitrary setting').
Since the $l$ and $v$ vectors are different,
the additional sampling of light ray for each view sample point is required.
It makes this problem impractical. %infeasible.
However, \cite{bi2020neural} show that one specific light setting exists,
which can be used to handle this problem.
When the light ray $l$ coincide with the view ray $v$,
then sampling points $p(z)$ and $p'(z)$ would be the same,
which in turn means that $\tau_c(p_i) = \tau_l(p_i)$ ('co-located setting').
In this case, there is no need to additionally sample light rays,
which leave the processing time requirements on the same level as the original work by \cite{mildenhall2020nerf}.
% \Cref{fig:colocated_light_source} \im{add figure} shows this process in more details.
Another advantage of this setting is that it has an accessible real-world analogy.
In particular, all modern cellphones are equipped with cameras
and flashlights that are placed close to each other.
On the scale of the scene, this displacement can be neglected and the setting can be considered to be co-located.

The co-located setting results in a very sparse sampling of the scene in terms of light-view directions.
During training, there are no other samples
where the angle between light and view rays is greater than zero.
This fact negatively affects the overall training performance,
although \cite{bi2020neural} claim
%proves
that even with these restrictions
the network is still able to generalize and reconstruct the scene appearance in high quality.
The authors also show some renders where the 'arbitrary setting' had been used during the prediction phase.

\section{Explicit Neural Reflectance Field}
\label{sec:explicit_scheme}

The described above methods have already achieved some appealing results.
However, all of them have their own limitations that in either event reduce the performance either qualitatively (no light interaction) or quantitatively (too slow).
The explained below solution is constructed to take advantage of both NRF and NSVF approaches.
This method is later referred to as the 'brute-force scheme' as it performs the light rays sampling without any approximation techniques.

Since the NRF approach \cite{bi2020neural} is based on NeRF, it inherits its poor performance behavior
and forces it to be only applicable for the 'co-located setting'.
Although the authors are even able to render the scene within the 'arbitrary setting'
when only being trained on the co-located dataset,
the sparsity of the training dataset still highly affects the network's generalization opportunities.

\cite{liu2021neural} in turn propose the technique for increasing the effectiveness of the training
for the color-based original NeRF approach.
The usage of gradually refined octrees gives more control over the sampling process
and makes the whole training phase faster.

\begin{figure}[!htb]
    \centering
    \includegraphics[width=0.9\textwidth]{figures/explicit_scheme.png}
    \caption{Model architecture for the 'explicit scheme'
    (\Cref{sec:explicit_scheme}).
The overall structure follows one from NSVF work.
The outputs of the network are similar to the NRF model.
BRDF evaluation ('appearance model' block) is required for achieving $f_r$ value.
    }
    \label{fig:mlnrf_bfex}
\end{figure}

Since both works NSVF and NRF use the original NeRF framework,
in particular similar numerical integral estimation,
the process of reformulating NSVF problem to work with light radiances instead of color values is fairly straightforward.
For the integral estimation \Cref{eq:integral_estimation} is directly replaced with \Cref{eq:integral_estimation_nrf}.
The network architecture follows the guidelines from the NSVF work,
namely the encoder is shallow while the texture predictor is deeper
comparing to the original NeRF method.
Appearance model block (right blue rhombus) includes BRDF evaluation,
which results in $f_r$ value required for final radiance calculation (using integral estimation formula \cref{eq:integral_estimation_nrf})

In order to train the model on arbitrary light dataset
an additional step of light rays sampling and volume density prediction is needed.
This requires some nested model evaluations in order to retrieve $\sigma$ values at light sample points.
This is quite a memory intense part, however, the lower model size comparing to the NRF approach makes it more accessible.
Another keynote here is that there is no need in getting texture field predictions,
as for light rays the only valuable information is the volume density.
This allows to only evaluate the corresponding part of the model.

Another key point here is the format of the dataset.
The NeRF and NSVF methods only work with color values,
which allows them to use the LDR images in the datasets
(i.e. PNG, JPG image formats and others).
However, the NRF approach implies light intensity values that are integrated into the final radiance calculations.
The LDR images are usually lacking the information of radiances
and only operate with color values.
All these facts make the LDR images bad candidates for the training dataset.
One should instead consider using HDR images (i.e. HDR, EXR formats).

The point light intensity value $I$ that takes part in the calculation of $L_l(p, l) = f_{att} \cdot I$
represents the radiation power of the light source.
This value is known when processing artificially generated scenes
and thus can directly be used as the value of $I$.
The real-world scenes (e.g. captured with the cellphone and flashlight)
on the contrary do not have the exact light intensity value.
One could estimate the power of the flashlight, but this would give only a rough estimation.
To make the method robust to this kind of estimations,
the light intensity value $I$ is passed to the optimizer (along with other model parameters $\Theta$).
This technique requires some robust estimation of $I$ as an initialization value as well as an increased learning rate.

\section{In-Voxel Light Approximation}
\label{sec:invoxel}

Using sparse octree voxels for increasing the efficiency of the method
makes the whole problem feasible within some commonly accessible hardware.
However, the training procedure still remained highly computationally expensive and memory demanding
and therefore very slow in training or still even intractable for high-resolution scenes.

The main load of this approach still remains in the additional sampling of light rays
and the consecutive model evaluation for calculating $\tau_l(p', l)$.
This fact leads to the task of minimizing the overhead that is caused by calculating light ray transmittances.

Predictions of volume densities at view ray sample points have primary importance
as they contribute to the final radiance value the most.
This assumption brings to the fact that light rays are not required
to be sampled as densely as view rays to remain the same level of accuracy.
Thus the lower number of points can be sampled on the light rays,
which would result in lower memory and time consumption.
However, this approach only alleviates the problem while not solving its root cause.

The octree structure that is used to encode feature vectors $\Tilde{g_i}$ in the corners of voxels
represents itself a coarse geometry of the scene.
Starting with only a few voxels that cover the whole bounding box around the scene,
the voxels undergo the \textit{self-pruning} and \textit{refinement} procedure (explained in \Cref{subsec:pruning}),
which increases the detailing capabilities of the octree
by increasing the number of voxels, lowering its sizes and rejecting empty voxels.
After only a few steps of the aforementioned operations, the overall voxel structure
repeats scene geometry with a considerable amount of accuracy.
These voxels that intersect with the light rays can be used as an estimation of volume densities.

% \begin{figure}
%     \begin{tabular}{ccc}
%           \includegraphics[width=0.1\textwidth]{figures/sampling_nerf.png}
%           & \includegraphics[width=0.1\textwidth]{figures/sampling_nsvf.png}
%           & \includegraphics[width=0.1\textwidth]{figures/sampling_nrf.png}
%           \\(a) NeRF \cite{mildenhall2020nerf} & (b) NSVF \cite{liu2021neural} & (c) NRF \cite{bi2020neural}
%           \\[6pt]\multicolumn{3}{1}{
%               \begin{tabular}{cc}
%                 \includegraphics[width=0.1\textwidth]{figures/sampling_bfex.png}
%                 & \includegraphics[width=0.1\textwidth]{figures/sampling_iva.png}
%                 \\[6pt](d) 'Brute-force' & (e) In-voxel approximation
%               \end{tabular}
%           }
%     \end{tabular}
%     \caption{caption}
% \end{figure}
\begin{figure}[!htbp]
    \setlength\tabcolsep{4pt}
    \begin{tabular}{ccc}
          \includegraphics[width=0.284\textwidth]{figures/sampling_nerf.png}
          & \includegraphics[width=0.261\textwidth]{figures/sampling_nsvf.png}
          & \includegraphics[width=0.35\textwidth]{figures/sampling_nrf.png}
          \\(a) NeRF \cite{mildenhall2020nerf} & (b) NSVF \cite{liu2021neural} & (c) NRF \cite{bi2020neural} (general)
          \\[6pt]\includegraphics[width=0.25\textwidth]{figures/sampling_nrf_colocated.png}
          & \includegraphics[width=0.315\textwidth]{figures/sampling_bfex.png}
          & \includegraphics[width=0.32\textwidth]{figures/sampling_iva.png}
          \\(d) NRF (colocated) & (e) 'Brute force'& (f) In-voxel approximation
    \end{tabular}
    \caption{
Sampling techniques used in different approaches.
The 2D sketch schemes are used instead of 3D visualizations for simplicity.
Blue lines represent view rays $p(z)$ with direction vector $v$,
purple lines stand for light rays $p'(z)$ with light direction vector $l$
(only two of those are shown for brevity).
Red dots represent sampled view points $p_i$, yellow dots stand for light sample points $p'_i$.
Scheme (a) shows the original NeRF sampling technique without \textit{hierarchy sampling} (\Cref{subsec:hierarchy_sampling}).
Scheme (b) correspond to the NSVF sampling technique
that only occurs inside intersected voxels (represented by green boxes).
Scheme (c) shows sampling of the general case of the NRF approach
when light source is placed at arbitrary position.
Scheme (d) displays the 'colocated setting'
when point light source is positioned at the same location as camera.
In this case light sample points (yellow dots) coincide with the view sample points (red dots).
Scheme (e) correspond to the proposed in this work 'brute-force' approach
when light rays sampling is reduced by using the same voxel structure as in NSVF (fig. (b)).
Scheme (f) shows sampling proposed in this work in-voxel approximation scheme.
View rays are sampled equally as in NSVF approach
while light ray transmittances are estimated using travelled distances $d_{V_i}$
of the light ray inside voxels.
}
\label{fig:samplings}
\end{figure}

There are plenty of techniques that can be applied here in order to calculate light transmittances more efficiently.
The main proposed technique implies the assumption
that the volume density is homogeneously distributed inside the voxel.
This assumption is certainly wrong for the general case
but should give a fair estimation for the light ray transmittances.
In this case, the traveled distance $d_{V_k}$ of the ray inside voxel $V_k$
can be used to weigh some value $\hat{\sigma_{V_k}}$ (\Cref{fig:samplings} f):
\begin{equation}
    \label{eq:light_ray_transmittance}
    \tau_l(p_i, l) = \exp \sum_{V_k \in \mathbb{V}^*} -\hat{\sigma}_{V_k} \cdot d_{V_k},
\end{equation}
where $\tau_l(p_i, l)$ is the light transmittance value at the view sample point $p_i$
and $\mathbb{V}^* \subset \mathbb{V}$ is the set of voxels $V_k$
that have been intersected by the light ray $p'(z') = p'_0 + z' \cdot l$.

The model can be periodically evaluated to predict the values of $\hat{\sigma}_{V_k}$ and consequently calculate $\tau_l$.
The period of evaluation can be similar to periods for \textit{self-pruning} and \textit{refinement} procedures
as some coarse value would already give an acceptable level of certainty.

Some other options can be applied here such as:
\begin{enumerate}
    \item calculate $\hat{\sigma}_{V_k}$ using points sampled in centers of voxels $V_k \in \mathbb{V}^*$
    \item calculate mean $\hat{\sigma}_{V_k}$ value from points of intersection of light ray with voxels $V_k \in \mathbb{V}^*$ (requires handling of edge cases)
    \item assign $\hat{\sigma}_{V_k} = \const, \forall V_k \in \mathbb{V}^*$
    \item use $\hat{\sigma}_{V_k}$ that was calculated during pruning stage
\end{enumerate}

\section{Implicit Neural Reflectance Field}
\label{sec:implicit_scheme}

The aforementioned methods that are proposed within this work
extend existing approaches in terms of efficiency for the problem of reproducing the scene under novel view and light conditions.
The novel light conditions are the essential requirement here
as it expects the method to not only account for color values
but also care about the reflectance of the surface.
Although the NRF approach is formulated in general form
when any differentiable appearance model can be used for final radiance evaluation,
the result is still limited by the chosen BRDF.

\begin{figure}[!htb]
    \centering
    \includegraphics[width=0.9\textwidth]{figures/implicit_scheme.png}
    \caption{Network architecture for the implicit scheme.
The structure of the network mostly remains the same as in original NSVF work.
Some additional parameters such as light ray direction $l$
and distance $d$ from view sample point $p$ to the point light
are provided in advance into texture predictor.
}
    \label{fig:implicit_scheme}
\end{figure}

To avoid
%expand
these limitations one can shift the appearance evaluation burden to the deep neural network,
which is able to get trained for very complex appearance representations.
This 'implicit scheme' would have a simpler neural network structure,
which is shown in \Cref{fig:implicit_scheme}.
The light interaction is performed here implicitly by providing the network
with the light direction $l$ as well as the distance from the sample point to the light source $d_i = \norm{p^l_k - p_i}$
as denoted in the figure.
Light ray direction $l$ is also positionally encoded with the same $L$ as view ray direction $v$ is.
Although the shadowing is not explicitly modeled,
the network is expected to learn this effect by itself.

The main downside of this scheme comes from its advantage.
Implicit representation significantly decreases the amount of control over the learned field.
In other words, bigger amount of parameters that are responsible for different effects on the scene
are hidden in the weights of the model and cannot be easily attained.

Another characteristic of this scheme is that it does not imply any kind of regularization.
This means that the network is not guaranteed to be able to extract the appearance of the scene.
This behavior can be clearly seen from the experiments of the \textit{ImNRF} on the colocated light dataset \Cref{subsec:experiments_coloc}.

% Another property of this technique will be the increased complexity of the learning function,
% which would expect the network structure with greater capacity.

% \im{\textit{Disadvantage: almost no control over implicit representation}}

% \section{Implicit Neural Reflectance Field}

% Existing BRDF models limits the type of scenes that can be captured
% and successfully reconstructed afterwards.
% To model object appearance one can employ neural network, which is able to get trained for a very complex appearance representations.
% In this work I \im{we?} call this technique as "Implicit scheme".

% Texture predictor not only is dependant on view direction,
% but also requires the light direction and distance to light source
% (for modelling the light attenuation effect).
% Figure \# shows the network structure for this approach.

% \begin{enumerate}
%     \item Light rays are not sampled (-> varying light transmittance either not considered or assumed to get learned by network\im{???})
%     \item |--> Tangential space for coordinates (and parametrization as half-vectors. not done)
% \end{enumerate}

% Since the appearance effects happen mostly in a local coordinate frame, the usage of the global direction vectors implies on the model to learn global to local coordinate system transformation. Although this is generally achievable, the overall complexity of the task can be too overwhelming for the model and some kind of correlations might affect the model. In order to increase the training performance this transformation can be done deterministically and the view and light directions in local coordinate frame are to be passed to the input of the model.

% The usage of the cartesian vectors is not very effective for reflectance representations. The half diff vectors (rusinkiewicz parametrization) can be used instead of positionally encoded l and v.
% \sm{or in combination with positional encoding}

% \section{In-Voxel Approximation}

% Using the octree allows to approximate light rays sampling inside voxels in order to increase the performance of the method.

% Instead of performing inverse CDF sampling as it is done for view rays, one can do the following:
% \begin{enumerate}
%     \item Bravely \sm{colloquial} assume the media homogenious inside voxels and boldly approximate it as constant
%     \item Under the same brave assumption perform some sampling inside the voxel once at N iterations (e.g. right after pruning has been performed)
%     \item The in-voxel sampling can be lighter than inverse CDF (e.g. just sample the center of voxel, or voxel corners + trilinear interpolation)
% \end{enumerate}

% NSVF proposed a good technique of using Sparse Voxel Trees in order to increase the rendering speed. However, the scene is still lacking the light interaction.

% One can achieve this by also passing light directions along with distance to the light source into the network, in order to make it distinguish different surface properties for different view and light directions.

% This is some test area for new mathematical helper macros to nicely visualize mathematical formulas.

% \section{Numbers}
% \begin{align}
%     \mathbb{C}
%     \qquad
%     \mathbb{R}
%     \qquad
%     \mathbb{Q}
%     \qquad
%     \mathbb{Z}
%     \qquad
%     \mathbb{N}
% \end{align}

% \section{Numbers with physical units}
% \begin{align}
%     \SI{1.23}{\meter\per\second}
% \end{align}
% \begin{align}
%     \si{\meter\per\second}
% \end{align}
% \begin{align}
%     \SI{1.23\pm0.45}{\meter\per\second}
% \end{align}
% \begin{align}
%     \SI{3e8}{\meter\per\second}
% \end{align}
% \begin{align}
%     \SI{32}{\giga\byte} = \SI{32e9}{\byte}
% \end{align}
% \begin{align}
%     \SI{32}{\gibi\byte} = \SI[exponent-base=2]{32e30}{\byte}
% \end{align}

% \section{Norm, Dot, Abs, Interval}
% \begin{align}
%     \pi = \const
% \end{align}
% \begin{align}
%     1 \in \interval{0}{2}
% \end{align}
% \begin{align}
%     1 \in \order{n}
% \end{align}
% \begin{align}
%     \evalat{ \frac{\partial f}{\partial x} }{ x = 0 }
% \end{align}
% \begin{align}
%     \norm{p} \qquad \norm{\frac{p}{2}}
% \end{align}
% \begin{align}
%     \abs{p} \qquad \abs{\frac{p}{2}}
% \end{align}
% \begin{align}
%     \dotproduct{p}{q} \qquad \dotproduct{\frac{p}{2}}{q}
% \end{align}
% \begin{align}
%     \crossproduct{p}{q} \qquad \crossproduct{\frac{p}{2}}{q}
% \end{align}

% \section{Vector, Matrix}
% \begin{align}
%     \vec{p} \qquad \vecarrow{p}
% \end{align}
% \begin{align}
%     \vec{p}^{\transposed}
% \end{align}
% \begin{align}
%     \gradient{\vec{p}}
% \end{align}
% \begin{align}
%     \divergence{\mat{A}}
% \end{align}
% \begin{align}
%     \laplacian{\mat{A}}
% \end{align}
% \begin{align}
%     \mat{A}
% \end{align}
% \begin{align}
%     \set{K} , K
%     \qquad
%     \set{N} , N
% \end{align}
% \begin{align}
%     \neighborhood{\vec{p}} = \left\{ \vec{q} \mid \norm{\vec{p} - \vec{q}} < \epsilon \right\}
% \end{align}

% \section{Set operations}
% \begin{align}
%     A \intersect B
% \end{align}
% \begin{align}
%     A \union B
% \end{align}
% \begin{align}
%     A \difference B
% \end{align}

% \section{Derivative, Integral, Sum, Probability}
% \begin{align}
%     \int_H x \, dx
% \end{align}
% \begin{align}
%     \sum_H x
% \end{align}
% \begin{align}
%     \probability{x}
% \end{align}
% \begin{align}
%     \probabilitygiven{x}{y}
% \end{align}
% \begin{align}
%     \expectation{x}
% \end{align}
% \begin{align}
%     \deviation{x}
% \end{align}
% \begin{align}
%     \variance{x}
% \end{align}

% \section{Lemma, Theorem, Corollary}
% \begin{lemma}
%     This is a lemma.
% \end{lemma}
% \begin{proof}
%     Proof of lemma.
% \end{proof}

% \begin{theorem}
%     This is a theorem.
% \end{theorem}
% \begin{proof}
%     Proof of theorem.
% \end{proof}

% \begin{corollary}
%     This is a corollary.
% \end{corollary}
% \begin{proof}
%     Proof of corollary.
% \end{proof}


\section{The \partialsum algorithm}
\Label{sec:partialsum}

The \partialsum algorithm in the \cxx Standard Library \cite[\S 29.8.6]{cxx-17-draft} computes
the sum of a given initial value and the elements in a range.
%
Our version of the original signature
reads:

\begin{lstlisting}[style=acsl-block]

  size_type
  partial_sum(const value_type* a, size_type n, value_type* b);
\end{lstlisting}

After executing the function \partialsum the array \inl{b[0..n-1]} holds the following values
\begin{align}
\Label{eq:partialsum}
   \mathtt{b}[i] &= \sum_{k = 0}^{\mathtt{i}} \mathtt{a}[k]
\end{align}
for $0 \leq i < n$.
%
Equations~\eqref{eq:partialsum} and~\eqref{eq:accumulate-default}
suggest that we define in the following listing the \acsl predicate \PartialSum
by using the logic function \logicref{AccumulateDefault}.

\input{Listings/PartialSum.acsl.tex}

\clearpage

\subsection{Formal specification of \partialsum}

The specification of \specref{partialsum} demands that the arrays
\inl{a[0..n-1]} and \inl{b[0..n-1]}
are separated, that is, they do not overlap.
Note that is a stricter requirement than in the case of the original
\cxx version of \partialsum, which allows that~\inl{a} equals~\inl{b},
thus allowing the computation of partial sums \emph{in place}.

\input{Listings/partial_sum.h.tex}

\clearpage

\subsection{Implementation of \partialsum}

The following listing shows an implementation of \partialsum with corresponding loop annotations.

\input{Listings/partial_sum.c.tex}

\clearpage


% !TEX root = index.tex

\section{How Curved are Curves?}
\epigraph{The real problem in speech is not precise language. The problem is clear language.}{Richard Feynmann}
\subsection{Curvature of Curves}
Our first goal is to define a quantitative measure of curvature. For example, we would like to determine what the curvature of the sine curve is at various points. For us a \textbf{curve} $ C$ is the image of a smooth function $ c(t): (a,b) \rightarrow \R^2$ or $ \R^3$. The function $ c(t)$ is called a \textbf{parametrization} of the curve $ C$.
\begin{example} $ $
	\label{example:parametrizations}
	\begin{enumerate}
		\item The circle $ x^2 + y^2 = r^2$ can be parametrized as \begin{align*}
			      c(t) = (r \cos (\omega t), r \sin(\omega t))
		      \end{align*}
		      where $ \omega$ is any non-zero constant.
		\item The sine curve can be parametrized as
		      \begin{align*}
			      c(t) = (t, \sin t)
		      \end{align*}
		\item More generally, the graph $ y=f(x)$ can be parametrized as
		      \begin{align*}
			      c(t) = (t,f(t))
		      \end{align*}
	\end{enumerate}
\end{example}

\begin{figure}[H]
	\centering
	\begin{subfigure}[t]{0.4\textwidth}
		\centering
		\includegraphics[height=6cm]{small_radius_circle}
	\end{subfigure}
	\begin{subfigure}[t]{0.59\textwidth}
		\centering
		\includegraphics[height=6cm]{large_radius_circle}
	\end{subfigure}
	\caption{The smaller the radius of the circle the faster the rate of change of the tangent vector.}
\end{figure}

Recall that $ c'(t)$ is the \textbf{tangent vector} to the curve $ C$ at the point $ c(t)$. If a curve has large curvature then the angular component of the tangent vector should change rapidly as we move along the curve, conversely if a curve has small curvature then the angular component should change slowly, which suggests the following definition.

\begin{definition}
	The \textbf{curvature} $ \kappa$ of a curve $ C$ parametrized by $ c(t)$ is defined to be the rate of change of the angular component of $ c'(t)$.
\end{definition}
Recall that every non-zero vector $ \vec{v}$ can be written as $ \vec{v} = \norm{\vec{v}} . u$, where $ u$ is the unit vector in the direction of $ v$. The length $ \norm{\vec{v}}$ is the \textbf{radial component} of $ v$ and $ u = \vec{v} / \norm{\vec{v}}$ is the \textbf{angular component} of $ v$. We can make the above definition more precise as follows.
\begin{definition}
	The \textbf{curvature} of a curve $ C$ parametrized by $ c(t)$ is defined to be
	\begin{align*}
		\kappa
		&:= \mbox{ rate of change of the angular component of } c'(t) \\
		&= \left \lVert \dfrac{d}{dt} \left( \dfrac{c'(t)}{\norm{c'(t)}}\right) \right \rVert \cdot \dfrac{1}{\norm{c'(t)}}
	\end{align*}
	assuming $c'(t) \neq 0$.
\end{definition}
We divide by $\norm{c'(t)}$ to normalize the rate at which we're moving along the curve.
\begin{remark}
	It's not too hard to find parametrizations with $c'(t) \neq 0$, at least in a small neighborhood of the point under consideration. For example, all the examples in Example \ref{example:parametrizations} satisfy this condition (check this).
\end{remark}

From this definition we immediately get a way to compute the curvature in the special case when $ \norm{c'(t)}=1$ for all $ t$. Such a parametrization is called a \textbf{unit speed parametrization}.
\begin{prop}
For a unit speed parametrization $c(t)$ the curvature is given by
\begin{align*}
	\kappa
	&= \left \lVert \dfrac{d}{dt} \left({c'(t)}\right) \right \rVert = \norm{c''(t)}
\end{align*}
\end{prop}
\begin{example}
	A circle of radius $ r$ can be parametrized as
	\begin{align*}
		c(t) = (r \cos (\omega t), r \sin(\omega t))
	\end{align*}
	for some constant $ \omega$. For this parametrization
	$$\norm{c'(t)} = \norm{ (-r \omega \sin (\omega t), r \omega \cos(\omega t))} = r \omega $$
	If we choose $ \omega = 1/r$ the parametrization becomes unit speed. In this case the curvature equals
	\begin{align*}
		\kappa
		 & = \norm{c''(t)}                            \\
		 & = \norm{(r \cos (t/r), r \sin(t/r))}       \\
		 & = \norm{(-1/r \cos (t/r), -1/r \sin(t/r))} \\
		 & = \dfrac{1}{r}
	\end{align*}
:D
\end{example}

\begin{thm}
	The circle of radius $ r$ has constant curvature of $ 1/r$.
\end{thm}
It is not easy to find unit speed parametrizations for more complicated curves (try it out). We need a formula which is valid even for non-unit speed parametrizations.

\begin{thm}
	\label{thm:curvature_formula}
	The \textbf{curvature} of a curve $ C$ parametrized by $ c(t)$ is given by
	\begin{align*}
		\kappa = \dfrac{\norm{c'(t) \times c''(t)}}{\norm{c'(t)}^3}
	\end{align*}
	assuming $c'(t) \neq 0$.
\end{thm}
\noindent The proof is in the following exercise.
\begin{ques}
	Let $ c(t)$ be a parametrization of $ C$ which is not necessarily unit speed. Let $ r(t) = \norm{c'(t)}$ be the radial component of the velocity, suppose that $ r(t)$ is never 0. Let $ \theta(t) = c'(t)/ \norm{c'(t)}$ be the angular component of velocity. Note that $r(t)$ is a scalar valued function and $\theta(t)$ is a vector valued function. The curvature is given by
	\begin{align*}
		\kappa = \dfrac{\norm{\theta'(t)}}{r(t)}
	\end{align*}
	Use the following steps to compute $ \kappa$.
	\begin{enumerate}
    \item Express $c'(t)$ in terms of $r(t)$ and $\theta(t)$.
    \item Show that $ \theta'(t) \cdot \theta'(t)$ is a constant and equals 1.
		\item Show that $ \theta'(t) \cdot \theta''(t) = 0$ and hence $\theta'(t) \perp \theta''(t)$ for all $t$.
		\item Conclude that $ \norm{\theta'(t) \times \theta''(t)} = \norm{\theta''(t)}$.
		\item Express $ c''(t)$ in terms of $ r(t)$, $ \theta(t)$, and their derivatives.
		\item Prove Theorem \ref{thm:curvature_formula}.
	\end{enumerate}
\end{ques}
\begin{ques}
	Use the parametrization $(r \cos (\omega(t)), r \sin(\omega(t)))$ to compute the curvature of a circle of radius $r$, where now $\omega$ is a function of $t$.
\end{ques}
\begin{ques}
	An easy computation shows that curvature of the graph $y=x^4$ is 0 at $x=0$. What does this mean geometrically?
\end{ques}
\begin{ques} $ $
	\begin{enumerate}
		\item Find the curvature of the graph $y=f(x)$ where $f:\R \rightarrow \R$ is any smooth function.
		\item Guess the relationship between the curvature of the graphs $y=f(x)$ and $y=kf(x/k)$, where $k$ is a positive real number. Prove your guess.
		\item Guess the relationship between the curvature of curves parametrized by $c(t)$ and $kc(t)$, where $k$ is a positive real number. Prove your guess.
	\end{enumerate}
\end{ques}
The formula for curvature in Theorem \ref{thm:curvature_formula} explicitly depends on the parametrization $ c(t)$ but the curvature itself does not. Have you seen such phenomenon before? Perhaps in your favorite branch of mathematics?
\begin{remark}[Generalized Curvature]
	\label{remark:signed_curvature}
	Often in calculus, not taking absolute value or norm gives us quantities which do not have explicit geometric significance but are easier to manipulate. For example, the integral itself has no geometric significance rather the area under a curve is the \emph{absolute value} of the integral, however, the Fundamental Theorem of Calculus does not contain any absolute values. Analogously, we'll call the vector quantity
	\begin{align*}
		\vec{\kappa} := \dfrac{d}{dt} \left( \dfrac{c'(t)}{\norm{c'(t)}}\right) \cdot \dfrac{1}{\norm{c'(t)}}
	\end{align*}
the \textbf{Generalized Curvature} for the lack of a better name. The following theorem is the analogue of Fundamental Theorem of Calculus for Curvature.
\end{remark}
\begin{ques}
	For a curve $C$ with a parametrization $c(t)$ compute the integral
	\begin{align*}
		\int \limits_{t_0}^{t_1} \vec \kappa \: ds
	\end{align*}
	(Hint: Think about what this integral is computing geometrically. It might help to first solve this problem for a unit speed parametrization).
\end{ques}

\subsection{Curvature and Taylor approximation}

Let us now consider curves which are graphs $y=f(x)$. For simplicity we'll assume that 0 is a critical point of $f(x)$ i.e. $f'(0) = 0$, and that $f''(0) \ge 0$. The following theorem is an easy exercise.
\begin{thm}
  If $0$ is a the \textbf{critical point} of $f: \R \rightarrow \R$ then the curvature of the graph $y=f(x)$ at $(0,f(0))$ equals ${f''(0)}$.
\end{thm}
\begin{ques}
	Prove this.
\end{ques}
\noindent Thus the degree 2 Taylor approximation of $ f(x) $ at the critical point $ x=0$ is given by
\begin{align}
	\label{eq:eq2}
	\begin{split}
	f(x) &\approx f(0) + f''(0) \dfrac{x^2}{2} \\
  &=f(0) + \kappa \dfrac{x^2}{2}
\end{split}
\end{align}
There is another familiar function which has the same Taylor approximation. The equation of a circle of radius $ r$ centered at the origin in $ \R^2$ is $$ x^2 + y^2 = r^2 $$
\emph{Near} the point $(0,-r)$ we can express the circle as the graph $ y = -\sqrt{r^2 - x^2}$.
\begin{ques}
	Show that the degree 2 Taylor approximation of $ -\sqrt{r^2 - x^2} $ at $ x=0$ is given by
	\begin{align}
		\label{eq:eq1}
		-\sqrt{r^2 - x^2} & \approx -r + \dfrac{x^2}{2r}
	\end{align}
\end{ques}
Notice that $ 1/r$ which is the coefficient of $ x^2/2$ is exactly the curvature of the circle. We can interpret Equations \eqref{eq:eq1} and \eqref{eq:eq2} as saying that,
\begin{prop}
	\label{prop:approximating_circle}
	 If the curvature of the curve $ y=f(x)$ is $ \kappa$ at a critical point $p$ then \textbf{the circle that best approximates the curve at $p$ has radius $ 1/\kappa$}.
\end{prop}
Since we can always rotate a curve without changing it's curvature, Proposition \ref{prop:approximating_circle} is true in much more generality and we can drop the condition that $p$ is a critical point. We know from calculus that the first derivative gives us the slope of the tangent line. Here we are saying that the second derivative gives us the \emph{tangent} circle\footnote{The technical term is an \emph{osculating} circle.}.
\begin{ques}
	Think about how you might generalize the above methods to surfaces.
\end{ques}
\begin{figure}[H]
	\centering
		\includegraphics[height=9cm]{parabola}
		\caption{A circle of radius $1/2$ best approximates the parabola $y=x^2$ at $x=0$.}
\end{figure}
\begin{figure}[H]
		\centering
		\includegraphics[height=9cm]{horizontal_parabola}
		\caption{Even if our curve is not a graph the same interpretation remains true.}
\end{figure}


\documentclass{article}

\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{amsmath,amssymb}
\usepackage{parskip}
\usepackage{graphicx}

% Margins
\usepackage[top=2.5cm, left=3cm, right=3cm, bottom=4.0cm]{geometry}
% Colour table cells
\usepackage[table]{xcolor}

% Get larger line spacing in table
\newcommand{\tablespace}{\\[1.25mm]}
\newcommand\Tstrut{\rule{0pt}{2.6ex}}         % = `top' strut
\newcommand\tstrut{\rule{0pt}{2.0ex}}         % = `top' strut
\newcommand\Bstrut{\rule[-0.9ex]{0pt}{0pt}}   % = `bottom' strut

%%%%%%%%%%%%%%%%%
%     Title     %
%%%%%%%%%%%%%%%%%
\title{Research Assistant Crypto Response}
\author{Haile Lagi}
\date{September 25, 2020}

\begin{document}
\maketitle

%%%%%%%%%%%%%%%%%
% Math problem   %
%%%%%%%%%%%%%%%%%
\section{Math}
 Over all real numbers, find the minimum value of a positive real number, $y$ such that
\begin{align}
    \label{eq:obj_fn} % Equation label; can be used for referencing
    y = \sqrt{(x+6)^2 + 25)} + \sqrt{(x-6)^2 + 121} \,.
\end{align}

To find a stationary point(in this case mininum) for \ref{eq:obj_fn} the procedure is as follows:
\begin{enumerate}
\item Given y = f(x) find f'(x)
\item Let $\frac{dy}{dx}=0$ and solve for the x value(s).
\item find the corresponding y value(s).
\item Determine the nature of the equation using:
\begin{itemize}
\item Second derivative and substitute x if x is positive, negative or zero
\end{itemize}
\begin{itemize}
\item Alternatively, find gradient before and after the stationary + to -, - to - or + to +
\end{itemize}
\end{enumerate}

First we simplify \ref{eq:obj_fn} by expanding the polynomials of the form
$(x + c)^n$ and we have

\begin{align}
    \label{eq: linear}
    y = \sqrt{(x+6)(x+6) + 25)} + \sqrt{(x-6)(x-6) + 121}
    = \sqrt{x^2+12x+61} + \sqrt{x^2 - 12x + 157} \,.
\end{align}

To find f'(x) using chain rule, in an equation of the form
$\frac{dy}{dx}=u^n$ where u is function of x and n is some exponent this holds
$\frac{dy}{dx}=\frac{dy}{du} * \frac{du}{dx}$. Divide equation \ref{eq: linear}
into two parts $y = y_{1} + y_{2}$ where

\begin{align}
    \label{eq: linear_one}
    y_{1} = \sqrt{x^2+12x+61}  \,.
\end{align}

\begin{align}
\label{eq: linear_two}
    y_{2} = \sqrt{x^2 - 12x + 157} \,.
\end{align}

Applying the chain rule in \ref{eq: linear_one} and \ref{eq: linear_two}
\begin{align}
    \label{eq: y_one}
    \frac{dy_{1}}{dx} = u^\frac{1}{2} \,.
\end{align}

where $u_{1} = x^2 + 12x + 61$ and $\frac{du_{1}}{dx} = 2x + 12$

\begin{align}
    \label{eq: y_two}
    \frac{dy_{2}}{dx} = u^\frac{1}{2} \,.
\end{align}

where $u_{2} = x^2 - 12x - 12x + 157$ and $\frac{du_{2}}{dx} = 2x - 12$

Combining \ref{eq: y_one} and \ref{eq: y_two} we have \ref{eq: diffed}

\begin{align}
    \label{eq: diffed}
\frac{dy}{dx} = \frac{2x + 12}{2 * \sqrt(x^2 + 12x + 61)} + \frac{2x - 12}{2 * \sqrt(x^2 - 12x + 157)} \,.
\end{align}

By simple factorization of 2 and Let $\frac{dy}{dx}=0$

\begin{align}
    \label{eq: irration}
 0 = \frac{x + 6}{(x^2 + 12x + 61)^\frac{1}{2}} + \frac{x - 6}{(x^2 - 12x + 157)^\frac{1}{2}} \,.
\end{align}

At this point it becomes impossible to solve for x because the values of x are complex(or imaginary) so, the root(s) of x are \textbf{unknown}.

\begin{align}
    \label{eq: solve_y}
 y = (x^2 + 12x + 61)^\frac{1}{2} + (x^2 - 12x + 157)^\frac{1}{2} \,.
\end{align}

The minimum value of a real x is assumed to be  0

Substituting $x = 0$ in \ref{eq: solve_y} we have the minimum value of y as:
\begin{align}
    \label{eq: final}
 y = \sqrt{61} + \sqrt{157} \,.
\end{align}

Finally we have $y_{min} = (0, \sqrt{61} + \sqrt{157})$

\end{document}


\subsection{Dynamic Time Warping}

\begin{frame}{Dynamic Time Warping}
    \begin{itemize}
        \item A widely used and robust distance measure for time series

        \item \textit{"Allowing similar shapes to match even if they are out of phase in the time axis"}
            \cite{keogh2002exact}
    \end{itemize}
\end{frame}

\begin{frame}{Dynamic Time Warping}{Calculation}
    \begin{block}{Given}
        \begin{itemize}
            \item A domain set $\mathbb{U}$

            \item A distance measure function $d$ with $d: \mathbb{U} \times \mathbb{U} \to \mathbb{R}$

        \end{itemize}
    \end{block}
    \begin{block}{Input}
        \begin{itemize}
            \item A time series $Q = (q_1, q_2, \dots, q_i, \dots, q_l)$ with length $l$ over the domain set
                $\mathbb{U}$

            \item A time series $C = (c_1, c_2, \dots, c_j, \dots, c_k)$ with length $k$ over the domain set
                $\mathbb{U}$
        \end{itemize}
    \end{block}
\end{frame}

\begin{frame}{Dynamic Time Warping}{Calculation}
    \begin{block}{Distance Calculation}
        \begin{itemize}
            \item Calculation of Matrix $M$ with size $l \times k$ based on \cite{sart2010accelerating}

            \item {\small $M_{i, j} = \begin{cases}
                d(q_i,c_j) & \text{if } i = 1 \wedge j = 1\\
                M_{i,j-1} + d(q_i,c_j) & \text{if } i = 1 \wedge j \neq 1\\
                M_{i-1,j} + d(q_i,c_j) & \text{if } i \neq 1 \wedge j = 1\\
                min(M_{i-1,j}, M_{i-1,j-1}, M_{i,j-1}) + d(q_i,c_j) & \text{if } i \neq 1 \wedge j \neq 1
            \end{cases}$ }

            \item $M_{l,k}$ is DTW distance between $Q$ and $C$
        \end{itemize}
    \end{block}
\end{frame}

\begin{frame}<handout:0>{Dynamic Time Warping}{Example}
    \begin{center}
        \resizebox {\textwidth} {!} {
            \begin{tabular}{cc}
                \resizebox* {!} {0.3\textwidth} {
                    \begin{tikzpicture}
                        \begin{axis}[
                            xmin=0,
                            xmax=47,
                            xlabel=time,
                            ylabel=acceleration,
                            width=\axisdefaultwidth,
                            height=0.7*\axisdefaultheight,
                            reverse legend,
                            legend pos=south east]
                            \addplot[red, thick, mark=none] table {../data/fig/dynamictimewarping/q.dat};
                            \addlegendentry{Q}
                            \addplot[blue, thick, mark=none] table {../data/fig/dynamictimewarping/c.dat};
                            \addlegendentry{C}
                        \end{axis}
                    \end{tikzpicture}
                } & \quad
                \resizebox* {!} {0.3\textwidth} {
                    \begin{tabular}[b]{ll}
                        \begin{turn}{90}
                            \begin{tikzpicture}
                                \begin{axis}[
                                    xmin=0,
                                    xmax=47,
                                    ymin=-100,
                                    ymax=0,
                                    hide x axis,
                                    hide y axis,
                                    width=\axisdefaultwidth,
                                    height=0.7*\axisdefaultheight]
                                    \addplot[red, ultra thick, mark=none] table {../data/fig/dynamictimewarping/q.dat};
                                \end{axis}
                            \end{tikzpicture}
                        \end{turn} \hspace*{3em} &
                        \begin{tikzpicture}
                            \begin{axis}[
                                enlargelimits=false,
                                ymin=0,
                                ymax=47,
                                hide x axis,
                                hide y axis,
                                width=\axisdefaultwidth,
                                height=\axisdefaultwidth,
                                colorbar,
                                colormap/viridis high res]
                                \addplot[matrix plot*,
                                    mesh/cols=48,
                                    point meta=explicit] table[meta=C] {../data/fig/dynamictimewarping/matrix.dat};
                            \end{axis}
                        \end{tikzpicture} \\
                        &
                        \\[1em]
                        &
                        \begin{tikzpicture}
                            \begin{axis}[
                                xmin=0,
                                xmax=47,
                                ymin=-100,
                                ymax=0,
                                hide x axis,
                                hide y axis,
                                width=\axisdefaultwidth,
                                height=0.7*\axisdefaultheight]
                                \addplot[blue, ultra thick, mark=none] table {../data/fig/dynamictimewarping/c.dat};
                            \end{axis}
                        \end{tikzpicture}
                    \end{tabular}
                }
            \end{tabular}
        }
    \end{center}
\end{frame}

\begin{frame}{Dynamic Time Warping}{Example}
    \begin{center}
        \resizebox {\textwidth} {!} {
            \begin{tabular}{cc}
                \resizebox* {!} {0.3\textwidth} {
                    \begin{tikzpicture}
                        \begin{axis}[
                            xmin=0,
                            xmax=47,
                            xlabel=time,
                            ylabel=acceleration,
                            width=\axisdefaultwidth,
                            height=0.7*\axisdefaultheight,
                            reverse legend,
                            legend pos=south east]
                            \addplot[gray, quiver={u=\thisrow{u}, v=\thisrow{v}}] table {../data/fig/dynamictimewarping/path.dat};
                            \addplot[red, thick, mark=none] table {../data/fig/dynamictimewarping/q.dat};
                            \addlegendentry{Q}
                            \addplot[blue, thick, mark=none] table {../data/fig/dynamictimewarping/c.dat};
                            \addlegendentry{C}
                        \end{axis}
                    \end{tikzpicture}
                } & \quad
                \resizebox* {!} {0.3\textwidth} {
                    \begin{tabular}[b]{ll}
                        \begin{turn}{90}
                            \begin{tikzpicture}
                                \begin{axis}[
                                    xmin=0,
                                    xmax=47,
                                    ymin=-100,
                                    ymax=0,
                                    hide x axis,
                                    hide y axis,
                                    width=\axisdefaultwidth,
                                    height=0.7*\axisdefaultheight]
                                    \addplot[red, ultra thick, mark=none] table {../data/fig/dynamictimewarping/q.dat};
                                \end{axis}
                            \end{tikzpicture}
                        \end{turn} \hspace*{3em} &
                        \begin{tikzpicture}
                            \begin{axis}[
                                enlargelimits=false,
                                ymin=0,
                                ymax=47,
                                hide x axis,
                                hide y axis,
                                width=\axisdefaultwidth,
                                height=\axisdefaultwidth,
                                colorbar,
                                colormap/viridis high res]
                                \addplot[matrix plot*,
                                    mesh/cols=48,
                                    point meta=explicit] table[meta=C] {../data/fig/dynamictimewarping/matrix.dat};
                                \addplot[white, ultra thick, mark=*, mark size=1] table {../data/fig/dynamictimewarping/matrix_path.dat};
                            \end{axis}
                        \end{tikzpicture} \\
                        &
                        \\[1em]
                        &
                        \begin{tikzpicture}
                            \begin{axis}[
                                xmin=0,
                                xmax=47,
                                ymin=-100,
                                ymax=0,
                                hide x axis,
                                hide y axis,
                                width=\axisdefaultwidth,
                                height=0.7*\axisdefaultheight]
                                \addplot[blue, ultra thick, mark=none] table {../data/fig/dynamictimewarping/c.dat};
                            \end{axis}
                        \end{tikzpicture}
                    \end{tabular}
                }
            \end{tabular}
        }
    \end{center}
\end{frame}

\input{background/dynamic_time_warping/sakoe-chiba_band.tex}
\input{background/dynamic_time_warping/time_series_normalization.tex}


%!TEX root = TTK4150-Summary.tex
\section{Nonlinear design tools}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Backstepping}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\paragraph{General idea}
Start by selecting a state $x_i$, where $i$ is some index. Given $\dot{x}_i = f_i(x)$, consider one of the other states present in $f_i$ as the input. Let's call this state $x_j$. Find an expression $x_j = \phi_j(x)$ that stabilizes $x_i$. (Using a Lyapunov function $V(x_i)$.) Then, define $z_j = x_j - \phi_j(x)$, and rewrite the system in terms of $x_i$ and $z_j$. Now, considering $\dot{z}_j$, use the same method to find an expression for a state present in $\dot{z}_j$ to stabilize $z_j$ and $x_i$. (Now with a Lyapunov function $V(x_i, z_j)$.) Keep going until you run out of states to stabilize.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Passivity-based control}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{align}
	\dot{x} &= f(x,u) \label{eq:pin-pout-x} \\
	y       &= h(x)   \label{eq:pin-pout-y}
\end{align}

\paragraph{Theorem 14.4}
If \eqref{eq:pin-pout-x}--\eqref{eq:pin-pout-y} is
\begin{itemize}
	\item passive with an RU, pos. def. storage function,
	\item zero-state observable
\end{itemize}
then $x = 0$ can be globally stabilized by $u = -\phi(y)$, with $\phi$ locally Lipschitz with $\phi(0) = 0$, $y\T \phi(y) > 0 \forall y \neq 0$.

TTK4150 Nonlinear control systems/sec-nonlinear-design-tools.tex


\subsection{Example: Slider-crank Mechanism}

\begin{frame}
	\begin{block}{Example 1: Slider-crank Mechanism}
		\begin{table}
			\begin{minipage}{0.5\linewidth}
				\begin{tabular}{l|l}
					      & $l_{AB}=l_1=0.5m$ \\
					Given & $l_{BC}=l_2=1m$ \\
					      & $\theta_1=60^\circ$ \\ \hline
					Find  & $x_C$ \\
				\end{tabular}
			\end{minipage}\hfill
			\begin{minipage}{0.5\linewidth}
				\includegraphics[width=50mm]{images/R-RRT.png}
			\end{minipage}
		\end{table}
	\end{block}
\emph{Solution 1}\vskip2.5mm
Position of joint $B$: $\displaystyle \vb{r}{B} = x_B\ih + y_B\jh = l_1\cos{\theta_1}\ih + l_1\sin{\theta_1}\jh$\\
Position of joint $C$: $\displaystyle \vb{r}{C} = x_C\ih$
\[
\Rightarrow(x_B-x_C)^2+y_B^2=l_1^2
\]
Solving the system of equations yields $x_{C_1}$ and $x_{C_2}$. Notice that in the mechanism, $x_C>x_B$ is the condition to obtain correct solution.
\end{frame}

\begin{frame}
\emph{Solution 2}\vskip2.5mm
Let $\vb{r}{B}=l_1\cos{\theta_1}\ih+l_1\sin{\theta_1}\jh$\vskip1.5mm
We can determine position of C by using direct solution:\vskip1.5mm
$\displaystyle\vb{r}{C}=\left(l_1\cos{\theta_1}+l_2\left(\arcsin{\frac{l_1\sin{\theta_1}}{l_2}}\right)\right)\ih$\vskip7.5mm
\emph{MATLAB R2019a code}\\
\lstinputlisting[style=Matlab-editor, basicstyle=\mlttfamily]{codes/RRRT-position2.m}
\end{frame}

\begin{frame}{MATLAB R2019a code}
	\lstinputlisting[style=Matlab-editor, basicstyle=\mlttfamily]{codes/RRRT-position1.m}
\end{frame}
\begin{frame}{Plotting using MATLAB R2019a}
	\lstinputlisting[style=Matlab-editor, basicstyle=\mlttfamily]{codes/RRRT-plot.m}
\end{frame}
\begin{frame}{Output figure}
\centering
\includegraphics[width=100mm]{images/RRRT-plot.png}
\end{frame}
\begin{frame}{Trajectory plotting using MATLAB R2019a}
\lstinputlisting[style=Matlab-editor, basicstyle=\mlttfamily]{codes/RRRT-trajectory.m}
\end{frame}
\begin{frame}{Output figure}
\centering
\includegraphics[width=100mm]{images/RRRT-trajectory.png}
\end{frame}


\chapter{Assignment-Complex Numbers}
\section{MCQ}
\begin{enumerate}
	\item The amplitude of $\frac{1+i \sqrt{3}}{\sqrt{3}+i}$ is
 \begin{tasks}(2)
	\task[\textbf{a.}]$\frac{\pi}{3}$
	\task[\textbf{b.}]$-\frac{\pi}{3}$
	\task[\textbf{c.}]$\frac{\pi}{6}$
	\task[\textbf{d.}]  $-\frac{\pi}{6}$
\end{tasks}
	\item If $\frac{1-i x}{1+i x}=a+i b$, then $a^{2}+b^{2}$ is
	 \begin{tasks}(2)
		\task[\textbf{a.}]1
		\task[\textbf{b.}]$-1$
		\task[\textbf{c.}] 0
		\task[\textbf{d.}] none of these
	\end{tasks}
	\item If $z=1-\cos \theta+i \sin \theta$, then $|z|$ equals
	 \begin{tasks}(2)
		\task[\textbf{a.}]$2 \sin \frac{\theta}{2}$
		\task[\textbf{b.}]$2 \cos \frac{\theta}{2}$
		\task[\textbf{c.}]$2\left|\sin \frac{\theta}{2}\right|$
		\task[\textbf{d.}] $2\left|\cos \frac{\theta}{2}\right|$
	\end{tasks}
	\item If $z=\frac{1}{(2+3 i)^{2}}$, then $|z|$ equals
	 \begin{tasks}(2)
		\task[\textbf{a.}]$\frac{1}{13}$
		\task[\textbf{b.}]$\frac{1}{15}$
		\task[\textbf{c.}]$\frac{1}{12}$
		\task[\textbf{d.}] none of these
	\end{tasks}
	\item If $x_{n}=\cos \left(\frac{\pi}{2^{n}}\right)+i \sin \left(\frac{\pi}{2^{n}}\right)$
	Then the value of $x_{1} \cdot x_{2} \cdot x_{3} \ldots$, up to infinity is
	 \begin{tasks}(2)
		\task[\textbf{a.}] $-2$
		\task[\textbf{b.}] $+2$
		\task[\textbf{c.}] $-1$
		\task[\textbf{d.}] $+1$
	\end{tasks}
	\item If $\left|z-\frac{4}{z}\right|=2$, then the maximum value of $|z|$ is
	 \begin{tasks}(2)
		\task[\textbf{a.}]$\sqrt{5}$
		\task[\textbf{b.}]$\sqrt{5}+1$
		\task[\textbf{c.}]$\sqrt{5}-1$
		\task[\textbf{d.}] none of these
	\end{tasks}
	\item The value of $1+\sum_{k=0}^{14}\left\{\cos \frac{(2 k+1) \pi}{15}+i \sin \frac{(2 k+1) \pi}{15}\right\}$ is
	 \begin{tasks}(2)
		\task[\textbf{a.}]0
		\task[\textbf{b.}] $-1$
		\task[\textbf{c.}]1
		\task[\textbf{d.}] $i$
	\end{tasks}
	\item The point representing complex number for which $|z+4|^{2}-|z-4|^{2}=8$ lie on
	 \begin{tasks}(2)
		\task[\textbf{a.}]a straight line parallel to $x$-axis
		\task[\textbf{b.}]a straight line parallel to $y$-axis
		\task[\textbf{c.}]a circle with centre as origin
		\task[\textbf{d.}] a circle with centre other than origin
	\end{tasks}
	\item If $\left|\begin{array}{ccc}6 i & -3 i & 1 \\ 4 & 3 i & -1 \\ 20 & 3 & i\end{array}\right|=x+i y$, then
	 \begin{tasks}(2)
		\task[\textbf{a.}] $x=3, y=1$
		\task[\textbf{b.}] $x=1, y=3$
		\task[\textbf{c.}]$x=0, y=3$
		\task[\textbf{d.}]  $x=0, y=0$
	\end{tasks}
	\item If the number $\frac{z-1}{z+1}$ is purely imaginary, then
	 \begin{tasks}(2)
		\task[\textbf{a.}]$|z|=1$
		\task[\textbf{b.}]$|z|>1$
		\task[\textbf{c.}]$|z|<1$
		\task[\textbf{d.}] $|z|>2$
	\end{tasks}
	\item The value of integral $I=\int_{0}^{\pi} \frac{2 d \theta}{R-\cos \theta}$ is given by where $R$ is real constant.
	\begin{tasks}(2)
		\task[\textbf{a.}]$\frac{-1}{2 \sqrt{R^{2}-1}}$
		\task[\textbf{b.}]$\frac{2 \pi}{\sqrt{R^{2}-1}}$
		\task[\textbf{c.}]$\frac{\pi}{\sqrt{1-R^{2}}}$
		\task[\textbf{d.}] $\frac{\pi}{\sqrt{R^{2}-1}}$
	\end{tasks}
	\item The value of integral $\int_{-\infty}^{+\infty} \frac{d x}{\left(1+x^{2}\right)^{2}}$ is given by
	\begin{tasks}(2)
		\task[\textbf{a.}]$\frac{\pi}{2}$
		\task[\textbf{b.}]$\pi$
		\task[\textbf{c.}] $i \frac{\pi}{2}$
		\task[\textbf{d.}]  $\frac{1}{4 i}$
	\end{tasks}
	\item The value of $\oint_{C} \frac{\sin 3 z}{z^{2}} d z$
	$c:|z|=\pi$ is given by
	\begin{tasks}(2)
		\task[\textbf{a.}]$6 \pi i$
		\task[\textbf{b.}]$-6 \pi i$
		\task[\textbf{c.}]0
		\task[\textbf{d.}]  3
	\end{tasks}
	\item The value of integral $\int_{0}^{2 \pi} \frac{\cos ^{2} \theta d \theta}{5-4 \cos \theta}$
	\begin{tasks}(2)
		\task[\textbf{a.}]$\frac{\pi}{12}$
		\task[\textbf{b.}]$-\frac{5 \pi}{12}$
		\task[\textbf{c.}]$\frac{5 \pi}{12}$
		\task[\textbf{d.}] $\frac{-5}{12}$
	\end{tasks}
	\item $\int_{0}^{2 \pi} \frac{\cos \theta}{13-12 \cos 2 \theta} d \theta$
	\begin{tasks}(2)
		\task[\textbf{a.}]0
		\task[\textbf{b.}]$\frac{+1}{10} \sqrt{\frac{2}{3}}$
		\task[\textbf{c.}] $\frac{-1}{10} \sqrt{\frac{2}{3}}$
		\task[\textbf{d.}]  $\frac{i}{10} \sqrt{\frac{2}{3}}$
	\end{tasks}
	\item Consider a complex function $f(z)=\frac{1}{z\left(z+\frac{1}{2}\right) \cos (z \pi)}$. Which one of the following
	statements is correct?
	\begin{tasks}(2)
		\task[\textbf{a.}] $f(z)$ has simple poles at $z=0$ and $z=-\frac{1}{2}$
		\task[\textbf{b.}]$f(z)$ has second order pole at $z=-\frac{1}{2}$
		\task[\textbf{c.}]$f(z)$ has infinite number of second order poles
		\task[\textbf{d.}] $f(z)$ has all simple poles
	\end{tasks}
	\item The value of integral
	$$
	I=\oint_{c} \frac{\sin z}{2 z-\pi} d z
	$$
	with $c$ a circle $|z|=2$, is
	\begin{tasks}(2)
		\task[\textbf{a.}]0
		\task[\textbf{b.}]$2 \pi i$
		\task[\textbf{c.}]$\pi i$
		\task[\textbf{d.}] $-\pi i$
	\end{tasks}
	\item The value of integral $I=\int_{0}^{x} \frac{(\ln x)^{2}}{x^{2}+1} d x$ is given by
	\begin{tasks}(2)
		\task[\textbf{a.}]$\frac{\pi^{3}}{2}$
		\task[\textbf{b.}]$\frac{\pi^{3}}{4}$
		\task[\textbf{c.}]$\frac{\pi^{3}}{8}$
		\task[\textbf{d.}]  $\frac{\pi^{3}}{14}$
	\end{tasks}
	\item $\int_{c} \frac{d z}{z^{2} \sinh z}$ where contour is defined as $|z-1|=2$. The value of integral is
	\begin{tasks}(2)
		\task[\textbf{a.}]$\frac{\pi i}{3}$
		\task[\textbf{b.}]$\frac{-\pi i}{3}$
		\task[\textbf{c.}]$\frac{\pi i}{6}$
		\task[\textbf{d.}] $\frac{-\pi i}{6}$
	\end{tasks}
	\item The value of the integral $\int_{0}^{\infty} \frac{\ln x^{2}}{\left(x^{2}+1\right)^{2}} d x$ is
	\begin{tasks}(2)
		\task[\textbf{a.}]0
		\task[\textbf{b.}] $\frac{-\pi}{4}$
		\task[\textbf{c.}]$\frac{-\pi}{2}$
		\task[\textbf{d.}] $\frac{\pi}{2}$
	\end{tasks}
	\item The value of the integral $\int_{C} \frac{z^{3} d z}{-2 z^{2}+10 z-12}$, where $C$ is a closed contour defined by the cquation $2|=|-5=0$, traversed in the anti-clockwise direction, is
	\begin{tasks}(2)
		\task[\textbf{a.}]$-16 \pi i$
		\task[\textbf{b.}]$16 \pi i$
		\task[\textbf{c.}]$8 \pi i$
		\task[\textbf{d.}] $2 \pi i$
	\end{tasks}
	\item The value of $\oint \frac{\cos \pi z}{z^{2}-1} d z$ around a rectangle with vertices at $2 \pm i,-2 \pm i$ is
	\begin{tasks}(2)
		\task[\textbf{a.}]$\pi$
		\task[\textbf{b.}] $-\pi$
		\task[\textbf{c.}]$2 \pi$
		\task[\textbf{d.}] 0
	\end{tasks}
	\item The principal value of the integral $\int_{-\infty}^{\infty} \frac{\sin (2 x)}{x^{3}} d x$ is
	\begin{tasks}(2)
		\task[\textbf{a.}]$-2 \pi$
		\task[\textbf{b.}]$-\pi$
		\task[\textbf{c.}] $\pi$
		\task[\textbf{d.}] $2 \pi$
	\end{tasks}
	\item The value of $\int_{0}^{2 \pi} \frac{d \theta}{5+4 \cos \theta}$ is:
	\begin{tasks}(2)
		\task[\textbf{a.}]$\frac{2 \pi}{3}$
		\task[\textbf{b.}]$\frac{\pi}{3}$
		\task[\textbf{c.}]$-\frac{\pi}{3}$
		\task[\textbf{d.}] 0
	\end{tasks}
	\item The value of integral $\int_{-\infty}^{+\infty} \frac{x}{\left(x^{2}+1\right)\left(x^{2}+4\right)} d x$ is
	\begin{tasks}(2)
		\task[\textbf{a.}]0
		\task[\textbf{b.}]1
		\task[\textbf{c.}]2
		\task[\textbf{d.}] 3
	\end{tasks}
	\item The value of integral $\int_{0}^{2 \pi} \frac{d \theta}{13-5 \sin \theta}$ is
	\begin{tasks}(2)
		\task[\textbf{a.}]$\frac{\pi}{2}$
		\task[\textbf{b.}]$\frac{\pi}{3}$
		\task[\textbf{c.}]$\frac{\pi}{4}$
		\task[\textbf{d.}] $\frac{\pi}{6}$
	\end{tasks}
	\item The value of $\int_{0}^{\infty} \frac{\ln x}{\left(x^{2}+4\right)^{2}} d x$ is
	\begin{tasks}(2)
		\task[\textbf{a.}]$\frac{\pi}{32}(\ln 2+1)$
		\task[\textbf{b.}] $\frac{\pi}{32}(\ln 2-1)$
		\task[\textbf{c.}]$\frac{\pi}{32} \ln 2$
		\task[\textbf{d.}]  $\frac{\pi}{32}$
	\end{tasks}
	\item If $0<p<1$, then the value of integral $\int_{0}^{\infty} \frac{x^{p-1}}{1+x} d x$ is
	\begin{tasks}(2)
		\task[\textbf{a.}]$\frac{\pi}{2 \sin p \pi}$
		\task[\textbf{b.}] $\frac{1}{\sin \frac{p}{2} \pi}$
		\task[\textbf{c.}]$\frac{\pi}{\sin p \pi}$
		\task[\textbf{d.}]  $\frac{\pi}{\sin \frac{p}{2} \pi}$
	\end{tasks}
	\item If $C$ is the circle given by $|z|=\pi$, and the integral is evaluated counterclockwise then the value of integral $\oint_{c} \frac{z \cosh z \pi}{z^{4}+13 z^{2}+36} d z$ is
	\begin{tasks}(2)
		\task[\textbf{a.}] $\frac{2 \pi i}{5}$
		\task[\textbf{b.}]$\frac{3 \pi i}{5}$
		\task[\textbf{c.}]$\frac{4 \pi i}{5}$
		\task[\textbf{d.}]  $\frac{6 \pi i}{5}$
	\end{tasks}
	\section{NAT}
	\item The least positive integer $n$ for which $\left(\frac{1+i}{1-i}\right)^{n}$ is real is...
	\item The value of $i^{n}+i^{n+1}+i^{n+2}+i^{n+3}$ is
	\item The value of $\frac{(\cos 3 \theta+i \sin 3 \theta)^{4}(\cos 4 \theta-i \sin 4 \theta)^{5}}{(\cos 4 \theta+i \sin 4 \theta)^{3}(\cos 5 \theta+i \sin 5 \theta)^{-4}}$ is
	\item For $n=6 k, k \in z$,\\
	$\left(\frac{1-i \sqrt{3}}{2}\right)^{n}+\left(\frac{-1-i \sqrt{3}}{2}\right)^{n}$ has the value...
	\item If $z=\frac{\sqrt{3}+i}{2}$, then $z^{69}$ equals -----------
	\section{MSQ}
	\item If $z=4+2 i$, then
	 \begin{tasks}(2)
		\task[\textbf{a.}] The magnitude of $z$ is $2 \sqrt{5}$ units
		\task[\textbf{b.}]The magnitude of $z$ is $3 \sqrt{5}$ units
		\task[\textbf{c.}] The argument or amplitude of $z$ is $\tan ^{-1} \frac{1}{3}$
		\task[\textbf{d.}]  The argument or amplitude of $z$ is $\tan ^{-1} \frac{1}{2}$
	\end{tasks}
	\item Consider a complex number $z=1-\sqrt{3} i$
	 \begin{tasks}(2)
		\task[\textbf{a.}]The magnitude of $z$ is 2 units
		\task[\textbf{b.}]The magnitude of $z$ is 4 units
		\task[\textbf{c.}]The complex number z can be represented by a point $(1,-\sqrt{3})$ in the complex or Argand plane
		\task[\textbf{d.}] The complex number z can be represented by a point $(-\sqrt{3},-1)$ in the complex plane.
	\end{tasks}
	\item If $z_{1}=2+3 i$ and $z_{2}=1+2 i$ then
	 \begin{tasks}(2)
		\task[\textbf{a.}] $z_{1}+z_{2}$ is equal to $4+6 i$
		\task[\textbf{b.}]$z_{1}-z_{2}$ is equal to $1+i$
		\task[\textbf{c.}]$z_{1} \cdot z_{2}$ is equal to $-4+7 i$
		\task[\textbf{d.}]  $\frac{z_{1}}{z_{2}}$ is equal to $\frac{8}{5}-\frac{1}{5} i$
	\end{tasks}
	\item Pick out the correct statement (S)
	 \begin{tasks}(2)
		\task[\textbf{a.}]Addition of two complex number is commutative
		\task[\textbf{b.}]For any three complex numbers $z_{1}, z_{2}$ and $z_{3}$ we have $\left(z_{1}+z_{2}\right)+z_{3}=z_{1}+\left(z_{2}+z_{3}\right)$
		\task[\textbf{c.}]For any two complex numbers $z_{1}$ and $z_{2}$ we have $z_{1} \cdot z_{2}=z_{2} \cdot z_{1}$
		\task[\textbf{d.}] Multiplication of two complex number are not commutative
	\end{tasks}
	\item Pick out the correct statement(s)
	 \begin{tasks}(2)
		\task[\textbf{a.}] Let $z$ and $\bar{z}$ denote a complex number and its conjugate respectively then, $z=\bar{z}$ implies $z$ is purely imaginary
		\task[\textbf{b.}]Let $z$ and $\bar{z}$ denote a complex number and its conjugate respectively then, $z+\bar{z}=0$ implies $z$ is purely imaginary
		\task[\textbf{c.}]In polar form the complex number $z=1+i$ can be written as $\sqrt{2}\left(\cos \frac{\pi}{4}+i \sin \frac{\pi}{4}\right)$
		\task[\textbf{d.}]  In polar form the complex number $1+i$ can be written as $\sqrt{2}\left(\cos \frac{\pi}{3}+i \sin \frac{\pi}{3}\right)$
	\end{tasks}

\end{enumerate}

CSIR- Mathematical Physics/chapter/Assignments/Assignment-Complex Numbers.tex


\section{Evaluation}
\label{sec:evaluation}

All the results presented in this section are obtained with the following parameters:
\begin{itemize}
    \item $n_D = 50$
    \item $n_{max} = 250$
    \item $N = 200$
\end{itemize}

\subsection{MinOver}
\begin{figure}[t]
	\centering
	\includegraphics[width=\columnwidth]{figures/base}
    \caption{Generalization error of a perceptron trained with the MinOver algorithm on a linearly separable dataset.}
	\label{fig:base}
\end{figure}

\cref{fig:base} shows the generalization error of the perceptron trained with the MinOver algorithm on a linearly separable dataset (with no noise, $\lambda = 0$).
Since the number of dimensions of the dataset $N$ is fixed, $\alpha = P / N$ is proportional to the number of examples $P$ used for the training.
The generalization error decreases for higher values of $\alpha$, i.e. when the number of examples increases.
In other words, it seems to be the case:
$$\lim_{\alpha \to \infty} \bm{\mathsf{w}} = \bm{\mathsf{w}}^{*}$$

The examples are distributed in all the space and the boundary between the classes is fixed and determined by the teacher vector $\bm{\mathsf{w}}^{*}$.
Intuitively, for a higher number of examples $P$, the maximum margin between the $2$ classes decreases, since it is more likely that some example is really close to the boundary hyperplane determined by $\bm{\mathsf{w}}^{*}$.
Since the MinOver algorithm tries to find some separation hyperplane, it is easier to get closer to the teacher vector $\bm{\mathsf{w}}^{*}$ if the maximum possible margin is smaller.

\subsection{MinOver vs Rosenblatt}
\begin{figure}[t]
	\centering
	\includegraphics[width=\columnwidth]{figures/comparison}
    \caption{Generalization error for the MinOver and the Rosenblatt algorithms on a linearly separable dataset.}
	\label{fig:comparison}
\end{figure}

\cref{fig:comparison} shows the generalization error of the perceptron trained with the MinOver and the Rosenblatt algorithms on a linearly separable dataset (without noise, $\lambda = 0$).
For both algorithms the generalization error decreases with higher numbers of examples for the training (see the previous section).
In absence of noise, the performances of the $2$ algorithms are quite close, with MinOver performing slightly better than Rosenblatt for low values of $\alpha$.

\subsection{Noise}
\begin{figure}[t]
	\centering
	\includegraphics[width=\columnwidth]{figures/noise}
    \caption{Generalization error for the MinOver and the Rosenblatt algorithms on a noisy dataset. The dataset is generated starting from a linearly separable set of examples and swapping the labels of each example with probability $\lambda$. The lines for $\lambda = 0$ (no noise) are left as a reference.}
	\label{fig:noise}
\end{figure}

\cref{fig:noise} shows the generalization error of the perceptron trained with the MinOver and the Rosenblatt algorithms on a noisy dataset.
For both algorithms, the generalization error increases with the noise $\lambda$.

The error for the MinOver algorithm initially decreases by adding new examples, while it starts to increase again if the dataset gets bigger:
the algorithm diverges from the teacher vector $\bm{\mathsf{w}}^{*}$.
The more noise is present, the sooner the algorithm starts to diverge.
At each step, the algorithm selects the example with the lower stability $\kappa^\mu \propto \bm{\mathsf{w}} \cdotp \xi^\mu S^\mu_R$:
this quantity is positive for correctly classified examples and negative for wrongly classified ones.
If there are misclassified examples, the MinOver algorithm chooses one of them to update the weights vector $\bm{\mathsf{w}}$.
If the dataset is non linearly separable, the MinOver algorithm stacks forever:
each update can correct the classification for some point, but not for all of them;
in the following epochs, only the misclassified examples cause updates and create new wrongly classified point.
For the way it is constructed, the probability that the dataset is not linearly separable is non-zero (for $\lambda > 0$) and increases with $\alpha$.
For $\lambda = 0.3$, the algorithms has a generalization error of around $0.45$, very close to the error for a random guess.
The MinOver algorithm does not seem to be suitable for the classification of noisy data.

The Rosenblatt algorithm has similar performances, but it does not diverge when $\alpha$ increases.
In other words, the Rosenblatt algorithm is up to a certain degree able to generalize even for noisy training data.


\documentclass{report}

\usepackage{amsmath}
\usepackage{graphicx}

\begin{document}
\chapter*{Oblique Spherical Triangles Toolbox}
\section*{Introduction}

Spherical trigonometry is generally much more involved than its
plain counterpart. A nice example of this is the seemingly simple
spherical triangle (a triangle on a sphere). The usual laws for
plain triangles no longer hold: the sum of the angles can easily
exceed 180$^\circ$, the Pythagorean theorem does not apply when
one of the angles is 90$^\circ$, and so on.

The formulas to find quantities in spherical triangles are quite
tedious. Their derivation is also not very trivial, which makes
them also harder to remember. For that reason, this little toolbox
has been created.

\begin{figure}[h!]
         \centering
         \includegraphics[bb=0 0 205 148]{OblSphTri.png}
         \caption{An arbitrary oblique spherical triangle.}
         \label{fig}
\end{figure}

A general spherical triangle is shown in Figure \ref{fig}. Note
that there are two angles associated with every ``corner'' of the
triangle. Also keep in mind that each ``side'' of a spherical
triangle is not a length, but an angle (with respect to the center
of the sphere). Then, if 3 arbitrary quantities (angles, sides)
are given and the other 3 quantities are to be found, 6 different
sub-problems can be identified. These can be defined as
follows:

\begin{table}[h!]
  \centering
  \caption{The 6 categories of oblique spherical triangles.
  Adapted from Table~A-1 in~\cite{Wertz2001}}
  \label{table}
  \begin{tabular}{|c||c|c|c|}\hline
         \textbf{Type}   & \textbf{Given} & \textbf{Find} &
         \textbf{No. of Solutions}\\\hline
         Side-Side-Side  & a, b, c & A, B, C & 0 or 2\\
         Side-Side-Angle & a, b, A & B, C, c & 0 or 2\\
         Side-Angle-Side & a, C, b & A, B, c & 2\\
         Angle-Angle-Side & A, B, a & b, c, C & 0 or 2\\
         Angle-Side-Angle & A, c, B & a, C, b & 2\\
         Angle-Angle-Angle & A, B, C & a, b, c & 0 or 2\\\hline
  \end{tabular}
\end{table}

All these problems involve rather long equations which will not be
repeated here. The interested reader is referred to Appendix A of
\cite{Wertz2001}, or similar standard works on spherical
trigonometry.

\cite{Wertz2001} uses a somewhat unconventional method to automate
solving these problems, namely the \texttt{acos2}-function. Indeed
it is the four-quadrant arccosine function, which is defined as

\[
        \text{acos2}(x) = H(x)\cdot \text{acos}(x),
\]

where $H(x) = +1$ if $x < 180^\circ$, and $H(x) = -1$ if $x >
180^\circ$. Defined like this, the \texttt{acos2} function
completely does away with the need of user intervention when
solving either of the 6 sub-problems, thus enabling full
automation.

\section*{The Toolbox}
This little toolbox is simply an implementation of all of the
above sub-problems. All functions are fully vectorized and should
work for vector or matrix input. The main components are:

\subsubsection{\texttt{sss.m} and \texttt{sssd.m}}
Usage:

\[
         \texttt{[A1, B1, C1, A2, B2, C2] = sss(a, b, c)}
\]

Returns both solutions to the Side-Side-Side problem. If no
solution exists, \texttt{NaN} is returned. Uses the \texttt{acos2}
function. \texttt{sss.m} uses radians, whereas \texttt{sssd.m}
uses degrees.

\subsubsection{\texttt{ssa.m} and \texttt{ssad.m}}
Usage:

\[
         \texttt{[B1, C1, c1, B2, C2, c2] = ssa(a, b, A)}
\]

Returns both solutions to the Side-Side-Angle problem. If no
solution exists, \texttt{NaN} is returned. Uses the
implementations of the Middle Angle Law (\texttt{mal.m}) and
Middle Side Law (\texttt{msl.m}). \texttt{ssa.m} uses radians,
whereas \texttt{ssad.m} uses degrees.

\subsubsection{\texttt{sas.m} and \texttt{sasd.m}}
Usage:

\[
         \texttt{[c1, A1, B1, c2, A2, B2] = sas(a, C, b)}
\]

Returns both solutions to the Side-Angle-Side problem. Uses the
\texttt{acos2} function. \texttt{sas.m} uses radians, whereas
\texttt{sasd.m} uses degrees.

\subsubsection{\texttt{aas.m} and \texttt{aasd.m}}
Usage:

\[
         \texttt{[b1, c1, C1, b2, c2, C2] = aas(A, B, a)}
\]

Returns both solutions to the Angle-Angle-Side problem. If no
solution exists, \texttt{NaN} is returned. Uses the
implementations of the Middle Angle Law (\texttt{mal.m}) and
Middle Side Law (\texttt{msl.m}). \texttt{aas.m} uses radians,
whereas \texttt{aasd.m} uses degrees.

\subsubsection{\texttt{asa.m} and \texttt{asad.m}}
Usage:

\[
         \texttt{[C1, a1, b1, C2, a2, b2] = asa(A, B, c)}
\]

Returns both solutions to the Angle-Side-Angle problem. Uses the
\texttt{acos2} function. \texttt{asa.m} uses radians, whereas
\texttt{asad.m} uses degrees.

\subsubsection{\texttt{aaa.m} and \texttt{aaad.m}}
Usage:

\[
         \texttt{[a1, b1, c1, a2, b2, c2] = aaa(A, B, C)}
\]

Returns both solutions to the Angle-Angle-Angle problem. If no
solution exists, \texttt{NaN} is returned. Uses the \texttt{acos2}
function. \texttt{aaa.m} uses radians, whereas \texttt{aaad.m}
uses degrees.

\section*{Auxiliary Functions}
These are included in the auxiliary folder. They are:

\subsubsection{\texttt{mal.m} and \texttt{mald.m}}
The implementation of the Middle-Angle Law for spherical
triangles:

\begin{eqnarray*}
\sin c &=& \frac{\sin a\cos b\cos B + \sin b \cos a \cos A}
{1-\sin a \sin b \sin A \sin B},\\
\cos c &=& \frac{\cos a\cos b + \sin a \sin b \cos A\cos B}
{1-\sin a \sin b \sin A \sin B},\\
c &=& \text{atan2}(\sin c,\,\cos c).
\end{eqnarray*}

This function is used only internally. Instructions on how to use
it manually are given in the help.

\subsubsection{\texttt{msl.m} and \texttt{msld.m}}
The implementation of the Middle-Side Law for spherical triangles:

\begin{eqnarray*}
\sin C &=& \frac{\sin A\cos B\cos b + \sin B \cos A \cos a}
{1-\sin a \sin b \sin A \sin B},\\
\cos C &=& \frac{-\cos A\cos B + \sin A \sin B \cos a\cos b}
{1-\sin a \sin b \sin A \sin B},\\
c &=& \text{atan2}(\sin c,\,\cos c).
\end{eqnarray*}

This function is used only internally. Instructions on how to use
it manually are given in the help.

\subsubsection{\texttt{acos2.m}, \texttt{acos2d.m}, \texttt{H.m} and \texttt{Hd.m}}
\texttt{H.m} and \texttt{Hd.m} are the implementations of the
Hemisphere-function $H(x)$, which determines the sign of the
arccosine in \texttt{acos2.m} or \texttt{acos2d.m}.

This function is used only internally. Instructions on how to use
it manually are given in the help.

\subsubsection{\texttt{blkassign.m}}
This is a tool that prevents long blocks of assignments, when each
variable to be assigned is a column or row of a given matrix.
Normally, this is done as

\begin{eqnarray*}
    A &=& \texttt{rand}(5);\\
    a &=& A(:,\,1);\\
    b &=& A(:,\,2);\\
    c &=& A(:,\,3);\\
    &&\text{etc.}
\end{eqnarray*}

but with \texttt{blkassign.m}, this can be shortened as

\begin{eqnarray*}
    A &=& \texttt{rand}(5);\\
    \left[a,\,b,\,c,\,\dots\right] &=& \texttt{blkassign}(A);\\
\end{eqnarray*}

Also this function is used internally only. Instructions on how to
use it manually are given in the help.

\bibliographystyle{plainnat}
\bibliography{bibliography}
\end{document}