% $Id: ScriptedMath.tex,v 1.1 2008/01/31 18:04:17 dconway Exp $
\chapter{\label{chapter:InlineMath}Inline Mathematics in GMAT}
\chapauthor{Darrel J. Conway}{Thinking Systems, Inc.}

GMAT provides a flexible mechanism that lets users place both scalar and matrix computations into
the command sequence for a mission.  This mechanism is implemented in a set of classes described in
this chapter.

\section{Scripting GMAT Mathematics}

Mathematics in GMAT scripts follow the conventions established in MATLAB; an equation consists of an
object on the left side of an equals sign, with an equation on the right.  Equations can be entered
either in script files, or using a panel on the graphical user interface.  Parentheses are used to
set the precedence of operations when the normal precedence rules are not valid.
Table~\ref{table:operators} lists the operators implemented in GMAT. The table is arranged in order
of operator precedence; operators higher in the table are evaluated before operators that appear
lower in the table. Users can override this order through selective use of parentheses.

\begin{table}[tb]

\caption{\label{table:operators}Operators and Operator Precedence in GMAT}
\begin{tabular}{|>{\raggedright\hspace{0pt}}p{1.15in}%
                |>{\raggedright\hspace{0pt}}p{1.15in}%
                |>{\raggedright\hspace{0pt}}p{1.5in}%
                |>{\raggedright\hspace{0pt}}p{1.5in}|}
\hline
  \textbf{Operator or Function}& \textbf{Implemented Cases}&\textbf{Comments}& \textbf{Example}
\tabularnewline
\hline \hline
%  Evaluate Parameters and object& Any GMAT parameter& & sat.Earth.SMA
%\tabularnewline \hline
  Evaluate Conversion Functions& DegToRad, RadToDeg& Converts between radians and
degrees&DegToRad(sat.RAAN)
\tabularnewline \hline
  Evaluate Matrix Operations& transpose and ', det, inv and \textasciicircum{}(-1), norm& & mat',
det(mat)
\tabularnewline \hline
  Evaluate Math Functions& sin, cos, tan, asin, acos, atan, atan2, log, log10, exp, sqrt& Angles in
the trig functions are in radians& sin(DegToRad(sat.TA))
\tabularnewline \hline
  Exponentiation&\textasciicircum{}& Powers are any real number& sin(radTA)\textasciicircum{}0.5
\tabularnewline \hline
  Multiplication and Division& {*} /& & sat.RMAG / sat.SMA
\tabularnewline \hline
  Addition and Subtraction& + -& & sat.RAAN + sat.AOP
\tabularnewline \hline
\end{tabular}
\end{table}

Mathematics in GMAT are scripted using the same syntax as assignments.  Three
samples of the scripting for the operations in Table~\ref{table:operators} are
provided here to and discussed in the design presentation to help explain how
GMAT manipulates its internal data structures to perform scripted mathematics.

\subsection*{Example 1: Basic Arithmetic}
In this simplest example, a user needs to write script to perform the
calculation of the longitude of periapsis,

\begin{equation}\label{eq:mathLongPeri}
\Pi=\Omega+\omega
\end{equation}

\noindent for the spacecraft named sat.  The scripting for this calculation is
straight forward:

\begin{quote}
\begin{verbatim}
Create Spacecraft sat;
Create Variable arg
GMAT arg = sat.RAAN + sat.AOP
\end{verbatim}
\end{quote}

\subsection*{Example 2: More Complicated Expressions}

This snippet calculates the separation between two spacecraft, using the
Pythagorean theorem:

\begin{equation}\label{eq:mathSatSep}
\Delta R = \sqrt{(X_1 - X_2)^2 + (Y_1 - Y_2)^2 + (Z_1 - Z_2)^2}
\end{equation}

\noindent This is a useful example because, as we will see, it exercises the
parser to ensure that operations are performed in the correct order.  The
script for this example is, again, pretty simple:

\begin{quote}
\begin{verbatim}
Create Spacecraft sat1, sat2;
Create Variable sep
GMAT sep = sqrt((sat1.X-sat2.X)^2 + (sat1.Y-sat2.Y)^2 + (sat1.Z-sat2.Z)^2)
\end{verbatim}
\end{quote}

\subsection*{Example 3: Matrix Computations}

This final example is more complex, and exercises both operator ordering and
matrix computations to calculate a component of the analytic gradient of a
function used in optimization.  This script snippet assumes that GMAT can
calculate the State Transition Matrix and provide users with access to the
corresponding 3x3 submatrices of it.  The scripting for that calculation is:

\begin{quote}
\begin{verbatim}
% This script snippet uses the following definitions for pieces of the
% State Transition Matrix (STM):
%    Sat.Phi is a 6x6 matrix that is the spacecraft STM
%    Sat.PhiA is the upper left 3x3 portion of the STM
%    Sat.PhiB is the upper right 3x3 portion of the STM
%    Sat.PhiC is the lower left 3x3 portion of the STM
%    Sat.PhiD is the lower right 3x3 portion of the STM

Create Spacecraft Sat1, Sat2
Create Array Svec[3,1] Svecdot[3,1] S[1,1] dSdotdR[1,3]

