Questions:
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. x=c(rep(0,500),seq(0,1,length.out=1000), rep(1,500)) y=c(seq(-1,1,length.out=500),rep(0,1000), seq(-1,1,length.out=500)) z=rbind(x,y) plot(y~x, xlim=c(-3,3), ylim=c(-3,3))

Then, 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.

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

Hint: Use x11() to open a new plotting window in R. Upload your document as a .RMD file. I will know if your assignment is correct if the animation runs. correctly

library(tidyverse)
library(animation)
library(gifski)

Build the first letters for both your first and last name using point plots in R

# build letter C
X_c = c(seq(-2,-1,length.out=500),rep(-2,1000), seq(-2,-1,length.out=500))
Y_c = c(rep(-1,500),seq(-1,1,length.out=1000), rep(1,500))
plot(Y_c ~ X_c, xlim = c(-3, 3), ylim = c(-3, 3), main = "Point plot of letter C", ylab = "Y", xlab = "X")

# build the letter O
X_o = c(seq(0,1,length.out=500),rep(0,1000), seq(0,1,length.out=500), rep(1,1000))
Y_o = c(rep(-1,500),seq(-1,1,length.out=1000), rep(1,500), seq(-1,1,length.out=1000))
plot(Y_o ~ X_o, xlim = c(-3, 3), ylim = c(-3, 3), main = "Point plot of letter O", ylab = "Y", xlab = "X")

# build the letters C and O
X_co = c(seq(-2,-1,length.out=500),rep(-2,1000), seq(-2,-1,length.out=500), 
      seq(0,1,length.out=500),rep(0,1000), seq(0,1,length.out=500), rep(1,1000)
      )
Y_co = c(rep(-1,500),seq(-1,1,length.out=1000), rep(1,500),
      rep(-1,500),seq(-1,1,length.out=1000), rep(1,500), seq(-1,1,length.out=1000)
     )
Z = rbind(X_co, Y_co)
# plot the letters C and O
plot(Y_co ~ X_co, xlim = c(-3,3), ylim = c(-3,3), main = "Point plot of letters C and O", ylab = "Y", xlab = "X")

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

# Write a code to left multiply (%>%) a square matrix (x) against each of the vectors of points (y)

# create the left_multiply function
left_multiply  <- function(x,y){
   x %*% y
}

# apply the function
identity_matrix = diag(6)
left_multiply(matrix(rep(seq(1,6, length.out= 6), 6), nrow = 6, ncol = 6), identity_matrix)
##      [,1] [,2] [,3] [,4] [,5] [,6]
## [1,]    1    1    1    1    1    1
## [2,]    2    2    2    2    2    2
## [3,]    3    3    3    3    3    3
## [4,]    4    4    4    4    4    4
## [5,]    5    5    5    5    5    5
## [6,]    6    6    6    6    6    6

Transformations

Shear Transformation

anim = ani.record(reset = TRUE, replay.cur = FALSE)
# Create display window for animation
x11()
# Shear transformation
for (i in seq(0,1,length.out = 20)) {
  z_shear <- apply(Z,2,function(x) left_multiply(x,matrix(c(1,i,0,1),nrow=2,ncol=2)))
  plot(z_shear[2,] ~ z_shear[1,], xlim = c(-3,3), ylim = c(-3,3), 
       main = "Shear Transformation", ylab = "Y", xlab = "X")
}

Scaling Transformation

anim = ani.record(reset = TRUE, replay.cur = FALSE)
# Create display window for animation
x11()
# Scale Transformation
for (i in seq(1,4,length.out = 20)) {
  z_scale <- apply(Z, 2, function(x) left_multiply(x, matrix(c(i,0,0,i), nrow=2, ncol=2)))
  plot(z_scale[2,] ~ z_scale[1,], xlim = c(-3,3), ylim = c(-3,3), 
       main = "Scaling Transformation", ylab = "Y", xlab = "X")
}

Rotation Transformation

anim = ani.record(reset = TRUE, replay.cur = FALSE)
# Create display window for animation
x11()
# Rotation Transformation
for (i in seq(0, pi*2, length.out=20)) {
  z_rotation <- apply(Z, 2, function(x) left_multiply(x, matrix(c(cos(i), -sin(i), sin(i), cos(i)), nrow=2, ncol=2)))
  plot(z_rotation[2,] ~ z_rotation[1,], xlim = c(-3,3), ylim = c(-3,3), 
       main = "Rotation Transformation", ylab = "Y", xlab = "X")
}

Projection Transformation

anim = ani.record(reset = TRUE, replay.cur = FALSE)
# Create display window for animation
x11()
# Projection Transformation
for (i in seq(0, 2*pi, length.out=20)) {
  z_proj <- rbind(Z, rep(0, ncol(Z)))
  z_projection <-apply(z_proj, 2, function(x) left_multiply(x, matrix(c(1, 0, 0, 0, cos(i), -sin(i), 0, sin(i), cos(i)), nrow=3, ncol=3)))
  plot(z_projection[2,] ~ z_projection[1,], xlim = c(-3,3), ylim = c(-3,3), 
       main = "Projection Transformation", ylab = "Y", xlab = "X")
}