S 2 ベクトルの表現と1次変換

2.1 p.10の例

a <- c(3,1)
b <- c(-1,2)
c <- a + b

plot(0, 0, xlim=c(min(0,a[1],b[1],c[1]), max(0,a[1],b[1],c[1])),
     ylim=c(min(0,a[2],b[2],c[2]), max(0,a[2],b[2],c[2])),
     asp=1,xlab = "", ylab = "")
arrows(0, 0, x1 = a[1], y1 = a[2]); text(a[1]*1.05, a[2]*1.05, "a")
arrows(0, 0, x1 = b[1], y1 = b[2]); text(b[1]*1.05, b[2]*1.05, "b")
arrows(0, 0, x1 = c[1], y1 = c[2],col="red"); text(c[1]*1.05, c[2]*1.05, "c")

2.2 問題2.1

a <- c(2,3)
b <- c(-1,4)
c <- c(-3,-4)

plot(0, 0, xlim=c(min(0,a[1],b[1],c[1]), max(0,a[1],b[1],c[1])),
     ylim=c(min(0,a[2],b[2],c[2]), max(0,a[2],b[2],c[2])),
     asp=1,xlab = "", ylab = "")
arrows(0, 0, x1 = a[1], y1 = a[2]); text(a[1]*1.05, a[2]*1.05, "a")
arrows(0, 0, x1 = b[1], y1 = b[2]); text(b[1]*1.05, b[2]*1.05, "b")
arrows(0, 0, x1 = c[1], y1 = c[2]); text(c[1]*1.05, c[2]*1.05, "c")

a <- c(2,3)
d <- -2*a

plot(0, 0, xlim=c(min(0,a[1],d), max(0,a[1],d[1])),
     ylim=c(min(0,a[2],d[2]), max(0,a[2],d[2])),
     asp=1,xlab = "", ylab = "")
arrows(0, 0, x1 = a[1], y1 = a[2]); text(a[1]*1.05, a[2]*1.05, "a")
arrows(0, 0, x1 = d[1], y1 = d[2], col = "red"); text(d[1]*1.05, d[2]*1.05, "d")

a <- c(2,3)
b <- c(-1,4)
d <- a+b

plot(0, 0, xlim=c(min(0,a[1],b[1],d[1]), max(0,a[1],b[1],d[1])),
     ylim=c(min(0,a[2],b[2],d[2]), max(0,a[2],b[2],d[2])),
     asp=1,xlab = "", ylab = "")
arrows(0, 0, x1 = a[1], y1 = a[2]); text(a[1]*1.05, a[2]*1.05, "a")
arrows(0, 0, x1 = b[1], y1 = b[2]); text(b[1]*1.05, b[2]*1.05, "b")
arrows(0, 0, x1 = d[1], y1 = d[2], col = "red"); text(d[1]*1.05, d[2]*1.05, "d")

a <- c(2,3)
c <- c(-3,-4)
d <- 3*a+2*c

plot(0, 0, xlim=c(min(0,a[1],c[1],d[1]), max(0,a[1],c[1],d[1])),
     ylim=c(min(0,a[2],c[2],d[2]), max(0,a[2],c[2],d[2])),
     asp=1,xlab = "", ylab = "")
arrows(0, 0, x1 = a[1], y1 = a[2]); text(a[1]*1.05, a[2]*1.05, "a")
arrows(0, 0, x1 = c[1], y1 = c[2]); text(c[1]*1.05, c[2]*1.05, "c")
arrows(0, 0, x1 = d[1], y1 = d[2], col = "red"); text(d[1]*1.05, d[2]*1.05, "d")

a <- c(2,3)
b <- c(-1,4)
c <- c(-3,-4)
d <- 5*a-b+2*c

plot(0, 0, xlim=c(min(0,a[1],b[1],c[1],d[1]), max(0,a[1],b[1],c[1],d[1])),
     ylim=c(min(0,a[2],b[2],c[2],d[2]), max(0,a[2],b[2],c[2],d[2])),
     asp=1,xlab = "", ylab = "")
arrows(0, 0, x1 = a[1], y1 = a[2]); text(a[1]*1.05, a[2]*1.05, "a")
arrows(0, 0, x1 = b[1], y1 = b[2]); text(b[1]*1.05, b[2]*1.05, "b")
arrows(0, 0, x1 = c[1], y1 = c[2]); text(c[1]*1.05, c[2]*1.05, "c")
arrows(0, 0, x1 = d[1], y1 = d[2], col = "red"); text(d[1]*1.05, d[2]*1.05, "d")

2.3 問題2.2

library(tidyverse)
## -- Attaching packages --------------- tidyverse 1.3.0 --
## √ ggplot2 3.3.0     √ purrr   0.3.4
## √ tibble  3.0.1     √ dplyr   0.8.5
## √ tidyr   1.0.3     √ stringr 1.4.0
## √ readr   1.3.1     √ forcats 0.5.0
## -- Conflicts ------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
A <- matrix(c(2,-1,1,3),2,2)
A %*% matrix(c(2,3),2,1)
##      [,1]
## [1,]    7
## [2,]    7
cardesian <- expand.grid(x = seq(-10,10,1), y = seq(-10,10,1))

trans <- cardesian
for (i in 1:nrow(cardesian)) {
  trans[i,]<-A %*% t(as.matrix(cardesian[i,]))
}

