\documentclass[11pt]{article}

\usepackage{amsmath}
\usepackage[margin=1in]{geometry}
\usepackage{graphicx}
\graphicspath{ {/} }

\makeatletter
\def\l@section{\@dottedtocline{1}{0em}{3em}}
\makeatother

\begin{document}

\title{An Introduction to Cost Estimation of Relational Query Plans}
\author{Yannis Velegrakis}

\maketitle

\tableofcontents

\section{Page}

Every time a hard drive is instructed to read or write something on the disk it does so by reading or writing units of specific size. This size is called a PAGE and has always a fixed size for every system. For example, if the page size is 1024 bytes and the database wants to read 3 bytes from the disk, the disk will read and return to the database 1024 bytes of which the database will throw away the 1021 and keep only the needed 3.
The size of a page on the disk will be denoted as $P$.

\section{Size of a Record}

We assume that the records of a relation have more or less the same size. The size of a record indicating how much space (in bytes) a record occupies when stored on the disk. The size of a record will be typically given, or if not, it would be possible to compute from the size of the individual attributes. For example, if a relation has five attributes, three of them being integers and two being VARCHAR(25), then the size of a record for this relation is 3*4+2*25=62 bytes. The size of a record of a relation R will be denoted as $t_R$.

\section{Pages of Relations}

The data of every relation are stored on the disk. When a page contains some data of a relation, no records from other relations are allowed in that page. What the databases try to do is to try to fill a page with as many records of the same relation as they can, and if no more records can fit, then they start with another page. This means that we can safely assume that all the pages that a relation occupies on the disk are full.
The number of pages that a relation R occupies on the disk will be denoted as $P_R$.

\section{Cardinality of a Relation}

A relation is a set of records. The number of records a relation has is the cardinality of the relation. The cardinality of a relation R will be denoted as $|R|$.

\section{Cardinality of an Attribute}

The cardinality of an attribute in a relation is the number of different distinct values that the attribute has. If the attribute is a key, clearly the cardinality of the attribute is the same as the cardinality of the relation, i.e., the number of records of the relation (since a value cannot be repeated). \\
The cardinality of two or more attributes is similarly the number of different distinct combinations of values of these attributes across the records.
The cardinality of an attribute A of a relation R, will be denoted as $|R.A|$, and the cardinality of two or more attributes as $|R.A_1, R.A_2, ..., R.A_n|$. So, if the attribute A is a key, then $|R| = |R.A|$, otherwise $|R.A| \le |R|$.

\section{Records per Page}
The database does not store in the same page records from different relations. Furthermore, the database does not store a record across different pages. For instance, if a page size is 100 bytes and a record has a size of 30 bytes, then only 3 records will fit in a page (taking space 3*30=90 bytes), and the remaining 10 bytes from the 100 will be left empty. This means that

