R version 2.9.0 (2009-04-17) Copyright (C) 2009 The R Foundation for Statistical Computing ISBN 3-900051-07-0 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > # chex57.r Demonstration of Givens transformations > # for simple linear regression problem > # Example 5.7 > # > X <- matrix(c(rep(1,4),(1:4)),4,2) # design matrix > X [,1] [,2] [1,] 1 1 [2,] 1 2 [3,] 1 3 [4,] 1 4 > # > # function to do Givens on col i, rows i,j > Givens <- function(X,i,j) { + a <- X[i,i] ; b <- X[j,i] + r <- sqrt(a*a+b*b) # length + Uij <- matrix( c(a/r,-b/r,b/r,a/r),2,2) # essential + print(Uij) # matrix + X[c(i,j),] <- Uij %*% X[c(i,j),] # main computation + U <- diag(4) ; # identity + U[c(i,j),c(i,j)] <- Uij # modify row/col i,j + print(U) # big matrix + Givens <- X } # return modified X > # > # go through a sequence of steps, putting zeros in X > X <- Givens(X,1,2) [,1] [,2] [1,] 0.7071068 0.7071068 [2,] -0.7071068 0.7071068 [,1] [,2] [,3] [,4] [1,] 0.7071068 0.7071068 0 0 [2,] -0.7071068 0.7071068 0 0 [3,] 0.0000000 0.0000000 1 0 [4,] 0.0000000 0.0000000 0 1 > X # U12*X [,1] [,2] [1,] 1.414214 2.1213203 [2,] 0.000000 0.7071068 [3,] 1.000000 3.0000000 [4,] 1.000000 4.0000000 > X <- Givens(X,1,3) [,1] [,2] [1,] 0.8164966 0.5773503 [2,] -0.5773503 0.8164966 [,1] [,2] [,3] [,4] [1,] 0.8164966 0 0.5773503 0 [2,] 0.0000000 1 0.0000000 0 [3,] -0.5773503 0 0.8164966 0 [4,] 0.0000000 0 0.0000000 1 > X # U13*U12*X [,1] [,2] [1,] 1.732051 3.4641016 [2,] 0.000000 0.7071068 [3,] 0.000000 1.2247449 [4,] 1.000000 4.0000000 > X <- Givens(X,2,3) [,1] [,2] [1,] 0.5000000 0.8660254 [2,] -0.8660254 0.5000000 [,1] [,2] [,3] [,4] [1,] 1 0.0000000 0.0000000 0 [2,] 0 0.5000000 0.8660254 0 [3,] 0 -0.8660254 0.5000000 0 [4,] 0 0.0000000 0.0000000 1 > X # U23*U13*U12*X [,1] [,2] [1,] 1.732051 3.464102 [2,] 0.000000 1.414214 [3,] 0.000000 0.000000 [4,] 1.000000 4.000000 > X <- Givens(X,1,4) [,1] [,2] [1,] 0.8660254 0.5000000 [2,] -0.5000000 0.8660254 [,1] [,2] [,3] [,4] [1,] 0.8660254 0 0 0.5000000 [2,] 0.0000000 1 0 0.0000000 [3,] 0.0000000 0 1 0.0000000 [4,] -0.5000000 0 0 0.8660254 > X # U14*U23*U13*U12*X [,1] [,2] [1,] 2.000000e+00 5.000000 [2,] 0.000000e+00 1.414214 [3,] 0.000000e+00 0.000000 [4,] -1.110223e-16 1.732051 > X <- Givens(X,2,4) [,1] [,2] [1,] 0.6324555 0.7745967 [2,] -0.7745967 0.6324555 [,1] [,2] [,3] [,4] [1,] 1 0.0000000 0 0.0000000 [2,] 0 0.6324555 0 0.7745967 [3,] 0 0.0000000 1 0.0000000 [4,] 0 -0.7745967 0 0.6324555 > X # U24*U14*U23*U13*U12*X [,1] [,2] [1,] 2.000000e+00 5.000000 [2,] -8.599751e-17 2.236068 [3,] 0.000000e+00 0.000000 [4,] -7.021667e-17 0.000000 > # clean and close > rm(list=ls()) > q()