# 一気にやる場合
# A %*% t(cardesian) %>% 
#   t() %>% as_tibble() %>% 
#   rename(x = V1, y = V2) -> trans

bind_rows("Original" = cardesian,
          "Transformed" = trans,
          .id = "type") %>%
  ggplot() +
  geom_point(aes(x=x,y=y,color=type)) +
  coord_fixed() +
  theme_classic()

線形従属の例

A <- matrix(c(2,2,1,1),2,2)
cardesian <- expand.grid(x = seq(-10,10,1), y = seq(-10,10,1))

trans <- cardesian
for (i in 1:nrow(cardesian)) {
  trans[i,]<-A %*% t(as.matrix(cardesian[i,]))
}

bind_rows("Original" = cardesian,
          "Transformed" = trans,
          .id = "type") %>% 
  ggplot() + 
  geom_point(aes(x=x,y=y,color=type)) +
  coord_fixed() +
  theme_classic()

2.4 問題2.3

証明略

library(tidyverse)

## rotate 45 degree
alpha <- pi*(45/180) 
A <- matrix(c(cos(alpha),sin(alpha),-sin(alpha),cos(alpha)),2,2)

cardesian <- expand.grid(x = seq(-10,10,1), y = seq(-10,10,1))

trans <- cardesian
for (i in 1:nrow(cardesian)) {
  trans[i,]<-A %*% t(as.matrix(cardesian[i,]))
}

bind_rows("Original" = cardesian,
          "Transformed" = trans,
          .id = "type") %>% 
  ggplot() + 
  geom_point(aes(x=x,y=y,color=type)) +
  coord_fixed() +
  theme_classic()

2.5 問題2.4

A <- matrix(c(2,-1,1,-3,2,4,1,3,-2),3,3)
A
##      [,1] [,2] [,3]
## [1,]    2   -3    1
## [2,]   -1    2    3
## [3,]    1    4   -2
A %*% matrix(c(3,1,2),3,1)
##      [,1]
## [1,]    5
## [2,]    5
## [3,]    3

2.6 演習問題2

1

\[\left( \begin{array}{cc} 2 & -1 \\ 1 & 3 \\ \end{array} \right)\left( \begin{array}{c} x \\ y \\ \end{array} \right)=\left( \begin{array}{c} x' \\ y' \\ \end{array} \right)\]

A <- matrix(c(2,1,-1,3),2,2)
A %*% matrix(c(1,0),2,1)
##      [,1]
## [1,]    2
## [2,]    1
A %*% matrix(c(0,1),2,1)
##      [,1]
## [1,]   -1
## [2,]    3
A %*% matrix(c(2,-1),2,1)
##      [,1]
## [1,]    5
## [2,]   -1

2

set.seed(8931)
expand.grid(x = seq(-10,10,1), y = seq(-10,10,1)) %>% 
  sample_n(50) %>% 
 mutate(col = x*y) -> cardesian

cardesian %>% 
  ggplot() + 
  geom_point(aes(x=x,y=y,color =col)) +
  scale_color_gradientn(colours = rainbow(7)) +
  coord_fixed() +
  theme_classic()

  1. \(x\)
A <- matrix(c(1,0,0,-1),2,2)

trans <- cardesian
for (i in 1:nrow(cardesian)) {
  trans[i,1:2]<-A %*% t(as.matrix(cardesian[i,1:2]))
}

trans %>% 
  ggplot() + 
  geom_point(aes(x=x,y=y,color =col)) +
  scale_color_gradientn(colours = rainbow(7)) +
  coord_fixed() +
  theme_classic()

  1. \(y\)
A <- matrix(c(-1,0,0,1),2,2)

trans <- cardesian
for (i in 1:nrow(cardesian)) {
  trans[i,1:2]<-A %*% t(as.matrix(cardesian[i,1:2]))
}

trans %>% 
  ggplot() + 
  geom_point(aes(x=x,y=y,color =col)) +
  scale_color_gradientn(colours = rainbow(7)) +
  coord_fixed() +
  theme_classic()

  1. 原点
A <- matrix(c(-1,0,0,-1),2,2)

trans <- cardesian
for (i in 1:nrow(cardesian)) {
  trans[i,1:2]<-A %*% t(as.matrix(cardesian[i,1:2]))
}

trans %>% 
  ggplot() + 
  geom_point(aes(x=x,y=y,color =col)) +
  scale_color_gradientn(colours = rainbow(7)) +
  coord_fixed() +
  theme_classic()

  1. \(y=x\)
A <- matrix(c(0,1,1,0),2,2)

trans <- cardesian
for (i in 1:nrow(cardesian)) {
  trans[i,1:2]<-A %*% t(as.matrix(cardesian[i,1:2]))
}

trans %>% 
  ggplot() + 
  geom_point(aes(x=x,y=y,color =col)) +
  scale_color_gradientn(colours = rainbow(7)) +
  coord_fixed() +
  theme_classic()

3

A <- matrix(c(3,1,1,2),2,2)
B <- matrix(c(2,3,-3,-4),2,2)
x <- matrix(c(2,1),2,1)

A %*% B
##      [,1] [,2]
## [1,]    9  -13
## [2,]    8  -11
B %*% A
##      [,1] [,2]
## [1,]    3   -4
## [2,]    5   -5
A %*% B %*% x
##      [,1]
## [1,]    5
## [2,]    5
B %*% A %*% x
##      [,1]
## [1,]    2
## [2,]    5