% To be compiled by XeLaTeX, preferably under TeX Live.
% LaTeX source for ``Yanqi Lake Lectures on Algebra'' Part III.
% Copyright 2019  李文威 (Wen-Wei Li).
% Permission is granted to copy, distribute and/or modify this
% document under the terms of the Creative Commons
% Attribution-NonCommercial 4.0 International (CC BY-NC 4.0)
% https://creativecommons.org/licenses/by-nc/4.0/

% To be included
\chapter{Integral dependence, Nullstellensatz and flatness}

The materials below largely come from \cite[\S 4]{Eis95}, \cite[V.4]{AK70} and \cite[\S\S 5--6]{Mat80}. In what follows, any ring $R$ and the $R$-algebras are assumed to be commutative. For elements $a,b, \ldots$ in an $R$-algebra, denote by $R[a,b, \ldots]$ the $R$-subalgebra generated by them.

\section{Integral extensions}
Consider an $R$-algebra $A$. Recall that to give an $R$-algebra $A$ is the same as to give a ring homomorphism $\varphi: R \to A$. We shall switch freely between algebra and homomorphisms, omitting $\varphi$ if necessary. In many concrete circumstances $R$ is simply a subring of $A$.

\begin{definition}\index{integrality}
	An element $x \in A$ is said to be \emph{integral} over $R$ if there exists a monic polynomial $P(X) = X^n + a_{n-1} X^{n-1} + \cdots + a_0 \in R[X]$ (with $n \geq 1$) such that $P(x)=0$. If every $x \in A$ is integral over $R$, we say $A$ is integral over $R$.
\end{definition}
Note that elements from $R$ are trivially integral: take $P(X)$ with $n=1$. In the case $A = \CC$ and $R = \Z$, we recover the notion of \emph{algebraic integers}.

When $R$ is a field, we usually say \emph{algebraic} instead of \emph{integral}. The monic assumption on $P$ is crucial when $R$ is not a field, as illustrated by the following proof.

\begin{proposition}
	An element $x \in A$ is integral over $R$ if and only if there exists an $R$-submodule $M \subset A$ such that
	\begin{compactitem}
		\item $M$ is a finitely generated $R$-module;
		\item $xM \subset M$, thus $M$ is an $R[x]$-module;
		\item $M$ is a faithful $R[x]$-module i.e. $\mathrm{ann}_{R[x]}(M) = \{0\}$, and
	\end{compactitem}
	If $x$ is integral, $M := R[x]$ satisfies the conditions listed above.
\end{proposition}
\begin{proof}
	This is a familiar application of Cayley-Hamilton theorem, which we recall below. If $x$ is integral, say $x^n + a_{n-1} x^{n-1} + \cdots + a_0 = 0$, a straightforward induction shows that
	\[ R[x] = \sum_{i=0}^{n-1} Rx^i. \]
	In particular we may take $M := R[x]$ to be the required submodule, which is faithful as $1 \in R[x]$. Conversely, given a submodule $M$ as above, with generators $x_1, \ldots, x_m$, we make $M$ into an $R[X]$-module by letting the variable $X$ act as multiplication by $x$. Writing $x \cdot x_i = \sum_{j=1}^m a_{ij} x_j$, there is the matrix equation
	\[ (X \cdot 1_{m \times m} - A) \begin{pmatrix} x_1 \\ \vdots \\ x_m \end{pmatrix} = 0, \quad A = (a_{ij})_{1 \leq i,j \leq m} \in \text{Mat}_{m \times m}(R) \]
	over $R[X]$. Now multiply both sides by the cofactor matrix $(X \cdot 1_{m \times m} - A)^\vee$, we get $P(X) x_i = 0$ for all $i$, where $P \in R[X]$ is the characteristic polynomial of $A$, i.e. $P(x)M = \{0\}$. Since $M$ is faithful as an $R[x]$-module, we get $P(x)=0$.
\end{proof}

\begin{corollary}
	The integral elements in an $R$-subalgebra $A$ form a subalgebra. In particular, $A$ is integral over $R$ if and only if it has a set of integral generators.
\end{corollary}
\begin{proof}
	Let $a, b \in A$ be integral elements. One readily checks that
	\begin{itemize}
		\item $R[a,b]$ is finitely generated as an $R$-module (say by certain monomials $a^i b^j$);
		\item $R[a,b]$ is faithful (as an $R[a,b]$-module) because it contains $1$.
	\end{itemize}
	Thus $R[a,b]$ witnesses the integrality of $a+b$ and $ab$, since they both stabilize $R[a,b]$.
\end{proof}

\begin{proposition}\label{prop:integrality-tower}
	Consider ring homomorphisms $R \to A \to B$ such that $A$ is integral over $R$. If $y \in B$ is integral over $A$, then it is integral over $R$.
\end{proposition}
\begin{proof}
	Assume $y^n + a_{n-1} y^{n-1} + \cdots + a_0 = 0$ with $a_0, \ldots, a_{n-1} \in A$ integral over $R$. The $R[a_0, \ldots, a_{n-1}]$-module $R[a_0, \ldots, a_{n-1}][y]$ is also finitely generated over $R$, faithful and preserved by $y$, hence it witnesses the integrality of $y$ over $R$.
\end{proof}

This subalgebra of integral elements in $A$ is called the \emph{integral closure}\index{integral closure} of $R$ in $A$. If the integral closure equals the image of $R$ in $A$, we say $R$ is \emph{integrally closed} in $A$. The integral closure is automatically integrally closed by virtue of Proposition \ref{prop:integrality-tower}.

\begin{definition}\index{normal}
	Let $R$ be an integral domain and denote by $K$ its field of fractions. The integral closure of $R$ in $K$ is called the \emph{normalization} of $R$. The domain $R$ is said to be \emph{normal} if $R$ is integrally closed in $K$.
\end{definition}

The first examples of normal domains come from unique factorization domains (UFD) that you have seen in undergraduate algebra, including $\Z$, $\Q[X,Y]$, etc.

\begin{proposition}
	Unique factorization domains are normal.
\end{proposition}
\begin{proof}
	Given $x = r/s \in K$ with coprime $r,s \in R$. If there is an integral dependence relation $x^n + a_{n-1} x^{n-1} + \cdots + a_0 = 0$, we will have $r^n + a_{n-1} r^{n-1}s + \cdots + a_0 s^n = 0$, hence $s \mid r^n$. As $r$ is coprime to $s$, we see $s \in R^\times$ and $x \in R$.
\end{proof}

Let us show that taking integral closure commutes with localizations. Geometrically, this means that taking integral closure is a local operation on $\Spec(R)$.

\begin{lemma}
	Let $A$ be an $R$-algebra, $S$ be a multiplicative subset of $R$. Denote by $\tilde{R}$ the integral closure of $R$ in $A$. Then the integral closure of $R[S^{-1}]$ in $A[S^{-1}]$ equals $\tilde{R}[S^{-1}]$.
\end{lemma}
\begin{proof}
	Suppose that $x \in A$ satisfies $x^n + \sum_{i=0}^{n-1} b_i x^i = 0$ with $b_0, \ldots, b_{n-1} \in R$. For all $s \in S$ we deduce $\left(\frac{x}{s}\right)^n + \sum_{i=0}^{n-1} \frac{b_i}{s^{n-i}} \cdot \left( \frac{x}{s} \right)^i = 0$ in $A[S^{-1}]$. Therefore $\tilde{R}[S^{-1}]$ is in the integral closure of $R[S^{-1}]$ in $A[S^{-1}]$.

	Conversely, suppose that $x/s \in A[S^{-1}]$ satisfies
	\[ \left(\frac{x}{s}\right)^n + \sum_{i=0}^{n-1} \frac{a_i}{s_i} \left( \frac{x}{s} \right)^i = 0, \quad a_i \in A, \; s_i \in S. \]
	Cleaning denominators, we see that $x s_1 \cdots s_{n-1}$ is integral over $R$, hence $x = \frac{xs_1 \cdots s_{n-1}}{s_1 \cdots s_n} \in \tilde{R}[S^{-1}]$.
\end{proof}

In particular, localizations of a normal domain are still normal. We have the following converse.
\begin{exercise}
	Let $R$ be a domain. If for every maximal ideal $\mathfrak{m}$ of $R$ we have $R_{\mathfrak{m}}$ is normal, then so is $R$ itself. Hint: recall that $R = \bigcap_{\mathfrak{m}} R_{\mathfrak{m}}$ inside the fraction field $K$.
\end{exercise}

\begin{exercise}
	Let $R := \CC[X,Y]/(Y^2-X^3)$. Show that
	\begin{compactenum}[(i)]
		\item $R$ is a domain, or equivalently, $Y^2 - X^3$ is an irreducible polynomial;
		\item the element $x := \bar{Y}/\bar{X} \in \text{Frac}(R)$ does not lie in $R$, where $\bar{X},\bar{Y}$ denote the images of $X,Y \in \CC[X,Y]$;
		\item show that $x^3 \in R$, therefore $R$ is not a normal ring.
	\end{compactenum}
\end{exercise}

\begin{remark}
	The rationale of the previous Exercise comes from singularity. Consider the \emph{cuspidal curve} $C := \{ (x,y) \in \CC^2: y^2=x^3\}$. It has an isolated singularity at $(0,0)$ since $\nabla (y^2-x^3) = (0,0)$ at the origin. On the other hand, it admits a (non-injective) parametrization from $\CC$ by
	\[ t \mapsto (x=t^2, y=t^3). \]
	Following Milnor \cite{Mil68}, a nice way to understand the ``cusp'' at $(0,0)$ is to cut $C$ by a $3$-sphere $S := \{(x,y) \in \CC^2: |x|^2 + |y|^2 = \epsilon \}$ enclosing the origin, with $0 < \epsilon \ll 1$. Writing the parametrization above as $t=re^{i\theta}$ in polar coordinates, we get the equation $r^4 + r^6 = \epsilon$ which has a unique positive root $\rho$. Hence $S \cap C$ is described by the closed curve $\theta \mapsto (\rho^2 e^{2i\theta}, \rho^3 e^{3i\theta})$ in $S \simeq \mathbb{S}^3$. We call $S \cap C$ the \emph{link of singularity} at $(0,0)$; in this case it is equivalent to the $(2,3)$-torus knot, i.e. the \emph{trefoil} shown below.

	\begin{center}\vspace{1.5em}
		% Draw　the trefoil knot using the knots library in TikZ
		\begin{tikzpicture}
			\begin{knot}[
				consider self intersections,
				flip crossing = 2,
				clip width = 7]
			\strand[ultra thick, black]
				(90:2) to[out=180, in=-120, looseness=2]
				(-30:2) to[out=60, in=120, looseness=2]
				(210:2) to[out=-60, in=0, looseness=2] (90:2);
			\end{knot}
		\end{tikzpicture}
	\end{center}

	If we started from a non-singular curve in $\CC^2$, the result would then be an unknotted $\mathbb{S}^1$ inside $\mathbb{S}^3$. In this sense the link provides a measure for the singularity.
\end{remark}

Our study of normality has to be paused here. We refer to \cite[\S\S 8--9]{Mum99} for a beautiful discussion on the algebro-geometric content of normality, as well as important consequences such as Zariski's Main Theorem. Roughly speaking, normality means there is only one branch through each point of the corresponding variety.

\section{Nullstellensatz}
Our aim is to present a generalization of the celebrated \emph{Nullstellensatz}, which is one of the cornerstones of algebraic geometry. We shall also write the nilpotent radical of a ring $R$ as
\[ \text{nil}(R) := \sqrt{0_R}. \]
For an ideal $\mathfrak{a} \subset R$, we write $\mathfrak{a}[X]$ for the ideal of $R[X]$ formed by polynomials with all coefficients lying in $\mathfrak{a}$.

\begin{definition}\label{def:Jacobson-ring}\index{Jacobson ring}
	A ring $R$ is called a \emph{Jacobson ring} if every prime ideal $\mathfrak{p}$ satisfies
	\begin{gather}\label{eqn:Hilbert}
		\mathfrak{p} = \bigcap_{\mathfrak{m}: \text{maximal ideal } \supset \mathfrak{p}} \mathfrak{m}.
	\end{gather}
	Equivalently, we require that the Jacobson radical $\text{rad}(R/\mathfrak{p}) = \text{nil}(R/\mathfrak{p}) = \{0\}$ for all $\mathfrak{p}$. Note that $\supset$ always holds.
