論理和\(p\lor q\)の真理表
p <- c(TRUE, TRUE, FALSE, FALSE)
q <- c(TRUE, FALSE, TRUE, FALSE)
p_or_q <- p | q
cbind(p,q,p_or_q)
## p q p_or_q
## [1,] TRUE TRUE TRUE
## [2,] TRUE FALSE TRUE
## [3,] FALSE TRUE TRUE
## [4,] FALSE FALSE FALSE
論理積\(p\land q\)の真理表
p <- c(TRUE, TRUE, FALSE, FALSE)
q <- c(TRUE, FALSE, TRUE, FALSE)
p_and_q <- p & q
cbind(p,q,p_and_q)
## p q p_and_q
## [1,] TRUE TRUE TRUE
## [2,] TRUE FALSE FALSE
## [3,] FALSE TRUE FALSE
## [4,] FALSE FALSE FALSE
否定\(\lnot\)の真理表
p <- c(TRUE, FALSE)
not_p <- !p
cbind(p,not_p)
## p not_p
## [1,] TRUE FALSE
## [2,] FALSE TRUE
p.9 式(1.4) \(\neg p \lor \neg q\)
p <- c(TRUE, TRUE, FALSE, FALSE)
q <- c(TRUE, FALSE, TRUE, FALSE)
not_p_or_not_q <- !p | !q
cbind(p,q,not_p_or_not_q)
## p q not_p_or_not_q
## [1,] TRUE TRUE FALSE
## [2,] TRUE FALSE TRUE
## [3,] FALSE TRUE TRUE
## [4,] FALSE FALSE TRUE
条件式\(\to\)と同値の真理表
p <- c(TRUE, TRUE, FALSE, FALSE)
q <- c(TRUE, FALSE, TRUE, FALSE)
p_cond_q <- !p | q
cbind(p,q,p_cond_q)
## p q p_cond_q
## [1,] TRUE TRUE TRUE
## [2,] TRUE FALSE FALSE
## [3,] FALSE TRUE TRUE
## [4,] FALSE FALSE TRUE
双条件式\(\leftrightarrow\)と同値の真理表
p <- c(TRUE, TRUE, FALSE, FALSE)
q <- c(TRUE, FALSE, TRUE, FALSE)
p_bicond_q <- (p & q) | (!p & !q)
cbind(p,q,p_bicond_q)
## p q p_bicond_q
## [1,] TRUE TRUE TRUE
## [2,] TRUE FALSE FALSE
## [3,] FALSE TRUE FALSE
## [4,] FALSE FALSE TRUE
Rのベクトル型を集合と見なして,基本的な集合演算ができる.
ただし,ベクトルなので重複を許容することに注意.ベクトルの重複を削りたい場合はunique関数を使う.
要素:\(a \in A\)
A <- unique(c(1,1,2,3))
B <- c(1,2,3,4,5)
C <- c(3,4,5)
E <- c()
in_A<-sapply(B, function(x) is.element(x,A))
# in_A
cbind(B,in_A) #cbindするとBoole値は1,0に変換される
## B in_A
## [1,] 1 1
## [2,] 2 1
## [3,] 3 1
## [4,] 4 0
## [5,] 5 0
部分集合関係\(A\subset B\)を見る場合,定義よりis.element
でのチェックをすべて行って,すべてTRUE
の場合,部分集合と判断する.
subset <- function(X, Y) {
all(sapply(X, function(x) is.element(x,Y)))
}
subs <- c(subset(A,B),subset(C,B),subset(A,C),subset(E,A))
names(subs) <- c("A <= B","C <= B","A <= C","E <= A")
subs
## A <= B C <= B A <= C E <= A
## TRUE TRUE FALSE TRUE
和集合:\(A \cup B\)
union(A,B)
## [1] 1 2 3 4 5
union(C,B)
## [1] 3 4 5 1 2
union(A,C)
## [1] 1 2 3 4 5
積集合:\(A \cap B\)
intersect(A,B)
## [1] 1 2 3
intersect(C,B)
## [1] 3 4 5
intersect(A,C)
## [1] 3
補集合:\(A^B\)
setdiff(B,A)
## [1] 4 5
setdiff(C,A)
## [1] 4 5
setdiff(A,C)
## [1] 1 2
集合演算のパッケージとしてはいくつかのものが公開されているようだ.
R
で解析的に微分を行う場合はexpression()
関数で,数式表現オブジェクトに変換した上でD(func,"var")
もしくはderiv(func, "var")
.ただし,あまり使い道はなさそう.
f <- expression(x^2 - 2*x + 4)
D(f,"x")
## 2 * x - 2
deriv(f,"x") #func = TRUE
## expression({
## .expr2 <- 2 * x
## .value <- x^2 - .expr2 + 4
## .grad <- array(0, c(length(.value), 1L), list(NULL, c("x")))
## .grad[, "x"] <- .expr2 - 2
## attr(.value, "gradient") <- .grad
## .value
## })
integrate()
関数を使った数値積分が可能
f <- function(x) x^2
integrate(f, 0, 1)
## 0.3333333 with absolute error < 3.7e-15
不定積分(の近似)はfunction(x) integrate(f,b,x)
という関数で定義すればよい.
f <- function(x) x^2
F <- function(x) integrate(f,0,x)$value
plot(function(x) x^3/3,0,1,xlab="",ylab="")
par(new = T)
plot(cbind(seq(0,1,0.1),sapply(seq(0,1,0.1),F)),col="red",xlab="",ylab="")
標準正規分布のpdfの全範囲積分.
stdnorm_pdf <- function(x) exp(-x^2/2)/sqrt(2*pi)
integrate(stdnorm_pdf, -Inf, Inf)
## 1 with absolute error < 9.4e-05
stdnorm_cdf <- function(x) integrate(stdnorm_pdf, -Inf, x)$value
plot(stdnorm_pdf,-3,3,xlim=c(-3,3),ylim=c(0,1),xlab="",ylab="")
par(new = T)
plot(cbind(seq(-3,3,0.1),sapply(seq(-3,3,0.1),stdnorm_cdf)),col="red"
,xlim=c(-3,3),ylim=c(0,1),xlab="",ylab="")