-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.R
131 lines (106 loc) · 3.25 KB
/
tests.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# Tests for rowMins_R(), rowMins_C()
X <- matrix(1:5, nrow = 5, ncol = 10)
print(X)
# Expected output added as comment after test
# TESTS FOR rowMins_R()
rowMins_R(X)
# Check: [1] 1 2 3 4 5
rowMins_R(X, cols = 6:8)
# Check: [1] 26 27 28 29 30
# Passing non matrix value as argument x
rowMins_R('string input', cols = 6:8)
# Check:
#Error in rowMins_R("string input", cols = 6:8) :
# Argument x must be a matrix
# Passing non vector value as argument cols
rowMins_R(X, cols = matrix(1:20, nrow = 4, ncol = 5))
# Check:
#Error in rowMins_R(X, cols = matrix(1:20, nrow = 4, ncol = 5)) :
# Argument cols must be a vector
# Handling out of range subscript
rowMins_R(X, cols = 'c')
# Check:
#Error in rowMins_R(X, cols = "c") : Index out of bounds
# Warning for non numeric matrix values
charMatrix = matrix(1:4, nrow =2, ncol = 2)
charMatrix[1,1] = '1'
print(charMatrix)
rowMins_R(charMatrix, cols = 1:2)
# Check:
#[1] "1" "2"
#Warning message:
# In rowMins_R(charMatrix, cols = 1:2) : Non numeric values present in x
# TESTS FOR rowMins_C()
rowMins_C(X)
# Check: [1] 1 2 3 4 5
rowMins_C(X, cols = 6:8)
# Check: [1] 26 27 28 29 30
# Passing non matrix value as argument x
rowMins_C('string input', cols = 6:8)
# Check:
#Error in rowMins_C("string input", cols = 6:8) :
# Argument x must be a matrix
# Passing non vector value as argument cols
rowMins_C(X, cols = matrix(1:20, nrow = 4, ncol = 5))
# Check:
#Error in rowMins_C(X, cols = matrix(1:20, nrow = 4, ncol = 5)) :
# Argument cols must be a vector
# Handling out of range subscript
rowMins_C(X, cols = 'c')
# Check:
#Error in rowMins_C(X, cols = "c") : Index out of bounds
# For non numeric matrix values gives numeric result
# if coerce is successful else gives NA
print(charMatrix)
rowMins_C(charMatrix, cols = 1:2)
# Check:
#[1] 1 2
charMatrix[2,1] = 'string'
rowMins_C(charMatrix, cols = 1:2)
# Check:
#[1] 1 NA
#Warning message:
# In rowMins_C(charMatrix, cols = 1:2) : NAs introduced by coercion
# TESTING ROW MINS FOR COLUMN GROUPS
X <- matrix(1:50, nrow = 5, ncol = 10)
X
factors <- as.factor(c(3,1,1,2,2,1,1,2,3,1))
groups <- split(seq(along=factors), factors)
Y <- sapply(groups, FUN=function(idxs) rowMins_R(X, cols=idxs))
for(i in 1:3){
cat('\nColumns in consideration\n')
print(X[, groups[[i]]])
print('Corresponding rowMins_R')
print(Y[,i])
}
Z <- sapply(groups, FUN=function(idxs) rowMins_C(X, cols=idxs))
for(i in 1:3){
cat('\nColumns in consideration\n')
print(X[, groups[[i]]])
print('Corresponding rowMins_C')
print(Z[,i])
}
# Time comparison for large matrices
# Matrix of size 10^4 * 10^4 of ~750Mb, here is the time comparison:
#[1] "Matrix size: 10000 * 10000"
#[1] "rowMins_R:"
#user system elapsed
#116.891 0.000 115.666
#[1] "rowMins_C:"
#user system elapsed
#0.000 0.000 0.456
# increase range of i with caution, creates very large matrix i=4 onwards
# rowMins_R() will consume lot of time from i=4 onwards
print('Performance comparison of rowMins_R() and rowMins_C()')
for (i in 1:3){
print(paste('Matrix size:',10^i,'*',10^i,sep=' '))
B = matrix(rnorm(10^(2*i)), ncol = 10^i, nrow = 10^i)
print("rowMins_R:")
ptm = proc.time()
Temp = rowMins_R(B)
print(proc.time() - ptm)
print("rowMins_C:")
ptm = proc.time()
Temp = rowMins_C(B)
print(proc.time() - ptm)
}