\documentclass[prb,preprint]{revtex4-1}
% The line above defines the type of LaTeX document.
% Note that AJP uses the same style as Phys. Rev. B (prb).

% The % character begins a comment, which continues to the end of the line.

\usepackage{amsmath}  % needed for \tfrac, \bmatrix, etc.
\usepackage{amsfonts} % needed for bold Greek, Fraktur, and blackboard bold
\usepackage{graphicx}
\usepackage{amssymb} % needed for figures

\DeclareMathOperator{\dd}{d\!}
\DeclareMathOperator{\ddd}{\mathrm{d}}

\begin{document}

% Be sure to use the \title, \author, \affiliation, and \abstract macros
% to format your title page.  Don't use lower-level macros to  manually
% adjust the fonts and centering.

\title{Demystifying the Lagrangian of classical mechanics} %Derivation and Motivation of the Lagrangian of Classical Mechanics
% In a long title you can use \\ to force a line break at a certain location.

\author{Gerd Wagner}
\email{gerdhwagner@t-online.de} % optional
\affiliation{Mayener Str. 131, 56070 Koblenz, Germany} % optional second address
% If there were a second author at the same address, we would put another
% \author{} statement here.  Don't combine multiple authors in a single
% \author statement.
%\affiliation{mailing address}
% Please provide a full mailing address here.

\author{Matthew W. Guthrie}
\email{matthew.guthrie@ucf.edu}
\affiliation{Department of Physics, University of Central Florida, Orlando, FL 32816}

% See the REVTeX documentation for more examples of author and affiliation lists.

\date{\today}

\begin{abstract}
The Lagrangian formulation of classical mechanics is extremely useful for a vast array of physics problems encountered in the undergraduate and graduate physics curriculum. Unfortunately, many treatments of this topic lack explanations of the most basic details that make Lagrangian mechanics so practical. In this paper, we detail the steps taken to arrive at the principle of stationary action, the Euler-Lagrange equations, and the Lagrangian of classical mechanics. These steps are: 1) the calculation of the minimal distance between two points in a plane, to introduce the variational principle and to derive the Euler-Lagrange equation; 2) a straightforward reformulation of Newton's second law in the form of Euler-Lagrange equations and formulation of the principle of stationary action; and 3) proving that Euler-Lagrange equations are independent of arbitrary coordinate transformations and motivating that this independence is desirable for classical mechanics. This paper is targeted toward the advanced undergraduate student who, like our own experiences, struggles with details which are not seen as crucial to the utilization of the tools developed by Lagrangian mechanics, and is especially frustrated by the question ``\textit{why} is the Lagrangian always kinetic minus potential energy?'' We answer this question in a simple and approachable manner.
\end{abstract}

\maketitle

\section{Introduction}\label{introduction}

Lagrangian formulation of mechanics is a powerful description of classical mechanics. To some students, Lagrangian mechanics can be seemingly separate from the more familiar Newtonian formulation. Many undergraduate students who are taking a classical mechanics course have difficulties understanding how and why the Lagrangian formulation is an equivalent description of motion. Significant learning difficulty arises from the presentation of the material when students first encounter Lagrangian mechanics. Lagrangian mechanics presents an entirely new way for students to think about physics and this shift in thinking can be difficult. Often through a lack of class time to cover material, many of the treatments of the subject in university courses and popular textbooks relies on incomplete arguments, especially when considering the Lagrangian ($L$) itself. It is difficult to understand why the Lagrangian takes the intriguing form $L=T-V$, the difference between a particle's kinetic ($T$) and potential ($V$) energies (which are functions of position \boldmath$r$\unboldmath, velocity \boldmath$\dot{r}$\unboldmath, and time $t$).

The most popular undergraduate level treatments of the Lagrangian simply accept the definition as it is given and move on to using it to solve problems. Taylor~\cite[p.~238]{taylor2005classical} includes the explicit statement that the reader is ``certainly entitled to ask why the quantity $T-U$ should be of any interest''\footnote{Taylor uses $U$ for the potential energy function.} and continues to say that ``there seems to be no simple answer to this question except that it is.'' Marion ~\cite[p.~198-199]{marion1970classical} and Folwes and Cassiday~\cite[p.~393]{fowles1999analytical} merely define the Lagrangian and move to its utility. Gregory~\cite[p.~348]{gregory2006classical} explains the Lagrangian in further detail, including a detailed derivation, although the information is presented over multiple sections and chapters with numerous digressions.

In our treatment presented in this paper, the Lagrangian formalism arises naturally through an introductory example from geometry. We then utilize the formalism to reformulate Newton's second law in the form of the Euler-Lagrange equation. To motivate the new formulation of Newton's second law, we prove the invariance of the Euler-Lagrange equation under arbitrary coordinate transformations. To make clear why this property is important for physics, we remind the reader that the laws of nature do not depend on the coordinates we use to describe them. On the other hand, physicists cannot formulate laws without coordinates and physical equations usually look different in different coordinates\footnote{Coordinate-free laws of physics do exist (see Maxwell's equations written in the language of differential forms or Einstein's equations). However, these formulations are not very useful until a coordinate system is chosen.}. This is why the relation of physical theories to coordinates should be as well defined and restricted as possible. The Lagrangian formalism fulfills this demand through the Euler-Lagrange equations being independent of coordinate transformations. Lagrangian functionals do depend on coordinates but in the simplest way physicists can think of: they transform like scalars.

Thinking about the relation of the laws of physics to coordinates proved fruitful in the past.
Probably the most famous example is Einstein's first paper on special relativity~\cite{EinsteinSpecialRelativity}.
In this paper we follow this tradition, reformulating Newton's second law in the form of Euler-Lagrange equations. As a result, the Lagrangian of classical mechanics appears naturally without requiring further motivation or derivation.

\section{The Lagrangian formalism and the minimal distance between two points in a plane}\label{distance}

This section is adapted from many sources, chiefly Goldstein's Classical Mechanics textbook~\cite{goldstein2002classical}. Because it is a simple introduction to both the functional that we will derive to be the Lagrangian, and to techniques in the calculus of variations (a pre-requisite topic to understanding the Lagrangian formalism), we aim to show that the minimal distance between two points in a plane is a straight line.
%This same derivation is often used to motivate and introduce the calculus of variations which is a pre-requisite topic to understand the Lagrangian formalism.
\footnote{Some limitations are: we limit ourselves to lines that can be written as one dimensional functions $y=f(x)$ while curves in two dimensions would be more general. We assume the shortest distance must be a differentiable function (although continuous would be sufficient). Foremost, we satisfy ourselves with finding a condition that makes the curve only stationary instead of minimal.}

The arc length $S$ of a function $y(x)$ between two points $(x_1,y_1)$ and $(x_2,y_2)$ is given by

\begin{equation}
S=\int\limits_{x_1}^{x_2}\sqrt{\dd x^2 + \dd y^2}.
\end{equation}
Factoring a $\dd x$ from the radical results in the following equation
\begin{equation}
S= \int\limits_{x_1}^{x_2}\sqrt{1 + \left(\frac{\dd y}{\dd x}\right)^2} \dd x,
\end{equation}
and defining $y' := \frac{\dd y}{\dd x}$ gives a convenient representation for the equation representing the arc length of $y(x)$ as a function of how $y$ changes over its length, %``changes'' is not the best terminology.

