\documentclass[twocolumn]{article}
% \documentclass{article}

\setlength{\oddsidemargin}{-0.5in}
\setlength{\evensidemargin}{-0.5in}
\setlength{\topmargin}{0.0in}
\setlength{\headheight}{0in}
\setlength{\headsep}{0in}
\setlength{\textwidth}{7.0in}
\setlength{\textheight}{9.5in}
\setlength{\parindent}{0in}
\setlength{\parskip}{0.05in}
\setlength{\columnseprule}{0.3pt}

\setlength\columnsep{25pt}

\usepackage{fancyvrb}
\usepackage{relsize}
\usepackage{hyperref}

\usepackage{listings}
\usepackage{amsmath}

\begin{document}

\lstset{xleftmargin=5mm,framexleftmargin=10mm,basicstyle=\footnotesize}

Name: \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_

Directions: MAKE SURE TO COPY YOUR ANSWERS TO A SEPARATE SHEET FOR SENDING
ME AN ELECTRONIC COPY LATER.

{\bf 1.} (10) With the new reference classes, R has been moving somewhat
away from its philosophy of having no \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_.

{\bf 2.} (30) The {\bf pdist()} function in the R package of the same
name computes distances of rows of a matrix A and rows of a matrix B.
(For our purposes, they will be distinct matrices.)  Say A has {\it n}
rows and B has {\it p} rows (the two matrices must have the same number
of columns).  Then the major output of {\bf pdist()} is an $n \times p$
matrix, with the $(i,j)$ element giving the distance from row $i$ of A
to row $j$ of B. However, these distances are stored in linear form, in
row-major order, i.e.\ first all distances from row 1 of A are stored,
then all distances from row 2 of A, etc.

Here is an example:

\begin{lstlisting}
> a
     [,1] [,2] [,3] [,4]
[1,]    0    1    1    1
[2,]    1    0    0    1
> b
     [,1] [,2] [,3] [,4]
[1,]    1    1    0    1
[2,]    0    0    0    1
> str(pdist(a,b))
Formal class 'pdist' [package "pdist"] with 4 slots
  ..@ dist    : atomic [1:4] 1.41 1.41 1 1
  .. ..- attr(*, "Csingle")= logi TRUE
  ..@ n       : int 2
  ..@ p       : int 2
  ...
\end{lstlisting}

The function below takes a {\bf pdist} object {\bf pdout} and returns
the distances in R matrix form, again the numbers in row 1 being
distances from row 1 of A to rows of B.  For example, 

\begin{lstlisting}
> pdtomat(pdist(a,b))
         [,1] [,2]
[1,] 1.414214    0
[2,] 1.732051    1
\end{lstlisting}

Fill in the blank.  {\bf NOTE:} Write this as ``\#1'' in your quiz file,
not ``\#1a''.

\begin{lstlisting}
pdtomat <- function(pdout) {
   n <-  pdout@n
   p <-  pdout@p
   blank   (a whole line)
}
\end{lstlisting}

{\bf 3.} (60) The R {\bf head()} generic function prints the first few
pieces of the object it is called on.  For vectors, this is the first
few elements; for matrices and data frames, it is the first few rows.
The default view of ``few'' is 6.  

Here we will extend {\bf head()} to objects of class {\bf "ut"} in
Section 12.3.2.  Fill in the blanks:

\begin{lstlisting}
blank (a) <- function(utmat,k=6) {
   n <- blank (b)
   k <- blank (c)
   for (row in 1:k) {
      zeros <- blank (d)
      cat(zeros," ")
      for (col in row:n) {
         rowcolval <- blank (e)
         cat(rowcolval," ")
      }
      cat("\n")
   }
}
\end{lstlisting}

{\bf Solutions:}

{\bf 1.} side effects

{\bf 2.}

\begin{lstlisting}
pdtomat <- function(pdout) {
   n <- pdout@n
   p <- pdout@p
   matrix(pdout@dist,byrow=TRUE,ncol=p)
}
\end{lstlisting}

{\bf 3.}

\begin{lstlisting}
head.ut <- function(utmat,k=6) {
   n <- length(utmat$ix)
   k <- min(k,n)
   for (row in 1:k) {
      zeros <- rep(0,row-1)
      cat(zeros," ")
      for (col in row:n) {
         rowcolval <- utmat$mat[utmat$ix[col]+row-1]
         cat(rowcolval," ")
      }
      cat("\n")
   }
}
\end{lstlisting}

\end{document}

