\BoSSSopen{tutorial10-PoissonSystem/Poisson}
\graphicspath{{tutorial10-PoissonSystem/Poisson.texbatch/}}

\BoSSScmd{
/// \section*{What's new?}
/// \label{sec:poissonSystem_new}
/// \begin{itemize}
///     \item {implementation of a saddle point problem into \BoSSS{}
///           (e.g. the Poisson equation as a system)}
///     \item {central-difference-form and strong form}
///     \item {comparison of both formulations}
///     \item advanced: {algebraic reduction of the poisson problem}
/// \end{itemize}
/// %==========================================
/// \section*{Prerequisites}
/// %==========================================
/// \begin{itemize}
///    \item {implementation of numerical fluxes, chapter \ref{NumFlux}}
///    \item {spatial operator, chapter \ref{SpatialOperator}}
/// \end{itemize}
/// %==========================================
/// \section{Problem statement}
/// %==========================================
/// Within this exercise, we are going to investigate
/// the discretization of a Poisson equation as a system.
/// Obviously, it is possible to discretize the Poisson equation as a system of
/// first-order-PDE's, introducing a vector field $\vec{\sigma}$:
/// \begin{alignat}{3}
///  \vec{\sigma}  + \nabla u & = 0, & & \text{ in } \domain
///    \label{eq:PoissonSys1} \\
///  \operatorname{div}(\vec{\sigma}) &  = g_{\domain}, & & \text{ in } \domain
///    \label{eq:PoissonSys2} \\
///   u                                               & = g_D, & & \text{ on } \Gamma_D \\
///   - \vec{\sigma} \cdot \vec{n}_{\partial \domain} & = g_N, & & \text{ on } \Gamma_N
/// \end{alignat}
/// resp. in matrix-notation:
/// \begin{align*}
///   \begin{bmatrix}
///     \one & \nabla \\
///     \operatorname{div} & 0 \\
///   \end{bmatrix}\cdot
///   \begin{bmatrix}
///     \vec{\sigma}\\
///     u
///   \end{bmatrix}=
///   \begin{bmatrix}
///     0 \\
///     g_{\domain}
///   \end{bmatrix}
/// \end{align*}
/// This exercise, together with the previous one,
/// will form the foundation for an incompressible Stokes- resp. Navier-Stokes solver.
/// \section{Solution within the BoSSS framework}
 }
\BoSSSexeSilent
%
\BoSSScmd{
restart
 }
\BoSSSexeSilent
%
\BoSSScmd{
using ilPSP.LinSolvers; \newline
using BoSSS.Solution.Tecplot; \newline
using ilPSP.Connectors.Matlab;
 }
\BoSSSexe
%
\BoSSScmd{
 %
 }
\BoSSSexe
\BoSSScmd{
/// % ===================================
/// \subsection{Tests on the divergence}
/// % ===================================
 }
\BoSSSexe
\BoSSScmd{
/// \paragraph{Common base-class for $\text{div}$-implementations}
/// We are going to implement two different formulations of the
/// divergence-operator for which going to show equivalence.
/// We implement a common base-class for both formulations:
abstract public class BaseDivergence :  \newline
\btab \btab BoSSS.Foundation.IEdgeForm, // edge integrals \newline
\btab \btab BoSSS.Foundation.IVolumeForm     // volume integrals \newline
\{ \newline
    /// We don't use parameters (e.g. variable viscosity, ...)
    /// at this point: so the parameter list can be null, resp. empty:
\btab public IList<string> ParameterOrdering \{  \newline
\btab \btab get \{ return null; \}  \newline
\btab \} \newline
 \newline
    /// But we have a vector argument variable,
    /// $ [ \sigma_1, \sigma_2 ] = \vec{\sigma} $
    /// (our trial function):
\btab public IList<String> ArgumentOrdering \{  \newline
\btab \btab get \{ return new string[] \{ "sigma1", "sigma2" \}; \}  \newline
\btab \} \newline
 \newline
\btab public TermActivationFlags VolTerms \{ \newline
\btab \btab get \{ \newline
\btab \btab \btab return TermActivationFlags.AllOn; \newline
\btab \btab \} \newline
\btab \} \newline
 \newline
\btab public TermActivationFlags InnerEdgeTerms \{ \newline
\btab \btab get \{ \newline
\btab \btab \btab return (TermActivationFlags.AllOn);  \newline
\btab \btab \} \newline
\btab \} \newline
 \newline
\btab public TermActivationFlags BoundaryEdgeTerms \{ \newline
\btab    get \{ \newline
\btab \btab    return TermActivationFlags.AllOn; \newline
\btab \btab \} \newline
\btab \} \newline
 \newline
    /// The following functions cover the actual math.
    /// For any discretization of the divergence-operator, we have to specify:
    /// \begin{itemize}
    ///    \item a volume integrand,
    ///    \item an edge integrand for inner edges, i.e. on $ \Gamma_i$,
    ///    \item an edge integrand for boundary edges,
    ///          i.e. on $\partial \Omega$.
    /// \end{itemize}
    /// These functions are declared as \code{abstract}, meaning that one has
    /// to specify them in classes derived from \code{BaseLaplace}.
 \newline
\btab abstract public double VolumeForm(ref CommonParamsVol cpv,  \newline
\btab \btab    double[] U, double[,] GradU,  \newline
\btab \btab    double V, double[] GradV);         \newline
 \newline
\btab abstract public double InnerEdgeForm(ref CommonParams inp,  \newline
\btab \btab double[] U\_IN, double[] U\_OT, double[,] GradU\_IN, double[,] GradU\_OT,  \newline
\btab \btab double V\_IN, double V\_OT, double[] GradV\_IN, double[] GradV\_OT); \newline
 \newline
\btab abstract public double BoundaryEdgeForm(ref CommonParamsBnd inp,  \newline
\btab \btab double[] U\_IN, double[,] GradU\_IN, double V\_IN, double[] GradV\_OT); \newline
\}
 }
\BoSSSexe
\BoSSScmd{
/// We are going to use both, Dirichlet- and Neumann-boundary conditions
/// in this exercise; the function \code{IsDirichletBndy} is used to
/// specify the type of boundary condition at point \code{X}:
Func<double[],bool> IsDirichletBndy = delegate(double[] X) \{ \newline
\btab double x = X[0]; \newline
\btab double y = X[1]; \newline
\btab if(Math.Abs(x - (-1.0)) < 1.0e-8) \newline
\btab \btab return true;     \newline
\btab if(Math.Abs(y - (-1.0)) < 1.0e-8) \newline
\btab \btab return true;     \newline
\btab return false; \newline
\}
 }
\BoSSSexe
\BoSSScmd{
/// \paragraph{Formulation (i): Central-difference-form of $\text{div}$}
/// The implementation of the central-difference form is as follows:
 }
