setwd("~/Dropbox/Works/Class/Math_Foundations/")
\(a_1\), \(a_2\), \(a_3\), \(a_4\), \(a_5\) 에 대하여 더하기 연산
\(a_1 = 80\), \(a_2 = 75\), \(a_3 = 90\), \(a_4 = 83\), \(a_5 = 70\)
a <- c(80, 75, 90, 83, 70)
a
## [1] 80 75 90 83 70
a[1]
## [1] 80
a[c(2, 5)]
## [1] 75 70
a[-3]
## [1] 80 75 83 70
length(a)
## [1] 5
\(\sum_{i=1}^5 a_i\) 와 \(\sum_{i=3}^{10} c_i\)
sum(a)
## [1] 398
b <- rev(a)
b
## [1] 70 83 90 75 80
c <- c(a, b)
c
## [1] 80 75 90 83 70 70 83 90 75 80
c[3:10]
## [1] 90 83 70 70 83 90 75 80
sum(c[3:10])
## [1] 641
a^3
## [1] 512000 421875 729000 571787 343000
sum(a^3)
## [1] 2577662
A <- rbind(a, b)
A
## [,1] [,2] [,3] [,4] [,5]
## a 80 75 90 83 70
## b 70 83 90 75 80
dimnames(A) <- list(paste("r", 1:2, sep=""), paste("c", 1:5, sep=""))
A
## c1 c2 c3 c4 c5
## r1 80 75 90 83 70
## r2 70 83 90 75 80
A[1, 1]
## [1] 80
A[2, 1]
## [1] 70
A[1, c(1, 3)]
## c1 c3
## 80 90
A[-1, ]
## c1 c2 c3 c4 c5
## 70 83 90 75 80
A[, 1]
## r1 r2
## 80 70
A[, 1, drop = FALSE]
## c1
## r1 80
## r2 70
A[1, 1:3]
## c1 c2 c3
## 80 75 90
sum(A[1, 1:3])
## [1] 245
A[2, 1:3]
## c1 c2 c3
## 70 83 90
sum(A[2, 1:3])
## [1] 243
apply(A[, 1:3], 1, sum)
## r1 r2
## 245 243
sum(A[1, 1:3]) + sum(A[2, 1:3])
## [1] 488
sum(apply(A[, 1:3], 1, sum))
## [1] 488
sum(A[, 1:3])
## [1] 488
A[, 1]
## r1 r2
## 80 70
sum(A[, 1])
## [1] 150
A[, 2]
## r1 r2
## 75 83
sum(A[, 2])
## [1] 158
A[, 1]
## r1 r2
## 80 70
sum(A[, 2])
## [1] 158
apply(A[, 1:3], 2, sum)
## c1 c2 c3
## 150 158 180
sum(A[, 1]) + sum(A[, 2]) + sum(A[, 2])
## [1] 466
sum(apply(A[, 1:3], 2, sum))
## [1] 488
sum(A[, 1:3])
## [1] 488
a[1]*a[2]*a[3]*a[4]*a[5]
## [1] 3137400000
prod(a)
## [1] 3137400000
a + b
## [1] 150 158 180 158 150
prod(a + b)
## [1] 101104200000
열의 합이나 행의 합을 간단히 표현
\(\sum_{i=1}^n a_{ij} = a_{\cdot j}\), \(\sum_{j=1}^m a_{ij} = a_{i\cdot}\)
A
## c1 c2 c3 c4 c5
## r1 80 75 90 83 70
## r2 70 83 90 75 80
sum(A[, 1])
## [1] 150
sum(A[, 2])
## [1] 158
sum(A[, 3])
## [1] 180
sum(A[, 4])
## [1] 158
sum(A[, 5])
## [1] 150
apply(A, 2, sum)
## c1 c2 c3 c4 c5
## 150 158 180 158 150
colSums(A)
## c1 c2 c3 c4 c5
## 150 158 180 158 150
sum(A[1, ])
## [1] 398
sum(A[2, ])
## [1] 398
apply(A, 1, sum)
## r1 r2
## 398 398
rowSums(A)
## r1 r2
## 398 398
rowSums(A)
## r1 r2
## 398 398
sum(rowSums(A))
## [1] 796
colSums(A)
## c1 c2 c3 c4 c5
## 150 158 180 158 150
sum(colSums(A))
## [1] 796
\(a_{\cdot j}^2 = (\sum_{i=1}^n a_{ij})^2\) 과 \(\sum_{i=1}^n a_{ij}^2\) 의 구분.
합의 제곱
A
## c1 c2 c3 c4 c5
## r1 80 75 90 83 70
## r2 70 83 90 75 80
sum(A[, 1])
## [1] 150
sum(A[, 1])^2
## [1] 22500
sum(A[, 2])
## [1] 158
sum(A[, 2])^2
## [1] 24964
sum(A[, 3])
## [1] 180
sum(A[, 3])^2
## [1] 32400
sum(A[, 4])
## [1] 158
sum(A[, 4])^2
## [1] 24964
sum(A[, 5])
## [1] 150
sum(A[, 5])^2
## [1] 22500
colSums(A)^2
## c1 c2 c3 c4 c5
## 22500 24964 32400 24964 22500
A[, 1]
## r1 r2
## 80 70
A[, 1]^2
## r1 r2
## 6400 4900
sum(A[, 1]^2)
## [1] 11300
A[, 2]
## r1 r2
## 75 83
A[, 2]^2
## r1 r2
## 5625 6889
sum(A[, 2]^2)
## [1] 12514
A[, 3]
## r1 r2
## 90 90
A[, 3]^2
## r1 r2
## 8100 8100
sum(A[, 3]^2)
## [1] 16200
A[, 4]
## r1 r2
## 83 75
A[, 4]^2
## r1 r2
## 6889 5625
sum(A[, 4]^2)
## [1] 12514
A[, 5]
## r1 r2
## 70 80
A[, 5]^2
## r1 r2
## 4900 6400
sum(A[, 5]^2)
## [1] 11300
A^2
## c1 c2 c3 c4 c5
## r1 6400 5625 8100 6889 4900
## r2 4900 6889 8100 5625 6400
colSums(A^2)
## c1 c2 c3 c4 c5
## 11300 12514 16200 12514 11300
M1 <- matrix(c(80, 75, 90, 83, 70, 3.0, 3.5, 4.0, 3.1, 2.2), ncol=2)
M1
## [,1] [,2]
## [1,] 80 3.0
## [2,] 75 3.5
## [3,] 90 4.0
## [4,] 83 3.1
## [5,] 70 2.2
str(M1)
## num [1:5, 1:2] 80 75 90 83 70 3 3.5 4 3.1 2.2
stat.score <- c(80, 75, 90, 83, 70)
GPA <- c(3.0, 3.5, 4.0, 3.1, 2.2)
M2 <- cbind(stat.score, GPA)
M2
## stat.score GPA
## [1,] 80 3.0
## [2,] 75 3.5
## [3,] 90 4.0
## [4,] 83 3.1
## [5,] 70 2.2
dimnames(M2)[[1]] <- paste("student", 1:5, sep="")
M2
## stat.score GPA
## student1 80 3.0
## student2 75 3.5
## student3 90 4.0
## student4 83 3.1
## student5 70 2.2
str(M2)
## num [1:5, 1:2] 80 75 90 83 70 3 3.5 4 3.1 2.2
## - attr(*, "dimnames")=List of 2
## ..$ : chr [1:5] "student1" "student2" "student3" "student4" ...
## ..$ : chr [1:2] "stat.score" "GPA"
M3 <- matrix(c(80, 3.0, 75, 3.5, 90, 4.0, 83, 3.1, 70, 2.2), ncol=2, byrow=TRUE)
M3
## [,1] [,2]
## [1,] 80 3.0
## [2,] 75 3.5
## [3,] 90 4.0
## [4,] 83 3.1
## [5,] 70 2.2
dim(M3)
## [1] 5 2
nrow(M3)
## [1] 5
ncol(M3)
## [1] 2
A <- matrix(c(3, -2, -1, 6, 8, 4), nrow=2)
A
## [,1] [,2] [,3]
## [1,] 3 -1 8
## [2,] -2 6 4
D <- diag(c(-3, 5, -6))
D
## [,1] [,2] [,3]
## [1,] -3 0 0
## [2,] 0 5 0
## [3,] 0 0 -6
diag(3)
## [,1] [,2] [,3]
## [1,] 1 0 0
## [2,] 0 1 0
## [3,] 0 0 1
diag(3, nrow=1)
## [,1]
## [1,] 3
T.lower <- matrix(c(1, 0, 0, 0, 3, 0, 0, 0, 3, -1, 3, 0, 4, 2, 5, 6), nrow=4)
T.lower
## [,1] [,2] [,3] [,4]
## [1,] 1 3 3 4
## [2,] 0 0 -1 2
## [3,] 0 0 3 5
## [4,] 0 0 0 6
Matrix::isTriangular(T.lower)
## [1] TRUE
## attr(,"kind")
## [1] "U"
T.upper <- matrix(c(3, -2, 5, 0, 0, 5, -4, 2, 0, 0, 3, 7, 0, 0, 0, 0), nrow=4)
T.upper
## [,1] [,2] [,3] [,4]
## [1,] 3 0 0 0
## [2,] -2 5 0 0
## [3,] 5 -4 3 0
## [4,] 0 2 7 0
Matrix::isTriangular(T.upper)
## [1] TRUE
## attr(,"kind")
## [1] "L"
P <- matrix(c(0.2, 0.4, 0.8, 0.6), nrow=2)
P
## [,1] [,2]
## [1,] 0.2 0.8
## [2,] 0.4 0.6
rowSums(P)
## [1] 1 1
x <- c(3, -2, 0, 1)
is.vector(x)
## [1] TRUE
is.matrix(x)
## [1] FALSE
dim(x)
## NULL
length(x)
## [1] 4
str(x)
## num [1:4] 3 -2 0 1
x.mat <- matrix(x, ncol=1)
x.mat
## [,1]
## [1,] 3
## [2,] -2
## [3,] 0
## [4,] 1
is.vector(x.mat)
## [1] FALSE
is.matrix(x.mat)
## [1] TRUE
dim(x.mat)
## [1] 4 1
length(x.mat)
## [1] 4
str(x.mat)
## num [1:4, 1] 3 -2 0 1
t(x.mat)
## [,1] [,2] [,3] [,4]
## [1,] 3 -2 0 1
is.vector(t(x.mat))
## [1] FALSE
is.matrix(t(x.mat))
## [1] TRUE
dim(t(x.mat))
## [1] 1 4
length(t(x.mat))
## [1] 4
str(t(x.mat))
## num [1, 1:4] 3 -2 0 1
save.image(file="chapter_01_contents.rda")