\end{definition}
Observations:
\begin{compactitem}
	\item Quotients of Jacobson rings are still Jacobson.
	\item Fields are trivially Jacobson.
\end{compactitem}

\begin{exercise}
	Prove that every principal ideal domain (a domain in which every ideal is generated by one element) with infinitely many maximal ideals is Jacobson.
\end{exercise}

\begin{theorem}[E.\ Snapper]\label{prop:Snapper}
	For any $R$, the polynomial algebra $R[X]$ satisfies $\mathrm{rad}(R[X]) = \mathrm{nil}(R[X])$.
\end{theorem}
\begin{proof}
	To show that $\text{nil}(R[X]) \supset \text{rad}(R[X])$, let $f(X) = \sum_i a_i X^i \in \text{rad}(R[X])$, then $1+Xf(X) = 1 + \sum_i a_i X^{i+1} \in R[X]^\times$. By looking at the reduction modulo $\mathfrak{p}$ of $f(X)$ for every prime ideal $\mathfrak{p}$ of $R$, we see that $a_i \in \bigcap \mathfrak{p} = \text{nil}(R)$ for all $i$. Hence $f(X) \in \text{nil}(R)[X] \subset \text{nil}(R[X])$.
\end{proof}

\begin{lemma}
	Let $R \subset A$ be integral domains such that $A$ is a finitely generated $R$-algebra. If $\mathrm{rad}(R)=\{0\}$, then $\mathrm{rad}(A)=\{0\}$.
\end{lemma}
\begin{proof}
	We may assume that $A$ is generated by a single element $a \in A$ over $R$. If $a$ is transcendental over $K := \text{Frac}(R)$, Theorem \ref{prop:Snapper} above can be applied as $\mathrm{nil}(R[X]) = \{0\}$. Let us assume that $a$ satisfies $f(a)=0$ for some $f(X) = \sum_{i=0}^n r_i X^i \in R[X]$ with $r_n \neq 0$. Let $b \in \text{rad}(A)$ and suppose $b \neq 0$. Hereafter we embed everything into the $K$-algebra $\text{Frac}(A)$. Since $a$ is algebraic over $K$, so is every element from $R[a]$ or even $K[a] \subset \text{Frac}(A)$. Hence $b$ is integral over $K$ as well. By cleaning denominators, we arrive at $g(b)=0$ for some $g(X) = \sum_{i=0}^m s_i X^i \in R[X]$ with the smallest possible degree $m$. Since $A$ is a domain, we have $s_0 \neq 0$.

	Using $\text{rad}(R) =\{0\}$, there exists a maximal ideal $\mathfrak{m}$ of $R$ such that $r_n s_0 \not\in \mathfrak{m}$. Taking localization at $\mathfrak{m}$, we get the subring $A' := A \otimes_R R_{\mathfrak{m}} \subset \text{Frac}(A)$ containing $R_{\mathfrak{m}}$, and $A'$ is also a finitely generated $R_{\mathfrak{m}}$-module since we inverted $r_n$. Nakayama's Lemma for $R_{\mathfrak{m}}$-modules implies $\mathfrak{m} A' \subsetneq A'$, thus $\mathfrak{m} A \subsetneq A$. Now choose a maximal ideal $\mathfrak{m}_A$ of $A$ over $\mathfrak{m}$. We must have $\mathfrak{m}_A \cap R = \mathfrak{m}$, which entails $s_0 \not\in \mathfrak{m}_A$. This is a contradiction since $s_0 = -\sum_{i=1}^m s_i b^i \in \text{rad}(A)$.
\end{proof}

\begin{theorem}[Nullstellensatz]\label{prop:Nullstellensatz-gen}\index{Nullstellensatz}
	Let $A$ be a finitely generated $R$-algebra. Assume that $R$ is a Jacobson ring, then the following statements hold.
	\begin{enumerate}[(i)]
		\item $A$ is a Jacobson ring.
		\item Let $\mathfrak{n} \in \Spec(A)$ be maximal, then its image $\mathfrak{m} \in \Spec(R)$ is maximal as well, and $A/\mathfrak{n}$ is a finite extension of the field $R/\mathfrak{m}$.
	\end{enumerate}
\end{theorem}
\begin{proof}
	We start with (i). One may assume $R \subset A$ from the outset. Condition \eqref{eqn:Hilbert} for $A$ amounts to $\text{rad}(A/\mathfrak{p})=0$ for every $\mathfrak{p} \in \Spec(A)$. Apply the previous Lemma to the integral domains $R/R \cap \mathfrak{p} \subset A/\mathfrak{p}$ to prove (i).

	Now turn to (ii). Using (i) and induction, we may assume that $A=R[a]$ for some $a \in A$. By considering the homomorphism $R/\mathfrak{m} \hookrightarrow A/\mathfrak{n}$ between Jacobson rings, we may further reduce to the case $\mathfrak{n}=\{0\}$ and $\mathfrak{m}=\{0\}$, so that $R$ is a domain embedded in the field $A$. In particular $a$ cannot be transcendental (as $R[X]$ is not a field) and must satisfy $\sum_{i=0}^n c_i a^i = 0$ for some $c_0, \ldots, c_n \in R$ with $c_n \neq 0$. Let $\mathfrak{k}$ be any maximal ideal of $R$ not containing $c_n$, which exists since $\text{rad}(R) = \{0\}$.

	As in the proof of the previous Lemma, $a$ becomes integral over $R_{\mathfrak{k}}$ and Nakayama's Lemma for $R_{\mathfrak{k}}$-modules entails $\mathfrak{k}A \neq A$, hence $\mathfrak{k}=0$ because $A$ is a field. This implies that $R$ is a field and $A$ is a finite extension of $R$.
\end{proof}

\begin{corollary}
	Let $\Bbbk$ be an algebraically closed field, and $A := \Bbbk[X_1, \ldots, X_n]$. The maximal ideals of $A$ are in bijection with $\Bbbk^n$ by attaching to each $x := (x_1, \ldots, x_n) \in \Bbbk^n$ the ideal
	\[ \mathfrak{m}_x = \{f \in A : f(x)=0 \} = (X_1 - x_1, \ldots, X_n - x_n). \]
\end{corollary}
\begin{proof}
	Since $A/\mathfrak{m}_x \rightiso \Bbbk$ by evaluation at $x$, we see $\mathfrak{m}_x$ is indeed maximal. It is routine to show that $x = y \iff \mathfrak{m}_x = \mathfrak{m}_y$. It remains to show that every maximal ideal $\mathfrak{n}$ contains some $\mathfrak{m}_x$. Indeed, Theorem \ref{prop:Nullstellensatz-gen} implies the field $A/\mathfrak{n}$ is algebraic over $\Bbbk$, hence $A/\mathfrak{n} \simeq \Bbbk$ as $\Bbbk$-algebras. Let $x_i$ be the image of $X_i$ under $A \twoheadrightarrow A/\mathfrak{n} \rightiso \Bbbk$ and set $x := (x_1, \ldots, x_n)$, then $\mathfrak{n} \supset \mathfrak{m}_x$ as required.
\end{proof}

\begin{corollary}
	Keep the notations above and set
	\begin{align*}
		Z(\mathfrak{a}) & := \{x \in \Bbbk^n: \forall f \in \mathfrak{a}, \; f(x)=0 \}, \\
		I(\mathcal{X}) & := \{f \in A: \forall x \in X, \; f(x)=0 \}
	\end{align*}
	for ideals $\mathfrak{a} \subset A$ and subsets $\mathcal{X} \subset \Bbbk^n$, then we have $IZ(\mathfrak{a}) = \sqrt{\mathfrak{a}}$ for all $\mathfrak{a}$.
\end{corollary}
If we identify $\Bbbk^n$ with $\MaxSpec(A)$ by the previous Corollary, then $Z(\mathfrak{a})$ is just the intersection of $V(\mathfrak{a}) \subset \Spec(A)$ and $\MaxSpec(A)$. Details are left to the readers.
\begin{proof}
	If $f \in A$ and $f^n \in \mathfrak{a}$ for some $n$, the vanishing of $f^n$ on $Z(\mathfrak{a})$ will entail that of $f$, hence the inclusion $\supset$ holds. Assume conversely that $f \in A$ vanishes on $Z(\mathfrak{a})$. This means: for every maximal ideal $\mathfrak{m}_x$ we have
	\[ \mathfrak{m}_x \supset \mathfrak{a} \iff x \in Z(\mathfrak{a}) \implies f(x)=0 \iff f \in \mathfrak{m}_x. \]
	Hence
	\[ f \in \bigcap_{\mathfrak{m}_x \supset \mathfrak{a}} \mathfrak{m}_x = \sqrt{\mathfrak{a}}, \]
	the last equality being based on Definition \ref{def:Jacobson-ring} since $A/\mathfrak{a}$ is a Jacobson ring. This concludes the $\subset$.
\end{proof}

\begin{remark}
	How about $Z I(\mathcal{X})$? Unwinding definitions, it is seen to equal the set of points that ``satisfy the algebraic equations that $\mathcal{X}$ satisfies.'' The Zariski topology on $\Bbbk^n$ is defined by stipulating the subsets $\{ x \in \Bbbk^n: f(x)=0 \}$ to be closed, for all $f \in A$, so we obtain $ZI(\mathcal{X}) = \bar{\mathcal{X}}$, the Zariski-closure of $\mathcal{X}$. The reader is invited to verify that by identifying $\Bbbk^n$ with $\MaxSpec(A)$, the foregoing topology is induced from the Zariski topology on the prime spectrum $\Spec(A)$.
\end{remark}

An (algebraic, closed) subvariety of $\Bbbk^n$ is the vanishing locus $f_1 = \cdots = f_m = 0$ for some $f_1, \ldots, f_m \in \Bbbk[X_1, \ldots, X_n]$; it is determined by the ideal $\mathfrak{a} = (f_1, \ldots, f_m)$, in fact it depends only on $\sqrt{\mathfrak{a}}$. An ideal $\mathfrak{a}$ is called \emph{radical} if $\sqrt{\mathfrak{a}}=\mathfrak{a}$. To recap, we obtain a dictionary:
\begin{center}\begin{tabular}{c|c}
	Subvariety $\mathcal{X}$ in $\Bbbk^n$ & Radical ideal $\mathfrak{a}$ in $\Bbbk[X_1, \ldots, X_n]$ \\
	Points of $\mathcal{X}$ & $\MaxSpec(A)$, \;$A = A_{\mathcal{X}} := \Bbbk[X_1, \ldots, X_n]/\mathfrak{a}$ \\
	Union of two varieties & product or intersection of two ideals \\
	Intersection of varieties & sum of ideals \\
	Polynomial map $\mathcal{X} \to \mathcal{Y}$ & $\Bbbk$-homomorphism $A_{\mathcal{Y}} \to A_{\mathcal{X}}$ \\
	\vdots & \vdots
\end{tabular}\end{center}

If one allows arbitrary rings $A$ instead of just $\Bbbk[X_1, \ldots, X_n]/\mathfrak{a}$, and consider $\Spec(A)$ instead of $\MaxSpec(A)$ (the latter is well-behaved only for Jacobson rings), the result is the category of \emph{affine schemes}. A proper treatment of these ideas should be left to the Algebraic Geometry course, if it exists......

\section{Flatness: the first glance}
To begin with, we consider a ring $A$ and a module $N$. It has been observed that the additive functor $N \dotimes{A} -: A\dcate{Mod} \to A\dcate{Mod}$ is \emph{right exact}, namely it preserves the exactness of sequences like
\[ \bullet \to \bullet \to \bullet \to 0. \]
If we consider exactness of sequences like $0 \to \bullet \to \bullet \to \bullet$, the corresponding notion is \emph{left exactness}. The same applies to any additive functor $F$ instead of $N \dotimes{A} -$. Being both left and right exact is equivalent to that $F$ preserves all exact sequences; in this case we say $F$ is an \emph{exact functor}.

\begin{definition}\index{flat}\index{faithfully flat}
	We say $N$ is a \emph{flat} $A$-module if $N \dotimes{A} -$ is exact. We say $N$ is \emph{faithfully flat} if for every sequence $M_\bullet = [\cdots \to M_i \to M_{i-1} \to \cdots]$ of $R$-modules, we have $M_\bullet \dotimes{R} N$ is exact if and only if $M_\bullet$ is.
\end{definition}