\BoSSSexe
\BoSSScmd{
class Divergence\_cendiff : BaseDivergence \{ \newline
 \newline
 \newline
    /// The volume form is equal to
    /// $ -\vec{\sigma} \cdot \nabla v$:
\btab override public double VolumeForm(ref CommonParamsVol cpv,  \newline
\btab \btab double[] Sigma, double[,] GradSigma,  \newline
\btab \btab double V, double[] GradV) \{ \newline
\btab \btab double Acc = 0; \newline
\btab \btab for(int d = 0; d < cpv.D; d++) \{ \newline
\btab \btab \btab Acc -= Sigma[d]*GradV[d]; \newline
\btab \btab \} \newline
\btab \btab return Acc; \newline
\btab \} \newline
 \newline
    /// At the cell boundaries, we use a central-difference-flux,
    /// i.e. $\mean{\vec{\sigma}} \cdot \vec{n}_{\Gamma} \jump{v}$:
\btab override public double InnerEdgeForm(ref CommonParams inp,  \newline
\btab \btab double[] Sigma\_IN, double[] Sigma\_OT, double[,] GradSigma\_IN, double[,] GradSigma\_OT,  \newline
\btab \btab double V\_IN, double V\_OT, double[] GradV\_IN, double[] GradV\_OT) \{ \newline
 \newline
\btab \btab double Acc = 0; \newline
\btab \btab for(int d = 0; d < inp.D; d++) \{ \newline
\btab \btab \btab Acc += 0.5*(Sigma\_IN[d] + Sigma\_OT[d])*inp.Normale[d]*(V\_IN - V\_OT); \newline
\btab \btab \} \newline
\btab \btab return Acc; \newline
\btab \} \newline
 \newline
\btab override public double BoundaryEdgeForm(ref CommonParamsBnd inp,  \newline
\btab \btab double[] Sigma\_IN, double[,] GradSigma\_IN, double V\_IN, double[] GradV\_OT) \{ \newline
 \newline
\btab \btab double Acc = 0; \newline
 \newline
\btab \btab if(IsDirichletBndy(inp.X)) \{ \newline
            /// Dirichlet-boundary: by taking the inner value of $\vec{\sigma}$,
            /// this is a free boundary with respect to $\vec{\sigma}$.
\btab \btab \btab for(int d = 0; d < inp.D; d++) \{ \newline
\btab \btab \btab \btab Acc += Sigma\_IN[d]*inp.Normale[d]*V\_IN; \newline
\btab \btab \btab \} \newline
\btab \btab \} else \{ \newline
            /// Neumann-boundary
\btab \btab \btab double gNeu = 0.0; \newline
\btab \btab \btab Acc += gNeu*V\_IN; \newline
\btab \btab \} \newline
\btab \btab return Acc; \newline
\btab \} \newline
\}
 }
\BoSSSexe
\BoSSScmd{
/// \paragraph{Formulation (ii): 'Strong' form of $\text{div}$:}
/// Here, we use the form
/// \[
///    b(\vec{\sigma},v) =
///    \oint_{\Gamma \backslash \Gamma_D}
///           \mean{v} \jump{\vec{\sigma}} \cdot \vec{n}_\Gamma
///    \dA
///    -
///    \int_{\domain} \operatorname{div}(\vec{\sigma}) \cdot v \dV
/// \]
/// This is actually the negative divergence, which will be more useful
/// later on.
class Divergence\_strong : BaseDivergence \{ \newline
 \newline
    /// We have to implement \code{VolumeForm},
    /// \emph{InnerEdgeForm} and \code{BoundaryEdgeForm}:
\btab override public double VolumeForm(ref CommonParamsVol cpv,  \newline
\btab \btab double[] Sigma, double[,] GradSigma,  \newline
\btab \btab double V, double[] GradV) \{ \newline
\btab \btab double Acc = 0; \newline
\btab \btab for(int d = 0; d < cpv.D; d++) \{ \newline
\btab \btab \btab Acc -= GradSigma[d,d]*V; \newline
\btab \btab \} \newline
\btab \btab return Acc; \newline
\btab \} \newline
 \newline
\btab override public double InnerEdgeForm(ref CommonParams inp,  \newline
\btab \btab double[] Sigma\_IN, double[] Sigma\_OT, double[,] GradSigma\_IN, double[,] GradSigma\_OT,  \newline
\btab \btab double V\_IN, double V\_OT, double[] GradV\_IN, double[] GradV\_OT) \{ \newline
 \newline
\btab \btab double Acc = 0; \newline
\btab \btab for(int d = 0; d < inp.D; d++) \{ \newline
\btab \btab \btab Acc += 0.5*(V\_IN + V\_OT)*(Sigma\_IN[d] - Sigma\_OT[d])*inp.Normale[d]; \newline
\btab \btab \} \newline
\btab \btab return Acc; \newline
\btab \} \newline
 \newline
\btab override public double BoundaryEdgeForm(ref CommonParamsBnd inp,  \newline
\btab \btab double[] Sigma\_IN, double[,] GradSigma\_IN, double V\_IN, double[] GradV\_OT) \{ \newline
 \newline
\btab \btab double Acc = 0; \newline
 \newline
\btab \btab if(IsDirichletBndy(inp.X)) \{ \newline
\btab \btab \btab Acc = 0;\newline
\btab \btab \} else \{ \newline
\btab \btab \btab double gNeu = 0.0; \newline
\btab \btab \btab for(int d = 0; d < inp.D; d++) \{ \newline
\btab \btab \btab \btab Acc += Sigma\_IN[d]*inp.Normale[d]*V\_IN; \newline
\btab \btab \btab \} \newline
\btab \btab \btab Acc -= gNeu*V\_IN; \newline
\btab \btab \} \newline
\btab \btab return Acc; \newline
\btab \} \newline
\}
 }
\BoSSSexe
\BoSSScmd{
    /// %==========================================
    /// \subsection{Equality test}
    /// %==========================================
 }
\BoSSSexe
\BoSSScmd{
    /// We are going to test the equivalence of both formulations
    /// on a 2D grid, using a DG basis of degree 1:
 var grd2D               = Grid2D.Cartesian2DGrid(GenericBlas.Linspace(-1,1,6),                                                    GenericBlas.Linspace(-1,1,7)); \newline
 var gdata2D             = new GridData(grd2D); \newline
 var b                   = new Basis(gdata2D, 1); \newline
 SinglePhaseField sigma1 = new SinglePhaseField(b,"sigma1"); \newline
 SinglePhaseField sigma2 = new SinglePhaseField(b,"sigma2"); \newline
 CoordinateVector sigma  = new CoordinateVector(sigma1,sigma2); \newline
 var TrialMapping        = sigma.Mapping; \newline
 var TestMapping         = new UnsetteledCoordinateMapping(b);
 }
\BoSSSexe
\BoSSScmd{
    /// We create the matrix of the central-difference formulation:
 var OpDiv\_cendiff = (new Divergence\_cendiff()).Operator(); \newline
 var MtxDiv\_cendiff = OpDiv\_cendiff.ComputeMatrix(TrialMapping,  \newline
\btab \btab \btab \btab \btab \btab \btab \btab \btab \btab \btab \btab   null,  \newline
\btab \btab \btab \btab \btab \btab \btab \btab \btab \btab \btab \btab   TestMapping);
 }