$$
\text{\#Records of R per Page} = \Big\lfloor\frac{\text{Page Size}}{\text{Tuple Size of R}}\Big\rfloor = \Big\lfloor\frac{P}{t_R}\Big\rfloor
$$

The symbol $\lfloor \rfloor$ means that we round down to the closest integer. So if the division gives 4.3, then the number of records per page will be 4.

\section{Relation Size}

The size of a relation is the space it occupies on the disk. Since there are spaces left in pages, that size is often more than the actual size of its records. So, the size of R in bytes is:

$$
\text{Size of R} = \text{\#Pages of R} * \text{PageSize} = \Big\lfloor\frac{\text{\#Tuples of R}}{\text{\#Tuples of R per page}}\Big\rfloor P \\
= \Big\lfloor\frac{ |R| }{\lfloor\frac{P}{t_R}\rfloor}\Big\rfloor P
$$

The symbol $\lceil \rceil$	means that we round up to the closest integer. So, if the internal part is 4.3, then the $\lceil \rceil$ will be 5.
From the above mathematical equation, it becomes clear that:

$$
|R| = P_R * \#\text{of records of R in a page} = P_R * \Big\lfloor\frac{P}{t_R}\Big\rfloor
$$

\section{Cost}

Operations that involve reading from the disk or writing to the disk are called I/O. Operations that are done only in memory are called in-memory. The cost of an in-memory operations is so much smaller than that of an I/O that it is the number of needed I/O operators that are actually determining the cost (time) that a query will take to be executed. For this reason, we will consider the cost of any inmemory operation to be 0 (zero) and we will measure the cost of the execution of a query in terms of number of I/O operations. By definition we will assume that the cost of reading or writing a page on the disk is equal to 1.

\section{Scan}

A scan operation is a sequential read of a relation. The cost is the cost of reading all the pages that the relation occupies. Thus,

$$
\text{Cost of Scan} = \#\text{Pages of the relation}
$$

For the database to find and read a record in a relation normally it needs to do a scan since the record can be in any of the pages that the relation occupies, and in the absence of any auxiliary structure (for instance the indexes we will see later) or any special organization of the data (for instance if the data is sorted), then the scan is the only option.

\section{Sorting}
If we need to sort a relation based on the value of a specific attribute, there are two main algorithms that this can be done. One of these algorithms assumes that the database has only 3 buffers in its memory. If N is the number of pages occupied by the relation that needs to be sorted, the cost of the sorting (in terms of required I/O operations) is:

$$
2N(\lceil \log_2(N)\rceil + 1)
$$

Typically, a database has more than 3 memory buffers available for sorting, so an optimized algorithm can be used. If there are B available buffers (with B>3), then the cost of sorting a relation that occupies N pages (in terms of required I/O operations) is:

$$
2N(\lceil \log_{B - 1}(\lceil\frac{N}{B}\rceil)\rceil + 1)
$$

The details of the algorithms can be found in the course textbook, but for us it is enough to simply know these two algorithms and the cost that each one has.

\section{Indexes}

An index is an auxiliary structure that is built based on the values of a specific attribute of the database. It helps in identifying the location of the records (meaning the pages in which they reside) that have a value in that attribute that satisfies some specific condition. There are two kinds of Indexes that are of interest to us: The Hash and the B+-trees.
A Hash index is an index that helps in finding fast records that satisfy conditions of the form A=value, where the A is the attribute on which the hash index has been constructed. For example, if we construct a hash index on the attribute “Age” of a table “Person”, then the index can help identify fast all these records that have Age=10.
A B+-tree index is an index that helps finding fast records that satisfy either equality conditions like those that also the Hash index can answer, but also can help with range conditions. Range conditions are conditions that require the value of an attribute to be between two values, or greater than a value, or less that a value. Some examples of range conditions are: $A > 5, A \ge 10$, or even $A \le 67 AND A \ge 150$.
The advantage of a Hash index is that it is much faster than the B+-tree but cannot answer range queries. The advantage of B+-tree is that it can answer range queries but for the equality conditions it is slower than the Hash.

\section{Composite Index}

It is possible to create an index that depends on more than one attribute. These are called composite indexes. In the case of B+trees, the order in which the attributes have been defined plays a role. The idea of the composite index in B+-trees is that the data is indexed first based on the first field, then on the second, then in the third, etc. This means that a B+-tree can be used when we are searching not only for all the attributes of the composite index, but also when searching only for those at the beginning. However, if we are searching for those at the end, the B+-tree cannot be used. For example, a composite index on (firstName, LastName) means that the records are indexed first on he first name, and then among all those with the same first name, they are indexed on the last name. This means that the B+-tree can be used when searching for someone with a specific first and last name. The same index can also be used for searching for someone with a specific first name. However, it cannot be used to search for someone with a specific last name (without knowing the first name).

\section{Updating Index}

Every time the data of a relation are updated (the records are modified, new records are inserted or existing records get deleted), the index needs to be updated as well. This of course affects the time of the query execution, which is why it is not a good idea to put too many indexes, but only on those that are really needed. Nevertheless, for our goal here we will ignore the time needed to update the index.

\section{Index Lookup}

The operation of using the index to identify where the records that satisfy some condition are located is called the index lookup. The output of an index lookup operation is a set of pointers that indicate the pages that contain these records.
Indexes are also taking space and since the database tables may be really big, unavoidably the indexes may become also big, to a point that it is not possible to keep them in memory so they are stored on the disk. This means that in order to perform an index lookup, we may need to do a number of disk accesses. Fortunately, the indexes are much smaller than the actual tables, so the index lookups are not very expensive.

In particular, we can safely consider the generic case in which a Hash index lookup costs 1.2 I/O (meaning that in average 1.2 page reads are needed) and a B+-tree lookup costs  $\log_s | R.A |$ I/O (meaning $\log_s | R.A |$ page reads needed) where the A is the attribute on which the index has been defined. If the |R.A| is not known, then the value 3 can be considered for a B+tree lookup that is a typical value. The base s of the log depends on how the B+-tree has been implemented. It is the (maximum) number of children that a node of the B+-tree can have. If every node of the B+-tree can have at most 2 children, then the $s=2$.

The look-up cost will be denoted as $L$ ($L_h$ if a hash, $L_t$ if a B+tree).

\section{The Selectivity Factor}

One question that is usually important to know is how many records are expected to be retrieved given a condition. (They are called the qualifying records). Given a condition c and a relation R, we will denote by Rc the records of R that satisfy the condition c. For instance, Rname=”John” is the set of records of R that have the value “John” in the attribute “name”. The database knows these numbers by keeping some statistics, in structures like histograms. If the histograms are not available, then some probability theory can help. We can assume that the values have an equal distribution. So, if we have a relation R with an attribute A, then the expected number of qualifying records, i.e., records that satisfy the condition A=x where x is some constant value, is
$$
|R_{A = c}| = \frac { |R| }{ |R.A| }
$$
Sometimes we have something that is called the “selectivity factor”. The selectivity factor is the percentage of the total number of records expected to be retrieved for a specific condition. For instance, with a selectivity factor f for the attribute A, the expected number of records that satisfy the condition A=x, is $f|R|$.
Note that for multiple conditions the selectivity factors are multiplied. For example, if the attribute A has selectivity factor f, and the attribute B has selectivity factor g, the expected number of records that satisfy the condition $A=x AND B=y$ is $fg|R|$.

\section{Clustered/Unclustered}

An index can be clustered or unclustered. Clustered means that all the records that are satisfying a specific condition are stored one next to the other. In the case of an unclustered index, the lookup returns one page-pointer for every qualifying record located on the disk. Instead in the case of a clustered index, a pointer is returned for those pages that contain the qualifying records, which is clearly much smaller than the number of qualifying records. Given the above, for a condition c on a relation R, the number of page pointers returned by the lookup in an clustered index is

$$
\text{\# pages occupied by these tuples} = \frac{\text{\# tuples of R that satisfy}}{\text{\# tuples of R that fit in a page}} = \frac{ |R_c| }{\lfloor\frac{P}{t_R}\rfloor}
$$

\section{Cost of Retrieving Qualifying Records}
To retrieve the qualifying records after an index lookup has the cost of reading each of the pages for which the lookup returned a pointer. For example, assume that we need to retrieve the records of a table R for which Age=5. If there is no index, the cost will be the cost of reading the whole relation that will be equal to the number of pages that the relation occupies. If there is an index on an attribute different than Age, it cannot be used so the cost would be the same (the number of pages of the whole relation). If there is an index on attribute A and is unclustered, the cost is the cost of the lookup, plus the cost of reading the records. Since the index is unclustered, the lookup will return one pointer for every qualifying record, and there is a need for one read for each such pointer. Thus, the total cost will be:

$$
L + |R_{age}=5|
$$

If the index is instead clustered, then the cost will be:

$$
L + \frac{ |R_{age}=5| }{\lfloor\frac{P}{t_R}\rfloor}
$$

\section{Cost of an update operator}

The cost of an update operator is the cost of the index lookup + the cost of reading the page + the cost of writing the page, meanings that the cost would be:

$$
\text{Lookup} + \text{\#pages that contain the records to be changed} * (1 + 1)
$$

\section{Joins}

The join is the most fundamental operator in queries and special attention has been paid to it. There are many different algorithms for efficiently and effectively implement the joins. Here we mention the most prevalent. Given two relations, one that is called R (occupying M pages and having K records) and the other called S (occupying N pages and having L records). Then

Simple Nested Loops Join: It is the method that can always be done since it does not need any auxiliary structure. It simply reads the first relation and for each page of the first reads the whole second relation. Thus, if we do the $R\bowtie S$ will have a cost $M + NM$, while $S \bowtie R$ will have a cost $N + MN$ (which means that it is better if we start from the smaller relation.
Sort Merge Join: We first sort each relation and then we can simply perform the join through an one pass over each relation (having one pointer in each one that we advance). Thus, the cost is: $\text{Cost to Sort R} + \text{Cost to Sort S} + M + N$. The cost to sort a relation may be ignored if the relation is already sorted. If not, ref to the cost of sorting described previously.
Hash Join: Assuming that we have enough memory to store a hash structure, we can construct such a hash structure and then use it to perform the join. The algorithm has a cost $3(M+N)$
Index Nested Loops Join: This is used when there is an index that can be exploited. We read one relation and then for each record in that relation we use the index on the other to select the matching records (and only the matching records). Of course the index of the other relation has to exist and has to be on the join attribute. Otherwise the idea cannot be used. If the index exists, then the cost will be:

\begin{align*}
\text{Cost To Read R} + \text{\#RecordsInR} * \text{Cost to Retrieve the Matching records in S} = \\
 = M + K * (\text{Index Lookup Cost} + \text{Cost of Retrieving Qualifying Records})
\end{align*}

The index Lookup and the Qualifying records retrieval cost have already been discussed above.

\section{Storing the results of a query}
The results of a query can be displayed on the screen of the user. This has no cost since every record that the query generates is simply shown on the screen and then thrown away. However, often there is a need to store the results back into the database, which means that we have to write the results on the disk. The cost of this operation is the number of pages that we need to write, which is the same at the number of pages that the records will occupy. So assume that we have created a relation (meaning a set of records). The cost of saving it on the disk is:

$$
\text{\# of Pages Needed for these Tuples} = \Big\lceil\frac{\text{\# of Tuples}}{\text{\# of Tuples that fit in a page}}\Big\rceil = \frac{\text{\#Tuples of R}}{\lfloor\frac{P}{t}\rfloor}
$$

Note that some time during the execution of a query, a query may need to store some intermediate results on the disk because they may be big and cannot be kept into memory. The cost for doing so is 2 times the cost of the above formula: one for writing the results, and one for reading them afterwards.
It is important to note that when intermediate results are stored on the disk, clearly they have no index on them, which means that the only way to read them is to use a scan, which explains why the cost of reading is equal to the cost of writing them.

\section{Query Execution and Query Plans}

When a query is given to the database for execution the database converts it into what is called a query plan. A query plan is a tree of relational algebra operations that implement the query. Clearly there may be many different query plans (meaning trees) for the same SQL query. Given a query plan we need to be able to compute the total cost, i.e., the cost of every operator (node) of the tree. For each node we need to be able to compute apart from the cost, the number of records that the operator generates (remember that every operator is generating always a relation, meaning a set of records).
The database has a special component called optimization, the role of which is to evaluate the queries that it received and identify the most effective execution plan to perform.

\section{On-the-fly Operations}

Certain operators in the query plan can be done as the records arrive (meaning there is no need to wait for all the results to arrive first). So once a record is produced from the operators below, the operator above is executed. This means that the cost is 0 since it is done in memory. These are called on-the-fly operators. For example, imagine the projection on attributes A and B. We do not need to wait for all the records but as a record is given, the A and B are kept and the rest is eliminated. The A and B values of the record can be provided to the operator above the projection.
Note that if there is a need to do duplicate elimination, then unavoidably the operator has to wait for the whole set of records to be first received.

\section{Index-only Operations}

An operation, e.g., a select, a join, etc., that does not require access the actual records but only from the index can find what is needed, is called an index-only operation. For example, the query:

$$\text{select S.A from S, T where S.B=T.C}$$

with an index on T.C, is index-only because it only needs a lookup on the Index and not to actually read the records of T.

\begin{figure}[h]
\includegraphics[scale=0.5]{img1.jpg}
\centering
\caption{An Example of a Query Plan}
\end{figure}

\begin{figure}[h]
\includegraphics[scale=0.47]{img2.jpg}
\centering
\caption{Cost of operations that do not involve Joins}
\end{figure}

\end{document}


% !TEX root = main.tex

%-------------------------------------------------
\section{Classical probability}\label{sec:classical}

We now return to the question of how we might reasonably assign probabilities to random events. Classical probability deals with finite sample spaces in which all outcomes are equally likely. This means that the probability of an event $A$ is proportional to the number of outcomes it contains, which is called the \emph{cardinality} of the set and denoted by $|A|$. The classical definition of probability can therefore be written as
\[
P(A) = |A|/|\Omega|.
\]
Problems can be solved by counting the number of ways different events can occur. For example, if a game has $n$ possible outcomes of which $m$ correspond to winning, then the probability of winning is $m/n$.

\begin{example}
An urn contains $3$ white balls and $5$ black balls. If two balls are drawn at random from the urn, what is the probability that they are both white?
\begin{solution}
\bit
\it The sample space $\Omega$ is the set of all possible pairs:
$|\Omega|	= \binom{8}{2} = \frac{8!}{6!2!} = 28$.
\it Let $A$ be the event that both balls are white:
$|A|  = \binom{3}{2} = \frac{3!}{1!2!} = 3$.
\eit
The probability that both balls are white is $P(A) = |A|/|\Omega| = 3/28$.
\end{solution}
\end{example}

\begin{exercise}[The Division Paradox]
A \emph{fair game} is one in which the probability of winning is equal to the probability of losing. Two players $A$ and $B$ decide to play a sequence of fair games until one of the players wins 6 games, but they stop when the score is 5:3 in favour of player $A$. How should the prize money be fairly divided?
\begin{answer}
Assume that the players carried on playing the sequence of games. The maximum number of additional games is 3, and the sample space can be expressed as
\[
\{AAA,AAB,ABA,BAA,ABB,BAB,BBA,BBB\}.
\]
The games are fair, so all outcomes are equally likely.
\bit
\it Only one outcome is in favour of player $B$, the other seven are in favour of $A$.
\it The prize money should therefore be divided in the ratio $7:1$ in favour of $A$.
\eit
\end{answer}
\end{exercise}

%The classical method can only be applied to problems which can be broken down into outcomes that are equally likely, which is not always possible.

%-------------------------------------------------
\section{Relative frequency}\label{sec:frequentist}

Classical probability exploits the symmetry that exists in many random experiments, for example those involving dice, coins and cards, to choose sensible values for the probabilities of different events. How might we define probability in more general situations? If a random experiment can be repeated many times under the same conditions it is natural to think of probability as the number of times an event occurs as a proportion of the total number times that the experiment is repeated.

\begin{definition}\label{def:relative_frequency}
Let $N$ be the number of times an experiment is repeated and let $N(A)$ be the number of times event $A$ occurs during these $N$ repetitions. The ratio $N(A)/N$ is called the \emph{relative frequency} of $A$.
\end{definition}

\begin{definition}\label{def:frequentist_probability}
Under the \emph{frequentist model}, the probability of $A$ is the limit of its relative frequency as the number of trials increases to infinity:
\[
\prob(A) = \lim_{N\to\infty} \frac{N(A)}{N}.
\]
\end{definition}

\begin{exercise}
Let $\Omega$ be a finite sample space. Show that probability as defined by the frequentist model has the following properties:
\ben
\it $P(\emptyset)=0$ and $P(\Omega)=1$.
\it Complementarity: if $A\subseteq B$ then $P(A^c) = 1 - P(A)$.
\it Monotonicity: $P(A)\leq P(B)$.
\it Additivity: If $A$ and $B$ are disjoint, $P(A\cup B) = P(A) + P(B)$.
\een
Show also that the conditional probability of $A$ given $B$ is $P(A\cap B)/P(B)$ whenever $P(B)>0$.

\begin{answer}
\ben
\it
$N(\emptyset)=0$ and $N(\Omega)=N$ for any number of repetitions $N$, so $P(\emptyset)=0$ and $P(\Omega)=1$.
\it
$A^c$ occurs if and only if $A$ does not, so $N(A^c) = N - N(A)$ and thus
\[
P(A^c)
	= \lim_{N\to\infty}\frac{N(A^c)}{N}
	= \lim_{N\to\infty}\frac{N - N(A)}{N}
	= 1 - \lim_{N\to\infty}\frac{N(A)}{N}
	= 1 - P(A^c).
\]
\it
If $A\subseteq B$ then $A$ occurs whenever $B$ occurs, so $N(A)\leq N(B)$ and hence
\[
P(A)
	= \lim_{N\to\infty}\frac{N(A)}{N}
	\leq \lim_{N\to\infty}\frac{N(B)}{N}
	= P(B).
\]
\it
If $A$ and $B$ are disjoint, they cannot both occur together, so $N(A\cup B)=N(A)+N(B)$ and thus
\[
P(A\cup B)
	= \lim_{N\to\infty}\frac{N(A\cup B)}{N}
	= \lim_{N\to\infty}\frac{N(A)+N(B)}{N}
	= \lim_{N\to\infty}\frac{N(B)}{N} + \lim_{N\to\infty}\frac{N(B)}{N}
	= P(A) + P(B).
\]
\een
Let $P(A|B)$ denote the conditional probability of $A$ given $B$. This is the number of trials in which $A$ and $B$ both occur expressed as a proportion of the number of trials in which $B$ occurs. If $N(A,B)$ is the number of times $A$ and $B$ both occur then
\[
\prob(A|B)	= \lim_{N\to\infty}\frac{N(A,B)}{N(B)}
		= \lim_{N\to\infty}\frac{N(A,B)/N}{N(B)/N}
		= \frac{\prob(A\cap B)}{\prob(B)}.
\]
as required.
\end{answer}
\end{exercise}

The frequentist model is the basis of how probability theory is applied to real-world problems, and dominates in many areas of science (e.g.\ medical trials). It does however have some serious practical and philosophical drawbacks.

\bit
\it
Not all experiments can be repeated many times under the same conditions. For example, it is reasonable to consider the probability that Wales will win the World Cup in 2018. The frequentist model does not provide an adequate definition of such probabilities.
\it
In practical applications, an experiment is repeated finitely many times and the relative frequency of an event is taken as an approximation of its ``true'' probability. If we accept that we can only measure probability with some error of measurement, we find that an error of measurement can itself only be expressed as a probability, which is the very concept we are trying to define.
\it
The frequentist model is also limited when dealing with infinite sample spaces. For example, consider a random experiment where a coin is tossed repeatedly until the first head occurs, and whose the outcome is the total number of times the coin is tossed. The sample space is the countably infinite set $\{1,2,3,\ldots\}$ but no matter how many times we repeat the experiment, there are outcomes which cannot be observed (e.g\ if we repeat the experiment $N$ times, we cannot observe runs of length $N+1, N+2,\ldots$).
\eit

Frequentist probability is best regarded an informal theory which is useful in many practical applications, but which lacks the mathematical clarity offered by Kolmogorov's axiomatic theory.


\refstepcounter{dummy}
\pdfbookmark[1]{Notation}{notation}
\chapter*{Mathematical Notation}
\label{front:notation}

Throughout this document, the following conventions regarding mathematical notation are being assumed, unless stated otherwise:

\paragraph{General}
\begin{items}
	\item Vectorial quantities are marked with an arrow (\(\vec{u}\)).
	\item A dot \(\cdot\) between vectors denotes a scalar product, and a cross \(\times\) the vector product.
	\item Unit vectors carry a caret (\(\hat{x}\)).
\end{items}

\paragraph{Coordinate Systems}
\begin{items}
	\item In%
		\sidefigure*{}{\resizebox{.9\marginparwidth}{!}{\input{figures/frontmatter/spherical.tex}}}[2]%
	Cartesian coordinates, the directions are denoted as
	\begin{equation}
	\hat{x} = \left( \begin{matrix} 1 \\ 0 \\ 0 \end{matrix} \right), \quad
	\hat{y} = \left( \begin{matrix} 0 \\ 1 \\ 0 \end{matrix} \right), \quad
	\hat{z} = \left( \begin{matrix} 0 \\ 0 \\ 1 \end{matrix} \right).
	\end{equation}
	Following the convention in Geophysics, \(x\) points in zonal direction, \(y\) in meridional direction, and \(z\) into the vertical direction (skyward). Components of the velocity \(\vec{u}\) in \(x\), \(y\), and \(z\)-direction are denoted as \(u\), \(v\), and \(w\), respectively.
	\item In spherical surface coordinates, the zonal coordinate is denoted as \(\phi\) (longitude), and the meridional coordinate as \(\theta\) (latitude).
\end{items}

\paragraph{Differentiation}
\begin{items}
	\item The partial derivative of a quantity \(h\) is written as
	\begin{equation} \frac{\partial h}{\partial \phi} \equiv h_\phi. \end{equation}
	\item It is often necessary to calculate the derivative of a quantity \emph{along a streamline}. This \emph{material derivative} is denoted as
	%
	\begin{equation} \Ddx q = \shortunderbrace{q_t}_{\mathclap{\text{partial derivative}}} + \overbrace{\vec{u} \cdot \nabla q}^{\mathclap{\text{advection}}}. \end{equation}
	%
	\item The Nabla operator \(\nabla\), as usual in vector calculus, is defined as
	\begin{equation} \nabla = \begin{pmatrix} \partial/(\partial x) \\ \partial/(\partial y) \\ \partial/(\partial z) \end{pmatrix}. \end{equation}
	%
	Hence, \(\nabla f\) denotes the gradient of the scalar field \(f\), and \(\nabla \cdot \vec{u}\) and \(\nabla \times \vec{u}\) the divergence and curl of a vector field \(\vec{u}\), respectively.
	\item The horizontal equivalent of \(\nabla\) is denoted as \(\nabla_H\) and only operates on the \(x\) and \(y\)-directions of a vector field. The horizontal divergence and curl thus read
	\begin{alignat}{3}
	\divergence_H (\vec{u}) &= \nabla_H \cdot \vec{u} &&= u_x + v_y \\
	\curl_H (\vec{u}) &= \nabla_H \times \vec{u} &&= v_x - u_y.
	\end{alignat}
	\item The Jacobian determinant\sidenote{For the sake of brevity just refered to as \enquote{Jacobian}.} \(J(a,b)\) of two scalar fields \(a\), \(b\) is defined as
	\begin{equation} J(a,b) := a_x b_y - a_y b_x. \end{equation}
\end{items}

\paragraph{Scale Analysis}
\begin{items}
	\item The operator \(\orderof{x}\) is used several times during scale analyses, and simply means \enquote{order of} --- it maps a physical quantity \(x\) to a corresponding typical scale\sidenote[-2]{These scales are chosen in a heuristic manner, providing motivations rather than formal derivations. While their exact value can be argued, their order of magnitude usually cannot, allowing the comparison of terms that vary by several orders of magnitude.}.
\end{items}

\paragraph{Other}
\begin{items}
	\item The temporal mean value of a quantity \(u\) is denoted as \(\mean{u}\).
\end{items}


\section{Modified Mini Batch $k$-Means Algorithm}
\label{sec:TheoryKMeans}
The $k$-Means algorithm takes $k$ random points in space representing the center of $k$ classes. These class centers are then refined in two steps: First, each data point in a set of training data is classified to belong to one of the classes by selecting the closest class center. Second, the class centers are moved towards the geometric center of all the data points classified as belonging to the class. These steps are repeated until the class centers stop moving.\\
However, this approach is impractical for large data sets, since every iteration needs to calculate distances from every data point to every class center. A modification of this algorithm called Mini-batch $k$-Means \cite{bib:Elkan2003} uses only a random subset of all data points for each iteration. This results in slightly less accurate class centers, but much greater performance.\\
In our case though, there is no Euclidean feature space in which we could position class centers. The only measure available is the DTW-distance between the different samples. Thus, we modified the second step of the Mini-batch $k$-Means algorithm to choose the center-most sample instead of the arithmetic center. The center-most sample is defined as the one sample in the class that has the least distance from all other samples in the class.\\
Running this algorithm on our data set results in $k$ class centers. New samples can then be classified to belong to one of these centers by selecting the closest one.\\
Calculating class centers requires the calculation of many distances between many samples. In order to speed up this process, we pre-calculated all the sample distances as described in chapter~\ref{sec:Distance}.


\subsection{Woody Fibers Decomposition Model}
Fungi with the ability to decompose lignin or cellulose~\cite{lignin}\cite{cellulose} are distributed on an ideal cylindrical dead wood. Under certain environmental temperature and moisture conditions, the hyphae begin to expand and grow, and fungi secrete biologically active enzymes that can decompose the woody fibers. As time goes by, the outer layer of woody fibers decompose and fall off, and the fungi gradually approach the axis of the dead wood. Ignoring the surface area of cylinder bottoms, use the \textit{cylinder side surface area formula}, and mark it as \textit{Eq.~(\ref{cylindersurfacearea})}
\begin{equation}
  \label{cylindersurfacearea}
  S_{cylinder} = 2\pi r\cdot h
\end{equation}
where $r$ represents the radius of dead wood, and $h$ represents the total length of dead wood. As the radius decreases, it comes to a conclusion that the surface area of cylinder also decreases.
\par
Based on the conclusion, it is obvious that the number of fungi per unit area would increase. Meanwhile, fungal hyphae can generally grow to several times their initial size~\cite{initialsize}. Therefore, even considering the loss of fungi because of wind and rain, the number of fungi would still increase significantly as their distribution gradually approaches the central axis.
\par
Combining with the relevant knowledge of electromagnetic in physics~\cite{physics}, we find that the distribution of fungi has many similarities with the \textit{Gaussian surface} in the electric field. On the one hand, the distribution of fungi is cylindrical and could be represented by a cylindrical Gaussian surface. On the other hand, as the radius decreases, both the electric field strength and the number of fungi per unit area (also known as fungi decomposition rate) would increase. From \textit{Gauss theorem}, the decomposition cylinder is shown in \textit{Figure~1}, and  we can get \textit{Eq.~(\ref{secondeq})}, \textit{Eq.~(\ref{thirdeq})} and \textit{Eq.~(\ref{fourtheq})} as follows.
\begin{figure}[H]
  \centering
  \label{figure1}
  \subfigure[Electric field intensity $\bm{E}$ and area differential $d\bm{S}$.]{
    \includegraphics[width=0.45\textwidth]{figures/plain.png}
  }
  \subfigure[Ideal cylindrical wood.]{
    \includegraphics[width=0.45\textwidth]{figures/cylinder.jpg}
  }
  \caption{Decomposition cylinder.}
\end{figure}
\begin{equation}
  \label{secondeq}
  \oint_S \bm{E} \cdot d\bm{S}=\frac{1}{\varepsilon}\int_V \rho d V
\end{equation}
\begin{equation}
  \label{thirdeq}
  \bm{E}=\frac{\lambda}{2\pi \varepsilon R}
\end{equation}
\begin{equation}
  \label{fourtheq}
  \Phi = \oint_S \bm{E}\cdot d\bm{S} = \oint_S \frac{\lambda}{2\pi\varepsilon R}d S = \frac{\lambda}{2\pi\varepsilon R} 2\pi R l = \frac{\lambda l}{\varepsilon}
\end{equation}
where $\bm{E}$ represents electric field intensity on the surface of a uniformly charged cylinder, whose direction is radial, and $\Phi$ represents electric flux. Convert the above electromagnetic equations and parameters into formulas for estimating the decomposition rate ($\bm{DR}$) of fungi in \textit{Eq.~(\ref{fiftheq})}, \textit{Eq.~(\ref{sixtheq})} and \textit{Eq.~(\ref{seventheq})}
\begin{equation}
  \label{fiftheq}
  \oint_S DR\cdot dS = \frac{1}{\varepsilon_{DR}}\sum N_{inside} = \frac{1}{\varepsilon_{DR}} \int_V \rho dV
\end{equation}
\begin{equation}
  \label{sixtheq}
  DR = \frac{\alpha}{2\pi \varepsilon_{DR} R}
\end{equation}
\begin{equation}
  \label{seventheq}
  ACT=\oint_S DR\cdot dS = \oint_S \frac{\alpha}{2\pi \varepsilon_{DR} R} dS = \frac{\alpha}{2\pi \varepsilon_{DR} R} 2\pi R h = \frac{\alpha h}{\varepsilon_{DR}}
\end{equation}
where $\alpha$ represents fungi linear density, $DR$ represents the decomposition rate, $\varepsilon_{DR}$ represents environmental decomposition constant determined by surrounding meteorological indicators, and $ACT$ represents the \textbf{fungal activity factor}, which is determined by the suitable living concentration of specific fungal types, moisture tolerance and the influence between different fungal species. According to \textit{Eq.~(\ref{sixtheq})}, as fungi gradually approach the central axis, the decomposition rate of fungi increases, which is consistent with the previous analysis.


\lab{Algorithms}{Conditioning and Stability}{Conditioning and Stability}
\objective{Explore the condition of problems and the stability of algorithms.}
\label{lab:conditioning_stability}

\begin{equation*}
\mathlarger{ \mathlarger{ \mathlarger{f:X \rightarrow Y}}}
\end{equation*}
\begin{center} vs. \end{center}
\begin{equation*}
\mathlarger{ \mathlarger{ \mathlarger{\hat{f}:\hat{X} \rightarrow \hat{Y}}}}
\end{equation*}

%\begin{eqnarray}
%\mathlarger{\mathlarger{\mathlarger{f:X \rightarrow Y}}}\\ \mathlarger{\mathlarger{\mathlarger{ f:\hat{X} \rightarrow \hat{Y} }}}\\
%\mathlarger{\mathlarger{\mathlarger{ \hat{f}:X \rightarrow Y }}}%\\
%%\mathlarger{\mathlarger{\mathlarger{ \hat{f}:\hat{X} \rightarrow \hat{Y} }}}
% \end{eqnarray}
%

\section*{Conditioning of a Problem}

%\begin{equation*}
%\mathlarger{ \mathlarger{ \mathlarger{f:\hat{X} \rightarrow \hat{Y}}}}
%\end{equation*}

A \emph{problem} is a function $f:X \rightarrow Y$, where $X$ is a vector space of data and $Y$ is a vector space of solutions. In a perfect world, this function takes an exact input $x$ and correctly returns the exact answer $y$. However we are working in the finite world of computers.

Since $X$ and $Y$ are vector spaces of infinite cardinality, we cannot represent these spaces on a computer perfectly. We can only represent finite subsets $\tilde{X} \subset X$ and $\tilde{Y} \subset Y$. This means that the best we can do is approximate $x \in X$ with some $\tilde{x} \in \tilde{X}$ and return an approximate correct answer $\tilde{y} \in \tilde{Y}$. But if we change the input from $x$ to $\tilde{x}$, how much will our output change?

We define the \emph{condition number} of $f$ at $x$. Let $\delta x$ denote a small perturbation of $x$, and let $\delta f = f(x+\delta x) - f(x)$. Then the \emph{absolute condition number} of $f$ at $x$ is
 \[
\hat{\mathcal{K}} = \lim_{\delta \rightarrow 0} \sup_{\norm{\delta x} \leq \delta} { \frac{\norm{\delta f}}{\norm{\delta x}} }
\]

This is the ratio of output error to input error. These are \emph{absolute} errors; however, the error introduced by floating point arithmetic on a computer is \emph{relative} error. Therefore we define also \emph{relative condition number}:

\begin{equation}
\mathcal{K} = \lim_{\delta \rightarrow 0} \sup_{\norm{\delta x} \leq \delta} \left({ \frac{\norm{\delta f}}{\norm{f(x)}} } \middle/ { \frac{\norm{\delta x}}{\norm{x}} }\right)
\label{def:relativeconditionnumber}
\end{equation}

Relative condition number is usually a more useful concept that absolute condition number.

We say that a problem $f$ is \emph{well-conditioned} at $x$ if $\mathcal{K}$ is small. In this case, small changes to $x$ result in small changes to $f(x)$. We say $f$ is \emph{ill-conditioned} at $x$ if $\mathcal{K}$ is large. In this case, small changes to $x$ may result in large changes to $f(x)$.

\begin{example}[The Wilkinson Polynomial]
Polynomial root finding is a notoriously ill-conditioned problem. Root-finding for a degree $n$ polynomial can be described by a function $f$, which takes a vector $x$ of $n+1$ coefficients and returns a vector $y$ of $n$ roots. If two of the roots in $y$ are close to each other, the condition number of $f$ at $x$ will be large. In fact if $y$ contains repeated roots, the condition number is $\infty$.

James Wilkinson illustrated that root-finding can be extremely ill-conditioned even if the roots are far apart. His classic example is called the \emph{Wilkinson polynomial}:

\[
w(x) = (x-1)(x-2)(x-3)\hdots(x-20)
\]

Expanding this polynomial to obtain coefficients gives

\[\begin{array}{cl}
 w(x)=& x^{20}-210x^{19} +20615x^{18} -1256850 x^{17} +53327946 x^{16}\\
 &-1672280820x^{15} + 40171771630x^{14} -756111184500x^{13}\\
&+11310276995381 x^{12} -135585182899530 x^{11}\\
&+1307535010540395 x^{10} -10142299865511450 x^9\\
&+63030812099294896 x^8 -311333643161390640 x^7\\
&+1206647803780373360 x^6 -3599979517947607200 x^5\\
&+8037811822645051776 x^4 -12870931245150988800 x^3\\
& +13803759753640704000 x^2 -8752948036761600000 x\\
& +2432902008176640000
      \end{array} \]
Figure \ref{fig:wilkinsonpolynomial} shows what happens to the roots of $w(x)$ when we make tiny perturbations to the coefficients. The condition number of this polynomial is $\mathcal{K} \approx 5.1 \times 10^{13}$.

\begin{figure}
\centering
\includegraphics[height=2in]{wilkinsonpolynomial.pdf}
\caption{The blue dots are the roots of w(x). The red dots in the complex plane are roots of a randomly perturbed polynomial with coefficients $\hat{a}_k = a_k(1+r_k)$, where the $r_k$ are normally distributed with mean $0$ and standard deviation $10^{-5}$.}
\label{fig:wilkinsonpolynomial}
\end{figure}
\end{example}

\begin{problem}
Write a function to reproduce Figure \ref{fig:wilkinsonpolynomial}.
Perturb $w(x)$ as described in the caption for this Figure.
Instead of plotting the roots for just one random pertubation of $w(x)$, plot the roots for several random perturbations.

Write another function that, given an array of roots for a polynomial, computes the polynomial object representing the polynomial with the desired roots, then finds the roots of the resulting polynomial and plots them with the original roots.
You may find the \li{numpy.poly()} function useful for this.
Be sure you use different colors for the different sets of points.
Using equispaced points at integer values, what does the degree of the polynomial have to be for this computation to return incorrect answers?
\end{problem}

\begin{example}[Calculating Eigenvalues]
Consider the problem of finding the eigenvectors of a given $n \times n$ matrix $A$. If $A$ is symmetric, then fortunately the eigenvalue problem is well-conditioned. However the problem can be extremely ill-conditioned for non-symmetric matrices, even if $n$ is small. For example, the two matrices
\[ \left( \begin{array}{cc}
1 & 1000 \\
0 & 1
\end{array} \right)
%
\left( \begin{array}{cc}
1 & 1000 \\
0.001 & 1
\end{array} \right)
\]
have eigenvalues $\{ 1,1\}$ and $\{0,2 \}$ respectively.
\end{example}

\begin{problem}
Let $f:X\rightarrow Y$ be a function that accepts an $n\times n$ matrix $A$ and returns an $n \times 1$ vector of eigenvalues $y$. Write your own function that accepts a matrix $A$ and estimates the condition number $\mathcal{K}$ of $f$ at $A$. Recall that the definition of $\mathcal{K}$ in Equation \ref{def:relativeconditionnumber} includes the limit as $\delta \rightarrow 0$ and the supremum over all possible $\delta x \leq \delta$. To mimic this on a computer, simply take $\delta$ to be very small. Then calculate $\mathcal{k} =  { \frac{\norm{\delta f}}{\norm{f(x)}} } / { \frac{\norm{\delta x}}{\norm{x}} }$ a large number of times, taking a random $\delta x$ each time such that $\norm{\delta x} \leq \delta$. Let $\mathcal{K}$ be the largest of the values $\mathcal{k}$ calculated. (Hint: Let each $\delta x$ be a random normal matrix with mean $0$ and standard deviation $\approx 10^{-4}$.)

Experiment with inputting random symmetric and non-symmetric matrices. Remember that if $A$ is any matrix, then $A'A$ is symmetric. What kinds of condition numbers do you get for symmetric vs. non-symmetric matrices? What kinds of matrices give the biggest condition numbers for the eigenvalue problem?
\end{problem}

\begin{example}[Condition of a System of Equations]
Consider the system of equations
\[ Ax = b \]
for an $n \times n$ matrix $A$ and $n \times 1$ vectors $x$ and $b$. If we hold $A$ fixed, consider the problem of computing $b$ with respect to small changes in $x$. Alternatively, hold $A$ fixed and consider the problem of computing $x = A^{-1} b$ with respect to small changes in $b$. Finally, we may hold $b$ fixed and consider the problem of computing $x = A^{-1}b$ with respect to small changes in $A$.

It turns out that each of these problems has the \emph{same} relative condition number. This number is
\[
\mathcal{K} (A) = \norm{A}_2 \norm{A^{-1}}_2
\]

This is called the \emph{condition number of $A$}.
The condition number of a matrix can be computed using \li{numpy.linalg.cond()}.

Notice that the condition number of a matrix cannot be less that $1$.
It is almost always best to work with orthonormal matrices (or operations that can be mathematically represented by orthonormal matrices).
Since orthonormal matrices have a norm of $1$, as do their inverses, these transformations have the best possible condition number.
The low condition number of orthonormal matrices is one of the primary reasons that Householder reflections and Givens rotations are used in so many algorithms.
\end{example}

\section*{Stability of an Algorithm}
The analysis of problem-solving given above assumed that for a given input $\hat{x}$, we could determine the correct output $f(\hat{x})$ exactly. In other words, even if $\hat{x} \neq x$, we assumed that we could calculate $f(\hat{x})$ exactly although it may be different from $f(x)$.

In practice, our calculation of $f(\hat{x})$ is not always accurate, \emph{even if} the problem $f$ is well-conditioned. Our method of solving the problem may introduce new error. Sometimes the new error we introduce may be very large.

We define an \emph{algorithm} to be a method of solving a given problem. (The algorithm for solving a problem is different than the problem itself.) If an algorithm introduces large errors while solving a problem, the algorithm is called \emph{unstable}. A \emph{stable} algorithm does not introduce large error while solving a problem.

What could go wrong in an algorithm that would introduce new errors? Usually this happens when the algorithm breaks the problem into sub-problems, and one of these sub-problems is ill-conditioned. For example, the problem of computing the matrix $e^A$ may be well-conditioned for a given $A$, while the problem of computing the eigenvalues and eigenvectors of the same $A$ may be ill-conditioned. If an algorithm for computing $e^A$ relies on the intermediate step of computing the eigenvalues and eigenvectors, new error will be introduced.

As another example, take the problem of computing the eigenvalues of a matrix $A$. If $A$ is symmetric, the eigenvalue problem is well-conditioned. However, suppose we use an algorithm that first computes the coefficients of the characteristic polynomial, and then finds the roots of that polynomial. Unavoidably, tiny errors will creep in when we compute the coefficients of the characteristic polynomial. Since root-finding is an ill-conditioned problem, these tiny errors will be magnified. This algorithm is unstable.

\begin{warn}
Be careful to not confuse the stability of an algorithm with the conditioning of a problem.
These are two very different things.
A stable algorithm is, roughly speaking, an algorithm that will return a correct result if the problem is well conditioned.
If a problem is poorly conditioned, special algorithmic changes may be necessary to ensure a reasonable degree of accuracy.
\end{warn}

\begin{example}[A Simple but Unstable Algorithm]
Consider
\[\int_0^1 x^n e^{x - 1} dx\]
It is easily seen that this integral is always positive and always less than $1$.
It can be shown that, when $n > 1$, the value for this integral is always equal to
\[\left(-1\right)^{n} !n + \left(-1\right)^{n + 1} \frac{n!}{e}\]
where $!n$ is a combinatorial function called a derangement (it is equal to the number of permutations of a set that change the position of each element).
$!n$ is given by the recurrence relation $\left(n - 1\right)\left(!\left(n - 1\right) + !\left(n - 2\right)\right)$ and is defined to have the initial values $!0 = 1$ and $!1 = 0$.
There is a similar recurrence relation for the factorial function.
Given these recurrence relations, we can compute the value for this integral as using the following blocks of code:
\begin{lstlisting}
import numpy as np
def derangement(n):
    d0, d1 = 1, 0
    for i in xrange(1, n):
        d0, d1 = d1, i * (d0 + d1)
    return d1
def factorial(n):
    d0, d1 = 1, 1
    for i in xrange(1, n):
        d0, d1 = d1, i * (d0 + d1)
    return d1
def integral(n):
    return (-1)**n * derangement(n) + (-1)**(n-1) * factorial(n) / np.e
\end{lstlisting}
Unfortunately, since we are taking the difference of large floating point numbers that are very close to one another, this algorithm only gives correct values for the first few integrals.
The output is shown in Table \ref{table:unstable_computation}.

\begin{table}
\centering
\begin{tabular}{|l|l|l|}
\hline
Integrand & Computed Value & Actual Value \\
\hline
$x^{1}e^{x}$: & $0.367879441171$ & $0.367879441171$ \\
$x^{5}e^{x}$: & $0.145532940573$ & $0.145532940573$ \\
$x^{10}e^{x}$: & $0.0838770701084$ & $0.0838770701034$ \\
$x^{15}e^{x}$: & $0.0590209960938$ & $0.0590175408793$ \\
$x^{20}e^{x}$: & $0.0$ & $0.0455448840758$ \\
$x^{25}e^{x}$: & $1073741824.0$ & $0.0370862144237$ \\
$x^{30}e^{x}$: & $-1.80143985095 \cdot 10^{16}$ & $0.0312796739322$ \\
$x^{35}e^{x}$: & $6.04462909807 \cdot 10^{23}$ & $0.0270462894091$ \\
$x^{40}e^{x}$: & $0.0$ & $0.023822728669$ \\
$x^{45}e^{x}$: & $0.0$ & $0.0212860390856$ \\
$x^{50}e^{x}$: & $1.46150163733 \cdot 10^{48}$ & $0.0192377544343$ \\
\hline
\end{tabular}
\caption{Inaccuracy of values computed using an unstable algorithm.}
\label{table:unstable_computation}
\end{table}

The algorithms that we have studied to solve linear systems have different levels of stability.
LU decomposition (with pivoting) is usually good enough, but there are some pathological examples of matrices that cause it to break down.
QR decomposition (with pivoting) is generally considered to be a better option than the LU decomposition.
Solving a linear system using the SVD is even more stable than the QR decomposition.
(Pivoting is a modification that is commonly made to the LU decomposition and QR decomposition algorithms we have discussed in earlier labs to make them more stable.)
Unfortunately, in this case, the algorithms that are more stable are also slower.
The LU decomposition is used by \li{scipy.linalg.solve()}.
The SVD is used by \li{scipy.linalg.lstsq()}.
Here is some code that uses the QR decomposition of a matrix $A$ to solve the linear system $A x = b$ for $x$.
It uses a lower-level function included in SciPy to perform the back substitution required to solve this system.
\begin{lstlisting}
from scipy import linalg as la
from scipy.linalg.flapack import dtrtrs
def qr_solve(A, b):
    Q, R = la.qr(A)
    return dtrtrs(R.T, Q.T.dot(b), lower=1, trans=1)[0]
\end{lstlisting}
A solution using a pivoted QR decomposition would be better, but this will be good enough for demonstration purposes.

The following are routines that generate matrices designed to show the relative benefits of each of these algorithms.
\begin{lstlisting}
from numpy.random import rand

def bad_arr_1(n):
    """ Construct a specific pathological example
    that breaks LU decomposition. These examples
    are very rare, but they do exist.
    Strictly speaking, the condition number
    for this matrix isn't terribly bad. """
    A = - np.ones((n, n))
    A[:,:-1] = np.tril(A[:,:-1])
    np.fill_diagonal(A, 1)
    A[:,-1] = 1
    return A

def bad_arr_2(n, peturbation = 1E-8):
    """ Construct another matrix that is nearly singular
    by computing A.dot(A.T) for a matrix A that is
    not square and then adding some small changes
    so it is not exactly singular. """
    A = rand(n, n // 2)
    return A.dot(A.T) + peturbation * rand(n, n)
\end{lstlisting}
\end{example}

\begin{problem}
For each of the array creation routines above, plot the error $\norm{\text{solve}\left(A, A b\right) - b}$ of each of the methods mentioned above for solving a linear system.
Use a log-scaled $y$-axis.
What do you observe?
\end{problem}

%\subsection*{Stable vs. Backward Stable Algorithms}

%\subsection*{Table}
%
%\begin{table}[h]
%\begin{tabular}{|l|l|l|}
%\hline \textbf{Well-Conditioned} & \textbf{Ill-Conditioned} & \textbf{Systems of Equations} \\
%{\parbox{0.3\textwidth}{\raggedleft
%            \begin{itemize}[leftmargin=*]
%                \item finding eigenvalues of a symmetric (or normal) matrix
%                \item calculating $e^x$ for relatively small values of $x$
%                \item calculating $\ln(x)$ for $x$ not close to $1$
%            \end{itemize} }}           &
%
%{\parbox{0.3\textwidth}{\raggedleft
%            \begin{itemize}[leftmargin=*]
%                \item calculating $x_1 - x_2$ when $x_1 \approx x_2$
%                \item computing roots of a polynomial, given the coefficients
%                \item computing eigenvalues of a non-symmetric matrix
%            \end{itemize} }}              &
%
%{\parbox{0.3\textwidth}{
%            \begin{itemize}[leftmargin=*]
%                 \item relative condition number is \[ \mathcal{K} = ||A|| ||A^{-1}|| \]
%            \end{itemize} }}   \\ \hline
%\end{tabular}
%\end{table}
%
%
%\begin{table}[h]
%\begin{tabular}{|l|l|}
%\hline \textbf{Stable} & \textbf{Unstable} \\
%{\parbox{0.45\textwidth}{\raggedleft
%            \begin{itemize}[leftmargin=*]
%                \item finding eigenvalues of a symmetric (or normal) matrix
%                \item calculating $e^x$ for relatively small values of $x$
%                \item calculating $\ln(x)$ for $x$ not close to $1$
%            \end{itemize} }}           &
%
%{\parbox{0.45\textwidth}{\raggedleft
%            \begin{itemize}[leftmargin=*]
%                \item calculating $x_1 - x_2$ when $x_1 \approx x_2$
%                \item computing roots of a polynomial, given the coefficients
%                \item computing eigenvalues of a non-symmetric matrix
%            \end{itemize} }}    \\ \hline
%\end{tabular}
%\end{table}
%


\subsection{Engel curves and income elasticity of demand}

The Engel curve shows demand for a good as a function of income.

Derivative
\(x_{di}=x_{di}(I, \mathbf p)\)

\(\dfrac{\delta }{\delta I}x_{di}(I, \mathbf p)\)

Income elasticity of demand
\(\xi_i =\dfrac{\delta x_{di}}{\delta I}\dfrac{I}{x_{di}}\)


\chapter{Electrodynamics}
In this chapter, we would like to present an important example of field theory. We want to focus on the Hamiltonian description of electrodynamics and see that it yields the classical results. The Lagrangian of the free electromagnetic field without charges is
\begin{align}
L[A^{\mu}(\bar{x}), \dot{A}^{\mu}(\bar{x})]  = - \frac{1}{4} \displaystyle\int d^3 x \ F_{\mu \nu}(\bar{x}) F^{\mu \nu}(\bar{x}),
\end{align}
where we use natural units and
\begin{align}
F_{\mu \nu}(\bar{x}) = \partial_{\mu} A_{\nu}(\bar{x}) - \partial_{\nu} A_{\mu}(\bar{x})
\end{align}
is the antisymmetric field tensor. Since it is expressed through the vector potential $A^{\mu}(\bar{x})$, the vector potential takes the role of our field $\varphi(\bar{x})$. Note that we have four different fields, since $\mu = 0,1,2,3$.
That's why we define the generalized momenta to be
\begin{align}
\pi_{\rho}(\bar{y}) = \frac{\delta L}{\delta \dot{A}^{\rho}(\bar{y})}.
\end{align}
Inserting the Lagrangian, one gets
\begin{align}
\pi_{\rho}(\bar{y}) &= \frac{\delta L}{\delta \dot{A}^{\rho}(\bar{y})} = - \frac{1}{4} \int d^3 x \ \frac{\delta \left( F_{\mu \nu}(\vec{x}) F^{\mu \nu}(\bar{x}) \right)}{\delta \dot{A}^{\rho}(\bar{y})} \notag \\
&= - \frac{1}{2} \int d^3 x \ F_{\mu \nu}(\bar{x}) \ \frac{\delta \left(\partial^{\mu} A^{\nu}(\bar{x}) - \partial^{\nu} A^{\mu}(\bar{x}) \right)}{\delta \dot{A}^{\rho}(\bar{y})} \notag \\
&= - \frac{1}{2} \int d^3 x \ F_{\mu \nu}(\bar{x}) \left( \delta_0^{\mu} \ \frac{\delta \dot{A}^{\nu}(\bar{x})}{\delta \dot{A}^{\rho}(\bar{y})} - \delta_0^{\nu} \ \frac{\delta \dot{A}^{\mu}(\bar{x})}{\delta \dot{A}^{\rho}(\bar{y})} \right) \notag \\
&= - \frac{1}{2} \int d^3 x \ F_{\mu \nu}(\bar{x}) \left( \delta_0^{\mu} \delta_{\rho}^{\nu} \ \delta^3(\bar{x} - \bar{y}) - \delta_0^{\nu} \delta_{\rho}^{\mu} \ \delta^3(\bar{x} - \bar{y}) \right) \notag \\
&= - \frac{1}{2} \int d^3 x \ \left( F_{0 \rho}(\bar{x}) \ \delta^3(\bar{x} - \bar{y}) - F_{\rho 0}(\bar{x}) \ \delta^3(\bar{x} - \bar{y}) \right) \notag \\
&= - \frac{1}{2} \left( F_{0 \rho}(\bar{y}) - F_{\rho 0}(\bar{y}) \right) = F_{\rho 0}(\bar{y}) .
\end{align}
Using the explicit formula for $F_{\mu \nu}$, the generalized momenta can be written as
\begin{align}
\pi_{\rho}(\bar{y}) = \partial_{\rho} A_0(\bar{y}) - \dot{A}_{\rho}(\bar{y}).
\end{align}
\label{sec:electrodynamics_primary_constraints}
To find the Hamiltonian, one has to invert this equation and express $\dot{A}_{\rho}(\vec{y})$ through $\pi_{\rho}(\vec{y})$ and the field components. We see that this won't be possible for $\rho = 0$, we have a primary constraint:
\begin{align}
\pi_0(\bar{y}) = \partial_0 A_0(\bar{y}) - \dot{A}_0(\bar{y}) = 0 \ \ \ \Longrightarrow \ \ \ \phi_1 = \pi_0(\bar{y}) = 0 \ \ \forall \bar{y} \in \mathbb{R}^3.
\end{align}
More exactly, we have infinitly many constraints because the momentum $\pi_0(\bar{y})$ vanishes in every point in space. This corresponds to the gauge freedom of $A^0(\bar{x})$ as we will see later. No matter how we choose it, the conjugated momentum vanishes identically. For the other components, everything is fine and no more constraints arise. \\
\label{sec:electrodynamics_hamiltonian}
The Hamiltonian is therefore
\begin{align}
H[A^{\mu}(\bar{x}), \pi_{\mu}(\bar{x})] &= \int d^3 x \ \dot{A}^{\mu}(\bar{x}) \pi_{\mu}(\bar{x}) \ - \ L[A^{\mu}(\bar{x}), \dot{A}^{\mu}(\bar{x})] \notag \\
&= \int d^3 x \ \dot{A}^{\mu}(\bar{x}) \pi_{\mu}(\bar{x}) \ + \  \frac{1}{4} \displaystyle\int d^3 x \ F_{\mu \nu}(\bar{x}) F^{\mu \nu}(\bar{x}) \notag \\
&= \int d^3 x \left( \dot{A}^{i}(\bar{x}) \pi_{i}(\bar{x}) + \frac{1}{2} F_{i 0}(\bar{x}) F^{i 0}(\bar{x}) + \frac{1}{4} F_{i k}(\bar{x}) F^{i k}(\bar{x}) \right) \notag \\
&= \int d^3 x \left( \left( \pi_i(\bar{x}) - \partial_i A_0(\bar{x}) \right) \pi_i(\bar{x}) + \frac{1}{2} \pi_i(\bar{x}) (- \pi_i(\bar{x})) + \frac{1}{4} F_{i k}(\bar{x}) F^{i k}(\bar{x}) \right) \notag \\
&= \int d^3 x \left( \frac{1}{2} \pi_i(\bar{x})\pi_i(\bar{x}) - \partial_i A_0(\bar{x}) \pi_i(\bar{x}) + \frac{1}{4} F_{i k}(\bar{x}) F^{i k}(\bar{x}) \right) \notag \\
&= \int d^3 x \left( \frac{1}{2} \pi_i(\bar{x})\pi_i(\bar{x}) + A_0(\bar{x}) \partial_i \pi_i(\bar{x}) + \frac{1}{4} F_{i k}(\bar{x}) F^{i k}(\bar{x}) \right),
\end{align}
where in the last step we integrated by parts and assumed that the fields vanish at infinity. Terms like $\pi_i(\bar{x})\pi_i(\bar{x})$, where both indices are down or up, denote usual summation. \\
The first term contains the momenta and is equivalent to the kinetic energy. The last term contains spatial derivatives of the fied components and is therefore equivalent to a potential energy. The second term is a mix of kinetic and potential energy and should not appear in a regular Hamiltonian. We have to check whether the consistency condition for our primary constraint is fulfilled or we have secondary constraints. \\
The total Hamiltonian is
\begin{align}
H_T = H + \int d^3 x \ u(\bar{x}) \pi_0(\bar{x}).
\end{align}
And the equation of motion is given by
\begin{align}
\dot{g} = \left \{ g,H_T \right \} = \left \{ g,H \right \} + \int d^3 x \ u(\bar{x}) \left \{ g,\pi_0(\bar{x}) \right \},
\end{align}
where the Poisson bracket is given by
\begin{align}
\left \{ f,g \right \} = \int d^3 z \left( \frac{\delta f}{\delta A^{\mu}(\bar{z})} \frac{\delta g}{\delta \pi_{\mu}(\bar{z})} - \frac{\delta g}{\delta A^{\mu}(\bar{z})} \frac{\delta f}{\delta \pi_{\mu}(\bar{z})}  \right),
\end{align}
analog to the definition in field theory. It follows that
\begin{align}
0 \overset{!}{=} \dot{\pi}_0(\bar{y}) &= \left \{ \pi_0(\bar{y}),H(\bar{y}) \right \} + \int d^3 x \ u(\bar{x}) \left \{ \pi_0(\bar{y}),\pi_0(\bar{x}) \right \} \notag \\
&= - \int d^3 z \ \frac{\delta \pi_0(\bar{y})}{\delta \pi_{\mu}(\bar{z})} \frac{\delta H(\bar{y})}{\delta A^{\mu}(\bar{z})} = - \frac{\delta H(\bar{y})}{\delta A^0(\bar{y})} \notag \\
&= - \int d^3 x \ \partial_i \pi_i(\bar{x}) \frac{\delta A^0(\bar{x})}{\delta A^0(\bar{y})} = - \partial_i \pi_i(\bar{y}).
\end{align}
\label{sec:electrodynamics_secondary_constraints}
So we really get a secondary constraint which is
\begin{align}
\phi_2 = \partial_i \pi_i = \text{div} \ \bar{\pi} = 0.
\end{align}
Again we have to proof that this constraint is fulfilled at every moment (if not, then the primary constraint wouldn't be fulfilled at every moment). \\
It follows that
\begin{align}
0 \overset{!}{=} \frac{d}{dt} \Big( \partial_i \pi_i(\bar{y}) \Big) &= \left \{ \partial_i \pi_i(\bar{y}),H(\bar{y}) \right \} + \int d^3 x \ u(\bar{x}) \left \{ \partial_i \pi_i(\bar{y}),\pi_0(\bar{x}) \right \} \notag \\
&= - \int d^3 z \ \frac{\delta (\partial_i B_i(\bar{y}))}{\delta B_{\mu}(\bar{z})} \frac{\delta H(\bar{y})}{\delta A^{\mu}(\bar{z})} = - \int d^3 z \  \partial_i \left(\delta^3(\bar{y} - \bar{z})\right) \frac{\delta H(\bar{y})}{\delta A^i(\bar{z})} \notag \\
&= - \partial^i \left( \frac{\delta H(\bar{y})}{\delta A^i(\bar{y})} \right),
\end{align}
where the variation is given by
\begin{align}
\frac{\delta H(\bar{y})}{\delta A^i(\bar{y})} &= \frac{\delta}{\delta A^i(\bar{y})} \left( \frac{1}{4} \int d^3 x \ F_{lk}(\bar{x})F^{lk}(\bar{x}) \right) \notag \\
&= \frac{1}{2} \int d^3 x \ F_{lk}(\bar{x}) \frac{\delta F^{lk}(\bar{x})}{\delta A^i(\bar{y})} \notag \\
&= \frac{1}{2} \int d^3 x \ F_{lk}(\bar{x}) \frac{\delta (\partial^l A^k(\bar{x}) - \partial^k A^l(\bar{x}))}{\delta A^i(\bar{y})} \notag \\
&= \frac{1}{2} \int d^3 x \ F_{lk}(\bar{x}) \left( \delta_i^k  \partial^l \left(\delta^3(\bar{x} - \bar{y})\right) - \delta_i^l  \partial^k \left(\delta^3(\bar{x} - \bar{y})\right) \right) \notag \\
&= \frac{1}{2} \int d^3 x \ \left( - \partial^l F_{li}(\bar{x}) + \partial^k F_{ik}(\bar{x}) \right) \delta^3(\bar{x} - \bar{y}) \notag \\
&= - \partial^k F_{ki}(\bar{y}).
\end{align}
In the end, we get
\begin{align}
\frac{d}{dt} \Big( \partial_i \pi_i(\bar{y}) \Big) = \partial^i \partial^k F_{ki}(\bar{y}),
\end{align}
which is clearly zero since we have a contraction of a symmetric with an antisymmetric tensor. So the secondary constraint is automatically conserved and we get no more constraints. \\

Now, that our procedure is finished, we can classify the constraints and say which is first-class and which is second-class.
Since both constraints depends only on the momentum, they are first-class. So one can write the generalized Hamiltonian as
\begin{align}
H_E = H + \int d^3 x \ v(\bar{x}) \pi_0(\bar{x}) + \int d^3 x \ V(\bar{x}) \partial_i \pi_i(\bar{x}),
\end{align}
where $v(\bar{x})$ and $V(\bar{x})$ are arbitrary functions. \\

\label{sec:electrodynamics_transformations}
Since we have two undetermined functions in our Hamiltonian, this corresponds to two degrees of gauge freedom.
Let's see what transformations $g \longrightarrow g' = g + \delta g$ these first-class constraints do generate. \\
We calculate the small shift $\delta g$ analog to the classical discrete case:
\begin{align}
\delta g = \varepsilon_m \left\{ g,\phi_m \right\} \ \ \ \Longrightarrow \ \ \ \delta g = \int d^3 y \ \varepsilon(\bar{y}) \left\{ g,\phi(\bar{y}) \right\}.
\end{align}
We also have a kind of canonical commutation relation
\begin{align}
\left\{ A^{\mu}(\bar{x}), \pi_{\nu}(\bar{y}) \right\} = \delta_{\nu}^{\mu} \ \delta^3(\bar{x} - \bar{y}),
\end{align}
analog to $\left\{ q_i, p_k \right\} = \delta_{ik}$. Now, we are able to determine the gauge transformations generated by the two constraints.

\begin{itemize}
\item Choosing $g = A^{\mu}(\bar{x})$, we get for the first constraint:
\begin{align}
\delta A^{\mu}(\bar{x}) &= \int d^3 y \ \varepsilon(\bar{y}) \left\{ A^{\mu}(\bar{x}), \pi_0(\bar{y}) \right\} \notag \\
&= \int d^3 y \ \varepsilon(\bar{y}) \ \delta_0^{\mu} \ \delta^3(\bar{x} - \bar{y}) \notag \\
&= \delta_0^{\mu} \ \varepsilon(\bar{x}).
\end{align}
So the first constraint only generates transformations
\begin{align}
A_0(\bar{x}) \ \longrightarrow \ A'_0(\bar{x}) = A_0(\bar{x}) + \varepsilon(\bar{x})
\end{align}
and leaves the momentum unchanged since $\left\{ \pi_{\mu}(\bar{x}), \pi_0(\bar{y}) \right\} = 0$.
\item The second constraint leaves the momentum unchanged too because of the same reason. Furthermore it leaves $A_0$ unchanged because the constraint contains only spatial terms and the Poisson bracket vanishes. So we have
\begin{align}
\delta A^k(\bar{x}) &= \int d^3 y \ \tilde{\varepsilon}(\bar{y}) \left\{ A^k(\bar{x}), \partial_i \pi_i(\bar{y}) \right\} \notag \\
&= - \int d^3 y \ \tilde{\varepsilon}(\bar{y}) \ \delta_i^k \ \partial^i \left( \delta^3(\bar{x} - \bar{y}) \right) \notag \\
&= \partial^k \tilde{\varepsilon}(\bar{x}).
\end{align}
So the second constraint generates transformations
\begin{align}
A^k(\bar{x}) \ \longrightarrow \ A'^k(\bar{x}) = A^k(\bar{x}) + \partial^k \tilde{\varepsilon}(\bar{x}),
\end{align}
or in vector notation
\begin{align}
\bar{A}(\bar{x}) \ \longrightarrow \ \bar{A}'(\bar{x}) = \bar{A}(\bar{x}) + \nabla \tilde{\varepsilon}(\bar{x}).
\end{align}
\end{itemize}

So one can write the generalized Hamiltonian as
\begin{align}
H_E &= \int d^3 x \left( \frac{1}{2} \pi_i(\bar{x})\pi_i(\bar{x}) + (A_0(\bar{x}) + V(\bar{x})) \partial_i \pi_i(\bar{x}) + \frac{1}{4} F_{i k}(\bar{x}) F^{i k}(\bar{x}) + v(\bar{x}) \pi_0(\bar{x}) \right) \notag \\
&= \int d^3 x \left( \frac{1}{2} \pi_i(\bar{x})\pi_i(\bar{x}) + V(\bar{x}) \partial_i \pi_i(\bar{x}) + \frac{1}{4} F_{i k}(\bar{x}) F^{i k}(\bar{x}) + v(\bar{x}) \pi_0(\bar{x}) \right),
\end{align}
because of the gauge freedom of $A_0$, and we see that no mixed terms with momenta and coordinates appear. The Hamiltonian is regular and can be devided in a kinetic and potential energy part. \\

Let's analyse the spatial part of the Hamiltonian and see what it means in terms of the electric field $\bar{E}$ and the magnetic field $\bar{B}$. \\
Remembering that the electromagnetic field tensor in natural units is
\begin{align}
F^{\mu \nu} =
\left(
\arraycolsep=1.4pt\def\arraystretch{1.2}
\begin{array}{cccc}
0 & - E^1 & - E^2 & - E^3 \\
E^1 & 0 & - B^3 & B^2 \\
E^2 & B^3 & 0 & - B^1 \\
E^3 & - B^2 & B^1 & 0
\end{array} \right) \ \ \
\text{and} \ \ \
\bar{B} = \nabla \times \bar{A},
\end{align}
the momentum is nothing more than the electric field:
\begin{align}
\pi^i = F^{i 0} = E^i.
\end{align}
Moreover, if one notices that
\begin{align}
\bar{B}^2 &= \left( \varepsilon_{ijk} \partial^j A^k \right) \left( \varepsilon_{ilm} \partial^l A^m \right) \notag \\
&= \left( \delta_{jl} \delta_{km} - \delta_{jm} \delta_{kl} \right)  \partial^j A^k \partial^l A^m \notag \\
&= \partial_l A_m(\bar{x}) \partial^l A^m(\bar{x}) - \partial_m A_l(\bar{x}) \partial^l A^m(\bar{x}),
\end{align}
we can rewrite
\begin{align}
F_{i k}(\bar{x}) F^{i k}(\bar{x}) &= \big( \partial_i A_k(\bar{x}) - \partial_k A_i(\bar{x}) \big) \left( \partial^i A^k(\bar{x}) - \partial^k A^i(\bar{x}) \right) \notag \\
&= 2 \left( \partial_i A_k(\bar{x}) \partial^i A^k(\bar{x}) - \partial_i A_k(\bar{x}) \partial^k A^i(\bar{x}) \right) \notag \\
&= 2 \bar{B}^2.
\end{align}
So the spatial part of the Hamiltonian can be written as
\begin{align}
H_E = \int d^3 x \left( \frac{\bar{E}^2(\bar{x})}{2} + \frac{\bar{B}^2(\bar{x})}{2} \right) + \int d^3 x \ V(\bar{x}) \ \nabla \cdot \bar{E}(\bar{x}),
\end{align}
which is the classical result for the energy of the electromagnetic field. The first term is the kinetic term, the second term is the potential and the third term generates the gauge transformation $\bar{A}' = \bar{A} + \nabla \varepsilon$ that leaves the magnetic field $B$ unchanged.


\subsection{Binary operators}

A binary operator takes an additional input.

\begin{itemize}
\item If then - \(\theta \rightarrow \gamma \)
\item Then if - \(\theta \leftarrow \gamma \)
\item Iff - \(\theta \leftrightarrow \gamma \)
\item And / Conjunction - \(\theta \land \gamma \)
\item Or / Disjunction - \(\theta \lor \gamma \)
\end{itemize}


\chapter{ELEC 220 CheatSheet}
\begin{multicols}{3}

\section{Ch.1 DC Conduction}
$\sigma$ = conductivity (S/m) and $ \rho$ = resistivity ($ \Omega $m). $\sigma = 1 /\ \rho $.
\begin{table}
\begin{tabular}{c|c}
	Hall coefficient    & $R_H= \frac{v_D}{J}=\frac{-1}{eN_e}=(-eN_e)^{-1}$ \\
	Ohm's Law  & $J = \sigma \cdot E \quad A /\ m^{2}$\\
	Resistance & $R= \frac{\rho \cdot  L}{A}= \frac{L}{\sigma \cdot A}$ \\
	Drude Model  & $J = -eN_ev_D$ \\
	Viscosity Model  & $v_D=\frac{-e\tau}{m_e}E$
\end{tabular}
\caption{Some Equations for DC Conduction}
\end{table}
$$\textbf{Scattering time Formula:} \quad \sigma= \frac{e^2N_e\tau}{m_e} $$

\section{Ch. 2 AC Conduction}
\begin{align}
\textbf{Skin Depth:} \quad \delta=
\left(\frac{2}{\omega \mu \sigma}\right)^{1/2} \\
E= E_0e^{-i(wt-z/\ \delta)}e^{-z/\ \delta} \\
I \sim e^{-2z /\ \delta} \\
I \propto |E|^{2} \\
\omega = \frac{2\pi c}{\lambda_o}
\end{align}
\begin{align}
\textbf{Plasma Frequency:} \quad \omega_p=
\left(\frac{N_ee^2}{m \epsilon}\right)^{1/2} \\
k^2= \omega^2 \mu \epsilon - \frac{N_ee^2\mu}{m}= \omega^2\mu\epsilon\left(1-\frac{\omega_p^2}{\omega^2}\right)
\end{align}
%\def\bsq#1{%both single quotes
%	\lq{#1}\rq}
\section{Ch. 3 DC/AC Dielectrics}
\begin{align}
\textbf{Relative Permittivity:} \quad = \frac{C^\prime}{C}=\frac{Q^\prime}{Q}
= \epsilon_r \\
PA=Q^\prime - Q = C^\prime V - CV= CV(\epsilon_r-1) \\
P= \epsilon_0E(\epsilon_r-1) \ and \ \epsilon_r=1+\frac{P}{\epsilon_0E}= 1+\chi \\
D = \epsilon E = \epsilon_0 \epsilon_r E = \epsilon_0 E+ P \\
P = Np = Nqd \\
\epsilon_= 1 + \frac{N \alpha_e}{\epsilon_0}=1+\chi
\end{align}
\begin{align}
\textbf{Debye Model:} \quad
\epsilon_d(\omega)= \frac{\epsilon_d(0)}{1-i\omega \tau_r}=\epsilon_d^\prime+i\epsilon_d^{\prime \prime}\\
\epsilon_d^\prime= \frac{\epsilon_d(0)}{1+\omega^2 \tau_r^2} \quad and \quad \epsilon_d^{\prime \prime}(\omega)= \frac{\epsilon_d(0)}{1+\omega^2 \tau_r^2}\omega \tau_r
\end{align}
Different Polarization Mechanisms(Decreasing Speed)
Electronic	\\
Ionic	\\
Dipolar (Orientational)	\\
Space Charge (Interfacial)	\\
Ferroelectric
\section{Ch. 4 AC Dielectrics Cont'd}
\begin{align}
\nu =\frac{c}{n}= \frac{1}{\sqrt{\epsilon_r \epsilon_o \mu_o}} \\
n = \sqrt{\epsilon_r} \quad \text{reflactive index} \\
\sin(\theta_c)=\frac{n_1}{n2} \\
k_{imag}=\frac{\omega \epsilon_r^{\prime \prime}}{2c\sqrt{\epsilon_r^\prime}}= \frac{\omega}{2c}\sqrt{\epsilon_r^\prime}\tan{\delta} \\
dB = 8.69 \times k_{imag}
\end{align}
\section{Ch. 8 Schrodinger's Equation}
\begin{align}
\textbf{Planck's constant} \quad \hbar = \frac{h}{2 \pi} \\
1.6 \times 10^{-19}J= 1 eV	\\
p = \frac{h}{\lambda} \\
E = \frac{\hbar^2 k^2}{2m}= \frac{n^2h^2}{8mL^2} \\
k=(2mE)^{1/2}h^{-1}	\\
\int_{0}^{L} \psi^2 dz = 1 = A_n^2 \int_{0}^{L} sin^2(n \pi z /L) dz = \frac{A_n^2}{2}L \\
<S> = \frac{\int \psi^*S\psi dV}{\int \psi^*\psi dV}=\int \psi^*S\psi dV
\end{align}
The wavefunction y is complex valued
• We interpret the absolute value of y squared
(i.e., $\psi \times \psi^*$) as the probability that the
particle is in a given position
• This requires appropriate normalization over
space so that the total probability that the
particle is anywhere is 1 (i.e., the particle
exists)
\section{Ch. 12 Free Electron Theory of Metals}
\begin{align}
\textbf{1D Box:} \quad k_F = \frac{N \pi}{2 L} \\
E_F=\frac{\hbar^2k_F^2}{2m}=
\frac{h^2}{32m}\left(\frac
{N}{L}\right)^2 \\
Z(E)=\frac{dN(E)}{dE}= CE^{-1/2} \\
\textbf{2D Box:} \quad k_F^2 = 2 \pi \frac{N}{L^2} \\
E_F=\frac{\hbar^2k_F^2}{2m}=
\frac{h^2}{4 \pi m}\frac
{N}{L^2}	\\
Z(E)=\frac{dN(E)}{dE}= C \\
\textbf{3D Box:} \quad k_F^3 = 3 \pi^2 \frac{N}{L^3} \\
E_F=\frac{\hbar^2k_F^2}{2m}=
\frac{h^2}{2m}\left(\frac
{3N}{8 \pi L^3}\right)^{2/3} \\
\quad Z(E)=\frac{dN(E)}{dE}= \frac{4\pi L^3 (2m)^{3/2}}{h^3}
E^{1/2}=CE^{1/2}
\end{align}
\subsection{Fermi Distribution}
\begin{align}
F(E)= 1 \ if \ E < E_F \\
F(E)= 0 \ if \ E > E_F \\
F(E)= \frac{1}{1+e^{\frac{E-E_F}{k_BT}}} \\
E_{tot}= \int EZ(E)F(E)dE
\end{align}
\section{Ch. 13 Band Theory}
\begin{align}
V= V_0 \cos\left( \frac{2 \pi x}{a}\right) \\
v_g = \frac{ \partial \omega}{ \partial k} =\frac{1}{\hbar} \frac{ \partial E}{\partial k} \\
a = \frac{d v_g}{ d t}= \frac{1}{\hbar} \frac{ \partial^2 E}{ \partial k^2}
\frac {d k}{d t}	\\
F = \frac {dp}{dt}= \hbar \frac{k}{t} \\
m^*=\frac{F}{a}=\frac{\hbar^2}
{\frac{\partial^2 E}{\partial k^2}}
\end{align}
\section{Ch. 14 Metals and Insulators}
\begin{align}
\sigma = \frac{v_F^2Z(E_F)}{3}e^2\tau_F \\
\nu = \frac{E_g}{h}
\end{align}
\section{Ch. 15 Semiconductors}
\begin{align*}
\textbf{Total \# of Electrons in
	Conduction Band:} N_e = N_c \cdot e^{\left(\frac{E_c-E_F}{k_BT}\right)} \\
N_c = 2 \left[\frac{2\pi m_e^*kT}{h^2}^{3/2}\right] \\
\textbf{Total \# of Hole in
	Valence Band:} \quad N_h = N_v \cdot e^{\left(\frac{E_F-E_v}{k_BT}\right)} \\
N_v = 2 \left[\frac{2\pi m_h^*kT}{h^2}^{3/2}\right] \\
E_f=E_v+\frac{E_g}{2}-\frac{1}{2}kTln\left(\frac{N_c}{N-V}\right)=
E_v+\frac{E_g}{2}-\frac{3}{4}kTln\left(\frac{m_e^*}{m_h^*}\right) \\
\textbf{Total Conductivity:} \quad \sigma =e(N_e\mu_e+
N_h\mu_h)=eN_e(\mu_e+\mu_h) \\
\text{Einstein's Relation(Ch.16.3 Diffusion Current)} \quad \frac{D_h}{\mu_h} = \frac{k_bT}{e}
\end{align*}  $N_i$ = Intrinsic Carrier Density. For an Intrinsic semiconductor holes = electrons
$N_i^2=N_vN_c\exp(-E_g/(kT))$. \\ n-type: $N_e \approx N_D \quad and \quad N_h \approx \frac{N_i^2}{N_D}$ Minority carrier in %n-type is
holes \\
p-type: $N_h \approx N_A \ \ and \ \ N_e \approx \frac{N_i^2}{N_A}$
Minority
carrier %in p-type
is
electrons
\end{multicols}


\section{Floquet-Drude Conductivity in Quantum Hall Systems}

The general expression for the conductivity [*Ref: Martin Wackerl Thesis 1.250] with the disorder averaging can be represent as follows
\begin{equation} \label{7.1}
  \begin{aligned}
    \lim_{\omega \to 0}
    \text{Re}[{\sigma}^{xx}(0,\omega)] &=
    \frac{-1}{4\pi\hbar A}
    \int_{\lambda-\hbar\Omega/2}^{\lambda+ \hbar\Omega/2} d\varepsilon
    \qty(
    -\frac{\partial f}{\partial \varepsilon})
    \\
    & \times
    \tr
    \qty[
    {j}^x_0
    \qty(
    \mb{G}^{r} (\varepsilon) - \mb{G}^{a} (\varepsilon)
    )
    {j}^x_0
    \qty(
    \mb{G}^{r}_0 (\varepsilon) - \mb{G}^{a}_0 (\varepsilon)
    )
    ].
  \end{aligned}
\end{equation}
where ${j}^x_0$ and $\mb{G}^{r,a} (\varepsilon)$ are $x$ directional current operator matrix and white noise disorder averaged Green function matrix respectvely defined against to the \textit{Floquet modes} of the system. Here we have assumed that only $s=0$ Fourier component of the current operator is contributing to the conductivity.

\noindent
Now this can be expand in off resonant regime ($\omega\tau_0 \gg 1$)using only central entry Fourier components ($l=l'=0$) of \textit{Floquet modes} mentioned in Eq. \eqref{6.1} as
\begin{equation} \label{7.2}
  \begin{aligned}
    \lim_{\omega \to 0}
    \text{Re}[{\sigma}^{xx}(0,\omega)] & =
    \frac{-1}{4\pi\hbar A}
    \int_{\lambda-\hbar\Omega/2}^{\lambda+ \hbar\Omega/2} d\varepsilon
    \qty(
    -\frac{\partial f}{\partial \varepsilon})
    \\
    & \times
    \frac{1}{V_{k_x}} \sum_{k_x}
    \sum_{n}
    \mel{n,k_x}{
    {j}^x_0
    \qty(
    \mb{G}^{r} (\varepsilon) - \mb{G}^{a} (\varepsilon)
    )
    {j}^x_0
    \qty(
    \mb{G}^{r}_0 (\varepsilon) - \mb{G}^{a}_0 (\varepsilon)
    )
    }
    {n,k_x}
  \end{aligned}
\end{equation}
and one can evaluate these matrix elements as follows
\begin{equation} \label{7.3}
  \begin{aligned}
    \lim_{\omega \to 0}
    \text{Re}[{\sigma}^{xx}(0,\omega)] & =
    \frac{-1}{4\pi\hbar A}
    \int_{\lambda-\hbar\Omega/2}^{\lambda+ \hbar\Omega/2} d\varepsilon
    \qty(
    -\frac{\partial f}{\partial \varepsilon})
    \frac{1}{V_{k_x}} \sum_{k_x} \sum_{n}
    \frac{1}{{L_x}^3} \sum_{{k_x}_1,{k_x}_2,{k_x}_3}
    \sum_{n_1,n_2,n_3}
    \\
    & \times
    \mel{n,k_x}{
    {j}^x_0}
    {n_1,{k_x}_1}
    \mel{{n_1,{k_x}_1}}{
    \qty(
    \mb{G}^{r} (\varepsilon) - \mb{G}^{a} (\varepsilon)
    )}
    {n_2,{k_x}_2} \\
    & \times
    \mel{n_2,{k_x}_2}{
    {j}^x_0}
    {n_3,{k_x}_3}
    \mel{n_3,{k_x}_3}{
    \qty(
    \mb{G}^{r} (\varepsilon) - \mb{G}^{a} (\varepsilon)
    )
    }
    {n,k_x}
  \end{aligned}
\end{equation}
Since we can diagonalize the impurity averaged Green's function using unitary trasnformation ($\mb{T} = \ket{n,k_x}$) [*Ref: Martin Wackerl - Paper] and we can evaluate the matrix element of differece between retarded and advanced Green's function as follows
[*Ref: My report 2.535]
\begin{equation} \label{7.4}
  \mel{{n_1,{k_x}_1}}{
  \mb{T}^{\dagger}
  \qty(
  \mb{G}^{r} (\varepsilon) - \mb{G}^{a} (\varepsilon)
  )\mb{T}}
  {n_2,{k_x}_2} =
  \qty[
  \frac{2i \text{Im}\qty(\mb{T}^{\dagger} \sum^r \mb{T})
  \delta_{n_1,n_2}\delta_{{k_x}_1,{k_x}_2}}
  {
  \qty(
  \frac{1}{\hbar}\varepsilon -
  \frac{1}{\hbar}\varepsilon_{n_1}
  )^2
  + \qty[\text{Im}\qty(\mb{T}^{\dagger} \sum^r \mb{T})]^2
  }]
\end{equation}
and
\begin{equation} \label{7.5}
  \mel{{n_3,{k_x}_3}}{
  \mb{T}^{\dagger}
  \qty(
  \mb{G}^{r} (\varepsilon) - \mb{G}^{a} (\varepsilon)
  )\mb{T}}
  {n,{k_x}} =
  \qty[
  \frac{2i \text{Im}\qty(\mb{T}^{\dagger} \sum^r \mb{T})
  \delta_{n_3,n}\delta_{{k_x}_3,{k_x}}}
  {
  \qty(
  \frac{1}{\hbar}\varepsilon -
  \frac{1}{\hbar}\varepsilon_{n}
  )^2
  + \qty[\text{Im}\qty(\mb{T}^{\dagger} \sum^r \mb{T})]^2
  }]
\end{equation}

\noindent
Then appying the results we derived in previous section \eqref{6.17} we can calculate the conductivity
\begin{equation} \label{7.6}
  \begin{aligned}
    \lim_{\omega \to 0}
    \text{Re}[{\sigma}^{xx}(0,\omega)] & =
    \frac{-1}{4\pi\hbar A}
    \int_{\lambda-\hbar\Omega/2}^{\lambda+ \hbar\Omega/2} d\varepsilon
    \qty(
    -\frac{\partial f}{\partial \varepsilon})
    \frac{1}{V_{k_x}} \sum_{k_x} \sum_{n}
    \frac{1}{{V_{k_x}}^3} \sum_{{k_x}_1,{k_x}_2,{k_x}_3}
    \sum_{n_1,n_2,n_3}
    \\
    & \times
    \frac{e^2B}{{m_e}}
    \delta_{k_x,{k_x}_1}
    \qty(\sqrt{\frac{n+1}{2}} \delta_{n_1,n+1} + \sqrt{\frac{n}{2}}
    \delta_{n_1,n-1})
    \qty[
    \frac{2i \text{Im}\qty(\mb{T}^{\dagger} \sum^r \mb{T})
    \delta_{n_1,n_2}\delta_{{k_x}_1,{k_x}_2}}
    {
    \qty(
    \frac{1}{\hbar}\varepsilon -
    \frac{1}{\hbar}\varepsilon_{n_1}
    )^2
    + \qty[\text{Im}\qty(\mb{T}^{\dagger} \sum^r \mb{T})]^2
    }] \\
    & \times
    \frac{e^2B}{{m_e}}
    \delta_{{k_x}_2,{k_x}_3}
    \qty(\sqrt{\frac{n_2+1}{2}} \delta_{n_3,n_2+1} + \sqrt{\frac{n_2}{2}}
    \delta_{n_3,n_2-1})
    \qty[
    \frac{2i \text{Im}\qty(\mb{T}^{\dagger} \sum^r \mb{T})
    \delta_{n_3,n}\delta_{{k_x}_3,{k_x}}}
    {
    \qty(
    \frac{1}{\hbar}\varepsilon -
    \frac{1}{\hbar}\varepsilon_{n}
    )^2
    + \qty[\text{Im}\qty(\mb{T}^{\dagger} \sum^r \mb{T})]^2
    }]
  \end{aligned}
\end{equation}
and this will be modified to
\begin{equation} \label{7.7}
  \begin{aligned}
    \lim_{\omega \to 0}
    \text{Re}[{\sigma}^{xx}(0,\omega)] & =
    \frac{-1}{4\pi\hbar A}
    \int_{\lambda-\hbar\Omega/2}^{\lambda+ \hbar\Omega/2} d\varepsilon
    \qty(
    -\frac{\partial f}{\partial \varepsilon})
    \frac{1}{V_{k_x}} \sum_{k_x} \sum_{n}
    \sum_{n_1,n_2}
    \\
    & \times
    \frac{e^2B}{{m_e}}
    \qty(\sqrt{\frac{n+1}{2}} \delta_{n_1,n+1} + \sqrt{\frac{n}{2}}
    \delta_{n_1,n-1})
    \qty[
    \frac{2i \text{Im}\qty(\mb{T}^{\dagger} \sum^r \mb{T})
    \delta_{n_1,n_2}}
    {
    \qty(
    \frac{1}{\hbar}\varepsilon -
    \frac{1}{\hbar}\varepsilon_{n_1}
    )^2
    + \qty[\text{Im}\qty(\mb{T}^{\dagger} \sum^r \mb{T})]^2
    }] \\
    & \times
    \frac{e^2B}{{m_e}}
    \qty(\sqrt{\frac{n_2+1}{2}} \delta_{n,n_2+1} + \sqrt{\frac{n_2}{2}}
    \delta_{n,n_2-1})
    \qty[
    \frac{2i \text{Im}\qty(\mb{T}^{\dagger} \sum^r \mb{T})
    }
    {
    \qty(
    \frac{1}{\hbar}\varepsilon -
    \frac{1}{\hbar}\varepsilon_{n}
    )^2
    + \qty[\text{Im}\qty(\mb{T}^{\dagger} \sum^r \mb{T})]^2
    }]
  \end{aligned}
\end{equation}
and the only non-zero term would be
\begin{equation} \label{7.8}
  \begin{aligned}
    \lim_{\omega \to 0}
    \text{Re}[{\sigma}^{xx}(0,\omega)] & =
    \frac{-1}{4\pi\hbar A}
    \frac{e^4B^2}{{{m_e}^2}}
    \int_{\lambda-\hbar\Omega/2}^{\lambda+ \hbar\Omega/2} d\varepsilon
    \qty(
    -\frac{\partial f}{\partial \varepsilon})
    \frac{1}{V_{k_x}} \sum_{k_x} \sum_{n}
    \qty(n+1)
    \\
    & \times
    \qty[
    \frac{2i \text{Im}\qty(\mb{T}^{\dagger} \sum^r \mb{T})_{\varepsilon_{n+1}}
    }
    {
    \qty(
    \frac{1}{\hbar}\varepsilon -
    \frac{1}{\hbar}\varepsilon_{n+1}
    )^2
    + \qty[\text{Im}\qty(\mb{T}^{\dagger} \sum^r \mb{T})_{\varepsilon_{n+1}}]^2
    }]
    \qty[
    \frac{2i \text{Im}\qty(\mb{T}^{\dagger} \sum^r \mb{T})_{\varepsilon_{n}}
    }
    {
    \qty(
    \frac{1}{\hbar}\varepsilon -
    \frac{1}{\hbar}\varepsilon_{n}
    )^2
    + \qty[\text{Im}\qty(\mb{T}^{\dagger} \sum^r \mb{T})_{\varepsilon_{n}}]^2
    }]
  \end{aligned}
\end{equation}
\hfill$\blacksquare$

\noindent
Then using the following identity derived in [*Ref: My report 2.509]
\begin{equation} \label{7.9}
  \qty(\frac{1}{\tau(\varepsilon,k_x)})_{ll} =
  -2\text{Im}\qty[\qty(\mb{T}^{\dagger} {\sum}^r \mb{T})_{\varepsilon}]_{ll}
\end{equation}
using central element of the inverse scattering time matrix we can modify our result as
\begin{equation} \label{7.10}
  \begin{aligned}
    \lim_{\omega \to 0}
    \text{Re}[{\sigma}^{xx}(0,\omega)] & =
    \frac{1}{4\pi\hbar A}
    \frac{e^4B^2}{{{m_e}^2}}
    \int_{\lambda-\hbar\Omega/2}^{\lambda+ \hbar\Omega/2} d\varepsilon
    \qty(
    -\frac{\partial f}{\partial \varepsilon})
    \frac{1}{V_{k_x}} \sum_{k_x} \sum_{n}
    \qty(n+1)
    \\
    & \times
    \qty[
    \frac{\qty(\frac{1}{\tau(\varepsilon_{n+1},k_x)})
    }
    {
    \qty(
    \frac{1}{\hbar}\varepsilon -
    \frac{1}{\hbar}\varepsilon_{n+1}
    )^2
    + \qty(\frac{1}{2\tau(\varepsilon_{n+1},k_x)})^2
    }]
    \qty[
    \frac{\qty(\frac{1}{\tau(\varepsilon_{n},k_x)})
    }
    {
    \qty(
    \frac{1}{\hbar}\varepsilon -
    \frac{1}{\hbar}\varepsilon_{n}
    )^2
    + \qty(\frac{1}{2\tau(\varepsilon_{n},k_x)})^2
    }]
  \end{aligned}
\end{equation}

\noindent
We have identited that the inverse scattering time matrix's central element is not $k_x$ dependent we can get the sum over all available momentum space in $x$ direction. However by considering the condition that the center of the force of the oscillator $y_0$ must physically liw within the system $-L_y/2 < y_0 < L_y/2$, one can derive that
\begin{equation} \label{7.11}
 -\frac{m_e\omega_0 Ly}{2\hbar} \leq k_x \leq \frac{m_e\omega_0 Ly}{2\hbar}
\end{equation}
and we can derive that
\begin{equation} \label{7.12}
    \frac{1}{V_{k_x}}\sum_{k_x} = \frac{m_e\omega_0 Ly}{\hbar V_{k_x}} = 1
\end{equation}
Thefore Eq. \eqref{7.10} modified to
\begin{equation} \label{7.13}
  \begin{aligned}
    \lim_{\omega \to 0}
    \text{Re}[{\sigma}^{xx}(0,\omega)] & =
    \frac{e^2 \omega_0^2}{4\pi\hbar A}
    \int_{\lambda-\hbar\Omega/2}^{\lambda+ \hbar\Omega/2} d\varepsilon
    \qty(
    -\frac{\partial f}{\partial \varepsilon})
    \sum_{n}
    \qty(n+1)
    \\
    & \times
    \qty[
    \frac{\qty(\frac{1}{\tau(\varepsilon_{n+1})})
    }
    {
    \qty(
    \frac{1}{\hbar}\varepsilon -
    \frac{1}{\hbar}\varepsilon_{n+1}
    )^2
    + \qty(\frac{1}{2\tau(\varepsilon_{n+1})})^2
    }]
    \qty[
    \frac{\qty(\frac{1}{\tau(\varepsilon_{n})})
    }
    {
    \qty(
    \frac{1}{\hbar}\varepsilon -
    \frac{1}{\hbar}\varepsilon_{n}
    )^2
    + \qty(\frac{1}{2\tau(\varepsilon_{n})})^2
    }]
  \end{aligned}
\end{equation}

\noindent
Then using Fermi-Dirac distribution as our partical distribution function ($f$) for this system
\begin{equation} \label{7.14}
  f(\varepsilon) = \frac{1}{\qty[\exp(\varepsilon - \varepsilon_F)/k_B T]+1}
\end{equation}
where $k_B$ is Botlzmann constant, $T$ is absolute tempurature and $\varepsilon_F$ is Fermi energy of the system. Using above distribution, for extreamly low tempuratures we can appromixate that
\begin{equation} \label{7.15}
  - \pdv{f(\varepsilon)}{\varepsilon} \approx \delta(\varepsilon - \varepsilon_F)
\end{equation}
and this will mpre simplify our derivation of conductivity as
\begin{equation} \label{7.16}
  \begin{aligned}
    \lim_{\omega \to 0}
    \text{Re}[{\sigma}^{xx}(0,\omega)] & =
    \frac{e^2 \omega_0^2}{4\pi\hbar A}
    \sum_{n}
    \qty(n+1)
    \qty[
    \frac{\qty(\frac{1}{\tau(\varepsilon_{n+1})})
    }
    {
    \qty(
    \frac{1}{\hbar}\varepsilon_F -
    \frac{1}{\hbar}\varepsilon_{n+1}
    )^2
    + \qty(\frac{1}{2\tau(\varepsilon_{n+1})})^2
    }]
    \qty[
    \frac{\qty(\frac{1}{\tau(\varepsilon_{n})})
    }
    {
    \qty(
    \frac{1}{\hbar}\varepsilon_F -
    \frac{1}{\hbar}\varepsilon_{n}
    )^2
    + \qty(\frac{1}{2\tau(\varepsilon_{n})})^2
    }]
  \end{aligned}
\end{equation}

\noindent
Now introduce a new paramter with a physical meaning od scattering-induced broading of the Landau level as follows
\begin{equation} \label{6.17}
  \Gamma_n \equiv\Gamma(\varepsilon_n) \equiv \qty(\frac{\hbar }{2\tau(\varepsilon_n)})
\end{equation}
and then we can re-write Eq. \eqref{7.16} as follows
\begin{equation} \label{7.18}
  \begin{aligned}
    \lim_{\omega \to 0}
    \text{Re}[{\sigma}^{xx}(0,\omega)] & =
    \frac{e^2 (\hbar\omega_0)^2}{\pi\hbar A}
    \sum_{n}
    \qty(n+1)
    \qty[
    \frac{\Gamma(\varepsilon_{n+1})
    }
    {
    \qty(
    \varepsilon_F - \varepsilon_{n+1}
    )^2
    + \Gamma^2(\varepsilon_{n+1})
    }]
    \qty[
    \frac{\Gamma(\varepsilon_{n})
    }
    {
    \qty(
    \varepsilon_F - \varepsilon_{n}
    )^2
    + \Gamma^2(\varepsilon_{n})
    }]
  \end{aligned}
\end{equation}
\begin{equation} \label{7.19}
  \begin{aligned}
    \lim_{\omega \to 0}
    \text{Re}[{\sigma}^{xx}(0,\omega)] & =
    \frac{e^2 (\hbar\omega_0)^2}{\pi\hbar A}
    \sum_{n}
    \qty(n+1)
    \qty[
    \frac{\Gamma_{n+1}
    }
    {
    \qty(
    \varepsilon_F - \varepsilon_{n+1}
    )^2
    + \Gamma^2_{n+1}
    }]
    \qty[
    \frac{\Gamma_{n}
    }
    {
    \qty(
    \varepsilon_F - \varepsilon_{n}
    )^2
    + \Gamma^2_{n}
    }]
  \end{aligned}
\end{equation}

\noindent
Now use new dimentionless paramters
\begin{equation} \label{7.20}
  X_F \equiv \frac{\varepsilon_F}{\hbar \omega_0} -\frac{1}{2}
\end{equation}
and
\begin{equation} \label{7.21}
  \gamma_n \equiv \frac{\Gamma_n}{\hbar \omega_0}.
\end{equation}
Therefore the Eq. \eqref{7.19} leads to
\begin{equation} \label{7.22}
  \begin{aligned}
    \lim_{\omega \to 0}
    \text{Re}[{\sigma}^{xx}(0,\omega)] & =
    \frac{e^2}{\hbar}
    \frac{1}{\pi A}
    \sum_{n}
    \qty(n+1)
    \qty[
    \frac{\gamma_{n+1}
    }
    {
    \qty(
    X_F - n - 1
    )^2
    + \gamma^2_{n+1}
    }]
    \qty[
    \frac{\gamma_{n}
    }
    {
    \qty(
    X_F - n
    )^2
    + \gamma^2_{n}
    }]
  \end{aligned}
\end{equation}
and
\begin{equation} \label{7.23}
  \begin{aligned}
    \lim_{\omega \to 0}
    \text{Re}[{\sigma}^{xx}(0,\omega)] & =
    \frac{e^2}{\hbar}
    \frac{1}{\pi A}
    \sum_{n}
    \frac{\qty(n+1)}{\gamma_{n}\gamma_{n+1}}
    \qty[
      \frac{1}
      {
        1 + \qty(\frac{X_F - n -1}{\gamma_{n+1}})^2
      }
    ]
    \qty[
      \frac{1}
      {
        1 + \qty(\frac{X_F - n}{\gamma_{n}})^2
      }
    ]
  \end{aligned}
\end{equation}
\hfill$\blacksquare$

\noindent
Same as above derivation we can derive the transverse conductivity in $y$ direction by using the current operator derived in Eq. \eqref{6.27} as follows
\begin{equation} \label{7.24}
  \begin{aligned}
    \lim_{\omega \to 0}
    \text{Re}[{\sigma}^{yy}(0,\omega)] & =
    \frac{1}{4\pi\hbar A}
    \frac{e^2\hbar^2}{{m^2}}
    \int_{\lambda-\hbar\Omega/2}^{\lambda+ \hbar\Omega/2} d\varepsilon
    \qty(
    -\frac{\partial f}{\partial \varepsilon})
    \frac{1}{V_{k_x}} \sum_{k_x} \sum_{n}
    -\qty(n+1)
    \\
    & \times
    \qty[
    \frac{2i \text{Im}\qty(\mb{T}^{\dagger} \sum^r \mb{T})_{\varepsilon_{n+1}}
    }
    {
    \qty(
    \frac{1}{\hbar}\varepsilon -
    \frac{1}{\hbar}\varepsilon_{n+1}
    )^2
    + \qty[\text{Im}\qty(\mb{T}^{\dagger} \sum^r \mb{T})_{\varepsilon_{n+1}}]^2
    }]
    \qty[
    \frac{2i \text{Im}\qty(\mb{T}^{\dagger} \sum^r \mb{T})_{\varepsilon_{n}}
    }
    {
    \qty(
    \frac{1}{\hbar}\varepsilon -
    \frac{1}{\hbar}\varepsilon_{n}
    )^2
    + \qty[\text{Im}\qty(\mb{T}^{\dagger} \sum^r \mb{T})_{\varepsilon_{n}}]^2
    }]
  \end{aligned}
\end{equation}
and same as above derivation this can be simplified into
\begin{equation} \label{7.25}
  \begin{aligned}
    \lim_{\omega \to 0}
    \text{Re}[{\sigma}^{yy}(0,\omega)] & =
    \frac{e^2}{\hbar}
    \frac{1}{\pi A}
    \frac{1}{e^2B^2}
    \sum_{n}
    \frac{\qty(n+1)}{\gamma_{n}\gamma_{n+1}}
    \qty[
      \frac{1}
      {
        1 + \qty(\frac{X_F - n -1}{\gamma_{n+1}})^2
      }
    ]
    \qty[
      \frac{1}
      {
        1 + \qty(\frac{X_F - n}{\gamma_{n}})^2
      }
    ]
  \end{aligned}
\end{equation}

\hfill$\blacksquare$


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{mathtools}
\usepackage{soul}
\usepackage{float}
\usepackage{listings}

\title{Types and Programming Languages (Pierce)}
\author{Bruno Flores}
\date{September 2021}

\begin{document}

\maketitle

\section{Untyped Arithmetic Expressions}

\subsection{Syntax}

\begin{figure}[h!]
    \begin{verbatim}
        t ::=
            true
            false
            if t then t else t
            0
            succ t
            pred t
            iszero t
    \end{verbatim}
    \caption{BNF}
\end{figure}

\begin{figure}[h!]
    \begin{enumerate}
        \item \{\texttt{true, false, 0}\} \(\subseteq\) \(\mathcal{T}\);
        \item
            if \texttt{t\(_1\)} \(\subseteq\) \(\mathcal{T}\),
            then \{\texttt{succ t\(_1\), pred t\(_1\), iszero t\(_1\)}\} \(\subseteq\) \(\mathcal{T}\);
        \item
            if \texttt{t\(_1\)} \(\subseteq\) \(\mathcal{T}\), \texttt{t\(_2\)} \(\subseteq\) \(\mathcal{T}\),
            and \texttt{t\(_3\)} \(\subseteq\) \(\mathcal{T}\),
            then \texttt{if t\(_1\) then t\(_2\) else t\(_3\)} \(\subseteq\) \(\mathcal{T}\).
    \end{enumerate}
    \caption{Terms, inductively}
\end{figure}

\begin{table}[h!]
    \centering
    \begin{tabular}{c c c}
         \texttt{true} \(\subseteq\) \(\mathcal{T}\) & \texttt{false} \(\subseteq\) \(\mathcal{T}\) & \texttt{0} \(\subseteq\) \(\mathcal{T}\) \\
         \\
         \begin{tabular}{c}
              t\(_1\) \(\subseteq\) \(\mathcal{T}\) \\
              \hline
              \texttt{succ t\(_1\)} \(\subseteq\) \(\mathcal{T}\)
         \end{tabular} & \begin{tabular}{c}
              t\(_1\) \(\subseteq\) \(\mathcal{T}\) \\
              \hline
              \texttt{pred t\(_1\)} \(\subseteq\) \(\mathcal{T}\)
         \end{tabular} & \begin{tabular}{c}
              t\(_1\) \(\subseteq\) \(\mathcal{T}\) \\
              \hline
              \texttt{iszero t\(_1\)} \(\subseteq\) \(\mathcal{T}\)
         \end{tabular} \\
         \\
         \multicolumn{3}{c}{
             \begin{tabular}{c}
                  t\(_1\) \(\subseteq\) \(\mathcal{T}\) t\(_2\) \(\subseteq\) \(\mathcal{T}\) t\(_3\) \(\subseteq\) \(\mathcal{T}\) \\
                  \hline
                  \texttt{if t\(_1\) then t\(_2\) else t\(_3\)} \(\subseteq\) \(\mathcal{T}\)
             \end{tabular}
         }
    \end{tabular}
    \caption{Terms, by inference rules}
    \label{tab:my_label}
\end{table}

\begin{table}[h!]
    \centering
    \begin{tabular}{l l l l}
        S\(_0\) & = & \(\emptyset\) & \\
        S\(_{\textit{i}+1}\) & = & & \{\texttt{true, false, 0}\} \\
        & & \(\cup\) & \{\texttt{succ t\(_1\), pred t\(_1\), iszero t\(_1\)} \(|\) \texttt{t\(_1\)} \(\in\) S\(_i\)\} \\
        & & \(\cup\) & \{\texttt{if t\(_1\) then t\(_2\) else t\(_3\)} \(|\) \texttt{t\(_1\), t\(_2\), t\(_3\)} \(\in\) S\(_i\)\}
    \end{tabular}
    \caption{Terms, concretely}
\end{table}

Let

\begin{center}
    S = \(\underset{i}{\text{\Large\(\cup\)}}\) S\(_i\).
\end{center}

S\(_0\) is empty; S\(_1\) contains just the constants; S\(_2\) contains the constants plus the phrases that can be built with constants and just one \texttt{succ, pred, iszero}, or \texttt{if}; S\(_3\) contains these and all phrases that can be built using \texttt{succ, pred, iszero}, and \texttt{if} on phrases in S\(_2\); and so on. \hl{S collects together all the phrases that can be built this way.}

\subsection{Semantics}

\textit{Operational semantics} specifies the behaviour of a programming language by defining a simple \textit{abstract machine} for it. This machine is "abstract" in the sense that it uses the terms of the language as its machine code, rather than some low-level microprocessor instruction set. For simple languages, a \textit{state} of the machine is just a term, and the machine's behaviour is defined by a \textit{transition function} that, for each state, either gives the next state by performing a step of simplification on the term or declares that the machine has halted. The \textit{meaning} of a term \texttt{t} can be taken to be the final state that the machine reaches when started with \texttt{t} as its initial state.

\hl{Strictly speaking, the above describes the \textit{small-step} style of operational semantics.}

\subsubsection{Run-time error}

From the concept of "stuckness". Intuitively, it characterises situations where the operational semantics does not know what to do because the program has reached a "meaningless state".

\paragraph{Definition} A closed term is \textit{stuck} if it is in normal form but not a value.

\end{document}