|
| 1 | +devtools::load_all() |
| 2 | + |
| 3 | +mm3 <- function(a, b, c) { |
| 4 | + declare( |
| 5 | + type(a = double(m, k)), |
| 6 | + type(b = double(k, n)), |
| 7 | + type(c = double(n, p)) |
| 8 | + ) |
| 9 | + ab <- a %*% b |
| 10 | + out <- ab %*% c |
| 11 | + out |
| 12 | +} |
| 13 | + |
| 14 | +xtx_scale <- function(x) { |
| 15 | + declare(type(x = double(NA, NA))) |
| 16 | + xtx <- crossprod(x) |
| 17 | + half_xtx <- 0.5 * xtx |
| 18 | + out <- xtx + half_xtx |
| 19 | + out |
| 20 | +} |
| 21 | + |
| 22 | +atb_c <- function(a, b, c) { |
| 23 | + declare( |
| 24 | + type(a = double(m, k)), |
| 25 | + type(b = double(n, k)), |
| 26 | + type(c = double(n, p)) |
| 27 | + ) |
| 28 | + atb <- a %*% t(b) |
| 29 | + out <- atb %*% c |
| 30 | + out |
| 31 | +} |
| 32 | + |
| 33 | +sum_of_products <- function(a, b, c, d) { |
| 34 | + declare( |
| 35 | + type(a = double(m, k)), |
| 36 | + type(b = double(k, n)), |
| 37 | + type(c = double(m, r)), |
| 38 | + type(d = double(r, n)) |
| 39 | + ) |
| 40 | + left <- a %*% b |
| 41 | + right <- c %*% d |
| 42 | + out <- left + right |
| 43 | + out |
| 44 | +} |
| 45 | + |
| 46 | +chain_plus <- function(a, b, c, j) { |
| 47 | + declare( |
| 48 | + type(a = double(m, k)), |
| 49 | + type(b = double(k, n)), |
| 50 | + type(c = double(n, p)), |
| 51 | + type(j = double(m, p)) |
| 52 | + ) |
| 53 | + q <- a %*% b %*% c + j |
| 54 | + q |
| 55 | +} |
| 56 | + |
| 57 | +chain_mix <- function(a, b, c) { |
| 58 | + declare( |
| 59 | + type(a = double(m, k)), |
| 60 | + type(b = double(n, k)), |
| 61 | + type(c = double(n, n)) |
| 62 | + ) |
| 63 | + q <- (a %*% t(b)) %*% c + 0.25 * (a %*% t(b)) |
| 64 | + q |
| 65 | +} |
| 66 | + |
| 67 | +crossprod_plus <- function(x, y, j) { |
| 68 | + declare( |
| 69 | + type(x = double(m, k)), |
| 70 | + type(y = double(k, n)), |
| 71 | + type(j = double(k, n)) |
| 72 | + ) |
| 73 | + q <- crossprod(x) %*% y + j |
| 74 | + q |
| 75 | +} |
| 76 | + |
| 77 | +sum_of_products_line <- function(a, b, c, d, j) { |
| 78 | + declare( |
| 79 | + type(a = double(m, k)), |
| 80 | + type(b = double(k, n)), |
| 81 | + type(c = double(m, r)), |
| 82 | + type(d = double(r, n)), |
| 83 | + type(j = double(m, n)) |
| 84 | + ) |
| 85 | + q <- a %*% b + c %*% d + j |
| 86 | + q |
| 87 | +} |
| 88 | + |
| 89 | +bad_conformable <- function(a, b) { |
| 90 | + declare( |
| 91 | + type(a = double(m, k)), |
| 92 | + type(b = double(n, p)) |
| 93 | + ) |
| 94 | + a %*% b |
| 95 | +} |
| 96 | + |
| 97 | +cat("=== r2f: mm3 ===\n") |
| 98 | +print(r2f(mm3)) |
| 99 | +cat("\n=== r2f: xtx_scale ===\n") |
| 100 | +print(r2f(xtx_scale)) |
| 101 | +cat("\n=== r2f: atb_c ===\n") |
| 102 | +print(r2f(atb_c)) |
| 103 | +cat("\n=== r2f: sum_of_products ===\n") |
| 104 | +print(r2f(sum_of_products)) |
| 105 | +cat("\n=== r2f: chain_plus ===\n") |
| 106 | +print(r2f(chain_plus)) |
| 107 | +cat("\n=== r2f: chain_mix ===\n") |
| 108 | +print(r2f(chain_mix)) |
| 109 | +cat("\n=== r2f: crossprod_plus ===\n") |
| 110 | +print(r2f(crossprod_plus)) |
| 111 | +cat("\n=== r2f: sum_of_products_line ===\n") |
| 112 | +print(r2f(sum_of_products_line)) |
| 113 | +cat("\n=== r2f: bad_conformable (expect warning) ===\n") |
| 114 | +print(r2f(bad_conformable)) |
| 115 | + |
| 116 | +set.seed(1) |
| 117 | +m <- 80 |
| 118 | +k <- 60 |
| 119 | +n <- 40 |
| 120 | +p <- 50 |
| 121 | + |
| 122 | +a <- matrix(runif(m * k), m, k) |
| 123 | +b <- matrix(runif(k * n), k, n) |
| 124 | +c <- matrix(runif(n * p), n, p) |
| 125 | +j <- matrix(runif(m * p), m, p) |
| 126 | + |
| 127 | +x <- matrix(runif(120 * 80), 120, 80) |
| 128 | + |
| 129 | +a2 <- matrix(runif(90 * 30), 90, 30) |
| 130 | +b2 <- matrix(runif(70 * 30), 70, 30) |
| 131 | +c2 <- matrix(runif(70 * 40), 70, 40) |
| 132 | + |
| 133 | +c_left <- matrix(runif(80 * 20), 80, 20) |
| 134 | +d_right <- matrix(runif(20 * 50), 20, 50) |
| 135 | +a_sp <- matrix(runif(80 * 60), 80, 60) |
| 136 | +b_sp <- matrix(runif(60 * 50), 60, 50) |
| 137 | +c_sp <- matrix(runif(80 * 20), 80, 20) |
| 138 | +d_sp <- matrix(runif(20 * 50), 20, 50) |
| 139 | + |
| 140 | +b_chain <- matrix(runif(55 * 30), 55, 30) |
| 141 | +c_chain <- matrix(runif(55 * 55), 55, 55) |
| 142 | +a_chain <- matrix(runif(90 * 30), 90, 30) |
| 143 | + |
| 144 | +x_cp <- matrix(runif(200 * 70), 200, 70) |
| 145 | +y_cp <- matrix(runif(70 * 60), 70, 60) |
| 146 | +j_cp <- matrix(runif(70 * 60), 70, 60) |
| 147 | + |
| 148 | +a_sum <- matrix(runif(100 * 80), 100, 80) |
| 149 | +b_sum <- matrix(runif(80 * 60), 80, 60) |
| 150 | +c_sum <- matrix(runif(100 * 30), 100, 30) |
| 151 | +d_sum <- matrix(runif(30 * 60), 30, 60) |
| 152 | +j_sum <- matrix(runif(100 * 60), 100, 60) |
| 153 | + |
| 154 | +q_mm3 <- quick(mm3) |
| 155 | +q_xtx_scale <- quick(xtx_scale) |
| 156 | +q_atb_c <- quick(atb_c) |
| 157 | +q_sum_of_products <- quick(sum_of_products) |
| 158 | +q_chain_plus <- quick(chain_plus) |
| 159 | +q_chain_mix <- quick(chain_mix) |
| 160 | +q_crossprod_plus <- quick(crossprod_plus) |
| 161 | +q_sum_of_products_line <- quick(sum_of_products_line) |
| 162 | + |
| 163 | +cat("\n=== bench: mm3 ===\n") |
| 164 | +print(bench::mark( |
| 165 | + mm3(a, b, c), |
| 166 | + q_mm3(a, b, c), |
| 167 | + check = TRUE |
| 168 | +)) |
| 169 | + |
| 170 | +cat("\n=== bench: xtx_scale ===\n") |
| 171 | +print(bench::mark( |
| 172 | + xtx_scale(x), |
| 173 | + q_xtx_scale(x), |
| 174 | + check = TRUE |
| 175 | +)) |
| 176 | + |
| 177 | +cat("\n=== bench: atb_c ===\n") |
| 178 | +print(bench::mark( |
| 179 | + atb_c(a2, b2, c2), |
| 180 | + q_atb_c(a2, b2, c2), |
| 181 | + check = TRUE |
| 182 | +)) |
| 183 | + |
| 184 | +cat("\n=== bench: sum_of_products ===\n") |
| 185 | +print(bench::mark( |
| 186 | + sum_of_products(a_sp, b_sp, c_sp, d_sp), |
| 187 | + q_sum_of_products(a_sp, b_sp, c_sp, d_sp), |
| 188 | + check = TRUE |
| 189 | +)) |
| 190 | + |
| 191 | +cat("\n=== bench: chain_plus ===\n") |
| 192 | +print(bench::mark( |
| 193 | + chain_plus(a, b, c, j), |
| 194 | + q_chain_plus(a, b, c, j), |
| 195 | + check = TRUE |
| 196 | +)) |
| 197 | + |
| 198 | +cat("\n=== bench: chain_mix ===\n") |
| 199 | +print(bench::mark( |
| 200 | + chain_mix(a_chain, b_chain, c_chain), |
| 201 | + q_chain_mix(a_chain, b_chain, c_chain), |
| 202 | + check = TRUE |
| 203 | +)) |
| 204 | + |
| 205 | +cat("\n=== bench: crossprod_plus ===\n") |
| 206 | +print(bench::mark( |
| 207 | + crossprod_plus(x_cp, y_cp, j_cp), |
| 208 | + q_crossprod_plus(x_cp, y_cp, j_cp), |
| 209 | + check = TRUE |
| 210 | +)) |
| 211 | + |
| 212 | +cat("\n=== bench: sum_of_products_line ===\n") |
| 213 | +print(bench::mark( |
| 214 | + sum_of_products_line(a_sum, b_sum, c_sum, d_sum, j_sum), |
| 215 | + q_sum_of_products_line(a_sum, b_sum, c_sum, d_sum, j_sum), |
| 216 | + check = TRUE |
| 217 | +)) |
0 commit comments