\BoSSSexe
\BoSSScmd{
    /// We create the matrix of the strong formulation
    /// and show that the matrices of both formulations are equal.
    /// We use the \code{InfNorm(...)}-method to identify whether a
    /// matrix is (approximately) zero or not.
 var OpDiv\_strong  = (new Divergence\_strong()).Operator(); \newline
 var MtxDiv\_strong = OpDiv\_strong.ComputeMatrix(TrialMapping, null, TestMapping); \newline
 var TestP         = MtxDiv\_cendiff + MtxDiv\_strong; \newline
 TestP.InfNorm();
 }
\BoSSSexe
\BoSSScmd{
/// %==================================
/// \subsection{The gradient-operator}
/// \label{subsec:poissonSystem_gradientOp}
/// %==================================
/// For the variational formulation of the gradient operator, a vector-valued
/// test-function is required. Unfourtunately, this is not supported by
/// \BoSSS. Therefore we have to discretize the gradent component-wise,
/// i.e. as $\partial_{x}$ and $\partial_y$. A single derivative
/// can obviously be expressed as a divergence by the
/// identity $ \partial_{x_d} = \text{div}( \vec{e}_d u ) $.
class Gradient\_d : \newline
\btab \btab BoSSS.Foundation.IEdgeForm, // edge integrals \newline
\btab \btab BoSSS.Foundation.IVolumeForm     // volume integrals \newline
\{ \newline
\btab public Gradient\_d(int \_d) \{ \newline
\btab \btab this.d = \_d; \newline
\btab \} \newline
 \newline
    /// The component index of the gradient:
\btab int d; \newline
 \newline
    /// As ususal, we do not use parameters:
\btab public IList<string> ParameterOrdering \{  \newline
\btab \btab get \{ return null; \}  \newline
\btab \} \newline
 \newline
    /// We have one argument $u$:
\btab public IList<String> ArgumentOrdering \{  \newline
\btab \btab get \{ return new string[] \{ "u" \}; \}  \newline
\btab \} \newline
 \newline
\btab public TermActivationFlags VolTerms \{ \newline
\btab \btab get \{ return TermActivationFlags.AllOn; \} \newline
\btab \} \newline
 \newline
\btab public TermActivationFlags InnerEdgeTerms \{ \newline
\btab \btab get \{ return (TermActivationFlags.AllOn); \} \newline
\btab \} \newline
 \newline
\btab public TermActivationFlags BoundaryEdgeTerms \{ \newline
\btab    get \{ return TermActivationFlags.AllOn; \} \newline
\btab \} \newline
 \newline
    /// Now, we implement
    /// \begin{itemize}
    ///    \item the volume form $u \vec{e}_d \cdot \nabla v$
    ///    \item the boundary form
    ///       $\mean{u \ \vec{e}_d} \cdot \vec{n}_\Gamma \jump{v}$
    /// \end{itemize}
\btab public double VolumeForm(ref CommonParamsVol cpv,  \newline
\btab \btab    double[] U, double[,] GradU,  \newline
\btab \btab    double V, double[] GradV) \{ \newline
 \newline
\btab \btab double Acc = 0; \newline
\btab \btab Acc -= U[0]*GradV[this.d]; \newline
\btab \btab return Acc; \newline
\btab \}         \newline
 \newline
\btab public double InnerEdgeForm(ref CommonParams inp,  \newline
\btab \btab double[] U\_IN, double[] U\_OT, double[,] GradU\_IN, double[,] GradU\_OT,  \newline
\btab \btab double V\_IN, double V\_OT, double[] GradV\_IN, double[] GradV\_OT) \{ \newline
 \newline
\btab \btab double Acc = 0; \newline
\btab \btab Acc += 0.5*(U\_IN[0] + U\_OT[0])*inp.Normale[this.d]*(V\_IN - V\_OT); \newline
\btab \btab return Acc;   \newline
\btab  \} \newline
 \newline
\btab public double BoundaryEdgeForm(ref CommonParamsBnd inp,  \newline
\btab \btab double[] U\_IN, double[,] GradU\_IN, double V\_IN, double[] GradV\_OT) \{ \newline
 \newline
\btab \btab double Acc = 0; \newline
\btab \btab if(IsDirichletBndy(inp.X)) \{ \newline
\btab \btab \btab double u\_Diri = 0.0; \newline
\btab \btab \btab Acc += u\_Diri*inp.Normale[this.d]*V\_IN; \newline
\btab \btab \} else \{ \newline
\btab \btab \btab Acc += U\_IN[0]*inp.Normale[this.d]*V\_IN; \newline
\btab \btab \} \newline
\btab \btab return Acc;               \newline
\btab \} \newline
\}
 }
\BoSSSexe
\BoSSScmd{
/// Now, we are ready to assemble the full $\nabla$ operator
/// as $\left[ \begin{array}{c} \partial_x \\ \partial_y \end{array} \right]$.
var OpGrad = new SpatialOperator(1,2,QuadOrderFunc.Linear(),"u","c1","c2"); \newline
OpGrad.EquationComponents["c1"].Add(new Gradient\_d(0)); \newline
OpGrad.EquationComponents["c2"].Add(new Gradient\_d(1)); \newline
OpGrad.Commit();
 }
\BoSSSexe
\BoSSScmd{
/// As an additional test, we create the gradient-matrix and verify that
/// its transpose
/// is equal to the negative \code{MtxDiv}-matrix:
var MtxGrad = OpGrad.ComputeMatrix(TestMapping, null, TrialMapping); \newline
var Test2   = MtxGrad.Transpose() - MtxDiv\_strong; \newline
Test2.InfNorm();
 }
\BoSSSexe
\BoSSScmd{
/// %====================================
/// \subsection{The complete Poisson-system}
/// %====================================
 }
\BoSSSexe
\BoSSScmd{
///\paragraph{Assembly of the system}
 }
\BoSSSexe
\BoSSScmd{
/// We also need the identity-matrix in the top-left corner
/// of the Poisson-system:
public class Identity :  \newline
\btab \btab BoSSS.Foundation.IVolumeForm  \newline
\{ \newline
\btab public IList<string> ParameterOrdering \{  \newline
\btab \btab get \{ return new string[0]; \}  \newline
\btab \} \newline
 \newline
\btab public string component;  \newline
 \newline
\btab public IList<String> ArgumentOrdering \{  \newline
\btab \btab get \{ return new string[] \{ component \}; \}  \newline
\btab \} \newline
 \newline
\btab public TermActivationFlags VolTerms \{ \newline
\btab \btab get \{ \newline
\btab \btab \btab return TermActivationFlags.AllOn; \newline
\btab \btab \} \newline
\btab \} \newline
 \newline
\btab public double VolumeForm(ref CommonParamsVol cpv,  \newline
\btab \btab    double[] U, double[,] GradU,  \newline
\btab \btab    double V, double[] GradV) \{ \newline
\btab \btab return U[0]*V;            \newline
\btab \} \newline
\}
 }
