9.1 Exercise

  1. 두 vector u=(1, 2, 3), v=(100, 200, 300)의 내적(inner product)을 구하시오.
u = c(1, 2, 3)
v = c(100, 200, 300)
sum(u*v)
## [1] 1400
  1. 두 vector u=(1, 2, 3), v=(4, 5, 6)이 이루는 각도(radian)를 구하시오.
u = c(1, 2, 3)
v = c(4, 5, 6)
acos(sum(u*v)/sqrt(sum(u*u))/sqrt(sum(v*v)))
## [1] 0.2257
  1. 두 vector a=(1, 2, 3), b=(4, 5, 6)의 외적을(outer product) 구하시오.
a = c(1, 2, 3)
b = c(4, 5, 6)
OuterProd(a, b)
## [1] -3  6 -3
  1. 두 vector a=(1, 2, 3), b=(4, 5, 6)가 이루는 삼각형의 면적을 구하시오.
a = c(1, 2, 3)
b = c(4, 5, 6)
z = OuterProd(a, b)
sqrt(sum(z*z))/2
## [1] 3.674
  1. Vector a = (1, 2, 3)을, v1 = (0, 1, 1), v2 = (1, 0, 1), v3 = (1, 1, 0)의 선형결합으로 나타내시오.
M = matrix(c(0,1,1, 1,0,1, 1,1,0), ncol=3)
a = c(1, 2, 3)
solve(M, a)
## [1] 2 1 0
  1. 세 vector u=(1, 2, 3), v=(4, 5, 6), w=(7, 8, 9) 가 \(R^3\)의 기저(basis)가 될 수 있는지 판정하라.
u=c(1, 2, 3); v=c(4, 5, 6); w=c(7, 8, 9)
det(cbind(u, v, w))
## [1] 0
  1. 세 vector v1 = (1, 1, 2), v2 = (1, 2, 1), v3 = (2, 1, 1) 은 일차독립(linear independent)인가?
v1 = c(1, 1, 2); v2 = c(1, 2, 1); v3 = c(2, 1, 1)
det(cbind(v1, v2, v3))
## [1] -4
  1. Vector a=(1, 2, 3)을 v1 = (1, 1, 2), v2 = (1, 2, 1), v3 = (2, 1, 1)을 기저(basis)로 나타내시오.
a = c(1, 2, 3)
v1 = c(1, 1, 2); v2 = c(1, 2, 1); v3 = c(2, 1, 1)
solve(cbind(v1, v2, v3), a)
##   v1   v2   v3 
##  1.5  0.5 -0.5
1.5*v1 + 0.5*v2 - 0.5*v3
## [1] 1 2 3
  1. 어떤 행렬의 전치행렬(transpose)를 구해보시오.
A = matrix(c(1,2,3, 4,5,6), ncol=2) ; A
##      [,1] [,2]
## [1,]    1    4
## [2,]    2    5
## [3,]    3    6
t(A)
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
  1. 대칭행렬(symmetric matrix)의 예를 드시오.
A = matrix(c(1,2,3, 2,1,2, 3,2,1), ncol=3) ; A
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    2    1    2
## [3,]    3    2    1
t(A)
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    2    1    2
## [3,]    3    2    1
  1. 다음 용어를 정의하거나 설명하시오.
  • idempotent matrix (멱등행렬) : AA = A
  • orthogonal matrix (직교행렬) : AA’ = I
  • null matrix (영핼렬) : all 0
  • I matrix (단위행렬) : AI = IA = A, diag(n)
  • J matrix : all 1
  • trace of A : sum(diag(A))
  • rank (계수) of A : 독립열(또는 행)의 수
  • determinant (행렬식) of A : \(\left| A \right|\) , det(A)
  • inverse (역행렬) of A: \(AA^{-1} = I \quad A^{-1}A = I\)
  • singular (비정칙) : det(A) = 0
  • positive definite (양정치) : A such that x’Ax > 0 for all vector x except zero vector
  • eigen value of A : real value \(\lambda\) such that Au = \(\lambda\)u or \(\left| A - \lambda I \right|=0\)
  • eigen vector of A : real vector u of the above
  • spectral decomposition : getting eigen values and vectors, eigen()
  1. 다음의 예를 드시오.
  • (AB)’ = B’A’
  • AA’은 대칭행렬이다.
A = matrix(c(1, 2, 3, 4), ncol=2)
B = matrix(c(3, 2, 1, 0), ncol=2)
t(A %*% B)
##      [,1] [,2]
## [1,]    9   14
## [2,]    1    2
t(B) %*% t(A)
##      [,1] [,2]
## [1,]    9   14
## [2,]    1    2
A %*% t(A)
##      [,1] [,2]
## [1,]   10   14
## [2,]   14   20
B %*% t(B)
##      [,1] [,2]
## [1,]   10    6
## [2,]    6    4
  1. 직교행렬(orthogonal matrix)의 예를 드시오.
A = matrix(c(1, 1, -1, 1), ncol=2)/sqrt(2) ; A
##        [,1]    [,2]
## [1,] 0.7071 -0.7071
## [2,] 0.7071  0.7071
A %*% t(A)
##      [,1] [,2]
## [1,]    1    0
## [2,]    0    1
  1. 다음 삼원일차 연립방정식을 풀이하시오. \[{}\] \[\begin{align*} x + 4y + 7z &= 6 \\ 2x + 5y + 8z &= 6 \\ 3x + 7y + 9z &= 6 \end{align*}\]
