%* glpk04.tex *%

\chapter{Advanced API Routines}

\section{Background}
\label{basbgd}

Using vector and matrix notations the LP problem (1.1)---(1.3) (see
Section \ref{seclp}, page \pageref{seclp}) can be stated as follows:

\noindent
\hspace{.5in} minimize (or maximize)
$$z=c^Tx_S+c_0\eqno(3.1)$$
\hspace{.5in} subject to linear constraints
$$x_R=Ax_S\eqno(3.2)$$
\hspace{.5in} and bounds of variables
$$
\begin{array}{l@{\ }c@{\ }l@{\ }c@{\ }l}
l_R&\leq&x_R&\leq&u_R\\
l_S&\leq&x_S&\leq&u_S\\
\end{array}\eqno(3.3)
$$
where:

$x_R=(x_1,\dots,x_m)$ is the vector of auxiliary variables;

$x_S=(x_{m+1},\dots,x_{m+n})$ is the vector of structural variables;

$z$ is the objective function;

$c=(c_1,\dots,c_n)$ is the vector of objective coefficients;

$c_0$ is the constant term (``shift'') of the objective function;

$A=(a_{11},\dots,a_{mn})$ is the constraint matrix;

$l_R=(l_1,\dots,l_m)$ is the vector of lower bounds of auxiliary
variables;

$u_R=(u_1,\dots,u_m)$ is the vector of upper bounds of auxiliary
variables;

$l_S=(l_{m+1},\dots,l_{m+n})$ is the vector of lower bounds of
structural variables;

$u_S=(u_{m+1},\dots,u_{m+n})$ is the vector of upper bounds of
structural variables.

From the simplex method's standpoint there is no difference between
auxiliary and structural variables. This allows combining all these
variables into one vector that leads to the following problem
statement:

\noindent
\hspace{.5in} minimize (or maximize)
$$z=(0\ |\ c)^Tx+c_0\eqno(3.4)$$
\hspace{.5in} subject to linear constraints
$$(I\ |-\!A)x=0\eqno(3.5)$$
\hspace{.5in} and bounds of variables
$$l\leq x\leq u\eqno(3.6)$$
where:

$x=(x_R\ |\ x_S)$ is the $(m+n)$-vector of (all) variables;

$(0\ |\ c)$ is the $(m+n)$-vector of objective
coefficients;\footnote{Subvector 0 corresponds to objective
coefficients at auxiliary variables.}

$(I\ |-\!A)$ is the {\it augmented} constraint
$m\times(m+n)$-matrix;\footnote{Note that due to auxiliary variables
matrix $(I\ |-\!A)$ contains the unity submatrix and therefore has full
rank. This means, in particular, that the system (3.5) has no linearly
dependent constraints.}

$l=(l_R\ |\ l_S)$ is the $(m+n)$-vector of lower bounds of (all)
variables;

$u=(u_R\ |\ u_S)$ is the $(m+n)$-vector of upper bounds of (all)
variables.

By definition an {\it LP basic solution} geometrically is a point in
the space of all variables, which is the intersection of hyperplanes
corresponding to active constraints\footnote{A constraint is called
{\it active} if at a given point it is satisfied as equality, otherwise
it is called {\it inactive}.}. The space of all variables has the
dimension $m+n$, therefore, to define some basic solution we have to
define $m+n$ active constraints. Note that $m$ constraints (3.5) being
linearly independent equalities are always active, so remaining $n$
active constraints can be chosen only from bound constraints (3.6).

A variable is called {\it non-basic}, if its (lower or upper) bound is
active, otherwise it is called {\it basic}. Since, as was said above,
exactly $n$ bound constraints must be active, in any basic solution
there are always $n$ non-basic variables and $m$ basic variables.
(Note that a free variable also can be non-basic. Although such
variable has no bounds, we can think it as the difference between two
non-negative variables, which both are non-basic in this case.)

Now consider how to determine numeric values of all variables for a
given basic solution.

Let $\Pi$ be an appropriate permutation matrix of the order $(m+n)$.
Then we can write:
$$\left(\begin{array}{@{}c@{}}x_B\\x_N\\\end{array}\right)=
\Pi\left(\begin{array}{@{}c@{}}x_R\\x_S\\\end{array}\right)=\Pi x,
\eqno(3.7)$$
where $x_B$ is the vector of basic variables, $x_N$ is the vector of
non-basic variables, $x=(x_R\ |\ x_S)$ is the vector of all variables
in the original order. In this case the system of linear constraints
(3.5) can be rewritten as follows:
$$(I\ |-\!A)\Pi^T\Pi x=0\ \ \ \Rightarrow\ \ \ (B\ |\ N)
\left(\begin{array}{@{}c@{}}x_B\\x_N\\\end{array}\right)=0,\eqno(3.8)$$
where
$$(B\ |\ N)=(I\ |-\!A)\Pi^T.\eqno(3.9)$$

\newpage

Matrix $B$ is a square non-singular $m\times m$-matrix, which is
composed from columns of the augmented constraint matrix corresponding
to basic variables. It is called the {\it basis matrix} or simply the
{\it basis}. Matrix $N$ is a rectangular $m\times n$-matrix, which is
composed from columns of the augmented constraint matrix corresponding
to non-basic variables.

From (3.8) it follows that:
$$Bx_B+Nx_N=0,\eqno(3.10)$$
therefore,
$$x_B=-B^{-1}Nx_N.\eqno(3.11)$$
Thus, the formula (3.11) shows how to determine numeric values of basic
variables $x_B$ assuming that non-basic variables $x_N$ are fixed on
their active bounds.

The $m\times n$-matrix
$$\Xi=-B^{-1}N,\eqno(3.12)$$
which appears in (3.11), is called the {\it simplex
tableau}.\footnote{This definition corresponds to the GLPK
implementation.} It shows how basic variables depend on non-basic
variables:
$$x_B=\Xi x_N.\eqno(3.13)$$

The system (3.13) is equivalent to the system (3.5) in the sense that
they both define the same set of points in the space of (primal)
variables, which satisfy to these systems. If, moreover, values of all
basic variables satisfy to their bound constraints (3.3), the
corresponding basic solution is called {\it (primal) feasible},
otherwise {\it (primal) infeasible}. It is understood that any (primal)
feasible basic solution satisfy to all constraints (3.2) and (3.3).

The LP theory says that if LP has optimal solution, it has (at least
one) basic feasible solution, which corresponds to the optimum. And the
most natural way to determine whether a given basic solution is optimal
or not is to use the Karush---Kuhn---Tucker optimality conditions.

\def\arraystretch{1.5}

For the problem statement (3.4)---(3.6) the optimality conditions are
the following:\footnote{These conditions can be appiled to any solution,
not only to a basic solution.}
$$(I\ |-\!A)x=0\eqno(3.14)$$
$$(I\ |-\!A)^T\pi+\lambda_l+\lambda_u=\nabla z=(0\ |\ c)^T\eqno(3.15)$$
$$l\leq x\leq u\eqno(3.16)$$
$$\lambda_l\geq 0,\ \ \lambda_u\leq 0\ \ \mbox{(minimization)}
\eqno(3.17)$$
$$\lambda_l\leq 0,\ \ \lambda_u\geq 0\ \ \mbox{(maximization)}
\eqno(3.18)$$
$$(\lambda_l)_k(x_k-l_k)=0,\ \ (\lambda_u)_k(x_k-u_k)=0,\ \ k=1,2,\dots,
m+n\eqno(3.19)$$
where:
$\pi=(\pi_1,\pi_2,\dots,\pi_m)$ is a $m$-vector of Lagrange
multipliers for equality constraints (3.5);
$\lambda_l=[(\lambda_l)_1,(\lambda_l)_2,\dots,(\lambda_l)_n]$ is a
$n$-vector of Lagrange multipliers for lower bound constraints (3.6);
$\lambda_u=[(\lambda_u)_1,(\lambda_u)_2,\dots,(\lambda_u)_n]$ is a
$n$-vector of Lagrange multipliers for upper bound constraints (3.6).

\newpage

Condition (3.14) is the {\it primal} (original) system of equality
constraints (3.5).

Condition (3.15) is the {\it dual} system of equality constraints.
It requires the gradient of the objective function to be a linear
combination of normals to the planes defined by constraints of the
original problem.

Condition (3.16) is the primal (original) system of bound constraints
(3.6).

Condition (3.17) (or (3.18) in case of maximization) is the dual system
of bound constraints.

Condition (3.19) is the {\it complementary slackness condition}. It
requires, for each original (auxiliary or structural) variable $x_k$,
that either its (lower or upper) bound must be active, or zero bound of
the corresponding Lagrange multiplier ($(\lambda_l)_k$ or
$(\lambda_u)_k$) must be active.

In GLPK two multipliers $(\lambda_l)_k$ and $(\lambda_u)_k$ for each
primal (original) variable $x_k$, $k=1,2,\dots,\linebreak m+n$, are
combined into one multiplier:
$$\lambda_k=(\lambda_l)_k+(\lambda_u)_k,\eqno(3.20)$$
which is called a {\it dual variable} for $x_k$. This {\it cannot} lead
to the ambiguity, because both lower and upper bounds of $x_k$ cannot be
active at the same time,\footnote{If $x_k$ is a fixed variable, we can
think it as double-bounded variable $l_k\leq x_k\leq u_k$, where
$l_k=u_k.$} so at least one of $(\lambda_l)_k$ and $(\lambda_u)_k$ must
be equal to zero, and because these multipliers have different signs,
the combined multiplier, which is their sum, uniquely defines each of
them.

\def\arraystretch{1}

Using dual variables $\lambda_k$ the dual system of bound constraints
(3.17) and (3.18) can be written in the form of so called {\it ``rule of
signs''} as follows:

\medskip

\begin{center}
\begin{tabular}{|@{\,}c@{$\,$}|@{$\,$}c@{$\,$}|@{$\,$}c@{$\,$}|
@{$\,$}c|c@{$\,$}|@{$\,$}c@{$\,$}|@{$\,$}c@{$\,$}|}
\hline
Original bound&\multicolumn{3}{c|}{Minimization}&\multicolumn{3}{c|}
{Maximization}\\
\cline{2-7}
constraint&$(\lambda_l)_k$&$(\lambda_u)_k$&$(\lambda_l)_k+
(\lambda_u)_k$&$(\lambda_l)_k$&$(\lambda_u)_k$&$(\lambda_l)_k+
(\lambda_u)_k$\\
\hline
$-\infty<x_k<+\infty$&$=0$&$=0$&$\lambda_k=0$&$=0$&$=0$&$\lambda_k=0$\\
$x_k\geq l_k$&$\geq 0$&$=0$&$\lambda_k\geq 0$&$\leq 0$&$=0$&$\lambda_k
\leq0$\\
$x_k\leq u_k$&$=0$&$\leq 0$&$\lambda_k\leq 0$&$=0$&$\geq 0$&$\lambda_k
\geq0$\\
$l_k\leq x_k\leq u_k$&$\geq 0$& $\leq 0$& $-\infty\!<\!\lambda_k\!<
\!+\infty$
&$\leq 0$& $\geq 0$& $-\infty\!<\!\lambda_k\!<\!+\infty$\\
$x_k=l_k=u_k$&$\geq 0$& $\leq 0$& $-\infty\!<\!\lambda_k\!<\!+\infty$&
$\leq 0$&
$\geq 0$& $-\infty\!<\!\lambda_k\!<\!+\infty$\\
\hline
\end{tabular}
\end{center}

\medskip

May note that each primal variable $x_k$ has its dual counterpart
$\lambda_k$ and vice versa. This allows applying the same partition for
the vector of dual variables as (3.7):
$$\left(\begin{array}{@{}c@{}}\lambda_B\\\lambda_N\\\end{array}\right)=
\Pi\lambda,\eqno(3.21)$$
where $\lambda_B$ is a vector of dual variables for basic variables
$x_B$, $\lambda_N$ is a vector of dual variables for non-basic variables
$x_N$.