\BoSSSexe
\BoSSScmd{
/// We are going to implement the linear Poisson-operator
/// \[
/// \left[ \begin{array}{ccc}
///      1           &  0         & \partial_x \\
///      0           &  1         & \partial_y \\
///      -\partial_x & -\partial_y & 0
/// \end{array} \right]
/// \cdot
/// \left[ \begin{array}{c} \sigma_0 \\ \sigma_1 \\ u \end{array} \right]
/// =
/// \left[ \begin{array}{c} c_0 \\ c_1 \\ c_2 \end{array} \right]
/// \]
/// The variables $c_0$, $c_1$ and $c_2$, which correspond to the
/// test functions are also called co-domain variables of the operator.
/// We are using the negative divergence, since this will lead to a
/// symmetric matrix, instead of a anti-symmetric one.
/// By doing so, we can e.g. use a Cholesky-factorization to determine
/// whether the system is definite or not.
var OpPoisson = new SpatialOperator(3, 3,  \newline
\btab \btab \btab \btab \btab   QuadOrderFunc.Linear(), \newline
\btab \btab \btab \btab \btab   "sigma1", "sigma2", "u", // the domain-variables \newline
\btab \btab \btab \btab \btab   "c1", "c2", "c3"); //       the co-domain variables  \newline
/// Now we add all required components to \code{OpPoisson}:
OpPoisson.EquationComponents["c1"].Add(new Gradient\_d(0)); \newline
OpPoisson.EquationComponents["c1"].Add(new Identity() \{ component = "sigma1" \}); \newline
OpPoisson.EquationComponents["c2"].Add(new Gradient\_d(1)); \newline
OpPoisson.EquationComponents["c2"].Add(new Identity() \{ component = "sigma2" \}); \newline
OpPoisson.EquationComponents["c3"].Add(new Divergence\_strong()); \newline
OpPoisson.Commit();
 }
\BoSSSexe
\BoSSScmd{
/// We create mappings $[\sigma_1, \sigma_2, u ]$:
/// three different combinations of DG orders will be investigated:
/// \begin{itemize}
/// \item equal order: the same polynomial degree for $u$ and $\vec{\sigma}$
/// \item mixed order: the degree of $u$ is lower than the degree
///       of $\vec{\sigma}$.
/// \item `strange' order: the degree of $u$ is higher than the degree of
///       $\vec{\sigma}$.
/// \end{itemize}
var b3         = new Basis(gdata2D, 3); \newline
var b2         = new Basis(gdata2D, 2); \newline
var b4         = new Basis(gdata2D, 4); \newline
var EqualOrder = new UnsetteledCoordinateMapping(b3,b3,b3); \newline
var MixedOrder = new UnsetteledCoordinateMapping(b4,b4,b3); \newline
var StrngOrder = new UnsetteledCoordinateMapping(b2,b2,b3);
 }
\BoSSSexe
\BoSSScmd{
var MtxPoisson\_Equal = OpPoisson.ComputeMatrix(EqualOrder, null, EqualOrder); \newline
var MtxPoisson\_Mixed = OpPoisson.ComputeMatrix(MixedOrder, null, MixedOrder); \newline
var MtxPoisson\_Strng = OpPoisson.ComputeMatrix(StrngOrder, null, StrngOrder);
 }
\BoSSSexe
\BoSSScmd{
/// We show that the matrices are symmetric
/// (use e.g. \code{SymmetryDeviation(...)}), but indefinite
/// (use e.g. \code{IsDefinite(...)}).
 }
\BoSSSexe
\BoSSScmd{
double symDev\_Equal = MtxPoisson\_Equal.SymmetryDeviation();\newline
symDev\_Equal;
 }
\BoSSSexe
\BoSSScmd{
double symDev\_Mixed = MtxPoisson\_Mixed.SymmetryDeviation();\newline
symDev\_Mixed;
 }
\BoSSSexe
\BoSSScmd{
double symDev\_Strng = MtxPoisson\_Strng.SymmetryDeviation();\newline
symDev\_Strng;
 }
\BoSSSexe
\BoSSScmd{
MtxPoisson\_Equal.IsDefinite();
 }
\BoSSSexe
\BoSSScmd{
MtxPoisson\_Mixed.IsDefinite();
 }
\BoSSSexe
\BoSSScmd{
MtxPoisson\_Strng.IsDefinite();
 }
\BoSSSexe
\BoSSScmdSilent{
/// BoSSScmdSilent BoSSSexeSilent
NUnit.Framework.Assert.LessOrEqual(symDev\_Equal, 1.0e-8);\newline
NUnit.Framework.Assert.LessOrEqual(symDev\_Mixed, 1.0e-8);\newline
NUnit.Framework.Assert.LessOrEqual(symDev\_Strng, 1.0e-8);
 }
\BoSSSexeSilent
\BoSSScmd{
    /// %==========================================
    /// \section{Advanced topics}
    /// %==========================================
 }
\BoSSSexe
\BoSSScmd{
/// %====================================
/// \subsection{Algebraic reduction}
/// %====================================
/// Since the top-left corner of our matrix
/// \[
/// \left[ \begin{array}{cc}
/// 1   & B \\
/// B^T & 0
/// \end{array} \right]
/// \]
/// is actually very easy to eliminate the variable $\vec{\sigma}$
/// from our system algebraically.
/// The matrix of the reduces system is obviously $B^T \cdot B$.
 }
\BoSSSexe
\BoSSScmd{
/// \paragraph{Extraction of sub-matrices and elimination}
/// From the mapping, we can actually obtain index-lists for each variable,
/// which can then be used to extract sub-matrices from
/// \code{MtxPoisson\_Equal}, \code{MtxPoisson\_Mixed}, resp.
/// \code{MtxPoisson\_Strng}.
int[] SigmaIdx\_Equal = EqualOrder.GetSubvectorIndices(true, 0,1); \newline
int[] uIdx\_Equal     = EqualOrder.GetSubvectorIndices(true, 2); \newline
int[] SigmaIdx\_Mixed = MixedOrder.GetSubvectorIndices(true, 0,1); \newline
int[] uIdx\_Mixed     = MixedOrder.GetSubvectorIndices(true, 2); \newline
int[] SigmaIdx\_Strng = StrngOrder.GetSubvectorIndices(true, 0,1); \newline
int[] uIdx\_Strng     = StrngOrder.GetSubvectorIndices(true, 2);
 }
\BoSSSexe
\BoSSScmd{
// The extraction of the sub-matrix and the elimination, for the equal order: \newline
var MtxPoissonRed\_Equal =  \newline
\btab   MtxPoisson\_Equal.GetSubMatrix(uIdx\_Equal, SigmaIdx\_Equal)  // -Divergence \newline
\btab * MtxPoisson\_Equal.GetSubMatrix(SigmaIdx\_Equal, uIdx\_Equal); // Gradient
 }
