########################################################################
# Taking the Example from Lecture 12                                   #
########################################################################

#a <- rbind(cbind(1,1,1,1,1),cbind(2,3,1,4,1))
a <- cbind(5,2,3,4,1)

########################################################################
# First we compute a a'                                                #
########################################################################

aat <- a%*%t(a)

########################################################################
# Next, compute the eigenvectors for a a' -> u                         #
########################################################################

eaat <- eigen(aat)
u <- eaat$vectors

########################################################################
# Next, take the outer product of the matrices a'a                     #
########################################################################

ata <- t(a)%*%a

########################################################################
# The v matrix is the matrix of eigenvectors from this product         #
########################################################################

eata <- eigen(ata)
v <- eata$vectors

########################################################################
# The s matrix is the zero matrix with the first two diagonal elments  #
# being the square root of thenon-zero eigenvalues of the outer product#
########################################################################

s <- matrix(0,nrow=nrow(a),ncol=ncol(a))

s[1,1] <- sqrt(eata$values[1])
#s[2,2] <- sqrt(eata$values[2])

########################################################################
# Test for orthogonality                                               #
########################################################################

print(a%*%v[,cbind(3,4,5)])
