DATA 605: Homework Week 1

First letters of my first name and last name are BB

x=c(rep(0,500), seq(0,.2,length.out=100), seq(0,.2,length.out=100),seq(0,.2,length.out=100),
    seq(0.2, 0.5, length.out=100 ),seq(0.2, 0.5, length.out=100 ),
    seq(0.2, 0.7, length.out=100 ),seq(0.2, 0.7, length.out=100 ),
    rep(0,500))

 
y=c(seq(-1,1, length.out=500),rep(0,100), rep(1,100), rep(-1,100),
    seq(0, 0.5, length.out=100),seq(1, 0.5, length.out=100),
    seq(0, -0.5, length.out=100),seq(-1, -0.5, length.out=100),
    seq(-1,1,length.out=500))

z=rbind(x,y)

plot(y~x, xlim=c(-3,3), ylim=c(-3,3))

x=c (rep(0,100), sin(seq(pi*-.05,pi,length.out=2000)), sin(seq(pi,pi*-0.1,length.out=2000)))       
y=c(seq(-1,1,length.out=100),seq(1,0,length.out=2000),  seq(0, -1,length.out=2000))        

z= rbind(x,y)
plot(y~x, xlim=c(-3,3), ylim=c(-3,3))

Ploting BB

x=c(rep(0,100), sin(seq(pi*-.05,pi,length.out=2000)), sin(seq(pi,pi*-0.1,length.out=2000)),  rep(2,100), 2+ sin(seq(pi*-.05 ,pi ,length.out=2000)), 2+ sin(seq(pi, (pi*-0.1),length.out=2000)) )       
y=c(seq(-1,1,length.out=100),seq(1,0,length.out=2000),  seq(0, -1,length.out=2000),   seq(-1,1,length.out=100),seq(1,0,length.out=2000),  seq(0, -1,length.out=2000) )        

z= rbind(x,y)
plot(y~x, xlim=c(-4,4), ylim=c(-4,4))

R code that will left multiply (%>%) a square matrix (x) against each of the vectors of points (y). Initially, that square matrix will be the Identity matrix.

# Identity matrix
iMatrix4 <- diag(4)
iMatrix4 
##      [,1] [,2] [,3] [,4]
## [1,]    1    0    0    0
## [2,]    0    1    0    0
## [3,]    0    0    1    0
## [4,]    0    0    0    1
# multiplication vector
mul_vec=c(1,2,3,4)
print(mul_vec)
## [1] 1 2 3 4
leftMultiply  <- function(x,y){
   x %*% y
}

print(leftMultiply( iMatrix4, mul_vec))
##      [,1]
## [1,]    1
## [2,]    2
## [3,]    3
## [4,]    4
# print multiplication result
print(iMatrix4*mul_vec)
##      [,1] [,2] [,3] [,4]
## [1,]    1    0    0    0
## [2,]    0    2    0    0
## [3,]    0    0    3    0
## [4,]    0    0    0    4

Use a loop that changes the transformation matrix incrementally to demonstrate 1) shear, 2) scaling, 3) rotation , and 4) projection in animated fashion.

Shear

## Shear

for (i in seq(0,1,length.out=10)) {
  z1<-apply(z,2,function(x) leftMultiply(x,matrix(c(1,i,0,1),nrow=2,ncol=2)))
   plot(z1[2,]~z1[1,], xlim=c(-3,3), ylim=c(-3,3))
}

Scale

## Scale

for (i in seq(1,4,length.out=5)) {
  z1<-apply(z,2,function(x) leftMultiply(x,matrix(c(i,0,0,i),nrow=2,ncol=2)))
   plot(z1[2,]~z1[1,], xlim=c(-3,3), ylim=c(-3,3))
}

Rotation

## Rotation 

for (i in seq(0,pi*2,length.out=5)) {
  z1<-apply(z,2,function(x) leftMultiply(x,matrix(c(cos(i),-sin(i),sin(i),cos(i)),nrow=2,ncol=2)))
   plot(z1[2,]~z1[1,], xlim=c(-3,3), ylim=c(-3,3))
}

Projection

## Projection

for (i in seq(0,2*pi,length.out=10)) {
  tempZ<-rbind(z,rep(0,ncol(z)))
  z1<-apply(tempZ,2,function(x) leftMultiply(x,matrix(c(1,0,0,0,cos(i),-sin(i),0,sin(i),cos(i)),nrow=3,ncol=3)))
   plot(z1[2,]~z1[1,], xlim=c(-3,3), ylim=c(-3,3), col='grey')
}