\BoSSSexe
\BoSSScmd{
/// Finally, we also
/// create the reduced system for the mixed and the strange
/// order, test for the definiteness of the reduced system.
/// Equal and mixed order are positive definite, while the strange order
/// is indefinite - a clear indication that something ist wrong:
 }
\BoSSSexe
\BoSSScmd{
var MtxPoissonRed\_Mixed =  \newline
\btab   MtxPoisson\_Mixed.GetSubMatrix(uIdx\_Mixed, SigmaIdx\_Mixed)  // -Divergence \newline
\btab * MtxPoisson\_Mixed.GetSubMatrix(SigmaIdx\_Mixed, uIdx\_Mixed); // Gradient   \newline
var MtxPoissonRed\_Strng =  \newline
\btab   MtxPoisson\_Strng.GetSubMatrix(uIdx\_Strng, SigmaIdx\_Strng)  // -Divergence \newline
\btab * MtxPoisson\_Strng.GetSubMatrix(SigmaIdx\_Strng, uIdx\_Strng); // Gradient
 }
\BoSSSexe
\BoSSScmd{
bool isdef\_red\_Equal = MtxPoissonRed\_Equal.IsDefinite();\newline
isdef\_red\_Equal;
 }
\BoSSSexe
\BoSSScmd{
bool isdef\_red\_Mixed = MtxPoissonRed\_Mixed.IsDefinite();\newline
isdef\_red\_Mixed;
 }
\BoSSSexe
\BoSSScmd{
bool isdef\_red\_Strng = MtxPoissonRed\_Strng.IsDefinite();\newline
isdef\_red\_Strng;
 }
\BoSSSexe
\BoSSScmdSilent{
/// BoSSScmdSilent BoSSSexeSilent
NUnit.Framework.Assert.IsTrue(isdef\_red\_Equal);\newline
NUnit.Framework.Assert.IsTrue(isdef\_red\_Mixed);\newline
NUnit.Framework.Assert.IsFalse(isdef\_red\_Strng);
 }
\BoSSSexeSilent
\BoSSScmd{
/// We compute the condition number of all three matrices; we observe that
/// the mixed as well as the equal-order discretization result give rather
/// moderate condition numbers. For the strange orders, the condition number
/// of the system is far to high:
 }
\BoSSSexe
\BoSSScmd{
double condest\_Mixed = MtxPoissonRed\_Mixed.condest();\newline
condest\_Mixed;
 }
\BoSSSexe
\BoSSScmd{
double condest\_Equal = MtxPoissonRed\_Equal.condest();\newline
condest\_Equal;
 }
\BoSSSexe
\BoSSScmd{
double condest\_Strng = MtxPoissonRed\_Strng.condest();\newline
condest\_Strng;
 }
\BoSSSexe
\BoSSScmdSilent{
/// BoSSScmdSilent BoSSSexeSilent
NUnit.Framework.Assert.LessOrEqual(condest\_Mixed, 1e5);\newline
NUnit.Framework.Assert.LessOrEqual(condest\_Equal, 1e5);\newline
NUnit.Framework.Assert.Greater(condest\_Strng, 1e10);
 }
\BoSSSexeSilent


\section{Error Calculation}
\label{sec:Error_Calculation}
This section contains the error calculation of the measured values. The error calculation is done for the fitted vacuum permeability $\mu_0$ (see sections \ref{subsubsec:Short_Cylindrical_Coil} to \ref{subsubsec:Long_Cylindrical_Coil_Field}).

\subsection{Uncertainties}
\label{subsec:Uncertainties}
All conducted measurements in this experiment have uncertainties. The uncertainty of the gaussmeter and the Hall sensor is really small (only 0.3 \%) compared to the other uncertainties. This means that it could be neglected. In this exercise it is considered nevertheless. The systematic uncertainties are shown in table \ref{tab:measurement_devices_and_sensors} and table \ref{tab:Specifications_Cylindrical_Coils} (the uncertainty of the radius $R$ is only half of the uncertainty of the diameter $d$).

\subsection{Calculating the Uncertainty of $\mu_0$}
\label{subsec:Calculating_the_Uncertainty}
To derive the total uncertainty of the vacuum permeability $\mu_0$ the following equations are used. The statistical uncertainty is obtained from the fits (from QtiPlot).
\begin{equation}
s_{\mu_{0,\ TOT}}=\sqrt{s_{\mu_{0,\ SYST}}^2+s_{\mu_{0,\ STAT}}^2}
\label{eq:total_uncert}
\end{equation}
with:
\begin{equation}
s_{\mu_{0,\ SYST}}=\sqrt{\left(\frac{\partial \mu_0}{\partial B_0}\Biggr|_{\mu_0}\cdot s_{B_0}\right)^2 + \left(\frac{\partial \mu_0}{\partial I}\Biggr|_{\mu_0}\cdot s_{I}\right)^2 + \left(\frac{\partial \mu_0}{\partial l}\Biggr|_{\mu_0}\cdot s_{l}\right)^2 + \left(\frac{\partial \mu_0}{\partial R}\Biggr|_{\mu_0}\cdot s_{R}\right)^2}
\label{eq:error_propagation}
\end{equation}

and with:

\[
\frac{\partial \mu_0}{\partial B_0}\Biggr|_{\mu_0}=\frac{l}{NI}\cdot\sqrt{1+(\,^{2R}\!/_{l})^2} \qquad , \qquad \frac{\partial \mu_0}{\partial I}\Biggr|_{\mu_0}=-\frac{B_0l}{NI^2}\cdot\sqrt{1+(\,^{2R}\!/_{l})^2}
\]

\[
\frac{\partial \mu_0}{\partial l}\Biggr|_{\mu_0}=\frac{B_0}{NI}\cdot\frac{1}{\sqrt{1+(\,^{2R}\!/_{l})^2}} \qquad , \qquad \frac{\partial \mu_0}{\partial R}\Biggr|_{\mu_0}=\frac{4B_0R}{lNI}\cdot\frac{1}{\sqrt{1+(\,^{2R}\!/_{l})^2}}
\]

where:
\begin{conditions}
	s_{\mu_{0,\ TOT}} & total uncertainty of $\mu_0$ \\
	s_{\mu_{0,\ SYST}} & systematical uncertainty of $\mu_0$ \\
	s_{\mu_{0,\ STAT}} & statistical uncertainty of $\mu_0$ (obtained from a fit) \\
	B_0 & magnetic field in the center \\
	\mu_0 & vacuum permeability \\
	N & number of turns \\
	I & current \\
	l & length \\
	R & radius
\end{conditions}

