One of the most useful applications for linear algebra in data science is image manipulation. We often need to compress, expand, warp, skew, etc. images. To do so, we left multiply a transformation matrix by each of the point vectors.

For this assignment, build the first letters for both your first and last name using point plots in R. For example, the following code builds an H.

Ploting C & L

# Plot "C"
x0 = c(-sin(seq(0, pi, length.out = 1000)))
y0 = c(cos(seq(0, pi, length.out = 1000)))
z0 = rbind(x0,y0)
plot(y0~x0, xlim=c(-3,3), ylim=c(-3,3))

# Plot "L"
x1 = c(seq(0.5, 1.5, length.out = 100), rep(0.5, 100))
y1 = c(rep(-1, 100), seq(-1, 1, length.out = 100))
z1 = rbind(x1,y1)
plot(y1~x1, xlim=c(-3,3), ylim=c(-3,3))

# Combine the above code and plot "CL"

x = c(-sin(seq(0, pi, length.out = 1000)), seq(0.5, 1.5, length.out = 100), rep(0.5, 100))
y = c(cos(seq(0, pi, length.out = 1000)), rep(-1, 100), seq(-1, 1, length.out = 100))
z = rbind(x,y)
plot(y~x, xlim=c(-3,3), ylim=c(-3,3))

Write 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.

LM <- function(x, y) {  x %*% y
}
ani_time <- 2
frame_rate <- 30
no_frames <- ani_time * frame_rate

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

Shear

ident <- diag(2)
ident[1, 2] <- 0.5

for (i in seq(0,1,length.out= no_frames )) {
   shear_matrix <- ident + i * (diag(2) - ident)
   trans_z <- shear_matrix %*% z
   plot(trans_z[2,]~trans_z[1,], xlim=c(-3,3), ylim=c(-3,3))
}

Scaling

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

Rotation

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

Projection

for (i in seq(0,pi*2,length.out= no_frames )) {
   proj <- rbind(z,rep(0,ncol(z))) 
   projection_matrix <- matrix(c(1,0,0,0,cos(i),-sin(i),0,sin(i),cos(i)),nrow=3,ncol=3)
   trans_z <- apply(proj, 2, function(x) LM(x, projection_matrix))
   plot(trans_z[2,]~trans_z[1,], xlim=c(-3,3), ylim=c(-3,3))
}