Assignment & Packages

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:
1.) Build the first letters for both your first and last name using point plots in R.
2.) 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.
3.) Use a loop that changes the transformation matrix incrementally to demonstrate shear, scaling, rotation and projection in animated fashion.

Packages utilized include tidyverse, animation and knitr

#library(magick)
library(animation) 
#library(gifski)
library(tidyverse)
library(knitr)

Initials

My initials are GM.

x=c(seq(0,1,length.out=500), rep(0,1000), 
    seq(0,1,length.out=500), rep(1,1000),
    seq(0,1,length.out=500), rep(2,1000),
    rep(3,1000)            , rep(4,1000),
    seq(2,3,length.out=500),seq(3,4,length.out=500))

y=c(rep(-1,500)          , seq(-1,1,length.out=1000), 
    rep(1,500)           , seq(1,-3,length=1000), 
    rep(-3,500)          , seq(1,-1,length=1000),
    seq(1,-1,length=1000),seq(1,-1,length=1000), 
    rep(1,500)           , rep(1,500))

z=rbind(x,y)

plot(y~x, xlim=c(-6,6), ylim=c(-6,6), col='light blue',
     main = "Initials", ylab = "Y", xlab = "X")

Left Multiple Function

leftmultiply<- function(x,y){
   x %*% y
}
leftmultiply(matrix(rep(seq(-2,2, length.out=3),3), nrow=3, ncol=3), diag(3))
##      [,1] [,2] [,3]
## [1,]   -2   -2   -2
## [2,]    0    0    0
## [3,]    2    2    2

Shear

#anim = ani.record(reset = TRUE, replay.cur = FALSE)

x11()
invisible(saveGIF(
for (i in seq(0,10,length.out=20)) {
  z1<-apply(z,2,function(x) leftmultiply(x,matrix(c(1,0,i,1), nrow=2, ncol=2)))
   plot(z1[2,]~z1[1,], xlim=c(-5,5), ylim=c(-5,5), col='orange',
        main = "Shear Transformation", ylab = "Y", xlab = "X")
   #ani.record()
}, movie.name = 'shear_animation.gif'))

include_graphics("shear_animation.gif")

Scale

x11()

invisible(saveGIF(
for (i in seq(0,8,length.out=20)) {
  z2<-apply(z,2,function(x) leftmultiply(x,matrix(c(i,0,0,i), nrow=2, ncol=2)))
   plot(z2[2,]~z2[1,], xlim=c(-5,5), ylim=c(-5,5), col='red',
        main = "Scale Transformation", ylab = "Y", xlab = "X")

}, movie.name = 'scale_animation.gif'))

include_graphics("scale_animation.gif")

Rotation

x11()

invisible(saveGIF(
for (i in seq(0,pi*2,length.out=20)) {
  z3<-apply(z,2,function(x) leftmultiply(x,matrix(c(cos(i),sin(i),-sin(i),cos(i)), nrow=2, ncol=2)))
   plot(z3[2,]~z3[1,], xlim=c(-5,5), ylim=c(-5,5), col='green',
        main = "Rotation Transformation", ylab = "Y", xlab = "X")
}, movie.name = 'r_animation.gif'))

include_graphics("r_animation.gif")

Projection

x11()

invisible(saveGIF(
for (i in seq(0,2*pi,length.out=20)) {
  tz<-rbind(z,rep(0,ncol(z)))
  z4<-apply(tz,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(z4[2,]~z4[1,], xlim=c(-5,5), ylim=c(-5,5), col='blue',
        main = "Projection Transformation", ylab = "Y", xlab = "X")

}, movie.name = 'p_animation.gif'))

include_graphics("p_animation.gif")