\subsection{Calculated Uncertainties $\mu_0$}
\label{subsec:Calculated_Uncertainties}
The systematic uncertainty was calculated using equation \ref{eq:error_propagation} and the statistical uncertainty was obtained from the fits (see sections \ref{subsubsec:Short_Cylindrical_Coil} to \ref{subsubsec:Long_Cylindrical_Coil_Field}). The total uncertainty was calculated by using equation \ref{eq:total_uncert}.
\begin{table}[H]
	\centering
	\renewcommand{\arraystretch}{1.3}
	\begin{tabular}{r||c|c|c}
		 & \textbf{Systematic} $(\,^\text{Vs}\!/_\text{Am})$ & \textbf{Statistical} $(\,^\text{Vs}\!/_\text{Am})$ & \textbf{Total} $(\,^\text{Vs}\!/_\text{Am})$ \\
		\hline\hline
		\textbf{Short Center} (sec. \ref{subsubsec:Short_Cylindrical_Coil}) & $1.92167\cdot10^{-8}$ & $1.87622\cdot10^{-10}$ & $1.92176\cdot10^{-8}$ \\
		\textbf{Short Field} (sec. \ref{subsubsec:Short_Cylindrical_Coil_Field}) & $1.92288\cdot10^{-8}$ & $6.34659\cdot10^{-10}$ & $1.92393\cdot10^{-8}$ \\
		\textbf{Long Center} (sec. \ref{subsubsec:Long_Cylindrical_Coil}) & $1.48213\cdot10^{-8}$ & $1.57922\cdot10^{-10}$ & $1.48221\cdot10^{-8}$ \\
		\textbf{Long Field} (sec. \ref{subsubsec:Long_Cylindrical_Coil_Field}) & $1.47958\cdot10^{-8}$ & $3.64561\cdot10^{-10}$ & $1.48003\cdot10^{-8}$ \\
	\end{tabular}
	\caption{Calculated Uncertainties}
	\label{tab:Calculated_Uncertainties}
\end{table}

To calculate the systematic uncertainty of the short coil (center value) the measurement point where the current $I=0.42$\ A was used (see table \ref{tab:Short_Cylindrical_Coil_Measurements_Central_Value}).

To calculate the systematic uncertainty of the short coil (field pattern) the measurement point which is closest to the center was used. The position of this point is $z=-0.003$\ m (see table \ref{tab:Short_Cylindrical_Coil_Measurements_Field_Pattern}).

To calculate the systematic uncertainty of the long coil (center value) the measurement point where the current $I=0.521$\ A was used (see table \ref{tab:Long_Cylindrical_Coil_Measurements_Central_Value}).

To calculate the systematic uncertainty of the long coil (field pattern) the measurement point which is closest to the center was used. The position of this point is $z=0.004$\ m (see table \ref{tab:Long_Cylindrical_Coil_Measurements_Field_Pattern}).


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\chapter{Dimensionality Reduction}
\label{chap:dim_reduct}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Feature Selection}
\label{dim_reduct:feature_selection}
% TODO
% TODO Can look at correlation or mutual information between variables
% TODO could also use a chi2 test for independence between input variables and the dependent variable, see which ones might be useful. See https://scikit-learn.org/stable/modules/generated/sklearn.feature_selection.chi2.html

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Forward and Backward Feature Selection}
\label{dim_reduct:feature_selection:forward_backward}
% TODO

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Principle Component Analysis (PCA)}
\label{dim_reduct:PCA}
% https://youtu.be/dhK8nbtii6I

Principle component analysis (PCA) \cite{pca} is a popular linear method
of reducing a dataset to a limited set of more descriptive dimensions.
PCA works by preforming a change of basis from
the dataset's original vector space to a new orthonormal basis which
minimizes the average squared distance between the data points and the basis vectors,
or equivalently maximizes the variance of the data in the new vector space.

To begin, we construct a $n \times m$ matrix $\mathbf{X}$
from the $n$ features and $m$ data points in the original vector space
with basis $\left\{\vu{e}_{i}\right\}$.
If we project the $\vb{x}_{j}$ data point on to
some new unit vector $\vu{u}_{i}$,
$\vb{x}_{j} \to \vu{u}_{i}\transpose \vb{x}_{j} \vu{u}_{i}$,
the variance of the data along $\vu{u}_{i}$ will be

\begin{subequations}\label{dim_reduct:PCA:variance}
\begin{align}
\variance{\mathbf{X} \vu{u}_{i}}
&= \frac{1}{m}\sum_{j=1}^{m} \left(
\vu{u}_{i}\transpose \vb{x}_{j}
-
\vu{u}_{i}\transpose \expval{\vb{x}}
\right)^{2}
= \frac{1}{m}\sum_{j=1}^{m} \left( \vu{u}_{i}\transpose \left(\vb{x}_{j} - \expval{\vb{x}}\right)\right)^{2}, \label{dim_reduct:PCA:variance:a} \\
&= \frac{1}{m}\sum_{j=1}^{m}
\vu{u}_{i}\transpose
\left(\vb{x}_{j} - \expval{\vb{x}}\right)
\left(\vb{x}_{j} - \expval{\vb{x}}\right)\transpose
\vu{u}_{i}
= \vu{u}_{i}\transpose \mathbf{M} \vu{u}_{i}, \label{dim_reduct:PCA:variance:M}
\end{align}
\end{subequations}

\noindent where $\mathbf{M}$ is
the covariance matrix\footnote{$M_{ij} = \cov{\mathbf{X} \vu{e}_{i}}{\mathbf{X} \vu{e}_{j}}$,
see \cref{stats:corr_covar:covar_matrix}.} of $\mathbf{X}$
and $\expval{\vb{x}} = \sum_{i=1}^{n} \expval{\mathbf{X} \vu{e}_{i}} \vu{e}_{i}$.
We now wish to find a new orthonormal basis $\left\{\vu{u}_{i}\right\}$
which maximizes $\variance{\mathbf{X} \vu{u}_{i}}$,
subject to the constraint that $\abs{\vu{u}_{i}} = \vu{u}_{i}\transpose \vu{u}_{i} = 1$,
\ie the classic Lagrange multiplier problem of \cref{opt:lagrange_mult}.

Taking matrix derivatives

\begin{subequations}\label{dim_reduct:PCA:lagrange}
\begin{align}
0 &= \dv{\vu{u}_{i}} \left( \vu{u}_{i}\transpose \mathbf{M} \vu{u}_{i}
+ \lambda \left( 1 - \vu{u}_{i}\transpose \vu{u}_{i} \right) \right)
= 2 \mathbf{M} \vu{u}_{i} - 2 \lambda \vu{u}_{i}, \label{dim_reduct:PCA:lagrange:setup} \\
&\implies \mathbf{M}\vu{u}_{i} = \lambda \vu{u}_{i}, \label{dim_reduct:PCA:lagrange:eigen}
\end{align}
\end{subequations}

\noindent we discover that $\left\{\vu{u}_{i}\right\}$
is just the eigenbasis of $\mathbf{M}$ with eigenvalues $\lambda_{i}$.
If we left multiply \cref{dim_reduct:PCA:lagrange:eigen} by $\vu{u}_{i}\transpose$
we see that the largest eigenvalue $\lambda_{1}$ corresponds to the
new $\vu{u}_{1}$ dimension with the largest variance, \ie the first principle component (PC1),
$\vu{u}_{2}$ with the second largest variance, \ie second principle component (PC2), and so on.