\begin{remark}
	In view of the right-exactness of $\otimes$, to assure flatness of $N$ it suffices that $N \dotimes{A} -$ preserves kernels.
\end{remark}

Now we consider a ring homomorphism $A \to B$, which makes $B$ into an $A$-algebra. Tensor product now gives an additive functor, often called the \emph{base change}:
\[ B \dotimes{A} -: A\dcate{Mod} \to B\dcate{Mod}. \]
Thus we can also talk about flatness and faithful flatness of $B$ over $A$. Since $B$ is naturally an $A$-module, this notion is compatible with the previous one.
\begin{example}\label{eg:localization-flatness}
	Let $S$ be a multiplicative subset of $A$, then $A[S^{-1}]$ is flat over $A$. It is not faithfully flat in general, however; see Theorem \ref{prop:faithfully-flat-criterion}.
\end{example}

\begin{example}
	A routine fact is that for any family $\left( M^{(i)}_\bullet \right)_{i \in I}$ of complexes of $A$-modules, we have
	\[ \forall i \in I, \; M^{(i)}_\bullet \text{ is exact} \iff \bigoplus_{i \in I} M^{(i)}_\bullet \text{ is exact}. \]
	Recall that $\otimes$ preserves direct sums. It follows that a direct sum of modules is flat if and only if each summand is flat. From this we deduce the flatness of free modules since $A \dotimes{A} M \simeq M$ functorially for each $M$. Furthermore, projective modules are flat as they are direct summands of free modules.
\end{example}

\begin{exercise}
	Show that $\Z/n\Z$ is not flat over $\Z$ for $n > 1$.
\end{exercise}

We list some basic properties below.
\begin{description}
	\item[Tensor products] Let $M,N$ be flat (resp. faithfully flat) $R$-modules, then so is $M \dotimes{R} N$. This follows the associativity constraint of tensor products: $(- \otimes M) \otimes N \simeq - \otimes (M \otimes N)$.
	\item[Transitivity] Given ring homomorphisms $A \to B \to C$, if $B$ is flat (resp. faithfully flat) over $A$ and $C$ is flat (resp. faithfully flat) over $B$, then $C$ is also flat (resp. faithfully flat) over $A$. This follows from the transitivity of base change, namely there is a isomorphism of functors $A\dcate{Mod} \to C\dcate{Mod}$.
	\[ (- \dotimes{A} B) \dotimes{B} C \rightiso - \dotimes{A} C. \]
	\item[Base change] Suppose $N$ is a flat (resp. faithfully flat) $A$-module and $B$ is any $A$-algebra, then $N \dotimes{A} B$ is a flat (resp. faithfully flat) $B$-module. Again, any $B$-module $M$ can be viewed as an $A$-module, and there is a functorial isomorphism
	\[ (N \dotimes{A} B) \dotimes{B} M \rightiso N \dotimes{A} M. \]
\end{description}

\begin{remark}\label{rem:exactness-local}
	A sequence $[ \cdots \to M_i \xrightarrow{d_i} M_{i-1} \to \cdots]$ of $R$-modules is a complex (resp. exact) if and only if so is its localization at $\mathfrak{m}$, for every maximal ideal $\mathfrak{m}$. Indeed:
	\begin{compactitem}
		\item $(M_\bullet, d_\bullet)$ is a complex if and only if $\Image(d_{i-1}d_i)=0$ for all $i$. Since localization is an exact functor, it preserves images and we know a module $N$ is zero if and only if $N_{\mathfrak{m}}=0$ for all $\mathfrak{m}$.
		\item a complex $(M_\bullet, d_\bullet)$ is exact if and only if $\Hm_i(M_\bullet)=0$ for all $i$. The same reasoning applies since localization preserves $H_i$.
	\end{compactitem}
\end{remark}

\begin{proposition}
	The following are equivalent for an $R$-module $N$.
	\begin{inparaenum}[(i)]
		\item $N$ is flat over $R$,
		\item $N_{\mathfrak{p}}$ is flat over $R_{\mathfrak{p}}$ for all prime ideal $\mathfrak{p}$,
		\item $N_{\mathfrak{m}}$ is flat over $R_{\mathfrak{m}}$ for all maximal ideal $\mathfrak{m}$.
	\end{inparaenum}
\end{proposition}
\begin{proof}
	Direct consequence of the exactness of localization and Remark \ref{rem:exactness-local}.
\end{proof}

