(HW=Homework!) 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.
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(gifski)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(plotly)
## Loading required package: ggplot2
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(anim.plots)
# Plot SL
x = c(sin(seq(-pi*.3,pi*2.3,length.out=2000))/2-1,seq(0,1,length.out=1000),c(rep(0,200)),c(rep(0,200)),seq(0,1,length.out=1000))
y = c(seq(-1,1,length.out=2000),rep(0,1000),seq(0,2,length.out=200),seq(0,2,length.out=200),rep(0,1000))
z=rbind(x,y)
plot(y~x, xlim=c(-3,3), ylim=c(-3,3))
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.
leftMultiply <- function(x,y){
x %*% y
}
leftMultiply(matrix(rep(seq(1,3, length.out=3),3), nrow = 3, ncol = 3),diag(3))
## [,1] [,2] [,3]
## [1,] 1 1 1
## [2,] 2 2 2
## [3,] 3 3 3
for (i in seq(0,2,length.out=10)) {
shear <- leftMultiply(matrix(c(1,0,i,1),byrow=T,nrow=2), z)
plot(shear[2,]~shear[1,], xlim=c(-3,2), ylim=c(-5,3))
}
for (i in seq(.5,1.5,length.out=10)) {
scale <- leftMultiply(matrix(c(i,0,0,i),byrow=T,nrow=2), z)
plot(scale[2,]~scale[1,], xlim=c(-3,2), ylim=c(-3,3))
}
for (i in seq(0,2*pi,length.out=10)) {
rotate <- leftMultiply(matrix(c(cos(i),sin(i),-sin(i),cos(i)),byrow=T,nrow=2), z)
plot(rotate[2,]~rotate[1,], xlim=c(-3,2), ylim=c(-3,3))
}
project_z <- rbind(z, rep(0, ncol(z)))
for (i in seq(0,2*pi,length.out=5)) {
project <- leftMultiply(matrix(c(1,0,0,0,cos(i),sin(i),0,-sin(i),cos(i)),byrow=T,nrow=3), project_z)
plot(project[2,]~project[1,], xlim=c(-3,2), ylim=c(-3,3))
}