A = matrix(c(1,2,3, 4,5,7, 7,8,9), ncol=3)
solve(A, c(6, 6, 6))
## [1] -1  0  1
  1. 다음 행렬들의 행렬식(determinant)과 역행렬(inverse)를 구하시오. \[{}\] \[A = \begin{bmatrix}2 & 1 \\ 1 & 2 \end{bmatrix}\] \[B = \begin{bmatrix}2 & 1 & 1 \\ 1 & 2 & 1 \\ 1 & 1 & 2 \end{bmatrix}\]
A = matrix(c(2,1,1,2), ncol=2) ; A
##      [,1] [,2]
## [1,]    2    1
## [2,]    1    2
det(A)
## [1] 3
solve(A)
##         [,1]    [,2]
## [1,]  0.6667 -0.3333
## [2,] -0.3333  0.6667
B = matrix(c(2,1,1, 1,2,1, 1,1,2), ncol=3) ; B
##      [,1] [,2] [,3]
## [1,]    2    1    1
## [2,]    1    2    1
## [3,]    1    1    2
det(B)
## [1] 4
solve(B)
##       [,1]  [,2]  [,3]
## [1,]  0.75 -0.25 -0.25
## [2,] -0.25  0.75 -0.25
## [3,] -0.25 -0.25  0.75
  1. 다음 행렬의 계수(rank)를 구하시오. \[{}\] \[B = \begin{bmatrix}2 & 1 & 1 \\ 1 & 2 & 1 \\ 1 & 1 & 2 \end{bmatrix}\]
B = matrix(c(2,1,1, 1,2,1, 1,1,2), ncol=3) ; B
##      [,1] [,2] [,3]
## [1,]    2    1    1
## [2,]    1    2    1
## [3,]    1    1    2
qr(B)$rank
## [1] 3
  1. 다음 행렬들의 고유치(eigenvalue)와 고유벡터(eigenvector)를 구하시오. \[{}\] \[A = \begin{bmatrix}2 & 1 \\ 1 & 2 \end{bmatrix}\] \[B = \begin{bmatrix}3 & 2 & 1 \\ 2 & 3 & 2 \\ 1 & 2 & 3 \end{bmatrix}\]
A = matrix(c(2,1,1,2), ncol=2) ; A
##      [,1] [,2]
## [1,]    2    1
## [2,]    1    2
eigen(A)
## eigen() decomposition
## $values
## [1] 3 1
## 
## $vectors
##        [,1]    [,2]
## [1,] 0.7071 -0.7071
## [2,] 0.7071  0.7071
B = matrix(c(3,2,1, 2,3,2, 1,2,3), ncol=3) ; B
##      [,1] [,2] [,3]
## [1,]    3    2    1
## [2,]    2    3    2
## [3,]    1    2    3
eigen(B)
## eigen() decomposition
## $values
## [1] 6.3723 2.0000 0.6277
## 
## $vectors
##        [,1]    [,2]    [,3]
## [1,] 0.5418 -0.7071  0.4544
## [2,] 0.6426  0.0000 -0.7662
## [3,] 0.5418  0.7071  0.4544
  1. 위 문제 B행렬의 5승과 1.5승을 계산하여라.
B = matrix(c(3,2,1, 2,3,2, 1,2,3), ncol=3) ; B
##      [,1] [,2] [,3]
## [1,]    3    2    1
## [2,]    2    3    2
## [3,]    1    2    3
eRes = eigen(B)
eVal = eRes$values
eVec = eRes$vectors
eVec %*% diag(eVal) %*% t(eVec)
##      [,1] [,2] [,3]
## [1,]    3    2    1
## [2,]    2    3    2
## [3,]    1    2    3
B%*%B%*%B%*%B%*%B
##      [,1] [,2] [,3]
## [1,] 3100 3658 3068
## [2,] 3658 4339 3658
## [3,] 3068 3658 3100
eVec %*% diag(eVal^5) %*% t(eVec)
##      [,1] [,2] [,3]
## [1,] 3100 3658 3068
## [2,] 3658 4339 3658
## [3,] 3068 3658 3100
eVec %*% diag(eVal^1.5) %*% t(eVec)
##       [,1]  [,2]  [,3]
## [1,] 6.238 5.427 3.410
## [2,] 5.427 6.935 5.427
## [3,] 3.410 5.427 6.238
  1. 정규분포를 따르는 난수로 생성한 임의의 3x3행렬을 LU decomposition하시오.
require(Matrix)
## Loading required package: Matrix
A = Matrix(rnorm(9), 3, 3) ; A 
## 3 x 3 Matrix of class "dgeMatrix"
##         [,1]    [,2]    [,3]
## [1,]  0.8957 -1.0969 -0.8111
## [2,]  0.6674 -0.8374 -0.6236
## [3,] -1.1468 -1.5804  1.8550
LUres = expand(lu(A, sparse=F))
LUres$L %*% LUres$U
## 3 x 3 Matrix of class "dgeMatrix"
##         [,1]    [,2]    [,3]
## [1,] -1.1468 -1.5804  1.8550
## [2,]  0.8957 -1.0969 -0.8111
## [3,]  0.6674 -0.8374 -0.6236
  1. 다음 행렬의 일반화역행렬(generalized inverse)을 구하시오. \[{}\] \[B = \begin{bmatrix}1 & 4 & 7 \\ 2 & 5 & 8 \\ 3 & 6 & 9 \end{bmatrix}\]
B = matrix(c(1,2,3, 4,5,6, 7,8,9), ncol=3) ; B
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9
# solve(B)
require(MASS)
## Loading required package: MASS
ginv(B)
##         [,1]       [,2]    [,3]
## [1,] -0.6389 -5.556e-02  0.5278
## [2,] -0.1667 -5.551e-17  0.1667
## [3,]  0.3056  5.556e-02 -0.1944
B %*% ginv(B) %*% B
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9