In practice, we should first center and normalize the data,
by subtracting the mean and dividing by the standard deviation along each dimension,
before preforming the eigendecomposition.
Intuitively, PCA is stretching and rotating the vector space to find the best PCs,
but the origin will remain fixed, $\vu{u}_{i}\transpose\,\va{0}\,\vu{u}_{i} = \va{0}$,
and the input dimensions are being mixed together $1 \mathbin{:} 1$ without regard for units.
Some PCA software packages perform these preprocessing steps by default,
but it is not universal so check the appropriate documentation.

If there are few data points relative to the original dimensions, $m < n$,
the eigendecomposition will still work, but some of the $\lambda_{i}$ may be $< 0$.
These eigenvalues should be disregarded, leaving $\min\left(m,n\right)$ possible principle components.

% TODO real estate example: show how PCA can merge fields like # rooms, # bathrooms, sq footage, zip code, distance to schools, etc down to two dimensions standing in for house size and location
% TODO include representative figure?
% TODO many ways of actually doing the computation, including sparse PCA

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Scree plots}
\label{dim_reduct:PCA:Scree}
% TODO

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Singular Value Decomposition (SVD)}
\label{dim_reduct:SVD}
% TODO

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Linear Discriminant Analysis (LDA)}
\label{dim_reduct:LDA}
% TODO

% TODO include Gaussian Discriminant Analysis (GDA) and Quadratic Discriminant Analysis (QDA)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Isomap}
\label{dim_reduct:isomap}
% TODO

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Factor Analysis and Confounding Variables}
\label{dim_reduct:factor_ana}
% TODO

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Mutual Information (MI)}
\label{dim_reduct:MI}
% TODO

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Normalized Mutual Information (NMI)}
\label{dim_reduct:MI:normalized}
% TODO

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Term Frequency-Inverse Document Frequency (TF-IDF)}
\label{dim_reduct:tfidf}

Term frequency-inverse document frequency (TF-IDF) is a statistic
used in natural language processing (NLP) to quantify
the importance, or uniqueness, of a term $t$ in a document $d$
with respect to a wider set of documents $D$\footnote{Note that $d$ is not necessarily an element of $D$, we can compare novel $d$ to a reference corpus $D$.}.
As the name suggests, TF-IDF is the product of two components,
one representing the frequency of the term under consideration within the document of interest,
and the other the inverse of the frequency of the term in all documents of the broader corpus.
There are a handful of definitions available for each term, but we will only describe
one of the more standard forms in this section:

\begin{subequations}\label{eq:unsupervised:tfidf}
\begin{align}
\text{tf}\left(t,d\right) &= \frac{n_{t,d}}{\sum_{t' \in d} n_{t',d}}, \label{eq:unsupervised:tfidf:tf} \\
\text{idf}\left(t,D\right) &= \log\left(\frac{\abs{D}}{1 + \abs{\left\{d' \in D \, | \, t \in d'\right\}}}\right), \label{eq:unsupervised:tfidf:idf} \\
\text{tf-idf}\left(t,d\right) &= \text{tf}\left(t,d\right) \times \text{idf}\left(t,D\right). \label{eq:unsupervised:tfidf:tfidf}
\end{align}
\end{subequations}

\noindent Here $n_{t,d}$ is number of times $t$ appears in $d$,
$\abs{D}$ is the number of documents in $D$,
and $\abs{\left\{d \in D \, | \, t \in d\right\}}$ is the number of number of documents in $D$ which contain $t$.
We take the natural log in the $\text{idf}\left(t,D\right)$ component to better accommodate large corpora of documents,
and include the constant $1+$ term to avoid divide-by-zero issues.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Prevalence Ratio}
\label{dim_reduct:prevalence_ratio}

The prevalence ratio (PR) is a similar concept to TF-IDF,
allowing us to identify important characteristics of a population of interest versus the wider population.
For example, we can use a PR to assess the association of hypertension with heart failure by comparing
the prevalence of hypertension in a cohort of heart failure patients
to the prevalence of hypertension in the general population,
or at least a representative sample\footnote{Defining an acceptable representative sample is often the hardest part of the analysis and may require stratification, \eg stratifying by age when investigating disease.} of it.

