-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.R
82 lines (70 loc) · 1.98 KB
/
functions.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# Random matrices generation ----------------------------------------------
# Generates a random square matrix which elements are samples from a
# Bernoulli(.5) distribution
# - input args:
# k is the number of rows / columns
# - output:
# k*k matrix
generate_random_matrix <- function(k=5) {
M <- matrix(rbinom(k*k, 1,.5), nrow=k)
return(M)
}
# Verifies the identity UV != W
# - input:
# for a given k:
# U is a k*k binary matrix
# V is a k*k binary matrix
# W is a k*k binary matrix
# - output:
# true if the UV != W, false otherwise.
check_triplet <- function(U,V,W) {
stopifnot(dim(U) == dim(V) && dim(V) == dim(W))
equal <- T
if(identical((U %*% V) %% 2, W)){
equal <- F
}
return(equal)
}
# Algorithms ------------------------------------------------------
# Implements the one step algorithm given in the homework text.
# - input:
# for a given k:
# U is a k*k binary matrix
# V is a k*k binary matrix
# W is a k*k binary matrix
# - output:
# false if UVz == Wz, true otherwise.
one_step <- function(U, V, W) {
stopifnot(dim(U) == dim(V) && dim(V) == dim(W))
z <- array(rbinom(dim(U)[1], 1, .5))
res <- (U %*% ((V %*% z) %% 2)) %% 2
if( identical
(res, (W %*% z) %% 2) ){
return(F)
} else {
return(T)
}
}
# Implements the m-step algorithm given in the homework text.
# - input:
# k is the number of columns / rows of the matrices U,V,W
# m is the number of trials
# U is a k*k binary matrix
# V is a k*k binary matrix
# W is a k*k binary matrix
# - output:
# an array containing the results obtained for each trial. Namely,
# the array entries are: false if UVz == Wz, true otherwise.
m_steps <- function(k = 5, m = 100, U = generate_random_matrix(k),
V = generate_random_matrix(k),
W = generate_random_matrix(k)) {
stopifnot(k > 0 && m > 0)
stopifnot(dim(U) == dim(V) && dim(V) == dim(W))
a = c(one_step(U,V,W))
if(m > 1) {
for( i in 2:m ) {
a = c(a, one_step(U,V,W))
}
}
return(a)
}