SVD demo 9/8 === author: JWS date: 9/8/17 Make a matrix and see its SVD === ```{r} A <- matrix(runif(4),2,2) A ``` === ```{r} s <- svd(A) s ``` Show how A transforms a vector "x" === I make x in a weird way. You'll see why in a minute... ```{r} U <- s\$u V <- s\$v D <- diag(s\$d) pis <- seq(from=0,to=2*pi,length=100) x1 <- sin(pis) x2 <- cos(pis) x <- cbind(x1,x2) ind <- 29 Ax <- as.vector(t(A%*%x[ind,])) ``` SVD = ? === Ax = (U(D(V'x))) plot each part ```{r} Vtx <- as.vector(t(V)%*%x[ind,]) DVtx <- as.vector(D%*%t(V)%*%x[ind,]) UDVtx <- as.vector(U%*%D%*%t(V)%*%x[ind,]) ``` SVD = ? === ```{r,echo=F} plot(0,1,xlab="X[1]",ylab="X[2]",xlim=c(-2,2), ylim=c(-2,2),type="n",main="Black line is x, Red is Ax") lines(c(0,x[ind,1]),c(0,x[ind,2]),lwd=3) lines(c(0,Ax[1]),c(0,Ax[2]),col="red",lwd=3) ``` SVD step 1 = Rotation === ```{r,echo=F} plot(0,1,xlab="X[1]",ylab="X[2]",xlim=c(-2,2), ylim=c(-2,2),type="n",main="Black line is x, Blue is V'x") lines(c(0,x[ind,1]),c(0,x[ind,2]),lwd=3) lines(c(0,Ax[1]),c(0,Ax[2]),col="red",lwd=3) lines(c(0,Vtx[1]),c(0,Vtx[2]),col="blue",lwd=3) ``` SVD step 2 = Stretch === ```{r,echo=F} plot(0,1,xlab="X[1]",ylab="X[2]",xlim=c(-2,2), ylim=c(-2,2),type="n",main="Black line is x, Green is DV'x") lines(c(0,x[ind,1]),c(0,x[ind,2]),lwd=3) lines(c(0,Ax[1]),c(0,Ax[2]),col="red",lwd=3) lines(c(0,Vtx[1]),c(0,Vtx[2]),col="blue",lwd=3) lines(c(0,DVtx[1]),c(0,DVtx[2]),col="green",lwd=3) ``` SVD step 3 = Rotation === ```{r,echo=F} plot(0,1,xlab="X[1]",ylab="X[2]",xlim=c(-2,2), ylim=c(-2,2),type="n",main="Black line is x, Dashed black is UDV'x") lines(c(0,x[ind,1]),c(0,x[ind,2]),lwd=3) lines(c(0,Ax[1]),c(0,Ax[2]),col="red",lwd=3) lines(c(0,Vtx[1]),c(0,Vtx[2]),col="blue",lwd=3) lines(c(0,DVtx[1]),c(0,DVtx[2]),col="green",lwd=3) lines(c(0,UDVtx[1]),c(0,UDVtx[2]),lwd=3,lty=3) ``` See how A transforms every vector in disk === ```{r,echo=F} par(mfrow=c(1,2)) trans.x <- t(A%*%t(x)) plot(0,1,xlab="X[1]",ylab="X[2]",xlim=range(rbind(x,trans.x)), ylim=range(rbind(x,trans.x)),type="n") for (i in 1:100) lines(c(0,x[i,1]),c(0,x[i,2])) lines(c(0,x[ind,1]),c(0,x[ind,2]),lwd=3) plot(0,1,xlab="AX[1]",ylab="AX[2]",xlim=range(rbind(x,trans.x)), ylim=range(rbind(x,trans.x)),type="n") for (i in 1:100) lines(c(0,trans.x[i,1]),c(0,trans.x[i,2])) lines(c(0,trans.x[ind,1]),c(0,trans.x[ind,2]),col="red",lwd=3) ``` Suppose A is singular === ```{r} A <- matrix(c(1,-1,1,-1),2,2) A ``` === ```{r} s <- svd(A) s ``` See how A transforms every vector in disk === ```{r,echo=F} par(mfrow=c(1,2)) trans.x <- t(A%*%t(x)) plot(0,1,xlab="X[1]",ylab="X[2]",xlim=range(rbind(x,trans.x)), ylim=range(rbind(x,trans.x)),type="n") for (i in 1:100) lines(c(0,x[i,1]),c(0,x[i,2])) plot(0,1,xlab="AX[1]",ylab="AX[2]",xlim=range(rbind(x,trans.x)), ylim=range(rbind(x,trans.x)),type="n") for (i in 1:100) lines(c(0,trans.x[i,1]),c(0,trans.x[i,2])) ```