\documentclass[11pt]{article}

\usepackage{amsfonts}
\usepackage{amsmath}
\usepackage{framed}
\usepackage{xcolor}
\usepackage{enumitem}
\usepackage{booktabs}
\usepackage{ltablex}
\usepackage{ifthen}
\usepackage[linktoc=all,colorlinks=true,urlcolor=blue]{hyperref}
\hypersetup{
  colorlinks,
  linkcolor=blue
}

\newcommand{\real}{\mathbb{R}}
\newcommand{\trans}{{\rm T}}

\newenvironment{function}[4]		%A new environment for functions
{
\begin{shaded*}
\noindent
\verb~#1~		%The function's name.
\end{shaded*}
\vspace*{-5pt}
\ifthenelse{\equal{\detokenize{#2}}{\detokenize{}}}
{}                              % what to do if there is no description
{\noindent
Input:
\begin{description}[leftmargin=1.5cm,labelindent=0.5cm,itemsep=-4pt,topsep=0pt]
#2
\end{description}}
\ifthenelse{\equal{\detokenize{#3}}{\detokenize{}}}
{}                              % what to do if there is no description
{\noindent
Output:
\begin{description}[leftmargin=1.5cm,labelindent=0.5cm,itemsep=-4pt,topsep=0pt]		%Output list.
#3
\end{description}}
\ifthenelse{\equal{\detokenize{#4}}{\detokenize{}}}
{}                              % what to do if there is no description
{\vspace*{5pt} \noindent #4}    % otherwise give the description
\vspace*{10pt}
}{}

\setlength{\oddsidemargin}{0cm}			%Page settings.
\setlength{\evensidemargin}{3cm}
\setlength{\marginparsep}{0.75cm}
\setlength{\marginparwidth}{2.5cm}
\setlength{\marginparpush}{1.0cm}
\setlength{\textwidth}{16.5cm}
\setlength{\textheight}{22.86cm}
\setlength{\topmargin}{-1.27cm}

\definecolor{shadecolor}{rgb}{0.9,0.9,0.9}

\begin{document}

\begin{center}			%Title.
\Large
{\bf Modern Robotics:  Mechanics, Planning, and Control} \\
{\bf Code Library} \\
\normalsize
Version 1.0.1\\
\vspace*{0.2in}
Huan Weng and Kevin M. Lynch \\
July 6, 2018 \\
(beta version:  January 14, 2017)
\end{center}

\section*{Introduction}

This is the documentation for the code library accompanying \emph{Modern Robotics:  Mechanics, Planning, and Control}, by Kevin M. Lynch and Frank C. Park, Cambridge University Press, 2017, \url{http://modernrobotics.org}.  The code is written for MATLAB, Mathematica, and Python, and originates from students' solutions to programming assignments in courses using material from the book.  The current version of the code is largely the work of Huan Weng, based on contributions from Bill Hunt, Jarvis Schultz, Mikhail Todes, Matthew Collins, Mojtaba Mozaffar, Chang Liu, and Wentao Chen.

The code is commented and mostly self-explanatory in conjunction with the book.  An example use is provided with each function.  The primary purpose of the software is to be easy to read and educational, reinforcing the concepts in the book.  The code is optimized neither for efficiency nor robustness (it does not perform error-checking on its inputs).  This is to keep the code as simple and unintimidating as possible.  Users are encouraged to use and modify the code however they wish; the process of using and modifying the code certainly aids in understanding the concepts in the book.

Information on installing and using the library is available at the code website, \url{https://github.com/NxRLab/ModernRobotics}.  Your feedback on bugs or documentation errors are appreciated via the issue tracker at the same site.

This document provides an overview of the available functions using MATLAB syntax.  Functions are organized according to the relevant chapter in the book.  Basic functions, such as functions to calculate the magnitude of a vector, normalize a vector, test if a value is near zero, and perform matrix operations such as multiplication and inverses, are not documented here.

Notation that is used throughout this document is summarized below.

\begin{tabularx}{\linewidth}{llp{4.5in}}
\toprule
{\bf Math} & {\bf Computer} & \\
{\bf symbol} & {\bf variable} & {\bf Description} \\
\midrule
$R$ & {\tt R} & $3 \times 3$ rotation matrix in $SO(3)$. \\
$\omega$ & {\tt omg} & $3$-vector angular velocity. \\
$\hat{\omega}$ & {\tt omghat} & $3$-vector unit rotation axis or unit angular velocity. \\
$\theta$ & {\tt theta} & Angle of rotation about an axis or distance traveled along a screw axis. \\
$\hat{\omega} \theta$ & {\tt expc3} & $3$-vector of exponential coordinates for rotation. \\
$[\omega], [\hat{\omega} \theta] $ & {\tt so3mat} & $3 \times 3$ skew-symmetric $so(3)$ representation of $\omega$ or $\hat{\omega} \theta$. \\
$p$ & {\tt p} & $3$-vector for a position in space. \\
$T$ & {\tt T} & $4 \times 4$ transformation matrix in $SE(3)$ corresponding to $(R,p)$. \\
$[\operatorname{Ad}_T]$ & {\tt AdT} & $6 \times 6$ matrix adjoint representation of $T \in SE(3)$. \\
$v$ & {\tt v} & $3$-vector linear velocity. \\
$\mathcal{V}$ & {\tt V} & $6$-vector twist $(\omega, v)$. \\
$\mathcal{S}$ & {\tt S} & A normalized $6$-vector screw axis $(\omega,v)$, where (a) $\|\omega\| = 1$ or (b) $\|\omega\| = 0$ and $\|v\| = 1$. \\
$\mathcal{S}\theta$ & {\tt expc6} & $6$-vector of exponential coordinates for rigid-body motion. \\
$[\mathcal{V}], [\mathcal{S} \theta]$ & {\tt se3mat} & $4 \times 4$ $se(3)$ representation of $\mathcal{V}$ or $\mathcal{S} \theta$. \\
$M$ & {\tt M} & End-effector configuration in $SE(3)$ when manipulator is at its zero position. \\
$\mathcal{B}_i$ & {\tt Blist} & $\mathcal{B}_i$ is the screw axis of the $i$th joint expressed in the end-effector frame when the manipulator is at the zero position.  {\tt Blist} is a list of all the joint screw axes for the manipulator, $i = 1, \ldots, n$. \\
$\mathcal{S}_i$ & {\tt Slist} & $\mathcal{S}_i$ is the screw axis of the $i$th joint expressed in the space frame when the manipulator is at the zero position.  {\tt Slist} is a list of all the joint screw axes for the manipulator,  $i = 1, \ldots, n$. \\
$J_b, J_s$ & {\tt Jb}, {\tt Js} & The $6 \times n$ manipulator Jacobian for a robot with $n$ joints, expressed in the end-effector frame ($J_b$) or the space frame ($J_s$). \\
${\epsilon}_{\omega}$ & {\tt eomg} & A small positive tolerance on the end-effector orientation error when calculating numerical inverse kinematics. \\
${\epsilon}_{\nu}$ & {\tt ev} & A small positive tolerance on the end-effector linear position error when calculating numerical inverse kinematics. \\
$\theta_0$ & {\tt thetalist0} & A list of joint variables that serve as an initial guess for the inverse kinematics solution.\\
$\theta_i$ & {\tt thetalist} & $\theta_i$ is the joint variable for joint $i$, and {\tt thetalist} is $\theta = (\theta_1, \ldots, \theta_n)$.\\
& {\tt thetamat} & An $N \times n$ matrix where each row represents $\theta$ one timestep after the row preceding it in the matrix. \\
$\dot{\theta}_i$ & {\tt dthetalist} & $\dot{\theta}_i$ is the rate of change of joint variable $i$, and {\tt dthetalist} is $\dot{\theta} = (\dot{\theta}_1, \ldots, \dot{\theta}_n)$. \\
& {\tt dthetamat} & An $N \times n$ matrix where each row represents $\dot{\theta}$ one timestep after the row preceding it in the matrix. \\
$\ddot{\theta}_i$ & {\tt ddthetalist} & $\ddot{\theta}_i$ is the acceleration of joint $i$, and {\tt ddthetalist} is $\ddot{\theta} = (\ddot{\theta}_1, \ldots, \ddot{\theta}_n)$. \\
& {\tt dthetamat} & An $N \times n$ matrix where each row represents $\ddot{\theta}$ one timestep after the row preceding it in the matrix. \\
$\mathfrak{g}$ & {\tt g} & $3$-vector for gravitational acceleration. \\
$\tilde{\mathfrak{g}}$ & {\tt gtilde} & A possibly incorrect model for $\mathfrak{g}$ used by a controller. \\
$M_{i-1,i} $ & {\tt Mlist} & $M_{i-1,i} \in SE(3)$ is the configuration of manipulator link $i$ relative to link $i-1$ when the manipulator is at its zero position.  The link frames are defined at the link centers of mass.  {\tt Mlist} is a list of all $M_{i-1,i}$ for $i = 1, \ldots, n+1$.  The frame $\{n+1\}$ is the end-effector frame, and it is fixed relative to the frame $\{n\}$ of the last link.  It simply offers the opportunity to define an end-effector frame other than at the center of mass of the last link. \\
& {\tt Mtildelist} & A possibly incorrect model for {\tt Mlist} used by a controller. \\
$\mathcal{F}_{\text{tip}}$ & {\tt Ftip} & $6$-vector wrench applied by the manipulator end-effector, expressed in the end-effector frame $\{n+1\}$. \\
& {\tt Ftipmat} & An $N \times 6$ matrix where each row represents $\mathcal{F}_{\text{tip}}$ one timestep after the row preceding it in the matrix. \\
$\mathcal{G}_i$ & {\tt Glist} & $\mathcal{G}_i$ is the $6\times 6$ spatial inertia matrix for link $i$ of the manipulator, and {\tt Glist} is a list of all $\mathcal{G}_i$ for $i = 1, \ldots, n$. \\
& {\tt Gtildelist} & A possibly incorrect model for {\tt Glist} used by a controller. \\
$\tau_i$ & {\tt taulist} & $\tau_i$ is the generalized force applied at joint $i$, and {\tt taulist} is the list of all joint forces/torques $\tau = (\tau_1, \ldots, \tau_n)$. \\
& {\tt taumat} & An $N \times n$ matrix where each row represents $\tau$ one timesetep after the row preceding it in the matrix. \\
$[\operatorname{ad}_{\mathcal{V}}]$ & {\tt adV} & $6 \times 6$ matrix adjoint representation of $\mathcal{V} \in se(3)$, used to calculate the Lie bracket of two twists, $[\operatorname{ad}_{\mathcal{V}_1}] \mathcal{V}_2$. \\
$T_f$ & {\tt Tf} & The total time of a motion in seconds from rest to rest when calculating trajectories. \\
$\Delta t$ & {\tt dt} & A timestep (e.g., between consecutive rows in a matrix representing a trajectory or force history). \\
$t$ & {\tt t} & The current time. \\
$\theta_{\text{start}}$ & {\tt thetastart} & An $n$-vector of initial joint variables with which to start a trajectory.\\
$\theta_{\text{end}}$ & {\tt thetaend} & An $n$-vector of final joint variables with which to end a trajectory.\\
$X_{\text{start}}$ & {\tt Xstart} & An initial end-effector configuration $X_{\text{start}} \in SE(3)$ with which to start a trajectory.\\
$X_{\text{end}}$ & {\tt Xend} & A final end-effector configuration $X_{\text{end}} \in SE(3)$ with which to end a trajectory.\\
$e_{\text{int}}$ & {\tt eint} & An $n$-vector of the time-integral of joint errors.\\
$\theta_d$ & {\tt thetalistd} & An $n$-vector of reference joint variables $\theta_d$.\\
& {\tt thetamatd} & An $N \times n$ matrix where each row represents $\theta_d$ one timestep after the row preceding it in the matrix. \\
$\dot{\theta}_d$ & {\tt dthetalistd} & An $n$-vector of reference joint velocities $\dot{\theta}_d$.\\
& {\tt dthetamatd} & An $N \times n$ matrix where each row represents $\dot{\theta}_d$ one timestep after the row preceding it in the matrix. \\
$\ddot{\theta}_d$ & {\tt ddthetalistd} & An $n$-vector of reference joint accelerations $\ddot{\theta}_d$.\\
& {\tt dthetamatd} & An $N \times n$ matrix where each row represents $\ddot{\theta}_d$ one timestep after the row preceding it in the matrix. \\
$K_p$ & {\tt Kp} & A scalar feedback proportional gain.\\
$K_i$ & {\tt Ki} & A scalar feedback integral gain.\\
$K_d$ & {\tt Kd} & A scalar feedback derivative gain.\\
& {\tt intRes} & The number of integration steps during each timestep $\Delta t$.  The value must be a positive integer.  Larger values result in slower simulations but less accumulation of integration error.  \\
\bottomrule
\end{tabularx}

\section*{Chapter 3:  Rigid-Body Motions}		%Chapter 3

\begin{function}			%RotInv
{invR = RotInv(R)}
{\item \verb~R~: Rotation matrix.}
{\item \verb~invR~: The inverse of {\tt R}.}
{For efficiency, the inverse is calculated as the transpose rather than a matrix inverse.}
\end{function}

\begin{function}			%VecToso3
{so3mat = VecToso3(omg)}
{\item \verb~omg~: A $3$-vector.}
{\item \verb~so3mat~: The corresponding $3 \times 3$ skew-symmetric matrix in $so(3)$.}
{}
\end{function}

\begin{function}			%so3ToVec
{omg = so3ToVec(so3mat)}
{\item \verb~so3mat~: A $3 \times 3$ skew-symmetric matrix (an element of $so(3)$).}
{\item \verb~omg~: The corresponding $3$-vector.}
{}
\end{function}

\begin{function}			%AxisAng3
{[omghat,theta] = AxisAng3(expc3)}
{\item \verb~expc3~: A $3$-vector of exponential coordinates for rotation $\hat{\omega}\theta$.}
{
\item \verb~omghat~: The corresponding unit rotation axis $\hat{\omega}$.
\item \verb~theta~: The corresponding rotation angle $\theta$.
}
{}
\end{function}

\begin{function}			%MatrixExp3
{R = MatrixExp3(so3mat)}
{\item \verb~so3mat~: An $so(3)$ representation of exponential coordinates for rotation, $[\hat{\omega} \theta]$.}
{\item \verb~R~: The $R^\prime \in SO(3)$ that is achieved by rotating about $\hat{\omega}$ by $\theta$ from an initial orientation $R=I$.}
{}
\end{function}

\begin{function}			%MatrixLog3
{so3mat = MatrixLog3(R)}
{\item \verb~R~: Rotation matrix.}
{\item \verb~so3mat~: The corresponding $so(3)$ representation of exponential coordinates.}
{}
\end{function}

\begin{function}			%DistanceToSO3
{d = DistanceToSO3(mat)}
{\item \verb~mat~: A $3 \times 3$ matrix $M$.}
{\item \verb~d~: A measure of the distance from $M$ to $SO(3)$, the space of rotation matrices.  If ${\rm det}(M)>0$ (the determinant of $M$ should be $1$ if $M \in SO(3)$), this distance is calculated as $\|M^\trans M -I\|_F$, since $M^\trans M$ should be the identity matrix if $M\in SO(3)$.  The Frobenius norm $\| \cdot \|_F$ of a matrix is the square root of the sum of the squares of the absolute values of the elements of the matrix.  If the determinant is not positive, a large value is returned. }
{}
\end{function}

\begin{function}			%TestIfSO3
{judge = TestIfSO3(mat)}
{\item \verb~mat~: A $3 \times 3$ matrix $M$.}
{\item \verb~judge~:  $1$ (True) if $M$ is a rotation matrix (an element of $SO(3)$) and $0$ (False) otherwise.  This function calls \verb~DistanceToSO3(mat)~ and tests if the returned distance is smaller than a small value (which you should feel free to change to suit your purposes).}
{}
\end{function}

\begin{function}			%ProjectToSO3
{R = ProjectToSO3(mat)}
{\item \verb~mat~: A $3 \times 3$ matrix $M$.}
{\item \verb~R~:  The closest rotation matrix (element of $SO(3)$) to $M$.  This function is only appropriate for matrices $M$ that are ``close'' to $SO(3)$.  For example, $M$ could be the result of a long series of multiplications of rotation matrices, which has caused the result to drift slightly away from satisfying the conditions of $SO(3)$ (${\rm det}(M) = 1, M^\trans M = I$) due to roundoff errors.}
{}
\end{function}

\begin{function}			%RpToTrans
{T = RpToTrans(R,p)}
{
\item \verb~R~: Rotation matrix.
\item \verb~p~: A position $p \in \real^3$.
}
{\item \verb~T~: The corresponding homogeneous transformation matrix $T \in SE(3)$.}
{}
\end{function}

\begin{function}			%TransToRp
{[R,p] = TransToRp(T)}
{\item \verb~T~: Transformation matrix.}
{
\item \verb~R~: The corresponding rotation matrix.
\item \verb~p~: The corresponding position.
}
{}
\end{function}

\begin{function}			%TransInv
{invT = TransInv(T)}
{\item \verb~T~: Transformation matrix.}
{\item \verb~invT~: Inverse of \verb~T~.}
{Uses the structure of transformation matrices to avoid taking a matrix inverse, for efficiency.}
\end{function}

\begin{function}			%VecTose3
{se3mat = VecTose3(V)}
{\item \verb~V~: A $6$-vector (representing a twist, for example).}
{\item \verb~se3mat~: The corresponding $4 \times 4$ $se(3)$ matrix.}
{}
\end{function}

\begin{function}			%se3ToVec
{V = se3ToVec(se3mat)}
{\item \verb~se3mat~: A $4 \times 4$ $se(3)$ matrix.}
{\item \verb~V~: The corresponding $6$-vector.}
{}
\end{function}

\begin{function}			%Adjoint
{AdT = Adjoint(T)}
{\item \verb~T~: Transformation matrix.}
{\item \verb~AdT~: The corresponding $6 \times 6$ adjoint representation $[\operatorname{Ad}_T]$.}
{}
\end{function}

\begin{function}			%ScrewToAxis
{S = ScrewToAxis(q,s,h)}
{
\item \verb~q~: A point $q \in \real^3$ lying on the screw axis.
\item \verb~s~: A unit vector $\hat{s} \in \real^3$ in the direction of the screw axis.
\item \verb~h~: The pitch $h \in \real$ (linear velocity divided by angular velocity) of the screw axis.
}
{\item \verb~S~: The corresponding normalized screw axis ${\mathcal S} = (\omega,v)$.}
{}
\end{function}

\begin{function}			%AxisAng6
{[S,theta] = AxisAng6(expc6)}
{\item \verb~expc6~: A $6$-vector of exponential coordinates for rigid-body motion, $\mathcal{S}\theta$.}
{
\item \verb~S~: The corresponding normalized screw axis $\mathcal{S}$.
\item \verb~theta~: The distance traveled along/about $\mathcal{S}$.
}
{}
\end{function}

\begin{function}			%MatrixExp6
{T = MatrixExp6(se3mat)}
{\item \verb~se3mat~: An $se(3)$ representation of exponential coordinates for rigid-body motion, $[\mathcal{S}\theta]$.}
{\item \verb~T~: The $T^\prime \in SE(3)$ that is achieved by traveling along/about the screw axis $\mathcal{S}$ a distance $\theta$ from an initial configuration $T=I$.}
{}
\end{function}

\begin{function}			%MatrixLog6
{se3mat = MatrixLog6(T)}
{\item \verb~T~: Transformation matrix.}
{\item \verb~se3mat~: The corresponding $se(3)$ representation of exponential coordinates.}
{}
\end{function}

\begin{function}			%DistanceToSE3
{d = DistanceToSE3(mat)}
{\item \verb~mat~: A $4 \times 4$ matrix $M$.}
{\item \verb~d~: A measure of the distance from $M$ to $SE(3)$, the
  space of transformation matrices.  Let $R$ be the top-left $3 \times 3$ submatrix of $M$, i.e., the portion of $M$ expected to represent a rotation matrix.  If ${\rm det}(R)>0$ (the determinant of $R$ should be $1$ if $R \in SO(3)$), the distance is calculated as $\|M^\prime -I\|_F$, where the top-left $3 \times 3$ submatrix of $M^\prime$ is $R^\trans R$ (which should be the identity matrix if $R \in SO(3)$), the $1 \times 4$ bottom row of $M^\prime$ is the same as the bottom row of $M$, and the elements $M^\prime_{14}$, $M^\prime_{24}$, and $M^\prime_{34}$ are zero.  The Frobenius norm $\| \cdot \|_F$ of a matrix is the the square root of the sum of the squares of the absolute values of the elements of the matrix.  If the determinant of $R$ is not positive, a large value is returned. }
{}
\end{function}

\begin{function}			%TestIfSE3
{judge = TestIfSE3(mat)}
{\item \verb~mat~: A $4 \times 4$ matrix $M$.}
{\item \verb~judge~:  $1$ if $M$ is a transformation matrix (an element of $SE(3)$) and $0$ otherwise.  This function calls \verb~DistanceToSE3(mat)~ and tests if the returned distance is smaller than a small value (which you should feel free to change to suit your purposes).}
{}
\end{function}

\begin{function}			%ProjectToSE3
{T = ProjectToSE3(mat)}
{\item \verb~mat~: A $4 \times 4$ matrix $M$.}
{\item \verb~T~:  The closest transformation matrix (element of $SE(3)$) to $M$.  This function is only appropriate for matrices $M$ that are ``close'' to $SE(3)$.  For example, $M$ could be the result of a long series of multiplications of transformation matrices, which has caused the result to drift slightly away from satisfying the conditions of $SE(3)$ (top-left $3\times 3$ submatrix is in $SO(3)$ and the bottom row is $[0 \; 0\; 0 \; 1]$) due to roundoff errors.  The top-left $3 \times 3$ submatrix of $T$ is the $SO(3)$ matrix closest to the top-left $3 \times 3$ submatrix of $M$, the bottom row of $T$ is $[0 \; 0\; 0\; 1]$, and the elements $T_{14}$, $T_{24}$, and $T_{34}$ are equivalent to the elements  $M_{14}$, $M_{24}$, and $M_{34}$, respectively.  }
{}
\end{function}

\section*{Chapter 4:  Forward Kinematics}		%Chapter 4

\begin{function}			%FKinBody
{T = FKinBody(M,Blist,thetalist)}
{
\item \verb~M~: The home configuration of the end-effector.
\item \verb~Blist~: The joint screw axes in the end-effector frame when the manipulator is at the home position.
\item \verb~thetalist~: A list of joint coordinate values.
}
{\item \verb~T~: The $T \in SE(3)$ representing the end-effector frame when the joints are at the specified coordinates.}
{}
\end{function}

\begin{function}			%FKinSpace
{T = FKinSpace(M,Slist,thetalist)}
{
\item \verb~M~: The home configuration of the end-effector.
\item \verb~Slist~: The joint screw axes in the space frame when the manipulator is at the home position.
\item \verb~thetalist~: A list of joint coordinate values.
}
{\item \verb~T~: The $T \in SE(3)$ representing the end-effector frame when the joints are at the specified coordinates.}
{}
\end{function}

\section*{Chapter 5:  Velocity Kinematics and Statics}		%Chapter 5

\begin{function}			%JacobianBody
{Jb = JacobianBody(Blist,thetalist)}
{
\item \verb~Blist~: The joint screw axes in the end-effector frame when the manipulator is at the home position.
\item \verb~thetalist~: A list of joint coordinate values.
}
{\item \verb~Jb~: The corresponding body Jacobian $J_b(\theta) \in \real^{6 \times n}$.}
{}
\end{function}

\begin{function}			%JacobianSpace
{Js = JacobianSpace(Slist,thetalist)}
{
\item \verb~Slist~: The joint screw axes in the space frame when the manipulator is at the home position.
\item \verb~thetalist~: A list of joint coordinate values.
}
{\item \verb~Js~: The corresponding space Jacobian $J_s(\theta) \in \real^{6 \times n}$.}
{}
\end{function}

\section*{Chapter 6:  Inverse Kinematics}		%Chapter 6

\begin{function}			%IKinBody
{[thetalist,success] = IKinBody(Blist,M,T,thetalist0,eomg,ev)}
{
\item \verb~Blist~: The joint screw axes in the end-effector frame when the manipulator is at the home position.
\item \verb~M~: The home configuration of the end-effector.
\item \verb~T~: The desired end-effector configuration $T_{sd}$.
\item \verb~thetalist0~: An initial guess $\theta_0 \in \real^n$ that is ``close'' to satisfying $T(\theta_0) = T_{sd}$.
\item \verb~eomg~: A small positive tolerance on the end-effector orientation error.  The returned joint variables must give an end-effector orientation error less than ${\epsilon}_{\omega}$.
\item \verb~ev~: A small positive tolerance on the end-effector linear position error.  The returned joint variables must give an end-effector position error less than ${\epsilon}_{\nu}$.
}
{
\item \verb~thetalist~:  Joint variables that achieve \verb~T~ within the specified tolerances.
\item \verb~success~:  A logical value where \verb~TRUE~ means that the function found a solution and \verb~FALSE~ means that it ran through the set number of maximum iterations without finding a solution within the tolerances ${\epsilon}_{\omega}$ and ${\epsilon}_{\nu}$.
}
{The algorithm uses an iterative Newton-Raphson root-finding method starting from the initial guess {\tt thetalist0}.  The algorithm terminates when the stopping criteria are met or after a maximum number of iterations, whichever comes first. The maximum number of iterations has been hardcoded in as a variable in the function, which can be changed if desired. If the stopping criteria are not met, the function returns the last calculation of \verb~thetalist~ as well as a \verb~FALSE~ value for the success variable.}
\end{function}

\begin{function}			%IKinSpace
{[thetalist,success] = IKinSpace(Slist,M,T,thetalist0,eomg,ev)}
{}
{}
{Equivalent to \verb~IKinBody~, except the joint screw axes are specified in the space frame.}
\end{function}

\section*{Chapter 8:  Dynamics of Open Chains}		%Chapter 8

This chapter is concerned with calculating and simulating the dynamics of a serial-chain manipulator with dynamics of the form
\[
\tau = M(\theta)\ddot{\theta} + c(\theta,\dot{\theta}) + g(\theta) + J^\trans(\theta) \mathcal{F}_{\text{tip}}.
\]

\begin{function}			%ad
{adV = ad(V)}
{\item \verb~V~: A $6$-vector (e.g., a twist).}
{\item \verb~adV~: The corresponding $6 \times 6$ matrix $[\operatorname{ad}_{\mathcal{V}}]$.}
{Used to calculate the Lie bracket $[\operatorname{ad}_{\mathcal{V}_1}]\mathcal{V}_2$.}
\end{function}

\begin{function}			%InverseDynamics
{taulist = InverseDynamics(thetalist,dthetalist,ddthetalist,\\
\hspace*{1in} g,Ftip,Mlist,Glist,Slist)}
{
\item \verb~thetalist~:  $n$-vector of joint variables $\theta$.
\item \verb~dthetalist~: $n$-vector of joint velocities $\dot{\theta}$.
\item \verb~ddthetalist~:  $n$-vector of joint accelerations $\ddot{\theta}$.
\item \verb~g~:  Gravity vector $\mathfrak{g}$.
\item \verb~Ftip~:  Wrench $\mathcal{F}_{\text{tip}}$ applied by the end-effector expressed in frame $\{n+1\}$.
\item \verb~Mlist~:  List of link frames $\{i\}$ relative to $\{i-1\}$ at the home position.
\item \verb~Glist~:  Spatial inertia matrices $\mathcal{G}_i$ of the links.
\item \verb~Slist~:  Screw axes $\mathcal{S}_i$ of the joints in a space frame.
}
{\item \verb~taulist~: The $n$-vector $\tau$ of required joint forces/torques.}
{This function uses forward-backward Newton-Euler iterations.}
\end{function}

\begin{function}			%MassMatrix
{M = MassMatrix(thetalist,Mlist,Glist,Slist)}
{
\item \verb~thetalist~:  $n$-vector of joint variables $\theta$.
\item \verb~Mlist~:  List of link frames $\{i\}$ relative to $\{i-1\}$ at the home position.
\item \verb~Glist~:  Spatial inertia matrices $\mathcal{G}_i$ of the links.
\item \verb~Slist~:  Screw axes $\mathcal{S}_i$ of the joints in a space frame.
}
{\item \verb~M~: The numerical inertia matrix $M(\theta)$ of an $n$-joint serial chain at the given configuration $\theta$.}
{This function calls {\tt InverseDynamics} $n$ times, each time passing a $\ddot{\theta}$ vector with a single element equal to one and all other inputs set to zero.  Each call of {\tt InverseDynamics} generates a single column of the robot's mass matrix, and these columns are assembled to create the full mass matrix.}
\end{function}

\begin{function}			%VelQuadraticForces
{c = VelQuadraticForces(thetalist,dthetalist,Mlist,Glist,Slist)}
{
\item \verb~thetalist~:  $n$-vector of joint variables $\theta$.
\item \verb~dthetalist~: $n$-vector of joint velocities $\dot{\theta}$.
\item \verb~Mlist~:  List of link frames $\{i\}$ relative to $\{i-1\}$ at the home position.
\item \verb~Glist~:  Spatial inertia matrices $\mathcal{G}_i$ of the links.
\item \verb~Slist~:  Screw axes $\mathcal{S}_i$ of the joints in a space frame.
}
{\item \verb~c~: The vector $c(\theta,\dot{\theta})$ of Coriolis and centripetal terms for a given $\theta$ and $\dot{\theta}$.}
{This function calls {\tt InverseDynamics} with $\mathfrak{g}=0$, $\mathcal{F}_{\textrm{tip}} = 0$, and $\ddot{\theta} = 0$.}
\end{function}

\begin{function}			%GravityForces
{grav = GravityForces(thetalist,g,Mlist,Glist,Slist)}
{
\item \verb~thetalist~:  $n$-vector of joint variables $\theta$.
\item \verb~g~:  Gravity vector $\mathfrak{g}$.
\item \verb~Mlist~:  List of link frames $\{i\}$ relative to $\{i-1\}$ at the home position.
\item \verb~Glist~:  Spatial inertia matrices $\mathcal{G}_i$ of the links.
\item \verb~Slist~:  Screw axes $\mathcal{S}_i$ of the joints in a space frame.
}
{\item \verb~grav~: The joint forces/torques required to balance gravity at $\theta$.}
{This function calls \verb~InverseDynamics~ with $\dot{\theta} = \ddot{\theta} = 0$ and $\mathcal{F}_{\textrm{tip}} = 0$.}
\end{function}

\begin{function}			%EndEffectorForces
{JTFtip = EndEffectorForces(thetalist,Ftip,Mlist,Glist,Slist)}
{
\item \verb~thetalist~:  $n$-vector of joint variables $\theta$.
\item \verb~Ftip~:  Wrench $\mathcal{F}_{\text{tip}}$ applied by the end-effector expressed in frame $\{n+1\}$.
\item \verb~Mlist~:  List of link frames $\{i\}$ relative to $\{i-1\}$ at the home position.
\item \verb~Glist~:  Spatial inertia matrices $\mathcal{G}_i$ of the links.
\item \verb~Slist~:  Screw axes $\mathcal{S}_i$ of the joints in a space frame.
}
{\item \verb~JTFtip~: The joint forces and torques $J^\trans(\theta) \mathcal{F}_{\text{tip}}$ required to create the end-effector force $\mathcal{F}_{\text{tip}}$.}
{This function calls \verb~InverseDynamics~ with $\mathfrak{g}=0$ and $\dot{\theta} = \ddot{\theta} = 0$.}
\end{function}

\begin{function}			%ForwardDynamics
{ddthetalist = ForwardDynamics(thetalist,dthetalist,taulist,\\
\hspace*{1in} g,Ftip,Mlist,Glist,Slist)}
{
\item \verb~thetalist~:  $n$-vector of joint variables $\theta$.
\item \verb~dthetalist~: $n$-vector of joint velocities $\dot{\theta}$.
\item \verb~taulist~: The $n$-vector $\tau$ of required joint forces/torques.
\item \verb~g~:  Gravity vector $\mathfrak{g}$.
\item \verb~Ftip~:  Wrench $\mathcal{F}_{\text{tip}}$ applied by the end-effector expressed in frame $\{n+1\}$.
\item \verb~Mlist~:  List of link frames $\{i\}$ relative to $\{i-1\}$ at the home position.
\item \verb~Glist~:  Spatial inertia matrices $\mathcal{G}_i$ of the links.
\item \verb~Slist~:  Screw axes $\mathcal{S}_i$ of the joints in a space frame.
}
{\item \verb~ddthetalist~:  The resulting joint accelerations $\ddot{\theta}$.}
{This function computes $\ddot{\theta}$ by solving
\[
M(\theta) \ddot{\theta} = \tau - c(\theta,\dot{\theta}) - g(\theta) - J^\trans(\theta) \mathcal{F}_{\textrm{tip}}.
\]
}
\end{function}

\begin{function}			%EulerStep
{[thetalistNext,dthetalistNext] = EulerStep(thetalist,dthetalist,ddthetalist,dt)}
{
\item \verb~thetalist~:  $n$-vector of joint variables $\theta$.
\item \verb~dthetalist~: $n$-vector of joint velocities $\dot{\theta}$.
\item \verb~ddthetalist~:  $n$-vector of joint accelerations $\ddot{\theta}$.
\item \verb~dt~: The timestep $\Delta t$.
}
{
\item \verb~thetalistNext~: Vector of joint variables $\theta$ after $\Delta t$ from first-order Euler integration.
\item \verb~dthetalistNext~: Vector of joint velocities $\dot{\theta}$ after $\Delta t$ from first-order Euler integration.
}
{
}
\end{function}

\begin{function}			%InverseDynamicsTrajectory
{taumat = InverseDynamicsTrajectory(thetamat,dthetamat,ddthetamat, \\
\hspace*{1in} g,Ftipmat,Mlist,Glist,Slist)}
{
\item \verb~thetamat~: An $N \times n$ matrix of robot joint variables.  Each row is an $n$-vector of joint variables, and the $N$ rows correspond to $N$ time instants.  (The time instants can be thought of as starting at time $0$ and ending at time $T_f$, in increments $\Delta t = T_f/(N-1)$.)
\item \verb~dthetamat~: An $N \times n$ matrix of robot joint velocities.
\item \verb~ddthetamat~: An $N \times n$ matrix of robot joint accelerations.
\item \verb~g~:  Gravity vector $\mathfrak{g}$.
\item \verb~Ftipmat~: An $N \times 6$ matrix, where each row is a vector of the form $\mathcal{F}_{\textrm{tip}}(k \Delta t)$. (If there are no tip forces the user should input a zero and a zero matrix will be used).
\item \verb~Mlist~:  List of link frames $\{i\}$ relative to $\{i-1\}$ at the home position.
\item \verb~Glist~:  Spatial inertia matrices $\mathcal{G}_i$ of the links.
\item \verb~Slist~:  Screw axes $\mathcal{S}_i$ of the joints in a space frame.
}
{\item \verb~taumat~: The $N \times n$ matrix of joint forces/torques for the specified trajectory, where each of the $N$ rows is the vector of joint forces/torques at each time step.}
{This function uses \verb~InverseDynamics~ to calculate the joint forces/torques required to move the serial chain along the given trajectory.}
\end{function}

\begin{function}			%ForwardDynamicsTrajectory
{[thetamat,dthetamat] = ForwardDynamicsTrajectory(thetalist,dthetalist,taumat, \\
\hspace*{1in} g,Ftipmat,Mlist,Glist,Slist,dt,intRes)}
{
\item \verb~thetalist~: $n$-vector of initial joint variables.
\item \verb~dthetalist~: $n$-vector of initial joint velocities.
\item \verb~taumat~: An $N \times n$ matrix of joint forces/torques, where each row is the joint force/torque at any instant.  The time corresponding to row $k$ is $k\Delta t$, $k \in \{0, \ldots, N-1\}$, where $\Delta t$ is defined below.
\item \verb~g~:  Gravity vector $\mathfrak{g}$.
\item \verb~Ftipmat~: An $N \times 6$ matrix, where each row is a vector of the form $\mathcal{F}_{\textrm{tip}}(k \Delta t)$. (If there are no tip forces the user should input a zero and a zero matrix will be used).
\item \verb~Mlist~:  List of link frames $\{i\}$ relative to $\{i-1\}$ at the home position.
\item \verb~Glist~:  Spatial inertia matrices $\mathcal{G}_i$ of the links.
\item \verb~Slist~:  Screw axes $\mathcal{S}_i$ of the joints in a space frame.
\item \verb~dt~: The timestep $\Delta t$ between consecutive joint forces/torques.
\item \verb~intRes~: This input must be an integer greater than or equal to 1.  {\tt intRes} is the number of Euler integration steps during each timestep $\Delta t$.  Larger values result in slower simulations but less accumulation of integration error.  }
{
\item \verb~thetamat~: The $N \times n$ matrix of robot joint variables resulting from the specified joint forces/torques.
\item \verb~dthetamat~: The $N \times n$ matrix of robot joint velocities resulting from the specified joint forces/torques.
}
{This function simulates the motion of a serial chain given an open-loop history of joint forces/torques.  It calls a numerical integration procedure that uses \verb~ForwardDynamics~.}
\end{function}

\section*{Chapter 9: Trajectory Generation}			%Chapter 9

\begin{function}			%CubicTimeScaling
{s = CubicTimeScaling(Tf,t)}
{
\item \verb~Tf~: Total time of the motion $T_f$ in seconds from rest to rest.
\item \verb~t~: The current time $t$ satisfying $0 \leq t \leq T_f$.
}
{\item \verb~s~: The path parameter $s(t)$ corresponding to a third-order polynomial motion that begins (at $s(0)=0$) and ends (at $s(T_f)=1$) at zero velocity.}
{}
\end{function}

\begin{function}			%QuinticTimeScaling
{s = QuinticTimeScaling(Tf,t)}
{
\item \verb~Tf~: Total time of the motion $T_f$ in seconds from rest to rest.
\item \verb~t~: The current time $t$ satisfying $0 \leq t \leq T_f$.
}
{\item \verb~s~: The path parameter $s(t)$ corresponding to a fifth-order polynomial motion that begins (at $s(0)=0$) and ends (at $s(T_f)=1$) at zero velocity and zero acceleration.}
{}
\end{function}

\begin{function}			%JointTrajectory
{traj = JointTrajectory(thetastart,thetaend,Tf,N,method)}
{
\item \verb~thetastart~: The initial joint variables $\theta_{\text{start}} \in \real^n$.
\item \verb~thetaend~: The final joint variables $\theta_{\text{end}}$.
\item \verb~Tf~: Total time of the motion $T_f$ in seconds from rest to rest.
\item \verb~N~: The number of points $N \geq 2$ in the discrete representation of the trajectory.
\item \verb~method~: The time-scaling method, where 3 indicates cubic (third-order polynomial) time scaling and 5 indicates quintic (fifth-order polynomial) time scaling.
}
{\item \verb~traj~: A trajectory as an $N \times n$ matrix, where each row is an $n$-vector of joint variables at an instant in time.  The first row is $\theta_{\text{start}}$ and the $N$th row is $\theta_{\text{end}}$.  The elapsed time between each row is $T_f/(N-1)$. }
{The returned trajectory is a straight-line motion in joint space.}
\end{function}

\begin{function}			%ScrewTrajectory
{traj = ScrewTrajectory(Xstart,Xend,Tf,N,method)}
{
\item \verb~Xstart~: The initial end-effector configuration $X_{\text{start}} \in SE(3)$.
\item \verb~Xend~: The final end-effector configuration $X_{\text{end}}$.
\item \verb~Tf~: Total time of the motion $T_f$ in seconds from rest to rest.
\item \verb~N~: The number of points $N \geq 2$ in the discrete representation of the trajectory.
\item \verb~method~: The time-scaling method, where 3 indicates cubic (third-order polynomial) time scaling and 5 indicates quintic (fifth-order polynomial) time scaling.
}
{\item \verb~traj~: The discretized trajectory as a list of $N$ matrices in $SE(3)$ separated in time by $T_f/(N-1)$.  The first in the list is $X_{\text{start}}$ and the $N$th is $X_{\text{end}}$.}
{This function calculates a trajectory corresponding to a screw motion about a constant screw axis.}
\end{function}

\begin{function}			%CartesianTrajectory
{traj = CartesianTrajectory(Xstart,Xend,Tf,N,method)}
{
\item \verb~Xstart~: The initial end-effector configuration $X_{\text{start}} \in SE(3)$.
\item \verb~Xend~: The final end-effector configuration $X_{\text{end}}$.
\item \verb~Tf~: Total time of the motion $T_f$ in seconds from rest to rest.
\item \verb~N~: The number of points $N \geq 2$ in the discrete representation of the trajectory.
\item \verb~method~: The time-scaling method, where 3 indicates cubic (third-order polynomial) time scaling and 5 indicates quintic (fifth-order polynomial) time scaling.
}
{\item \verb~traj~: The discretized trajectory as a list of $N$ matrices in $SE(3)$ separated in time by $T_f/(N-1)$.  The first in the list is $X_{\text{start}}$ and the $N$th is $X_{\text{end}}$.}
{Similar to {\tt ScrewTrajectory}, except the origin of the end-effector frame follows a straight line, decoupled from the rotational motion.}
\end{function}

\section*{Chapter 11:  Robot Control}			%Chapter 11

The two functions in this chapter focus on the use of the computed torque controller
\[
\tau = \widehat{M}(\theta) \left(\ddot{\theta}_d + K_p \theta_e + K_i \int \theta_e(t) dt + K_d \dot{\theta}_e\right) + \widehat{h}(\theta,\dot{\theta})
\]
to control the motion of a serial chain in free space.  The term $\widehat{h}(\theta,\dot{\theta})$ comprises the model of centripetal, Coriolis, and gravitational forces, and the term $\widehat{M}(\theta)$ is the model of the robot's mass matrix.

\begin{function}			%ComputedTorque
{taulist = ComputedTorque(thetalist,dthetalist,eint,g,\\
\hspace*{1in} Mlist,Glist,Slist,thetalistd,dthetalistd,ddthetalistd,Kp,Ki,Kd)}
{
\item \verb~thetalist~: $n$-vector of initial joint variables.
\item \verb~dthetalist~: $n$-vector of initial joint velocities.
\item \verb~eint~: An $n$-vector of the time-integral of joint errors.
\item \verb~g~:  Gravity vector $\mathfrak{g}$.
\item \verb~Mlist~:  List of link frames $\{i\}$ relative to $\{i-1\}$ at the home position.
\item \verb~Glist~:  Spatial inertia matrices $\mathcal{G}_i$ of the links.
\item \verb~Slist~:  Screw axes $\mathcal{S}_i$ of the joints in a space frame.
\item \verb~thetalistd~: $n$-vector of reference joint variables $\theta_d$.
\item \verb~dthetalistd~: $n$-vector of reference joint velocities $\dot{\theta}_d$.
\item \verb~ddthetalistd~: $n$-vector of reference joint accelerations $\ddot{\theta}_d$.
\item \verb~Kp~: The feedback proportional gain (identical for each joint).
\item \verb~Ki~: The feedback integral gain (identical for each joint).
\item \verb~Kd~: The feedback derivative gain (identical for each joint).
}
{\item \verb~taulist~: The vector of joint forces/torques computed by the computed torque controller at the current instant.}
{}
\end{function}

\begin{function}			%SimulateControl
{[taumat,thetamat] = SimulateControl(thetalist,dthetalist,g,Ftipmat,Mlist,Glist, \\
\hspace*{1in} Slist,thetamatd,dthetamatd,ddthetamatd,gtilde,Mtildelist, \\
\hspace*{1in} Gtildelist,Kp,Ki,Kd,dt,intRes)}
{
\item \verb~thetalist~: $n$-vector of initial joint variables.
\item \verb~dthetalist~: $n$-vector of initial joint velocities.
\item \verb~g~:  Actual gravity vector $\mathfrak{g}$.
\item \verb~Ftipmat~: An $N \times 6$ matrix, where each row is a vector of the form $\mathcal{F}_{\textrm{tip}}(k \Delta t)$. (If there are no tip forces the user should input a zero and a zero matrix will be used).
\item \verb~Mlist~:  Actual list of link frames $\{i\}$ relative to $\{i-1\}$ at the home position.
\item \verb~Glist~:  Actual spatial inertia matrices $\mathcal{G}_i$ of the links.
\item \verb~Slist~:  Screw axes $\mathcal{S}_i$ of the joints in a space frame.
\item \verb~thetamatd~: An $N \times n$ matrix of desired joint variables $\theta_d$ from the reference trajectory.  The first row is the initial desired joint configuration, and the $N$th row is the final desired joint configuration.  The time between each row is {\tt dt}, below.
\item \verb~dthetamatd~: An $N \times n$ matrix of desired joint velocities $\dot{\theta}_d$.
\item \verb~ddthetamatd~: An $N \times n$ matrix of desired joint accelerations $\ddot{\theta}_d$.
\item \verb~gtilde~: The (possibly incorrect) model of the gravity vector.
\item \verb~Mtildelist~: The (possibly incorrect) model of the link frame locations.
\item \verb~Gtildelist~: The (possibly incorrect) model of the link spatial inertias.
\item \verb~Kp~: The feedback proportional gain (identical for each joint).
\item \verb~Ki~: The feedback integral gain (identical for each joint).
\item \verb~Kd~: The feedback derivative gain (identical for each joint).
\item \verb~dt~: The timestep $\Delta t$ between points on the reference trajectory.
\item \verb~intRes~: This input must be an integer greater than or equal to 1.  {\tt intRes} is the number of Euler integration steps during each timestep $\Delta t$.  Larger values result in slower simulations but less accumulation of integration error.
}
{
\item \verb~taumat~: An $N \times n$ matrix of the controller's commanded joint forces/torques, where each row of $n$ forces/torques corresponds to a single time instant.
\item \verb~thetamat~: An $N \times n$ matrix of actual joint variables, to be compared to \verb~thetamatd~.
\item Plot: Plot of actual and desired joint variables.
}
{This function uses {\tt ComputedTorque}, {\tt ForwardDynamics}, and numerical integration to simulate the performance of a computed torque control law operating on a serial chain.  Disturbances come in the form of initial position and velocity errors; incorrect models of gravity, the locations of the link center of mass frames, and the link spatial inertias; and errors introduced by the numerical integration.}
\end{function}

\end{document}


\subsection{Grothendieck universes}\label{subsec:grothendieck_universes}

Instead of having one single universe, we can have multiple universes where each is contained in another one. The upside of this is that we can do category theory formally within set theory --- see the discussions in \fullref{def:category_size}. The downside of this is that, unlike models of \hyperref[def:zfc]{\logic{ZFC}}, models of \hyperref[def:axiom_of_universes]{\logic{ZFC+U}} (\logic{ZFC} with the axiom that every set is contained in some \hyperref[def:grothendieck_universe]{Grothendieck universe}) are much less studied. In particular, this axiom requires the existence of an unbounded hierarchy of \hyperref[rem:strongly_inaccessible_cardinal]{regular strong limit cardinal}, unlike \( \logic{ZFC} \) for which only one such cardinal is sufficient.

\begin{definition}\label{def:grothendieck_universe}\mcite{nLab:grothendieck_universe}
  We say that the set \( \mscrU \) is a \term{Grothendieck universe} if it satisfied the following conditions:
  \begin{thmenum}
    \thmitem[def:grothendieck_universe/nonempty]{GU1} It is \hyperref[def:empty_set]{nonempty}.

    \thmitem[def:grothendieck_universe/transitive]{GU2} It is a \hyperref[def:transitive_set]{transitive set}.

    \thmitem[def:grothendieck_universe/power_set]{GU3} For any \( A \in \mscrU \), the \hyperref[def:basic_set_operations/power_set]{power set} \( \pow(A) \) also belongs to \( \mscrU \).

    \thmitem[def:grothendieck_universe/union]{GU4} For any member \( A \in \mscrU \) and any \( A \)-indexed family \( \set{ B_a }_{a \in A} \subseteq \mscrU \), the union
    \begin{equation*}
      \bigcup\set{ B_a \given a \in A }
    \end{equation*}
    belongs to \( \mscrU \). This is a restriction from unions over completely arbitrary families of sets to those families that can be indexed by members of \( A \).
  \end{thmenum}

  We formalize the entire concept via the following monstrous formula:
  \small
  \begin{equation*}\taglabel[\op{IsUniverse}]{eq:def:grothendieck_universe/predicate}
    \begin{aligned}
      \ref{eq:def:grothendieck_universe/predicate}[\upsilon] \coloneqq
        &\neg \ref{eq:def:empty_set/predicate}[\upsilon]
        \wedge
        \qforall {\tau \in \upsilon}
        \vast(
          \ref{eq:def:subset/predicate}[\tau, \upsilon]
          \wedge
          \parens[\Big]
            {
              \qexists {\xi \in \upsilon}
              \ref{eq:def:basic_set_operations/power_set/predicate}[\xi, \tau]
            }
          \wedge \\ &\wedge
          \qforall \xi
          \qforall \eta
          \parens[\Bigg]
          {
            \parens[\Big]
              {
                \underbrace
                  {
                    \ref{eq:def:function/predicate}[\xi, \tau, \upsilon]
                  }_{\mathclap{\xi \T*{is a function} \tau \to \upsilon}}
                \wedge
                \underbrace
                  {
                    \ref{eq:def:grothendieck_universe/predicate_isimage}[\eta, \xi]
                  }_{ \mathclap{\eta \T*{is the image of} \xi} }
              }
            \rightarrow
            \underbrace
              {
                \qexists {\zeta \in \upsilon} \ref{eq:def:basic_set_operations/union/predicate}[\zeta, \xi]
              }_{ \bigcup \img(\xi) \in \upsilon}
        }
      \vast),
    \end{aligned}
  \end{equation*}
  \normalsize
  where
  \begin{equation*}\taglabel[\op{IsImage}]{eq:def:grothendieck_universe/predicate_isimage}
    \ref{eq:def:grothendieck_universe/predicate_isimage}[\rho, \tau]
    \coloneqq
    \qforall \xi
    \parens[\Big]
    {
      \xi \in \rho
      \leftrightarrow
      \underbrace
      {
        \qexists {\eta \in \tau}
        \qexists \zeta
        \ref{eq:def:cartesian_product/kuratowski_pair_predicate}[\eta, \zeta, \xi]
      }_{\tau(\zeta) = \xi \T*{for some} \xi \in \dom(\tau) }
    }.
  \end{equation*}
\end{definition}

\begin{lemma}\label{thm:grothendieck_universe_contains_finite_sets}
  Every Grothendieck universe is a superset of universe of hereditary finite sets \hyperref[def:universe_of_hereditary_finite_sets]{\( V_\omega \)}.
\end{lemma}
\begin{proof}
  Let \( \mscrU \) be a Grothendieck universe.

  \ref{def:grothendieck_universe/nonempty} ensures that it is nonempty. Then there exists some set \( A \in \mscrU \). By \ref{def:grothendieck_universe/power_set}, \( \pow(A) \in \mscrU \). By \ref{def:grothendieck_universe/transitive}, \( \varnothing \in \pow(A) \in \mscrU \) implies \( \varnothing \in \mscrU \).

  Finally, from \ref{def:grothendieck_universe/power_set} by \fullref{thm:bounded_transfinite_induction} if follows that \( V_{n+1} = \pow(V_n) \) is a member of \( \mscrU \) for every \( n \in \omega \).

  Therefore, \( V_\omega = \bigcup \set{ V_n \given n \in \omega } \) is a subset of \( \mscrU \).
\end{proof}

\begin{definition}\label{def:axiom_of_universes}\mcite{nLab:grothendieck_universe}
  The \term{axiom of universes} states that any set is contained in a \hyperref[def:grothendieck_universe]{Grothendieck universe}. Symbolically,
  \begin{equation}\label{eq:def:axiom_of_universes}
    \begin{aligned}
      \qforall \tau \qexists \upsilon \parens[\Big]{ \ref{eq:def:grothendieck_universe/predicate}[\upsilon] \wedge \tau \in \upsilon }.
    \end{aligned}
  \end{equation}

  We usually add this theorem to \hyperref[def:zfc]{ZFC} and call the resulting \hyperref[def:first_order_theory]{logical theory} \logic{ZFC+U}.
\end{definition}

\begin{example}\label{ex:def:axiom_of_universes}
  From \fullref{thm:grothendieck_universe_iff_regular_strong_limit} it follows that the universe of hereditary finite sets \hyperref[def:universe_of_hereditary_finite_sets]{\( V_\omega \)} is a Grothendieck universe.

  The existence of other universes cannot be proven in \logic{ZFC}. For this reason, we use the \hyperref[def:axiom_of_universes]{axiom of universes}.
\end{example}

\begin{proposition}\label{thm:smallest_grothendieck_universe_existence}
  Suppose that we are working in \logic{ZFC+U}. Then for any set \( A \), there exists a smallest Grothendieck universe containing \( A \).

  More generally, fix a set \( A \). Then there exists a smallest Grothendieck universe containing \( A \).
\end{proposition}
\begin{proof}
  If no set \( A \) is given, we simply take \( A = \varnothing \) since it must belong to every universe by definition.

  We use a trick analogous to \fullref{thm:smallest_inductive_set_existence}.

  The \hyperref[def:axiom_of_universes]{axiom of universes} states that there exists at least one universe \( \mscrU \) that contains \( A \). Define
  \begin{equation*}
    \widehat \mscrU \coloneqq \set{ x \in \mscrU \given x \T{belongs to every Grothendieck universe} }.
  \end{equation*}

  Now that we have defined \( \widehat \mscrU \), it remains to verify that it is itself a universe. To show \ref{def:grothendieck_universe/nonempty}, note that \( V_\omega \in \mscrU \) by \fullref{thm:grothendieck_universe_contains_finite_sets} and hence, by \ref{def:grothendieck_universe/transitive}, \( \omega \in \mscrU \).

  The rest of the verification is trivial.
\end{proof}

\begin{definition}\label{def:large_and_small_sets}\mcite{nLab:grothendieck_universe}
  Suppose \( \mscrV = (V, I) \) is a \hyperref[def:first_order_semantics/satisfiability]{model} of \logic{ZFC+U}. Let \( \mscrU \) be a fixed Grothendieck universe.

  We say that a set \( A \) is \( \mscrU \)-\term{small} if \( A \in \mscrU \) and \( \mscrU \)-\term{moderate} if \( A \subseteq \mscrU \). This situation resembles the difference between sets and proper classes described in \fullref{def:set_builder_notation}.

  A set that is not \( \mscrU \)-\term{small} is called \( \mscrU \)-\term{large}. Note that any strict superset of \( \mscrU \) is \( \mscrU \)-large, but not \( \mscrU \)-moderate.

  Without further context (i.e. in \enquote{ordinary mathematics}), we assume that \( \mscrU \) refers to the \hyperref[thm:smallest_grothendieck_universe_existence]{smallest Grothendieck universe} that contains all sets of interest and instead of the terms \( \mscrU \)-large and \( \mscrU \)-small, we simply use the terms \term{large} and \term{small}.

  In category theory, however, if there is nothing to guarantee the existence of a larger Grothendieck universe, we cannot construct the \hyperref[def:functor_category]{functor category} of \( \mscrU \)-large categories, as discussed in \fullref{rem:functor_category_size}. This is the main motivation for the \hyperref[def:axiom_of_universes]{axiom of universes}.
\end{definition}

\begin{example}\label{ex:large_and_small_sets}
  \hfill
  \begin{itemize}
    \item A set is finite if and only if it is \( V_\omega \)-small. A set is \( V_\omega \)-moderate if and only if it is an infinite family of finite sets.

    For finite mathematics such as most of combinatorics, we rarely need to work outside of \( V_\omega \).

    \item If \( \kappa < \mu \) are regular strong limit cardinals, the stage \( V_\kappa \) of von Neumann's hierarchy is \( V_\mu \)-small by \fullref{thm:def:cumulative_hierarchy/membership}.
  \end{itemize}
\end{example}

\begin{theorem}\label{thm:grothendieck_universe_iff_regular_strong_limit}\mcite{Kruse1966}
  The stage \( V_\kappa \) of the \hyperref[def:cumulative_hierarchy]{von Neumann's cumulative hierarchy} is a \hyperref[def:grothendieck_universe]{Grothendieck universe} for every \hyperref[rem:strongly_inaccessible_cardinal]{regular strong limit cardinal} \( \kappa \).

  Conversely, for every Grothendieck universe \( \mscrU \), there exists a regular strong limit cardinal \( \kappa \) such that \( \mscrU = V_\kappa \).
\end{theorem}
\begin{proof}
  \SufficiencySubProof Let \( \kappa \) be a regular strong limit cardinal.

  \SubProofOf*{def:grothendieck_universe/nonempty} Since \( \kappa \) is infinite and thus \( 0 < \kappa \), from \fullref{thm:def:cumulative_hierarchy/membership} it follows that \( V_0 \in V_\kappa \).

  \SubProofOf*{def:grothendieck_universe/transitive} The set \( V_\kappa \) is transitive as shown in \fullref{thm:def:cumulative_hierarchy/transitive}.

  \SubProofOf*{def:grothendieck_universe/power_set} Since \( \kappa \) is a limit ordinal, \( V_\kappa \) satisfies the axiom of power sets as shown in \fullref{thm:cumulative_hierarchy_model_of_z} and thus if \( A \in V_\kappa \), then \( \pow(A) \in V_\kappa \).

  \SubProofOf*{def:grothendieck_universe/union} Fix some member \( A \in V_\kappa \) and some \( A \)-indexed family \( \set{ B_a }_{a \in A} \subseteq V_\kappa \). From \fullref{thm:strong_regular_cardinal_stage_cardinality} it follows that \( \card(A) < \kappa \) and \( \card(B_a) < \kappa \) for every \( a \in A \). Thus,
  \begin{equation*}
    \card(\set{ B_a \given a \in A }) \leq \card(A) < \kappa
  \end{equation*}
  and from \fullref{thm:regular_cardinal_stage_inverse_transitivity} it follows that
  \begin{equation*}
    \set{ B_a \given a \in A } \in V_\kappa.
  \end{equation*}

  The union
  \begin{equation*}
    \bigcap \set{ B_a \given a \in A }
  \end{equation*}
  is then a member of a lower stage, hence it also belongs to \( V_\kappa \).

  \NecessitySubProof Let \( \mscrU \) be a Grothendieck universe and let \( \alpha \) be the smallest \hi{ordinal} not in \( \mscrU \). We will first show that \( \mscrU = V_\alpha \) and gradually prove that \( \alpha \) is actually an inaccessible cardinal.

  \SubProof*{Proof that \( V_\beta \in \mscrU \) for ordinals \( \beta \in \mscrU \)} We will use \fullref{thm:bounded_transfinite_induction} on \( \beta < \alpha \).
  \begin{itemize}
    \item From \fullref{thm:grothendieck_universe_contains_finite_sets} it follows that \( \varnothing \in \mscrU \).

    \item If \( \beta < \alpha \) and \( V_\beta \in \mscrU \), then by \ref{def:grothendieck_universe/power_set} we have \( V_{\beta + 1} = \pow(V_\beta) \in \mscrU \).

    We will come back to this step a bit later, but for now note that \( V_{\beta + 1} \in \mscrU \) regardless of whether \( \beta + 1 \in \mscrU \).

    \item If \( \lambda < \alpha \) is a limit ordinal and \( V_\beta \in \mscrU \) for every \( \beta < \lambda \), we have
    \begin{equation*}
      V_\lambda
      \reloset {\eqref{eq:def:cumulative_hierarchy}} =
      \bigcup\set{ V_\beta \given \beta < \lambda },
    \end{equation*}
    which is a \( \lambda \)-indexed union of members of \( \mscrU \). Since \( \lambda \in \mscrU \), by \ref{def:grothendieck_universe/power_set} we have \( V_\lambda \in \mscrU \).
  \end{itemize}

  \SubProof*{Proof that \( \alpha \) is a limit ordinal} In the successor case we noted that \( V_{\beta + 1} \in \mscrU \) for every \( \beta < \alpha \) regardless of whether \( \beta + 1 < \alpha \). Since \( \rank(\beta + 1) = \beta + 1 \), it follows that \( \beta + 1 \in V_{\beta + 2} \in \mscrU \) and thus by \ref{def:grothendieck_universe/transitive}, \( \beta + 1 \in \mscrU \). Therefore, \( \alpha \) cannot be a successor ordinal --- if \( \alpha = \beta + 1 \), then \( \beta \in \mscrU \) by definition of \( \alpha \) and thus \( \beta + 1 = \alpha \in \mscrU \), which is a contradiction.

  Since \( \alpha > 0 \), it remains for \( \alpha \) to be a limit ordinal.

  \SubProof*{Proof that \( V_\alpha \subseteq \mscrU \)} By \ref{def:grothendieck_universe/transitive}, \( V_\beta \subseteq \mscrU \) for every \( \beta < \alpha \). We can conclude that
  \begin{equation*}
    V_\alpha
    \reloset {\eqref{eq:def:cumulative_hierarchy}} =
    \bigcup\set{ V_\beta \given \beta < \alpha }
    \subseteq
    U.
  \end{equation*}

  In order to show that equality holds, we must first prove that \( \alpha \) is a strongly inaccessible cardinal. But this requires some auxiliary results.

  \SubProof*{Proof that \( \set{ B } \in \mscrU \) for every \( B \in \mscrU \)} By \ref{def:grothendieck_universe/power_set} we have that \( \pow(\pow(B)) \in \mscrU \). But \( \set{ B } \subseteq \pow(B) \) and hence \( \set{ B } \in \pow(\pow(B)) \). By \ref{def:grothendieck_universe/transitive}, \( \set{ B } \in \mscrU \).

  \SubProof*{Proof that \( \kappa = \alpha \) is a cardinal} Suppose that \( \alpha \) is not a cardinal. Indeed, suppose that there exists some \( \beta < \alpha \) such that there exists a bijective function \( f: \beta \to \alpha \). Then
  \begin{equation*}
    \alpha = \bigcup\set{ \set{ f(\gamma) } \given \gamma < \beta }
  \end{equation*}
  is a \( \beta \)-indexed union of members of \( \alpha \) and hence \( \alpha \in \alpha \). But this contradicts \fullref{thm:simple_foundation_theorems/member_of_itself}.

  Therefore, \( \alpha \) is a cardinal. We will henceforth denote it by \( \kappa \) to highlight that it is a cardinal.

  \SubProof*{Proof that \( \card(B) \in \mscrU \) for every \( B \in \mscrU \)} Let \( B \in \mscrU \) and let \( f: B \to \card(B) \) be a bijective function. Then
  \begin{equation*}
    \card(B)
    =
    f[B]
    =
    \set{ f(x) \given x \in B }
    =
    \bigcup\set[\Big]{ \set{ f(x) } \given x \in B }
  \end{equation*}
   is a \( B \)-indexed union of members of \( \mscrU \) and, by \ref{def:grothendieck_universe/union}, \( \card(B) \in \mscrU \).

  \SubProof*{Proof that \( \kappa \) is a strong limit} For every \( \beta < \kappa \) by \ref{def:grothendieck_universe/power_set} we have \( \pow(\beta) \in \mscrU \). We have already shown that \( \card(\pow(\beta)) \in \mscrU \) and, by \fullref{thm:cardinal_exponentiation_power_set}, we have
  \begin{equation*}
    \card(\pow(\beta)) = 2^{\card(\beta)} = 2^\beta.
  \end{equation*}

  Hence, \( 2^\beta < \kappa \) and \( \kappa \) is a strong limit.

  \SubProof*{Proof that \( \kappa \) is regular} Let \( C \subseteq \kappa \) be an unbounded set. We will show that \( \card(C) = \kappa \).

  Suppose that \( \card(C) < \kappa \). Then \( \card(C) \in \mscrU \) since \( \kappa = \alpha \) is the smallest ordinal not contained in \( \mscrU \). Let \( f: \card(C) \to C \) be a bijective function. Then
  \begin{equation*}
    C = \bigcup\set[\Big]{ \set{ f(\gamma) } \given \gamma < \card(C) }
  \end{equation*}
  and by \ref{def:grothendieck_universe/union}, \( C \in \mscrU \).

  Since \( C \) is unbounded, we have \( \sup C \geq \kappa \). But from \fullref{thm:union_of_set_of_ordinals} is follows that \( \bigcup C = \sup C \) and by \ref{def:grothendieck_universe/union}, \( \bigcup C \in \mscrU \), which implies that \( \sup C < \kappa \).

  The obtained contradiction shows that \( \card(C) = \kappa \). Since \( C \) was an arbitrary unbounded set, it follows that \( \kappa \) satisfies \fullref{def:regular_cardinal/unbounded_subsets} and is thus regular.

  \SubProof*{Proof that \( V_\kappa = U \)} Finally, now that we know that \( \kappa \) is a strongly inaccessible cardinal, we can show that equality holds in \( V_\kappa \subseteq \mscrU \).

  Aiming at a contradiction, suppose that \( U \setminus V_\kappa \) is nonempty. By the \hyperref[def:zfc/foundation]{axiom of foundation}, there exists a set \( C \in \mscrU \setminus V_\kappa \) such that
  \begin{equation*}
    C \cap (U \setminus V_\kappa) = \varnothing,
  \end{equation*}
  thus \( C \subseteq V_\kappa \). From \fullref{thm:strong_regular_cardinal_stage_cardinality} if follows that \( \card(C) < \kappa \) and from \fullref{thm:regular_cardinal_stage_inverse_transitivity} it follows that \( C \in V_\kappa \), which contradicts our choice of \( C \) as a member of \( U \setminus V_\kappa \).

  Therefore, \( V_\kappa = U \).
\end{proof}

\begin{corollary}\label{thm:grothendieck_universe_is_model_of_zfc}
  Every uncountable Grothendieck universe is a standard model of \hyperref[def:zfc]{\logic{ZFC}}.
\end{corollary}
\begin{proof}
  Follows from \fullref{thm:grothendieck_universe_iff_regular_strong_limit} and \fullref{thm:cumulative_hierarchy_model_of_zfc}.
\end{proof}


\documentclass{report}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{listings}
\begin{document}
\section{LDA}
\subsection{Abstract}
In this chapter, we will learn another algorithm of the binary-classification-hard-output: LDA (linear discriminant analysis). Actually, it's also used to reduce the dimension.\\\\
We'll select a direction and project the high-dimensional samples to this direction to divide them into two classes.
\subsection{Idea}
The core idea of $LDA$ is to make the projected data satisfy two conditions:
\begin{itemize}
	\item the distance between samples within the same class is close
	\item the distance between different classes is large.
\end{itemize}
\subsection{Algorithm}
Firstly, to reduce the dimension, we have to find out how to calculate the projection length.\\
we assume a sample $x$ and project it to the direction $w$.\\
As we know: $w\cdot x=||w||\ ||x||\ \cos{\theta}$\\\\
Here we assume $||w||=1$ to detemine the unique $w$ to prevent countless solutions caused by scaling.\\
so $w x=||x||\cos{\theta}$\\
And $||x||\cos{\theta}$ is exactly the definition of projection.\\
Therefore, the projection length of the sample on the vector $w$ is $wx$.\\\\
Thus the projection is $z=w^T x$
We assume the number of samples belonging to the two classes is $N1$,$N2$.\\\\
Below, as to the first condition: the distance of sample within the same class is close, we use the variance matrix to represent the overall distribution of each class.\\
Here we use the definitioin of covariance matrix and the covariance matrix of origin data $x$ is denoted as $S$.\\
\begin{equation}
\begin{aligned}C_1: Var_z[C_1]&=\frac{1}{N_1}\sum_{i=1}^{N_1} (z_i-\bar{z_{c1}})(z_i-\bar{z_{c1}})^T\\
&=\frac{1}{N_1}\sum_{i=1}^{N_1}(w^T x_i-\frac{1}{N_1}\sum_{j=1}^{N_1}w^T x_j)(w^T x_i-\frac{1}{N_1}\sum_{j=1}^{N_1}w^T x_j)^T \\&=w^T \frac{1}{N_1}\sum_{i=1}^{N_1}(x_i-\frac{1}{N_1}\sum_{j=1}^{N_1} x_j)(x_i-\frac{1}{N_1}\sum_{j=1}^{N_1} x_j)^T w\\&=w^{T} \frac{1}{N_{1}} \sum_{i=1}^{N_{1}}\left(x_{i}-\bar{x_{c 1}}\right)\left(x_{i}-\bar{x_{c 1}}\right)^{T} w\\&=w^T S_1 w\\C_2: Var_z[C_2]&=\frac{1}{N_2}\sum_{i=1}^{N_2} (z_i-\bar{z_{c2}})(z_i-\bar{z_{c2}})^T\\&=w^T S_2 w
\end{aligned}
\end{equation}
Therefore the distance between classes can be denoted by:
$$
Var_z[C_1]+Var_z[C_2]=w^T(S_1+S_2)w
$$
As to the second condition: the distance between different classes is large\\
The distance between classes can be denoted by the difference between the mean projection length of two classes.
\begin{equation}
\begin{aligned}
(z_{c1}-z_{c2})^2&=(\frac{1}{N_1}\sum_{i=1}^{N_1}w^T x_i - \frac{1}{N_2}\sum_{i=1}^{N_2}w^T x_i)^2\\
&=(w^T(\frac{1}{N_1}\sum_{i=1}^{N_1} x_i - \frac{1}{N_2}\sum_{i=1}^{N_2} x_i))^2\\
&=(w^T(\bar{x_{c1}}-\bar{x_{c2}}))^2\\
&=w^T(\bar{x_{c1}}-\bar{x_{c2}})(\bar{x_{c1}}-\bar{x_{c2}})^T w
\end{aligned}
\end{equation}
Well, let's look back on our two conditions:
\begin{itemize}
	\item the distance of samples within the same class is close
	\item the distance between different classes is large
\end{itemize}
So it's easy to obtain a intuitive loss function:
\begin{equation}
L(w)=\frac{Var_z[C_1]+Var_z[C_2]}{(z_{c1}-z_{c2})^2}
\end{equation}
Via minimizing the loss function, we can obtain the target $w$:
\begin{equation}
\begin{aligned}
\hat{w}=argmin(L(w))&=argmin(\frac{Var_z[C_1]+Var_z[C_2]}{(z_{c1}-z_{c2})^2})\\
&=argmin(\frac{w^T(S_1+S_2)w}{w^T(\bar{x_{c1}}-\bar{x_{c2}})(\bar{x_{c1}}-\bar{x_{c2}})^T w})\\
&=argmin(\frac{w^T S_w w}{w^T S_b w})\\
\end{aligned}\end{equation}
In the formula:
\begin{equation}
\begin{aligned}
&S_w: with-class:variance\ within\ the\ class\\
&S_b: between-class:variance\ between\ classes\\
\end{aligned}
\end{equation}
The following is the partial derivative of the above formula:\\
\begin{equation}
\begin{aligned}
\frac{\partial{L(w)}}{\partial{w}}
&=\frac{\partial}{\partial{w}}(w^T S_w w)(w^T S_b w)^{-1}\\
&=2S_{b} w\left(w^{T} S_{w} w\right)^{-1}-2 w^{T} S_{b} w\left(w^{T} S_{w} w\right)^{-2} S_{w} w=0\\
\end{aligned} \end{equation}
try to transform the equation:
$$
\begin{aligned}
\left(w^{T} S_{b} w\right) S_{w} w&=S_{b} w\left(w^{T} S_{w} w\right)\\
\left(w^{T} S_{b} w\right) w&=S_{w}^{-1}S_{b} w\left(w^{T} S_{w} w\right)
\end{aligned}
$$
Notes: the shape of $w^T S_b w$ and $w^T S_w w$ is : $(1,p)(p,p) (p,1)=(1,1)$\\\\
Since the two terms are scalars, they only scale the module of a vector and can't change its direction, so the above formula is updated to:
$$
w \propto S_{w}^{-1} S_{b} w=S_{w}^{-1}\left(\bar{x_{c 1}}-\bar{x_{c 2}}\right)\left(\bar{x_{c 1}}-\bar{x_{c 2}}\right)^{T} w
$$
And because $\left(\bar{x_{c 1}}-\bar{x_{c 2}}\right)^{T} w$ is also a scalae, we obtain the final formula:
$$
\hat{w}\propto S_{w}^{-1}\left(\bar{x_{c 1}}-\bar{x_{c 2}}\right)
$$
So $S_{w}^{-1}\left(\bar{x_{c 1}}-\bar{x_{c 2}}\right)$ is the direction we have been seeking, finally we can get the standard $w$ via scaling.
\newpage
\subsection{Implement}
\begin{lstlisting}[language={python}]
import numpy as np
import os
os.chdir("../")
from models.linear_models import LDA

x = np.linspace(0, 100, num=100)
w1, b1 = 0.1, 10
w2, b2 = 0.3, 30
epsilon = 2
k = 0.2
b = 20
w = np.asarray([-k, 1])
v1 = x * w1 + b1 + np.random.normal(scale=epsilon, size=x.shape)
v2 = x * w2 + b2 + np.random.normal(scale=epsilon, size=x.shape)
x1 = np.c_[x, v1]
x2 = np.c_[x, v2]
l1 = np.ones(x1.shape[0])
l2 = np.zeros(x2.shape[0])
data = np.r_[x1, x2]
label = np.r_[l1, l2]

model = LDA()
model.fit(x1, x2)
model.draw(data, label)
\end{lstlisting}
\end{document}


\subsection{F test for equal population means}


\documentclass{memoir}
\usepackage{notestemplate}

%\logo{./resources/pdf/logo.pdf}
%\institute{Rice University}
%\faculty{Faculty of Whatever Sciences}
%\department{Department of Mathematics}
%\title{Class Notes}
%\subtitle{Based on MATH xxx}
%\author{\textit{Author}\\Gabriel \textsc{Gress}}
%\supervisor{Linus \textsc{Torvalds}}
%\context{Well, I was bored...}
%\date{\today}

\begin{document}

% \maketitle

% Notes taken on 02/01/21

\section{Irreducibility Criteria}
\label{sec:irreducibility_criteria}

If \(R\) is an integral domain, then for \(f(x) \in R[x]\) monic, of degree \(>0\), is irreducible if and only if \(f(x)\) cannot be factored as a product of two polynomials of deg \(\geq 1\).
Fortunately, we have a few tools to get irreducibility of polynomials, such as Gauss' Lemma.\\

Another direction we can take is roots:
\begin{prop}
	Let \(f(x) \in F[x]\) for \(F \) a field. Then
	\begin{itemize}
		\item \(f(x)\) has a degree 1 factor if and only if \(f(x)\) has a root \(\alpha\) in \(F\), i.e. \(\exists \alpha \in F\) such that \(f(\alpha)=0\)
		\item \(f(x)\) of degree 2 or 3 is reducible if and only if \(f(x)\) has a root in \(F\)
	\end{itemize}
\end{prop}
If we look at \(R = \Z\) and \(F = \Q\) specifically, we have more options.
\begin{prop}[Rational Root Test]
	Let \(f(x) = \sum_{i=1}^{n} a_i x^{i} \in \Z[x]\).
\begin{itemize}
	\item If \(\frac{r}{s}\in \Q\) with \(gcd(r,s) = 1\) and \(\frac{r}{s}\) is a root of \(f(x)\), then \(r\mid a_0\) and \(s\mid a_n\).
	\item If \(f(x) \in Z[x]\) is monic and if \(f(\alpha)\neq 0\) for all \(\alpha \in \Z\) dividing \(\alpha_0\), then \(f(x)\) has no roots in \(\Q\).
\end{itemize}
\end{prop}
Unfortunately, while these theorems are powerful, they are relatively dependent on the polynomial being of low degree. Ideals can help us extend these ideas to higher degree polynomials.

\begin{prop}
	Let \(R\) be an integral domain, and let \(I \triangleleft R\) be a proper ideal of \(R\). Take \(f(x) \in R[x]\) a monic polynomial of degree \(\geq 1\). If the image of \(f(x)\) in \((R / I)[x]\) is irreducible, then \(f(x)\) is irreducible in \(R[x]\).
\end{prop}

While nice, unfortunately many irreducible polynomials are reducible when modulated by the ideal.

\begin{thm}[Eisenstein-Schonemann Criteria]
	Let \(P\) be a prime ideal of an integral domain \(R\), and take
	\begin{align*}
		f(x) = x^{n}+ a_{n-1}x^{n-1}+ \ldots + a_1x + a_0
	\end{align*}
	to be a monic polynomial in \(R[x]\) of degree \(\geq 1\). Suppose \(a_{n-1},\ldots,a_1,a_0 \in P\) and \(a_0 \not\in P^2\). Then \(f(x) \) is irreducible in \(R[x]\).
\end{thm}
A trick we can use to help apply this is that if \(f(x)\) doesn't satisfy the criteria, use \(f(x-c)\) and try again. If it is (ir)reducible for \(f(x-c)\), it is ir(reducible) for \(f(x)\).
\end{document}

Abstract Algebra - Introductory/Algebra II/Notes/source/Lecture4 - PolyIrreducibilityCrit.tex


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{indentfirst}

\usepackage{geometry}
\geometry{textwidth=20cm}

\title{CS131 B1: HW 6}
\author{Duy Nguyen}
\date{16 November 2016}

\begin{document}

\maketitle

\subsection*{1.}
There are 2504 CS students at a school. Of these, 1876 have taken a course in Java, 999 have
taken a course in Python, and 345 have taken a course in C++. Further, 876 have taken a courses
in both Java and Python, 231 have taken courses in both Python and C++, and 290 have taken
courses in both Java and C++. If 189 of these students have taken courses in Python, Java, and
C++, how many of these 2504 students have not taken a course in any of these three
programming languages?

The number of student who are taking at least a programming language: $1876 + 999 + 345 - 876 - 231 - 290 + 189 = 2012$.

Therefore, the number of student who aren't taking a programing language is $2054 - 2012 = 492$ students.

\subsection*{2.}
How many elements are there in the union of four sets if each of the sets has 100 elements,
each pair of the sets shares 50 elements, each three of the sets share 25 elements, and there
are 5 elements in all four sets?
\begin{center}
\begin{tabular}{r|l}
    Add all set together &  $100\cdot 4 = 400$ \\
    Minus all the pairs (6 pairs) & $- 50 \cdot 6 = 300 $\\
    Add all the triples (4 triples) & $ 25 \cdot 4 = 100$ \\
    Minus the quad & $-5$ \\ \hline
    & $195$
\end{tabular}
\end{center}
\subsection*{3.}
If the password locking TV consists of 4 digits, how many different passwords my son will have to try in the worst case to unlock the TV?

Like the iPhone old passcode lock system, 10 digits in each spot, 4 spot, so $10^4 = 10,000$.

\subsection*{4.}
 How many license plates can be made using either 3 uppercase English letters followed by 3 digits or 4 uppercase English letters followed by 2 digits?

In the first case: 3 uppercase English letters followed by 3 digits, there are $26^3 \cdot 10^3 = 17,576,000$ different plates.
In the second case: 4 uppercase English letters followed by 2 digits, there are $26^4 \cdot 10^2 = 45,697,600$ different plates.

Thus in total there are $17,576,000 + 45,697,600 = 63,273,600$ different plates.

\subsection*{5.}
There are four possibilities for each base in DNA: A, C, G, and T. How many 5-element DNA sequences of bases

a. end with A? $4^3 \cdot 1 = 64$

b. start with T and end with G? $1 \cdot 4^2 \cdot 1 = 16$

c. contain only A and T? $ 2^4 = 16$

d. do not contain C? $3^4 = 81$

\subsection*{6.}
A committee is formed consisting of one representative from each of the 50 states in the US, where a representative from a state is either the governor or one of the 2 senators from that state. How many ways are there to form this committee?

How many 50 people group you can pick out from 150 people? $$C(150,50) = \frac{150!}{50!(150-50)!}$$.
The result is $20,128,660,909,731,932,294,240,234,380,929,315,748,140$ different ways. This looks wrong!

\subsection*{7.}
A drawer shelf contains 6 pairs of socks, each pair of a certain color (different from the rest), and all the socks are mixed on a shelf. How many individual socks should be taken from the shelf to ensure that at least 2 of them are of the same color?

$$\left \lceil{\frac{7}{6}}\right \rceil = 2$$

So at least 7 socks.

\subsection*{8.}
Show that if there are 30 students in a class, then at least 2 have the last names that begin with the same letter

There are 26 letters in the English alphabet, so if there are 27 students at least two of them will share a common first letter in their last name according to Pigeonhole principle:
$$ \left \lceil{\frac{27}{26}} \right \rceil = 2$$. So 30 are more than enough.

\subsection*{9.}
What should be the minimum number of US students in a college to guarantee that there are at least 100 students from the same US state?

Through the Pigeonhole principle, we have $$ \left \lceil{\frac{N}{50}} \right \rceil = 100$$. We note that $\frac{4950}{50} = 99$, so 4951 students should suffice to have at least 100 students from an US state.

\subsection*{10.}
Find the number of ways that an organization consisting of 10 members can elect a president, a
treasurer, and a secretary (one person may be elected just for one position).

$$P(10,3) = 720$$

\subsection*{11.}
Find the number of ways that an organization consisting of 10 members can elect 3
representatives to a senate

$$C(10,3) = 120$$

\subsection*{12.}
How many different strings can be made by reordering the characters of “Massachusetts”?

\begin{center}
\begin{tabular}{c|c}
    M & $C(13, 1) = 13$ \\
    a & $C(12, 2) = 66$ \\
    s & $C(10, 4) = 210$ \\
    c & $C(6, 1) = 6$ \\
    h & $C(5, 1) = 5$ \\
    u & $C(4, 1) = 4$ \\
    e & $C(3, 1) = 3$ \\
    t & $C(2, 2) = 1 $ \\ \hline
    & $64,864,800$
\end{tabular}
\end{center}

\subsection*{13.}
How many different signals, each consisting of 6 flags hung in a vertical line, can be formed from
4 identical red flags and 2 identical blue flags?

$$\frac{6!}{4!2!} = 15 $$

\subsection*{14.}
Suppose that an ice-cream café has 10 different flavors of ice cream. In how many different
ways one can choose 3 scoops of ice-cream, so that order of flavors does not matter? (There is
an unlimited amount of each flavor of ice-cream).

$C(10, 3) = 120$ different ways to scoops those ice cream.

\end{document}


% arara: pdflatex
% arara: pdflatex
\documentclass[letterpaper,10pt]{article}
\usepackage{preamble}

\title{Error Propagation}

\begin{document}
\maketitle

\section{Background}

"Error propagation" refers to the task of determining the uncertainty (or error) in a quantity that is calculated from one or more quantities with uncertainty. For example, given two quantities
with uncertainty, $x$ and $y$, what is the uncertainty in the product $xy$? In general, if we have $z = f(x,y)$, we want to determine the uncertainty in $z$. The most common definition used
for the uncertainty in a quantity is the standard deviation\footnote{The uncertainty in experimental measurements is commonly taken to be the standard error in the mean. Since standard error is the standard deviation of the sampling distribution, all of the calculations involving the standard deviation will also apply to standard error.}:
$$
  \sigma_x = \sqrt{ \ex{ (x - \nom{x})^2 } } = \unc{x}
$$
So, if we have an uncertain quantity $x = \uq{x}$ and a calculation $z = f(x)$, the uncertainty in $z$ is defined as
$$
  \unc{z} = \sqrt{ \ex{ (z - \nom{z})^2 } }.
$$
If $x$ is the result of multiple measurements, ${x_i}$, then we could simple calculate $\unc{z}$ directly,
\begin{align}
  \label{eq:calc-nom}
  \nom{z} &= \avg{ f(x_i) },\\
  \unc{z} &= \sqrt{ \avg{ (f(x_i) - \nom{z})^2 } }.
\end{align}
This would give the standard deviation for $z$ based on the measured data. However, if $x$ is a random variable described by a probability distribution, then the expectation values should
be evaluated in the limit $N \rightarrow \infty$. And even if we have ${x_i}$ measurements, $x$ can be considered a random variable that we have approximated using sample statistics.

There are two commonly used methods for determining the uncertainty in a calculation involving uncertain quantities. The most common method is based on a first-order approximation to the function $f$.
The Taylor Series expansion of $f$ about $\nom{x}$ is:
$$
f(x) = f(\nom{x}) + f^{\prime}(\nom{x})(x - \nom{x}) + \mathcal{O}( (x-\nom{x})^2)
$$
With this approximation, the expectation values for $z$ can be evaluated

\begin{align*}
  \nom{z} &= \ex{ f(x) } \approx f(\nom{x}), \\
  \unc{z} &= \sqrt{ \ex{ (f(x) - \nom{z})^2 } } \approx \sqrt{ \ex{ \left( f^\prime(\nom{x}) (x - \nom{x}) \right)^2 } } = \sqrt{ (f^{\prime}(\nom{x}) \unc{x})^2 } = | f^{\prime}(\nom{x}) \unc{x} |.
\end{align*}

This method is of course an approximation, but it in most cases it is
sufficient because the uncertainty $\unc{x}$ should be small if $x$ is supposed
to be a measurement (if it isn't, then our measurement isn't very good).  There
are known issues with this method. For example, the uncertainty in $\sin(x)$ is
zero if $\nom{x} = n\pi/2$ and it may not be possible to compute the
derivatives of $f(x)$, but it is certainly the ``standard'' method.

The other most common method for calculating $\unc{z}$ is based on Monte Carlo.
Values for $x$ are drawn from its distribution and used to calculate multiple
values of $z$ which are then used to calculate the average and standard
deviation. While this method is more accurate (it works for arbitrary,
non-linear functions), it is also more time consuming and may not be feasible.

Other methods for error propagation based on the Taylor Series expansion of the function $f$ exist (all you have to do is keep more terms), but they are much less common because they require higher moments of $x$ to be known, which
is often not the case.

If $f$ is a function of two variables, $x$ and $y$, then the Taylor Series expansion of the function truncated to first order can still be used and the nominal value for $z$ is a straight forward generalization of Equation \ref{eq:calc-nom}.
However, since $\unc{z}$ is the expected value of $z - \nom{z}$ squared, it will contain cross terms of $x$ and $y$. The formulas are:
\begin{align}
  \nom{z} &= \ex{ f(x,y) } \approx f(\nom{x},\nom{y}), \\
  \unc{z} &= \sqrt{ \ex{ (f(x,y) - \nom{z})^2 } } \approx \sqrt{ (f^{\prime}_x(\nom{x},\nom{y}) \unc{x})^2 + (f^{\prime}_y(\nom{x},\nom{y}) \unc{y})^2 + 2f^{\prime}_{x}(\nom{x},\nom{y})f^{\prime}_{y}(\nom{x},\nom{y})\sigma_{x,y} }.
  \label{eq:calc-unc}
\end{align}
where $f^\prime_x$ and $f^\prime_y$ indicate the partial derivatives of $f$. The $\sigma_{x,y}$ in the cross term is called covariance, and is defined as the expectation value of the product of the $x$ and $y$ deviations:
$$
\sigma_{x,y} = \ex{ (x-\nom{x})(y-\nom{y}) }.
$$
the \emph{correlation} is defined as the covariance normalized to $\unc{x}\unc{y}$:
$$
\label{correlation}
r_{x,y} = \frac{ \ex{ (x-\nom{x})(y-\nom{y}) } }{\unc{x}\unc{y}}.
$$
The two quantities, $x$ and $y$ are said to be \emph{uncorrelated} if their deviations are independent of each other. In this case, positive and negative deviations in both $x$ and $y$ happen at random, and on average will all cancel
such that $\sigma_{x,y}$ (and $r_{x,y}$) will be zero.

\section{Numerical Method}
\label{sec-numrical-method}

The method implemented by this module differs slightly from the first-order method. Rather than computing the derivatives of $f(x)$ at $\nom{x}$, we simply evaluate $f(x)$ at $\nom{x}$ and $\nom{x} + \unc{x}$ and take the difference:
$$
\begin{aligned}
  \nom{z} &\approx f(\nom{x}), \\
  \unc{z} &\approx f(\upp{x}) - \nom{x}.
\end{aligned}
$$
This has the advantage of being very simple, and can be performed by hand if needed. In fact, that is the original motivation for this module. I needed to determine the uncertainty in a calculation so that I could check the uncertainty
calculated by students in a physic laboratory class. This method makes intuitive sense (we just want to see how much different our answer would be if $x$ increased by its uncertainty), but it is not without justification. The first-order
error propagation method is based on the approximation
$$
f(x) = f(\nom{x}) + f^{\prime}(\nom{x})(x - \nom{x}).
$$
Rewriting $x$ here as $\upp{x}$ gives
$$
\begin{aligned}
f(\upp{x}) &= f(\nom{x}) + f^{\prime}(\nom{x})\unc{x} \\
f^{\prime}(\nom{x})\unc{x} &= f(\upp{x}) - f(\nom{x})
\end{aligned}
$$
So, to first-order, our calculation agrees with the derivative based method.

We can do the same for functions of two (or more) quantities, but we must take care of correlations. Using the first-order approximation, we can show that
$$
\begin{aligned}
f^{\prime}_x(\nom{x},\nom{y})\unc{x} = f(\upp{x},\nom{y}) - f(\nom{x},\nom{y})\\
f^{\prime}_y(\nom{x},\nom{y})\unc{y} = f(\nom{x},\upp{y}) - f(\nom{x},\nom{y})
\end{aligned}
$$
Now, let $\unc{z}_x$ and $\unc{z}_y$ be the uncertainties in $z$ caused by the uncertainty in $x$ and $y$ separately,
$$
\begin{aligned}
\unc{z}_{x} &= f^{\prime}_x(\nom{x},\nom{y})\unc{x} =  f(\upp{x},\nom{y}) - f(\nom{x},\nom{y})\\
\unc{z}_{y} &= f^{\prime}_y(\nom{x},\nom{y})\unc{y} =  f(\nom{x},\upp{y}) - f(\nom{x},\nom{y})
\end{aligned}
$$
Then the total uncertainty $\unc{z}$ can be written as (from Equation \ref{eq:calc-unc}):

$$
\begin{aligned}
        \unc{z} = \sqrt{ \unc{z}_{x}^2 + \unc{z}_{y}^2 + 2\unc{z}_{x}\unc{z}_{y} r_{x,y} }
\end{aligned}
$$
If the correlation coefficient is negative, it is possible for the total uncertainty in $z$ to be zero.

\section{Correlations}
\label{sec-correlations}

When a calculation involves more than one uncertain quantity, then correlations
between the quantities in the calculation must be considered. In simple
experiments (like those performed in an undergraduate physics lab), the
quantities measured in the experiment are not correlated and correlations
involving the measurements can safely ignore correlation. \textbf{However}, if a
series of calculations are performed with the measurements, then the results of
these calculations \emph{will} be correlated, and additional calculations that
involve the result *cannot* ignore correlation. Basically, if you have a set of
uncorrelated measurements and you do not want to deal with correlation, then
you must evaluate the uncertainty of all of your calculations directly from the
uncertainty of the measurements. The following example illustrates this.

\subsection{Determining the Correlation Coefficients}
\label{sec-determining-the-correlation-coefficients}

The correlation between two measurements can be directly calculated from Equation \ref{eq:calc-unc}, it is the average of the product of their deviations. However, if we perform a calculation that involves a measured quantity, then
the result of the calculation will be correlated to the measurement and we must take this into account if other calculations involving the result are done.

We first consider the one dimensional case, $z = f(x)$. The correlation coefficient $r_{z,x}$ is defined as
$$
r_{z,x} = \frac{ \ex{ (z-\nom{z})(x-\nom{x}) } }{\unc{z}\unc{x}}.
$$
Replacing $z$ with its first order approximation gives
$$
\ex{ (z-\nom{z})(x-\nom{x})}  = \ex{ f^{\prime}(\nom{x})(x-\nom{x})(x-\nom{x}) } = f^{\prime}(\nom{x})\ex{ (x-\nom{x})^2 } = f^{\prime}(\nom{x})\unc{x}^2
$$
Recall that $\unc{z} = |f^{\prime}(\nom{x})\unc{x}|$, so the correlation coefficient can be written as
$$
r_{z,x} = \frac{ \pm\unc{z} \unc{x} }{\unc{z}\unc{x}} = \pm 1.
$$
So, the correlation coefficient for $z$ and $x$ will be plus or minus 1, depending on the sign of the derivative $f^{\prime}$. If the derivative is positive, then the correlation will be positive. This
indicates that $x$ and $z$ are directly correlated, which makes since if $z$ is directly calculated from $x$.

If $z$ is a function of two (or more) quantities, then we need to determine the correlation coefficient between $z$ and each of the quantities it depends on. The procedure is generally the same.
Let $z = f(x,y)$. Then, to first order, we can write
$$
f(x,y) = f(\nom{x},\nom{y})
      +  f^{\prime}_x(\nom{x},\nom{y}) (x-\nom{x})
      +  f^{\prime}_y(\nom{x},\nom{y}) (y-\nom{y})
$$
With this, the covariance of $z$ and $x$ is
$$
\begin{aligned}
\ex{(z-\nom{z})(x-\nom{x})} &= \ex{ (f^{\prime}_x(\nom{x},\nom{y}) (x-\nom{x}) +  f^{\prime}_y(\nom{x},\nom{y}) (y-\nom{y}))(x - \nom{x}) } \\
                            &= \ex{ (f^{\prime}_x(\nom{x},\nom{y}) (x-\nom{x})(x - \nom{x} )} + \ex{ f^{\prime}_y(\nom{x},\nom{y}) (y-\nom{y}))(x - \nom{x}) } \\
                            &= (f^{\prime}_x(\nom{x},\nom{y}) \ex{ (x-\nom{x})^2} +  f^{\prime}_y(\nom{x},\nom{y})\ex{ (y-\nom{y}))(x - \nom{x}) } \\
                            &= f^{\prime}_x(\nom{x},\nom{y}) \unc{x}^2 + f^{\prime}_y(\nom{x},\nom{y})\sigma_{y,x}
\end{aligned}
$$
And the correlation coefficient is (recall that $\unc{z}_x = f^{\prime}_x(\nom{x},\nom{y}) \unc{x}$)
$$
\begin{aligned}
r_{z,x} &= \frac{f^{\prime}_x(\nom{x},\nom{y}) \unc{x}^2}{\unc{z}\unc{x}} + \frac{f^{\prime}_y(\nom{x},\nom{y})\sigma_{y,x}}{\unc{z}\unc{x}} \\
        &= \frac{\unc{z}_x\unc{x}}{\unc{z}\unc{x}} + \frac{f^{\prime}_y(\nom{x},\nom{y})\unc{y}\sigma_{y,x}}{\unc{z}\unc{y}\unc{x}}  \\
        &= \frac{\unc{z}_x\unc{x}}{\unc{z}\unc{x}} + \frac{\unc{z}_y\sigma_{y,x}}{\unc{z}\unc{y}\unc{x}}  \\
        &= \frac{\unc{z}_x}{\unc{z}} + \frac{\unc{z}_y\sigma_{y,x}}{\unc{z}\unc{y}\unc{x}}  \\
        &= \frac{\unc{z}_x}{\unc{z}} + \frac{\unc{z}_y}{\unc{z}}r_{y,x}
\end{aligned}
$$
If $z$ depended on another quantity, then an additional term similar to the second would be required. Assume $z = f(x,y,w)$, then we would have
$$
r_{z,x} = \frac{\unc{z}_x}{\unc{z}} + \frac{\unc{z}_y}{\unc{z}}r_{y,x} + \frac{\unc{z}_w}{\unc{z}}r_{w,x}
$$
So, the correlation between $z$ and the quantities it is calculated from depends on the correlation between the quantities themselves. If all quantities are uncorrelated, then
$z$ will be correlated to each. However, if two or more quantities are correlated, then $z$ could be uncorrelated with them.

The first term can be rewritten in the same form as the others,
$$
r_{z,x} = \frac{\unc{z}_x}{\unc{z}}r_{x,x} + \frac{\unc{z}_y}{\unc{z}}r_{y,x} + \frac{\unc{z}_w}{\unc{z}}r_{w,x}.
$$
In general:
$$
r_{z,x_i} = \sum_j \frac{\unc{z}_{x_j}}{\unc{z}}r_{x_j,x_i} = \frac{1}{\unc{z}} \sum_j r_{x_j,x_i}\unc{z}_{x_j}.
$$
In fact, this formula works for quantities that $z$ does not directly depend on. Let $z = f(x,y,w)$, and consider the correlation between $z$ and $v$,
$$
\begin{aligned}
r_{z,v} &= \frac{1}{\unc{z}\unc{v}} \ex{ \left[f^{\prime}_x(\nom{x},\nom{y},\nom{w}) (x-\nom{x})
                                             + f^{\prime}_y(\nom{x},\nom{y},\nom{w}) (y-\nom{y})
                                             + f^{\prime}_w(\nom{x},\nom{y},\nom{w}) (w-\nom{w})\right] (v-\nom{v})
                                    } \\
        &= \frac{1}{\unc{z}\unc{v}} \left[f^{\prime}_x(\nom{x},\nom{y},\nom{w}) \sigma_{x,v}
                                        + f^{\prime}_y(\nom{x},\nom{y},\nom{w}) \sigma_{y,v}
                                        + f^{\prime}_w(\nom{x},\nom{y},\nom{w}) \sigma_{w,v}\right] \\
        &= \frac{\unc{z}_x}{\unc{z}} r_{x,v}
         + \frac{\unc{z}_y}{\unc{z}} r_{y,v}
         + \frac{\unc{z}_w}{\unc{z}} r_{w,v}
\end{aligned}
$$

It is possible for the uncertainty in $z$ to be zero if its inputs are correlated, which will cause the denominator to be zero. If this happens, then $z$ is
uncorrelated with the inputs.

\section{Summary}
\label{sec-summary}

In general, assume we have a calculation we need to perform that is a function of several uncertain quantities:
$$
z = f(x,y,w,\ldots)
$$
The nominal value for $z$ is
$$
\nom{z} = f(\nom{x},\nom{y},\nom{w},\ldots)
$$
The individual uncertainties are
$$
\begin{aligned}
\unc{z}_x &= f(\upp{x},\nom{y},\nom{w},\ldots) - \nom{z} \\
\unc{z}_y &= f(\nom{x},\upp{y},\nom{w},\ldots) - \nom{z} \\
\unc{z}_w &= f(\nom{x},\nom{y},\upp{w},\ldots) - \nom{z} \\
\ldots
\end{aligned}
$$
The total uncertainty is
$$
\unc{z} = \sqrt{ \unc{z}_x^2 + 2\unc{z}_x\unc{z}_y r_{x,y} + \unc{z}_y^2 + 2\unc{z}_y\unc{z}_wr_{y,w} + \unc{z}_w^2 + \ldots}.
$$
The quantity $z$ will be correlated to the inputs of $f$. The correlation coefficients are
$$
\begin{aligned}
r_{z,x} &= \frac{\unc{z}_x}{\unc{z}}
         + \frac{\unc{z}_y}{\unc{z}}r_{y,x}
         + \frac{\unc{z}_w}{\unc{z}}r_{w,x}
         + \ldots \\
r_{z,y} &= \frac{\unc{z}_x}{\unc{z}}r_{x,y}
         + \frac{\unc{z}_y}{\unc{z}}
         + \frac{\unc{z}_w}{\unc{z}}r_{w,y}
         + \ldots \\
r_{z,w} &= \frac{\unc{z}_x}{\unc{z}}r_{x,w}
         + \frac{\unc{z}_y}{\unc{z}}r_{y,w}
         + \frac{\unc{z}_w}{\unc{z}}
         + \ldots \\
\ldots
\end{aligned}
$$
If the input quantities are not correlated, then the total uncertainty will simplify
$$
\unc{z} = \sqrt{ \unc{z}_x^2 + \unc{z}_y^2 + \unc{z}_w^2 + \ldots}
$$
as will the correlations
$$
\begin{aligned}
r_{z,x} &= \frac{\unc{z}_x}{\unc{z}} \\
r_{z,y} &= \frac{\unc{z}_y}{\unc{z}} \\
r_{z,w} &= \frac{\unc{z}_w}{\unc{z}} \\
\ldots
\end{aligned}
$$

\end{document}


\subsection{Optimization}
\label{sec:optimization}

\textbf{Purpose}: Iteratively optimize yield, quality, etc. of crops as more environment condition and plant metric data is gathered across different programs over multiple growth cycles.

\textbf{Function}:
\begin{itemize}
    \item \textbf{Inputs}: Growth cycle datasets (Environment data across time (control system portion of $\vec E(t)$), plant performance metric (PPM) data across time ($\vec P(t)$), associated program (actuated portion of $\vec E(t)$))
    \item \textbf{Outputs}: Plant-program performance prediction model, novel programs
\end{itemize}

\textbf{Method}:

Assume a plant's growth rate (or state change) is related to its current internal state $\vec P \in \R^n$ (for $n$ plant metrics) and the environment conditions $\vec E \in \R^m$ (for $m$ environment parameters). Let these both be functions $\vec P (t),\vec E(t)$ defined at each $t$, where $t=0$ indicates the time of planting. Assume that this relationship is constant for all members of a given species.

Define plant state change $\vec P'$:

$$\vec P'(t) = \frac{d}{dt}\vec P(t)$$

Define the plant-environment behaviour function $Q$:

$$Q(\vec P(t), \vec E(t), t)=\vec P'(t)$$

Given the current internal and external states, determine the plant's state change.

\begin{enumerate}
    \item Set $\vec E_{set}(t)~\forall~ t$, aka the program (\ref{sec:automation});
    \item Record $\vec P(t)~\forall~ t$ and $\vec E(t)\approx \vec E_{set}(t)~\forall~ t$;
    \item Calculate $\vec P'(t)~\forall~ t$;
    \item Fit $\vec Q$ to our data;
\end{enumerate}

By fitting $\vec Q$ across iterations, we can predict $\vec P$ at any $\vec E$ and $t$. For example:

$$\vec P(t+\Delta t)=P(t)+\Delta t\cdot Q(\vec P(t),\vec E(t))$$

Gradient ascent with this model can be used to generate novel (theoretically improved) programs.

\textbf{Features}:
\begin{itemize}
    \item \textit{Machine Learning Model}: Represented by $Q$. Operates in the cloud.
    \item \textit{Environment Data} (over time): Represented by $\vec E(t)$. Collected by sensors (for \textit{control loop} environment parameters) and extracted from the associated program (for \textit{actuated instruction} environment parameters). See Section \ref{sec:automation}.
    \item \textit{PPM Data} (over time): Represented by $\vec P(t)$. Extracted from computer vision. See Section \ref{sec:automation}.
\end{itemize}


\section{\textbf{DisBarrierTest}}

\subsection{Particular Case (problem)}
The problem we are trying to solve is that of synchronization on a
multi-threaded environment; and the particular mechanism we want to
use for that is a barrier.

\subsection{Solution}
The particular type of barrier proposed for this exercise is called a
Dissemination Barrier, and it has a nice tree structure. Imagine that
in order to implement the barrier, you need to perform ``r'' rounds;
where each rounds means that all the threads involved communicated
with other two threads (everyone sent a message to one predefined
thread and waited to receive a message from another predefined
thread). We use the communication word loosely here, as in a shared
memory computer it will simply mean writing or reading from a shared
variable. \\

Now, each round is essentially some sort of permutation; is like you
align the $n$ threads in one line, duplicate that line and do a
shuffle on it, then you pair its elements with the original line. It
turns out (can be proved), that if you do $ceil(log_2(n))$ rounds and
on each round $k$ use the formula $(i+2^k) \bmod n$ for thread
communication (where $i$ is the slot associated to each thread on an
array), then all the threads can safely assumed that everybody has
finished its call to \C{await} and they can move on. The
implementation is compact enough to fit here, so here it goes: \\

\begin{lstlisting}[style=numbers]
public class DisBarrier implements Barrier {
  int size;
  int logSize;
  Node[][] node;
  ThreadLocal<Boolean> mySense;
  ThreadLocal<Integer> myParity;

  public DisBarrier(int capacity) {
    size = capacity;
    logSize = 0;
    while (capacity != 1) {
      this.logSize++;
      capacity >>= 1;
    }
    node = new Node[logSize][size];
    for (int r = 0; r < logSize; r++) {
      for (int i = 0; i < size; i++) {
        node[r][i] = new Node();
      }
    }
    int distance = 1;
    for (int r = 0; r < logSize; r++) {
      for (int i = 0; i < size; i++) {
        node[r][i].partner = node[r][(i + distance) % size];
      }
      distance *= 2;
    }
    this.mySense = new ThreadLocal<Boolean>() {
      protected Boolean initialValue() { return true; };
    };
    this.myParity = new ThreadLocal<Integer>() {
      protected Integer initialValue() { return 0; };
    };
  }

  public void await() {
    int parity = myParity.get();
      boolean sense = mySense.get();
      int i = ThreadID.get();
    for (int r = 0; r < logSize; r++) {
      node[r][i].partner.flag[parity] = sense;
      while (node[r][i].flag[parity] != sense) {}
    }
    if (parity == 1) {
      mySense.set(!sense);
    }
    myParity.set(1 - parity);
  }

  private static class Node {
    boolean[] flag = {false, false};  // signal when done
    Node partner;                     // partner node
  }
}
\end{lstlisting}
\hfill

\subsection{Experiment Description}
The test consists in having a shared array of 8 slots, one per thread
to be created. Then the test has two phases, both using the barrier;
the first phase is to wait all threads to write on their assigned slot
on the array and the second stage is to allow all threads to validate
that the rest actually finished their writing. These two phases are
repeated 8 times.

\subsection{Observations and Interpretations}
The test hangs time to time, on the 24 cores machine (with 2 cores
machine, issue was more difficult to reproduce);  the hanging threads
have an stack like this: \\

\begin{verbatim}
"Thread-5" prio=10 tid=0x00007f1bc0174800 nid=0x8414 runnable [0x00007f1b6bb59000]
java.lang.Thread.State: RUNNABLE
at barrier.DisBarrier.await(DisBarrier.java:59)
at barrier.DisBarrierTest$TestThread.run(DisBarrierTest.java:79)
\end{verbatim}
\hfill

The stack above refers to the following wait cycle from the \C{await} method: \\

\begin{lstlisting}[style=nonumbers]
      while (node[r][i].flag[parity] != sense) {}
\end{lstlisting}
\hfill

The problem then implies that the thread in charge of writing to the
flag of the hanging thread, never made the write ... well, that is the
appearance but in reality what happened is that the flag variable was
not declared as volatile, therefore there is no guarantee about when
that new value will be propagated from caches to main memory, and from
there to the hanging thread core's cache. Thus, even when the write
was actually done the other thread could not see it: \\

As with other tests from chapter 2, we can solve this issue simply by
declaring the flag as volatile: \\

\begin{lstlisting}[style=nonumbers]
volatile boolean[] flag = {false, false}
\end{lstlisting}
\hfill

With above fix the test no longer hangs on both 2 and 24 core machines.