\begin{lemma}\label{prop:flat-ring-localizations}
	Let $\varphi: R \to R'$ be a ring homomorphism, $\mathfrak{p}' \in \Spec(R')$ maps to $\mathfrak{p} \in \Spec(R)$ under $\varphi^\sharp$. If $\varphi$ is flat, so is the induced homomorphism $R_{\mathfrak{p}} \to R'_{\mathfrak{p}'}$.
\end{lemma}
\begin{proof}
	Set $S = R \smallsetminus \mathfrak{p}$ so that $\varphi(S) \subset R' \smallsetminus \mathfrak{p}'$. Factorize $R_{\mathfrak{p}} \to R_{\mathfrak{p}'}$ as
	\[ R_{\mathfrak{p}} \to \underbracket{R'[\varphi(S)^{-1}]}_{\text{as a ring}} \to R'_{\mathfrak{p}'}. \]
	The first arrow is also the base-change to $R_{\mathfrak{p}}$ of $\varphi$ (as a homomorphism of $R$-modules), whereas the second one is a localization of $R'$. Their composite is therefore flat.
\end{proof}

\begin{proposition}\label{prop:flatness-localized}
	Let $\varphi: R \to R'$ be a ring homomorphism. The following are equivalent:
	\begin{enumerate}[(i)]
		\item $R'$ is flat over $R$,
		\item $R'_{\mathfrak{p}'}$ is flat over $R_{\mathfrak{p}}$ for all $\mathfrak{p}' \in \Spec(R')$ with $\mathfrak{p} = \varphi^\sharp(\mathfrak{p}')$;
		\item \emph{Idem}, but for $\mathfrak{p}' \in \MaxSpec(R')$.
	\end{enumerate}
\end{proposition}
\begin{proof}
	(i) $\implies$ (ii): Set $S := R \smallsetminus \mathfrak{p}$. Base change implies $R'[S^{-1}]$ is flat over $R[S^{-1}] = R_{\mathfrak{p}}$. Since $R'_{\mathfrak{p}'}$ is a localization of $R[S^{-1}]$ (exercise), we conclude by transitivity.

	(ii) $\implies$ (iii): Trivial.

	(iii) $\implies$ (i): By Remark \ref{rem:exactness-local} applied to complexes of $R'$-modules, it suffices to show the exactness of the functor $- \dotimes{R} R'_{\mathfrak{p}'}$ for all $\mathfrak{p}' \in \MaxSpec(R')$. In view of Lemma \ref{prop:flat-ring-localizations}, the factorization of $R \to R'_{\mathfrak{p}'}$ into $R \to R_{\mathfrak{p}} \to R'_{\mathfrak{p}'}$ and the flatness of $R \to R_{\mathfrak{p}}$ show that $R'_{\mathfrak{p}'}$ is indeed flat over $R$.
\end{proof}

\begin{lemma}[Equational criterion of flatness]\label{prop:equational-flatness}
	An $R$-module $N$ is flat if and only if for all $r \geq 1$, $a_1, \ldots, a_r \in R$, $x_1, \ldots, x_r \in N$ verifying $\sum_{i=1}^r a_i x_i = 0$, there exist $s \in \Z_{\geq 1}$, an $R$-valued matrix $B = (b_{ij})_{\substack{1 \leq i \leq r \\ 1 \leq j \leq s }}$ and $y_1, \ldots, y_s \in N$ such that
	\[ \begin{pmatrix} x_1 \\ \vdots \\ x_r \end{pmatrix} = B \begin{pmatrix} y_1 \\ \vdots \\ y_s \end{pmatrix}, \quad \begin{pmatrix} a_1 & \cdots & a_r \end{pmatrix} B = 0. \]
\end{lemma}
\begin{proof}
	Suppose $M$ is flat and consider the exact sequence $0 \to \Ker(f) \to R^{\oplus r} \xrightarrow{f} R$ where $f(t_1, \ldots, t_r) = \sum_i a_i t_i$. We obtain an exact
	\[ 0 \to \Ker(f) \dotimes{A} M \to M^{\oplus r} \xrightarrow{(x_i)_i \mapsto \sum_i a_i x_i} M. \]
	Thus if $(x_1, \ldots, x_r) \mapsto 0$ by the arrow above, we can express it as $\sum_{j=1}^s (b_{1j}, \ldots, b_{rj}) \otimes y_j \in \Ker(f) \dotimes{A} M$ as required.

	To show the converse, we invoke the fact that flatness is equivalent to the injectivity of $\mathfrak{a} \otimes N \to \mathfrak{a} N$ for all finitely generated ideal $\mathfrak{a}$. See Proposition \ref{prop:flat-module}.
\end{proof}

It will be useful to rephrase the condition in Lemma \ref{prop:equational-flatness} as follows: for all
\begin{compactitem}
	\item homomorphism $x: R^{\oplus r} \to M$, where $r \in \Z_{\geq 1}$, and
	\item $K \subset \Ker(x)$: submodule generated by one element,
\end{compactitem}
there exist some $s \in \Z_{\geq 1}$ and a commutative diagram
\[\begin{tikzcd}[column sep=small]
	R^{\oplus r} \arrow[rd, "x"'] \arrow[rr, "B"] & & R^{\oplus s} \arrow[ld, "y"] \\
	& M &
\end{tikzcd} \quad \text{s.t. } K \subset \Ker(B). \]
Indeed, $x$ (resp. $y$) corresponds to $(x_1, \ldots, x_r)$ via $x: (t_1, \ldots, t_r) \mapsto \sum_i t_i x_i$ (resp. $y: (u_1, \ldots, u_s) \mapsto \sum_j u_j, y_j$), and $K \subset \Ker(x)$ corresponds to $R \cdot (a_1, \ldots, a_r)$. The homomorphism $B$ corresponds to the matrix $(b_{ij})_{i,j}$.

\begin{remark}\label{rem:equational-flatness-ext}
	For flat $N$, the equational criterion so rephrased is applicable to any finitely generated $K \subset \Ker(x)$. Indeed, one may iterate the construction for $R^{\oplus s} \xrightarrow{y} M$, etc. to make every generator of $K$ mapped to $0$.
\end{remark}

\section{Structure of flat modules}
Recall from homological algebra that the right exact functor $N \dotimes{A} -: R\dcate{Mod} \to R\dcate{Mod}$ has $(\Tor_i^R(N, -))_{i \geq 0}$ as its left derived functors.
\begin{proposition}\label{prop:flat-module}
	The following are equivalent for an $R$-module $N$:
	\begin{enumerate}[(i)]
		\item $N$ is flat,
		\item $\Tor_i^R(N,-)=0$ for all $i > 0$,
		\item $\Tor_1^R(N,-)=0$,
		\item $\Tor_1^R(N, R/\mathfrak{a})=0$ for all finitely generated ideal $\mathfrak{a}$, or equivalently $\mathfrak{a} \dotimes{R} N \to N$ is injective.
	\end{enumerate}
\end{proposition}
\begin{proof}
	First, the equivalence mentioned in (iv) is a consequence of the exact sequence
	\[ \underbracket{\Tor_1^R(N, R)}_{=0} \to \Tor_1^R(N, R/\mathfrak{a}) \to N \dotimes{R} \mathfrak{a} \to N \to N \dotimes{R} (R/\mathfrak{a}) \to 0 \]
	deduced from $0 \to \mathfrak{a} \to R \to R/\mathfrak{a} \to 0$.

	Clearly (i) $\implies$ (ii) $\implies$ (iii) $\implies$ (iv). To show (iii) $\implies$ (i), note that if $0 \to M' \to M \to M'' \to 0$ is exact, then $\Tor_1^R(N,M'') \to N \otimes M' \to N \otimes M \to N \otimes M'' \to 0$ is exact.

	We show (iv) $\implies$ (i), (ii) or (iii) as follows: $\Tor_1^R(N,M)=0$ can be tested for finitely generated $M$ only, since $\Tor^R_i(N,-)$ preserves filtered inductive limits such as
	\[ M = \varinjlim \left\{ \text{f.g. submodules} \right\}. \]
	We do induction on the minimal number $n$ of generators of $M$. If $M = Rx_1 + \cdots + Rx_n$, put $M' := \sum_{i < n} Rx_i$ so that we have a short exact sequence $0 \to M' \to M \to M'' \to 0$ where $M''$ is generated by the image of $x_n$, hence isomorphic to some $R/\mathfrak{a}$. Since $\Tor_1^R(N,M') \to \Tor_1^R(N, M) \to \Tor_1^R(N, M'')$ is exact, we are reduced to the $n=1$ case, i.e. $M = R/\mathfrak{a}$. It boils down to assure $\mathfrak{a} \otimes N \hookrightarrow N$. Again, using the exactness of filtered $\varinjlim$ and the fact that $\otimes$ respects $\varinjlim$, it suffices to test this on finitely generated $\mathfrak{a}$.

	For a down-to-earth approach, see \cite[\S 6.3]{Eis95}.
\end{proof}

\begin{corollary}
	If $r \in R$ is not a zero divisor, then $r$ is not a zero divisor for any flat $R$-module $N$.
\end{corollary}
\begin{proof}
	Take $\mathfrak{a} :=Rr$, which is $\simeq R$, and contemplate on $N \simeq \mathfrak{a} \dotimes{R} N \hookrightarrow N$.
\end{proof}

\begin{exercise}
	Suppose $R$ is a principal ideal domain. Show that $N$ is flat if and only if $N$ has no zero divisors except $0$. Hint: the ideals take the form $\mathfrak{a} = (t)$, so the condition (iv) amounts to $tx=0 \iff x=0$.
\end{exercise}

\begin{exercise}
	For all field $\Bbbk$, show that
	\begin{compactenum}[(i)]
		\item $\Bbbk[X,Y]/(XY-X)$ is not flat over $\Bbbk[X]$,
		\item $\Bbbk\llbracket t \rrbracket[Y,Z]/(YZ-t)$ is flat over $\Bbbk\llbracket t\rrbracket$.
	\end{compactenum}
\end{exercise}

\begin{lemma}
	Suppose $A \to B$ is flat. Write $M_B := B \dotimes{A} M$ for any $A$-module $B$. Then there are natural isomorphisms $\Tor_i^B(M_B, N_B) \simeq B \dotimes{A} \Tor_i^A(M,N)$ for all $i$.
\end{lemma}
\begin{proof}
	Take a projective resolution $0 \leftarrow M \leftarrow P_\bullet$ of $A$-modules. Since $B$ is flat over $A$, its base-change $0 \leftarrow M_B \leftarrow P_{\bullet,B}$ to $B$ is still a projective resolution; we are using the fact that base-change preserves projectivity. Hence $\Hm_i(P_{\bullet,B} \dotimes{B} N_B)$ computes $\Tor_i^B(M_B, N_B)$. On the other hand, by flatness the homology groups are equal to $\Hm_i(P_\bullet \dotimes{A} N) \dotimes{A} B$, that is, $\Tor_i^A(M,N) \dotimes{A} B$. We leave it to the reader to convince him- or herself that the isomorphism so constructed is natural.

	Another way is to use to associativity and commutativity of tensor products on the derived level, namely the flatness of $A \to B$ implies
	\[ M_B \otimesL_B N_B \simeq (M \otimesL_A B) \otimesL_B (N \otimes_A B) \simeq (M \otimesL_A N) \otimesL_A B \]
	in the derived categories, and it remains to take $\Hm_i$.
\end{proof}
In particular, let $S \subset R$ be a multiplicative subset. By Example \ref{eg:localization-flatness} we infer
\[ \Tor_i^{R[S^{-1}]}\left( M[S^{-1}], N[S^{-1}] \right) \simeq \Tor_i^R(M,N)[S^{-1}]. \]

\begin{theorem}\label{prop:free-proj}
	Let $R$ be a local ring with maximal ideal $\mathfrak{m}$. Let $M$ be a finitely generated $R$-module. The following are equivalent:
	\begin{compactitem}
		\item $M$ is free,
		\item $M$ is projective.
	\end{compactitem}
	If we assume moreover that $M$ is finitely presented, both conditions are equivalent to the flatness of $M$.
\end{theorem}
\begin{proof}
	Free modules are known to be projective. Now let $M$ be finitely generated projective, and take a basis $\bar{x}_1, \ldots \bar{x}_n$ of the $R/\mathfrak{m}$-vector space $M/\mathfrak{m}M$, together with liftings $M \ni x_i \mapsto \bar{x}_i$. Nakayama's Lemma then implies the surjectivity of
	\begin{align*}
		\Phi: R^{\oplus n} & \longrightarrow M \\
		(a_1, \ldots, a_n) & \longmapsto a_1 x_1 + \cdots + a_n x_n.
	\end{align*}
	As $M$ is projective, $\Phi$ admits a section so that we may identify $M$ with a direct summand of $R^{\oplus n}$, namely $M \oplus N = R^{\oplus n}$ for some $N$. Taking $- \otimes_R R/\mathfrak{m}$ leads to
	\[ (R/\mathfrak{m})^{\oplus n} = M/\mathfrak{m}M \oplus N/\mathfrak{m}N \quad \text{as vector spaces over } R/\mathfrak{m}, \]
	and by comparing dimensions we see $N/\mathfrak{m}N = \{0\}$, which in turn gives $N = \{0\}$ by Nakayama's Lemma ($N$ is finitely generated since $R^{\oplus n} \twoheadrightarrow N$). Hence $M \simeq R^{\oplus n}$ is free.

	Now turn to the second assertion. Projective modules are flat since they are direct summands of free modules, and it remains to show that every flat $M$ with finite presentation $R^{\oplus q} \to R^{\oplus r} \xrightarrow{x} M \to 0$ is a direct summand of a free module. Let's plug $x: R^{\oplus r} \twoheadrightarrow M$ and $K := \Ker(x)$ into the equational criterion of flatness (Lemma \ref{prop:equational-flatness}), rephrased as in Remark \ref{rem:equational-flatness-ext}. Let $N$ be the image of $B: R^{\oplus r} \to R^{\oplus s}$. One readily sees that $y$ induces $N \rightiso M$. This furnishes a section $s: M \to R^{\oplus s}$ for $y$.
\end{proof}

We deduce the following result characterizing finitely presented projective modules: in geometric language, they correspond to \emph{vector bundles} over the \emph{affine scheme} $\Spec(R)$.
\begin{corollary}
	Let $M$ be a finitely presented $R$-module. Then $M$ is projective if and only $M_{\mathfrak{m}}$ is free for every maximal ideal $\mathfrak{m}$.
\end{corollary}
\begin{proof}
	In view of Theorem \ref{prop:free-proj}, it suffices to show $M$~is projective if and only if $M_{\mathfrak{m}}$ is for all maximal ideal $\mathfrak{m}$. One direction is easy: if $M$ is a direct summand of a free module, then so is $M_{\mathfrak{m}}$.

	Conversely, the assumption on finite presentation entails an isomorphism between additive functors $R\dcate{Mod} \to R_{\mathfrak{m}}\dcate{Mod}$
	\[ \Hom_R(M, -) \dotimes{R} R_{\mathfrak{m}} \simeq \Hom_{R_{\mathfrak{m}}}(M_{\mathfrak{m}}, (-)_{\mathfrak{m}}). \]
	Hence $M_{\mathfrak{m}}$ is projective for all $\mathfrak{m}$ implies $M$ is projective, by Remark \ref{rem:exactness-local} and the exactness of localizations.
\end{proof}

Note that for finitely presented $M$, the equivalence between projectivity and flatness holds for any ring $R$. The arguments are verbatim, and this can also be deduced from the local case.

We close this section by a stronger result, whose proof is referred to \cite[Theorem A6.6]{Eis95}.
\begin{theorem}[Govorov--Lazard]
	An $R$-module is flat if and only if it is a filtered $\varinjlim$ of free $R$-modules.
\end{theorem}

\section{Faithful flatness and surjectivity}
We begin by a contemplation on the definition of faithful flatness. Recall that $R\dcate{Mod}$ is the template of \emph{abelian categories}, in which one can talk about the zero object $0$, direct sums, kernels, cokernels, images and exactness.

We say a functor between categories is \emph{faithful} if it induces injections on $\Hom$-sets. A functor between additive categories is called \emph{additive} if it induces group homomorphisms between $\Hom$-sets. An additive functor between abelian categories is \emph{exact} if it preserves all exact sequences. Exact functors preserve kernels, cokernels and images.

\begin{lemma}\label{prop:faithful-functor} \index{faithfully flat}\index{faithful functor}
	Let $F: \mathcal{C} \to \mathcal{C}'$ be an additive functor between abelian categories. The following are equivalent.
	\begin{enumerate}[(i)]
		\item $F$ is exact and faithful;
		\item $F$ is exact and $(FM=0 \iff M=0)$ for every object $M$ of $\mathcal{C}$;
		\item a sequence $M' \to M \to M''$ is exact in $\mathcal{C}$ if and only if $FM' \to FM \to FM''$ is exact in $\mathcal{C}'$.
	\end{enumerate}
\end{lemma}
\begin{proof}
	(i) $\implies$ (ii): If $FM=0$ then $F(\identity_M)=\identity_{FM}=0$, and the faithfulness implies $\identity_M=0$ in $\End_{\mathcal{C}}(M)$; this is possible only when $M=0$.

	(ii) $\implies$ (i): Suppose $u: N \to M$ is mapped to $0$ under $F$. Then we have $F(\Image(u))=0$, thereby $\Image(u)=0$ and $u=0$.

	(i) $\implies$ (iii): Suppose $M' \xrightarrow{u} M \xrightarrow{v} M''$ induces an exact sequence $FM' \xrightarrow{Fu} FM \xrightarrow{Fv} FM''$. From $F(vu) = F(v)F(u) = 0$ we get $vu=0$. Thus it makes sense to define $C := \Ker(v)/\Image(u)$. One has an exact sequence
	\[ \Image(u) \to \Ker(v) \to C \to 0. \]
	Since $F$ is exact, we deduce $FC=0$, which implies $C=0$ by (i) $\implies$ (ii).

	(iii) $\implies$ (i): Suppose $u: N \to M$ is mapped to $0$ under $F$. Consider $v: M \to \Coker(u)$. Since $F$ preserves exact sequences and $Fv$ is an isomorphism, we see $v$ is also an isomorphism, therefore $u=0$.
\end{proof}

\begin{proposition}\label{prop:faithful-flatness-crit}
	The following are equivalent for an $R$-module $N$.
	\begin{compactenum}[(i)]
		\item $N$ is faithfully flat.
		\item The functor $- \dotimes{R} N$ is exact and faithful.
		\item $N$ is flat and for every maximal ideal $\mathfrak{m}$ of $R$, we have $N/\mathfrak{m}N \simeq N \dotimes{R} R/\mathfrak{m} \neq 0$.
	\end{compactenum}
\end{proposition}
\begin{proof}
	The equivalence (i) $\iff$ (ii) has just been established. An $R$-module $M$ is nonzero if and only if there exist exact sequences
	\[ 0 \to R/\mathfrak{a} \to M, \quad R/\mathfrak{a} \to R/\mathfrak{m} \to 0 \]
	where $\mathfrak{a}$ is a proper ideal and $\mathfrak{m}$ is a maximal over-ideal of $\mathfrak{a}$. Next, let's show (iii) $\implies$ (i) or (ii): there are exact sequences
	\[ 0 \to T \to M \dotimes{R} N, \quad T \to M \dotimes{R} (R/\mathfrak{m}) \to 0 \]
	where $T := (R/\mathfrak{a}) \dotimes{R} N$, therefore $M \dotimes{R} N \neq 0$ and we apply Lemma \ref{prop:faithful-functor}. Finally, (i) or (ii) $\implies$ (iii) is clear.
\end{proof}

\begin{corollary}
	Let $R \to R'$ be a local homomorphism\footnote{That is, the preimage of the maximal ideal $\mathfrak{m}_{R'}$ equals $\mathfrak{m}_R$.} between local rings and let $M$ be a finitely generated $R'$-module, $M \neq 0$. Then as an $R$-module, $M$ is faithfully flat if and only if it is flat.
\end{corollary}
\begin{proof}
	Let $\mathfrak{m}, \mathfrak{m}'$ be the maximal ideals of $R, R'$, respectively. Since $\mathfrak{m}$ is mapped into $\mathfrak{m}' = \text{rad}(R')$, the assertion follows from Proposition \ref{prop:faithful-flatness-crit} together with Nakayama's Lemma.
\end{proof}

This is often applied to the cases $R=R'$ or $M=R'$.

\begin{exercise}[Faithfully flat descent for flatness]
	Given a faithfully flat homomorphism $\varphi: R \to R'$. If $M$ is an $R$-module such that $M \dotimes{R} R'$ is a flat (resp. faithfully flat) $R'$-module, then so is $M$ over $R$.
\end{exercise}

\begin{proposition}\label{prop:faithfully-flat-surj}
	Suppose $\varphi: A \to B$ is a faithfully flat ring homomorphism and regard $B$ as an $A$-algebra.
	\begin{itemize}
		\item For any $A$-module $M$, the natural map $M \to M \dotimes{A} B$ is injective; in particular, $\varphi$ is seen to be injective by taking $M=A$.
		\item For any ideal $\mathfrak{a} \subsetneq A$ we have $\varphi^{-1}(\mathfrak{a}B)=\mathfrak{a}$.
		\item The map $\varphi^\sharp: \Spec(B) \to \Spec(A)$ is surjective.
	\end{itemize}
\end{proposition}
\begin{proof}
	Let $N$ be the kernel of $M \to M \dotimes{A} B$. The $B$-module homomorphism $N \dotimes{A} B \to M \dotimes{A} B$ is injective by flatness; it is zero on $N \otimes 1$, hence identically zero and we obtain $N = \{0\}$ by faithful flatness (Lemma \ref{prop:faithful-functor}).

%	Take $x \in M$. Flatness implies $Ax \otimes B \hookrightarrow M \dotimes{A} B$. Note that $Ax \otimes B$ is generated by $x \otimes 1$. If $x \neq 0$ then $Ax \neq \{0\}$ and $Ax \otimes B \neq \{0\}$ since $\varphi$ is faithfully flat. This implies $x \mapsto x \otimes 1 \neq 0$.

	Let $\mathfrak{a} \subset A$ be a proper ideal. The previous step with $M := A/\mathfrak{a}$ implies that $A/\mathfrak{a} \to (A/\mathfrak{a}) \dotimes{A} B \simeq B/\mathfrak{a}B$ is injective, hence $\varphi^{-1}(\mathfrak{a}B) = \mathfrak{a}$.

	To show the surjectivity of $\varphi^\sharp$, consider a given $\mathfrak{p} \in \Spec(A)$ and form the faithfully flat ring homomorphism
	\[ \varphi_{\mathfrak{p}}: A_{\mathfrak{p}} \to B \dotimes{A} A_{\mathfrak{p}} = B_{\mathfrak{p}} \]
	by base change. The previous step implies $\varphi_{\mathfrak{p}}^{-1}(\mathfrak{p}B_{\mathfrak{p}}) = \mathfrak{p} A_{\mathfrak{p}}$, hence $\mathfrak{p}B_{\mathfrak{p}}$ is a proper ideal of $B_{\mathfrak{p}}$. Any maximal over-ideal $\mathfrak{m}_0$ of $\mathfrak{p}B_{\mathfrak{p}}$ satisfies $\varphi^{-1}_{\mathfrak{p}}(\mathfrak{m}_0) = \mathfrak{p} A_{\mathfrak{p}}$. Take $\mathfrak{m} \in \Spec(B)$ mapping to $\mathfrak{m}_0$. From the commutative diagrams
	\[\begin{tikzcd}
		B \arrow[r] & B_{\mathfrak{p}} \\
		A \arrow[u, "\varphi"] \arrow[r] & A_{\mathfrak{p}} \arrow[u, "\varphi_{\mathfrak{p}}"']
	\end{tikzcd} \qquad \begin{tikzcd}
		\Spec(B) \arrow[d, "\varphi^\sharp"'] & \Spec(B_{\mathfrak{p}}) \arrow[d, "\varphi_{\mathfrak{p}}^\sharp"] \arrow[l] \\
		\Spec(A) & \Spec(A_{\mathfrak{p}}) \arrow[l]
	\end{tikzcd}\]
	one infers $\varphi^\sharp(\mathfrak{m}) = \mathfrak{p}$.
\end{proof}

\begin{theorem}\label{prop:faithfully-flat-criterion}
	The following are equivalent for a ring homomorphism $\varphi: A \to B$.
	\begin{enumerate}[(i)]
		\item $\varphi$ is faithfully flat;
		\item $\varphi$ is flat and $\varphi^\sharp: \Spec(B) \to \Spec(A)$ is surjective;
		\item $\varphi$ is flat and for any maximal ideal $\mathfrak{p} \subset A$ there exists a maximal ideal $\mathfrak{m} \subset B$ such that $\varphi^{-1}(\mathfrak{m}) = \mathfrak{p}$.
	\end{enumerate}
\end{theorem}
\begin{proof}
	(i) $\implies$ (ii) is contained in Proposition \ref{prop:faithfully-flat-surj}. As for (ii) $\implies$ (iii), take any $\mathfrak{q} \in \Spec(B)$ that pulls back to $\mathfrak{p} \in \Spec(A)$, then any maximal over-ideal $\mathfrak{m}$ of $\mathfrak{q}$ also pulls back to $\mathfrak{p}$. To show (iii) $\implies$ (i), apply the criterion of Proposition \ref{prop:faithful-flatness-crit}: for any $\mathfrak{p} \in \MaxSpec(A)$, the existence of $\mathfrak{m} \mapsto \mathfrak{p}$ implies $\mathfrak{m} \supset \varphi(\mathfrak{p}) \cdot B = \mathfrak{p}B$, hence $\mathfrak{p}B \neq B$.
\end{proof}

The notion of flatness was first introduced by J.-P. Serre in \cite{Se55}. The surjections $\varphi^\sharp: \Spec(B) \to \Spec(A)$ (or rather their global avatars) for faithfully flat $\varphi: A \to B$ are often employed as candidates of ``coverings'' in algebraic geometry, leading up to the well-known \emph{fpqc} (faithfully flat + quasi-compact) and \emph{fppf} (faithfully flat + finitely presented) topologies in the sense of Grothendieck. They have been indispensable tools for contemporary geometers; \cite{Vi05} serves as a readable introduction to this circle of ideas.


\newcommand{\vect}[1]{\mathbf{#1}}
\newcommand{\mat}[1]{\mathbf{#1}}

\newcommand{\pos}{\vect{x}}
\newcommand{\dx}{\vect{\Delta x}}
\newcommand{\xcur}{\vect{x}_{n}}
\newcommand{\xnext}{\vect{x}_{n+1}}
\newcommand{\vel}{\vect{v}}
\newcommand{\dv}{\vect{\Delta v}}
\newcommand{\vcur}{\vect{v}_{n}}
\newcommand{\vnext}{\vect v_{n+1}}
\newcommand{\acc}{\vect{a}}
\newcommand{\force}{\vect{f}}
\newcommand{\forcext}{\vect{f}_{ext}}
\newcommand{\lam}{\vect{\lambda}}
\newcommand{\lcur}{\lam_{n}}
\newcommand{\lnext}{\lam_{n+1}}
\newcommand{\avlam}{\bar{\lam}}
\newcommand{\fcur}{\vect{f}_{n}}
\newcommand{\fnext}{\vect f_{n+1}}
\newcommand{\M}{\mat M}
\newcommand{\Minv}{\mat M^{-1}}
\newcommand{\Mbinv}{\bar{\mat M}^{-1}}
\renewcommand{\P}{\mat P}
\newcommand{\I}{\mat I}
\newcommand{\J}{\mat J}
\newcommand{\Jt}{\mat J^T}
\newcommand{\C}{\mat C}
\newcommand{\D}{\mat D}
\newcommand{\K}{\mat K}
\newcommand{\violation}{ \phi}
\newcommand{\dviolation}{\dot \violation}
\newcommand{\violcur}{\violation_{n}}
\newcommand{\dviolcur}{\dot \violcur}
\newcommand{\violnext}{\violation_{n+1}}
\newcommand{\dviolnext}{\dot \violation_{n+1}}
\newcommand{\cmp}{c}
\newcommand{\dampingratio}{d}

\section{Introduction}
This plugin serves two purposes:
\begin{itemize}
 \item to provide a new, more complete mechanisme for assembling the dynamical system matrices such as mass $\M$ and stiffness $\K$, as well as constraint Jacobians $\J$,
 \item and experiment a unified approach of soft and hard constraints, inspired from~\cite{servin2006interactive}.
\end{itemize}
Contrary with other implementations of matrix assembly available in SOFA, this one handles mappings, and even multimappings.

Sections \ref{sec constraint forces} and \ref{sec:time integration} explain the unified constraint approach, while Section~\ref{sec matrix assembly} presents the matrix assembly process based on an example. An overview of the corresponding API is given in Section~\ref{sec implementation}.

\section{Constraint forces} \label{sec constraint forces}
This approach unifies soft and hard constraints, by providing constraints with compliance.
\subsection{Hard constraints}
For a good introduction on hard constraints, see~\cite{witkinconstraints}.
Hard constraints are usually implemented using Lagrange multipliers $\lambda$ in the following equation:
\begin{equation} \label{eq acc hard}
\left( \begin{array}{cc}
\M & -\Jt \\
 \J &  \end{array}\right)
\left( \begin{array}{c}
\acc \\ \lam
\end{array}\right) = \left( \begin{array}{c}
\forcext  \\
-\ddot \violation
\end{array}\right)
\end{equation}
where $\M$ is the mass matrix (or, more generally, a dynamics matrix such as $\M-h²\K$ used in implicit time integration), $\J$ is the Jacobian matrix of the constraint(s), $\acc$ is the acceleration, $\forcext$ is the net external force applied to the system, $\lam$ is the constraint force $\violation$ is the constraint violation and $\ddot \violation$ is the second time derivative of the violation (i.e. the error on accelerations).
This form of equation system is typically called \textit{Karush-Kuhn-Tucker (KKT)}.
$\lam$ and $\ddot \violation$ are vectors with as many entries as scalar constraints. The equation system is typically solved using a Schur complement to compute the constraint forces:
\begin{equation}\label{eq schur rigid}
\J \Minv \Jt \lam = -\ddot \violation - \J \Minv \forcext
\end{equation}
and then the acceleration is computed as $\acc = \Minv( \forcext + \Jt \lam)$.

\subsection{Projective constraints}
When the constraints are simple enough, such as keeping points fixed or in some hyperplane, they are more easily handled by straightforwardly projecting forces and displacements.
The previous equation becomes:
\begin{equation} \label{eq acc hard projected}
\left( \begin{array}{cc}
\P\M & -\P\Jt \\
 \J\P &  \end{array}\right)
\left( \begin{array}{c}
\acc \\ \lam
\end{array}\right) = \left( \begin{array}{c}
\P\forcext  \\
-\ddot \violation
\end{array}\right)
\end{equation}
 where $\P$ is the orthogonal projection matrix corresponding to the constraint.

\subsection{Generalized constraints}
In the generalized approach, the constraint forces are considered directly proportional to the constraint violations $\violation$ and their first time derivative (error on velocity) $\dviolation$:
\begin{equation}\label{eq generalized constraint}
\lam = - \frac{1}{\cmp} \left( \violation + \dampingratio \dviolation \right)
\end{equation}
where the positive real number $\cmp$ is the compliance of the constraint and $\dampingratio$ is its damping ratio.
Combined with a time discretization scheme, this leads to an equation system similar to \eqref{eq schur rigid} as shown in Section~\ref{sec:time integration}.
% This leads to equation system:
% \begin{equation} \label{eq acc compliant}
% \left( \begin{array}{cc}
% \M & -\Jt \\
%  \J &  \C \end{array}\right)
% \left( \begin{array}{c}
% \acc \\ \lam
% \end{array}\right) = \left( \begin{array}{c}
% \forcext  \\
% -\violation - \dampingratio \dviolation
% \end{array}\right)
% \end{equation}
% where $\C$ is the compliance matrix, typically diagonal, which is null for hard constraints. The corresponding Schur complement is well-conditioned for hard constraints:
% \begin{equation}\label{eq schur compliant}
% \left( \J \Minv \Jt  +   \C \right) \lam = -\violation - \dampingratio\dviolation - \J \Minv \forcext
% \end{equation}
% contrary to the standard implicit integration approach, where the indefinite stiffness matrix grows to infinity for hard constraints.

\subsection{Force or constraint ?} \label{sec force or constraint}
ForceFields usually contribute the top line of Equation~\ref{eq acc hard}, by accumulating force in the right-hand term. In implicit integration, their stiffness matrix is also accumulated to the left-hand side.
When a ForceField has an invertible stiffness matrix, it can alternatively be handled as a constraint with non-null compliance, rather than a standard force. In this case, it contributes to the bottom of Equation~\ref{eq acc hard} rather than to the top.

Due to matrix conditioning issues, and depending on which linear solver is used, it may be a good idea to process very stiff forces as low compliances rather than large stiffnesses.
In this case, we call them \textit{compliant} or \textit{compliance forces}. Here the term compliant does not convey information on the magnitude of the stiffness (or compliance) but only on the way it is processed by the solver, namely in the bottom row of the KKT equation system.
Note, however, that each force handled as a constraint increases the size of the equation system, since it adds lines to the bottom (and columns to the right) of the equation system.
Conversely, it may be more efficient to handle soft forces as stiffnesses, since the size of the stiffness matrix depends on the number of independent DOFs, which does not increase along with the number of forces. We call such forces \textit{stiff} or \textit{stiffness forces}, though here again, the term does not convey information on the magnitude of the stiffness but only on the way it is handled by the solver, namely in the first row of the KKT equation system.

Note that geometric stiffness is not (yet) handled in the compliance view, which may result in instabilities in case of large displacements.

\section{Time integration} \label{sec:time integration}
Our integration scheme is:
\begin{eqnarray}
 \vnext &=& \vcur + h \Minv \left(\alpha \fnext + (1-\alpha) \fcur\right) \label{eq ode velocity} \\
  \xnext &=& \xcur + h \left( \beta \vnext + (1-\beta) \vcur \right) \label{eq ode position}
\end{eqnarray}
where index $n$ denotes current values while index $n+1$ denotes next values, $\alpha$ is the implicit velocity factor, and $\beta$ is the implicit position factor. Let
\begin{eqnarray}
 \dv = \vnext -    \vcur  &=& h \Minv \left(\alpha \fnext + (1-\alpha) \fcur\right) \\
\dx = \xnext -  \xcur    &=& h (v + \beta  \dv)
\end{eqnarray}
be the velocity and position changes across the time step.
We have not carefully studied the influence of these parameters, but it seems that $\alpha=0.5$ and $\beta=1$ corresponds to an \textbf{energy-conserving integration scheme}.

The constraint violation $\violation$ and its Jacobian $\J$ are:
\begin{eqnarray}
 \J &=& \frac{\partial \vect \violation}{\partial \pos} \\
 \violnext &\simeq& \violcur + \J \dx = \violcur + h   \dviolation + \J h \beta \Delta \vel  \label{eq violnext}\\
\dviolnext &\simeq& \dviolcur + \J \dv \label{eq dviolnext}
\end{eqnarray}
The corresponding forces are:
\begin{eqnarray}
 \force &=& \forcext + \Jt \lam \\
 \lam_i &=& -\frac{1}{c_i} (  \violation_i + d \dviolation_i ) \label{eq lambda}
\end{eqnarray}
where the subscript $i$ denotes a scalar constraint.

The average constraint forces are computed  using equations \ref{eq lambda}, \ref{eq violnext} and \ref{eq dviolnext}:
\begin{eqnarray*}
 \avlam_i &=& \alpha \lnext + (1-\alpha) \lcur \\
&=& -\frac{1}{c_i} ( \alpha \violation + \alpha h \dviolation  + \alpha h \beta \J \Delta \vel + \alpha d \dviolation + \alpha d \J \dv + (1-\alpha)\violation + (1-\alpha) d \dviolation  ) \\
&=& -\frac{1}{c_i} ( \violation + d\dviolation + \alpha h \dviolation + \alpha(h\beta+d)\J \dv )
\end{eqnarray*}
We can rewrite the previous equation as:
\begin{equation} \label{eq bottom}
 \J \dv + \frac{1}{\alpha(h\beta+d)}\C\avlam = - \frac{1}{\alpha(h\beta+d)} (\violation + (d+\alpha h)\dviolation)
\end{equation}
where values without indices denote current values.
The complete equation system has the following generalized KKT form:
\begin{equation} \label{eq:kktode}
 \left( \begin{array}{cc}
\frac{1}{h}\P\M & -\P\Jt \\
\J & \frac{1}{l} \C \end{array}\right)
\left( \begin{array}{c}
\dv \\ \avlam
\end{array}\right) = \left( \begin{array}{c}
\P\forcext  \\
- \frac{1}{l} (\violation +(d+\alpha h) \dviolation)
\end{array}\right)
\end{equation}
where $ l=\alpha(h \beta + d) $.
The matrix in equation~\ref{eq:kktode} is semi-definite when matrix $\C$ is null, or when matrix $\P $ removes independent degrees of freedom without reducing the size of the mass matrix accordingly.
This equation system can be solved using iterative solvers, such as CG or other Krylov methods.
An other option is to create a reduced equation system on constraint forces only:
\begin{equation} \label{eq schur complement}
 \left( h\J\Mbinv\Jt + \frac{1}{l}\C \right) \avlam = -\frac{1}{l} \left(\violation + (\dampingratio+h\alpha)\dviolation \right) - h\J \P^T \Mbinv \forcext
\end{equation}
where matrix $ h\J\Mbinv\Jt + \frac{1}{l}\C $ is called the Schur complement of the KKT equation system.
Matrix $\bar\M= \P \M \P^T + diag(\epsilon)$ is optionally regularized by the linear solver using a very small number $\epsilon$.
This makes the matrix PSD, and does not significantly change the value of the equation solution thanks to the small magnitude of the perturbation.

% Note that the system is singular due to matrix $\P $, which removes independent degrees of freedom without reducing the size of the mass matrix accordingly.
% To remedy this, we set an option to replace it with
% \begin{equation} \label{eq mbar}
% \bar\M = \P \M \P^T + diag(\epsilon),
% \end{equation}
%  where $\epsilon$ is a very small number.
% This makes the matrix PSD, and does not significantly change the value of the equation solution thanks to the small magnitude of the perturbation.
%
% % \[ \begin{array}{ccc}
% % \left( h\J\P\Minv\P\Jt + \frac{1}{l}\C \right) \avlam &=& -\frac{1}{l} \left(\violation + (\dampingratio+h\alpha)\dviolation \right) - h\J \Minv \forcext \\
% % \dv &=& h\P\Minv(\forcext +\Jt \avlam ) \\
% % \dx &=& h( \vel + \beta \dv )
% % \end{array} \]
%
% By inverting $\bar\M$, we can insert the first line of the equation system and get:
% \begin{equation} \label{eq schur complement}
%  \left( h\J\Mbinv\Jt + \frac{1}{l}\C \right) \avlam = -\frac{1}{l} \left(\violation + (\dampingratio+h\alpha)\dviolation \right) - h\J \P^T \Mbinv \forcext
% \end{equation}
% where matrix $ h\J\Mbinv\Jt + \frac{1}{l}\C $ is called the Schur complement of the KKT equation system.
Solving for $\avlam$ allows us to update the velocities and the positions:
\begin{eqnarray}
 \dv &=& h\P\Mbinv(\forcext +\Jt \avlam ) \label{eq update velocities}\\
\dx &=& h( \vel + \beta \dv ) \label{eq update positions}
\end{eqnarray}

\section{Matrix assembly} \label{sec matrix assembly}
The equation system, in its most general form, can be written as:
\begin{equation}
 \label{eq abstract system}
 \left( \begin{array}{cc}
\M & -\Jt \\
\J &  \C \end{array}\right)
\left( \begin{array}{c}
\pos \\ \lam
\end{array}\right) = \left( \begin{array}{c}
\force  \\
\violation
\end{array}\right)
\end{equation}
We assemble the $7$ terms of equation~\ref{eq abstract system} separately.

Figure~\ref{fig system graph} shows an example of mechanical system.
The independent DOFs are $X_{a}$ and $X_d$.
State $X_b$ is attached to $X_a$ using a simple mapping, and a mass matrix $M_{bb}$ is defined at this level.
State $X_c$ is attached to $X_b$ using a simple mapping, and a compliance matrix $C_{\alpha \alpha}$ (possibly a deformation force) is applied to these DOFs.
State $X_e$ is attached to $X_a$ and  $X_d$ at the same time, using a MultiMapping. A compliance matrix $C_{\beta \beta}$, possibly an interaction force, is applied to these DOFs,
while a mass $M_{dd}$ is applied to $X_d$.
\begin{figure}
\centering
%  \includegraphics[width=0.49\linewidth]{system-graph.png}
\includegraphics[clip,trim= 0mm 255mm 145mm 0mm,width=0.49\linewidth]{system-graph.pdf}
\caption{Example of mechanical system structure. The $X$ represent mechanical states, while the arrows represent the kinematic hierarchy, and the plain lines represent components acting on the states.}
\label{fig system graph}
\end{figure}

The corresponding equation system has the block structure shown in the right of Figure~\ref{fig system matrix}.
The main blocks of the equation system are highlighted in grey rectangles. The $J$ matrices are the mapping matrices. The bottom row has two mappings, since the state $X_e$ impacted by compliance $\beta$ depends on two parent states.
\begin{figure}
\centering
 \includegraphics[clip,trim=5mm 185mm 40mm 0mm,width=0.7\linewidth]{system-bigmatrix.pdf}
\caption{Block view of the equation~\ref{eq abstract system} applied to the system of Figure~\ref{fig system graph}, with non-null blocks highlighted in yellow.}
\label{fig system matrix}
\end{figure}

The assembly of local matrices and vectors to the global matrices and vectors which compose the system is performed using shift matrices, as illustrated in Figure~\ref{fig shifting}.
Shift matrices $J_{*0}$, shown in the top of the figure, are composed of identity matrices (represented with a diagonal in a block) and null blocks. They can be used to implement the shifting of vector entry indices from local to global.
\begin{figure}
\centering
%  \includegraphics[width=0.69\linewidth]{system-shift.png}
 \includegraphics[clip,trim=0mm 40mm 0mm 115mm,width=0.89\linewidth]{system-bigmatrix.pdf}
\label{fig shifting}
\caption{Shift matrices $J_{*0}$ are used to place blocks within global vectors and matrices. }
\end{figure}
The assembly is thus easily expressed as a sum of product of matrices, as illustrated in Figure~\ref{fig assembly}.
While shifting values in dense arrays using matrix products is not efficient (vector assembly is actually implemented using shifted copies), the sum of matrix products is a reasonable implementation of sparse matrix assembly.
\begin{figure}
\centering
%  \includegraphics[width=0.6\linewidth]{system-assembly.png}
 \includegraphics[clip,trim=5mm 185mm 70mm 5mm,width=0.6\linewidth]{system-assembly.pdf}
\caption{The assembly is performed by summing the global vectors and matrices obtained by shifting local vectors and matrices.}
\label{fig assembly}
\end{figure}

\section{Implementation} \label{sec implementation}

\subsection{Dependencies}
This plugin uses \texttt{Eigen} and \texttt{suitesparse}. Moreover, the test suite requires the \texttt{boost unit test framework}, see https://wiki.sofa-framework.org/wiki/UnitTesting  .

\subsection{Functions}

The virtual functions used for setting up the equation system are declared in BaseForceField.h, BaseMapping.h, BaseMass.h, BaseProjectiveConstraint.h, MechanicalParams.h.
They and documented in ``Experimental Compliance API'' documentation blocks of these files.

\subsection{BaseForceField functions}
ForceFields may be handled as forces or as soft constraints, as explained in Section~\ref{sec force or constraint}.
The default behavior is to handle them as forces, and in this case their force is accumulated using a classical \texttt{MechanicalOperations::computeForce} operation, while their stiffness matrix is accumulated using the \texttt{getStiffnessMatrix} briefly described below.
\begin{itemize}
 \item \texttt{getComplianceMatrix} returns NULL if the ForceField is to be handled as a traditional force function, while it returns a pointer to a compliance matrix if it is to be handled as a constraint.
 \item \texttt{addForc}e is used to accumulate the force in the right-hand term. This function is not specific to the \textit{Compliant} plugin. This should do nothing when the force must be handled as a constraint.
 \item \texttt{getStiffnessMatrix} is the complement of \texttt{getCompliantMatrix}. If one retuns NULL, then the other must return a pointer to a matrix.
 \item \texttt{writeConstraintValue} is used to write the constraint violation $\violation$ in Eq.\ref{eq generalized constraint} when the ForceField is handled as a constraint.
 \item \texttt{getDampingRatio} is used in the constraint case, and corresponds to parameter $ \dampingratio$ in Eq.\ref{eq generalized constraint}
\end{itemize}

\subsubsection{BaseMapping functions}
\begin{itemize}
 \item \texttt{getJs} returns a list of Jacobian matrices, one for each parent of the mapping. Typical mappings have only one parent, but MultiMappings have several ones.
 \item \texttt{getKs} returns a list of stiffness matrices, one for each parent. These correspond to geometric stiffness, i.e. change of forces due to mapping nonlinearity, like \texttt{addDJT} in the traditional API. \textbf{\begin{large}TODO:                                                                                \end{large}} refine this, to also return off-diagonal terms in MultiMappings.
\end{itemize}

\subsubsection{BaseMass functions}
\begin{itemize}
 \item \texttt{addMToMatrix} to create the mass matrix. This function is not specific to the \textit{Compliant} plugin.
\end{itemize}

\subsection{BaseProjectiveConstraint functions}
\begin{itemize}
 \item \texttt{projectMatrix} replaces a matrix with one corresponding to the same linear operator, projected to cancel, out the forbidden directions.
\end{itemize}

\subsection{MechanicalParams functions}
\begin{itemize}
 \item \texttt{implicitVelocity} is the $\alpha$ parameter in the implicit velocity integration of Eq.\ref{eq ode velocity}.
 \item \texttt{implicitPosition} is the $\beta$ parameter in the implicit position integration of Eq.\ref{eq ode position}.
\end{itemize}

\section{Alternative model} \label{sec alternative model}
To allow more flexibility in the definition of damping in the compliance approach, the following variant is possible.
We replace eq.~\ref{eq generalized constraint} with the following.
\begin{equation}\label{eq generalized damped constraint}
\lam = - \C^{-1} \violation - \D \dviolation
\end{equation}
where $\violation$ and $\dviolation$ are vectors, and $\C$ and $\D$ symmetric matrices.
This allows us to define non-uniform damping coefficients.
We rewrite eq.~\ref{eq bottom} as:
\begin{equation}
 \label{eq bottom alt}
 \alpha( h \beta \I + \C \D) \J \dv + \C \avlam = -\violation - \alpha(h\I + \C\D )\dviolation
\end{equation}
where $\I$ is the identity matrix of the appropriate size.
Unfortunately, the diagonal matrix in the left of $\J$ in the term on $\dv$ now makes the Schur complement unsymmetric. Therefore, we have not implemented this approach.

\section{Additional Notes}
\label{sec:additional notes}

\newcommand{\component}[1]{\texttt{#1}}
\newcommand{\compopt}[2]{\texttt{#1}=''#2''\\}

The initial implementation of the compliance framework is provided by
\component{ComplianceSolver}, which performs system assembly, system
solves and time-stepping. It uses a sparse Cholesky solver and is able
to handle bilateral constraints only.

With time, this component has been broken down to the following
components:

\begin{itemize}
\item \component{CompliantImplicitSolver}, which performs system assembly
  (through an internal \component{AssemblyVisitor}) and
  time-stepping.
\item \component{KKTSolver}, which performs the KKT solve associated
  with an \component{AssembledSystem}, itself provided by the
  \component{CompliantImplicitSolver} though its internal
  \component{AssemblyVisitor}.
\end{itemize}

\component{KKTSolver} is a virtual base class implemented in
\component{MinresSolver} and \component{LDLTSolver} for bilateral
constraints. The CompliantDev plugin contains additional KKT solvers
for unilateral and frictional constraints.

We now give a quick user documentation for the above components.

\subsection{CompliantImplicitSolver}

This component implements a linearized implicit Euler solver. The KKT
system is first assembled by an \component{AssemblyVisitor}, and
factorized by the \component{KKTSolver}.

The right-hand-side is then computed depending on constraint
stabilization and order of resolution, and the \component{KKTSolver}
provides system solves.

At the end of the time-step, velocities are updated and the system is
stepped forward. Optionally, one can propagate the computed Lagrange
multipliers upwards in the scene graph at the end of the time step in
the \emph{force} state vector for easy access.

\subsubsection{Order of Resolution}

\compopt{use\_velocity}{\textbf{true}, false}

Formulates the system at the velocity level. Acceleration is used
otherwise. This is mostly for debugging purpose, unless warm-starts
are disabled (see below).

\subsubsection{Warm Starts}

\compopt{warm\_start}{\textbf{true}, false}

Warm starts iterative solvers with previous velocity/lambda (velocity-level). At the
acceleration level, only the lambda are used.

When disabled and used with iterative solvers and premature exit, this
biases the solution towards zero, thus this results in numerical
damping with velocity-level.

This is mostly for debugging purpose.

\subsubsection{Constraint Stabilization}

\compopt{stablization}{\textbf{false}, true}

Performs simple constraint stabilization. This causes an additional
KKT solve during the time step.

You need to add a \component{Stabilization} component next to any
compliance you wish to stabilize. Only use with zero-compliance
(holonomic constraints).

TODO more, example

\subsection{Lagrange Multiplier Propagation}

\compopt{propagate\_lambdas}{\textbf{false}, true}

Optionally propagate/aggregate Lagrange multipliers from compliant
DOFs to independent DOFs at the end of the time step. This erases the
\emph{force} state vector.

TODO example

\subsection{LDLTSolver}

no options

TODO more

\subsection{MinresSolver}

\compopt{iterations}{\textbf{false}, true}

iteration count

\compopt{precision}{\textbf{1e-8}}

residual norm threshold

\compopt{relative}{\textbf{true}, false}

use relative precision, otherwise absolute

TODO parallel ?
TODO schur complement ?

TODO better description


\subsection{Evaluation}
\label{sec:eval}

In the evaluation phase of the CRISP-DM process, the model(s) produced in the
earlier step is carefully examined and evaluated; i.e. does it solve the
business problem? By the end of this phase a decision should be made wether the
data mining results can be used, or if more iteration is needed. Even if the
model is deployed, it is still possible to continue the process of iteration to
create a better model or discover more things about the data or business
problem.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsubsection{Dataset Bias}

While running the tests that produced the data in Table \ref{tab:formulacompare}
I noticed that while the accuracy stayed around $93 - 94$ \% for all the runs,
the model did not have the same precision when predicting \texttt{TRUE} as it
does when predicting \texttt{FALSE}. The model would be wrong when predicting a
customer to be \textit{retained} one time out of three, giving it an accuracy
for predicting \texttt{TRUE} of around $66.666$ \%. When guessing \texttt{FALSE}
on the other hand; the model would have an accuracy of around $95$ \% of the
time. The data for these calculations can be seen in Table
\ref{app:tab:confusion1}, Table \ref{app:tab:confusion2} and Table
\ref{app:tab:confusion3} in Appendix \ref{app:confusion}.

Looking at the datasets it looks like this trend is caused by the number of
observations for each class. Both the training and test dataset contains more
than ten times as many unretained customers as retained, the exact numbers can
be seen in Table \ref{tab:datasetretention}. This essentially returned me to the
data understanding phase of the CRISP-DM process, but for the sake of continuity
in this reportm the data preparation, modelling and evaluation of this extra
iteration will be written in this section.

\begin{table}[H]
  \centering
  \begin{tabular}{lll}
    \textbf{Dataset}  & \texttt{TRUE} & \texttt{FALSE} \\ \hline
    \textit{Training} & $30358$       & $433358$       \\
    \textit{Test}     & $40731$       & $454659$       \\
    \textit{Equal}    & $30358$       &  $30358$
  \end{tabular}
  \caption{The distribution of the \textit{iscjretained} target variable classes
    in the different datasets.}
  \label{tab:datasetretention}
\end{table}

In order to try and combat the bias in the dataset I constructed a new dataset I
will refer to as dataset \textit{equal}. This new dataset contains all of the
observations from the training dataset who is \textit{retained}, and a random
sample, of the same size, of the observations who is not.

Like in Section \ref{sec:formcompare} I created a new set of tests (the source
code for these tests is available in Figure \ref{app:code:equal} in Appendix
\ref{app:equal}) which would test the new accuracy of the model with varying
tree depths. Using this new test, the overall accuracy for drops to around $82$
\% ($81.6753$ \%, $81.9569$ \% and $81.9438$ \%) for maximum depths of 4, 6 and
8, for the detailed numbers, please refer to Table \ref{app:tab:confusion11},
Table \ref{app:tab:confusion22} and Table \ref{app:tab:confusion33} in Appendix
\ref{app:confusion}.

The new dataset and model does better at guessing retained customers with a mean
accuracy of around $82$ \% for both \texttt{TRUE} and \texttt{FALSE}. While this
increase in accuracy for retained customers is roughly equal to the decrease in
accuracy for unretained customers when looking at the percentages, the data that
the model should predict on contains more unretained customers than retained. So
the drop from $95$ \% to  $82$ \% in unretained accuracy represents a lot more
users that the increase in retained accuracy from  $66.666$ \% to  $82$ \% when
referencing the distribution in Table \ref{tab:datasetretention}. This means
that this model will misclassify a lot more unretained users as retained
compared to the improved ability to correctly classify retained customers as
retained.


%! Author = tstreule

\section{Computed Tomography}

Quantitative with \textbf{Houndsfield unit} (normed to water):\\
\highlight{$\displaystyle \textrm{CT value} = \frac{\mu - \mu\ped{water}}{\mu\ped{water}}\cdot \unit[1000]{HU}$}

Beam types: Pencil, Fan, Parallel fan, cone.

\textbf{Algebraic Reconstruction}:
Out of a $n \times n$-Image, make a $n^2 \times n^2$ equation system:
$\vec{P} = W \vec{\mu}$ (P are the measurements, W are the weightings, how much a beam crossed a box/pixel)\\
\highlight{$\displaystyle \vec{\mu} = (W^H W)^{-1} W^H \vec{P}$} $~^H$: hermetisch transponiert.

\textbf{Projection Slice Theorem}: Project a 2D function onto a line and do a 1D Fourier transform \quad $\iff$\\
Do a 2D Fourier transform of that 2D function and slice it through its origin (parallel to the line above).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Backprojection}
%
When doing simple Backprojection: $PSF = \frac{1}{|r|}$

\includegraphics[width = .8\linewidth]{CT_Sinogram}\\
$P_\phi(r) = -R[\mu](r, \phi) = -\iint \limits_{-\infty}^\infty \mu(x, y) \delta(\underbrace{x \cos \phi + y \sin \phi - r\vspace{-1mm}}_{\text{line formula}\vspace{-1mm}})dxdy$

Math. Deduction: \textbf{1st Reconstruction formula}:\\
Projection $P_\phi(r) = -\int \mu(r, s) ds$\\
$\mathcal{F}\{P_\phi(r)\}(u,\phi) = \int P_\phi(r) e^{-jur}dr =$\\
$= - \iint \mu(r, s) e^{-jur} dr ds =$ (with $r = x\cos \phi + y \sin \phi$)\\
$= -\iint \mu(x, y) \exp(-jx\underbrace{u \cos \phi\vspace{-1mm}}_{\vspace{-2mm}p \hat{=} k_x} - jy\underbrace{u\sin\phi\vspace{-1mm}}_{\vspace{-2mm}q \hat{=} k_y}) dxdy = $\\
$= - \iint \mu(x, y) e^{-jxp - jyq} dx dy$ (until here just proj. slice th.)\\
\highlight{$\displaystyle \implies \mu(x, y) = \frac{-1}{4\pi^2}\iint \mathcal{F}\{P_\phi(r)\}(p, q) \eu^{\iu xp + \iu yq} \diff p\diff q$}\\
Fill k-space with $\mathcal{F}$ of the proj.s $\hat{=}$ Backproj. of $\mathcal{F}$\\
Then do 2D-backtransformation

\textbf{2nd variant}: Change of integration variables:\\
\fbox{$u(x,y) = \frac{-1}{4\pi^2} \int \limits_0^\pi \int \limits_{-\infty}^\infty \mathcal{F}\{P_\phi(u)\}\eu^{\iu ur} \abs{u} \diff u \diff\phi$}

\textbf{Filtered Backprojection}:\\
$\highlight{\hat{u}(x, y) = \int_0^\pi P_\phi \cdot \mathcal{F}^{-1} \{|u|\} d\phi} = \sum_{j=0}^n \big( p(r,\varphi_j)*h(r) \big) \textrm{d}\varphi$.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Effects that occur in practice \& Spiral CT}
``Streak artifacts'': Too few projections have been acquired. Correction: Additional low-pass $\implies$ SNR rises.

``data grabbing'': Convert cartesian $\leftrightarrow$ polar coordinaates

Filter in image-domain: $|u| \quad \laplace \quad \frac{1}{(2\pi r)^2}$\\
If Fourier-domain is band-limited, the filter becomes a \textbf{Ram-Lak}: $\mathrm{sinc}(r) * \frac{1}{(2\pi r)^2}$
\quad\textcolor{gray}{(don't forget: $\mathrm{rect}(r) \quad\laplace\quad \mathrm{sinc}(r)$)}

Have other filter, in order to not amplify noise: \textbf{Shepp-Logan, Cosine or Hann-Filter}, i.e. combine with low-pass.

\textbf{Spiral CT}: Before backprojection, do linear interpolation of data. \textbf{Pitch}=1: The neighboring slices touch, Pitch=2: slice distance = 1 slice thickness
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{(Quantum) Noise}
%
Distribution of photon counts is $P_\lambda(x)$.
$\textrm{E}\{P_\lambda\} = \lambda \equiv \bar{N}$, $\sigma = \sqrt{\lambda}$.\\
$\!\implies \textrm{SNR} = \frac{\textrm{average}}{\textrm{std. deviation}} \!=\! \frac{\overline{N}}{\sqrt{\overline{N}}} = \sqrt{N}$\\
\phantom{$\!\implies$} where $N$: \#X-rays, $\lambda$: \#Photons registered.\\
$\!\implies \textrm{Image SNR} = \frac{\overline{I}}{\sqrt{\textrm{Var}\{I\}}}$ where $\sigma_I \equiv \sqrt{\textrm{Var}\{I\}} \propto \sqrt{\frac{1}{I_\textrm{A}\,t\,\Delta z}}$

Pixel noise = $\sigma = \sqrt{\frac{1}{M-1}\sum_{i=1}^M (I_i - \overline{I})^2} = \highlight{\sqrt{1 / (Q \Delta z) }}$\\
where Q: Tube current $I_\textrm{A}$ $\times$ scan time $t$, \quad $\Delta z$: detector thickness
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Dose considerations}
%
\textbf{Absorbed dose}: $\unit[1]{Gray} = \unit[1]{Gy} = \unitfrac[1]{J}{Kg}$\\
\textbf{Equivalent dose}: $1 \unit{Sievert} = \unit[1]{Sv} = Q \cdot N \unit{Gy}$\\
with the \textbf{Quality factor Q} (mostly 1) and the \\\textbf{Pertinent factor N} (bone, lung, stomach: 0.12 - skin: 0.1)

\textbf{Excess Relative Risk}:\\
$RR = \frac{\text{Cancer Deaths/y}}{\text{Cancer Deaths control pop./y}}
\hfill\highlight{ERR = \frac{RR-1}{\text{Additional dose}}}$

It is higher for women and children. $2.5/\unit{Sv} - 0.25/\unit{Sv}$


 \chapter[Computing the occurrence of a process]
   {Computing the occurrence of a process}

\section{Particle transport using the {\em differential} approach}
\subsection{The interaction length}
\begin{itemize}
\item[*]
         In a simple material the number of atoms per volume is:
         \[n  = \frac{\mathcal{N}\rho}{A}\]
         where:
         \begin{eqnarray*}
          \mathcal{N} &  & \mbox{Avogadro's number} \\
          \rho        &  & \mbox{density of the medium} \\
          A           &  & \mbox{mass of mole}
         \end{eqnarray*}
\item[*]
         In a compound material the number of atoms of Element elm per volume is:
         \[n_{elm}  = \frac{\mathcal{N}\rho w_{elm}}{A_{elm}}\]
         where:
         \begin{eqnarray*}
          \mathcal{N} &  & \mbox{Avogadro's number} \\
          \rho        &  & \mbox{density of the medium} \\
          w_{elm}     &  & \mbox{proportion by mass of the Element elm}\\
          A_{elm}     &  & \mbox{mass of mole of the Element elm}
         \end{eqnarray*}
\item[*]
         The mean free path, $\lambda$, of a process can be given in terms of its
 total cross section.
         \[
           \lambda(E) \equiv \frac{1}{\Sigma (E)}
             = \frac{1}{\sum_{elm}{\lbrack n_{elm} \sigma(Z_{elm},E)\rbrack}}
         \]
         where $\sigma(Z,E)$  is the total cross section per atom of the
         process and
          $\sum_{elm}$ runs over all Elements the material is made of.
\end{itemize}

\noindent
Cross sections per atom and mean free path values are tabulated during
initialisation.

\subsection{Determination of the interaction point}
The mean free path of a particle for a given process,
$\lambda$, depends on the medium and cannot be used
directly to sample the probability of an interaction in a heterogeneous
detector. The number of mean free paths which a particle travels is:

\begin{equation}
\label{int.c}
n_\lambda =\int \frac{dx}{\lambda(x)}
\end{equation}

and it is independent of the material traversed. If $n_r$ is
a random variable denoting the number of mean free paths from a given
point until the point of interaction, it can be shown that $n_r$ has the
distribution function
\begin{equation}
\label{int.d}
P( n_r < n_\lambda ) = 1-e^{-n_\lambda}
\end{equation}
The total number of mean free paths the particle travels before
the interaction point, $n_\lambda$, is sampled at the beginning
of the trajectory as:
\begin{equation}
\label{int.e}
n_\lambda = -\log \left ( \eta \right )
\end{equation}
where $\eta$ is a random number uniformly distributed
in the range $(0,1)$.
$n_\lambda$ is updated after each step $\Delta x$ according the formula:
\begin{equation}
\label{int.f}
n'_\lambda=n_\lambda -\frac{\Delta x }{\lambda(x)}
\end{equation}
until the step originating from $s(x) = n_\lambda \lambda(x)$ is
the shortest and this triggers the specific process.

The short description given above is the {\em differential approach}
 of the particle transport , which is used in most of the simulation
codes( e.g. \cite{int.geant3},\cite{int.egs4}).

 In this approach besides the other ({\em discrete}) processes
 the continuous energy loss imposes a limit on the stepsize, too.
 The reason of this is the
energy dependence of the cross sections. It is assumed in this approach
that
the cross sections of the particles are  constant during a step , i.e.
 the step size should be so small that the relative difference of the cross
 sections
at the beginning of the step and at the end can be small enough.In principle
one
has to use very small steps in order to have an accurate simulation , but the
computing
time increases if the stepsize decreases. As a good compromise the stepsize is
limited in GEANT4 by the requirement that the stopping range of the particle can
decrease by not more than 20 \% during the step. This condition works fine for
a particle of kinetic energy \(> 0.5 MeV - 1. MeV\) , but for low energy it
gives very short step sizes.
 To cure this problem a lower limitation on the stepsize is also introduced.
The lower limit of the stepsize is
 the {\em cut in range} parameter of the program.

 There is another disadvantage of this usual differential algorithm ,
 which can be a serious problem in the case of certain processes. If the
 interaction process has a cross section with narrow peaks in it ,
in this approach the particle can easily skip over these peaks.(This can happen
 in the case of a number of hadronic processes.)

 To overcome these shortcomings the {\em integral approach} has been implemented
 in GEANT4 as an option .

\section{Particle transport with {\em integral} approach}

 In this algorithm the integral in eq. \ref{int.c} is really computed for every
 process and another equation is used instead of eq. \ref{int.f} when
 $n_\lambda$ is updated. This section gives a short overview on the formulae
 used in this approach.

  The change in the number of mean free path after a {\em small} step
 can be written as

\begin{equation}
\label{int.g}
   \Delta n_\lambda = \frac{\Delta x}{\lambda (x)} .
\end{equation}

This formula can be rewritten in the following form

\begin{equation}
\label{int.h}
   \Delta n_\lambda = \frac{dx}{dT}\cdot \frac{1}{\lambda (T)} \cdot dT  ,
\end{equation}

where
\begin{tabular}[t]{ll}
$T$         & kinetic energy of the particle \\
$\frac{dx}{dT} = \frac{1}{\frac{dT}{dx}}$ & inverse of the well-known quantity {\em $\frac{dE}{dx}$}  . \\
\end{tabular}

After these preliminaires the number of mean free paths (eq. \ref{int.c})
 can be rewritten as
\begin{equation}
\label{int.i}
n_\lambda (T) = \int_{0}^{T}\frac{d\tau}{f(\tau)\cdot \lambda (\tau)}
\end{equation}

 or

\begin{equation}
\label{int.j}
n_\lambda (T) = \int_{0}^{T}\frac{\sigma (\tau ) \cdot d\tau}{f(\tau) }
\end{equation}

where $\frac{dE}{dx}$ has been denoted by $f()$ . Eqs. \ref{int.i} and \ref{int.j} are
the basic equations of the integral approach . The meaning of the integrals
on the right hand sides of the equations is the number of mean free paths
 for a given process between the initial state of energy $T$ and the stopping
 of the particle ( energy $0$).

 The steps of the algorithm using the integral approach are the following:
\begin{itemize}
\item the number of mean free paths the particle travels to the interaction
      point , $\Delta n_\lambda$ is sampled using eq. \ref{int.e}
\item initial value of the quantity $n_\lambda (T_0)$ is computed
\item the (possible) final energy $T$ is calculated from
  \begin{equation}
  \label{int.k}
   \Delta n_\lambda = n_\lambda (T_0) - n_\lambda (T)
  \end{equation}

\item the step limitation originating from the process is
   \begin{equation}
  \label{int.l}
   steplimit = r(T_0) - r(T)
  \end{equation},
  where the function $r(T)$ gives the range of the particle for a kinetic
 energy value T.
\end{itemize}

 If the interaction occurred at the end of the step was  some other
process or the step was limited by
the geometry (at medium boundary) , the number of mean free
paths $\Delta n_\lambda$ should be updated. This update can be easily done
 using the function $n_\lambda (T)$
\begin{equation}
\label{int.m}
 \Delta n_{\lambda,updated} =  \Delta n_\lambda - [n_\lambda (T_0)-
                                    n_\lambda (T) ]
\end{equation}

\section{Updating of the proper and laboratory time of the particle}

 The proper and lab. time of the particle should be updated after each step.
This update is done differently in the two approaches.

 In the differential approach the following formula is used to update the
particle time in the laboratory system

\begin{equation}
\label{int.n}
  \Delta t_{lab} = \frac{\Delta x}{\frac{v_0 + v}{2}}
\end{equation}

  , where
\[
\begin{array}{ll}
\Delta x    & \mbox{step travelled by the particle,} \\
 v_0  &
 \mbox{particle velocity at the beginning  of the step} \\
 v  &
 \mbox{particle velocity at the end of the step .} \\
\end{array}
\]
This expression is a good approximation for use in the differential
approach , because in this scheme the velocity is not allowed to
change too much during the step.This is not the case in the integral approach,
the kinetic energy and the velocity of the particle can change a lot here,
therefore the update of the laboratory time is done by using the integral
expression
\begin{equation}
\label{int.o}
 t = \int_{0}^{s} \frac{dx}{v(x)}
\end{equation}
which can be written as
\begin{equation}
\label{int.p}
 t = \int_{0}^{T} \frac{f(\tau ) \cdot d\tau }{v(\tau )}.
\end{equation}
In the eqs. \ref{int.o} and \ref{int.p}
\[
\begin{array}{ll}
t  &  \mbox{time}  \\
v  &  \mbox{velocity of the particle} \\
s  &  \mbox{path length can be travelled by the particle before stopping} \\
T  &  \mbox{kinetic energy} \\
f(T) & dE/dx \\
\end{array}
\]

 Using eq. \ref{int.p} the expression for $\Delta t_{lab}$ is
\begin{equation}
\label{int.q}
 \Delta t_{lab} = t(T_0)-t(T)  .
\end{equation}

 The update of the proper time is performed similarly in the two approaches.

\section{Status of this document}
  9.10.98   created by L. Urb\'an.

\begin{thebibliography}{99}
\bibitem[EGS4]{int.egs4} W.R. Nelson et al.:The EGS4 Code System.
   {\em SLAC-Report-265 , December 1985 }
\bibitem[GEANT3]{int.geant3}
  GEANT3 manual ,CERN Program Library Long Writeup W5013 (October 1994).
\end{thebibliography}