Matrix Arithmetic

Image Processing (Tora)

### Library
library(magick)
Linking to ImageMagick 6.9.12.3
Enabled features: cairo, freetype, fftw, ghostscript, heic, lcms, pango, raw, rsvg, webp
Disabled features: fontconfig, x11
inp_img <- image_read("http://polytopes.net/Tora_color.png")
image_info(inp_img)
  format width height colorspace matte filesize density
1    PNG    77    133       sRGB  TRUE    22377   28x28
plot(inp_img)

mod_img <- image_modulate(inp_img, brightness = 120, saturation = 20, hue = 20)
plot(mod_img)

Matrix Operations

## Example 49
v1 <- c(2, -1, 3)
v2 <- c(-1, 0, 4)
v1 + v2
[1]  1 -1  7
## Example 50
A <- matrix(c(3, 0, -5, -1, -3, 4), nrow = 2, ncol = 3, byrow = TRUE)
B <- matrix(c(-5, 5, 2, 1, -2, 0), nrow = 2, ncol = 3, byrow = TRUE)
A + B
     [,1] [,2] [,3]
[1,]   -2    5   -3
[2,]    0   -5    4
## Example 53
A <- matrix(c(3, 0, -5, -1, -3, 4), nrow = 2, ncol = 3, byrow = TRUE)
-3 * A
     [,1] [,2] [,3]
[1,]   -9    0   15
[2,]    3    9  -12
## Example 56
v1 <- c(2, -1, 3)
v2 <- c(-1, 0, 4)
v1 %*% v2
     [,1]
[1,]   10
## Example 57
A <- matrix(c(3, 0, -5, -1, -3, 4), nrow = 2, ncol = 3, byrow = TRUE)
B <- matrix(c(-5, 5, 2, 1, -2, 0), nrow = 3, ncol = 2, byrow = TRUE)
A %*% B
     [,1] [,2]
[1,]   -5   15
[2,]   -9   -8
## Example 59
A <- matrix(c(4, -1, -5, 0, 1, -2), 2, 3, byrow = TRUE)
t(A)
     [,1] [,2]
[1,]    4    0
[2,]   -1    1
[3,]   -5   -2

Application

library(mvtnorm)
library(ggplot2)
Warning: package 'ggplot2' was built under R version 4.2.3
library(matlib)

## Standard deviation
sigma <- matrix(c(4,2,2,3), ncol = 2, nrow = 2) 

## Mean
mu <- c(1, 2)
n <- 1000
set.seed(123)
x <- rmvnorm(n = n, mean = mu, sigma = sigma)
d <- data.frame(x)
p2 <- ggplot(d, aes(x = X1, y = X2)) + geom_point(alpha = .5) + geom_density_2d()
p2

#y <- x - mu
y <- x - t(matrix(rep(mu,n), nrow=2))
#E <- eigen(sigma)
#E$vectors
#y <- y %*% t(inv(E$vectors))
dd <- data.frame(y)
p3 <- ggplot(dd, aes(x = X1, y = X2)) + geom_point(alpha = .5) + geom_density_2d()
p3

Exercise

Can you make this data centered about the point \(0\)?

What is the (approximate) shape of the contours?

Can you propose a linear transformation to rotate the contours by \(45^{\circ}\)?

Can you propose a linear transformation to rotate the contours by \(90^{\circ}\)?

Can you propose a linear transformation to make the contours invariant under rotations?