By definition, bounds of basic variables are inactive constraints, so in
any basic solution $\lambda_B=0$. Corresponding values of dual variables
$\lambda_N$ for non-basic variables $x_N$ can be determined in the
following way. From the dual system (3.15) we have:
$$(I\ |-\!A)^T\pi+\lambda=(0\ |\ c)^T,\eqno(3.22)$$

\newpage

\noindent
so multiplying both sides of (3.22) by matrix $\Pi$ gives:
$$\Pi(I\ |-\!A)^T\pi+\Pi\lambda=\Pi(0\ |\ c)^T.\eqno(3.23)$$
From (3.9) it follows that
$$\Pi(I\ |-\!A)^T=[(I\ |-\!A)\Pi^T]^T=(B\ |\ N)^T.\eqno(3.24)$$
Further, we can apply the partition (3.7) also to the vector of
objective coefficients (see (3.4)):
$$\left(\begin{array}{@{}c@{}}c_B\\c_N\\\end{array}\right)=
\Pi\left(\begin{array}{@{}c@{}}0\\c\\\end{array}\right),\eqno(3.25)$$
where $c_B$ is a vector of objective coefficients at basic variables,
$c_N$ is a vector of objective coefficients at non-basic variables.
Now, substituting (3.24), (3.21), and (3.25) into (3.23), leads to:
$$(B\ |\ N)^T\pi+(\lambda_B\ |\ \lambda_N)^T=(c_B\ |\ c_N)^T,
\eqno(3.26)$$
and transposing both sides of (3.26) gives the system:
$$\left(\begin{array}{@{}c@{}}B^T\\N^T\\\end{array}\right)\pi+
\left(\begin{array}{@{}c@{}}\lambda_B\\\lambda_N\\\end{array}\right)=
\left(\begin{array}{@{}c@{}}c_B\\c_T\\\end{array}\right),\eqno(3.27)$$
which can be written as follows:
$$\left\{
\begin{array}{@{\ }r@{\ }c@{\ }r@{\ }c@{\ }l@{\ }}
B^T\pi&+&\lambda_B&=&c_B\\
N^T\pi&+&\lambda_N&=&c_N\\
\end{array}
\right.\eqno(3.28)
$$
Lagrange multipliers $\pi=(\pi_i)$ correspond to equality constraints
(3.5) and therefore can have any sign. This allows resolving the first
subsystem of (3.28) as follows:\footnote{$B^{-T}$ means $(B^T)^{-1}=
(B^{-1})^T$.}
$$\pi=B^{-T}(c_B-\lambda_B)=-B^{-T}\lambda_B+B^{-T}c_B,\eqno(3.29)$$
and substitution of $\pi$ from (3.29) into the second subsystem of
(3.28) gives:
$$\lambda_N=-N^T\pi+c_N=N^TB^{-T}\lambda_B+(c_N-N^TB^{-T}c_B).
\eqno(3.30)$$
The latter system can be written in the following final form:
$$\lambda_N=-\Xi^T\lambda_B+d,\eqno(3.31)$$
where $\Xi$ is the simplex tableau (see (3.12)), and
$$d=c_N-N^TB^{-T}c_B=c_N+\Xi^Tc_B\eqno(3.32)$$
is the vector of so called {\it reduced costs} of non-basic variables.

Above it was said that in any basic solution $\lambda_B=0$, so
$\lambda_N=d$ as it follows from (3.31).

The system (3.31) is equivalent to the system (3.15) in the sense that
they both define the same set of points in the space of dual variables
$\lambda$, which satisfy to these systems. If, moreover, values of all
dual variables $\lambda_N$ (i.e. reduced costs $d$) satisfy to their
bound constraints (i.e. to the ``rule of signs''; see the table above),
the corresponding basic solution is called {\it dual feasible},
otherwise {\it dual infeasible}. It is understood that any dual feasible
solution satisfy to all constraints (3.15) and (3.17) (or (3.18) in case
of maximization).

It can be easily shown that the complementary slackness condition
(3.19) is always satisfied for {\it any} basic solution.\footnote{Until
double-bounded variables appear.} Therefore, a basic
solution\footnote{It is assumed that a complete basic solution has the
form $(x,\lambda)$, i.e. it includes primal as well as dual variables.}
is {\it optimal} if and only if it is primal and dual feasible, because
in this case it satifies to all the optimality conditions
(3.14)---(3.19).

\def\arraystretch{1.5}

The meaning of reduced costs $d=(d_j)$ of non-basic variables can be
explained in the following way. From (3.4), (3.7), and (3.25) it follows
that:
$$z=c_B^Tx_B+c_N^Tx_N+c_0.\eqno(3.33)$$
Substituting $x_B$ from (3.11) into (3.33) we can eliminate basic
variables and express the objective only through non-basic variables:
$$
\begin{array}{r@{\ }c@{\ }l}
z&=&c_B^T(-B^{-1}Nx_N)+c_N^Tx_N+c_0=\\
&=&(c_N^T-c_B^TB^{-1}N)x_N+c_0=\\
&=&(c_N-N^TB^{-T}c_B)^Tx_N+c_0=\\
&=&d^Tx_N+c_0.
\end{array}\eqno(3.34)
$$
From (3.34) it is seen that reduced cost $d_j$ shows how the objective
function $z$ depends on non-basic variable $(x_N)_j$ in the neighborhood
of the current basic solution, i.e. while the current basis remains
unchanged.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newpage

\section{LP basis routines}
\label{lpbasis}

\subsection{glp\_bf\_exists --- check if the basis factorization
exists}

\synopsis

\begin{verbatim}
   int glp_bf_exists(glp_prob *P);
\end{verbatim}

\returns

If the basis factorization for the current basis associated with the
specified problem object exists and therefore is available for
computations, the routine \verb|glp_bf_exists| returns non-zero.
Otherwise the routine returns zero.

\para{Comments}

Let the problem object have $m$ rows and $n$ columns. In GLPK the
{\it basis matrix} $B$ is a square non-singular matrix of the order $m$,
whose columns correspond to basic (auxiliary and/or structural)
variables. It is defined by the following main
equality:\footnote{For more details see Subsection \ref{basbgd},
page \pageref{basbgd}.}
$$(B\ |\ N)=(I\ |-\!A)\Pi^T,$$
where $I$ is the unity matrix of the order $m$, whose columns correspond
to auxiliary variables; $A$ is the original constraint
$m\times n$-matrix, whose columns correspond to structural variables;
$(I\ |-\!A)$ is the augmented constraint $m\times(m+n)$-matrix, whose
columns correspond to all (auxiliary and structural) variables
following in the original order; $\Pi$ is a permutation matrix of the
order $m+n$; and $N$ is a rectangular $m\times n$-matrix, whose columns
correspond to non-basic (auxiliary and/or structural) variables.

For various reasons it may be necessary to solve linear systems with
matrix $B$. To provide this possibility the GLPK implementation
maintains an invertable form of $B$ (that is, some representation of
$B^{-1}$) called the {\it basis factorization}, which is an internal
component of the problem object. Typically, the basis factorization is
computed by the simplex solver, which keeps it in the problem object
to be available for other computations.

Should note that any changes in the problem object, which affects the
basis matrix (e.g. changing the status of a row or column, changing
a basic column of the constraint matrix, removing an active constraint,
etc.), invalidates the basis factorization. So before calling any API
routine, which uses the basis factorization, the application program
must make sure (using the routine \verb|glp_bf_exists|) that the
factorization exists and therefore available for computations.

\newpage

\subsection{glp\_factorize --- compute the basis factorization}

\synopsis

\begin{verbatim}
   int glp_factorize(glp_prob *P);
\end{verbatim}

\description

The routine \verb|glp_factorize| computes the basis factorization for
the current basis associated with the specified problem
object.\footnote{The current basis is defined by the current statuses
of rows (auxiliary variables) and columns (structural variables).}

The basis factorization is computed from ``scratch'' even if it exists,
so the application program may use the routine \verb|glp_bf_exists|,
and, if the basis factorization already exists, not to call the routine
\verb|glp_factorize| to prevent an extra work.

The routine \verb|glp_factorize| {\it does not} compute components of
the basic solution (i.e. primal and dual values).

\returns

\begin{retlist}
0 & The basis factorization has been successfully computed.\\
\verb|GLP_EBADB| & The basis matrix is invalid, because the number of
basic (auxiliary and structural) variables is not the same as the number
of rows in the problem object.\\

\verb|GLP_ESING| & The basis matrix is singular within the working
precision.\\

\verb|GLP_ECOND| & The basis matrix is ill-conditioned, i.e. its
condition number is too large.\\
\end{retlist}

\subsection{glp\_bf\_updated --- check if the basis factorization has
been updated}

\synopsis

\begin{verbatim}
   int glp_bf_updated(glp_prob *P);
\end{verbatim}

\returns

If the basis factorization has been just computed from ``scratch'', the
routine \verb|glp_bf_updated| returns zero. Otherwise, if the
factorization has been updated at least once, the routine returns
non-zero.

\para{Comments}

{\it Updating} the basis factorization means recomputing it to reflect
changes in the basis matrix. For example, on every iteration of the
simplex method some column of the current basis matrix is replaced by
a new column that gives a new basis matrix corresponding to the
adjacent basis. In this case computing the basis factorization for the
adjacent basis from ``scratch'' (as the routine \verb|glp_factorize|
does) would be too time-consuming.

On the other hand, since the basis factorization update is a numeric
computational procedure, applying it many times may lead to
accumulating round-off errors. Therefore the basis is periodically
refactorized (reinverted) from ``scratch'' (with the routine
\verb|glp_factorize|) that allows improving its numerical properties.

The routine \verb|glp_bf_updated| allows determining if the basis
factorization has been updated at least once since it was computed from
``scratch''.

\subsection{glp\_get\_bfcp --- retrieve basis factorization control
parameters}

\synopsis

\begin{verbatim}
   void glp_get_bfcp(glp_prob *P, glp_bfcp *parm);
\end{verbatim}

\description

The routine \verb|glp_get_bfcp| retrieves control parameters, which are
used on computing and updating the basis factorization associated with
the specified problem object.

Current values of the control parameters are stored in
a \verb|glp_bfcp| structure, which the parameter \verb|parm| points to.
For a detailed description of the structure \verb|glp_bfcp| see
comments to the routine \verb|glp_set_bfcp| in the next subsection.

\para{Comments}

The purpose of the routine \verb|glp_get_bfcp| is two-fold. First, it
allows the application program obtaining current values of control
parameters used by internal GLPK routines, which compute and update the
basis factorization.

The second purpose of this routine is to provide proper values for all
fields of the structure \verb|glp_bfcp| in the case when the
application program needs to change some control parameters.

\subsection{glp\_set\_bfcp --- change basis factorization control
parameters}

\synopsis

\begin{verbatim}
   void glp_set_bfcp(glp_prob *P, const glp_bfcp *parm);
\end{verbatim}

\description

The routine \verb|glp_set_bfcp| changes control parameters, which are
used by internal GLPK routines on computing and updating the basis
factorization associated with the specified problem object.

New values of the control parameters should be passed in a structure
\verb|glp_bfcp|, which the parameter \verb|parm| points to. For a
detailed description of the structure \verb|glp_bfcp| see paragraph
``Control parameters'' below.

The parameter \verb|parm| can be specified as \verb|NULL|, in which
case all control parameters are reset to their default values.

\para{Comments}

Before changing some control parameters with the routine
\verb|glp_set_bfcp| the application program should retrieve current
values of all control parameters with the routine \verb|glp_get_bfcp|.
This is needed for backward compatibility, because in the future there
may appear new members in the structure \verb|glp_bfcp|.

Note that new values of control parameters come into effect on a next
computation of the basis factorization, not immediately.

\para{Example}

\begin{footnotesize}
\begin{verbatim}
glp_prob *lp;
glp_bfcp parm;
. . .
/* retrieve current values of control parameters */
glp_get_bfcp(lp, &parm);
/* change the threshold pivoting tolerance */
parm.piv_tol = 0.05;
/* set new values of control parameters */
glp_set_bfcp(lp, &parm);
. . .
\end{verbatim}
\end{footnotesize}

\para{Control parameters}

This paragraph describes all basis factorization control parameters
currently used in the package. Symbolic names of control parameters are
names of corresponding members in the structure \verb|glp_bfcp|.

\medskip

{\tt int type} (default: {\tt GLP\_BF\_LUF + GLP\_BF\_FT})

Basis factorization type:

\verb~GLP_BF_LUF + GLP_BF_FT~ --- $LUF$, Forrest--Tomlin update;

\verb~GLP_BF_LUF + GLP_BF_BG~ --- $LUF$, Schur complement,
Bartels--Golub update;

\verb~GLP_BF_LUF + GLP_BF_GR~ --- $LUF$, Schur complement,
Givens rotation update;

\verb~GLP_BF_BTF + GLP_BF_BG~ --- $BTF$, Schur complement,
Bartels--Golub update;

\verb~GLP_BF_BTF + GLP_BF_GR~ --- $BTF$, Schur complement,
Givens rotation update.

In case of \verb|GLP_BF_FT| the update is applied to matrix $U$, while
in cases of \verb|GLP_BF_BG| and \verb|GLP_BF_GR| the update is applied
to the Schur complement.

%\medskip
%
%{\tt int lu\_size} (default: {\tt 0})
%
%The initial size of the Sparse Vector Area, in non-zeros, used on
%computing $LU$-factorization of the basis matrix for the first time.
%If this parameter is set to 0, the initial SVA size is determined
%automatically.

\medskip

{\tt double piv\_tol} (default: {\tt 0.10})

Threshold pivoting (Markowitz) tolerance, 0 $<$ \verb|piv_tol| $<$ 1,
used on computing $LU$-factoriza\-tion of the basis matrix. Element
$u_{ij}$ of the active submatrix of factor $U$ fits to be pivot if it
satisfies to the stability criterion
$|u_{ij}| >= {\tt piv\_tol}\cdot\max|u_{i*}|$, i.e. if it is not very
small in the magnitude among other elements in the same row. Decreasing
this parameter may lead to better sparsity at the expense of numerical
accuracy, and vice versa.

\medskip

{\tt int piv\_lim} (default: {\tt 4})

This parameter is used on computing $LU$-factorization of the basis
matrix and specifies how many pivot candidates needs to be considered
on choosing a pivot element, \verb|piv_lim| $\geq$ 1. If \verb|piv_lim|
candidates have been considered, the pivoting routine prematurely
terminates the search with the best candidate found.

\newpage

{\tt int suhl} (default: {\tt GLP\_ON})

This parameter is used on computing $LU$-factorization of the basis
matrix. Being set to {\tt GLP\_ON} it enables applying the following
heuristic proposed by Uwe Suhl: if a column of the active submatrix has
no eligible pivot candidates, it is no more considered until it becomes
a column singleton. In many cases this allows reducing the time needed
for pivot searching. To disable this heuristic the parameter
\verb|suhl| should be set to {\tt GLP\_OFF}.

\medskip

{\tt double eps\_tol} (default: {\tt 1e-15})

Epsilon tolerance, \verb|eps_tol| $\geq$ 0, used on computing
$LU$-factorization of the basis matrix. If an element of the active
submatrix of factor $U$ is less than \verb|eps_tol| in the magnitude,
it is replaced by exact zero.

%\medskip
%
%{\tt double max\_gro} (default: {\tt 1e+10})
%
%Maximal growth of elements of factor $U$, \verb|max_gro| $\geq$ 1,
%allowable on computing $LU$-factorization of the basis matrix. If on
%some elimination step the ratio $u_{big}/b_{max}$ (where $u_{big}$ is
%the largest magnitude of elements of factor $U$ appeared in its active
%submatrix during all the factorization process, $b_{max}$ is the
%largest magnitude of elements of the basis matrix to be factorized),
%the basis matrix is considered as ill-conditioned.

\medskip

{\tt int nfs\_max} (default: {\tt 100})

Maximal number of additional row-like factors (entries of the eta
file), \verb|nfs_max| $\geq$ 1, which can be added to
$LU$-factorization of the basis matrix on updating it with the
Forrest--Tomlin technique. This parameter is used only once, before
$LU$-factorization is computed for the first time, to allocate working
arrays. As a rule, each update adds one new factor (however, some
updates may need no addition), so this parameter limits the number of
updates between refactorizations.

\medskip

{\tt double upd\_tol} (default: {\tt 1e-6})

Update tolerance, 0 $<$ \verb|upd_tol| $<$ 1, used on updating
$LU$-factorization of the basis matrix with the Forrest--Tomlin
technique. If after updating the magnitude of some diagonal element
$u_{kk}$ of factor $U$ becomes less than
${\tt upd\_tol}\cdot\max(|u_{k*}|, |u_{*k}|)$, the factorization is
considered as inaccurate.

\medskip

{\tt int nrs\_max} (default: {\tt 100})

Maximal number of additional rows and columns, \verb|nrs_max| $\geq$ 1,
which can be added to $LU$-factorization of the basis matrix on
updating it with the Schur complement technique. This parameter is used
only once, before $LU$-factorization is computed for the first time, to
allocate working arrays. As a rule, each update adds one new row and
column (however, some updates may need no addition), so this parameter
limits the number of updates between refactorizations.

%\medskip
%
%{\tt int rs\_size} (default: {\tt 0})
%
%The initial size of the Sparse Vector Area, in non-zeros, used to
%store non-zero elements of additional rows and columns introduced on
%updating $LU$-factorization of the basis matrix with the Schur
%complement technique. If this parameter is set to 0, the initial SVA
%size is determined automatically.

\subsection{glp\_get\_bhead --- retrieve the basis header information}

\synopsis

\begin{verbatim}
   int glp_get_bhead(glp_prob *P, int k);
\end{verbatim}

\description

The routine \verb|glp_get_bhead| returns the basis header information
for the current basis associated with the specified problem object.

\returns

If basic variable $(x_B)_k$, $1\leq k\leq m$, is $i$-th auxiliary
variable ($1\leq i\leq m$), the routine returns $i$. Otherwise, if
$(x_B)_k$ is $j$-th structural variable ($1\leq j\leq n$), the routine
returns $m+j$. Here $m$ is the number of rows and $n$ is the number of
columns in the problem object.

\para{Comments}

Sometimes the application program may need to know which original
(auxiliary and structural) variable correspond to a given basic
variable, or, that is the same, which column of the augmented
constraint matrix $(I\ |-\!A)$ correspond to a given column of the
basis matrix $B$.

\def\arraystretch{1}

The correspondence is defined as follows:\footnote{For more details see
Subsection \ref{basbgd}, page \pageref{basbgd}.}
$$\left(\begin{array}{@{}c@{}}x_B\\x_N\\\end{array}\right)=
\Pi\left(\begin{array}{@{}c@{}}x_R\\x_S\\\end{array}\right)
\ \ \Leftrightarrow
\ \ \left(\begin{array}{@{}c@{}}x_R\\x_S\\\end{array}\right)=
\Pi^T\left(\begin{array}{@{}c@{}}x_B\\x_N\\\end{array}\right),$$
where $x_B$ is the vector of basic variables, $x_N$ is the vector of
non-basic variables, $x_R$ is the vector of auxiliary variables
following in their original order,\footnote{The original order of
auxiliary and structural variables is defined by the ordinal numbers
of corresponding rows and columns in the problem object.} $x_S$ is the
vector of structural variables following in their original order, $\Pi$
is a permutation matrix (which is a component of the basis
factorization).

Thus, if $(x_B)_k=(x_R)_i$ is $i$-th auxiliary variable, the routine
returns $i$, and if $(x_B)_k=(x_S)_j$ is $j$-th structural variable,
the routine returns $m+j$, where $m$ is the number of rows in the
problem object.

\subsection{glp\_get\_row\_bind --- retrieve row index in the basis
header}

\synopsis

\begin{verbatim}
   int glp_get_row_bind(glp_prob *P, int i);
\end{verbatim}

\returns

The routine \verb|glp_get_row_bind| returns the index $k$ of basic
variable $(x_B)_k$, $1\leq k\leq m$, which is $i$-th auxiliary variable
(that is, the auxiliary variable corresponding to $i$-th row),
$1\leq i\leq m$, in the current basis associated with the specified
problem object, where $m$ is the number of rows. However, if $i$-th
auxiliary variable is non-basic, the routine returns zero.

\para{Comments}

The routine \verb|glp_get_row_bind| is an inversion of the routine
\verb|glp_get_bhead|; that is, if \linebreak
\verb|glp_get_bhead|$(P,k)$ returns $i$,
\verb|glp_get_row_bind|$(P,i)$ returns $k$, and vice versa.

\subsection{glp\_get\_col\_bind --- retrieve column index in the basis
header}

\synopsis

\begin{verbatim}
   int glp_get_col_bind(glp_prob *P, int j);
\end{verbatim}

\returns

The routine \verb|glp_get_col_bind| returns the index $k$ of basic
variable $(x_B)_k$, $1\leq k\leq m$, which is $j$-th structural
variable (that is, the structural variable corresponding to $j$-th
column), $1\leq j\leq n$, in the current basis associated with the
specified problem object, where $m$ is the number of rows, $n$ is the
number of columns. However, if $j$-th structural variable is non-basic,
the routine returns zero.

\para{Comments}

The routine \verb|glp_get_col_bind| is an inversion of the routine
\verb|glp_get_bhead|; that is, if \linebreak
\verb|glp_get_bhead|$(P,k)$ returns $m+j$,
\verb|glp_get_col_bind|$(P,j)$ returns $k$, and vice versa.

\subsection{glp\_ftran --- perform forward transformation}

\synopsis

\begin{verbatim}
   void glp_ftran(glp_prob *P, double x[]);
\end{verbatim}

\description

The routine \verb|glp_ftran| performs forward transformation (FTRAN),
i.e. it solves the system $Bx=b$, where $B$ is the basis matrix
associated with the specified problem object, $x$ is the vector of
unknowns to be computed, $b$ is the vector of right-hand sides.

On entry to the routine elements of the vector $b$ should be stored in
locations \verb|x[1]|, \dots, \verb|x[m]|, where $m$ is the number of
rows. On exit the routine stores elements of the vector $x$ in the same
locations.

\subsection{glp\_btran --- perform backward transformation}

\synopsis

\begin{verbatim}
   void glp_btran(glp_prob *P, double x[]);
\end{verbatim}

\description

The routine \verb|glp_btran| performs backward transformation (BTRAN),
i.e. it solves the system $B^Tx=b$, where $B^T$ is a matrix transposed
to the basis matrix $B$ associated with the specified problem object,
$x$ is the vector of unknowns to be computed, $b$ is the vector of
right-hand sides.

On entry to the routine elements of the vector $b$ should be stored in
locations \verb|x[1]|, \dots, \verb|x[m]|, where $m$ is the number of
rows. On exit the routine stores elements of the vector $x$ in the same
locations.

\subsection{glp\_warm\_up --- ``warm up'' LP basis}

\synopsis

\begin{verbatim}
   int glp_warm_up(glp_prob *P);
\end{verbatim}

\description

The routine \verb|glp_warm_up| ``warms up'' the LP basis for the
specified problem object using current statuses assigned to rows and
columns (that is, to auxiliary and structural variables).

This operation includes computing factorization of the basis matrix
(if it does not exist), computing primal and dual components of basic
solution, and determining the solution status.

\returns

\begin{retlist}
0 & The operation has been successfully performed.\\

\verb|GLP_EBADB| & The basis matrix is invalid, because the number of
basic (auxiliary and structural) variables is not the same as the
number of rows in the problem object.\\

\verb|GLP_ESING| & The basis matrix is singular within the working
precision.\\

\verb|GLP_ECOND| & The basis matrix is ill-conditioned, i.e. its
condition number is too large.\\
\end{retlist}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newpage

\section{Simplex tableau routines}

\subsection{glp\_eval\_tab\_row --- compute row of the tableau}

\synopsis

\begin{verbatim}
   int glp_eval_tab_row(glp_prob *P, int k, int ind[], double val[]);
\end{verbatim}

\description

The routine \verb|glp_eval_tab_row| computes a row of the current
simplex tableau (see Subsection 3.1.1, formula (3.12)), which (row)
corresponds to some basic variable specified by the parameter $k$ as
follows: if $1\leq k\leq m$, the basic variable is $k$-th auxiliary
variable, and if $m+1\leq k\leq m+n$, the basic variable is $(k-m)$-th
structural variable, where $m$ is the number of rows and $n$ is the
number of columns in the specified problem object. The basis
factorization must exist.

The computed row shows how the specified basic variable depends on
non-basic variables:
$$x_k=(x_B)_i=\xi_{i1}(x_N)_1+\xi_{i2}(x_N)_2+\dots+\xi_{in}(x_N)_n,$$
where $\xi_{i1}$, $\xi_{i2}$, \dots, $\xi_{in}$ are elements of the
simplex table row, $(x_N)_1$, $(x_N)_2$, \dots, $(x_N)_n$ are non-basic
(auxiliary and structural) variables.

The routine stores column indices and corresponding numeric values of
non-zero elements of the computed row in unordered sparse format in
locations \verb|ind[1]|, \dots, \verb|ind[len]| and \verb|val[1]|,
\dots, \verb|val[len]|, respectively, where $0\leq{\tt len}\leq n$ is
the number of non-zero elements in the row returned on exit.

Element indices stored in the array \verb|ind| have the same sense as
index $k$, i.e. indices 1 to $m$ denote auxiliary variables while
indices $m+1$ to $m+n$ denote structural variables (all these variables
are obviously non-basic by definition).

\returns

The routine \verb|glp_eval_tab_row| returns \verb|len|, which is the
number of non-zero elements in the simplex table row stored in the
arrays \verb|ind| and \verb|val|.

\para{Comments}

A row of the simplex table is computed as follows. At first, the
routine checks that the specified variable $x_k$ is basic and uses the
permutation matrix $\Pi$ (3.7) to determine index $i$ of basic variable
$(x_B)_i$, which corresponds to $x_k$.

The row to be computed is $i$-th row of the matrix $\Xi$ (3.12),
therefore:
$$\xi_i=e_i^T\Xi=-e_i^TB^{-1}N=-(B^{-T}e_i)^TN,$$
where $e_i$ is $i$-th unity vector. So the routine performs BTRAN to
obtain $i$-th row of the inverse $B^{-1}$:
$$\varrho_i=B^{-T}e_i,$$
and then computes elements of the simplex table row as inner products:
$$\xi_{ij}=-\varrho_i^TN_j,\ \ j=1,2,\dots,n,$$
where $N_j$ is $j$-th column of matrix $N$ (3.9), which (column)
corresponds to non-basic variable $(x_N)_j$. The permutation matrix
$\Pi$ is used again to convert indices $j$ of non-basic columns to
original ordinal numbers of auxiliary and structural variables.

\subsection{glp\_eval\_tab\_col --- compute column of the tableau}

\synopsis

\begin{verbatim}
   int glp_eval_tab_col(glp_prob *P, int k, int ind[], double val[]);
\end{verbatim}

\description

The routine \verb|glp_eval_tab_col| computes a column of the current
simplex tableau (see Subsection 3.1.1, formula (3.12)), which (column)
corresponds to some non-basic variable specified by the parameter $k$:
if $1\leq k\leq m$, the non-basic variable is $k$-th auxiliary
variable, and if $m+1\leq k\leq m+n$, the non-basic variable is
$(k-m)$-th structural variable, where $m$ is the number of rows and $n$
is the number of columns in the specified problem object. The basis
factorization must exist.

The computed column shows how basic variables depends on the specified
non-basic variable $x_k=(x_N)_j$:
$$
\begin{array}{r@{\ }c@{\ }l@{\ }l}
(x_B)_1&=&\dots+\xi_{1j}(x_N)_j&+\dots\\
(x_B)_2&=&\dots+\xi_{2j}(x_N)_j&+\dots\\
.\ \ .&.&.\ \ .\ \ .\ \ .\ \ .\ \ .\ \ .\\
(x_B)_m&=&\dots+\xi_{mj}(x_N)_j&+\dots\\
\end{array}
$$
where $\xi_{1j}$, $\xi_{2j}$, \dots, $\xi_{mj}$ are elements of the
simplex table column, $(x_B)_1$, $(x_B)_2$, \dots, $(x_B)_m$ are basic
(auxiliary and structural) variables.

The routine stores row indices and corresponding numeric values of
non-zero elements of the computed column in unordered sparse format in
locations \verb|ind[1]|, \dots, \verb|ind[len]| and \verb|val[1]|,
\dots, \verb|val[len]|, respectively, where $0\leq{\tt len}\leq m$ is
the number of non-zero elements in the column returned on exit.

Element indices stored in the array \verb|ind| have the same sense as
index $k$, i.e. indices 1 to $m$ denote auxiliary variables while
indices $m+1$ to $m+n$ denote structural variables (all these variables
are obviously basic by definition).

\returns

The routine \verb|glp_eval_tab_col| returns \verb|len|, which is the
number of non-zero elements in the simplex table column stored in the
arrays \verb|ind| and \verb|val|.

\para{Comments}

A column of the simplex table is computed as follows. At first, the
routine checks that the specified variable $x_k$ is non-basic and uses
the permutation matrix $\Pi$ (3.7) to determine index $j$ of non-basic
variable $(x_N)_j$, which corresponds to $x_k$.

The column to be computed is $j$-th column of the matrix $\Xi$ (3.12),
therefore:
$$\Xi_j=\Xi e_j=-B^{-1}Ne_j=-B^{-1}N_j,$$
where $e_j$ is $j$-th unity vector, $N_j$ is $j$-th column of matrix
$N$ (3.9). So the routine performs FTRAN to transform $N_j$ to the
simplex table column $\Xi_j=(\xi_{ij})$ and uses the permutation matrix
$\Pi$ to convert row indices $i$ to original ordinal numbers of
auxiliary and structural variables.

\subsection{glp\_transform\_row --- transform explicitly specified row}

\synopsis

\begin{verbatim}
   int glp_transform_row(glp_prob *P, int len, int ind[], double val[]);
\end{verbatim}

\description

The routine \verb|glp_transform_row| performs the same operation as the
routine \verb|glp_eval_tab_row| with exception that the row to be
transformed is specified explicitly as a sparse vector.

The explicitly specified row may be thought as a linear form:
$$x=a_1x_{m+1}+a_2x_{m+2}+\dots+a_nx_{m+n},$$
where $x$ is an auxiliary variable for this row, $a_j$ are coefficients
of the linear form, $x_{m+j}$ are structural variables.

On entry column indices and numerical values of non-zero coefficients
$a_j$ of the specified row should be placed in locations \verb|ind[1]|,
\dots, \verb|ind[len]| and \verb|val[1]|, \dots, \verb|val[len]|, where
\verb|len| is number of non-zero coefficients.

This routine uses the system of equality constraints and the current
basis in order to express the auxiliary variable $x$ through the current
non-basic variables (as if the transformed row were added to the problem
object and the auxiliary variable $x$ were basic), i.e. the resultant
row has the form:
$$x=\xi_1(x_N)_1+\xi_2(x_N)_2+\dots+\xi_n(x_N)_n,$$
where $\xi_j$ are influence coefficients, $(x_N)_j$ are non-basic
(auxiliary and structural) variables, $n$ is the number of columns in
the problem object.

On exit the routine stores indices and numerical values of non-zero
coefficients $\xi_j$ of the resultant row in locations \verb|ind[1]|,
\dots, \verb|ind[len']| and \verb|val[1]|, \dots, \verb|val[len']|,
where $0\leq{\tt len'}\leq n$ is the number of non-zero coefficients in
the resultant row returned by the routine. Note that indices of
non-basic variables stored in the array \verb|ind| correspond to
original ordinal numbers of variables: indices 1 to $m$ mean auxiliary
variables and indices $m+1$ to $m+n$ mean structural ones.

\returns

The routine \verb|glp_transform_row| returns \verb|len'|, the number of
non-zero coefficients in the resultant row stored in the arrays
\verb|ind| and \verb|val|.

\newpage

\subsection{glp\_transform\_col --- transform explicitly specified
column}

\synopsis

\begin{verbatim}
   int glp_transform_col(glp_prob *P, int len, int ind[], double val[]);
\end{verbatim}

\description

The routine \verb|glp_transform_col| performs the same operation as the
routine \verb|glp_eval_tab_col| with exception that the column to be
transformed is specified explicitly as a sparse vector.

The explicitly specified column may be thought as it were added to
the original system of equality constraints:
$$
\begin{array}{l@{\ }c@{\ }r@{\ }c@{\ }r@{\ }c@{\ }r}
x_1&=&a_{11}x_{m+1}&+\dots+&a_{1n}x_{m+n}&+&a_1x \\
x_2&=&a_{21}x_{m+1}&+\dots+&a_{2n}x_{m+n}&+&a_2x \\
\multicolumn{7}{c}
{.\ \ .\ \ .\ \ .\ \ .\ \ .\ \ .\ \ .\ \ .\ \ .\ \ .\ \ .\ \ .\ \ .}\\
x_m&=&a_{m1}x_{m+1}&+\dots+&a_{mn}x_{m+n}&+&a_mx \\
\end{array}
$$
where $x_i$ are auxiliary variables, $x_{m+j}$ are structural variables
(presented in the problem object), $x$ is a structural variable for the
explicitly specified column, $a_i$ are constraint coefficients at $x$.

On entry row indices and numerical values of non-zero coefficients
$a_i$ of the specified column should be placed in locations
\verb|ind[1]|, \dots, \verb|ind[len]| and \verb|val[1]|, \dots,
\verb|val[len]|, where \verb|len| is number of non-zero coefficients.

This routine uses the system of equality constraints and the current
basis in order to express the current basic variables through the
structural variable $x$ (as if the transformed column were added to the
problem object and the variable $x$ were non-basic):
$$
\begin{array}{l@{\ }c@{\ }r}
(x_B)_1&=\dots+&\xi_{1}x\\
(x_B)_2&=\dots+&\xi_{2}x\\
\multicolumn{3}{c}{.\ \ .\ \ .\ \ .\ \ .\ \ .}\\
(x_B)_m&=\dots+&\xi_{m}x\\
\end{array}
$$
where $\xi_i$ are influence coefficients, $x_B$ are basic (auxiliary
and structural) variables, $m$ is the number of rows in the problem
object.

On exit the routine stores indices and numerical values of non-zero
coefficients $\xi_i$ of the resultant column in locations \verb|ind[1]|,
\dots, \verb|ind[len']| and \verb|val[1]|, \dots, \verb|val[len']|,
where $0\leq{\tt len'}\leq m$ is the number of non-zero coefficients in
the resultant column returned by the routine. Note that indices of basic
variables stored in the array \verb|ind| correspond to original ordinal
numbers of variables, i.e. indices 1 to $m$ mean auxiliary variables,
indices $m+1$ to $m+n$ mean structural ones.

\returns

The routine \verb|glp_transform_col| returns \verb|len'|, the number of
non-zero coefficients in the resultant column stored in the arrays
\verb|ind| and \verb|val|.

\newpage

\subsection{glp\_prim\_rtest --- perform primal ratio test}

\synopsis

\begin{verbatim}
   int glp_prim_rtest(glp_prob *P, int len, const int ind[], const double val[],
                      int dir, double eps);
\end{verbatim}

\description

The routine \verb|glp_prim_rtest| performs the primal ratio test using
an explicitly specified column of the simplex table.

The current basic solution associated with the LP problem object must
be primal feasible.

The explicitly specified column of the simplex table shows how the
basic variables $x_B$ depend on some non-basic variable $x$ (which is
not necessarily presented in the problem object):
$$
\begin{array}{l@{\ }c@{\ }r}
(x_B)_1&=\dots+&\xi_{1}x\\
(x_B)_2&=\dots+&\xi_{2}x\\
\multicolumn{3}{c}{.\ \ .\ \ .\ \ .\ \ .\ \ .}\\
(x_B)_m&=\dots+&\xi_{m}x\\
\end{array}
$$

The column is specifed on entry to the routine in sparse format.
Ordinal numbers of basic variables $(x_B)_i$ should be placed in
locations \verb|ind[1]|, \dots, \verb|ind[len]|, where ordinal number
1 to $m$ denote auxiliary variables, and ordinal numbers $m+1$ to $m+n$
denote structural variables. The corresponding non-zero coefficients
$\xi_i$ should be placed in locations
\verb|val[1]|, \dots, \verb|val[len]|. The arrays \verb|ind| and
\verb|val| are not changed by the routine.

The parameter \verb|dir| specifies direction in which the variable $x$
changes on entering the basis: $+1$ means increasing, $-1$ means
decreasing.

The parameter \verb|eps| is an absolute tolerance (small positive
number, say, $10^{-9}$) used by the routine to skip $\xi_i$'s whose
magnitude is less than \verb|eps|.

The routine determines which basic variable (among those specified in
\verb|ind[1]|, \dots, \verb|ind[len]|) reaches its (lower or upper)
bound first before any other basic variables do, and which, therefore,
should leave the basis in order to keep primal feasibility.

\returns

The routine \verb|glp_prim_rtest| returns the index, \verb|piv|, in the
arrays \verb|ind| and \verb|val| corresponding to the pivot element
chosen, $1\leq$ \verb|piv| $\leq$ \verb|len|. If the adjacent basic
solution is primal unbounded, and therefore the choice cannot be made,
the routine returns zero.

\para{Comments}

If the non-basic variable $x$ is presented in the LP problem object,
the input column can be computed with the routine
\verb|glp_eval_tab_col|; otherwise, it can be computed with the routine
\verb|glp_transform_col|.

\newpage

\subsection{glp\_dual\_rtest --- perform dual ratio test}

\synopsis

\begin{verbatim}
   int glp_dual_rtest(glp_prob *P, int len, const int ind[], const double val[],
                      int dir, double eps);
\end{verbatim}

\description

The routine \verb|glp_dual_rtest| performs the dual ratio test using
an explicitly specified row of the simplex table.

The current basic solution associated with the LP problem object must
be dual feasible.

The explicitly specified row of the simplex table is a linear form
that shows how some basic variable $x$ (which is not necessarily
presented in the problem object) depends on non-basic variables $x_N$:
$$x=\xi_1(x_N)_1+\xi_2(x_N)_2+\dots+\xi_n(x_N)_n.$$

The row is specified on entry to the routine in sparse format. Ordinal
numbers of non-basic variables $(x_N)_j$ should be placed in locations
\verb|ind[1]|, \dots, \verb|ind[len]|, where ordinal numbers 1 to $m$
denote auxiliary variables, and ordinal numbers $m+1$ to $m+n$ denote
structural variables. The corresponding non-zero coefficients $\xi_j$
should be placed in locations \verb|val[1]|, \dots, \verb|val[len]|.
The arrays \verb|ind| and \verb|val| are not changed by the routine.

The parameter \verb|dir| specifies direction in which the variable $x$
changes on leaving the basis: $+1$ means that $x$ goes on its lower
bound, so its reduced cost (dual variable) is increasing (minimization)
or decreasing (maximization); $-1$ means that $x$ goes on its upper
bound, so its reduced cost is decreasing (minimization) or increasing
(maximization).

The parameter \verb|eps| is an absolute tolerance (small positive
number, say, $10^{-9}$) used by the routine to skip $\xi_j$'s whose
magnitude is less than \verb|eps|.

The routine determines which non-basic variable (among those specified
in \verb|ind[1]|, \dots,\linebreak \verb|ind[len]|) should enter the
basis in order to keep dual feasibility, because its reduced cost
reaches the (zero) bound first before this occurs for any other
non-basic variables.

\returns

The routine \verb|glp_dual_rtest| returns the index, \verb|piv|, in the
arrays \verb|ind| and \verb|val| corresponding to the pivot element
chosen, $1\leq$ \verb|piv| $\leq$ \verb|len|. If the adjacent basic
solution is dual unbounded, and therefore the choice cannot be made,
the routine returns zero.

\para{Comments}

If the basic variable $x$ is presented in the LP problem object, the
input row can be computed\linebreak with the routine
\verb|glp_eval_tab_row|; otherwise, it can be computed with the routine
\linebreak \verb|glp_transform_row|.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newpage

\section{Post-optimal analysis routines}

\subsection{glp\_analyze\_bound --- analyze active bound of non-basic
variable}

\synopsis

\begin{verbatim}
   void glp_analyze_bound(glp_prob *P, int k, double *limit1, int *var1,
                          double *limit2, int *var2);
\end{verbatim}

\description

The routine \verb|glp_analyze_bound| analyzes the effect of varying the
active bound of specified non-basic variable.

The non-basic variable is specified by the parameter $k$, where
$1\leq k\leq m$ means auxiliary variable of corresponding row, and
$m+1\leq k\leq m+n$ means structural variable (column).

Note that the current basic solution must be optimal, and the basis
factorization must exist.

Results of the analysis have the following meaning.

\verb|value1| is the minimal value of the active bound, at which the
basis still remains primal feasible and thus optimal. \verb|-DBL_MAX|
means that the active bound has no lower limit.

\verb|var1| is the ordinal number of an auxiliary (1 to $m$) or
structural ($m+1$ to $m+n$) basic variable, which reaches its bound
first and thereby limits further decreasing the active bound being
analyzed. if \verb|value1| = \verb|-DBL_MAX|, \verb|var1| is set to 0.

\verb|value2| is the maximal value of the active bound, at which the
basis still remains primal feasible and thus optimal. \verb|+DBL_MAX|
means that the active bound has no upper limit.

\verb|var2| is the ordinal number of an auxiliary (1 to $m$) or
structural ($m+1$ to $m+n$) basic variable, which reaches its bound
first and thereby limits further increasing the active bound being
analyzed. if \verb|value2| = \verb|+DBL_MAX|, \verb|var2| is set to 0.

The parameters \verb|value1|, \verb|var1|, \verb|value2|, \verb|var2|
can be specified as \verb|NULL|, in which case corresponding information
is not stored.

\subsection{glp\_analyze\_coef --- analyze objective coefficient at
basic variable}

\synopsis

\begin{verbatim}
   void glp_analyze_coef(glp_prob *P, int k,
                         double *coef1, int *var1, double *value1,
                         double *coef2, int *var2, double *value2);
\end{verbatim}

\description

The routine \verb|glp_analyze_coef| analyzes the effect of varying the
objective coefficient at specified basic variable.

The basic variable is specified by the parameter $k$, where
$1\leq k\leq m$ means auxiliary variable of corresponding row, and
$m+1\leq k\leq m+n$ means structural variable (column).

Note that the current basic solution must be optimal, and the basis
factorization must exist.

Results of the analysis have the following meaning.

\verb|coef1| is the minimal value of the objective coefficient, at
which the basis still remains dual feasible and thus optimal.
\verb|-DBL_MAX| means that the objective coefficient has no lower
limit.

\verb|var1| is the ordinal number of an auxiliary (1 to $m$) or
structural ($m+1$ to $m+n$) non-basic variable, whose reduced cost
reaches its zero bound first and thereby limits further decreasing the
objective coefficient being analyzed.
If \verb|coef1| = \verb|-DBL_MAX|, \verb|var1| is set to 0.

\verb|value1| is value of the basic variable being analyzed in an
adjacent basis, which is defined as follows. Let the objective
coefficient reach its minimal value (\verb|coef1|) and continue
decreasing. Then the reduced cost of the limiting non-basic variable
(\verb|var1|) becomes dual infeasible and the current basis becomes
non-optimal that forces the limiting non-basic variable to enter the
basis replacing there some basic variable that leaves the basis to keep
primal feasibility. Should note that on determining the adjacent basis
current bounds of the basic variable being analyzed are ignored as if
it were free (unbounded) variable, so it cannot leave the basis. It may
happen that no dual feasible adjacent basis exists, in which case
\verb|value1| is set to \verb|-DBL_MAX| or \verb|+DBL_MAX|.

\verb|coef2| is the maximal value of the objective coefficient, at
which the basis still remains dual feasible and thus optimal.
\verb|+DBL_MAX| means that the objective coefficient has no upper
limit.

\verb|var2| is the ordinal number of an auxiliary (1 to $m$) or
structural ($m+1$ to $m+n$) non-basic variable, whose reduced cost
reaches its zero bound first and thereby limits further increasing the
objective coefficient being analyzed.
If \verb|coef2| = \verb|+DBL_MAX|, \verb|var2| is set to 0.

\verb|value2| is value of the basic variable being analyzed in an
adjacent basis, which is defined exactly in the same way as
\verb|value1| above with exception that now the objective coefficient
is increasing.

The parameters \verb|coef1|, \verb|var1|, \verb|value1|, \verb|coef2|,
\verb|var2|, \verb|value2| can be specified as \verb|NULL|, in which
case corresponding information is not stored.

%* eof *%


\section{Evaluation}
\label{sec:evaluation}
%
In this section, we introduce several pairs of coding solutions,
translated to functional programs.
%
For each pair of programs $P_0$ and $P_1$, we identify an inductive
set of pairs of expressions $I$, and give a map from all pairs of
expressions in $I$ to relational invariants that prove the partial
equivalence of $P_0$ and $P_1$.

\paragraph{Solutions to Add Digits}
%
\NV{complete}

\paragraph{Solutions to Climbing Stairs}
%
\begin{figure}[t]
  \begin{minipage}{0.48\linewidth}
    \input{code/ClimbStairs0.ml}
  \end{minipage}
  \begin{minipage}{0.48\linewidth}
    \input{code/ClimbStairs1.ml}
  \end{minipage}
  %
  \caption{\cc{ClimbStairs0} and \cc{ClimbStairs1}: two solutions to
    the Climbing Stairs problem hosted on LeetCode, manually translated
    to OCaml.
    %
    All subexpressions are labeled with indices in comments.
    %
  }
  \label{fig:climb-stairs}
\end{figure}
%
\autoref{fig:climb-stairs} contains the pseudocode for two solutions
to the Climbing Stairs Problem hosted on the coding platform the
LeetCode.
% problem statement:
The Climbing Stairs Problem is to take an integer $n$ and return the
number of distinct sequences of steps that can be taken to climb $n$
steps, where in each step either one or two steps can be climbed. If
$n$ is less than or equal to one, then the solution is one.
%
\BH{walk through how the two solutions work}

%
\cc{climbStairs0} and \cc{climbStairs1} have the following proof of
partial equivalence:
%
\BH{put the pairs of subexpressions in a set}
%
\begin{align*}
  % pair of main subexpressions:
  (\cc{0}, \cc{0}) \mapsto & %
  \alpha_0^0 = \alpha_1^0 \implies \nu_0 = \nu_1 \\
  % pair of function bodies:
  (\cc{1}, \cc{1}) \mapsto & %
  \cc{n}_0 = \cc{n}_1 \implies \nu_0 = \nu_1 \\
  % pair of if guards:
  (\cc{2}, \cc{2}) \mapsto & %
  \cc{n}_0 = \cc{n}_1 \implies \nu_0 = \nu_1 \\
  % pair of then expressions:
  (\cc{3}, \cc{3}) \mapsto & \nu_0 = \nu_1 \\
  % pair of else expressions:
  (\cc{4}, \cc{4}) \mapsto & %
  \cc{n}_0 = \cc{n}_1 \implies \nu_0 = \nu_1 \\
  % pair of fixpoint functions:
  (\cc{5}, \cc{5}) \mapsto & %
  % inductive relational invariant (indices in step):
  (\cc{n}_0 = \cc{n}_1 \land %
  \alpha_0^0 = \alpha_1^1 \land %
  \alpha_0^1 = \alpha_1^1 \land %
  \alpha_0^3 + 1 = \alpha_1^2 \implies %
  \nu_0 = \nu_1) \land \\
  % for establishing final property:
  & (\cc{n}_0 = \cc{n}_1 \land %
  \alpha_0^0 = \alpha_0^1 + \alpha_1^1 \land %
  \alpha_0^1 = \alpha_1^1 \land %
  \alpha_0^3 = \alpha_1^2 \implies %
  \nu_0 = \nu_1) \\
  % pair of argument vectors:
  (\cc{6}, \cc{6}) \mapsto & %
  \pi_0(\nu_0) = \pi_0(\nu_1) + \pi_1(\nu_1) \land %
  \pi_1(\nu_0) = \pi_1(\nu_1) \land %
  \pi_3(\nu_0) = \pi_3(\nu_3) \\
  % fixpoint body:
  (\cc{7}, \cc{7}) \mapsto &
  % inductive relational invariant (indices in step):
  (\cc{n}_0 = \cc{n}_1 \land %
  \cc{sum}_0 = \cc{count2}_1 \land %
  \cc{prev}_1 = \cc{count2}_1 \land %
  \cc{i}_3 + 1 = \cc{i}_1 \implies %
  \nu_0 = \nu_1) \land \\
  % for establishing final property:
  & (\cc{n}_0 = \cc{n}_1 \land %
  \cc{sum}_0 = \cc{count1}_1 + \cc{count2}_1 \land %
  \cc{prev}_1 = \cc{count2}_1 \land %
  \cc{i}_3 = \cc{i}_1 \implies %
  \nu_0 = \nu_1) \\
  % fixpoint body guards:
  (\cc{8}, \cc{8}) \mapsto & 0 \leq \cc{i}_1 - \cc{i}_0 \leq 1 \implies %
  \nu_0 = \nu_1 \\
  % fixpoint body then expressions:
  (\cc{9}, \cc{9}) \mapsto &
  (\cc{n}_0 = \cc{n}_1 \land %
  \cc{sum}_0 = \cc{count2}_1 \land %
  \cc{prev}_0 = \cc{count2}_1 \land %
  \cc{i}_0 + 1 = \cc{i}_1 \implies %
  \nu_0 = \nu_1) \land \\
  % for establishing final property:
  & (\cc{n}_0 = \cc{n}_1 \land \cc{i}_0 \land %
  \cc{sum}_0 = \cc{count1}_1 + \cc{count2}_1 \land %
  \cc{prev}_1 = \cc{count2}_1 \land %
  \cc{i}_0 = \cc{i}_1 \implies %
  \nu_0 = \nu_1) \\
  % fixpoint body else expressions:
  (\cc{10}, \cc{10}) \mapsto & \cc{sum}_0 = \cc{count2}_1 \implies %
  \nu_0 = \nu_1 \\
  % argument steps preserve relative invariants over arguments:
  % subcase: CS1 is one step ahead of CS0:
  (\cc{11}, \cc{11}) \mapsto &
  (\cc{sum}_0 = \cc{count2}_1 \land %
  \cc{prev}_1 = \cc{count2}_1 \land %
  \cc{i}_3 + 1 = \cc{i}_1 \implies \\
  & \pi_0(\nu_0) = \pi_1(\nu_1) \land %
  \pi_1(\nu_0) = \pi_0(\nu_1) \land %
  \pi_3(\nu_0) = \pi_2(\nu_1)) \land \\
  %
  & (\cc{sum}_0 = \cc{count1}_1 + \cc{count2}_1 \land %
  \cc{prev}_1 = \cc{count2}_1 \land %
  \cc{i}_0 = \cc{i}_1 \implies \\ %
  & \pi_0(\nu_0) = \pi_0(\nu_1) + \pi_1(\nu_1) \land %
  \pi_1(\nu_0) = \pi_1(\nu_1) \land %
  \pi_3(\nu_0) = \pi_2(\nu_1)) \\
\end{align*}
%
\BH{define notation for logical variables}
%
\BH{refactor entries to use common invariant meta-variables}

\BH{walk through relational invariants}

\NV{Question: Why 9 has n0 equals tp n1 precondition but 10 does not?}

\paragraph{Solutions to Reverse}
%
\NV{complete}

\paragraph{Solutions to Trailing Zeroes}
\begin{hcode}
trailingZeroes0 {- 0 -} n
 = {- 1 -} go0 ((0, n) {- 2 -}) {- 3 -}
  where
    go0 {- 4 -} (x, y) {- 5 -}
      | 5 <= y  {- 6 -}
      = go0 ((x + (y `div` 5), y `div` 5) {- 9-}) {- 7 -}
      | otherwise
      = x {- 8 -}

trailingZeroes1 {- 0 -} n
 = {- 1 -} go1 ((0, n `div` 5) {- 2 -}) {- 3 -}
  where
    go1 {- 4 -} (s, n) {- 5 -}
      | 0 < n   {- 6 -}
      = go1 ((s + n, n `div` 5) {- 9 -}) {- 7 -}
      | otherwise
      = s {- 8 -}
\end{hcode}

\newcommand\rinv[3]{\ensuremath{(\cc{#1}, \cc{#2}) }}
\newcommand\iinv[2]{\ensuremath{\mathcal{I}(#1, #2)}}
\newcommand\precondition[2]{\ensuremath{\mathcal{P}(#1, #2)}}
\newcommand\postcondition[2]{\ensuremath{\mathcal{Q}(#1, #2)}}

$$
\text{Invariant: }\iinv{x_0}{x_1} = x_0^0 = x_1^0 \land x_0^1 = x_0^1 / 5
$$
$$
\text{Precondition: }\precondition{x_0}{x_1} = x_0^0 = x_1^0 \land x_0^1 = x_0^1 / 5
$$
$$
\text{Postcondition: }\postcondition{x_0}{x_1} = x_0 = x_1
$$

$$
\begin{array}{ r c l}
  % pair of main subexpressions:
  \rinv{0}{0} &\mapsto& \alpha_0 = \alpha_1 \implies \nu_0 = \nu_1 \\
  % pair of body
  \rinv{1}{1} &\mapsto& n_0 = n_1 \implies \nu_0 = \nu_1 \\
  % pair on arguments
  \rinv{2}{2} &\mapsto& \precondition{\nu_0}{\nu_1} \\
  % pair of fixpoint functions (like step 5 above):
  \rinv{3}{3} &\mapsto& n_0 = n_1 \implies \postcondition{\nu_0}{\nu_1} \\
  % pair of fixpoint functions (like step 5 above):
  \rinv{4}{4} &\mapsto& \precondition{\alpha_0}{\alpha_1} \implies \postcondition{\nu_0}{\nu_1} \\
  \rinv{5}{5} &\mapsto& \precondition{(x_0, y_0)}{(s_1, n_1)} \implies \postcondition{\nu_0}{\nu_1} \\
  % pair for condition
  \rinv{6}{6}  &\mapsto&  \precondition{(x_0, y_0)}{(s_1, n_1)}
                          \implies 5 \leq y_0 \Leftrightarrow 0 < n_1
                          \implies \postcondition{\nu_0}{\nu_1}   \\
  % pair for then
  \rinv{7}{7}  &\mapsto&  \precondition{(x_0, y_0)}{(s_1, n_1)}
                          \implies \iinv{(x_0, y_0)}{(s_1, n_1)}
                          \implies \postcondition{\nu_0}{\nu_1}   \\
  % pair for else
  \rinv{8}{8}  &\mapsto&  \precondition{(x_0, y_0)}{(s_1, n_1)}
                          \implies x_0 = s_1
                          \implies \postcondition{\nu_0}{\nu_1} \\
  % pair for rec call
  \rinv{9}{9}  &\mapsto& \iinv{(x_0, y_0)}{(s_1, n_1)} \implies \iinv{\nu_0}{\nu_1}\\
\end{array}
$$

%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:


\documentclass{article}
\input{../homework-problems}

\toggletrue{solutions}
%\togglefalse{solutions}
\toggletrue{answers}
\newtheorem{problem}{Problem}

\newcommand{\hide}[1]{}

\renewcommand{\fcProblemRef}{\theproblem.\theenumi}
\renewcommand{\fcSubProblemRef}{\theenumi.\theenumii}

\begin{document}
\begin{center}
\Large
Master Problem Sheet \\ Calculus I \\
\end{center}

%\noindent \textbf{Name:} \hfill{~}
%\begin{tabular}{c|c|c|c|c|c|c|c|c||c}
%Problem&1 &2&3&4&5&6&7&8& $\sum$\\ \hline
%Score&&&&&&&&&\\ \hline
%Max&20&20&20&20&20&10&20&20&150
%\end{tabular}

This master problem sheet contains all freecalc problems on the topics studied in Calculus I. For a list of contributors/authors of the freecalc project (and in particular, the present problem collection) see file contributors.tex.

\fcLicenseContent

\tableofcontents

\section{Functions, Basic Facts}\label{secMPSfunctionBasics}
\subsection{Understanding function notation}
\begin{problem}
\input{\freecalcBaseFolder/modules/precalculus/homework/functions-evaluate-simplify}
\end{problem}
\subsection{Domains and ranges}
\begin{problem}
\input{../../modules/precalculus/homework/functions-domains-ranges-1}
\end{problem}
\subsection{Piecewise Defined Functions}
\begin{problem}
\input{../../modules/functions-basics/homework/piecewise-linear-formula-from-plot-1}
\end{problem}

\begin{problem}
\input{../../modules/functions-basics/homework/piecewise-linear-piecewise-circular-formula-from-plot-1}
\end{problem}

\begin{problem}
\input{../../modules/precalculus/homework/piecewise-defined-plot-from-formula}
\end{problem}

\subsection{Function composition}
\begin{problem}
\input{../../modules/functions-basics/homework/function-composition-domains-and-ranges-1}
\end{problem}
\begin{problem}
\input{../../modules/functions-basics/homework/functions-composing-fractional-linear-1}
\end{problem}
\subsection{Linear Transformations and Graphs of Functions}
\begin{problem}
\input{../../modules/precalculus/homework/functions-plot-transformations}
\end{problem}

\begin{problem}
\input{../../modules/function-graph-linear-transformations/homework/use-known-graph-to-sketch-its-linear-transformation-1}
\end{problem}
\input{../../modules/function-graph-linear-transformations/homework/use-known-graph-to-sketch-its-linear-transformation-1-solutions}

\section{Trigonometry}\label{secMPStrigonometry}
\subsection{Angle conversion}
\begin{problem}
\input{../../modules/trigonometry/homework/trigonometry-angle-conversion-degree-to-radian}
\end{problem}
\begin{problem}
\input{../../modules/trigonometry/homework/trigonometry-angle-conversion-radian-to-degree}
\end{problem}
\subsection{Trigonometry identities}
\begin{problem}
\input{../../modules/trigonometry/homework/trigonometry-identities}
\end{problem}

\subsection{Trigonometry equations}
\begin{problem}
\input{../../modules/trigonometry/homework/trigonometry-equations}
\end{problem}
\input{../../modules/trigonometry/homework/trigonometry-equations-solutions}

\section{Limits and Continuity}
\subsection{Limits as $x$ tends to a number}\label{secMPSlimitsXtendsToNumer}
\begin{problem}
\input{../../modules/limits/homework/limit-x-tends-to-a-common-factor}
\end{problem}
\input{../../modules/limits/homework/limit-x-tends-to-a-common-factor-solutions}
\begin{problem}
\input{../../modules/limits/homework/limit-x-tends-to-a-common-factor2}
\end{problem}
\begin{problem}
\input{../../modules/limits/homework/limit-x-tends-to-a-direct-substitution}
\end{problem}
\subsection{Limits involving $\infty $}
\subsubsection{Limits as $x\to\pm \infty$}\label{secMPSlimitsXtoInfty}
\begin{problem}
\input{../../modules/limits/homework/limit-x-tends-to-infinity-1}
\end{problem}
\input{../../modules/limits/homework/limit-x-tends-to-infinity-1-solutions}

\subsubsection{Limits involving vertical asymptote}\label{secMPSlimitsVerticalAsymptote}
\begin{problem}
\input{../../modules/curve-sketching/homework/asymptotes-vertical}
\end{problem}
\begin{problem}
\input{../../modules/curve-sketching/homework/asymptotes-vertical2}
\end{problem}
\input{../../modules/curve-sketching/homework/asymptotes-vertical2-solutions}
\subsubsection{Find the Horizontal and Vertical Asymptotes}\label{secMPShorAndVertAsymptotes}
\begin{problem}
\input{../../modules/curve-sketching/homework/asymptotes-vertical-horizontal}
\end{problem}
\input{../../modules/curve-sketching/homework/asymptotes-vertical-horizontal-solutions}

\subsection{Limits - All Cases - Problem Collection}
\begin{problem}
\input{../../modules/limits/homework/limit-problem-collection-1}
\end{problem}
\subsection{Continuity}
\subsubsection{Continuity to evaluate limits}
\begin{problem}
\input{../../modules/continuity/homework/continuity-to-evaluate-limits-1}
\end{problem}

\subsubsection{Conceptual problems} \label{secMPScontinuityConceptual}
\begin{problem}
\input{../../modules/continuity/homework/continuity-problems-involving-understanding}
\end{problem}
\begin{problem}
\input{../../modules/continuity/homework/continuity-problems-involving-understanding-2}
\end{problem}
\subsubsection{Continuity and Piecewise Defined Functions} \label{secMPScontinuityPiecewise}
\begin{problem}
\input{../../modules/continuity/homework/continuity-with-piecewise-defined-functions}
\end{problem}
\begin{problem}
\input{../../modules/continuity/homework/continuity-with-piecewise-defined-functions-2}
\end{problem}
\begin{problem}
\input{../../modules/continuity/homework/continuity-with-piecewise-defined-functions-3}
\end{problem}
\subsection{Intermediate Value Theorem}\label{secMPSintermediateValueTheorem}
\begin{problem}
\input{../../modules/continuity/homework/IVT-problems}
\end{problem}
\input{../../modules/continuity/homework/IVT-problems-solutions}
\begin{problem}
\input{../../modules/continuity/homework/IVT-problems2}
\end{problem}
\input{../../modules/continuity/homework/IVT-problems2-solutions}
\section{Inverse Functions}\label{secMPSInverseFunctions}

\subsection{Problems Using Rational Functions Only}
\begin{problem}
\input{../../modules/inverse-functions/homework/inverse-functions-no-exponents-logarithms-1}
\end{problem}
\input{../../modules/inverse-functions/homework/inverse-functions-no-exponents-logarithms-1-solutions}

\begin{problem}
\input{../../modules/inverse-functions/homework/inverse-functions4}
\end{problem}

\subsection{Problems Involving Exponents, Logarithms}
\begin{problem}
\input{../../modules/inverse-functions/homework/inverse-functions1}
\end{problem}
\input{../../modules/inverse-functions/homework/inverse-functions1-solutions}

\section{Logarithms and Exponent Basics}\label{secMPSLogarithmsExponentsBasics}
\subsection{Exponents Basics}
\begin{problem}
\input{../../modules/exponential-functions/homework/exponent-simplify}
\end{problem}
\input{../../modules/exponential-functions/homework/exponent-simplify-solutions}

\subsection{Logarithm Basics}
\begin{problem}
\input{../../modules/logarithms/homework/logarithms-basic-no-properties-1}
\end{problem}
\input{../../modules/logarithms/homework/logarithms-basic-properties-1-solutions}
\begin{problem}
\input{../../modules/logarithms/homework/logarithms-combine-1}
\end{problem}
\input{../../modules/logarithms/homework/logarithms-combine-1-solutions}
\begin{problem}
\input{../../modules/logarithms/homework/logarithms-basic-properties-1}
\end{problem}
\input{../../modules/logarithms/homework/logarithms-basic-properties-1-solutions}

\subsection{Some Problems Involving Logarithms}
\begin{problem}
\input{../../modules/logarithms/homework/logarithms-equations}
\end{problem}
\input{../../modules/logarithms/homework/logarithms-equations-solutions}

\section{Derivatives}
\subsection{Derivatives and Function Graphs: basics}\label{secMPSderivativesFunGraphsBasics}
\begin{problem}
\input{../../modules/curve-sketching/homework/match-graph-to-derivative-graph-2}
\end{problem}
\begin{problem}
\input{../../modules/curve-sketching/homework/match-graph-to-derivative-graph-2-solution}
\end{problem}
\begin{problem}
\input{../../modules/curve-sketching/homework/match-graph-to-derivative-graph}
\end{problem}
\subsection{Product and Quotient Rules}\label{secMPSproductQuotientRules}

\begin{problem}
\input{../../modules/derivatives/homework/derivatives-product-quotient-rule-1}
\end{problem}
\input{../../modules/derivatives/homework/derivatives-product-quotient-rule-1-solutions}

\begin{problem}
\input{../../modules/derivatives/homework/derivatives-product-quotient-rule-2}
\end{problem}
\input{../../modules/derivatives/homework/derivatives-product-quotient-rule-2-solutions}

\begin{problem}
\input{../../modules/derivatives/homework/derivatives-product-quotient-rule-3}
\end{problem}
\input{../../modules/derivatives/homework/derivatives-product-quotient-rule-3-solutions}

\subsection{Basic Trigonometric Derivatives}\label{secMPStrigDerivatives}
\begin{problem}
\input{../../modules/derivatives/homework/derivatives-trig}
\end{problem}
\begin{problem}
\input{../../modules/derivatives/homework/derivatives-trig-2}
\end{problem}
\subsection{Natural Exponent Derivatives}
\input{../../modules/exponential-functions/homework/exponent-derivative}

\subsection{The Chain Rule}\label{secMPSchainRule}

\begin{problem}
\input{../../modules/chain-rule/homework/chain-rule1}
\end{problem}
\input{../../modules/chain-rule/homework/chain-rule1-solutions}

\begin{problem}
\input{../../modules/chain-rule/homework/chain-rule2}
\end{problem}

\begin{problem}
\input{../../modules/chain-rule/homework/chain-rule3}
\end{problem}
\input{../../modules/chain-rule/homework/chain-rule3-solutions}

\begin{problem}
\input{../../modules/chain-rule/homework/chain-rule4}
\end{problem}
\begin{problem}
\input{../../modules/chain-rule/homework/chain-rule5}
\end{problem}
\subsection{Problem Collection All Techniques}
\begin{problem}
\input{../../modules/derivatives/homework/derivative-problem-collection-1}
\end{problem}
\input{../../modules/derivatives/homework/derivative-problem-collection-1-solutions}

\subsection{Implicit Differentiation}\label{secMPSImplicitDifferentiation}
\begin{problem}
\input{../../modules/implicit-differentiation/homework/implicitly-differentiate}
\end{problem}
\begin{problem}
\input{../../modules/implicit-differentiation/homework/implicit-tangent}
\end{problem}
\input{../../modules/implicit-differentiation/homework/implicit-tangent-solutions}

\subsection{Implicit Differentiation and Inverse Trigonometric Functions}
\begin{problem}
\input{../../modules/implicit-differentiation/homework/implicit-inverse-trig1}
\end{problem}
\begin{problem}
\input{../../modules/implicit-differentiation/homework/implicit-inverse-trig2}
\end{problem}

\subsection{Derivative of non-Constant Exponent with non-Constant Base}\label{secMPSDerivativeNonConstExponent}
\begin{problem}
\input{../../modules/logarithms/homework/derivatives-non-const-exponent-2}
\end{problem}
\input{../../modules/logarithms/homework/derivatives-non-const-exponent-2-solutions}

\begin{problem}
\input{../../modules/logarithms/homework/derivatives-non-const-exponent}
\end{problem}

\subsection{Related Rates}\label{secMPSrelatedRates}
\begin{problem}
\input{../../modules/related-rates/homework/related-rates-text-problems-2}
\end{problem}
\input{../../modules/related-rates/homework/related-rates-text-problems-2-solutions}
\begin{problem}
\input{../../modules/related-rates/homework/related-rates-text-problems}
\end{problem}
\input{../../modules/related-rates/homework/related-rates-text-problems-solutions}

\section{Graphical Behavior of Functions}
\subsection{Mean Value Theorem}\label{secMPS-MVT}

\begin{problem}
\input{../../modules/maxima-minima/homework/find-the-mvt-points-1}
\end{problem}

\begin{problem}
\input{../../modules/maxima-minima/homework/mvt-to-estimate-fmax-fmin-1}
\end{problem}

\begin{problem}
\input{../../modules/maxima-minima/homework/mvt-show-only-one-root-1}
\end{problem}
\input{../../modules/maxima-minima/homework/mvt-show-only-one-root-1-solutions}

\subsection{Maxima, Minima}\label{secMPSoneVariableMinMax}
\subsubsection{Closed Interval method}\label{secMPSclosedInterval}
\begin{problem}
\input{../../modules/maxima-minima/homework/maxima-minima}
\end{problem}

\begin{problem}
\input{../../modules/logarithms/homework/logarithm-physics}
\end{problem}

\subsubsection{Derivative tests}
\begin{problem}
\input{../../modules/maxima-minima/homework/local-maxima-minima-derivative-tests-1}
\end{problem}

\subsubsection{Optimization}\label{secMPSoptimization}
\begin{problem}
\input{../../modules/maxima-minima/homework/maxima-minima-text-problems-1}
\end{problem}
\begin{problem}
\input{../../modules/optimization/homework/optimization-problem-collection-1}
\end{problem}
\input{../../modules/optimization/homework/optimization-problem-collection-1-solutions}

\subsection{Function Graph Sketching}\label{secMPSfunctionGraphSketching}
\begin{problem}
\input{../../modules/curve-sketching/homework/sketch-graph-with-all-the-details}
\end{problem}
\input{../../modules/curve-sketching/homework/sketch-graph-with-all-the-details-solutions}

\begin{problem}
\input{../../modules/curve-sketching/homework/sketch-graph-with-all-the-details-2}
\end{problem}

\section{Linearizations and Differentials} \label{secMPSLinearizationAndDifferentials}
\begin{problem}
\input{../../modules/differentials/homework/linearization-problems-1}
\end{problem}
\input{../../modules/differentials/homework/linearization-problems-1-solutions}

\section{Integration Basics}
\subsection{Riemann Sums}\label{secMPSRiemannSums}
\begin{problem}
\input{../../modules/integration/homework/riemann-sum-problems-1}
\end{problem}
\input{../../modules/integration/homework/riemann-sum-problems-1-solutions}

\subsection{Antiderivatives}\label{secMPSantiderivatives}
\begin{problem}
\input{../../modules/antiderivatives/homework/antiderivatives-basic-integrals-1}
\end{problem}
\begin{problem}
\input{../../modules/antiderivatives/homework/antiderivatives-basic-integrals-initial-condition-1}
\end{problem}
\begin{problem}
\input{../../modules/antiderivatives/homework/antiderivatives-verify-by-differentiation-1}
\end{problem}

\subsection{Basic Definite Integrals} \label{secMPSBasicDefiniteIntegrals}
\begin{problem}
\input{../../modules/integration/homework/basic-definite-integrals-1}
\end{problem}
\input{../../modules/integration/homework/basic-definite-integrals-1-solutions}
\begin{problem}
\input{../../modules/integration/homework/basic-definite-integrals-2}
\end{problem}
\subsection{Fundamental Theorem of Calculus Part I}\label{secMPSFTCpart1}
\begin{problem}
\input{../../modules/integration/homework/FTC-part1-problems-1}
\end{problem}
\input{../../modules/integration/homework/FTC-part1-problems-1-solutions}

\subsection{Integration with The Substitution Rule}
\label{secMPSintegrationSubstitutionRule}
\subsubsection{Substitution in Indefinite Integrals}
\label{secMPSintegrationSubstitutionRuleIndefinite}
\begin{problem}
\input{../../modules/substitution-rule/homework/substitution-rule-1}
\end{problem}
\input{../../modules/substitution-rule/homework/substitution-rule-1-solutions}
\begin{problem}
\input{../../modules/substitution-rule/homework/substitution-rule-2}
\end{problem}
\input{../../modules/substitution-rule/homework/substitution-rule-2-solutions}

\subsubsection{Substitution in Definite Integrals}
\label{secMPSintegrationSubstitutionRuleDefinite}
\begin{problem}
\input{../../modules/substitution-rule/homework/substitution-rule-definite-1}
\end{problem}
\input{../../modules/substitution-rule/homework/substitution-rule-definite-1-solutions}

\section{First Applications of Integration}
\subsection{Area Between Curves}\label{secMPSareaBetweenCurves}
\begin{problem}
\input{../../modules/area-between-curves/homework/area-between-curves-problems-1}
\end{problem}
\input{../../modules/area-between-curves/homework/area-between-curves-problems-1-solutions}
\subsection{Volumes of Solids of Revolution}\label{secMPSvolumesSolidsRevolution}
\subsubsection{Problems Geared towards the Washer Method}\label{secMPSvolumesSolidsRevolutionWashers}
\begin{problem}
\input{../../modules/volumes/homework/solids-of-revolution-problems-1}
\end{problem}
\input{../../modules/volumes/homework/solids-of-revolution-problems-1-solutions}

\subsubsection{Problems Geared towards the Cylindrical Shells Method}\label{secMPSvolumesSolidsRevolutionShells}
\begin{problem}
\input{../../modules/volumes/homework/solids-of-revolution-cylindrical-shells-1}
\end{problem}
\end{document}


\section{Implementation} \label{sec:implementation}

\subsection{Parameters of the Implementation}

\begin{table}[tbh]
\begin{center}
\begin{tabular}{p{.2 \textwidth} | p{.6 \textwidth} | p{.2 \textwidth}}\hline
Field & Description & Default value\\ \hline
$P\in\mathbb{Z}$ & Number of populations. & 1 \\
$n\in\mathbb{Z}$ & Maximum number of pure strategies per population. & - \\
$S\in\mathbb{Z}^{P}$ & Vector of pure strategies in each population, such that $1<S(i)\leq n$. & ones(G.P, 1) * G.n \\
$m\in\mathbb{R}^P$ & Vector with the mass of each population. & ones(G.P, 1) \\
$x0\in\mathbb{R}^{P\times n}$ & Initial state of the society (normalized). & random \\
$f:\mathbb{R}^{P\times n} \rightarrow \mathbb{R}^{1\times n}$ or $f:\mathbb{R}^{P\times n} \rightarrow \mathbb{R}^{P\times n}$ & Function that returns the fitness the population's strategies. It might return a vector with the fitness of a single population or the fitness of the whole society. & - \\
pop\_wise & If true, the fitness function $f$ returns the fitness of a single population. Otherwise, $f$ returns the fitness of the whole society. & True \\
\hline
\end{tabular}
\end{center}
\caption{Parameters of the game.}
\label{tab:game}
\end{table}

\begin{table}[tbh]
\begin{center}
\begin{tabular}{p{.2 \textwidth} | p{.6 \textwidth} | p{.2 \textwidth}}\hline
Field & Description & Default value\\ \hline
ode & ODE solver for the evolutionary dynamics. & `ode45' \\
dynamics & Evolutionary dynamic. Current version support combinations of {`rd', `maynard\_rd',  `bnn', `smith',  `logit'}. & `rd' \\
gamma & Defines the weight given to each dynamic when using combined dynamics. &  $\sum \gamma(i) = 1$ \\
step & Simulations are made using the time spam: step:step:time+step. & $0.01$ \\
tol & Defines the error tolerance of the ODE solver, namely, RelTol and AbsTol. If it is not defined, then RelTol and AbsTol can be assigned independently. &  RelTol=AbsTol=$10^{-4}$\\
stop\_c  & Enables the interruption of the simulations if the norm of the state's change is less than the parameter c\_error. & False \\
c\_error &  Convergence error used to stop the simulations. & $10^{-5}$ \\
verb & Allows the display of information, such as the dynamics used and the time spent running the game. & True \\
\hline
\end{tabular}
\end{center}
\caption{Parameters of the dynamical implementation.}
\label{tab:req_a}
\end{table}

\begin{table}[tbh]
\begin{center}
\begin{tabular}{p{.2 \textwidth} | p{.6 \textwidth} | p{.2 \textwidth}}\hline
Field & Description & Default value \\ \hline
$N\in\mathbb{Z}$ & Number of agents & 100 \\
$R\in\mathbb{R}$ & Rate of the Poisson clock & 1 \\
time & Run time of the simulation (number of iterations in the discrete case). & 30 \\
revision\_protocol & Revision protocol. The current version support one of the following: `comparison2average', `pairwise\_comparison', `logit\_choice', `proportional\_imitation'. & `proportional\_imitation' \\ \hline
\end{tabular}
\end{center}
\caption{Parameters of the revision protocol.}
\label{tab:req_b}
\end{table}

The toolbox uses a structure that contains all the parameters required to run the simulations. The parameters of a population game are defined in Table \ref{tab:game}. On the other hand, the parameters to run simulations with large or small number of agents are specified in Tables \ref{tab:req_a} and \ref{tab:req_b}, respectively.  In this case the behavior of a population with large and small number of agents is simulated using differential equations and revision protocols, respectively. The following is an example to define a game with one population and three strategies per population:
%
\begin{lstlisting}
G = struct('n', 3, 'f', @fitness1, 'dynamics', {rd},  'ode', 'ode113', 'x0',  [0.2 .7 0.1]', 'time', 60);
\end{lstlisting}
%
$n$ defines the number of strategies per population, $f$ is a function handler that calculates the fitness of the strategies in each population, and $dynamics$ defines the name of the evolutionary dynamics that we want to use. The simulations are run using the ordinal differential equation (ODE) solver called $ode113$, with initial condition $x0 = [0.2, \, 0.7, \, 0.1 ]^\top$ during $60$ time units.
Note that the number of populations and the mass of each population are defined by default to one.  The simulation can be started by executing
%
\begin{lstlisting}
 G.run()
\end{lstlisting}

On the other hand, the following structure is used to define a population game with small number of agents per population:
%
\begin{lstlisting}
G = struct('N', 200, 'n', 3, 'f', @fitness1, 'x0',  [0.2 .7 0.1]', 'ode', 'ode113', 'time', 10000, 'eta', 0.02, 'revision_protocol', @proportional_imitation);
\end{lstlisting}
%
The finite population case uses the same parameters than the dynamical implementation, except for the dynamical model. However, it is necessary to define the revision protocol  and the number of agents $N$ per population.
Table \ref{tab:req_b} contains the list of parameters required to run the revision protocol. The simulation of the revision protocol can be started by executing
\begin{lstlisting}
 G.run_finite()
\end{lstlisting}

The functions \verb|G.graph()| and \verb|G.graph_evolution()| can be used to graph the simplex and the state evolution of the society for both cases.

\subsection{Example: Rock-Paper-Scissors Game}

\begin{figure}[th]
  \centering
  \begin{subfigure}[b]{0.4\textwidth}
	  \includegraphics[width=\textwidth]{./images/test_finite_proportional_imitation.eps}
	  \caption{Small population.}
	  \label{fig:finite1_protocol}
  \end{subfigure}
  ~
  \begin{subfigure}[b]{0.4\textwidth}
	  \includegraphics[width=\textwidth]{./images/test1_simplex_rd.eps}
	  \caption{Large popultion.}
	  \label{fig:finite1_dynamics}
  \end{subfigure}
  \caption{Rock-paper-scissors game with a) proportional imitation revision protocol and b) replicator dynamics.}
  \label{fig:finite1}
\end{figure}

\begin{figure}[th]
  \centering
  \begin{subfigure}[b]{0.4\textwidth}
	  \includegraphics[width=\textwidth]{./images/test_finite_comparison2average.eps}
	  \caption{Small population.}
	  \label{fig:finite2_protocol}
  \end{subfigure}
  ~
  \begin{subfigure}[b]{0.4\textwidth}
	  \includegraphics[width=\textwidth]{./images/test1_simplex_bnn.eps}
	  \caption{Large popultion.}
	  \label{fig:finite2_dynamics}
  \end{subfigure}
  \caption{Rock-paper-scissors game with a) comparison to average revision protocol and b) BNN dynamics.}
  \label{fig:finite2}
\end{figure}

\begin{figure}[tbh]
  \centering
  \begin{subfigure}[b]{0.4\textwidth}
	  \includegraphics[width=\textwidth]{./images/test_finite_pairwise_comparison.eps}
	  \caption{Small population.}
	  \label{fig:finite3_protocol}
  \end{subfigure}
  ~
  \begin{subfigure}[b]{0.4\textwidth}
	  \includegraphics[width=\textwidth]{./images/test1_simplex_smith.eps}
	  \caption{Large popultion.}
	  \label{fig:finite3_dynamics}
  \end{subfigure}
  \caption{Rock-paper-scissors game with a) pairwise comparison revision protocol and b) Smith dynamics.}
  \label{fig:finite3}
\end{figure}

\begin{figure}[tbh]
  \centering
  \begin{subfigure}[b]{0.4\textwidth}
	  \includegraphics[width=\textwidth]{./images/test_finite_logit_choice.eps}
	  \caption{Small population.}
	  \label{fig:finite4_protocol}
  \end{subfigure}
  ~
  \begin{subfigure}[b]{0.4\textwidth}
	  \includegraphics[width=\textwidth]{./images/test1_simplex_logit.eps}
	  \caption{Large popultion.}
	  \label{fig:finite4_dynamics}
  \end{subfigure}
  \caption{Rock-paper-scissors game with a) logit choice revision protocol and b) Logit dynamics with $\eta=0.02$.}
  \label{fig:finite4}
\end{figure}

In this section, we implement rock-paper-scissors game with both revision protocols and evolutionary dynamics presented above, to observe the behavioral differences between a society with small number of agents and its approximation to a dynamical system.
The game has only one population with three strategies, denoted $x = [x_1, \, x_2, \, x_3]^\top$. The fitness function is defined as $F(x)=Ax$, where A is equal to
\begin{equation}
  A = \begin{pmatrix}
  2  & 1 &  3 \\
  3  & 2 &  1 \\
  1 &  3 &  2
  \end{pmatrix}
\end{equation}
Note that we modify the payoff matrix proposed in the literature to ensure positive payoffs.
Fig. \ref{fig:finite1} to \ref{fig:finite4} show the evolution of the society with each revision protocol and its approximation to differential equations. We set the initial condition $x_0 = [0.2, \, 0.7, \, 0.1 ]^\top$. The small population cases are made with $200$ agents and $10000$ iterations. The dynamical cases are run during
30 time units.

%The evolution might take place in days, months, years.


\chapter{Conclusion}
This paper uses the Gibbs sampler with a Metropolis-Hastings step to generate new samples from a NHPP. Test statistics are used to check if the new samples are from the NHPP. The NHPP used has a rate function which is a combination of a log-linear function and a power-law function. By testing the samples for different parameters in the rate function, non of the null hypothesis were rejected. Hence this sampler can be used to generate new samples from a NHPP given a data set.