
\documentclass[twocolumn]{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}
\usepackage{fancyvrb}
\usepackage{relsize}
\usepackage{hyperref}

\usepackage{listings}

\begin{document}

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

Directions: {\bf Work only on this sheet} (on both sides, if needed); do
not turn in any supplementary sheets of paper. There is actually plenty
of room for your answers, as long as you organize yourself BEFORE
starting writing.

{\bf 1.} (30)  Fill in the blanks in the following R code, which
computes and returns the matrix A in Equation (10.20), C = AX/n.  Note:
R's exponentiation operator is \verb#^#, so that for instance \verb#2^3#
is 8.

R's {\bf diag()} function is quite versatile.  When applied to a matrix,
you get a vector (formed from the diagonal of the matrix), and vice versa.

\begin{Verbatim}[fontsize=\relsize{-2}]
makeamat <- fqnction(n,q)  {
   m <- matrix(nrow=n,ncol=n)
   for (i in 1:n) {
      for (j in i:n) {
         if (i == j) {
                                  # first blank (one line)
         }
         else {
                                  # second blank (one line)
            m[j,i] <- m[i,j]
         }
      }
   }
   return(m)
}
\end{Verbatim}

{\bf 2.} (30) Fill in the blanks in the following R code to implement the
Jacobi algorithm on a GPU.  It solves {\bf ax = b}, returning {\bf x}.

\begin{Verbatim}[fontsize=\relsize{-2}]
library(gputools)

jcb <- function(a,b,eps) {
   n <- length(b)
   d <- diag(a)  # a vector, not a matrix
   tmp <- diag(d)  # a matrix, not a vector
   o <-                     # first blank (partial line)
   di <- 1/d
   x <- b  # initial guess, could be better
   repeat {
      oldx <- x
      tmp <-                # second blank (partial line)
      tmp <- b - tmp
      x <- di * tmp  # elementwise multiplication
                            # third blank (one line)
   }
}
\end{Verbatim}

{\bf 3.} (40) Fill in the blanks in the following R code, which implements
smoothing (removal of ``blips'') on sound data (loudness) {\bf snd},
cutting off frequencies for $k$  greater than {\bf maxidx} in (10.13).
R's {\bf fft()} function, when applied to a vector {\bf x}, finds the
Discrete Fourier Transform of {\bf x}.  If the optional second argument
{\bf inverse} is set to TRUE, it will find the inverse transform
instead.  You'll find R's {\bf rep()} function useful; e.g. {\bf
rep(2,5)} is the vector (2,2,2,2,2).

\begin{Verbatim}[fontsize=\relsize{-2}]
lp <- function(snd,maxidx) {
         four <-            # first blank (partial line)
         n <- length(four)
         newfour <-         # second blank (partial line)
                            # third blank (one line)
}
\end{Verbatim}

{\bf Solutions:}

{\bf 1.}

\begin{Verbatim}[fontsize=\relsize{-2}]
makeamat <- function(n,u)  {
   m <- matrix(nrow=n,ncol=n)
   for (i in 1:n) {
      for (j in i:n) {
         if (i == j) {
            m[i,i] <- u^((i-1)^2)
         }
         else {
            m[i,j] <- u^((i-1)*(j-1))
            m[j,i] <- m[i,j]
         }
      }
   }
   return(m)
}
\end{Verbatim}

{\bf 2.}

\begin{Verbatim}[fontsize=\relsize{-2}]
library(gputools)

jcb <- function(a,b,eps) {
   n <- length(b)
   d <- diag(a)  # a vector, not a matrix
   tmp <- diag(d)  # a matrix, not a vector
   o <- a - diag(d)
   di <- 1/d
   x <- b  # initial guess, could be better
   repeat {
      oldx <- x
      tmp <- gpuMatMult(o,x)
      tmp <- b - tmp
      x <- di * tmp  # elementwise multiplication
      if (sum(abs(x-oldx)) < n * eps) return(x)
   }
}
\end{Verbatim}

{\bf 3.} 

\begin{Verbatim}[fontsize=\relsize{-2}]
lp <- function(snd,maxidx) {
         four <- fft(snd)
         n <- length(four)
         newfour <- c(four[1:maxidx],rep(0,n-maxidx))
         return(Re(fft(newfour,inverse=T)/n))
}
\end{Verbatim}

\end{document}

