S 6 ベクトルの1次独立性と行列の階数

6.1 例題6.1

対角要素が0になったら打ち止めになるように,hakidashi関数を改造.

hakidashi2 <- function(Ab) {
  d <- dim(Ab)[1]
  for (i in 1:d) {
    if (all(Ab[i:d,i] == 0)) break # lesser rank
    if (Ab[i,i] == 0) { # pivoting
      c <- c(i:d)[Ab[i:d,1] != 0][1]
      temp <- Ab[c,]
      Ab[c,] <- Ab[i,]
      Ab[i,] <- temp
    }
    Ab[i,] <- Ab[i,]/Ab[i,i]
    for (j in setdiff(1:d,i)) {
      Ab[j,] <- Ab[j,]-Ab[i,]*Ab[j,i]
    }
  }
  round(Ab, digits = 5)
}

Ab <- matrix(c(0,3,-2,1,4,-2,6,2,3,11,9,2),3,4)
hakidashi2(Ab)
##      [,1] [,2] [,3] [,4]
## [1,]    1    0    0    3
## [2,]    0    1    0   -1
## [3,]    0    0    1    2
Ab <- matrix(c(1,3,3,2,4,2,-1,2,7,3,5,1,1,4,5),3,5)
hakidashi2(Ab)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    0  4.0   -1  2.0
## [2,]    0    1 -2.5    2 -0.5
## [3,]    0    0  0.0    0  0.0
Ab <- matrix(c(1,3,3,2,4,2,-1,2,7,3,5,1,1,4,7),3,5)
hakidashi2(Ab)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    0  4.0   -1  2.0
## [2,]    0    1 -2.5    2 -0.5
## [3,]    0    0  0.0    0  2.0

6.2 問題6.2

A <- matrix(c(1,1,1,0,1,1,2,1,0),3,3)
det(A)
## [1] -1
A <- matrix(c(1,3,5,4,3,9,-4,6,2),3,3)
det(A)
## [1] -1.065814e-14
hakidashi2(A)
##      [,1] [,2] [,3]
## [1,]    1    0    4
## [2,]    0    1   -2
## [3,]    0    0    0
4*A[,1] -2*A[,2]
## [1] -4  6  2

6.3 ランク

定義通りにランクを計算する関数.要gtools

library(gtools) # for combinations
rank <- function(A) {
  r_max <- min(dim(A))
  for (k in 1:r_max) {
    r <- r_max+1-k
    row_combi <- combinations(dim(A)[1],r)
    col_combi <- combinations(dim(A)[2],r)
    dets <- c()
    for (i in 1:nrow(row_combi)) {
      for (j in 1:nrow(col_combi)) {
        det <- det(as.matrix(A[row_combi[i,],col_combi[j,]]))
        dets <- c(dets, round(det, digits = 5))
      }
    }
    if (any(dets != 0 )) break
  }
  r
}

Ab <- matrix(c(1,3,3,2,4,2,-1,2,7,3,5,1),3,4)
rank(Ab)
## [1] 2
A <- matrix(c(1,3,5,4,3,9,-4,6,2),3,3)
det(A)
## [1] -1.065814e-14
rank(A)
## [1] 2
A <- matrix(c(2,1,4,2),2,2)
rank(A)
## [1] 1

6.4 問題6.3

A <- matrix(c(2,5,4,-3,-8,-7,4,7,2,-2,-4,-2),3,4)
b <- matrix(c(4,9,6),3,1)
Ab <- cbind(A,b)

rank(A)
## [1] 2
rank(Ab)
## [1] 2
# solve(A,b) # error
hakidashi2(Ab)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    0   11   -4    5
## [2,]    0    1    6   -2    2
## [3,]    0    0    0    0    0
A <- matrix(c(2,5,4,-3,-8,-7,4,7,2,-2,-4,-2),3,4)
b <- matrix(c(4,9,3),3,1)
Ab <- cbind(A,b)

rank(A)
## [1] 2
rank(Ab)
## [1] 3
hakidashi2(Ab)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    0   11   -4    5
## [2,]    0    1    6   -2    2
## [3,]    0    0    0    0   -3

6.5 演習問題6

1

A <- matrix(c(1,2,3,3,4,5,-2,1,4,3,5,7),3,4)
rank(A)
## [1] 2
hakidashi2(A)
##      [,1] [,2] [,3] [,4]
## [1,]    1    0  5.5  1.5
## [2,]    0    1 -2.5  0.5
## [3,]    0    0  0.0  0.0
A <- matrix(c(3,4,5,1,2,3,-2,1,4,3,2,2,2,3,4),3,5)
rank(A)
## [1] 3
hakidashi2(A)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    0 -2.5    2  0.5
## [2,]    0    1  5.5   -3  0.5
## [3,]    0    0  0.0    1  0.0

2

A <- matrix(c(1,2,5,8,-6,6,2,1,4),3,3)
b <- matrix(c(5,2,14),3,1)
Ab <- cbind(A,b)
solve(A,b)
##      [,1]
## [1,]  3.0
## [2,]  0.5
## [3,] -1.0
hakidashi2(Ab)
##      [,1] [,2] [,3] [,4]
## [1,]    1    0    0  3.0
## [2,]    0    1    0  0.5
## [3,]    0    0    1 -1.0
A <- matrix(c(1,2,5,2,5,3,1,3,5),3,3)
b <- matrix(c(2,6,10),3,1)
Ab <- cbind(A,b)
solve(A,b)
##      [,1]
## [1,]    0
## [2,]    0
## [3,]    2
hakidashi2(Ab)
##      [,1] [,2] [,3] [,4]
## [1,]    1    0    0    0
## [2,]    0    1    0    0
## [3,]    0    0    1    2
A <- matrix(c(1,2,3,2,3,1,3,5,4),3,3)
b <- matrix(c(4,9,7),3,1)
Ab <- cbind(A,b)
# solve(A,b) # error
hakidashi2(Ab)
##      [,1] [,2] [,3] [,4]
## [1,]    1    0    1    6
## [2,]    0    1    1   -1
## [3,]    0    0    0  -10
A <- matrix(c(1,2,3,-4,-1,2,-11,-8,-5,13,5,-3),3,4)
b <- matrix(c(6,5,4),3,1)
Ab <- cbind(A,b)
# solve(A,b) # error
hakidashi2(Ab)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    0   -3    1    2
## [2,]    0    1    2   -3   -1
## [3,]    0    0    0    0    0