For I = 1: 100
   % Step the spacecraft
   Propagate LowEarthProp(Sat1,Sat2);

   % Calculate the relative position and velocity vectors
   GMAT Svec(1,1) = Sat2.X - Sat1.X;
   GMAT Svec(2,1) = Sat2.Y - Sat1.Y;
   GMAT Svec(3,1) = Sat2.Z - Sat1.Z;
   GMAT Svecdot(1,1) = Sat2.VX - Sat1.VX;
   GMAT Svecdot(2,1) = Sat2.VY - Sat1.VY;
   GMAT Svecdot(3,1) = Sat2.VZ - Sat1.VZ;

   % Calculate range
   GMAT S =  norm(Svec);

   % Calculate the change in the range rate due to a change in the
   % initial position of sat1
   GMAT dSdotdR = 1/S*( Svecdot' - Svec'*Svecdot*Svec'/S^2 )*(- Sat1.PhiA )...
                  + Svec'/S*(-Sat1.PhiC);
EndFor;
\end{verbatim}
\end{quote}

\noindent The last expression here, dsDotdR, will be used in the design
discussion.

\section{Design Overview}

When GMAT encounters the last line of the first script snippet:

\begin{quote}\begin{verbatim}
GMAT arg = sat.RAAN + sat.AOP
\end{verbatim}\end{quote}

\noindent it creates an assignment command that assigns the results of a calculation to the variable
named arg. The right side of this expression -- the equation -- is converted into GMAT objects using
an internal class in GMAT called the MathParser.  The MathParser sets up custom calculations by
breaking expressions -- like the ones scripted in the preceding section -- into a tree structure
using a recursive descent algorithm.  This decomposition is performed during script parsing when the
user is running from a script file, and during application of user interface updates if the user is
constructing the mathematics from the GMAT graphical user interface.  GMAT stores the tree
representation of the mathematics in an internal object called the MathTree.  During script
execution, the MathTree is populated with the objects used in the calculation during mission
initialization in the Sandbox.  The equation is evaluated when the associated Assignment command is
executed by performing a depth-first traversal of the tree to obtain the desired results.  The
algorithms implemented here are extensions of the approach presented in chapter 40 of
\cite{schildt}.

\begin{figure}[tb]
\begin{center}
\includegraphics[scale=0.5]{Images/nodePlusAop.eps}
\caption{\label{figure:longPeriapseTree}Tree View of the Longitude of Periapsis Calculation}
\end{center}
\end{figure}

The tree based structure of the computations enforces the operator precedence rules tabulated above.
 In this section the construction and evaluation of the trees for the examples is presented, and the
classes used in this process are introduced.  The sections that follow this overview present the
classes in a more systematic manner, discuss how the scripting is parsed to create the GMAT objects
used in evaluation, and then tie these pieces together by discussing how the constructed objects
interact as a program executes.

Figure~\ref{figure:longPeriapseTree}\footnote{In this figure and those that follow, the components
that can be evaluated into Real numbers are drawn on elongated octagons, and the operators are drawn
in a circle or ellipse.  Matrices are denoted by a three-dimensional box.  Empty nodes are denoted
by black circles, and numbers, by orange squares with rounded corners.} shows the tree generated for
the longitude of periapsis calculation scripted above.  This simplest example illustrates the layout
of the tree in memory that results from a simple arithmetic expression. The GMAT MathParser class is
fed the right side of the expression from the script -- in this case, that is the string "sat.RAAN +
sat.AOP".  This string is passed to the recursive descent code, which breaks it into three pieces --
two expressions that can be evaluated directly, and an operator that combines these expressions.
These pieces are stored in an internal class in GMAT called the MathTree.  The expressions
"sat.RAAN" and "sat.AOP" are placed into the "leaves" of the tree, while the addition operator is
placed in the top, "internal" node.  The leaf nodes are all instances of a class named
"MathElement", and the internal nodes, of classes derived from a class named "MathFunction".  When
the assignment command containing this construct is executed, each of the leaves of the tree is
evaluated, and then combined using the code for the addition operator.

\begin{figure}[tb]
\begin{center}
\includegraphics[scale=0.5]{Images/satSep.eps}
\caption{\label{figure:satelliteSeparationTree}Tree View of the Satellite
Separation Calculation}
\end{center}
\end{figure}

The second example, illustrated in Figure~\ref{figure:satelliteSeparationTree},
provides a more illustrative example of the parsing and evaluation algorithms
implemented in GMAT.  This tree illustrates the equation encoded in example 2:

\begin{quote}\begin{verbatim}
GMAT sep = sqrt((sat1.X-sat2.X)^2 + (sat1.Y-sat2.Y)^2 + (sat1.Z-sat2.Z)^2)
\end{verbatim}\end{quote}

\noindent Each node in the MathTree can be one of three types: a
function node, an operator node (both of these types are embodied in the
MathFunction class), or an element node (in the MathElement class).  The
element nodes are restricted to being the leaf nodes of the tree; the internal
nodes are all either function nodes or operator nodes.

Each MathElement node consists of two separate pieces; a string containing the
text of the expression represented by the node, and either a pointer to the
object that embodies that expression or, for constants, a local member
containing the value of the expression.  The pointer member is initially set to NULL when the
MathElement node is constructed during script parsing.  When the
script is initialized in the GMAT Sandbox, these pointers are set to the
corresponding objects in the Sandbox's configuration.  Each time the assignment command associated
with the MathTree executes, an Evaluate() method is called
on the MathTree, as described below.

The function and operator nodes consist of several pieces as well.  Each of
these nodes contain subnode pointers that identify the input value or values
needed for the node evaluation, and a method that performs the actual
mathematics involved in the evaluation.  The mathematical operations for each
of these nodes is coded to work on either a scalar value or a matrix; the
specific rules of implementation are operator specific.

The Evaluate() method for the MathTree calls the Evaluate() method for the
topmost node of the tree.  This method call is evaluated recursively for all of the subnodes of the
tree, starting at the top node.  The method checks to see
if the node is a leaf node or an internal node.  If it is a leaf node, it is
evaluated and the resulting value is returned to the object that called it.  If it is an internal
node, it evaluates its subnodes by calling Evaluate() first
on the left node, then on the right node.  Once these results are obtained,
they are combined using the mathematical algorithm coded for the node, and the
resulting value is then returned to the calling object.

\begin{figure}[tb]
\begin{center}
\includegraphics[scale=0.5]{Images/MatrixExample.eps}
\caption{\label{figure:matrixMathTree}Tree View of the Matrix Calculation in
Example 3}
\end{center}
\end{figure}

Finally, the gradient component scripted in the third example:

\begin{quote}\begin{verbatim}
GMAT dSdotdR = 1/S*( Svecdot' - Svec'*Svecdot*Svec'/S^2 )*(- Sat1.PhiA )...
               + Svec'/S*(-Sat1.PhiC);
\end{verbatim}\end{quote}

\noindent produces Figure~\ref{figure:matrixMathTree}.  Evaluation for this tree proceeds as
outlined above, with a few variations.  Instead of calling the Evaluate() method for the nodes in
the tree, expressions that use matrices call the MatrixEvaluate method.  Another wrinkle introduced
by the matrix nature of this example is that the internal nodes now have an additional requirement;
each node needs to determine that the dimensionality of the subnodes is consistent with the
requested operations.  This consistency check is performed during initialization in the Sandbox,
using the ValidateInputs() method.
MatrixEvaluate may perform additional checks during execution, so that
singularities in the computation can be flagged and brought to the attention of the user.

\section{Core Classes}

\begin{figure}
\begin{center}
\includegraphics[scale=0.5]{Images/MathClasses.eps}
\caption{\label{figure:MathClasses}Classes Used to Implement GMAT Mathematics}
\end{center}
\end{figure}

Figure~\ref{figure:MathClasses} shows the class hierarchy implemented to
perform the operations described above, along with some of the core members of
these classes.  The core classes used in GMAT to perform mathematical
operations are shown in green in this figure, while the helper classes used to
setup the binary tree structure are shown in orange.  The MathTree and its
nodes are all owned by instances of the Assignment command, shown in yellow in
the figure.  Core GMAT classes are shaded in blue.  The main features of these
classes are shown here, and discussed in the following paragraphs.  At the end
of this section, the principal elements of the base classes are collected for
reference.

The MathTree class is the container for the tree describing the equation.  It
contains a pointer to the topmost node of the tree, along with methods used to
manipulate the tree during initialization and execution.  This class is used to provide the
interface between the tree and the Assignment command.

Each node in a MathTree is derived from the MathNode class.  That base class
provides the structures and methods required by the MathTree to perform its
functions.  There are two classes derived from the MathNode base: MathElement
and MathFunction.  The MathElement class is used for leaf nodes, and can store
either a numerical value, a matrix, or a GMAT object that evaluates to a
floating point number -- for example, a Parameter, or a real member of a core
GMAT object.  MathFunction instances are used to implement mathematical
operators and functions.  The left and right subnodes of these nodes contain
the function or operator operands.  Subnodes are evaluated before the operator
is evaluated, producing results that are used when evaluating the function.

The MathNode base class contains two members that are used to check the
compatibility of operands during initialization.  The EvaluateInputs() method
checks the return dimensions of the subnodes of the node, and returns true if
either the node is a MathElement or if the subnodes are compatible with the
current node's Evaluate() and MatrixEvaluate() methods.  The ReportOutputs()
method is called on subnodes to obtain the dimensions of matrices returned from calls to
MatrixEvaluate().  That method provides an interface used by the
EvaluateInputs() method to perform its evaluation.

One additional item worth mentioning in the MathNode base class is the
implementation of the MatrixEvaluate() method.  The Evaluate() method is pure
virtual, and therefore not implemented in the base class.  MatrixEvaluate(), on the other hand, is
implemented to apply the Evaluate() method element by
element to the matrix members.  In other words, the default MatrixEvaluate()
method implements the algorithm

\begin{equation}\label{eq:mathDefaultMatrixCalc}
M_{ij} = Op(L_{ij},R_{ij})
\end{equation}

\noindent where $M_{ij}$ is the [i,j] element of the resultant, $L_{ij}$ is the
[i,j] element of the left operand, and $R_{ij}$ is the [i,j] element of the
right operand.  Most classes derived from the MathFunction class will override
this implementation.

The classes implementing mathematical operations are derived from the MathFunction class.
Figure~\ref{figure:MathClasses} shows some (but not all) of these derived classes.  Operators that
have a one to one functional correspondence with MATLAB operations are named identically to the
MATLAB function.  That means that operators like the transpose operator will violate the GMAT naming
conventions, at least for the string name assigned to the class, because the MATLAB operator is
lowercase, ``transpose'', while the GMAT naming convention specified that class names start with an
upper case letter.

Operations that can rely on the algorithm presented in equation~\ref{eq:mathDefaultMatrixCalc} do
not need to implement the MatrixEvaluate() method; for the classes shown here, that means that Add,
Subtract, sin, cos, and asin only need to implement the Evaluate() method, while Multiply, Divide,
transpose, norm and Invert need to implement both the Evaluate() and MatrixEvaluate() methods.

\subsection{MathTree and MathNode Class Hierarchy Summary}

This section describes the top level classes in the MathTree subsystem, summarizing key features and
providing additional information about the class members.

\subsubsection{MathTree}

A MathTree object is a container class used to help initialize and manage the tree representing an
equation.  It standardizes the interface with the Assignment command and acts as the entry point for
the evaluation of an equation.  It is also instrumental in setting the object pointers on the tree
during initialization in the Sandbox.  Key members of this class are described below.

\subparagraph{\textit{Class Attributes}}
\begin{itemize}
\item \textbf{topNode}: A pointer to the topmost node in the MathTree.
\end{itemize}

\subparagraph{\textit{Methods}}
\begin{itemize}
\item \textbf{Evaluate()}: Calls the Evaluate() method on the topNode and returns the value obtained
from that call.
\item \textbf{MatrixEvaluate()}: Calls the MatrixEvaluate() method on the topNode and returns the
matrix obtained from that call.
\item \textbf{ReportOutputs(Integer \&type, Integer \&rowCount, Integer \&colCount)}:  Calls
ReportOutputs(...) on the topNode and returns the data obtained in that call, so that the Assignment
command can validate that the returned data is compatible with the object that receives the
calculated data (i.e. the object on the left side of the equation).
\item \textbf{Initialize(std::map<std::string,GmatBase*> *objectMap)}: Initializes the data members
in the MathTree by walking through the tree and setting all of the object pointers in the
MathElement nodes.
\end{itemize}

\subsubsection{MathNode}

MathNode is the base class for the nodes in a MathTree.  Each MathNode supports methods used to
determine the return value from the node, either as a single Real number or as a matrix.  The
MathNodes also provide methods used to test the validity of the calculation contained in the node
and any subnodes that may exist.  The core MathNode members are listed below.

\subparagraph{\textit{Class Attributes}}
\begin{itemize}
\item \textbf{realValue}: Used to store the most recent value calculated for the node.
\item \textbf{matrix}: Used to store the most recent matrix data calculated for the node, when the
node is used for matrix calculations.
\end{itemize}

\subparagraph{\textit{Methods}}
\begin{itemize}
\item \textbf{Evaluate()}: An abstract method that returns the value of the node.  For MathElements,
this method returns the current value of the element, either by evaluating a Parameter and returning
the value, accessing and returning an object's internal data, or returning a constant.  For
MathFunctions, the Evaluate() method appies the function and returns the result.  If the encoded
function cannot return a Real number, Evaluate() throws an exception.
\item \textbf{MatrixEvaluate()}: Fills in a matrix with the requested data.  For MathFunction
objects, this method performs the calculation of the operation and fills in the matrix with the
results.  The default implementation uses equation~\ref{eq:mathDefaultMatrixCalc} to fill in the
matrix element by element.  Operations that do not return matrix values, like norm and determinant,
throw exceptions when this method is called.  MathElements simply return the matrix associated with
the node.
\item \textbf{EvaluateInputs()}: Checks the inputs to the node to be sure that they are compatible
with the calculation that is being performed.  For MathElement nodes, this method always returns
true if the node was successfully initialized.  For MathFunction nodes, this method calls its
subnodes and checks to be sure that the subnodes return compatible data for the function.
\item \textbf{ReportOutputs(Integer \&type, Integer \&rowCount, Integer \&colCount)}:  This method
tells the calling object the type and size of the calculation that is going to be performed by
setting values of the parameters used in the call.  The first parameter, `type', is set to indicate
whether the return value will be a matrix or a Real number.  `rowCount' and `colCount' are set to
the dimensions of the matrix if the return value is a matrix, or to 0 if the return value is scalar.
 This method is used in the EvaluateInputs() method to determine the suitability of subnodes for a
given calculation, and by the MathTree class to obtain the size of the answer returned from a
complete calculation.
\end{itemize}

\subsubsection{MathElements}

The leaf nodes of a MathTree are all instances of the MathElement class.  The MathElement class acts
as a wrapper for GMAT objects, using the methods defined in the GmatBase base class to set these
referenced objects up for the MathElement's use.  The GmatBase methods SetRefObject(),
SetRefObjectName(), GetRefObject(), and GetRefObjectName() are overridden to set the internal data
structures in the node.  The other relevant members of this class are listed below.

\subparagraph{\textit{Class Attributes}}
\begin{itemize}
\item \textbf{refObjectName}: Holds the name of the GMAT object that is accessed by this node.
\item \textbf{refObject}: A pointer to the referenced object.  This pointer is set when the MathTree
is initialized in the Sandbox.
\end{itemize}

\subparagraph{\textit{Methods}}
\begin{itemize}
\item \textbf{SetRealValue(Real value)}: Sets the value of the node when it contains a constant.
\end{itemize}

\subsubsection{MathFunctions}

The internal nodes of a MathTree are all instances of classes derived from MathFunction.  This class
contains pointers to subnodes in the tree which are used to walk through the tree structure during
initialization and evaluation.  The relevant members ate described below.

\subparagraph{\textit{Class Attributes}}
\begin{itemize}
\item \textbf{left}: A pointer to the left subnode used in the calculation.  MathFunctions that only
require a right subnode leave this pointer in its default, NULL setting.
\item \textbf{right}: A pointer to the right subnode used in the calculation.  MathFunctions that
only require a left subnode leave this pointer in its default, NULL setting.
\end{itemize}

\subparagraph{\textit{Methods}}
\begin{itemize}
\item \textbf{SetChildren(MathNode *leftChild, MathNode *rightChild)}: Sets the pointers for the
left and right child nodes.  If a node is not going to be set, the corresponding parameter in the
call is set to NULL.
\item \textbf{GetLeft()}: Returns the pointer to the left node.
\item \textbf{GetRight()}: Returns the pointer to the right node.
\item \textbf{Evaluate()}: In derived classes, this method is overridden to perform the mathematical
operation represented by this node.
\item \textbf{MatrixEvaluate()}: In derived classes that do not use the default matrix calculations
(equation~\ref{eq:mathDefaultMatrixCalc}), this method is overridden to perform the mathematical
operation represented by this node.
\end{itemize}

\subsection{Helper Classes}

There are two classes that help configure a MathTree: MathParser and MathFactory.  In addition, the
Assignment command acts as the interface between a MathTree and other objects in GMAT, and the
Moderator provides the object interfaces used to configure the tree.  This section sketches the
actions taken by these components.

\subsubsection{MathParser}

The Interpreter subsystem (see Section~\ref{section:InterpreterOverview}) in GMAT includes an
interface that can be used to obtain a MathParser object.  This object takes the right side of an
equation, obtained from either the GMAT GUI or the ScriptInterpreter, and breaks it into a tree
that, when evaluated depth first, implements the equation represented by the equation.  The
MathParser uses the methods described below to perform this task.

\subparagraph{\textit{Methods}}
\begin{itemize}
\item \textbf{Parse(const std::string \&theEquation)}: Breaks apart the text representation of an
equation and uses the component pieces to construct the MathTree.
\item \textbf{CreateNode(const std::string \&genString)}: Uses the generating string ``genString'',
to create a node for insertion into the MathTree.
\item \textbf{Decompose(const std::string \&composite)}: This method is the entry point to the
recursive descent algorithm.  It uses internal methods to take a string representing the right side
of the equation and break it into the constituent nodes in the MathTree.  The method returns the
topmost node of the MathTree, configured with all of the derived subnodes.
\end{itemize}

\subsubsection{MathFactory}

The MathFactory is a GMAT factory (see Chapter~\ref{chapter:Factories} that is used to construct
MathNodes.  It has one method of interest here:

\subparagraph{\textit{Methods}}
\begin{itemize}
\item \textbf{CreateNode(const std::string \&ofType)}: Creates a MathNode that implements the
operation contained in the string.  If no such operator exists, the MathFactory creates a
MathElement node and sets the reference object name on that node to the test of the `ofType' string.
\end{itemize}

\subsubsection{The Assignment Command and the Moderator}

The Assignment command is the container for the MathTree described in this chapter.  All GMAT
equations are formatted with a receiving object on the left side of an equals sign, then the equals
sign, and then the equation on the right.  When the interpreter system is configuring an Assignment
command, it detects when the right side is an equation, and passes the string describing the
equation into a MathParser.  That MathParser proceeds to parse the equation, making calls into the
Moderator when a new MathNode is required.  The Moderator accesses the MathFactories through the
FactoryManager, and obtains MathNodes as required.  These nodes are not added to the Configuration
Manager, but they are returned to the MathParser for insertion into the current MathTree.  Once the
tree is fully populated, it is returned to the Assignment command, completing the parsing of the
expression.

When the Moderator is instructed to run a mission, it passes the configured objects into the
Sandbox, and then initializes the Sandbox.  The last step in Sandbox initialization is to initialize
all of the commands in the mission sequence.  When one of these commands is an Assignment command
that includes a MathTree, that command initializes the MathTree after initializing all of its other
elements, and then validates that the MathTree is compatible with the object on the left side of the
equation.  If an error is encountered at this phase, the Assignment command throws an exception that
describes the error and includes the text of the command that failed initialization.  If
initialization succeeds, the Moderator then tells the Sandbox to run the mission.  The Sandbox
starts at the first command in the mission sequence, and executes the command stream as described in
Chapter~\ref{chapter:Commands}.

\section{Building the MathTree}

\begin{figure}
\begin{center}
\includegraphics[scale=0.5]{Images/MathParserTop.eps}
\caption{\label{figure:MathParserTop}Control Flow for Parsing an Equation}
\end{center}
\end{figure}

Scripted mathematics are constructed using the MathParser class, which builds
the binary tree representing the equation that is evaluated by constructing
nodes for the tree and placing these nodes into the tree one at a time.
Figure~\ref{figure:MathParserTop} shows the high level control flow used to
create the MathTree.  An empty MathTree is created, and then that tree is
passed into the MathParser along with the string representation of the
equation.  The Mathparser takes the MathTree and populates it with MathNodes
based on the equation string.  The top node of this completed tree is then
returned from the parser, and set on the assignment command for use during
execution of the mission.

\begin{figure}
\begin{center}
\includegraphics[scale=0.4]{Images/ParseRecursion.eps}
\caption{\label{figure:ParserRecursion}Parser Recursion Sequence}
\end{center}
\end{figure}

The middle step in the process outlined in Figure~\ref{figure:MathParserTop}
encapsulates the recursive descent decomposition of the equation.
Figure~\ref{figure:ParserRecursion} provides a more detailed view of this
algorithm.  The InterpretAction method of the Assignment command determines
that the right side of the assignment is an equation, and then creates a
MathTree and a MathParser to break this equation into the components needed for evaluation during
execution.  The MathTree and the equation string are passed
into the MathParser.

The MathParser takes the input string, and attempts to break it into three
pieces: an operator, a left element, and a right element.  Any of these three
pieces can be the empty string; if the operator string is empty, only the left
string contains data, denoting that the string is used to build a MathElement
node, on one of the leaves of the MathTree.

If the operator string is not empty, the operator string is used to build a
MathFunction node.  MathFunction nodes are used to perform all mathematical
operations: basic math like addition, subtraction, multiplication, division,
and exponentiation, along with unary negation and mathematical functions.  The
arguments of the MathFunction are contained in the left and right strings.
These strings are passed into the MathParser's Parse method for further
decomposition, and the process repeats until all of the strings have been
decomposed into operators and the MathElement leaf nodes.  If either string is
empty, the corresponding child node on the MathFunction is set to NULL.

Once a leaf node has been constructed, that node is set as the left or right
node on the operator above it.  Once the left and right nodes are set on a
MathFunction, that node is returned as a completed node to the calling method,
terminating that branch of the recursion.  When the topmost node has its child
nodes filled in, the MathParser returns from the recursion with the completed
MathTree.

\section{Program Flow and Class Interactions}

The preceding section describes the construction of the MathTree that
represents an equation. The parsing described above places the instances of the
MathFunction nodes into the MathTree, along with the string names of the
MathElement nodes.  The objects evaluated in the MathElement nodes are not
placed into the MathTree, because those elements depend on local objects in the
GMAT Sandbox when a script is executed.  This section explains how those
objects are placed into the MathTree in the Sandbox, and then evaluated to
complete a calculation for an Assignment command.

\subsection{Initialization}

\begin{figure}
\begin{center}
\includegraphics[scale=0.5]{Images/MathTreeInitialization.eps}
\caption{\label{figure:MathInitialization}MathTree Initialization in the
Sandbox}
\end{center}
\end{figure}

Figure~\ref{figure:MathInitialization} shows the process of initialization of
the Command Sequence in the Sandbox, with a focus on the MathTree
initialization.  Section \ref{section:SandboxInitialization} describes the
general initialization process in the Sandbox.  Sandbox initialization proceeds
as described there, initializing the objects and then the command sequence.
When the command in the sequence is an Assignment command containing in-line
mathematics, the Assignment command performs the details shown here to
initialize the MathTree.  The command first accesses the top node of the
MathTree.  If that node has subnodes, those subnodes are initialized
iteratively until a MathElement node is encountered.

When a MathElement node is encountered, that node is queried for its referenced
object's name.  If the node returns a name, that object's pointer is accessed
in the local object map owned by the Sandbox and set on the node using the
SetRefObject() method.  If the reference object name is empty, the node is a
numerical constant, and no further initialization is required.

When all of the subnodes of a MathFunction node have been initialized, that
node validates that the dimensionality of the operands are compatible with the
mathematical operation represented by the node.  This validation is done by
calling the ReportOutputs() method on the child nodes and ensuring that the
results are consistent with the requirements of the operation.  If the results
are consistent, local variables are used to save data so that parent nodes to
the current node can obtain consistency data without recursing through the
MathTree.  When the results are inconsistent with the operation, a warning
message (which indicates the inconsistency of the calculation and the text of
the line that generate the MathTree) is posted to the user, and an internal
flag is set to false, indicating that the calculation cannot be performed.
That flag is returned when the EvaluateInputs() method is called on the node.
This completes the initialization of the MathFunction node, and control is
returned to the node above the current node.

When the topmost node in the MathTree finishes initialization, the MathTree
calls the EvaluateInputs() method for the top node.  If that call returns a
false value, an exception is thrown and initialization terminates for the
Assignment command.  When the call to EvaluateInputs() succeeds, the MathTree
reports successful initialization to the Assignment command, which validates
that the result of the calculation is consistent with the object that will be
receiving the result, and, if so, returns a flag indicating that the
calculation initialized successfully.  If the resultant of the MathTree
calculation is determined to be inconsistent with the receiving object, an
exception is thrown that contains the text of the line that generated the
Assignment command, along with information about the error encountered.

\subsection{Execution}

\begin{figure}
\begin{center}
\includegraphics[scale=0.5]{Images/MathTreeExecution.eps}
\caption{\label{figure:MathTreeExecution}Evaluation of a MathTree Assignment}
\end{center}
\end{figure}

The task of evaluating a calculation is shown in Figure~\ref{figure:MathTreeExecution}.  The
Assignment command determines if a MathTree calculation is being performed by determining if the
right side of the assignment (denoted RHS in the figure) is a MathTree.  If it is, the Assignment
command checks to see if the result of the calculation should be a scalar value or a matrix by
calling ReportOutputs() on the MathTree.  If the result of this call indicates that the output is
one row by one column, the output from the calculation is scalar; otherwise, it is a matrix.  The
corresponding Evaluate() method is called on the MathTree.

The MathTree Evaluate() methods behave identically in control flow; the
difference between Evaluate() and MatrixEvaluate() is in the return value of
the call.  Similarly, the MathNode Evaluate() and MatrixEvaluate() methods
follow identical control flow, differing only in return types.  When the
correct Evaluate() method is called on the MathTree, the MathTree calls the
corresponding Evaluate() method on the topmost MathNode in the tree.
Evaluation is then performed recursively on the nodes of the tree, as described
here.

When an Evaluate() method is called on a node, the evaluation process proceeds
based on the type of node that owns the method.  If the node is a MathFunction
node, then it calls the corresponding Evaluate() method on each of its child
nodes, evaluating the left node first, then the right node.  If one of those
nodes is NULL that phase of the evaluation is skipped.  This can occur when the
mathematical operation only requires one operand -- for example, for most of
the trigonometric functions, or for unitary matrix operations like the
transpose operation.  When the child node evaluation is complete, the returned
data from that evaluation are used as the operands for the mathematical
operation.  The operation is performed, and the resulting data are passed to
the calling method.

MathElement nodes are evaluated directly when encountered, and can return
either a real number or a matrix of real numbers based on which method is
called -- either Evaluate() for a Real, or MatrixEvaluate() for a matrix.  The
result of this evaluation is passed to the calling method.  Since all of the
leaf nodes on a MathTree are MathElement nodes, these nodes terminate the
iteration through the tree.

When the calculation iteration reaches the topmost node in the MathTree, the
operation for that node is performed and the resulting data are returned to the Assignment command.
The Assignment command then sets the data on the GMAT
object designated on the left side of the statement, designated the LHS in the
figure.  This completes the evaluation of the Assignment command.


 \usepackage{amsmath}       % I think this gives me some symbols
 \usepackage{amsthm}        % Does theorem stuff
 \usepackage{amssymb}       % more symbols and fonts
 \usepackage{mathtools}     % More math macros
 \usepackage{empheq}        % Some more extensible arrows, like \xmapsto
  \usepackage{bbm}
 %\usepackage{mbboard} % greek bb math characters, etc.
% \usepackage{mathabx}
 \usepackage{mathrsfs}      % Sheafy font \mathscr{}
% \usepackage{picinpar}      % for pictures in paragraphs

\usepackage{enumerate} %% To customize enumeration.
%\usepackage{pdfsync}
\usepackage{bbold}
\usepackage[symbol]{footmisc}

%%% Tikz
\usepackage{tikz}
%\usetikzlibrary[snakes]
\usetikzlibrary[shapes]
\usepgflibrary[arrows]
\usetikzlibrary{matrix}
\usepackage{tikz-cd}

\usepackage{mdframed}

% Stuff to import svg graphics
\usepackage{import}
\usepackage{xifthen}
\usepackage{pdfpages}
\usepackage{transparent}
\usepackage{calc}

\newcommand*{\incfig}[2][1]{%
    \def\svgscale{#1}
    \import{./images/}{#2.pdf_tex}
}

% \usepackage{pstricks}      % PSTricks!
% \usepackage[active]{srcltx}
% \usepackage{xcolor}        % for colors (links)
% \usepackage[all]{xy}       % Include XY-pic
    %\SelectTips{cm}{10}     % Use the nicer arrowheads
    %\everyxy={<2.5em,0em>:} % Sets the scale I like
 \usepackage[colorlinks,
             linkcolor=black,
             bookmarksnumbered=true]{hyperref}

%%%%%%% Pagestyle stuff %%%%%%%%%%%%%%%%%%%
 \usepackage{fancyhdr}                   %%
   \pagestyle{fancy}                     %%
   \fancyhf{} %delete the current section for header and footer
 \usepackage[paperheight=11in,%          %%
             paperwidth=8.5in,%          %%
             outer=1.2in,%               %%
             inner=1.2in,%               %%
             bottom=.7in,%               %%
             top=.7in,%                  %%
             includeheadfoot]{geometry}  %%
   \addtolength{\headwidth}{.75in}       %%
   \fancyhead[RO,LE]{\thepage}           %%
   \fancyhead[RE,LO]{\sectionname}       %%
   \setlength{\headheight}{15.8pt}       %%
   \raggedbottom                         %%
%%%%%%% End Pagestyle stuff %%%%%%%%%%%%%%%

% Bibliography related
\usepackage[
            hyperref=true,
            url=false,
            isbn=false,
            backref=true,
            style=alphabetic,
            block=none
]{biblatex}
\addbibresource{references.bib}

%% Include git info
\usepackage[mark]{gitinfo2}
\renewcommand{\gitMark}{Commit hash: \gitAbbrevHash{} (\gitAuthorDate)}

%%%%%%% Stuff for keeping track of sections %%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \newcommand{\sektion}[1]{\newpage\section{#1}%                       %%
                          \gdef\sectionname{\thesection\quad #1}}     %%
 \newcommand{\subsektion}[1]{\subsection*{#1}%                        %%
                         \addcontentsline{toc}{subsection}{#1}}       %%
 %This is the empty section title, before any section title is set    %%
 \newcommand\sectionname{}                                            %%
%%%%%%% End stuff for keeping track of sections %%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%% Theorem Styles and Counters %%%%%%%%%%%%%%%%%%%%%%%%%%
\newcounter{homework}
\newcounter{project}

 \renewcommand{\theequation}{\thesection.\arabic{equation}}         %%
  \renewcommand{\thehomework}{\thesection.\arabic{homework}}
   \renewcommand{\theproject}{\thesection.\arabic{project}}
 \makeatletter                                                      %%
    \@addtoreset{equation}{section} % Make the equation counter reset each section
    \@addtoreset{footnote}{section} % Make the footnote counter reset each section
    \@addtoreset{homework}{section} % Make the homework counter reset each section
    \@addtoreset{project}{section} % Make the project counter reset each section
                                                                    %%
 \newenvironment{warning}[1][]{%                                    %%
    \begin{trivlist} \item[] \noindent%                             %%
    \begingroup\hangindent=2pc\hangafter=-2                         %%
    \clubpenalty=10000%                                             %%
    \hbox to0pt{\hskip-\hangindent\manfntsymbol{127}\hfill}\ignorespaces%
    \refstepcounter{equation}\textbf{Warning~\theequation}%         %%
    \@ifnotempty{#1}{\the\thm@notefont \ (#1)}\textbf{.}            %%
    \let\p@@r=\par \def\p@r{\p@@r \hangindent=0pc} \let\par=\p@r}%  %%
    {\hspace*{\fill}$\lrcorner$\endgraf\endgroup\end{trivlist}}     %%

                                                                    %%
\newenvironment{exercise}{
        \refstepcounter{homework}
        \begin{trivlist}%                        %%
    \item{\bf Exercise~\thehomework.}}{\end{trivlist}}

 \newenvironment{solution}{\begin{trivlist}%                        %%
    \item{\it Solution:}}{\end{trivlist}}                           %%
                                                                    %%
    \newenvironment{project}{
        \refstepcounter{project}
        \begin{trivlist}%                        %%
    \item{\bf Project~\thehomework.}}{\end{trivlist}}

 \def\newprooflikeenvironment#1#2#3#4{%                             %%
      \newenvironment{#1}[1][]{%                                    %%
          \refstepcounter{equation}                                 %%
          \begin{proof}[{\rm\csname#4\endcsname{#2~\theequation}%   %%
          \@ifnotempty{##1}{\the\thm@notefont \ (##1)}\csname#4\endcsname{.}}]%%
          \def\qedsymbol{#3}}%                                      %%
         {\end{proof}}}                                             %%
 \makeatother                                                       %%
                                                                    %%

                       %%
 \theoremstyle{plain}               %%%%% Theorem-like commands
 \newtheorem{theorem}[equation]{Theorem}                            %%
% \newtheorem*{claim}{Claim}                                         %%
 \newtheorem*{lemma*}{Lemma}                                        %%
 \newtheorem*{theorem*}{Theorem}                                    %%
 \newtheorem{lemma}[equation]{Lemma}                                %%
\newtheorem{corollary}[equation]{Corollary}                        %%
 \newtheorem{proposition}[equation]{Proposition}                    %%
  \newtheorem{conjecture}[equation]{Conjecture}                    %%
  \newtheorem{property}[equation]{Property}                                %%

 \theoremstyle{definition} %%%% Definition-like Commands
  \newtheorem{fact}[equation]{Fact}
%\newtheorem{definition}[equation]{Definition}
%\newtheorem{example}[equation]{Example}
%\newtheorem{excercise}[equation]{Exercise}

 \newprooflikeenvironment{definition}{Definition}{$\diamond$}{textbf}%
 \newprooflikeenvironment{example}{Example}{$\diamond$}{textbf}     %%
 \newprooflikeenvironment{remark}{Remark}{$\diamond$}{textbf}       %%
  %\newprooflikeenvironment{homework}{Homework}{$\diamond$}{textbf}
 \newprooflikeenvironment{digression}{Digression}{$\diamond$}{textbf}       %%
 \newprooflikeenvironment{claim}{Claim}{$\diamond$}{textbf}%

\theoremstyle{remark} %%%%%% Remark-Like Commands
% \newtheorem{remark}[equation]{Remark}
 \newtheorem{question}[equation]{Question}
  \newtheorem{idea}[equation]{Idea}
  %\newtheorem{solution}[equation]{Solution}

%  \newtheorem{digression}[equation]{Digression}
 %\newtheorem{warning}[equation]{Warning}

%%%%%%%%%%% End Theorem Styles and Counters %%%%%%%%%%%%%%%%%%%%%%%%%%

%%% These three lines load and resize a caligraphic font %%%%%%%%%
%%% which I use whenever I want lowercase \mathcal %%%%%%%%%%%%%%%
 \DeclareFontFamily{OT1}{pzc}{}                                 %%
 \DeclareFontShape{OT1}{pzc}{m}{it}{<-> s * [1.100] pzcmi7t}{}  %%
 \DeclareMathAlphabet{\mathpzc}{OT1}{pzc}{m}{it}                %%
                                                                %%
%%% and this is manfnt; used to produce the warning symbol %%%%%%%
 \DeclareFontFamily{U}{manual}{}                                %%
 \DeclareFontShape{U}{manual}{m}{n}{ <->  manfnt }{}            %%
 \newcommand{\manfntsymbol}[1]{%                                %%
    {\fontencoding{U}\fontfamily{manual}\selectfont\symbol{#1}}}%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%% TikZ Commands and Macros %%%%%%%%%%%%%

%%%% These draw triple or quadruple set of arrows of length 0.5 cm
\DeclareMathOperator{\righttriplearrows} {{\; \tikz{ \foreach \y in {0, 0.1, 0.2} { \draw [-stealth] (0, \y) -- +(0.5, 0);}} \; }}
\DeclareMathOperator{\lefttriplearrows} {{\; \tikz{ \foreach \y in {0, 0.1, 0.2} { \draw [stealth-] (0, \y) -- +(0.5, 0);}} \; }}
\DeclareMathOperator{\rightquadarrows} {{\; \tikz{ \foreach \y in {0, 0.1, 0.2, 0.3} { \draw [-stealth] (0, \y) -- +(0.5, 0);}} \; }}
\DeclareMathOperator{\leftquadarrows} {{\; \tikz{ \foreach \y in {0, 0.1, 0.2, 0.3} { \draw [stealth-] (0, \y) -- +(0.5, 0);}} \; }}

%%%%%%% End TikZ Commands and Macros %%%%%%%%%%%%%

%%%%%%%%%%%%%%%% Macros %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcommand{\confused}[1]{[[\ensuremath{\bigstar\bigstar\bigstar} #1]]}  %%% Three Eye-Catching Stars
\renewcommand{\labelitemi}{--}  % changes the default bullet in itemize

\newcommand{\mcg}[1][g]{\mathrm{Mod}(#1)}
\newcommand{\mcgb}[1][1]{\mathrm{Mod}^{#1}}

\newcommand{\twobar}{/\kern-0.2em/}

\DeclareMathOperator{\RR}{\mathbb{R}}
\newcommand{\dd}{\mathrm{\,d}}
% \DeclareMathOperator{\dim}{\mathrm{dim}}
\newcommand{\norm}[1]{\left\lVert#1\right\rVert}

\newcommand{\wt}[1]{\widetilde{#1}}

% \newcommand{\tg}[1][g]{\mathcal{T}_{#1}}
% \newcommand{\mg}[1][g]{\mathcal{M}_{#1}}
% \newcommand{\sg}[1][g]{S_{#1}}
% \DeclareMathOperator{\out}{\mathrm{Out}}
% \DeclareMathOperator{\fg}{\pi_1}
% \DeclareMathOperator{\Sp}{\mathrm{Sp}}
% \DeclareMathOperator{\pgl}{\mathrm{PGL}}

% \let\hom\relax % kills the old hom, which is lowercase
% \DeclareMathOperator{\hom}{\mathrm{Hom}}

% \DeclareMathOperator{\diff}{\mathrm{Diff}}
% \DeclareMathOperator{\homeo}{\mathrm{Homeo}}
% \DeclareMathOperator{\cp}{\mathbb{CP}}
% \DeclareMathOperator{\gr}{\mathrm{Gr}}
% \DeclareMathOperator{\hilb}{{\sf Hilb}}
% \DeclareMathOperator{\arc}{\mathcal{A}}
% \DeclareMathOperator{\curve}{\mathcal{Z}}
% \DeclareMathOperator{\Proj}{\mathcal{P}\kern-.125em\mathpzc{roj}}
%  \DeclareMathOperator{\ad}{ad}
%  \DeclareMathOperator{\ann}{ann}
%  \DeclareMathOperator{\aut}{\underline{Aut}}
%  \DeclareMathOperator{\Aut}{Aut}
%  \newcommand{\bbar}[1]{\setbox0=\hbox{$#1$}\dimen0=.2\ht0 \kern\dimen0 \overline{\kern-\dimen0 #1}}
%   \DeclareMathOperator{\ber}{\textrm{Ber}}
%  \DeclareMathOperator{\coker}{coker}
%   \DeclareMathOperator*{\colim}{colim}
%   \def\dbar{{\slash\mkern-12muD}}  % This makes a Dirac "D" with a slash through it.
%   \DeclareMathOperator{\dash}{{\textrm{-}}}  % shortcut for a normal text dash mark -
%  \DeclareMathOperator{\diff}{Diff}
%  \newcommand{\e}{\varepsilon}
%   \DeclareMathOperator{\ext}{Ext}
%  \DeclareMathOperator{\End}{\ensuremath{\mathcal{E}\kern-.125em\mathpzc{nd}}}
%  \DeclareMathOperator{\fun}{Fun}
%  \newcommand{\Ga}{\Gamma}
%   \DeclareMathOperator{\hocolim}{hocolim}
%   \DeclareMathOperator{\holim}{holim}
%  \let\hom\relax % kills the old hom, which is lowercase
%  \DeclareMathOperator{\hom}{Hom}
%  \DeclareMathOperator{\Hom}{\mathcal{H}\kern-.125em\mathpzc{om}}
%  \DeclareMathOperator{\HOM}{HOM}

%  \DeclareMathOperator{\id}{id}
%  \DeclareMathOperator{\im}{im}
%  \DeclareMathOperator{\isom}{\underline{Isom}}
%  \newcommand{\liset}{\text{\it lis-et}}
%  \newcommand{\Liset}{\text{\it Lis-Et}}
%   \DeclareMathOperator{\map}{Map}
%  \DeclareMathOperator{\mfg}{{\mathcal{M}_{FG}}}
%  \DeclareMathOperator{\mfgs}{{\mathcal{M}_{FG}^s}}
%  \renewcommand\mod{\textrm{\sf Mod}}
%  \DeclareMathOperator{\nat}{Nat}
%  \DeclareMathOperator{\Nat}{NAT}
%  \newcommand{\Om}{\Omega}
%  \newcommand{\pb}{\rule{.4pt}{5.4pt}\rule[5pt]{5pt}{.4pt}\llap{$\cdot$\hspace{1pt}}}
%  \newcommand{\po}{\rule{5pt}{.4pt}\rule{.4pt}{5.4pt}\llap{$\cdot$\hspace{1pt}}}
%  \DeclareMathOperator{\proj}{Proj}
%  \DeclareMathOperator{\quot}{Quot}
%   \DeclareMathOperator{\RP}{{\mathbb{RP}}}
%     \DeclareMathOperator{\sdim}{\textrm{sdim}}
%  \renewcommand{\setminus}{\smallsetminus}
%  \DeclareMathOperator{\spec}{Spec}
%   \DeclareMathOperator{\spf}{Spf}

%  \DeclareMathOperator{\Spec}{\mathcal{S}\!\mathpzc{pec}}
%   \DeclareMathOperator{\str}{\textrm{str}}
%  \DeclareMathOperator{\supp}{Supp}
%   \DeclareMathOperator{\sym}{Sym}
%  \DeclareMathOperator{\tot}{Tot}
%  \DeclareMathOperator{\Tot}{\ensuremath{\mathpzc{Tot}}}
%   \DeclareMathOperator{\tr}{tr}
%  \newcommand{\udot}{\ensuremath{{\lower .183333em \hbox{\LARGE \kern -.05em$\cdot$}}}}
%  \newcommand{\uudot}{{\ensuremath{\!\mbox{\large $\cdot$}\!}}}
%  \DeclareMathOperator{\uhom}{\underline{Hom}}

% % Categories
%  \DeclareMathOperator{\ab}	{\sf Ab}
%  \newcommand{\Ab}{\text{\sf{Ab}}}
%   \DeclareMathOperator{\alg}	{{\sf Alg}}
%  \DeclareMathOperator{\algsp}	{\sf AlgSp}
%  \DeclareMathOperator{\aff}{	{\sf Aff}}
%  \DeclareMathOperator{\bibun}	{{\sf Bibun}}
%   \DeclareMathOperator{\bimod}	{{\sf Bimod}}

%     \DeclareMathOperator{\bord}{{\mathcal{B}ord}}

%  \DeclareMathOperator{\cat}	{\sf Cat}
%  \newcommand{\Cat}{\text{\sf{Cat}}}
%  \DeclareMathOperator{\coh}	{\sf Coh}
%   \DeclareMathOperator{\comm}{{\sf Comm}}
%   \DeclareMathOperator{\CG}	{{\sf CptGen}}
% \newcommand{\CP}{\mathbb{CP}}

%  \DeclareMathOperator{\daff}	{{\sf dAff}}
%  \DeclareMathOperator{\dset}	{{\sf dSet}}
%  \newcommand{\Fun}{\text{\sf{Fun}}}
%   \DeclareMathOperator{\gpd}	{{\sf Gpd}}
% \DeclareMathOperator{\gpoid}	{\sf Gpoid}
% \DeclareMathOperator{\group}	{\sf Group}
% \newcommand{\Grpd}{\text{\sf{Grpd}}}
% \DeclareMathOperator{\Haus}	{{\sf Haus}}
%   \DeclareMathOperator{\hoKan}{\sf{ hoKan}}
%     \DeclareMathOperator{\hotop}{\sf{ hoTop}}

%  \DeclareMathOperator{\Kan}	{{\sf Kan}}
%   \DeclareMathOperator{\Lie}	{{\sf Lie}}
%   \DeclareMathOperator{\man}	{\sf Man}
%     \DeclareMathOperator{\Mod}	{{\text{-}\sf Mod}}

%      \DeclareMathOperator{\nbord}{{n\mathcal{B}ord}}

%  \DeclareMathOperator{\oper}	{{\sf Operad}}
%  \DeclareMathOperator{\pdset}	{{\sf pdSet}}
%   \DeclareMathOperator{\poset}	{{\sf Poset}}
%  \newcommand{\Pre}{\text{\sf{Pre}}}
%   \DeclareMathOperator{\qcat}	{{\sf QCat}}
%  \DeclareMathOperator{\qco}	{\sf Qcoh}
%  \DeclareMathOperator{\salg}	{\sf SAlg}
%  \DeclareMathOperator{\sch}	{\sf Sch}
%  \DeclareMathOperator{\scomm}{{\sf sComm}}
%  \newcommand{\ssD}{\text{\sf{sD}}}
%  \DeclareMathOperator{\set}	{\sf Set}
%   \DeclareMathOperator{\sh}	{\sf Sh}
%     \DeclareMathOperator{\sKan}{\sf{ sKan}}
%   \DeclareMathOperator{\slie}	{\sf SLieGroup}
%  \DeclareMathOperator{\sman}	{\sf SMan}
%  \newcommand{\Spaces}{\text{\sf{Spaces}}}
%  \DeclareMathOperator{\Sp}	{{\sf Spectra}}
%    \DeclareMathOperator{\sset}	{{\sf sSet}}
%      \DeclareMathOperator{\scat}{{\sf sSet-Cat}}
%         \DeclareMathOperator{\sTop}	{{\sf sTop}}
%      \DeclareMathOperator{\stopab}	{{\sf sTopAb}}
%       \DeclareMathOperator{\strat} {{\sf Strat}}
%  \DeclareMathOperator{\strattop} {{\sf StrTop}}
% \DeclareMathOperator{\svect}	{\sf SVect}
%  \DeclareMathOperator{\Top}	{\sf Top}
%  \DeclareMathOperator{\topab}	{\sf TopAb}
%  \DeclareMathOperator{\topoi}	{\sf Topoi}
%  \DeclareMathOperator{\tors}	{\sf Tors}
%   \DeclareMathOperator{\uTop}	{{\sf \underline{To}p}}
%  \DeclareMathOperator{\vect}	{\sf Vect}
%  \DeclareMathOperator{\Vect}	{{\sf Vect}}

% Letter Short Cuts

 \newcommand{\cA}{\mathcal{A}}
 \newcommand{\cB}{\mathcal{B}}
 \newcommand{\cC}{\mathcal{C}}
 \newcommand{\cD}{\mathcal{D}}
 \newcommand{\cE}{\mathcal{E}}
 \newcommand{\cF}{\mathcal{F}}
 \newcommand{\cG}{\mathcal{G}}
 \newcommand{\cH}{\mathcal{H}}
 \newcommand{\cI}{\mathcal{I}}
 \newcommand{\cJ}{\mathcal{J}}
 \newcommand{\cK}{\mathcal{K}}
 \newcommand{\cL}{\mathcal{L}}
 \newcommand{\cM}{\mathcal{M}}
 \newcommand{\cN}{\mathcal{N}}
 \newcommand{\cO}{\mathcal{O}}
\newcommand{\cP}{\mathcal{P}}
\newcommand{\cQ}{\mathcal{Q}}
\newcommand{\cR}{\mathcal{R}}
\newcommand{\cS}{\mathcal{S}}
 \newcommand{\cT}{\mathcal{T}}
 \newcommand{\cU}{\mathcal{U}}
 \newcommand{\cV}{\mathcal{V}}
 \newcommand{\cW}{\mathcal{W}}
 \newcommand{\cX}{\mathcal{X}}
 \newcommand{\cY}{\mathcal{Y}}
 \newcommand{\cZ}{\mathcal{Z}}

 \newcommand{\A}{\mathbb{A}}
 \newcommand{\B}{\mathbb{B}}
\newcommand{\C}{\mathbb{C}}
\newcommand{\D}{\mathbb{D}}
 \newcommand{\E}{\mathbb{E}}
 \newcommand{\F}{\mathbb{F}}
\newcommand{\G}{\mathbb{G}}
 \renewcommand{\H}{\mathbb{H}} % old \H{x} is an x with a weird umlaut in text mode
\newcommand{\I}{\mathbb{I}}
 \newcommand{\J}{\mathbb{J}}
 \newcommand{\K}{\mathbb{K}}
 \renewcommand{\L}{\mathbb{L}}
 \newcommand{\M}{\mathbb{M}}
 \newcommand{\N}{\mathbb{N}}
 \renewcommand{\O}{\mathbb{O}}
 \renewcommand{\P}{\mathbb{P}}
 \newcommand{\Q}{\mathbb{Q}}
 \newcommand{\R}{\mathbb{R}}
\renewcommand{\S}{\mathbb{S}}
 \newcommand{\T}{\mathbb{T}}
 \newcommand{\U}{\mathbb{U}}
 \newcommand{\V}{\mathbb{V}}
 \newcommand{\W}{\mathbb{W}}
\newcommand{\X}{\mathbb{X}}
\newcommand{\Y}{\mathbb{Y}}
  \newcommand{\Z}{\mathbb{Z}}

  \newcommand{\sA}{\mathsf{A}}
 \newcommand{\sB}{\mathsf{B}}
 \newcommand{\sC}{\mathsf{C}}
 \newcommand{\sD}{\mathsf{D}}
 \newcommand{\sE}{\mathsf{E}}
 \newcommand{\sF}{\mathsf{F}}
 \newcommand{\sG}{\mathsf{G}}
 \newcommand{\sH}{\mathsf{H}}
 \newcommand{\sI}{\mathsf{I}}
 \newcommand{\sJ}{\mathsf{J}}
 \newcommand{\sK}{\mathsf{K}}
 \newcommand{\sL}{\mathsf{L}}
 \newcommand{\sM}{\mathsf{M}}
 \newcommand{\sN}{\mathsf{N}}
 \newcommand{\sO}{\mathsf{O}}
\newcommand{\sP}{\mathsf{P}}
\newcommand{\sQ}{\mathsf{Q}}
\newcommand{\sR}{\mathsf{R}}
\newcommand{\sS}{\mathsf{S}}
 \newcommand{\sT}{\mathsf{T}}
 \newcommand{\sU}{\mathsf{U}}
 \newcommand{\sV}{\mathsf{V}}
 \newcommand{\sW}{\mathsf{W}}
 \newcommand{\sX}{\mathsf{X}}
 \newcommand{\sY}{\mathsf{Y}}
 \newcommand{\sZ}{\mathsf{Z}}

   \newcommand{\cl}{\mathfrak{cl}}
  \newcommand{\g}{\mathfrak{g}}
  \newcommand{\h}{\mathfrak{h}}
%    \newcommand{\l}{\mathfrak{l}}
 \newcommand{\m}{\mathfrak{m}}
 \newcommand{\n}{\mathfrak{n}}
  \newcommand{\p}{\mathfrak{p}}
  \newcommand{\q}{\mathfrak{q}}
 \renewcommand{\r}{\mathfrak{r}} % old \r{x} is a little circle over x in text mode

 \newcommand{\w}{\omega}
 \newcommand{\Dx}{\; \mathcal{D}x}

\setlength{\marginparwidth}{.8in}
\definecolor{MyBlue}{rgb}{.1,0.7,1.3}
%\begin{center}
%{\color{MyDarkBlue}This color is MyDarkBlue}
%\end{center}
\newcommand{\notetoself}[1]{\marginpar{\tiny\color{MyBlue}{ #1}}}

%%%%%%%%%%%% End Macros %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 \openout0=lastupdated.html
 \write0{\today}
 \closeout0


\documentclass[bigger]{beamer}

\input{header-beam} % change to header-handout for handouts

% ====================
\title[Lecture 20]{Logic I F13 Lecture 20}
\date{November 26, 2013}
% ====================

\include{header}

\setlength{\fitchprfwidth}{5em}

\section{Truth Functions}

\subsec{Truth Functions}{

\begin{block}{Definition}

An ($n$-place) \emph{truth function} $t$ is a mapping of $n$-tuples of \T{} and \F\ to either \T\ or \F.
\medskip

$n$-place truth functions correspond to truth tables of sentence with $n$ atomic sentences.
\end{block}

\begin{tabular}{cc@{\qquad}cc}
$\begin{array}{cc|c}
&& t_\land\\ \hline
\T & \T & \T \\
\T & \F & \F \\
\F & \T & \F \\
\F & \F & \F
\end{array}$
&
$A_1 \land A_2$ &
$\begin{array}{cc|c}
&& t_\lor \\ \hline
\T & \T & \T \\
\T & \F & \T \\
\F & \T & \T \\
\F & \F & \F
\end{array}$
& $A_1 \lor A_2$
\end{tabular}

}

\subsec{Truth Functions}{

\begin{block}{Definition}
A sentence $S$ containing the atomic sentences $A_1$, \dots, $A_n$
\emph{expresses} the truth function $t$ iff the truth value of $S$ on
the truth-value assignment which assigns $v_i$ to $A_i$ is $t(v_1,
\dots, v_n)$.
\medskip

An $n$-place truth function is \emph{expressible} if there is a
sentence containing atomic sentences $A_1$, \dots, $A_n$ that
expresses it.

\end{block}
}

\subsec{Examples}{

\begin{tabular}{cc}
$\begin{array}{cc|c}
& & t_1\\ \hline
\T & \T & \T \\
\T & \F & \T \\
\F & \T & \F \\
\F & \F & \F
\end{array}$
& $A_1 \quad\text{or:}\quad A_1 \land (A_2 \lor \lnot A_2)$ \\ \ \\
$\begin{array}{cc|c}
& & t_{XOR}\\ \hline
\T & \T & \F \\
\T & \F & \T \\
\F & \T & \T \\
\F & \F & \F
\end{array}$
&
$(A_1 \lor A_2) \land \lnot(A_1 \land A_2)$
\end{tabular}
}

\section{Truth-Functional Completeness}

\subsec{Truth-Functional Completeness}{

\begin{block}{Definition}

A set of connectives is \emph{truth-functionally complete} if every
truth function is expressible by a sentence containing only those
connectives.

\end{block}

}

\subsec{$\{\land, \lor\}$ Not Truth-Functionally Complete}{

\bit
\item $\{\land, \lor\}$ is not truth-functionally complete
\item Remember: To be truth functionally complete, \emph{every} truth
  function would have to be expressible using only $\land$ and $\lor$
\item Which 2-place truth-functions can be expressed using $\land$ and
  $\lor$?
\item Not this one:
\[
\begin{array}{cc|c}
\T & \T & \F \\
\T & \F & \T \\
\F & \T & \T \\
\F & \F & \F
\end{array}
\]
  \eit

}

\subsec{Proof by Induction}{

\bit
\item Sometimes need to prove something for all sentences
\item E.g., ``every sentence containing only $\land$ and $\lor$ expresses a truth function \emph{other than} $t_\mathit{XOR}$''
\item Proof by Induction
\bit
\item Show that it holds for atomic sentences
\item Suppose sentences $\sf P$, $\sf Q$ have the property and
\item Show that it then also holds for $\sf\lnot P$, $\sf P \land Q$, $\sf P \lor Q$, etc.
\eit
\eit

}

\subsec{Example: Proof by Induction}{

\bit

\item Claim: every sentence contains an even number of parentheses
\item Proof strategy:
\bit
\item Show that every atomic sentence contains an even number of parentheses
\bit
\item $\sf B(a, b, c)$, $\bot$
\eit
\item Show that if $\sf P$ contains an even number of parentheses, so does $\sf\lnot P$
\item Show that if $\sf P$ and $\sf Q$ contain an even number of parentheses, so do
\[\sf (P \land Q), (P \lor Q), (P \to Q), (P \leftrightarrow Q) \]
\eit
\eit

}

\subsec{$\{\land, \lor\}$ Not Truth-Functionally Complete}{

\bits
\item Any sentence containing only $A_1$, $A_2$, $\land$, $\lor$
  expresses a truth function $t$ with $t(\T, \T) = \T$.

\item Proof.

\bits
\item Atomic sentences: $A_1$, $A_2$: express $t_1$, $t_2$.
\item Suppose $P$, $Q$ are sentences which
  contain only $A_1$, $A_2$, $\land$, $\lor$ and express truth
  functions $t$, $t'$ with $t(\T, \T) = t'(\T, \T) = \T$
\item $P \land Q$ expresses truth function $s$ with $s(\T, \T) = t_\land(t(\T, \T), t'(\T, \T)) = \T$
\item $P \lor Q$ expresses truth function $s$ with $s(\T, \T) = t_\lor(t(\T, \T), t'(\T, \T)) = \T$
\eit
\item Hence, no sentence containing only $\land$, $\lor$ can express a
  truth function $t$ with $t(\T, \T) = \F$.
  \eit
}

\subsec{$\{\land, \lor, \lnot\}$ is Truth-functionally Complete}{
\[
\begin{array}{lll|l@{\qquad}l}
A_1 & A_2 & A_3 & t_\mathit{ODD} & S \\
\hline
\T & \T & \T & \T & \uncovers{2-}{(A_1 \land A_2 \land A_3)} \uncovers{6-}{{}\lor{}}\\
\T & \T & \F & \F \\
\T & \F & \T & \F \\
\T & \F & \F & \T & \uncovers{3-}{(A_1 \land \lnot A_2 \land \lnot A_3)} \uncovers{6-}{{}\lor{}}\\
\F & \T & \T & \F \\
\F & \T & \F & \T & \uncovers{4-}{(\lnot A_1 \land A_2 \land \lnot A_3)} \uncovers{6-}{{}\lor{}}\\
\F & \F & \T & \T & \uncovers{5-}{(\lnot A_1 \land \lnot A_2 \land A_3)} \uncovers{6-}{{}\lor{}}\\
\F & \F & \F & \F
\end{array}\]
}

\subsec{$\{\land, \lor, \lnot\}$ is Truth-Functionally Complete}{

\bit
\item Each line makes one, and only one, conjunction true, e.g.,
\item $\lnot A_1 \land A_2 \land \lnot A_3$ is true in, and only in, line \F\ \T\ \F
\item Combine using $\lor$: make $S$ true in all (and only) the lines where it is supposed to be true
\eit

}

\subsec{The ``neither\dots nor \dots'' connective: $\downarrow$}{

\[
\begin{array}{cc|c}
P & Q & (P \downarrow Q)\\
\hline
\T & \T & \F\\
\T & \F & \F\\
\F & \T & \F\\
\F & \F & \T
\end{array}
\]

}

\subsec{$\{\downarrow\}$ is Truth-Functionally Complete}{

\bit
\item Already know that $\{\lnot, \land, \lor\}$ is truth-functionally
  complete, i.e.,
\item Every truth-function can be expressed using only $\lor$, $\land$, $\lnot$
\item To show $\downarrow$ is truth-functionally complete, suffices to
  show that \emph{every sentence containing only $\lnot$, $\lor$, $\land$ is
  tautologically equivalent to one containing only $\downarrow$}
\item For that, it suffices to show that any negated sentence,
  conjunction, disjunction, can be expressed using only $\downarrow$
\eit

}

\subsec{Expressing $\lnot$ Using $\downarrow$}{

\begin{columns}
\begin{column}{3cm}
\[\begin{array}{cc|c}
P & Q & (P \downarrow Q)\\
\hline
\T & \T & \F\\
\T & \F & \F\\
\F & \T & \F\\
\F & \F & \T
\end{array}\]
\end{column}
\begin{column}{7cm}
\bits
\item Note how $P \downarrow Q$ is \F{} in the first line and \T{} in the last (when $P$ and $Q$ have same truth value)
\item So $P \downarrow P$ is \F{} if $P$ is \T, and \T{} if $P$ is \F, i.e., \[
\lnot P \Leftrightarrow (P \downarrow P)
\]
\eit
\end{column}
\end{columns}

}

\subsec{Expressing $\lor$ Using $\downarrow$}{

\begin{columns}
\begin{column}{3cm}
\[\begin{array}{cc|c}
P & Q & (P \downarrow Q)\\
\hline
\T & \T & \F\\
\T & \F & \F\\
\F & \T & \F\\
\F & \F & \T
\end{array}\]
\end{column}
\begin{column}{7cm}
\bits
\item $P \downarrow Q$ is the ``neither \dots nor'' connective, which can also
be expressed as $\lnot(P \lor Q)$, i.e.,
\[ \lnot(P \lor Q) \Leftrightarrow P \downarrow Q \]
\item Negate both sides:
\[
P \lor Q \Leftrightarrow \lnot(P \downarrow Q)
\]
\item Apply what we figured out in last slide:
\[
P \lor Q \Leftrightarrow (P \downarrow Q)\downarrow(P \downarrow Q)
\]
\eit
\end{column}
\end{columns}

}

\subsec{Expressing $\land$ Using $\downarrow$}{

\begin{columns}
\begin{column}{3cm}
\[\begin{array}{cc|c}
P & Q & (P \downarrow Q)\\
\hline
\T & \T & \F\\
\T & \F & \F\\
\F & \T & \F\\
\F & \F & \T
\end{array}\]
\end{column}
\begin{column}{7cm}
\bits
\item $P \downarrow Q$ is the ``neither \dots nor'' connective, which can also
be expressed as $\lnot P \land \lnot Q$, i.e.,
\[ (\lnot P \land \lnot Q) \Leftrightarrow P \downarrow Q \]
\item Equivalence holds for \emph{all sentences} $P$, $Q$, so also if we replace $P$ by $\lnot R$ and $Q$ by $\lnot S$:
\[
\lnot\lnot R \land \lnot\lnot S \Leftrightarrow (\lnot R \downarrow \lnot S)
\]
\item Delete $\lnot\lnot$'s, and express $\lnot$ using $\downarrow$:
\[
R \land S \Leftrightarrow (R \downarrow R)\downarrow(S \downarrow S)
\]
\eit
\end{column}
\end{columns}

}

\subsec{Truth-Functionally Complete Sets of Connectives}{

\bit
\item De Morgan's Law: $\land$ can be expressed by $\lor$ and $\lnot$
\item Similarly: $\lor$ can be expressed by $\land$, $\lnot$
\item So $\{\lor, \lnot\}$ and $\{\land, \lnot\}$ are
  truth-functionally complete
\item $\{\to, \bot\}$ is truth-functionally complete (HW)
\item $\{\to, \lnot\}$ is truth-functionally complete
\item No other sets of connectives that don't contain one of these sets are complete
\item ``Neither \dots nor'' is truth-functionally complete by itself
\item ``Not both'' connective is truth-functionally complete by itself (HW)
\item No other 2-place connectives are complete by themselves
\eit

}

\end{document}


\lab{Algorithm}{Kruskal's and Prim's Algorithm}{Kruskal's and Prim's Algorithm}
\label{Ch:Kruskal}

\objective{This section teaches about how to find minimum spanning tree for a connected weighted graph using Kruskal's and Prim's Algorithm.}

\section*{Weight Graphs and Spanning trees}

%Lab \ref{Markov}

Remember that a graph is composed of two sets: a set of nodes and a set of edges that connect these nodes.

\begin{figure}[H]
\includegraphics[width = .4\textwidth]{graph1.pdf}
\caption{An example of a graph}
\end{figure}

A graph is directed if connections are uni-directional, and undirected if they are bi-directional. The above graphic shows an undirected graph. A weighted graph is a graph where each edge has a value associated with it. A connected graph is there is a path,or a set of edges, that connect every two nodes together. We can write a matrix that describes this type of graph. We let each row of our matrix represent our starting point and each column represent our destination. We put the value of the edge if there is a path and a 0 if there is not. For the above graph we generate the following matrix:

\[
A = \begin{pmatrix}
0 & 1 & 0 & 0 & 0 & 0\\
1 & 0 & 1 & 0 & 0 & 1\\
0 & 1 & 0 & 1 & 1 & 1\\
0 & 0 & 1 & 0 & 1 & 0\\
0 & 0 & 1 & 1 & 0 & 1\\
0 & 1 & 1 & 0 & 1 & 0
\end{pmatrix}
\]

This matrix is called an adjacency matrix. Note that this matrix is symmetric, since the graph is undirected.

Another way to store the graph is as a list of the edges and their weights that are connected (for an unweighted graph you can just store the edges). It would look like this:

$[('A', 'B'),
 ('B', 'C'),
 ('B', 'E'),
 ('B', 'F'),
 ('C', 'D'),\\
 ('C', 'E'),
 ('C', 'F'),
 ('D', 'E'),
 ('E', 'F')]$\\
A third entry would store the wieght.

For this lab, we will be forcusing on undirecting weighted graphs.

A spanning tree of a connected graph G is is an undirected graph that contains all the nodes of G, a subset of the edges, and contains no cycles. A cycle, for undirected graphs, is a path where you start and end on the same node without crossing any edge more than once.

\begin{figure}[H]
\includegraphics[width = .4\textwidth]{graph2.pdf}
\caption{An example of an minimal spaning tree for the previous graph}
\end{figure}
The minimun spanning tree (MST) of a wieghted, undirected graph is the spanning tree where the sum of the weights of the subset of edges is less than or equal to the sum of the weights of every other spanning tree. Both Kruskal's and Prim's Algorithm are methods that find the minimal spanning tree of a weighted directed graph.
A spanning tree of a connected graph G is is an undirected graph that contains all the nodes of G, a subset of the edges, and contains no cycles. A cycle, for undirected graphs, is a path where you start and and on the same node without using an edge more than once.
\begin{figure}[H]
\includegraphics[width = .4\textwidth]{graph3.pdf}
\caption{The path is red marks a cycle}
\end{figure}
The minimum spanning tree of a weighted, undirected graph is the spanning tree where the sum of the weights of the subset of edges is less than or equal to the sum of the weights of every other spanning tree. Both Kruskal's and Prim's Algorithm for the minimal spanning tree of a weighted directed graph.
\begin{figure}[H]
\includegraphics[width = .4\textwidth]{graph4.pdf}
\caption{A weighted directed graph}
\end{figure}
\begin{figure}[H]
\includegraphics[width = .4\textwidth]{graph5.pdf}
\caption{The MST of that graph}
\end{figure}

\section*{Kruskal's algorithm}

Given a weighted directed graph G with $n$ nodes, Kruskal's algorithm finds the minimun by: first sorting the edges from smallest to largest. Then starting from the smallest, the alogrithm adds the edge to the tree as long as the addition of the new edge does not create a cycle. When the $n-1$ edges have been added the algorithm stops. (put in an example)
Given a weighted directed graph G with $n$ nodes, Kruskal's algorithm finds the minimum by sorted the edges from smallest to largest. Starting from the smallest it adds the edges in order to a tree as long as the addition of a new edge does not create a cycle. When the $n-1$ edges have been added the algorithm stops. The fastest way to do this is as follows:
\flushleft
\begin{itemize}
\item Sort the edges by weight in descending order.
\item Initialize pointers to trees
\item For each edge $e=\{x, y\}$, until $n-1$ edges added
\begin{itemize}
\item if $x$ and $y$ are not in the same tree (i.e. don’t have the same root), add $e$
\item merge smaller tree (of $x$ or $y$) into larger tree
\end{itemize}
\end{itemize}

\begin{problem}
Write Kruskal's algorithm. Test your algorithm on random symmetric matrices. Use the graph above and the data from MSTdata.npy to test your tree. Use np.load("MSTdata.npy") to get it. Use the formChanger function to put it in the right form.
\end{problem}

\section*{Prim's algorithm}

Prim's algorithm accomplishes the same thing as Kruskal's algorithm, but with a different approach. Starting from any node it adds the edge with the least weight. Those two nodes form a tree. It than adds edges with the least weight to any node in the tree to any node not in the tree until all the nodes have been added. The fastest way to do this is as follows:
\flushleft
\begin{itemize}
\item Start with the smallest-weight edge $e = \{x, y\}$ in $S$
\begin{itemize}
\item discard $x$ and $y$ from the list of nodes to consider
\item for both $x$ and $y$, find the closest node, if any (among the non-discarded nodes), and keep them and their edge weights
\end{itemize}
\item Until $n-1$ edges have been added
\begin{itemize}
\item find the edge $e = \{x, y\}$ such that
\begin{itemize}
\item $x$ is in $S$ and $y$ is not in $S$
\item $e$ is the smallest weighted edge left
\end{itemize}
\item add $e$ and $y$ to $S$
\item discard y from the list of nodes to consider, and for both $x$ and $y$, find the closest node, if any (among the non-discarded nodes), and keep them and their edge weights
\end{itemize}
\end{itemize}

\begin{problem}
Write Prim's algorithm. Test you tree with the same data as the previous problem. Compare the speed of Prim's algorithm with Kruskal's algorithm.
\end{problem}

\section*{Complexity}
Let $n$ be the number of nodes and $m$ be the number of edges. Kruskal's Algorithm is O($m\log{n}$) which in the worst case is O($n^2\log{n}$). Prims's Algorithm is O($n^2$). So when the adjacent martix is sparse Kruskal's is better and when it is dense Prim's is better.


Although the traditional IT security layer provided to Industrial Control Systems offers a great deal of protection, security managers often overlook the possibility of a potential stealthy attacks at the process level. In the paper\supercite{Pasad:2018}, 'Truth Will Out: Departure-Based Process-Level Detection of Stealthy Attacks on Control Systems' the authors bring out a novel approach to detecting stealthy attacks on the control system through a process-aware methodology which they call - PASAD. It enables early detection in the subtle variation of process behavior, thus averting strategic adversaries from maliciously manipulating the industrial process within the noise level. The original implementation for the PASAD algorithm was in Matlab. In this replication, I use Python, and popular visualization library, Matplotlib, to obtain the results claimed.

\section*{Introduction}

A typical control system utilizes sensors, actuators, and controllers to regulate some controlled process. Sensor devices measure some physical property and communicate the measurements to a controller (e.g., a PLC), which based on a control algorithm, correspondingly issues commands to actuators
(e.g., control valves) that directly manipulate the physical process based on the received commands. A physical process may exhibit a noisy behavior by nature. A strategic adversary takes advantage of this fact and manipulates the process to stay within the noise level to remain undetected, thus leading to a potential degradation or suboptimal performance or, in the worst scenario, a complete failure of control system through cascading effect between various control loops. PASAD is capable of finding such attacks by utilizing a technique called 'singular spectrum analysis,' a model-free time-series analysis tool.

\section*{Four Steps of PASAD}
Consider a univariate real-valued time series of sensor measurements

\begin{center}
$\mathcal{T}=x_1, x_2,...,x_N, x_N+1$
\end{center}

\subsection*{Step 1: Embedding timeseries data into a trajectory matrix $X$}

Let $L$ be an integer referred to as lag parameter, which is typically close to N/2. Initial $L$ points of $\mathcal{T}$ form the first column vector of Trajectory Matrix $X$. Then the lag vector moves to the next point, similar to the concept of a `moving window' until it covers all the points, forming corresponding column vectors for each. Final trajectory matrix of time series data is as follows:-

$$
X = \begin{bmatrix}
	x_1 & x_2 & \dots  & x_K \\
	x_2 & x_3 & \dots  & x_{K+1} \\
	\vdots & \vdots & \ddots & \vdots \\
	x_L & x_{L+1} & \dots  & x_N
\end{bmatrix}
$$

\subsection*{Step 2: Singular Value Decomposition}

To obtain the noise-reduced version of the above trajectory matrix, we perform singular value decomposition(SVD) on the matrix and obtain the L eigenvectors $u_1, u_2,...,u_L$ and their corresponding eigenvalues. From a scree plot, we determine the cumulative contribution of each eigenvalue to determine the top $r$ contributors to the time series. $r$ represents the statistical dimension, which is the number of degrees of freedom that account for the deterministic variability.

\subsection*{Step 3: Creating a projection matrix}

After obtaining the statistical dimension $r$, we proceed to create the projection matrix. By performing SVD, we have effectively obtained the elementary matrices of $X$ that represents the optimal separation of the component in the trajectory space. Projection  matrix $P$ is obtained through reconstructing/ summing up the top $r$ elementary matrices\supercite{kaggle}. In this step, we also calculate the centroid of this 'reduced' euclidean space and the distance between the centroid and farthest lag vector. This distance is the threshold for determining the anomaly in future lag vectors.

\subsection*{Step 4: Distance Tracking and Anomaly Prediction}

As the lag vector moves forward after completion of threshold distance, we continue projecting lag vectors to the new subspace and see if it is within the threshold distance from the centroid. If yes, it is a usual case, and otherwise, we raise the alarm.

\section*{The Isometry Trick}

The essence of the original paper\supercite{Pasad:2018} is what is called \textbf{the isometry trick}.  In a nutshell, for an arbitrary vector $x$ in $\mathbf{R}^L$, computing the norm of the vector $U^Tx$ has the effect of implicitly projecting $x$ onto the subspace $\mathcal{L}^r$ and computing its norm there. Further reading on the Isometry trick is in Para 2.6 of the original article. By this method, authors have achieved close to linear complexity in the computation, ideal for implementation in low-end devices such as PLCs.

\section*{Implementation}

The original implementation of PASAD algorithm is in Matlab, and the source code is available \href{https://github.com/mikeliturbe/pasad}{here}. Authors have validated the algorithm with data from three different scenarios namely, The Tennessee-Eastman Process, SWaT Dataset, A Water-Distribution Plant. This reproduction is for the first scenario. Reproduced plots are given below for easy reference.

\subsection*{Scenario 1. (Stealthy Attack)}

Stealthy Attack compromising the control variable XMV(9) detected in sensor variable XMEAS(5).

\begin{figure}[H]
	\centering
	\begin{subfigure}[t]{0.45\textwidth}
		\centering
		\includegraphics[width=\textwidth]{imgs/sa1orig.png}
		\caption{Original Plot}\label{fig:1a}
	\end{subfigure}
	\qquad
	\begin{subfigure}[t]{0.45\textwidth}
		\centering
		\includegraphics[width=\textwidth]{imgs/sa1re.png}
		\caption{Reproduced plot}\label{fig:1b}
	\end{subfigure}
	\caption{Departure score plot for stealthy attack 1 scenario}\label{fig:1}
\end{figure}

\subsection*{Scenario 2. (Stealthy Attack)}

Stealthy attack SA2 compromising the control variable XMV(6) detected in sensor variable XMEAS(10).

\begin{figure}[H]
	\centering
	\begin{subfigure}[t]{0.45\textwidth}
		\centering
		\includegraphics[width=\textwidth]{imgs/sa2orig.png}
		\caption{Original Plot}\label{fig:1a}
	\end{subfigure}
	\qquad
	\begin{subfigure}[t]{0.45\textwidth}
		\centering
		\includegraphics[width=\textwidth]{imgs/sa2re.png}
		\caption{Reproduced plot}\label{fig:1b}
	\end{subfigure}
	\caption{Departure score plot for stealthy attack 2 scenario}\label{fig:1}
\end{figure}

\subsection*{Scenario 3. (Stealthy Attack)}

Stealthy attack SA3 compromising the control variable XMEAS (10) detected in sensor variable XMEAS(9).

\begin{figure}[H]
	\centering
	\begin{subfigure}[t]{0.45\textwidth}
		\centering
		\includegraphics[width=\textwidth]{imgs/sa3orig.png}
		\caption{Original Plot}\label{fig:1a}
	\end{subfigure}
	\qquad
	\begin{subfigure}[t]{0.45\textwidth}
		\centering
		\includegraphics[width=\textwidth]{imgs/sa3re.png}
		\caption{Reproduced plot}\label{fig:1b}
	\end{subfigure}
	\caption{Departure score plot for stealthy attack 3 scenario}\label{fig:1}
\end{figure}

\subsection*{Scenario 4. (Direct Attack)}

Direct Damage attack DA1 compromising the control variable XMV(10) detected in sensor variable XMEAS(15).

\begin{figure}[H]
	\centering
	\begin{subfigure}[t]{0.45\textwidth}
		\centering
		\includegraphics[width=\textwidth]{imgs/da1orig.png}
		\caption{Original Plot}\label{fig:1a}
	\end{subfigure}
	\qquad
	\begin{subfigure}[t]{0.45\textwidth}
		\centering
		\includegraphics[width=\textwidth]{imgs/da1re.png}
		\caption{Reproduced plot}\label{fig:1b}
	\end{subfigure}
	\caption{Departure score plot for direct attack 1 scenario}\label{fig:1}
\end{figure}

\subsection*{Scenario 5. (Direct Attack)}

Direct Damage attack DA2 compromising the control variable XMEAS(7) detected in sensor variable XMEAS(5).

\begin{figure}[H]
	\centering
	\begin{subfigure}[t]{0.45\textwidth}
		\centering
		\includegraphics[width=\textwidth]{imgs/da2orig.png}
		\caption{Original Plot}\label{fig:1a}
	\end{subfigure}
	\qquad
	\begin{subfigure}[t]{0.45\textwidth}
		\centering
		\includegraphics[width=\textwidth]{imgs/da2re.png}
		\caption{Reproduced plot}\label{fig:1b}
	\end{subfigure}
	\caption{Departure score plot for direct attack 2 scenario}\label{fig:1}
\end{figure}

\section*{Results and Observations}
Detection of process manipulations with respect to the 'Tennessee-Eastman Process' dataset, PASAD algorithm was successfully validated. A minor variation in the detection plot is attributable to the selection of statistical dimensions from the scree plot. During reproduction, the first eigenvalue was observed to be disproportionately high due to which the statistical dimensions were selected as 1 to obtain a successful detection phase. The reason for such behavior is attributable to the inherent characteristic of a trajectory matrix, as per clarification by author \href{https://github.com/mikeliturbe/pasad/issues/1}{here}. Variation in screeplot with and without top eigenvalue is as shown in figure \ref{fig:scree}

\begin{figure}[h]
	\centering
	\begin{subfigure}[t]{0.45\textwidth}
		\centering
		\includegraphics[width=\textwidth]{imgs/scree1.png}
		\caption{All eigenvalues}\label{fig:1a}
	\end{subfigure}
	\qquad
	\begin{subfigure}[t]{0.45\textwidth}
		\centering
		\includegraphics[width=\textwidth]{imgs/scree2.png}
		\caption{Eigenvalues excluding first}\label{fig:1b}
	\end{subfigure}
	\caption{Scree plot of top eigen values}\label{fig:scree}
\end{figure}

\section*{Acknowledgement}

This work is undertaken as part of course CS631A-Cybersecurity for Critical Infrastructure at IIT Kanpur by \href{https://security.cse.iitk.ac.in/node/96}{Prof. Sandeep Shukla}. I would also like to thank \href{https://www.labri.fr/perso/nrougier/}{Nicolas P. Rougier} for letting me know about \href{https://rescience.github.io}{ReScience C} and \href{https://www.cse.iitk.ac.in/users/pvcharan/} {P.V. Sai Charan} for collaborating.


\documentclass{article}
\usepackage{hyperref}
\usepackage{amsmath}

\title{Partial least squares}
\author{John Reid}

\begin{document}

\section{Partial least squares model}

The partial least
squares model is presented as a latent variable model in Kevin Murphy's 2012
book \href{https://probml.github.io/pml-book/}{Machine Learning: A
Probabilistic Perspective}.

\begin{eqnarray}
  p(z) &=& \mathcal{N}(z|0, I) \\
  p(v|z, W, \mu, \sigma) &=& \mathcal{N}(v|W z + \mu, \sigma^2 I)
\end{eqnarray}
where
\begin{eqnarray}
  W &=& \begin{pmatrix}
      W_y & 0 \\
      W_x & B_x \\
    \end{pmatrix} \\
  z &=& (z^s; z^x) \\
  v &=& (y; x) \\
  \mu &=& (\mu_y; \mu_x).
\end{eqnarray}
Marginalising $z$ gives
\begin{eqnarray}
  p(v|W, \mu, \sigma)
  &=& \int \mathcal{N}(v|W z + \mu, \sigma^2 I) \mathcal{N}(z|0, I)\,dz \\
  \label{eqn:joint_xy}
  &=& \mathcal{N}(v|\mu, W W^T + \sigma^2 I)
\end{eqnarray}
Conditioning on $x$ gives
\begin{eqnarray}
  p(y|x) &=& \mathcal{N}(y|m_{y|x}, S_{y|x})
\end{eqnarray}
where
\begin{eqnarray}
  C &=& {(B_x B_x^T + W_x W_x^T + \sigma^2 I)}^{-1} \\
  m_{y|x} &=& \mu_y + W_y W_x^T C (x - \mu_x) \\
  S_{y|x} &=& \sigma^2 I + W_y W_y^T - W_y W_x^T C W_x W_y^T
\end{eqnarray}

Suppose we now obtain $N$ independent observations from the model
\begin{eqnarray}
  v_n = (y_n; x_n),\quad 1 \le n \le N.
\end{eqnarray}
We wish to estimate $W, \mu \text{ and } \sigma$. We can do this by maximising the likelihood of the data
$v = x, y$~(\ref{eqn:joint_xy}) using stochastic gradient descent.

One way to validate that our implementation works is to sample data from the model and compare the estimated
parameters to those that we used to sample with. Suppose we sample $v_n$ from our model parameterised by
$\sigma^*, \mu^*_x, \mu^*_y, W^*_y, W^*_x, B^*_x$.
We can estimate $\hat{\sigma}, \hat{\mu}_x, \hat{\mu}_y, \hat{W}_y, \hat{W}_x, \hat{B}_x$ that maximise
\begin{eqnarray}
  \label{eqn:log_joint}
  \sum_n \log p(v|\sigma, \mu, W_y, W_x, B_x)
\end{eqnarray}
We would love to compare the estimated parameters to the underlying parameters but unfortunately (\ref{eqn:log_joint})
is invariant to orthonormal transformations of the $W_y, W_x, B_x$. That is
\begin{eqnarray}
  \sum_n \log p(v_n|\sigma, \mu, W_y U_s, W_x U_s, B_x U_x)
  &=&
  \sum_n \log p(v_n|\sigma, \mu, W_y, W_x, B_x)
\end{eqnarray}
for any orthonormal square matrices (of suitable dimension) $U_s, U_x$. We can see this as $W$ only appears
as $W W^T$ in the likelihood and $W W^T = W U U^T W^T$ for any orthonormal $U$.
\end{document}


\section{The \merge algorithm}
\label{sec:merge}

Our version of the \merge algorithm from the \cxx standard library\cite[28.7.5]{cxx-17-draft}
has the following signature.

\begin{lstlisting}[style = acsl-block]

void
merge(const value_type* a, size_type n,
      const value_type* b, size_type m,
      value_type* result);
\end{lstlisting}

The \merge algorithm is a part of the \emph{merge sort} algorithm.
It operates on the second step to merge two increasingly ordered sub-arrays into a new one.
The algorithm merges two increasingly ordered arrays
\inl{a[0..n-1]} and \inl{b[0..m-1]}, respectively.
The merged values are stored in the output array that starts at
\inl{result} which must be able to hold $m+n$ values of both input arrays.

\subsection{Formal specification of \merge}

The following listing~\ref{lst:merge:spec} shows the specification of \merge.
The specification expects the input arrays of the proper size and in increasing order
and the output array of enough size to contain all the input elements.
The input arrays should not overlap with the output array.
%
In the current edition of this guide, we prove only that the
resulting array is in increasing order.
Future editions will contain additional postconditions stating that the
result array consists of reordered input elements and the stability of the algorithm,
i.e., the same elements of the input arrays preserve their order in the output array.

\input{Listings/merge.h.tex}

%\clearpage

\subsection{More Lemmas on \WeaklyIncreasing}
\label{sec:WeaklyIncreasingLemmas}

We introduce in the following listing several lemmas about \logicref{WeaklyIncreasing}
that are helpful for the verification of \merge.

\begin{itemize}
\item
Lemma \logicref{WeaklyIncreasingShrink} allows to restrict the property
\emph{weakly increasing} onto a sub-array.

\item
Lemma \logicref{WeaklyIncreasingAddElement} defines the way a weakly
increasing array can be constructed.

\item
Lemma \logicref{WeaklyIncreasingShift} is used to handle
pointer arithmetic with respect to the \WeaklyIncreasing property.

\item
Lemmas  \logicref{WeaklyIncreasingUnchanged} and \logicref{WeaklyIncreasingEqual} state
that if an array is weakly increasing,
then another array (or the same array at another program point),
whose elements are in a one-to-one correspondence with the
original array, is also weakly increasing.

\item
Lemma \logicref{WeaklyIncreasingJoin} defines the conditions that two
consequent weakly increasing ranges
can be viewed as merged weakly increasing range.
\end{itemize}

\input{Listings/WeaklyIncreasingLemmas.acsl.tex}

%\clearpage

\subsection{Implementation of \merge}

The implementation of \merge, shown in the next listings is straightforward.
The algorithm operates by traversing both input arrays.
On each iteration it writes the smaller of both elements into the result array,
thus constructing an increasingly ordered array.
If the algorithm reaches the end of one of the input arrays,
it just copies the rest elements of the other array to the result array.
The listing contains a number of assertions to trigger an application of lemmas by
the provers.
The \inl{while} loop traverses the input arrays and constructs, in accordance with
\logicref{WeaklyIncreasingAddElement}, the resulting weakly increasing array.
After the loop, the algorithm copies the remaining elements to the resulting array.

\begin{listing}[hbt]
\begin{minipage}{\textwidth}
\lstinputlisting[linerange={1-33}, style=acsl-block, frame=single]{Source/merge.c}
\end{minipage}
\caption{\label{lst:merge-impl1}Implementation of \merge (1)}
\end{listing}

\index[examples]{merge@\texttt{merge}}

%\clearpage

We also use the following lemmas to support the verification of several properties.

\begin{itemize}
\item
Lemma \logicref{WeaklyIncreasingEqual} is used to show that the
copied elements from one of the input arrays preserve the \WeaklyIncreasing property.

\item
Lemma \logicref{WeaklyIncreasingJoin} is used
to
extend the \WeaklyIncreasing
property of the two sub-ranges of the resulting array over the whole range.
In order to deal with pointer arithmetic we employ Lemma \WeaklyIncreasingShift.

\item
Finally, Lemma \logicref{WeaklyIncreasingIncreasing}
is used to prove the output array is in increasing order.
\end{itemize}

\begin{listing}[hbt]
\begin{minipage}{\textwidth}
\lstinputlisting[linerange={34-100}, style=acsl-block, frame=single]{Source/merge.c}
\end{minipage}
\caption{\label{lst:merge-impl2}The Implementation of \merge (2)}
\end{listing}

\index[examples]{merge@\texttt{merge}}

\clearpage


%---------------------------Volume-----------------------------
\section{Pyramid Volume}

The pyramid volume metric is simply
\[
q = V.
\]

\othermetrictable{volume}%
{$L^3$}%                                      Dimension
{$[0,DBL\_MAX]$}%                             Acceptable range
{$[-DBL\_MAX,DBL\_MAX]$}%                     Normal range
{$[-DBL\_MAX,DBL\_MAX]$}%                     Full range
{$\frac{1}{3\sqrt{2}}$}%                      Unit element
{--}%                                         Citation
{v\_pyramid\_volume}%                         Verdict function name