这必须针对每个测试用例运行。对于5000个测试用例,我运行了这段代码,花了290秒。 java中的相同内容需要0.5秒。任何一个都可以被矢量化吗?还有其他R种方式编写内循环吗?
ode
是2d矩阵,在代码中更新。 pyxixj
和pyxi
是两个矩阵,其值用于更新ode
。
noClasses <- 3
nOfAttr <- 40
ode <- matrix(1, nrow = 3, ncol = 40, byrow = TRUE)
ind <- c(0, 125, 251, 377, 498, 619, 740, 861, 982, 1098, 1214, 1330, 1446,
1562, 1672, 1782, 1892, 2002, 2112, 2222, 2326, 2430, 2534, 2638, 2742,
2846, 2943, 3040, 3137, 3234, 3331, 3428, 3525, 3614, 3703, 3792, 3881,
3970, 4059, 4148, 4237, 4321, 4405, 4489, 4573, 4657, 4735, 4813, 4891,
4969, 5047, 5125, 5199, 5273, 5347, 5421, 5488, 5555, 5622, 5689, 5756,
5823, 5890, 5950, 6010, 6070, 6130, 6190, 6250, 6310, 6364, 6418, 6472,
6526, 6580, 6634, 6681, 6728, 6775, 6822, 6869, 6916, 6963, 7003, 7043,
7083, 7123, 7163, 7203, 7243, 7277, 7311, 7345, 7379, 7413, 7447, 7475,
7503, 7531, 7559, 7587, 7615, 7639, 7663, 7687, 7711, 7731, 7751, 7771,
7791, 7810, 7828, 7845, 7861, 7876, 7890, 7903, 7915, 7926, 7936, 7945,
7953, 7960, 7966, 7971, 7975, 7978, 7980, 7981, 7991, 7998, 7999)
attrIndex <- c(0, 1, 4, 9, 14, 20, 26, 33, 41, 46, 52, 56, 63, 70,
76, 83, 90, 96, 102, 106, 110:129)
x <- c(1, 1, 4, 3, 2, 1, 5, 2, 1, 1, 1, 3, 6, 6, 7, 6, 5, 6, 2, 4, rep(1, 20))
set.seed(340)
pyxixj <- matrix( rnorm(noClasses*9000,mean=0.5,sd=0.1), noClasses, 9000)
pyxixjopp <- matrix( rnorm(noClasses*9000,mean=0.5,sd=0.1), noClasses, 9000)
for (y in 1:noClasses) {
for (xi in 1:(nOfAttr-1)) {
if(is.na(x[xi])) next
p <- attrIndex[xi]+x[xi]
for (xj in (xi + 1):nOfAttr) {
vxj <- x[xj]
if(is.na(vxj)) next
ode[y,xj] <- ode[y,xj] * pyxixjopp[y, ind[p] + attrIndex[xj] + vxj]
ode[y,xi] <- ode[y,xi] * pyxixj[y, ind[p] + attrIndex[xj] + vxj]
}
}
}