The prevalence of a characteristic $c$ in a population $P$ is $\abs{\{p' \in P \, | \, c \in p'\}} \, / \, \abs{p' \in P}$,
note the similarity to $\text{tf}\left(t,d\right)$ \cref{eq:unsupervised:tfidf:tf}.
The prevalence ratio is then

\begin{equation}\label{eq:unsupervised:PR}
\text{PR}\left(t,d\right) = \frac{ \abs{\{p' \in P \, | \, c \in p'\}} \, / \, \abs{p' \in P} }{ \abs{\{p' \in P_{0} \, | \, c \in p'\}} \, / \, \abs{p' \in P_{0}} },
\end{equation}

\noindent where $P$ is the population of interest and $P_{0}$ is the wider population.

Note that the PR is mathematically identical to the relative risk, or hazard ratio (HR) discussed in \cref{chap:survival}.
There is a similar discussion in the literature \cite{pmid27460748,10.3389/fvets.2017.00193}
on prevalence ratios versus odds ratios as is described in \cref{survival:additional:odds}.

% See also: https://sph.unc.edu/wp-content/uploads/sites/112/2015/07/nciph_ERIC8.pdf

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{\texorpdfstring{$t$}{t}-Distributed Stochastic Neighbor Embedding (\texorpdfstring{$t$}{t}-SNE)}
\label{dim_reduct:tSNE}
% TODO
% TODO \tSNE


\documentclass[11pt,twocolumn]{amsart} % twocolumn
\usepackage{geometry}                % See geometry.pdf to learn the layout options. There are lots.
\geometry{a4paper}                   % ... or a4paper or a5paper or ...
\usepackage{layout}
%\geometry{landscape}                % Activate rotated page geometry
%\usepackage[parfill]{parskip}    % Activate to begin paragraphs with an empty line rather than an indent
\usepackage{graphicx}
\usepackage{amssymb}
\usepackage{epstopdf}
\DeclareGraphicsRule{.tif}{png}{.png}{`convert #1 `dirname #1`/`basename #1 .tif`.png}
\DeclareMathOperator*{\argmin}{arg\,min}

% shortcuts
\newcommand{\ve}[1]{\boldsymbol{#1}}
\newcommand{\ma}[1]{\boldsymbol{#1}}
\newenvironment{m}{\begin{bmatrix}}{\end{bmatrix}}
\def\argmax{\operatornamewithlimits{arg\,max}}

\title{Some stuff for \textbf{home}}
\author{Gaspard Buma}
%\date{}                                           % Activate to display a given date or no date

\begin{document}

\twocolumn[
\maketitle
]
\section{Notations}
Data set
\[
	\ve{s_t} = \begin{m} s_{t1} & \hdots & s_{t12} \end{m}
\]
\begin{align*}
	\ve{t_t} = & \begin{m} \ve{s_{t-24}} & \hdots & \ve{s_t} \end{m} \\
	\ve{t_t} = & \begin{m} s_{(t-24)1} & \hdots & s_{(t-24)12} \hdots s_{t1} & \hdots & s_{t12} \end{m}
\end{align*}

\begin{align*}
	\ve{u_t} = & \begin{m} f_{t1} & \hdots & f_{t12} \hdots a_{t1} & \hdots & a_{t12} & \hdots & \hat{s_{t1}} & \hdots & \hat{s_{t12}} \end{m}
\end{align*}

\end{document}


\documentclass{homework}
\course{Math 5522H}
\author{Jim Fowler}
\input{preamble}

\begin{document}
\maketitle

\begin{inspiration}
  \textit{Logic sometimes makes monsters.} For half a century we have
  seen a mass of bizarre functions which appear to be forced to
  resemble as little as possible honest functions which serve some
  purpose. More of continuity, or less of continuity, more
  derivatives, and so forth.
  \byline{Henri Poincar\'e} % can someone find the actual reference for this?!
\end{inspiration}

\section{Terminology}

\begin{problem}
  What is meant by uniform convergence on compact sets?
\end{problem}

\begin{problem}
  What is a biholomorphism $f : U \to V$?
\end{problem}

\begin{problem}
  Define \textbf{star-convex}.
\end{problem}

\section{Numericals}

\begin{problem}\label{max-modulus-numerical}
  Consider $f(z) = 4z - (2z - i)^2 - 2i - 4$.  Find $z$ in
  \[
    \{ x + iy \in \C : 0 \leq x \leq 1 \mbox{ and } 0 \leq y \leq 1 \}
  \]
  maximizing $\abs{f(z)}$.
\end{problem}

\section{Exploration}

\begin{problem}
  Sometimes things are \textit{much} better in the complex case than
  the real case.  Find a sequence of smooth functions
  $f_n : [-1,1] \to \R$ converging uniformly to $f(x) = \abs{x}$ which
  is not differentiable at zero.  Can the uniform limit of holomorphic
  functions fail to be holomorphic?
\end{problem}

\begin{problem}\label{hadamard-three-lines}Prove \textbf{Hadamard's
    three-lines theorem} which is about the strip
  \[
    S_{a,b} := \{ x+iy \in \C : a \leq x \leq b\}
  \]
  bounded by two horizontal lines, i.e., the line with real part $a$
  and the line with real part $b$.  Suppose $f : S_{a,b} \to \C$ is a
  bounded continuous function which is holomorphic on the interior of
  $S_{a,b}$.  Define $M : [a,b] \to \R$ by
  \[
    M(x) := \sup_{y \in \R} \abs{f(x+iy)},
  \]
  that is, $M(x)$ is the supremum of $\abs{f}$ on the (third!) line with real part $x$.

  Show that, if $t \in [0,1]$, then \[
    M\left( ta + (1-t)b \right) \leq M(a)^t M(b)^{1-t}.
  \]
\end{problem}

\begin{problem}\label{schwarz-lemma}Suppose $f : B_1(0) \to B_1(0)$ is holomorphic and $f(0) = 0$.  Show
  that $\abs{f(z)} \leq \abs{z}$ for all $z \in B_1(0)$.  How large
  could $\abs{f'(0)}$ be?  This is the \textbf{Schwarz lemma}.
\end{problem}

\begin{problem}\label{schwarz-lemma-2}Suppose again that $f : B_1(0) \to B_1(0)$ is holomorphic and $f(0) = 0$, and
  suppose further that $\abs{f(z)} = \abs{z}$ for some nonzero
  $z \in \C$.  Show that $f$ must be a rotation, i.e., there is some
  $\lambda \in \C$ with $\abs{\lambda} = 1$ and $f(z) = \lambda z$.
  (This is also the Schwarz lemma.)
\end{problem}

\begin{problem}\label{schwarz-lemma-3}Suppose that
  $f : B_1(0) \to B_1(0)$ is holomorphic and $f(0) = 0$ and $\abs{f'(0)} = 1$.  What
  can you deduce about $f$?
\end{problem}

\begin{problem}\label{schwarz-reflection-principle-2}Consider the
  closed upper half-plane
  \[
    H := \{ x + iy \in \C : x \in \R \mbox{ and } y \geq 0 \}.
  \]
  Suppose $f : H \to \C$ is continuous, holomorphic on the interior of
  $H$, and sends reals to reals, i.e., if $x \in \R$ then
  $f(x) \in \R$.  Use a trick like \ref{schwarz-reflection-principle}
  to describe an entire function agreeing with $f$ on its domain.
\end{problem}

\begin{problem}Consider the closed disk
  \[
    D := \{ z \in \C : \abs{z} \leq 1 \},
  \]
  and suppose $f : D \to D$ is continuous, holomorphic on the interior
  of $D$, sends boundary to boundary, i.e., if $z \in \partial D$ then
  $f(z) \in \partial D$, and misses an interior point, i.e., there is
  some $w$ in the interior of $D$ which is not in the image of $f$.

  Combine \ref{cayley-transform} and
  \ref{schwarz-reflection-principle-2} to produce an entire function.
  What can you deduce about $f$?
\end{problem}

\section{Prove or Disprove and Salvage if Possible}

\begin{problem}\label{cauchy-for-starlike}Suppose $U \subset \C$ is a
  star-convex open set, and $f : U \to \C$ is holomorphic, and
  $\gamma : [0,1] \to U$ is a smooth closed curve.  Then
  \[
    \int_\gamma f(z) \, dz = 0.
  \]
\end{problem}

\begin{problem}\label{moreras-theorem}If a function $f : B_r(0) \to \C$
  satisfies % missing continuous!
  \[
    \int_\gamma f(z) \, dz = 0
  \]
  for all piecewise smooth closed curves $\gamma$ in the disk
  $B_r(0)$, then $f$ is holomorphic.
\end{problem}

\begin{problem}\label{uniform-convergence-holomorphic}If the sequence of
  holomorphic functions $f_n : U \to \C$ converge pointwise to
  $f : U \to \C$, then $f$ is holomorphic.
\end{problem}

\begin{problem}\label{automorphisms-of-disk}Suppose $f, g : B_r(0) \to B_r(0)$ are holomorphic and
  $f \circ g = g \circ f$ are the identity on $B_r(0)$.  Then there is
  $w \in B_r(0)$ and $\theta \in [0,2\pi)$ with
  \[
    f(z) = e^{i\theta} \cdot \frac{w - z}{1 - \conj{w}z}.
  \]
  This should remind you of \ref{blaschke-factors}.  This result
  highlights the rigidity of biholomorphic functions as compared to,
  say, homeomorphisms.
\end{problem}

\end{document}