S 5 連立一次方程式

5.1 例題5.1

A <- matrix(c(1,2,4,2,1,5,3,4,6),3,3)
b <- matrix(c(1,-3,7),3,1)
solve(A,b)
##      [,1]
## [1,]    1
## [2,]    3
## [3,]   -2
c(det(cbind(b,A[,c(2,3)]))/det(A),
  det(cbind(A[,1],b,A[,3]))/det(A),
  det(cbind(A[,c(1,2)],b))/det(A)
  )
## [1]  1  3 -2

5.2 問題5.1

A <- matrix(c(4,3,-1,2),2,2)
b <- matrix(c(24,7),2,1)
solve(A,b)
##      [,1]
## [1,]    5
## [2,]   -4
c(det(cbind(b,A[,2]))/det(A),
  det(cbind(A[,1],b))/det(A)
  )
## [1]  5 -4
A <- matrix(c(1,2,4,2,3,2,3,5,1),3,3)
b <- matrix(c(2,5,9),3,1)
solve(A,b)
##      [,1]
## [1,]    3
## [2,]   -2
## [3,]    1
c(det(cbind(b,A[,c(2,3)]))/det(A),
  det(cbind(A[,1],b,A[,3]))/det(A),
  det(cbind(A[,c(1,2)],b))/det(A)
)
## [1]  3 -2  1

5.3 問題5.2

A <- matrix(c(3,4,2,1,3,-3,-2,-5,5),3,3)
det(A)
## [1] 6
A <- matrix(c(4,3,5,-2,5,3,5,-6,-2),3,3)
det(A)
## [1] 1.953993e-14
A
##      [,1] [,2] [,3]
## [1,]    4   -2    5
## [2,]    3    5   -6
## [3,]    5    3   -2
A[1,]+A[2,]-A[3,]
## [1] 2 0 1
#z = -2x
#y = -3x

5.4 例題5.3

掃き出し法をリテラルにやってみる.

Ab <- matrix(c(2,1,5,1,2,4,4,-2,-4,7,0,9),3,4)
Ab[1,] <- Ab[1,]/Ab[1,1]
Ab[2,] <- Ab[2,]-Ab[1,]*Ab[2,1]
Ab[3,] <- Ab[3,]-Ab[1,]*Ab[3,1]
Ab[2,] <- Ab[2,]/Ab[2,2]
Ab[1,] <- Ab[1,]-Ab[2,]*Ab[1,2]
Ab[3,] <- Ab[3,]-Ab[2,]*Ab[3,2]
Ab[3,] <- Ab[3,]/Ab[3,3]
Ab[1,] <- Ab[1,]-Ab[3,]*Ab[1,3]
Ab[2,] <- Ab[2,]-Ab[3,]*Ab[2,3]
Ab
##      [,1] [,2] [,3] [,4]
## [1,]    1    0    0  3.0
## [2,]    0    1    0 -1.0
## [3,]    0    0    1  0.5

関数としてパッケージ化.

hakidashi <- function(Ab) {
  d <- dim(Ab)[1]
  for (i in 1:d) {
    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(2,1,5,1,2,4,4,-2,-4,7,0,9),3,4)
hakidashi(Ab)
##      [,1] [,2] [,3] [,4]
## [1,]    1    0    0  3.0
## [2,]    0    1    0 -1.0
## [3,]    0    0    1  0.5

5.5 問題5.3

Ab <- matrix(c(1,2,3,1,-1,2,1,1,-1,6,3,4),3,4)
hakidashi(Ab)
##      [,1] [,2] [,3] [,4]
## [1,]    1    0    0    1
## [2,]    0    1    0    2
## [3,]    0    0    1    3
Ab <- matrix(c(3,2,1,-1,4,-1,2,-1,3,-7,28,-19),3,4)
hakidashi(Ab)
##      [,1] [,2] [,3] [,4]
## [1,]    1    0    0    3
## [2,]    0    1    0    4
## [3,]    0    0    1   -6

5.6 例題5.4

Ab <- matrix(c(1,3,1,2,1,1,4,0,2,
               1,0,0,0,1,0,0,0,1),3,6)
hakidashi(Ab)
##      [,1] [,2] [,3] [,4] [,5] [,6]
## [1,]    1    0    0   -1  0.0  2.0
## [2,]    0    1    0    3  1.0 -6.0
## [3,]    0    0    1   -1 -0.5  2.5

5.7 問題5.4

Ab <- matrix(c(2,4,1,3,1,0,0,1),2,4)
hakidashi(Ab)
##      [,1] [,2] [,3] [,4]
## [1,]    1    0  1.5 -0.5
## [2,]    0    1 -2.0  1.0
Ab <- matrix(c(1,0,0,1,1,0,1,1,1,
               1,0,0,0,1,0,0,0,1),3,6)
hakidashi(Ab)
##      [,1] [,2] [,3] [,4] [,5] [,6]
## [1,]    1    0    0    1   -1    0
## [2,]    0    1    0    0    1   -1
## [3,]    0    0    1    0    0    1
Ab <- matrix(c(1,4,6,2,3,3,3,4,2,
               1,0,0,0,1,0,0,0,1),3,6)
hakidashi(Ab)
##      [,1] [,2] [,3]  [,4]   [,5]   [,6]
## [1,]    1    0    0 -0.75  0.625 -0.125
## [2,]    0    1    0  2.00 -2.000  1.000
## [3,]    0    0    1 -0.75  1.125 -0.625

5.8 演習問題5

Ab <- matrix(c(1,5,2,4,-1,-2,3,1,
               -1,4,-1,-2,2,4,1,6,
               -3,18,14,6),4,5)
hakidashi(Ab)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    0    0    0    6
## [2,]    0    1    0    0    2
## [3,]    0    0    1    0    1
## [4,]    0    0    0    1   -3
Ab <- matrix(c(1,2,4,3,1,-1,3,1,
               2,1,5,3,1,-1,-1,2,
               10,-7,3,19),4,5)
hakidashi(Ab)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    0    0    0    1
## [2,]    0    1    0    0    2
## [3,]    0    0    1    0    0
## [4,]    0    0    0    1    7