\begin{equation}
S = \int\limits_{x_1}^{x_2}\sqrt{1 + y'^2} \dd x.
\end{equation}
We generalize this formula by writing
\begin{equation}
S=\int\limits_{x_1}^{x_2} G(y,y',x) \dd x .
\end{equation}
Although in our example $G = \sqrt{1 + y'^2}$ only depends on $y'$ and not explicitly on $y$ or $x$, we can also assume dependence on $y$ and $x$. %This is to say that later back replacements of $G$ by $\sqrt{1 + y'^2}$ will still be possible.
If formulas we derive for $G$ contain derivatives of $G$ with respect to $y$ or $x$, we just replace these terms with zero as the derivative of a function with respect to a variable it does not depend on is always zero.

% Former version:
% and making the assumption that $G = \sqrt{1 + y'^2}$ only depends on $y'$ and not on $y$ or $x$. We further do as if $G$ would depend on more variables than it actually does. This does no harm to later back replacements of $G$ either. Should formulas we derive for $G$ contain derivatives of $G$ with respect to these new variables, we just replace these terms with zero as the derivative of a function with respect to a variable it does not depend on is always zero. %I'm struggling to find a situation where this would happen (but it is of course mathematically true)

To find the function $y(x)$ that minimizes $S$, we make the simplifying assumption that it is sufficient to find the $y(x)$ that makes $S$ stationary. In other words, we assume that S has exactly one minimum.
To do this, we consider small but arbitrary variations $\delta y$ of $y$ and try to find a condition that causes $\delta S$ to vanish.
During this, the endpoints $(x_1,y_1)$ and $(x_2,y_2)$ are kept fixed; therefore the variations $\delta y$ have the property $\delta y(x_1) = \delta y(x_2) = 0$.
As a result,

\begin{equation}
\delta S = \int\limits_{x_1}^{x_2} \left(\frac{\partial G}{\partial y} \delta y
+ \frac{\partial G}{\partial y'} \delta y' \right) \dd x.
\end{equation}
Using $\delta y' = y_2' - y_1' = \frac{\ddd }{\dd x}(y_2 - y_1) = \frac{\ddd}{\dd x} \delta y$ and integration by parts for the second term, we find
\begin{equation}
\delta S = \int\limits_{x_1}^{x_2} \left( \frac{\partial G}{\partial y} \delta y
- \frac{\ddd}{\dd x}\frac{\partial G}{\partial y'} \delta y \right) \dd x
+ \left[\frac{\partial G}{\partial y'} \delta y \right]_{x_1}^{x_2}.
\end{equation}
The last term vanishes because $\delta y(x_1) = \delta y(x_2) = 0$, and we are left with
\begin{equation}
\delta S = \int\limits_{x_1}^{x_2} \left( \frac{\partial G}{\partial y}
- \frac{\ddd}{\dd x}\frac{\partial G}{\partial y'} \right) \delta y \; \dd x.
\end{equation}
For $S$ to be stationary, $\delta S$ must vanish. Since $\delta y$ is arbitrary the condition must be
\begin{equation}\label{e-l}
\frac{\partial G}{\partial y} - \frac{\ddd}{\dd x}\frac{\partial G}{\partial y'} = 0.
\end{equation}
This equation is called an Euler-Lagrange equation. The procedure of looking for a condition to make $S$ stationary under a function $G(y,y',x)$ is called the Lagrangian formalism. If we substitute $G = \sqrt{1+y'^2}$ into equation \eqref{e-l}, we see that $y'$ must be constant and thus $y(x)$ is a straight line connecting $(x_1,y_1)$ and $(x_2,y_2)$.

\section{Application to Newtonian mechanics} \label{application}
As we discussed in section \ref{introduction}% and will motivate through section \ref{invariance}
, it is desirable to write Newton's law $F=ma$ in the form of an Euler-Lagrange equation. Doing so will provide us with a function like $G$ from the previous section, only now the function will have important physical implications. This function is called the Lagrangian of classical mechanics.

To proceed, we rearrange Newton's second law

\begin{equation}
0 = ma - F.
\end{equation}
The first term can be rewritten as follows:
\begin{equation}
ma = m \ddot{r} = \frac{\ddd}{\dd t} (m \dot{r})
= \frac{\ddd}{\dd t} \frac{\partial}{\partial \boldmath\dot{r}\unboldmath} \left(\frac{1}{2} m \dot{r}^2 \right)
= \frac{\ddd}{\dd t} \frac{\partial T}{\partial \dot{r}},
\end{equation}
where $T:=\frac{1}{2} m \dot{r}^2$ is the classical kinetic energy of the system. For the second term, we assume the force $F$ to be conservative. Consequently, there exists a potential $V$ such that
\begin{equation}
F = - \frac{\partial V}{\partial r} = \frac{\partial (-V)}{\partial r}.
\end{equation}
Using both rewritten terms, Newton's law becomes
\begin{equation}
0 = \frac{\ddd}{\dd t} \frac{\partial T}{\partial \dot{r}} - \frac{\partial (-V)}{\partial r}.
\end{equation}
If we assume that $\partial T/ \partial r = 0$ and $\partial V / \partial \dot{r} = 0$, which is nearly always true in Newtonian mechanics, we can convert the equation to
\begin{equation}\label{e-lwithtv}
0 = \frac{\ddd}{\dd t} \frac{\partial (T-V)}{\partial \dot{r}} - \frac{\partial (T-V)}{\partial r}.
\end{equation}

\section{Invariance of the Euler-Lagrange equation under coordinate transformations} \label{invariance}

This section was inspired by the first chapter of Hagen (2009)~\cite{hagen2009path}.
Let $y=f(Y,x)$ be an invertible and differentiable coordinate transformation.
\footnote{The most effective tool in a physicist's toolbox when solving physics problems is picking an appropriate coordinate system.
A pendulum in Euclidean $(x,y)$ coordinates makes analyzing the problem cumbersome, but in circular $(r,\theta)$ coordinates, analysis becomes simple.
Translating from the coordinate system of the problem statement to the coordinate system that best simplifies the system usually provides great insight, but translating back to the coordinate system of the problem statement is still necessary to solve the problem.}
%This means for any $x$ the transformation $f$ is supposed to be an invertible and differentiable function of the new coordinate $Y$.
%The dependence on $x$ is optional, which is to say that we also well allow transformations $f$ that are only functions of $Y$.
%
We define the transformed function $\widetilde{G}$ through $G$ by
\begin{equation} \label{lagrangian-transform}
\widetilde{G}(Y,Y',x) := G(f,f',x).
\end{equation}
Using the derivation from section \ref{distance}, we find that making $S = \int\limits_{x_1}^{x_2} \widetilde{G}(Y,Y',x) \dd x$ stationary requires the Euler-Lagrange equation

\begin{equation}
\frac{\partial \widetilde{G}}{\partial Y}
- \frac{\ddd}{\dd x}\frac{\partial \widetilde{G}}{\partial Y'} = 0
\end{equation}
be satisfied. This result is again reached by considering a small but arbitrary variation $\delta Y$ which again vanishes at its endpoints.

Likewise, the variation of the same $S$ can be expressed by
\begin{equation}
\delta S = \int\limits_{x_1}^{x_2} \left( \frac{\partial G}{\partial f} \delta f
+ \frac{\partial G}{\partial f'} \delta f' \right) \dd x
\end{equation}
where $\delta f$ is given by $\delta f = \frac{\partial f}{\partial Y} \delta Y$.

Using $\delta f' = f_2' - f_1' = \frac{\ddd}{\dd x}(f_2 - f_1) = \frac{\dd}{\dd x} \delta f$ and integration by parts for the second term we find
\begin{equation}
\delta S = \int\limits_{x_1}^{x_2} \left( \frac{\partial G}{\partial f}
- \frac{\ddd}{\dd x} \frac{\partial G}{\partial f'} \right) \delta f \, \dd x \;
+ \; \left[\frac{\partial G}{\partial f'} \delta f \right]_{x_1}^{x_2}.
\end{equation}
As $\delta Y$ goes to $0$ at the endpoints, so does $\delta f$ which causes the last term to vanish. From the arbitrariness of $\delta Y$ follows the arbitrariness of $\delta f$. The only mechanism for $\delta S$ to vanish is

\begin{equation}
0 = \frac{\partial G}{\partial f} - \frac{\ddd}{\dd x} \frac{\partial G}{\partial f'}
= \frac{\partial G}{\partial y} - \frac{\ddd}{\dd x} \frac{\partial G}{\partial y'}.
\end{equation}
This shows that the Euler-Lagrange equation takes the same form under any coordinate transformation as long as the transformation of the function $G$ is given by $\widetilde{G}(Y,Y',x) := G(f,f',x)$. Functions that behave this way under coordinate transformations as $G$ are called scalar functions, especially in physical contexts.
%Can we give an example of a scalar function in undergraduate level mechanics? "Such as the gravitational potential or temperature over an object"
\footnote{An example for a scalar is air temperature.
To analyze this example we consider two coordinate systems with coordinates $x$ and $X$ and their transformation $x=f(X)$.
If $T=T(x)$ denotes temperature in the first coordinate system then as an analogue to definition \eqref{lagrangian-transform} we define the temperature in the second coordinate system by
\begin{equation} \label{definitionOfTemperatureTransform}
  \tilde{T}(X) := T(f(x))
\end{equation}

Now the physical fact that temperature is the same in both coordinate systems leads to the condition $\tilde{T}(X) = T(x)$ which by using definition \eqref{definitionOfTemperatureTransform} can be turned into $T(f(x)) = T(x)$.
Scalars that fulfill this condition are called invariant with respect to the transformation $f$.

An interesting point of the temperature example is that this condition becomes invalid if one of the two coordinate systems moves with a velocity that is not negligible compared to the average motion of the air molecules while the other stays relative to the air at rest.
}
In section \ref{lagrangian-def}, $G$ will be interpreted as the Lagrangian we mentioned in section \ref{introduction}. As we now see, $G$ does indeed have the transformation properties we claimed in section \ref{introduction}.

\section{Definition of the Lagrangian}\label{lagrangian-def} %We can easily remove this section header if a reviewer suggests it, though for now I think defining the Lagrangian is important enough to warrant it

Looking back at the original Euler-Lagrange equation \eqref{e-l} and the transformed version of equation \eqref{e-lwithtv}, let time $t$ take the place of a general independent variable $x$, position $r$ take the place of a general coordinate $y$, and $T-V$ take the place of $G$, we arrive at the following results:

Newton's second law takes the form
\begin{equation}
0 = \frac{\ddd}{\dd t} \frac{\partial L}{\partial \dot{r}} - \frac{\partial L}{\partial r},
\end{equation}
where
\begin{equation}
 L := T-V.
\end{equation}
This $L$ is precisely the Lagrangian from classical mechanics. It is not some divinely sanctioned quantity; it is just Newtonian mechanics under a simple change of variable.

Using this representation of $L$ allows the Lagrangian formulation of mechanics to be derived by requiring that the trajectory $r(t)$ which a particle takes between two endpoints $(t_1,r_1)$ and $(t_2,r_2)$ makes the integral
\begin{equation}\label{eqref:action}
S=\int\limits_{t_1}^{t_2} L \; \dd t
\end{equation}
stationary. From this, the equations of motion for the particle follow, being the particle's Euler-Lagrange equation. This is called the principle of stationary action in physics\footnote{The principle of stationary action is also sometimes called the principle of \emph{least} action. This can be confusing because the Euler-Lagrange equation finds instances where action is stationary (e.g. a saddle point or even maximized)~\cite{gray2007action}.}.

It is unnecessary to speciously explain the form of the classical Lagrangian as having an innate property of interest. We have shown that the Lagrangian takes its classical form
\begin{equation}
  L = T-V
\end{equation}
out of convenience.

Since $F=ma$ is formulated in Cartesian coordinates, the Lagrangian $L$ we derived is formulated in Cartesian coordinates, as well. The transformation law \eqref{lagrangian-transform} we found for $G$ provides us with a well defined method for the transformation of $L$ to other coordinate systems. The equations of motion which are now Euler-Lagrange equations are likewise the same in any coordinate system.  This way the relation of Newton's law to coordinates is restricted and well defined in the sense we mentioned in section \ref{introduction}. %$L$ has the dimension of energy, thus $S$ has dimensions of energy times time, a quantity also called action.

\section{Conclusion and Outlook}

Although the principle of stationary action is a new interpretation of classical mechanics, it nonetheless leads to the same equations of motion as Newtonian physics. In sections \ref{distance} and \ref{invariance} we explored the mathematical structure of the Lagrangian formalism and, because of its transformation properties, found it desirable for classical mechanics. This motivated and enabled us to give comprehensive derivations of the Lagrangian of classical mechanics $L=T-V$ and the principle of stationary action. We hope that this has been useful for clarifying at least one aspect of the Lagrangian formulation of classical mechanics for students who are curious about the subject.

Although the transformation properties of the Lagrangian and the Euler-Lagrange equations are already reason enough to formulate physical laws using the principle of stationary action, these are by far not the only reasons. The principle of stationary action has enormous analytical capabilities which lie far beyond those of Newtonian mechanics. As an outlook we mention some of these analytical capabilities.

\subsection{Conservation laws~\cite{KleinertConservation}}
The principle of stationary action allows derivation of conservation laws from transformation properties of the Lagrangian. From this, energy, momentum, angular momentum, and other quantities that may be conserved, are given formulations which depend only on the Lagrangian, coordinates, and time. Thus, concepts like energy, momentum, and angular momentum gain well-defined meaning for any physical system that has a Lagrangian. Since every physical theory can be described by the principle of stationary action (for examples see section \ref{field.theory}), the concepts of energy, momentum, angular momentum, and other conserved quantities are consistently defined for all of these theories.

\subsection{Quantum mechanics~\cite{Schwabl}}
The rules of quantum mechanics are mostly based on Hamiltonian physics and Poisson brackets which are both continuations of the principle of stationary action. A recent and in-depth discussion of an Lagrangian underlying the Schr{\"o}dinger equation can be found in Deriglazov's paper~\cite{Deriglazov}.

\subsection{Field theory~\cite{Sterman,Dirac}} \label{field.theory}
The principle of stationary action can be and has been extensively continued to field theories. For example, Maxwell's equations can be derived from a principle of stationary action in electrodynamics. There, Maxwell's equations play a similar role as Newton's laws did in this paper.

Most physical theories are field theories, and as a result the widest variety of Lagrangians are field Lagrangians. The most popular field theories apart from electrodynamics are
\begin{itemize}
  \item The Dirac Lagrangian for the relativistic field of fermions.
  \item The Klein-Gordon Lagrangian for the relativistic field of bosons.
  \item The Schr{\"o}dinger Lagrangian for non-relativistic quantum mechanics.
  \item The Lagrangian of the Standard Model of particle physics.
  \item The general relativity Lagrangian for Einstein's theory of general relativity.
\end{itemize}

\subsection{Constrained motion~\cite{Kuypers}}
The simplest example of a problem of constrained motion is that of particle on an inclined plane in a uniform gravitational field. There are, of course, situations with more complex constraints than an inclined plane. In these cases it can be difficult to find the equations of motion for the particle through using Newton's laws. At this point, the transformation law we derived in section \ref{invariance} becomes very helpful: assume there is a transformation of coordinates that well suits the constraints to the coordinates in which the Lagrangian is formulated. Once that is done, formula \eqref{lagrangian-transform} can be used to rewrite the Lagrangian in the coordinates that suit the constraints. The equations of the constrained motion are then the Euler-Lagrange equations of the transformed Lagrangian.

\acknowledgments{We would like to thank the many friends and teachers who helped us clarify numerous points in this manuscript and encouraged the curiosity that sparked this writing. Namely, John Jaszczak, Zhongzhou Chen, Tony Szedlak, David McGhan, and Chris Riley.}

\bibliographystyle{aipauth4-1}
\bibliography{lagrange}

% \begin{thebibliography}{9}

% \bibitem{taylor} John R. Taylor, Classical Mechanics, 2005, page 238

% \bibitem{marion} Jerry B. Marion, Classical Dynamics of Particles and Systems, 1970, page 198-199

% \bibitem{fowles} Grant R. Fowles, Analytical Mechanics, 1999, page 393

% \bibitem{gregory} R. Douglas Gregory, Classical Mechanics, 2006, page 348

% %\bibitem{Klopper} Juan Klopper, Understanding the Euler Lagrange Equation, \url{https://www.youtube.com/watch?v=08vJyA-XD3Q}

% \bibitem{Goldstein} Goldstein et.al., Classical mechanics 3 ed, Chapter 2.2.

% \bibitem{Kleinert} Hagen Kleinert, Path Integrals in Quantum Mechanics, Statistics, Polymer Physics, and Financial Markets, 2004, page 5, \url{https://books.google.com/books?id=dJ3FCgAAQBAJ}

% \bibitem{action-not-least} Edwin Taylor and C.G. Gray, When Action is Not Least, doi: 10.1119/1.2710480

% %\bibitem{Guthrie} Matt Guthrie, The Origin of the Lagrangian, \url{https://web2.ph.utexas.edu/~mwguthrie/t.lagrangian.pdf}

% Citations for conclusion subsections:

% \bibitem{Kleinert-conservation} Hagen Kleinert, Particles and Quantum Fields, 2015, Section 8 , \url{https://books.google.de/books?id=d1-2DAAAQBAJ}

% \bibitem{Schwabl} Franz Schwabl, Quantum Mechanics, 2007, 4th edition, Sections 2.5 and 2.6, \url{https://books.google.de/books?id=pTHb4NK2eZcC&printsec}

% \bibitem{Sterman} George Sterman, An Introduction to Quantum Field Theory, 1993, Sections 1.1 - 1.4, 5.3, 5.4, \url{https://books.google.de/books?id=rB_wdTnfTmoC}

% \bibitem{Dirac} P. A. M. Dirac, General theroy of relativitiy, 1975/1996, Sections 26 - 30, \url{https://books.google.de/books?id=qkWPDAAAQBAJ}

% \bibitem{Kuypers} Friedhelm Kuypers, Klassische Mechanik, 2016, Section 3, \url{https://books.google.de/books?id=dJ4CDAAAQBAJ}

% \end{thebibliography}

\end{document}


\subsubsection{Domain specific features}
\label{sec:methods_features_domain}

The objective of including domain specific features is to add into the
secondary model data that is intrinsically representative of the underlying
technology and market behavior. This subsection shows and explains the
transformations incurred to the listed features in \ref{sec:material_data_bitcoin_features}.

\paragraph{Addresses:} this group is composed of: \emph{total-addresses},
\emph{new-addresses}, \emph{active-addresses}, \emph{total-addresses},
\emph{sending-addresses}, \emph{receiving-addresses}. We can split these
features into two groups, in figure \ref{fig:total_addresses} the evolution of
total registered addresses in the network is plotted. A logarithm transformation
is show as well in green to stabilize its variance and range. Then, in figure
\ref{fig:addresses} we see the evolution of the other features. The reader may
observe the strong correlation between these four indexes and validate the
observation by inspecting figure \ref{fig:addresses_correlation} which shows the
correlation matrix between these four attributes. Because of that, only
\emph{active-addresses} will be used to train the model. We computed the
fractional differentiation optimum $d$ value for the latter index and turned out
to be 0.1.

\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{methods/images/total_addresses.png}
    \caption{Evolution of total registered addresses over time.}
    \label{fig:total_addresses}
\end{figure}

\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{methods/images/addresses.png}
    \caption{Evolution of \emph{new-addresses}, \emph{active-addresses}, \emph{total-addresses},
\emph{sending-addresses} and \emph{receiving-addresses} over time.}
    \label{fig:addresses}
\end{figure}

\begin{figure}[H]
    \centering
    \includegraphics[width=0.7\textwidth]{methods/images/addresses_correlation_mat.png}
    \caption{Correlation matrix of \emph{new-addresses}, \emph{active-addresses}, \emph{total-addresses},
\emph{sending-addresses} and \emph{receiving-addresses}.}
    \label{fig:addresses_correlation}
\end{figure}

\paragraph{Blocks:} in this group There are: \emph{blocks-mined},
\emph{block-interval-mean}, \emph{block-interval-median}, \emph{block-size-mean}
and \emph{block-size-total}. The first three exhibit a quite similar tendency
(see figure \ref{fig:blocks_produced_blocks_mined}) which is aligned with what
they mean: \emph{blocks-mined} describes the number of blocks added to the
blockchain and the other two are centrality measures of the production rate. The
correlation matrix confirms it, see figure \ref{fig:blocks_production_blocks_mined_corr}.
Only \emph{blocks-mined} will be used to avoid adding redundant information.
On the other hand, we have the \emph{block-size-mean} and
\emph{block-size-total} which are expressed in bytes. Both indexes are expected
to be correlated and \ref{fig:block_size} shows their evolution over time. A
fractional differentiation transformation is not enough to stabilize the series
and a logarithmic transformation is applied which results in
\ref{fig:block_size_log}. Only the logarithm of \emph{block-size-total} will be
used to train the model between these two.

\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{methods/images/block_production_blocks_mined.png}
    \caption{Evolution of \emph{block-interval-mean} and \emph{block-interval-median} are tracked on the left y-axis. \emph{blocks-mined} is tracked on the right y-axis.}
    \label{fig:blocks_produced_blocks_mined}
\end{figure}

\begin{figure}[H]
    \centering
    \includegraphics[width=0.7\textwidth]{methods/images/block_production_blocks_mined_corr.png}
    \caption{Correlation matrix of \emph{blocks-mined}, \emph{block-interval-mean} and \emph{block-interval-median}.}
    \label{fig:blocks_production_blocks_mined_corr}
\end{figure}

\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{methods/images/block_size.png}
    \caption{Evolution of \emph{block-size-mean} (in blue and tracked on the left y axis) and \emph{block-size-total} (in red and tracked on the right y axis) over time.}
    \label{fig:block_size}
\end{figure}

\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{methods/images/block_size_log.png}
    \caption{Same features as in \ref{fig:block_size} but with a logarithmic transformation. Due to the change of scale, both features are tracked on the same left y axis.}
    \label{fig:block_size_log}
\end{figure}

\paragraph{Fees:} there are two features related to fees: \emph{fees-total} and
\emph{fees-mean}. They both remain relatively stable with very low values but
there are high valued outliers that get out of range rapidly. Fractional
differentiation was tried but ended up in very low values of $d$ that made no
significant change.  A logarithmic transformation is applied to account the explosive change in local variance when
these outliers occur. Note also that because some values in the series are zero,
a $1$ is added to the logarithm argument to avoid having minus infinity in the
series after the transformation. For illustration purposes, figure
\ref{fig:total_fees} is shown.

\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{methods/images/total_fees.png}
    \caption{Evolution of \emph{fees-total} (in blue) and its the logarithmic transformation (in red) over time. The left y axis tracks the linear scale and the right y axis tracks the logarithmic scale.}
    \label{fig:total_fees}
\end{figure}

\paragraph{General indicators:} in this group we have \emph{sopr}, \emph{ratio},
\emph{daysTillHalving}, \emph{price-drawdown-from-ath}, \emph{market-cap} and
\emph{circulating-supply}. The relationship between \emph{market-cap} and
\emph{ratio} was already explained in section \ref{sec:intro_domain} when
referring to the stock to flow model so it will be omitted in this case. We will
just mention that \emph{market-cap} will suffer a logarithmic transformation to
stabilize its range. \emph{sopr} and \emph{price-drawdown-from-ath} are indexes
so no further transformation is required (see figures \ref{fig:sopr} and
\ref{fig:price-drawdown-from-ath}). Moreover, the \emph{circulating-supply}
values are in the order of millions and evolves asymptotically to 18 millions as
the issuance model expects it to be. In particular, we will create a derivative
index from the supply computed as the differentiated series of circulating
supply logarithm which shows the speed of issuance (see figure \ref{fig:circulating_supply_rate}).

\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{methods/images/sopr.png}
    \caption{Evolution of \emph{sopr} over time.}
    \label{fig:sopr}
\end{figure}

\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{methods/images/price-drawdown-from-ath.png}
    \caption{Evolution of \emph{price-drawdown-from-ath} over time. In vertical
    dashed red lines the halving dates are displayed}
    \label{fig:price-drawdown-from-ath}
\end{figure}

\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{methods/images/circulating_supply_rate.png}
    \caption{Evolution of the logarithm of issuance over time. In vertical
    dashed red lines the halving dates are displayed.}
    \label{fig:circulating_supply_rate}
\end{figure}

\paragraph{Transactions:} in this group we have \emph{transaction-size-total},
\emph{transaction-rate}, \emph{transaction-count}, \emph{transaction-size-mean},
\emph{transfer-volume-mean} and \emph{transfer-volume-median}. As it can be seen
in figure \ref{fig:transaction_corr}, the three indexes
\emph{transaction-size-total}, \emph{transaction-rate}, and
\emph{transaction-count} are highly correlated so only one will be taken as
input to the model, in particular the \emph{transaction-rate}. A note about this
index can be done when looking at the histogram which seems to be bimodal. A
derivative index is created from it which groups the values into their deciles
and aims to reduce the high frequency changes in the transaction rate that
affect the signal on a daily basis. Figure \ref{fig:transaction_rate} shows it
in detail. Moving to \emph{transaction-size-mean} the index exhibits some
frequent spikes but those are within range and there is no clear trend so the
feature remains as is (\ref{fig:transaction_size_mean}). Central transfer volume
features (\emph{transfer-volume-mean} and \emph{transfer-volume-median}) exhibit
a high volatility in the first issuance period and then it is more and more
stable. A logarithmic transformation is applied to both and even though they
try to predict the same, their tendencies are different in values and capture
differently the local volatility so both will be preserved (
\ref{fig:transfer_volume_central}). Similarly to the processing done to the
others, the \emph{transfer-volume-total} is transformed with a logarithm (
\ref{fig:transfer-volume-total}).

\begin{figure}[H]
    \centering
    \includegraphics[width=0.7\textwidth]{methods/images/transaction_corr.png}
    \caption{Correlation matrix between the transaction related features}
    \label{fig:transaction_corr}
\end{figure}

\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{methods/images/transaction_rate.png}
    \caption{Left: \emph{transaction-rate} (in blue) and decile index of the same feature (in red) over time. Right: histogram of \emph{transaction-rate}.}
    \label{fig:transaction_rate}
\end{figure}

\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{methods/images/transaction_mean_size.png}
    \caption{Evolution of \emph{transaction-size-mean} over time.}
    \label{fig:transaction_size_mean}
\end{figure}

\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{methods/images/transfer_volume_central.png}
    \caption{Evolution of the logarithm of \emph{transfer-volume-mean} and \emph{transfer-volume-median}.}
    \label{fig:transfer_volume_central}
\end{figure}

\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{methods/images/transfer_volume_total.png}
    \caption{Evolution of the logarithm of \emph{transfer-volume-total} over time.}
    \label{fig:transfer-volume-total}
\end{figure}

\paragraph{Unspent / spent transactions:} under this final group we can find:
\emph{utx-os-created} \emph{utx-os-spent}, \emph{utxo-value-spent-mean},
\emph{utxo-value-spent-median} and \emph{utxo-value-created-mean}. Following the
same procedure as with the others, a correlation matrix between these features
is created and shown in figure \ref{fig:utxo_corr}. We can find two pairs of
highly correlated features: \emph{utx-os-created} with \emph{utx-os-spent}, and
\emph{utxo-value-created-mean} with \emph{utxo-value-spent-mean}. Note that
\emph{utxo-value-spent-median} is mildly correlated with the others (0.48 and
0.49 respectively) which makes this feature to be kept. Provided that
\emph{utx-os-created} is a super set of \emph{utx-os-spent}, the former will be
preferred (see figure \ref{fig:utxo_created}). The same reasoning applies to
 \emph{utxo-value-created-mean} (see figure \ref{fig:mean_utxo_created}).

\begin{figure}[H]
    \centering
    \includegraphics[width=0.7\textwidth]{methods/images/utxo_corr.png}
    \caption{Correlation matrix between unspent / spent transaction features.}
    \label{fig:utxo_corr}
\end{figure}

\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{methods/images/utxo_created.png}
    \caption{Evolution \emph{utx-os-created} (in blue) and its logarithmic transform (in green) over time.}
    \label{fig:utxo_created}
\end{figure}

\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{methods/images/utxo_value_spent.png}
    \caption{Left: evolution of \emph{utxo-value-created-mean} (blue) and its logarithmic transformation (in green) over time. Right: evolution of \emph{utxo-value-created-median} (blue) and its logarithmic transformation (in green) over time.}
    \label{fig:utxo_value_spent}
\end{figure}

\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{methods/images/mean_utxo_created.png}
    \caption{Evolution of \emph{utxo-value-created-mean} (in blue) and its logarithmic transformation (in green) over time.}
    \label{fig:mean_utxo_created}
\end{figure}


\documentclass[pdf]{beamer}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{hyperref}
\usepackage{listings}
\usepackage{tcolorbox}
\usepackage[all]{xy}

\mode<presentation>{}

% ------------------------------------------------------------------------------
% Theme

\usetheme[usetitleprogressbar, nosmallcapitals, protectframetitle, usetotalslideindicator]{m}

\lstloadlanguages{Haskell}
\lstnewenvironment{code}
    {\lstset{}%
      \csname lst@SetFirstLabel\endcsname}
    {\csname lst@SaveFirstLabel\endcsname}
    \lstset{
      basicstyle=\ttfamily\footnotesize,
      flexiblecolumns=false,
      basewidth={0.5em,0.45em},
      literate={+}{{$+$}}1 {/}{{$/$}}1 {*}{{$*$}}1
               {\\\\}{{\char`\\\char`\\}}1
               {=>}{{$\Rightarrow$}}2
               {forall}{{$\forall$}}2
               {->}{{$\rightarrow$}}2
               {<-}{{$\leftarrow$}}2
               {>>}{{>>}}3 {>>=}{{>>=}}3,
      commentstyle={\ttfamily\color{gray}},
      language=haskell
    }

% ------------------------------------------------------------------------------
% Presentation

\title{Finally \texttt{mtl}!}
\date{\today}
\author{Joseph Tel Abrahamson / @sdbo / \texttt{github.com/tel} }

\renewcommand{\to}{\ensuremath{\rightarrow}}
\DeclareMathOperator{\ty}{\texttt{ :: }}

\begin{document}

\maketitle

\begin{frame}
  \frametitle{Synopsis}
  \begin{itemize}
  \item What is ``Finally Tagless''? What's important about it?
  \item What is the ``Monad Transformer Library'', \texttt{mtl}?
  \item \texttt{mtl} is a ``finally tagless'' effect library---and that's quite
    nice!
  \end{itemize}
\end{frame}

\section{A tale of three DSLs}

\begin{frame}[fragile]
  \frametitle{Hutton's Razor}
\begin{lstlisting}
data AddLang
  = AddLangIntLit Integer
  | Add AddLang AddLang
  deriving ( Show, Eq )

interpAddLang :: AddLang -> Integer
interpAddLang = \case
  AddLangIntLit i -> i
  Add l r  -> interpAddLang l + interpAddLang r

addLangExp :: AddLang
addLangExp = Add (AddLangIntLit 1) (AddLangIntLit 3)
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Hutton's Backup Razor}
\begin{lstlisting}
data MultLang
  = MultLangIntLit Integer
  | Mult MultLang MultLang
  deriving ( Show, Eq )

interpMultLang :: MultLang -> Integer
interpMultLang = \case
  MultLangIntLit i -> i
  Mult l r -> interpMultLang l * interpMultLang r

multLangExp :: MultLang
multLangExp = Mult (MultLangIntLit 1) (MultLangIntLit 3)
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Hutton's Travel Kit}
\begin{lstlisting}
data RingLang
  = RingLangIntLit Integer
  | RingAdd  RingLang RingLang
  | RingMult RingLang RingLang
  deriving ( Show, Eq )

interpRingLang :: RingLang -> Integer
interpRingLang = \case
  RingLangIntLit i -> i
  RingAdd  l r -> interpRingLang l + interpRingLang r
  RingMult l r -> interpRingLang l * interpRingLang r

ringLangExp :: RingLang
ringLangExp = RingMult (RingAdd (RingLangIntLit 3)
                                (RingLangIntLit 2))
                       (RingLangIntLit 3)
\end{lstlisting}
\end{frame}

\section{What's wrong with this picture?}

\begin{frame}[fragile]
  \frametitle{D. R. Y.}
\begin{lstlisting}
data RingLang
  = RingLangIntLit Integer
  | RingAdd  RingLang RingLang
  | RingMult RingLang RingLang
  deriving ( Show, Eq )

interpRingLang :: RingLang -> Integer
interpRingLang = \case
  RingLangIntLit i -> i
  RingAdd  l r -> interpRingLang l + interpRingLang r
  RingMult l r -> interpRingLang l * interpRingLang r
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{D. R. Y.}
\begin{lstlisting}

  = RingLangIntLit Integer
  | RingAdd  RingLang RingLang
  | RingMult RingLang RingLang

  RingLangIntLit i -> i
  RingAdd  l r -> interpRingLang l + interpRingLang r
  RingMult l r -> interpRingLang l * interpRingLang r
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{D. R. Y.}
\begin{lstlisting}

  | RingAdd  RingLang RingLang
  | RingMult RingLang RingLang

  RingAdd  l r -> interpRingLang l + interpRingLang r
  RingMult l r -> interpRingLang l * interpRingLang r
\end{lstlisting}
\end{frame}

\plain{Nooooooooooooooooooooo- oooooooooooooooooo!}

\begin{frame}
  \frametitle{Let's use category theory!}
  \begin{displaymath}
    \xymatrix{
      \mathtt{AddLang} & \bullet \ar[rdd]_{???} & & \ar[ldd]^{???} \bullet & \mathtt{MultLang} \\
      & & & & \\
      & & \bullet & & \\
      & & \mathtt{RingLang} & & \\
    }
  \end{displaymath}
\end{frame}

\section{Once more, with feeling!}

\begin{frame}[fragile]
  \frametitle{Routine recursion scheme surgery}
\begin{lstlisting}[p]
newtype Fix f = Fix { unFix :: f (Fix f) }

fixFold :: Functor f => (f a -> a) -> (Fix f -> a)
fixFold phi = go where go = phi . fmap go . unFix
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Routine recursion scheme surgery}
\begin{lstlisting}
{-# LANGUAGE UndecidableInstances #-}

deriving instance Show (f (Fix f)) => Show (Fix f)
deriving instance Eq   (f (Fix f)) => Eq   (Fix f)
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Routine recursion scheme surgery}
\begin{lstlisting}
{-# LANGUAGE DeriveFunctor #-}

data AddF  x = AddI  Integer | AddF  x x deriving ( Show, Eq, Functor )
data MultF x = MultI Integer | MultF x x deriving ( Show, Eq, Functor )

type AddLang'  = Fix AddF
type MultLang' = Fix MultF
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Routine recursion scheme surgery}
\begin{lstlisting}
addLangExp' :: AddLang'
addLangExp' = Fix (AddF (Fix (AddI 1)) (Fix (AddI 3)))

multLangExp' :: MultLang'
multLangExp' = Fix (MultF (Fix (MultI 1)) (Fix (MultI 3)))
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Routine recursion scheme surgery}
\begin{lstlisting}
-- Smart constructors! Yessssssss!
addI :: Integer -> AddLang'
addI = Fix . AddI

multI :: Integer -> MultLang'
multI = Fix . MultI

addAdd :: AddLang' -> AddLang' -> AddLang'
addAdd l r = Fix (AddF l r)

multMult :: MultLang' -> MultLang' -> MultLang'
multMult l r = Fix (MultF l r)

addLangExp' :: AddLang'
addLangExp' = addAdd (addI 1) (addI 3)

multLangExp' :: MultLang'
multLangExp' = multMult (multI 1) (multI 3)
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Routine recursion scheme surgery}
\begin{lstlisting}
interpAddF :: AddF Integer -> Integer
interpAddF = \case
  AddI i   -> i
  AddF l r -> l + r

interpMultF :: MultF Integer -> Integer
interpMultF = \case
  MultI i   -> i
  MultF l r -> l * r

interpAddLang' :: AddLang' -> Integer
interpAddLang' = fixFold interpAddF

interpMultLang' :: MultLang' -> Integer
interpMultLang' = fixFold interpMultF
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Routine recursion scheme surgery}
\begin{lstlisting}
{-# LANGUAGE TypeOperators #-}

data (f :+: g) x
  = Inl (f x)
  | Inr (g x)
  deriving ( Eq, Show, Functor )

-- Natural :+: eliminator
foldSum :: (f a -> a) -> (g a -> a) -> ((f :+: g) a -> a)
foldSum falg galg = \case
  Inl fa -> falg fa
  Inr ga -> galg ga
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{The prize!}
\begin{lstlisting}
type RingLang' = Fix (AddF :+: MultF)

-- More smart constructors!
ringI :: Integer -> AddLang'
ringI = Fix . Inl . AddI  -- why not via MultI?

ringAdd :: AddLang' -> AddLang' -> AddLang'
ringAdd l r = Fix (Inl (AddF l r))

ringMult :: MultLang' -> MultLang' -> MultLang'
ringMult l r = Fix (Inr (MultF l r))

ringLangExp' :: RingLang'
ringLangExp' = ringMult (ringAdd (ringI 3) (ringI 2)) (ringI 3)

interpRingLang' :: RingLang' -> Integer
interpRingLang' = fixFold (foldSum interpAddF interpMultF)
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
\begin{lstlisting}
Prelude> interpRingLang' ringLangExp'

\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
\begin{lstlisting}
Prelude> interpRingLang' ringLangExp'
15
\end{lstlisting}
\end{frame}

\plain{Yessssssss! HASKELL! \\ \textbackslash{}textbackslash o/}

\begin{frame}
  \frametitle{D. R. Y. Stats}
  \begin{center}
    40 additions, 3 new PRAGMAs

    \pause

    Used \texttt{Fix} in anger
  \end{center}
\end{frame}

\plain{Yessssssss! HASKELL! \\ (+1 Functional Programming)}

\begin{frame}
  \frametitle{In all seriousness...}
  Generalizing \lstinline{:+:}

  Wouter Swierstra, \textit{Data types \`a la carte}.

  Really cool.
\end{frame}

\section{Data types \`a la Carette, Kiselyov, and Shan}

\begin{frame}
  Let's try this again...
\end{frame}

\begin{frame}[fragile]
  \frametitle{Design by wishful thinking}
\begin{lstlisting}
class Adds v where
  add :: v -> v -> v

class Multiplies v where
  mult :: v -> v -> v
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
\begin{lstlisting}
instance Adds Integer where add = (+)
instance Multiplies Integer where mult = (*)

addsExp :: Integer
addsExp = add 1 3

multsExp :: Integer
multsExp = mult 1 3
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
\begin{lstlisting}
class FromInteger v where
  i :: Integer -> v

instance FromInteger Integer where
  i = id
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
\begin{lstlisting}
addsExp :: (FromInteger v, Adds v) => v
addsExp = add (i 1) (i 3)

multsExp :: (FromInteger v, Multiplies v) => v
multsExp = mult (i 1) (i 3)
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
\begin{lstlisting}
{-# LANGUAGE ConstraintKinds #-}

type Rings v = (FromInteger v, Adds v, Multiplies v)

ringsExp :: Rings v => v
ringsExp = mult (add (i 3) (i 2)) (i 3)
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
\begin{lstlisting}
Prelude> ringsExp

\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
\begin{lstlisting}
Prelude> ringsExp
15
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
\begin{lstlisting}
Prelude> ringsExp :: Integer
15
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{But then!}
\begin{lstlisting}
instance FromInteger RingLang where i    = RingLangIntLit
instance Adds        RingLang where add  = RingAdd
instance Multiplies  RingLang where mult = RingMult
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
\begin{lstlisting}
Prelude> ringsExp :: RingLang

\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
\begin{lstlisting}
Prelude> ringsExp :: RingLang
RingMult (RingAdd (RingLangIntLit 3)
                  (RingLangIntLit 2))
         (RingLangIntLit 3)
\end{lstlisting}
\end{frame}

\begin{frame}
  \frametitle{Recap}
  \begin{itemize}
  \pause
  \item One-line composabiity
  \pause
  \item Needn't mention concrete data types or \lstinline{Inl}/\lstinline{Inr}
    \pause
    \begin{itemize}
    \item ``tagless''
    \end{itemize}
  \pause
  \item But if we have them, we can still recover ``raw'' ASTs
  \end{itemize}
\end{frame}

\section{"Does is scale?"}

\begin{frame}
  Yes.
\end{frame}

\section{The "Monad Transformer Library"}

\begin{frame}
  Or: ``Haskell2010 Composable Effects Through Prolog Technology''
\end{frame}

\begin{frame}
  Or: ``Haskell2010 Composable Effects Through Typeclass Technology''
\end{frame}

\begin{frame}[fragile]
\begin{lstlisting}
import Control.Monad.State

inc :: MonadState Int m => m Int
inc = do
  count <- get
  set (count + 1)
  return count
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
\begin{lstlisting}
-- let's get a concrete stack from `transformers`
import Control.Monad.Trans.State (runState)

Prelude> flip runState 0 inc
(1, 1)
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
\begin{lstlisting}
-- let's get a concrete stack from `transformers`
import Control.Monad.Trans.State (runState)

Prelude> flip runState 0 (inc :: StateT Int Identity Int)
(1, 1)
\end{lstlisting}
\end{frame}

\begin{frame}
  Solve your monad stacks at compile time! \pause Not runtime\pause, nor write-time.
\end{frame}

\section{Complaints}

\begin{frame}[fragile]
  \frametitle{Just a few points in the lattice}
\begin{lstlisting}
MonadTrans (ContT r) Source
Monad (ContT r m) Source
Functor (ContT r m) Source
Applicative (ContT r m) Source
MonadIO m => MonadIO (ContT r m) Source
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{All points in the lattice}
\begin{lstlisting}
MonadCont (ContT r m) Source
\end{lstlisting}
  \pause
\begin{lstlisting}
MonadCont m => MonadCont (MaybeT m) Source
MonadCont m => MonadCont (ListT m) Source
MonadCont m => MonadCont (IdentityT m) Source
(Monoid w, MonadCont m) => MonadCont (WriterT w m) Source
(Monoid w, MonadCont m) => MonadCont (WriterT w m) Source
(Error e, MonadCont m) => MonadCont (ErrorT e m) Source
MonadCont m => MonadCont (ExceptT e m) Source
MonadCont m => MonadCont (StateT s m) Source
MonadCont m => MonadCont (StateT s m) Source
MonadCont m => MonadCont (ReaderT r m) Source
(Monoid w, MonadCont m) => MonadCont (RWST r w s m) Source
(Monoid w, MonadCont m) => MonadCont (RWST r w s m) Source
...
-- This gets quite complex
\end{lstlisting}
\end{frame}

\begin{frame}
  \frametitle{All points in the lattice}
  But this is just the name of the game! Effects don't commute.
\end{frame}

\begin{frame}[fragile,fragile]
  \frametitle{Lose control of ordering}
\begin{lstlisting}
op :: (MonadError e m, MonadState s m) => m ()
\end{lstlisting}
  \pause
\begin{lstlisting}
op ==> StateT s (Either e) ()
op ==> EitherT e (State s) ()
\end{lstlisting}
  \pause
No right answer!
\end{frame}

\begin{frame}
  \frametitle{Loose control of ordering}
  This is why we have laws.
  \pause
  Combining laws ``correctly'' \textit{is} hard.
\end{frame}

\begin{frame}[fragile]
  \frametitle{Loose control of ordering}
\begin{lstlisting}
class (MonadState s m, MonadError e m) => MonadParser s e m where {}
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Tight denotation of semantics}
\begin{lstlisting}
-- Tighten to your domain
class MonadParser m where
  type family Char m :: *
  type family Error m :: *

  peekChar :: m (Char m)
  getChar  :: m (Char m)

  failParse :: Error m -> m a
\end{lstlisting}
\end{frame}

\section{Thanks!}

\begin{frame}
  \frametitle{Tweet at me!}
  \begin{center}
    @sdbo
  \end{center}
\end{frame}

\end{document}

%%% Local Variables:
%%% coding: utf-8
%%% mode: latex
%%% TeX-engine: xetex
%%% End:


% !TEX root = ../thesis.texSecondly
%
\chapter{Conclusion}
\label{sec:conclusion}
In this thesis we present an zero-knowledge image retrieval system employing a \acrlong{fcn} for efficient object detection. With a set of transformed samples derived from a query image we fine-tune the underlying \gls{resnet}. Training is done without a training progress dependent abort condition. The linear classifier is converted to a convolution to predict sparse class probabilities on images of varying size. Using these probability maps we predict the existence of alike objects in images. We yield these patches and thereby we retrieve object instances from image set related to the training samples.

With our results on \textsc{Pascal}-Part and the application on art historical images we provide evidence that this approach can carry out image retrieval in unlabeled, context-less images using a small number of examples, ultimately only a single one. On the one hand, the tests on \textsc{Pascal}-Part verify the supposition, that providing the network with more positives image samples drastically improves its detection results, on the other hand we showed that under our test settings, results quickly converged over an increasing number of examples.\\
Although we prove the methods effectiveness we recognize multiple drawbacks. Firstly the time complexity \tref{sec:results:time} restricts its use on small datasets of images and makes instance retrieval somewhat cumbersome. Secondly our region scoring method throughout under-scales object boxes thus serving more as a translation prediction.\\
The approach could be continued and improved on multiple different ways. Of most importance is the unanswered question, how to use the image representation provided by the deep filter of a \gls{cnn} to bootstrap an approximate object detector without having to actually iteratively train the network. We could not test any approaches in this domain and as such can not predict a possible solution.\\
To fasten the approach it may be possible to formulate the complete pipeline inside the neural network, reducing time costs. Taking densities of regions is nothing else than average pooling and thus could be done within the network after convolutional upsampling.

All code is made publicly available under \url{https://github.com/mrtukkin/bachelor-thesis}.


\documentclass{article}

\usepackage{xstring}
\usepackage{calc}
\usepackage{xintexpr}
\usepackage{arrayjobx}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usepackage{etoolbox}

\title{Advent of Code - Day 8}
\author{Sansero}
\date{July 2020}

\newcommand\imgwidth{25}
\newcommand\imgheight{6}
\newcommand\pixelcount{0}

\renewcommand\pixelcount{\the\numexpr \imgwidth * \imgheight}

\typeout{moin}

\newread\file
\openin\file=input.txt
\read\file to \input % Reads a line of the file into \input
\closein\file

\typeout{Loaded file}

\newarray\bitmap
\expandarrayelementtrue

\newcommand\updatelayer[1]{
    \foreach \i in {1,...,\pixelcount}{
        \StrMid{#1}{\i}{\i}[\char]
        \IfEq{\char}{0}{
            \updatearray{\i}{0}
        }{
            \IfEq{\char}{1}{
                \updatearray{\i}{1}
            }{
                \IfEq{\char}{2}{
                    \updatearray{\i}{2}
                }{}
            }
        }
    }
}

\newcommand\updatearray[2]{
    \checkbitmap(#1)
    \ifemptydata
        \bitmap(#1)={#2}
    \else
        \if 2\cachedata \bitmap(#1)={#2} \fi
    \fi
}

\newcount\minzero
\newcount\minlayer
\minzero=\pixelcount
\newcount\onetimestwo
\newcount\ones
\newcount\twos
\newcount\layernr

\newcount\counter
\counter=0
\loop
    \typeout{\the\counter}
    \StrLeft{\input}{\pixelcount}[\layer]
    \StrGobbleLeft{\input}{\pixelcount}[\input]
    \StrLen{\layer}[\layerlength]
    \ifnum \layerlength=\pixelcount
        \updatelayer{\layer}
        \StrCount{\layer}{0}[\zerocount]
        \ifnum \zerocount<\minzero
            \StrCount{\layer}{1}[\onecount]
            \StrCount{\layer}{2}[\twocount]
            \minzero=\zerocount
            \minlayer=\layernr
            \ones=\onecount
            \twos=\twocount
            \onetimestwo=\the\numexpr \onecount * \twocount \relax
        \fi
    \fi
    \advance \counter \pixelcount
    \advance \layernr 1
\ifx\input\empty
\else
\repeat

\begin{document}

\maketitle

\section{Part 1}

Error detection. Get the layer with the fewest zeros

Layer: \the\minlayer

Zeros: \the\minzero

$1*2$: \the\onetimestwo

Ones: \the\ones

Twos: \the\twos

\section{Part 2}

The final image

% 0: black
% 1: white
% 2: transparent

\begin{tikzpicture}[]
  \tikzstyle{every node}=[minimum size=3mm]
  \tikzset{pre/.style={draw,fill=black, text=white}}
  \let\mymatrixcontent\empty
  \foreach \i in {0,...,\the\numexpr \imgheight -1} {%
      \foreach \y in {1,...,\imgwidth} {%
        \checkbitmap(\the\numexpr \i*\imgwidth + \y)%
        \if 0\cachedata
            \edef\x{
            \noexpand\gappto\noexpand\mymatrixcontent{ \noexpand\node[pre, fill=black, text=black]{0}; \&}}\x
        \else
            \if 1\cachedata
                \edef\x{
                \noexpand\gappto\noexpand\mymatrixcontent{ \noexpand\node[pre, fill=white]{1}; \&}}\x
            \else
                \if 2\cachedata
                    \edef\x{
                    \noexpand\gappto\noexpand\mymatrixcontent{ \noexpand\node[pre, fill=blue]{2}; \&}}\x
                \else
                    \edef\x{
                    \noexpand\gappto\noexpand\mymatrixcontent{ \noexpand\node[pre, fill=red]{e}; \&}}\x
                \fi
            \fi
        \fi
      }
      \gappto\mymatrixcontent{\\}%
  }

  \matrix[matrix of math nodes,%
      nodes = {pre},%
      ampersand replacement=\&] {%
    \mymatrixcontent
  };
\end{tikzpicture}

\end{document}


% \newcommand{\nconfirmed}{500}
\newcommand{\dantodo}[1]{{\color{blue}#1}}

\section{Method}
\label{sec:method}

\subsection{Rotation period measurement}

Since the \nconfirmed\ comoving stars identified in TGAS and confirmed with RV
follow-up we consider here are all \Kepler\ targets with precise light curves
covering around 4 years, we measured photometric rotation periods for these
stars.
We applied three different methods to the light curves: a Lomb-Scargle
periodogram method, an autocorrelation method and a Gaussian process method.
The exact implementations of the Lomb-Scargle and Gaussian process methods
were also used in the \citet{Angus2017} rotation period study.
In that work we found that the Gaussian process method produces slightly more
accurate rotation periods than the periodogram method and substantially more
accurate periods than the autocorrelation method for \kepler\ data.
However, we also found that each rotation period measurement method is very
sensitive to the exact implementations, for example any choices of heuristics
or priors affected the resulting rotation periods for an ensemble of light
curves.
For this reason we use all three methods here to ensure that our results are
not significantly altered by method choice.
We describe the implementation of each method below.

To measure periods using a Lomb-Scargle periodogram we first applied a
high-pass filter to the light curves to remove long-term trends.
We used the {\tt scipy} 3rd order Butterworth filter with a 35 day cut-off,
attenuating signals with periods greater than this threshold.
For each simulated light curve, we computed a LS periodogram\footnote{LS
periodograms were calculated using the {\tt scipy} Lomb-Scargle algorithm}
over a grid of 10,000 periods, evenly spaced in frequency, between 1 and 100
days.
We adopted the period of the highest peak in the periodogram as the measured
rotation period.
The uncertainties on the rotation periods were calculated using the
following equation for the standard deviation of the frequency
\citep{Horne1986, Kovacs1981}:
\begin{equation}
    \sigma_{\nu} = \frac{3\pi\sigma_N}{2N^{1/2}TA},
\end{equation}
where $A$ is the amplitude of the signal of highest power, $\sigma_N$ is the
variance of the time series, with the signal of highest power removed, $N$
is the number of observations and $T$ is the timespan of the data.
These formal uncertainties are only valid in the case that the noise is white,
the data are evenly sampled and there is only one signal present.
Since there are multiple signals present in these light curves, this formal
uncertainty is an underestimate of the true uncertainty.

\racomment{Add section on ACF method.}

The implementation of the Gaussian process method we use here is described and
discussed in detail in \citet{Angus2017}.
Given its success in \citet{Angus2017}, we chose to use the same kernel
function:
\begin{equation}
\label{eq:QP}
k_{i,j} = A \exp \left[-\frac{(x_i - x_j)^2}{2l^2} -
    \Gamma^2 \sin^2\left(\frac{\pi(x_i - x_j)}{P}\right) \right] + \sigma^2
    \delta_{ij},
\end{equation}
where $A$ is the covariance amplitude, $l$ is the length-scale of overall
covariance decay, $\Gamma$ is the parameter controlling
the intra-period variation (large $\Gamma$ permits more zero-crossings), $P$
is the rotation period and $\sigma$ is the additional white noise needed to
account for errorbar uncertainties.
The priors on each of these parameters takes a relatively simple functional
form, except for the period prior which is built from an initial
autocorrelation function guess.
These priors are fully described in \citet{Angus2017} and we will not go into
further detail here.

The rotation periods measured using these three methods are in good agreement:
N\% of periods agree to within 10\%.

\subsection{Age prediction}

Since we are assuming that the comoving stars are coeval we expect their
rotation periods to reflect this.
To quantify the rate of age-matches,

\subsection{Inferring intrinsic dispersion in the gyrochronology
relations}


\chapter[Bremsstrahlung]{Bremsstrahlung}

 The class G4eBremsstrahlung calculates the continuous energy loss
  due to {\em sub-cutoff} photons and
 simulates the 'discrete' part of the interaction ,
the bremsstrahlung  by $e^-/e^+$ . The formulae used here
 are the very similar to those used in GEANT3 (\cite{ebrem.geant3}).
 The only {\em important} difference is that in GEANT4 both of the LPM
effect and dielectric supression of the bremsstrahlung
(\cite{ebrem.galitsky},\cite{ebrem.anthony}) have been implemented,
while GEANT3 contains the latter only (using the name Migdal correction
for it) .

\section{Cross section and energy loss}

Let's call $d\sigma(Z,T,k)/dk$ the differential cross-section for
production of a photon of energy $k$ by an electron of kinetic energy
$T$ in the field of an atom of charge $Z$, and
$k_c$ the energy cut-off below which the soft photons are treated
as continuous energy loss .
Then the mean value of the energy lost by the electron due
to soft photons is
\begin{equation}
  E_{Loss}^{brem} (Z,T,k_c ) =
\int_{0}^{k_ c}k\frac{d \sigma (Z,T,k)}{dk}dk
\end{equation}
whereas the total cross-section for the emission of
a photon of energy larger than $k_c$ is
\begin{equation}
 \sigma_{brem} (Z,T,k_c ) = \int_{k_c}^{T}\frac{d \sigma (Z,T,k)}{dk} dk
\end{equation}
Many theories of the bremsstrahlung process exist, each with its own
limitations and regions of applicability. Perhaps the best synthesis
of these theories can be found in the paper of S.M. Seltzer and M.J.
Berger \cite{ebrem.seltzer}. The authors give a tabulation of the
bremsstrahlung cross-section $d\sigma/dk$ differential in the photon
energy $k$, for electrons with kinetic energies $T$ from 1 keV to 10
GeV. For electron energies above 10 GeV the screened Bethe-Heitler
differential cross-section can be used
(\cite{ebrem.egs4}
together with the dielectric supression
correction (\cite{ebrem.messel} , \cite{ebrem.migdal} ) .
  This correction
 decreases the differential cross-section at photon
energies below a certain fraction of the incident electron energy
($d\sigma/dk$ decreases significantly if $ k/T \leq 10^{-4}$.)

\subsection{Parameterisation of energy loss and total cross-section}
Using the tabulated cross-section values of Seltzer and Berger together
with the corrected Bethe-Heitler formula (which so includes the
 dilelectric supression,too) we have computed
$\sigma(Z,T,k_c ) $
and we have used these computed values as ``data points''
in the fitting procedure.
Calculating the ``low energy'' ($T\leq 10$ GeV) data we have applied the
dielectric supression to the results of Seltzer and Berger.
We have chosen the parameterisations:
\begin{equation}
\label{ebrem.a}
\sigma (Z,T,k_c ) =\frac{Z(Z+\xi_{\sigma} )(T+m)^2}
     {T(T+2m)}[ \ln (T/k_c )]^\alpha F_{\sigma} (Z,X,Y)
\mbox{\quad(barn)}\\
\end{equation}
and
\begin{equation}
\label{ebrem.b}
E_{Loss}^{brem} (Z,T,k_c ) =\frac{Z(Z+ \xi_l)(T+m)^2 }
      {(T+2m)}\left[\frac{k_c C_ M }{T}\right]^\beta
      F_{l} (Z,X,Y) \mbox{\quad(GeV barn)}
\end{equation}
where  $m$ is the mass of the electron,
\[
\begin{array}{lll}
X = \ln(E/m), & Y = \ln (v_{\sigma} E/k_c) &
\mbox{\quad for the total cross-section }\sigma \\
X = \ln(T/m), & Y = \ln(k_c/v_lE)       &
\mbox{\quad for the energy loss }  E_{Loss}^{brem}  \\
\end{array}
\]
with $E=T+m$.
The constants $\xi_{\sigma},\ \xi_l,\ \alpha,\ \beta,\ v_{\sigma},\ v_l$
are parameters to be fitted.
\begin{eqnarray*}
C_M &=&\frac{1}{1+\frac{n r_0 \lambda r^2  (T+m)^2}{\pi k^2_c}}
\end{eqnarray*}
is the Midgal correction factor, with
\begin{description}
\item $r_o$  classical electron radius;
\item $\lambda$  reduced electron Compton wavelength;
\item $n$  electron density in the medium.
\end{description}
The factors $(T+m)^2/T(T+2m)$ and $(T+m)^2/(T+2m)$ come from
the scaled cross-section computed by Seltzer and Berger:
\[
f(k/T) = \frac{\beta^2}{Z^2 }k\frac{d \sigma}{dk}
       = \frac{T(T+2m)} {(T+m)^2 Z^2}  k \frac{d \sigma}{dk}
\]

The functions $F_i(Z,X,Y)$ ($ i=\sigma,l $) have the form
\begin{equation}
 F_i(Z,X,Y) = F_{i0}(X,Y)+ZF_{i1}(X,Y)
\end{equation}
where $F_{ij}(X,Y)$ are polynomials of the variables $X,Y$
\begin{eqnarray}
F_{i0}(X,Y) & = &  (C_1+C_2X+\ldots+C_6X^5)+(C_7+C_8X+\ldots+C_{12}X^5)Y
                                                             \nonumber \\
          &   & +(C_{13}+C_{14}X+\ldots + C_{18}X^5)Y^2+\ldots \nonumber \\
          &   & +(C_{31}+C_{32}X+\ldots+C_{36}X^5)Y^5        \nonumber \\
          &   & \mbox{\hspace{11cm}}  Y\leq 0      \nonumber \\
          & = &  (C_1+C_2X+\ldots+C_6X^5)+(C_7+C_8X+\ldots+C_{12}X^5)
                                                             \nonumber \\
          &   & +(C_{37}+C_{38}X+\ldots+C_{42}X^5)Y^2+\ldots+ \nonumber \\
          &   &  (C_{55}+C_{56}X+\ldots+C_{60}X^5)Y^5        \nonumber \\
          &   & \mbox{\hspace{11cm}}  Y > 0        \nonumber \\
F_{i1}(X,Y) & = &  (C_{61}+C_{62}X+\ldots+C_{65}X^4)
                +(C_{66}+C_{67}X+\ldots+C_{70}X^4)Y          \nonumber \\
          &   & +(C_{71}+C_{72}X+\ldots+C_{75}X^4)Y^2+\ldots \nonumber \\
          &   & +(C_{81}+C_{82}X+\ldots+C_{85}X^4) Y^4       \nonumber \\
          &   &   \mbox{\hspace{11cm}}  Y\leq 0    \nonumber \\
          & = &  (C_{61}+C_{62}X+\ldots+C_{65}X^4)
                +(C_{66}+C_{67}X+\ldots+C_{70}X^4)Y          \nonumber \\
          &   & +(C_{86}+C_{87}X+\ldots+C_{90}X^4)Y^2+\ldots \nonumber \\
          &   & +(C_{96}+C_{97}X+\ldots+C_{100}X^4)Y^4       \nonumber  \\
          &   &  \mbox{\hspace{11cm}}  Y>0         \nonumber \\
           \nonumber
\end{eqnarray}
$F_{ij}(X,Y)$ denotes in fact a function constructed from two
polynomials
\[
F_{ij}(X,Y) = \left \{
\begin{array}{ll}
F_{ij}^{neg}(X,Y) & \mbox{for\hspace{0.5cm}} Y\leq 0 \\
F_{ij}^{pos}(X,Y) & \mbox{for\hspace{0.5cm}} Y>0
\end{array} \right .
\]
where the polynomials $F_{ij}$ fulfil the conditions
\[
F_{ij}^{neg}(X,Y)_{Y=0} = F_{ij}^{pos}(X,Y)_{Y=0}   \hspace{2cm}
\left ( \frac{\partial F_{ij}^{neg}}{\partial Y} \right )_{Y=0} =
\left ( \frac{\partial P_{ij}^{pos}}{\partial Y} \right )_{Y=0}
\]
We have computed 4000 ``data points'' in the range
\[
Z = 6  ;  13  ;  29  ;  47  ;  74  ;  92  \hspace{1cm}
10 \: \mbox{keV} \leq T \leq 10 \: \mbox{TeV}  \hspace{1cm}
10 \: \mbox{keV} \leq k_c \leq T
\]
and we have performed a least-squares fit to determine the parameters.

The values of the parameters ($\xi_{\sigma}$, $\alpha$,
$v_{\sigma}$, $C_i $ for $\sigma$ and
$\xi_l$, $\beta$, $V_l$, $C$ for $E_{Loss}^{brem}$)
can be found in the code of the class itself.

The errors of the parameterisations (\ref{ebrem.a}) and (\ref{ebrem.b})
can be estimated as

\begin{eqnarray*}
\frac{\Delta\sigma} {\sigma} & = & \left \{
\begin{array}{llr}
        12-15\%    & \mbox{for    } & T \leq 1 MeV \\
        \leq 5-6\% & \mbox{for    } & 1 MeV < T \leq 10 TeV
\end{array}
\right . \\[1cm]
\frac{\Delta E_{Loss}^{brem}}
     {E_{Loss}^{brem}} & = & \left \{
\begin{array}{llr}
        10-15\%    & \mbox{for    } & T \leq1 MeV  \\
        5-6\%      & \mbox{for    } & 1 MeV < T \leq 100 GeV\\
        10\%       & \mbox{for    } & 100 GeV < T \leq10 TeV
\end{array}
\right .
\end{eqnarray*}

The energy loss due to soft photon bremsstrahlung is tabulated at
initialisation time as a function of the medium and of the energy.

The mean free path for discrete bremsstrahlung is tabuled at initialisation
time as a function
of the medium and of the energy, too.

\subsection{Corrections for $e^+ e^-$ differences}

The radiative energy loss for electrons or positrons is:

\begin{eqnarray*}
-\frac{1}{\rho} \left ( \frac{dE}{dx} \right )_{rad}^{\pm} & = &
\frac{N_{Av} \alpha r_e^2}{A} (T+m) Z^2 \Phi_{rad}^{\pm}(Z,T) \\
\Phi^{\pm}_{rad}(Z,T) & = & \frac{1}{\alpha r_{e}^2 Z^2 (T+m)}
\int^{T}_{0}{k\frac{d\sigma^{\pm}}{dk}dk}
\end{eqnarray*}

Reference \cite{ebrem.kim} says that: \\
{\it ``The differences between the radiative loss of positrons
and electrons are considerable and cannot be disregarded.

[...] The ratio of the radiative energy loss for positrons
to that for electrons obeys a simple scaling law, [...] is a
function only of the quantity $T/Z^2$''}

In other words:

\begin{eqnarray*}
\eta & = & \frac{\Phi_{rad}^{+}(Z,T)}{\Phi_{rad}^{-}(Z,T)} =
\eta \left (\frac{T}{Z^2}\right )
\end{eqnarray*}

The authors have calculated this function in the range $10^{-7}
\leq \frac{T}{Z^2} \leq 0.5$ (here the kinetic energy T is
expressed in MeV). Their {\it data} can be fairly accurately
reproduced using a parametrisation:

\begin{eqnarray*}
\eta & = & \left \{
\begin{array}{llr}
0 & \mbox{if   } & x \leq -8 \\
\frac{1}{2} + \frac{1}{\pi} \arctan \left( a_1 x + a_3 x^3
+ a_5 x^5 \right ) & \mbox{if  } & -8 < x < 9 \\
1 & \mbox{if   } & x \geq 9
\end{array}
\right .
\end{eqnarray*}

where:

\begin{eqnarray*}
x & = & \log \left ( C \frac{T}{Z^2} \right ) \mbox{(T in GeV)} \\
C & = & 7.5221 \times 10^{6} \\
a_1 & = & 0.415 \\
a_3 & = & 0.0021 \\
a_5 & = & 0.00054
\end{eqnarray*}

This $e^+ e^-$ energy loss difference is not a pure low-energy
phenomenon (at least for high Z), as it can be seen from
Tables~\ref{ebrem.c}.

\begin{table}[hbt]
\begin{centering}
\begin{tabular}{rr|r|r} \hline
\multicolumn{1}{c}{$\frac{T}{Z^2} (GeV)$}
& \multicolumn{1}{c|}{T}
& \multicolumn{1}{c|}{$\eta$}
& \multicolumn{1}{c}{$\left ( \frac{rad. \ loss}{total \ loss}
\right )_{e^-}$} \\[3mm] \hline
$10^{-9}$ & $\sim 7 keV$ & $\sim 0.1$ & $\sim 0\%$ \\
$10^{-8}$ & $67 keV $ & $\sim 0.2$ & $\sim 1\%$ \\
$2 \times 10^{-7}$ & $1.35 MeV$ & $\sim 0.5$ & $\sim 15\%$ \\
$2 \times 10^{-6}$ & $13.5 MeV$ & $\sim 0.8$ & $\sim 60\%$ \\
$2 \times 10^{-5}$ & $135. MeV$ & $\sim 0.95$ & $> 90\%$ \\ \hline
\end{tabular}
\caption{ratio of the $e^+ e^-$ radiative energy loss in lead
(Z=82).}
\label{ebrem.c}
\end{centering}
\end{table}

The scaling holds for the ratio of the total radiative energy
losses, but it is significantly broken for the photon spectrum
in the screened case.
In case of a point Coulomb charge the
scaling would hold also for the spectrum.
The scaling can be expressed by:

\begin{eqnarray*}
\frac{\Phi^+}{\Phi^-} = \eta \left ( \frac{T}{Z^2} \right )
& \hspace{3cm} &
\frac{\frac{d\sigma^+}{dk}}{\frac{d\sigma^-}{dk}} =
\mbox{does not scale}
\end{eqnarray*}

If we consider the photon spectrum from bremsstrahlung reported
in \cite{ebrem.kim} we see that:

\begin{eqnarray*}
\frac{d\sigma^{\pm}}{dk} = S^{\pm} \left( \frac{k}{T} \right )
\hspace{2cm}
\frac{S^{+}(k)}{S^{-}(k)} \leq 1 & \hspace{1cm} & S^{+}(1) = 0
\hspace{2cm} S^{-}(1)  >  0
\end{eqnarray*}

We further assume that:

\begin{eqnarray}
\frac{d\sigma^+}{dk} = f(\epsilon) \frac{d\sigma^-}{dk}
& \hspace{2cm} &
\epsilon = \frac{k}{T}
\label{ebrem.d}
\end{eqnarray}

In order to satisfy approximately the scaling law for the ratio
of the total radiative energy loss, we require for $f(\epsilon)$:

\begin{eqnarray}
\int^{1}_{0}{f(\epsilon)d\epsilon} & = & \eta
\label{ebrem.e}
\end{eqnarray}

From the photon spectra we require:

\begin{eqnarray}
\left .
\begin{array}{l}
f(0) = 1 \\
f(1) = 0
\end{array}
\right \} \hspace{2cm} \mbox{for all $Z,T$}
\label{ebrem.f}
\end{eqnarray}

We have chosen a simple function $f$:

\begin{eqnarray}
f(\epsilon) & = & C (1-\epsilon)^{\alpha} \hspace{3cm} C,\alpha > 0
\label{ebrem.g}
\end{eqnarray}

from the conditions (\ref{ebrem.e}), (\ref{ebrem.f}) we get:

\begin{eqnarray*}
C & = & 1 \\
\alpha & = & \frac{1}{\eta} - 1 \hspace{2cm}
\mbox{($\alpha > 0$ because $\eta < 1$)} \\
f(\epsilon) & = & (1-\epsilon)^{\frac{1}{\eta}-1}
\end{eqnarray*}

We have defined weight factors $F_{l}$ and $F_{\sigma}$ for the
positron continuous energy loss and discrete bremsstrahlung cross
section:

\begin{eqnarray}
F_{l} = \frac{1}{\epsilon_{0}} \int^{\epsilon_{0}}_{0}
{f(\epsilon)d\epsilon} & \hspace{3cm} &
F_{\sigma} = \frac{1}{1-\epsilon_{0}} \int^{1}_{\epsilon_{0}}
{f(\epsilon)d\epsilon}
\label{ebrem.h}
\end{eqnarray}

where $\epsilon_{0} = \frac{k_c}{T}$ and $k_c$ is the photon
cut . In this scheme the positron energy loss and
discrete bremsstrahlung can be calculated as:

\begin{eqnarray*}
\left ( - \frac{dE}{dx} \right )^{+} = F_{l}
\left ( - \frac{dE}{dx} \right )^{-} & \hspace{2cm} &
\sigma^{+}_{brems} = F_{\sigma} \sigma^{-}_{brems}
\end{eqnarray*}

As in this approximation the photon spectra are identical, the
same sampling is used for generating $e^- e^+$ bremsstrahlung.
The following relations hold:

\begin{eqnarray*}
F_{\sigma} & = & \eta (1-\epsilon_{0})^{\frac{1}{\eta}-1}
< \eta \\
\epsilon_{0} F_{l} + (1-\epsilon_{0}) F_{\sigma} & = & \eta
\hspace{6cm} \mbox{from the def (\ref{ebrem.h})} \\
\Rightarrow F_{l} & = & \eta \frac{1-(1-\epsilon_{0})^{\frac{1}
{\eta}})}{\epsilon_{0}} > \eta \frac{1-(1-\epsilon_{0})}
{\epsilon_{0}} = \eta  \hspace{1cm}
\Rightarrow   \left \{
\begin{array}{l}
F_{l} > \eta \\
F_{\sigma} < \eta
\end{array} \right .
\end{eqnarray*}

which is consistent with the spectra.

The effect of this $e^- e^+$ bremsstrahlung difference can be also
seen in e.m. shower development, when the primary energy is not too
high.

\subsection{LPM effect}

The LPM effect (see e.g in \cite{ebrem.galitsky},\cite{ebrem.anthony} )
 is the supression of the photon production due to the
multiple scattering of the electrons.If an electron multiple scatters
while traversing the co called formation zone, the bremsstrahlung
amplitude from before and after the scattering can interfere, reducing the
amplitude for bremsstrahlung photon emission (similar supression occurs
for pair production,too). The supression becomes significant for photon
energies below a certain value, given by
\begin{equation}
\label{ebrem.k}
 \frac{k}{E} < \frac{E}{E_{LPM}}
\end{equation}
 , where
\[
\begin{array}{ll}
k    & \mbox{photon energy} \\
E    & \mbox{electron energy} \\
E_{LPM} & \mbox{characteristic energy for LPM effect (depend on the medium).}
\end{array}
\]

The value of the LPM characteristic energy can be written as

\begin{equation}
\label{ebrem.l}
  E_{LPM} = \frac{\alpha m^2 X_0}{4 h c}
\end{equation}

 , where
\[
\begin{array}{ll}
\alpha  & \mbox{fine structure constant} \\
m       & \mbox{electron mass} \\
X_0     & \mbox{radiation length in the material} \\
h       & \mbox{Planck constant} \\
c       & \mbox{velocity of light in vacuum.}
\end{array}
\]

 The LPM supression of the photon spectrum is given by the formula

\begin{equation}
\label{ebrem.m}
  S_{LPM} = \sqrt{\frac{E_{LPM} \cdot k}{E^2}}
\end{equation}

, while the dielectric supression (included already in the parametrisations)
can be written as

\begin{equation}
\label{ebrem.n}
  S_p = \frac{k^2}{k^2 + C_p \cdot E^2}
\end{equation}

 , where the quantity $C_p$ is given by

\begin{equation}
\label{ebrem.o}
   C_p = \frac{r^2_0 \lambda^2_e n}{\pi}
\end{equation}

In eq. \ref{ebrem.o} the parameters are
\[
\begin{array}{ll}
r_0     & \mbox{classical electron radius} \\
\lambda_e & \mbox{electron Compton wawelength} \\
n       & \mbox{electrondensity in the material.}
\end{array}
\]

 Both of the supression effects reduce the effective formation length
of the photon , so the supressions {\em do not simply multiply.} For the
total supression S the following equation holds (see \cite{ebrem.galitsky})
\begin{equation}
\label{ebrem.p}
  \frac{1}{S} = 1 + \frac{1}{S_p} + \frac{S}{S^2_{LPM}}
\end{equation}
 which can be solved easily for $S$
\begin{equation}
\label{ebrem.q}
  S = \frac{\sqrt{S^4_{LPM}\cdot (1 + \frac{1}{S_p})^2 + 4 \cdot S^2_{LPM}}
      -S^2_{LPM} \cdot (1 + \frac{1}{S_p})}{2}
\end{equation}

 The implementation of the LPM effect has been done in the following way:
The cross section and energy loss should be modified by applying the
 (energy and material dependent ) factor $\frac{S}{S_p}$ . This is done
at initialisation time for the energy loss by computing the correction
factor
\begin{equation}
\label{ebrem.r}
   f_c = \frac{\int_0^{k_cut} n_\gamma (k) \cdot \frac{S}{S_p} dk}
              {\int_0^{k_cut} n_\gamma (k) dk}
\end{equation}
 , wherea $n_\gamma (k)$ is the photon specrum .
 The total cross section given by the parametrization \ref{ebrem.a} has
not been modified , the algorithm of the photon generation takes care
of the LPM effect.

\section{Simulation of the discrete bremsstrahlung}

The photon energy is sampled according to the Seltzer and Berger
bremsstrahlung spectrum~\cite{ebrem.seltzer}.
 Seltzer and Berger have calculated the
spectra for materials with atomic numbers Z = 6,13,29,47,74,92
in the electron (kinetic) energy range  1 keV - 10 GeV. Their tabulated
results have been used as input in a parametrising-fitting procedure.
The functional form of the parameterisation for the quantity:

\[
S(x) = C k \frac{d \sigma}{d k}
\]

can be written as
\begin{equation}
\label{eq:phys341-1}
S(x) = \left \{
\begin{array}{ll}
(1-a_{h} \epsilon )F_{1}(\delta) + b_{h} \epsilon^{2} F_{2} (\delta)
& T \geq 1 MeV \\
1 + a_{l} x + b_{l} x^{2} & T < 1 MeV
\end{array} \right .
\end{equation}
where:
\[
\begin{array}{lcllcl}
C & & \mbox{normalisation constant} &
k & & \mbox{photon energy} \\ [1mm]
T, E & & \mbox{kinetic and total energy of the primary electron} &
x & = & \frac{k}{T} \\ [2mm]
\epsilon & = & \frac{k}{E} = x \frac{T}{E}
\end{array}
\]

The $F_{i}(\delta)$ screening functions depend on the screening variable:
\[
\begin{array}{lcll}
\delta & = & \frac{136 m_{e}}{Z^{1/3} E} \frac{\epsilon}{1-\epsilon} \\
F_{1}(\delta) & = & F_{0} (42.392 - 7.796 \delta +1.961 \delta^{2} - F)
& \delta \leq 1 \\
F_{2}(\delta) & = & F_{0} (41.734 - 6.484 \delta +1.250 \delta^{2} - F)
& \delta \leq 1 \\
F_{1}(\delta) & = & F_{2}(\delta) =
F_{0} (42.24 - 8.368 \ln(\delta + 0.952) -F) & \delta > 1 \\
F_{0} & = & \frac{1}{42.392-F} \\
F & = & 4 \ln Z - 0.55 (\ln Z)^{2}
\end{array}
\]

$a_{h,l}$ and $b_{h,l}$ are parameters to be fitted.

The `high energy' (T $>$  1 MeV) formula comes from the
Coulomb-corrected, sceened Bethe-Heitler formula (see e.g.
\cite{ebrem.williams,ebrem.butcher,ebrem.egs4}). However, there are two things in eq.
(\ref{eq:phys341-1}) which make a difference:

\begin{enumerate}
\item $a_{h}, b_{h}$ depend on T and on the atomic number Z ( in the case
of the Bethe-Heitler spectrum $a_{h} = 1$, $b_{h} =0.75$);
\item the function $F$ is not the same than that in the Bethe-Heitler
cross-section, this function gives a better behaviour in the
high frequency limit, i.e. when $k \rightarrow T$  ($x \rightarrow 1$).
\end{enumerate}

The T and Z dependence of the parameters are described by the equations:

\begin{eqnarray*}
a_{h} & = & 1 + \frac{a_{h1}}{u}+\frac{a_{h2}}{u^{2}}+\frac{a_{h3}}{u^{3}} \\
b_{h} & = & 0.75+\frac{b_{h1}}{u}+\frac{b_{h2}}{u^{2}}+\frac{b_{h3}}{u^{3}} \\
a_{l} & = & a_{l0} + a_{l1} u + a_{l2} u^{2} \\
b_{l} & = & b_{l0} + b_{l1} u + b_{l2} u^{2} \\
\mbox{with} \\
u & = & \ln \left ( \frac{T}{m_{e}} \right )
\end{eqnarray*}

the $a_{hi}, b_{hi}, a_{li}, b_{li}$ parameters are polynomials of second order
in the variable:

\[
v = [Z (Z+1)]^{1/3}
\]

It can be seen relatively easily that for the limiting case $T \rightarrow
\infty$, $a_{h} \rightarrow 1, b_{h} \rightarrow 0.75$,
so eq. (\ref{eq:phys341-1}) gives the Bethe-Heitler cross section.

There are altogether 36 linear parameter in the formulae , their
values are given in the code. The parameterisation reproduces
the Seltzer-Berger tables within a few \% (2-3 \% on average,
the maximum error being less than 10-12 \%), the tables, on the other hand,
agree well with the experimental data and theoretical (low- and high-energy)
results (less than 10 \% below 50 MeV, less than 5 \% above 50 MeV).

Apart from the normalisation the cross section differential in photon
energy can be written as:
\[
\frac{d \sigma}{d k} = \frac{1}{\ln \frac{1}{x_{c}}} \frac{1}{x}
g(x) = \frac{1}{\ln \frac{1}{x_{c}}} \frac{1}{x} \frac{S(x)}{S_{max}}
\]

where $x_{c} = k_{c}/T$, $k_{c}$ is the photon cut-off energy below
which the bremsstrahlung is treated as a continuous energy loss
. Using this decomposition of
the cross section and two random numbers $r_{1}$, $r_{2}$ uniformly
distributed in $]0,1[$, the sampling of $x$ is done as follows:
\begin{enumerate}
\item sample $x$ from
\[
\frac{1}{\ln \frac{1}{x_{c}}} \frac{1}{x} \mbox{\hspace{1cm}setting\hspace{1cm}}
x = e^{r_{1} \ln x_{c}}
\]

\item calculate the rejection function $g(x)$ and:
\begin{itemize}
\item if $r_{2} > g(x)$ reject $x$ and go back to 1;
\item if $r_{2} \leq g(x)$ accept $x$.
\end{itemize}
\end{enumerate}

To apply the dielectric supression  \cite{ebrem.migdal}
  all it has to be done is to
multiply the rejection function by the supression factor , which is
equivalent to the expression \ref{ebrem.n}:

\[
C_M (\epsilon)  =\frac{1 + C_0 / \epsilon_c^2}
               {1 + C_0 / \epsilon^2}
\]
 where
\[
C_0 =\frac{nr_0 \lambda^2 }{\pi}, \hspace{1cm} \epsilon_c = \frac{k_{c}}{E}
\]
\begin{itemize}
\item[$n$]           electron density in the medium
\item[$r_0$]         classical electron radius
\item[$\lambda$]    reduced Compton wavelength of the electron.
\end{itemize}
This correction decreases the cross-section for low photon energy.

 After having $\epsilon$ sampled , the supression factor $f_{LPM}=\frac{S}{S_p}$ is
used as a rejection function in order to take into account the LPM effect , too.
Here the supression factor is compared to a random number $r$ uniformly
distributed in the interval $(0,1)$. If  $f_{LPM} \geq r$ the simulation
is continuing otherwise the bremsstrahlung process is finished
{\em without photon production } .It can be seen easily that this procedure
takes into account the LPM supression correctly.

After the successful sampling of $\epsilon$,  the
polar angles of the radiated photon are generated with respect to the parent
electron's momentum. It is difficult to find in the literature
simple formulas for this angle. For example the double differential
cross section reported by Tsai~\cite{ebrem.tsai1,ebrem.tsai2} is the
following:
\begin{eqnarray*}
\frac{d \sigma}{dkd \Omega}
& = & \frac{2 \alpha^{2}e^{2}}{\pi k m^{4}}
  \left\{ \left[ \frac{2\epsilon-2}{(1+u^2)^2}+
\frac{12u^2(1-\epsilon)}{(1+u^2)^4}\right]
      Z(Z+1)  \right. \\
&   & \mbox{} + \left. \left[ \frac{2-2\epsilon-\epsilon^{2}}{(1+u^2)^2}-
      \frac{4u^2(1-\epsilon)}{(1+u^2)^4}
      \right]
      \left[ X-2Z^{2}f_{c}((\alpha Z)^{2})\right]
      \right\} \\
u & = & \frac{E \theta}{m} \\
X & = & \int_{t_{min}}^{m^{2}(1+u^{2})^{2}}
{\left [ G_{Z}^{el}(t) + G_{Z}^{in}(t) \right ] \frac{t-t_{min}}
{t^{2}} dt} \\
G_{Z}^{el, in}(t) & & \mbox{atomic form factors} \\
t_{min} & = & \left [ \frac{k m^{2} (1+u^{2})}{2 E (E-k)} \right ] ^{2}
 = \left [ \frac{\epsilon m^{2} (1+u^{2})}{2 E (1-\epsilon)} \right ] ^{2}
\end{eqnarray*}

This distribution is complicated to sample, and it is anyway only an
approximation to within few percent, if nothing else, due to
the presence of the atomic form-factors.
The angular dependence is contained in the
variable $u = E \theta m^{-1}$. For a given value
of $u$ the dependence of the shape of the function on $Z$, $E$,
$\epsilon = k/E$ is very weak.
Thus, the distribution can be approximated by a function
\begin{equation}
f(u) = C \left( u e^{-au} + d u e^{-3au} \right)
\end{equation}
where
\[
C = \frac{9a^{2}}{9 + d} \hspace{1cm} a = 0.625 \hspace{1cm}
d = 0.13 \: \left ( 0.8+\frac{1.3}{Z} \right ) \left (100+\frac{1}{E} \right )
(1+\epsilon)
\]
where $E$ is in GeV. While this approximation is good at high energies,
it becomes less accurate around few MeV. However in that region the
ionisation losses dominate over the radiative losses.

The sampling of the function $f(u)$ can be done in the following way
($r_{i},\: i=1,2,3$ are uniformly distributed random numbers
in [0,1]):
\begin{enumerate}
\item Choose between $u e^{-au}$ and $d u e^{-3au}$:
\[
b = \left \{ \begin{array}{ll}
a & \mbox{if\hspace{0.5cm}}r_{1} < 9/(9+d) \\
3a & \mbox{if\hspace{0.5cm}}r_{1} \geq 9/(9+d)
\end{array} \right .
\]
\item Sample $u e^{-bu}$:
\[
u=-\frac{\log ( r_{2} r_{3}) }{b}
\]
\item check that:
\[
u \leq u_{max} = \frac{E \pi}{m}
\]
otherwise go back to 1.
\end{enumerate}

The probability of failing in the last test is reported in
table~\ref{tb:phys341-1}.

\begin{table}
\begin{centering}
\begin{tabular}{|l|l|}
\multicolumn{2}{c}{$\displaystyle
P = \int^{\infty}_{u_{max}}{f(u) \: du} \hfill $} \\ [0.5cm]
\hline
E (MeV) & P(\%) \\ \hline
0.511 & 3.4 \\
0.6 &  2.2 \\
0.8 & 1.2 \\
1.0 & 0.7 \\
2.0 & $<$ 0.1 \\ \hline
\end{tabular}
\caption{Angular sampling efficiency}
\label{tb:phys341-1}
\end{centering}
\end{table}

The function $f(u)$ can be used also to describe
the angular distribution of the photon in $\mu$ bremsstrahlung and to
describe the angular distribution in photon pair production.

The azimuthal angle, $\Phi$, is generated isotropically.
This information is used to calculate the momentum vector of the radiated
photon, to transform it to the global  coordinate system.
Also, the momentum of the parent electron is updated.

\section{Status of this document}

   9.10.98  created by L. Urb\'an.

\begin{thebibliography}{99}

\bibitem[GEANT3]{ebrem.geant3}
  GEANT3 manual ,CERN Program Library Long Writeup W5013 (October 1994).
\bibitem[Gal64]{ebrem.galitsky}
  V.M.Galitsky and I.I.Gurevich. Nuovo Cimento 32 (1964) 1820.
\bibitem[Ant97]{ebrem.anthony}
  P.L. Anthony et al. SLAC-PUB-7413/LBNL-40054 (February 1997)
\bibitem[Sel85]{ebrem.seltzer}
  S.M.Seltzer and M.J.Berger. Nucl.Inst.Meth. 80 (1985) 12.
\bibitem[Mess70]{ebrem.messel}
  H.Messel and D.F.Crawford. Pergamon Press,Oxford,1970.
\bibitem[EGS4]{ebrem.egs4} W.R. Nelson et al.:The EGS4 Code System.
   {\em SLAC-Report-265 , December 1985 }
\bibitem[Mig56]{ebrem.migdal}
   A.B. Migdal. Phys.Rev. 103. (1956) 1811.
\bibitem[Kim86]{ebrem.kim}
   L. Kim et al. Phys. Rev. A33 (1986) 3002.
\bibitem[Tsai74]{ebrem.tsai1}
   Y-S. Tsai . Rev. Mod. Phys. 46. (1974) 815.
\bibitem[Tsai77]{ebrem.tsai2}
   Y-S. Tsai . Rev. Mod. Phys. 49. (1977) 421.
\bibitem[Will60]{ebrem.williams}
   R. W. Williams . Fundamental Formulas of Physics, vol.2. Dover Pubs.Inc,1960.
\bibitem[Butc60]{ebrem.butcher}
   J. C. Butcher and H. Messel. Nucl.Phys. 20. (1960) 15.

\end{thebibliography}