|
| 1 | +test_that("matrix ops infer destination sizes for assignments", { |
| 2 | + matmul_infer <- function(A, B) { |
| 3 | + declare(type(A = double(2, 3)), type(B = double(3, 2))) |
| 4 | + out <- A %*% B |
| 5 | + out |
| 6 | + } |
| 7 | + |
| 8 | + matvec_infer <- function(A, x) { |
| 9 | + declare(type(A = double(2, 3)), type(x = double(3))) |
| 10 | + out <- A %*% x |
| 11 | + out |
| 12 | + } |
| 13 | + |
| 14 | + vecmat_infer <- function(x, A) { |
| 15 | + declare(type(x = double(2)), type(A = double(2, 3))) |
| 16 | + out <- x %*% A |
| 17 | + out |
| 18 | + } |
| 19 | + |
| 20 | + cross_infer <- function(x) { |
| 21 | + declare(type(x = double(4, 3))) |
| 22 | + out <- crossprod(x) |
| 23 | + out |
| 24 | + } |
| 25 | + |
| 26 | + cross_infer2 <- function(x, y) { |
| 27 | + declare(type(x = double(4, 3)), type(y = double(4, 2))) |
| 28 | + out <- crossprod(x, y) |
| 29 | + out |
| 30 | + } |
| 31 | + |
| 32 | + tcross_infer <- function(x) { |
| 33 | + declare(type(x = double(4, 3))) |
| 34 | + out <- tcrossprod(x) |
| 35 | + out |
| 36 | + } |
| 37 | + |
| 38 | + tcross_infer2 <- function(x, y) { |
| 39 | + declare(type(x = double(4, 3)), type(y = double(2, 3))) |
| 40 | + out <- tcrossprod(x, y) |
| 41 | + out |
| 42 | + } |
| 43 | + |
| 44 | + outer_infer <- function(x, y) { |
| 45 | + declare(type(x = double(2)), type(y = double(3))) |
| 46 | + out <- outer(x, y) |
| 47 | + out |
| 48 | + } |
| 49 | + |
| 50 | + outer_op_infer <- function(x, y) { |
| 51 | + declare(type(x = double(2)), type(y = double(3))) |
| 52 | + out <- x %o% y |
| 53 | + out |
| 54 | + } |
| 55 | + |
| 56 | + forward_infer <- function(L, b) { |
| 57 | + declare(type(L = double(2, 2)), type(b = double(2, 2))) |
| 58 | + out <- forwardsolve(L, b) |
| 59 | + out |
| 60 | + } |
| 61 | + |
| 62 | + back_infer <- function(U, b) { |
| 63 | + declare(type(U = double(2, 2)), type(b = double(2))) |
| 64 | + out <- backsolve(U, b) |
| 65 | + out |
| 66 | + } |
| 67 | + |
| 68 | + set.seed(99) |
| 69 | + A <- matrix(rnorm(6), nrow = 2) |
| 70 | + B <- matrix(rnorm(6), nrow = 3) |
| 71 | + x2 <- rnorm(2) |
| 72 | + x3 <- rnorm(3) |
| 73 | + X <- matrix(rnorm(12), nrow = 4) |
| 74 | + Yc <- matrix(rnorm(8), nrow = 4) |
| 75 | + Yt <- matrix(rnorm(6), nrow = 2) |
| 76 | + v2 <- rnorm(2) |
| 77 | + v3 <- rnorm(3) |
| 78 | + |
| 79 | + L <- matrix(c(2, 0, 1, 3), nrow = 2, byrow = TRUE) |
| 80 | + U <- matrix(c(2, 1, 0, 3), nrow = 2, byrow = TRUE) |
| 81 | + b_mat <- matrix(rnorm(4), nrow = 2) |
| 82 | + b_vec <- rnorm(2) |
| 83 | + |
| 84 | + expect_quick_equal(matmul_infer, list(A = A, B = B)) |
| 85 | + expect_quick_equal(matvec_infer, list(A = A, x = x3)) |
| 86 | + expect_quick_equal(vecmat_infer, list(x = x2, A = A)) |
| 87 | + expect_quick_equal(cross_infer, list(x = X)) |
| 88 | + expect_quick_equal(cross_infer2, list(x = X, y = Yc)) |
| 89 | + expect_quick_equal(tcross_infer, list(x = X)) |
| 90 | + expect_quick_equal(tcross_infer2, list(x = X, y = Yt)) |
| 91 | + expect_quick_equal(outer_infer, list(x = v2, y = v3)) |
| 92 | + expect_quick_equal(outer_op_infer, list(x = v2, y = v3)) |
| 93 | + expect_quick_equal(forward_infer, list(L = L, b = b_mat)) |
| 94 | + expect_quick_equal(back_infer, list(U = U, b = b_vec)) |
| 95 | +}) |
| 96 | + |
| 97 | +test_that("matrix helpers report unsupported inputs", { |
| 98 | + matmul_bad_rank <- function(a, b) { |
| 99 | + declare(type(a = double(2, 2, 2)), type(b = double(2, 2))) |
| 100 | + a %*% b |
| 101 | + } |
| 102 | + |
| 103 | + transpose_bad_rank <- function(x) { |
| 104 | + declare(type(x = double(2, 2, 2))) |
| 105 | + t(x) |
| 106 | + } |
| 107 | + |
| 108 | + outer_bad_rank <- function(x, y) { |
| 109 | + declare(type(x = double(2, 2)), type(y = double(2))) |
| 110 | + outer(x, y) |
| 111 | + } |
| 112 | + |
| 113 | + outer_missing <- function(x) { |
| 114 | + declare(type(x = double(2))) |
| 115 | + outer(x) |
| 116 | + } |
| 117 | + |
| 118 | + forward_k <- function(L, b) { |
| 119 | + declare(type(L = double(2, 2)), type(b = double(2))) |
| 120 | + forwardsolve(L, b, k = 1) |
| 121 | + } |
| 122 | + |
| 123 | + back_bad_upper <- function(U, b, flag) { |
| 124 | + declare( |
| 125 | + type(U = double(2, 2)), |
| 126 | + type(b = double(2)), |
| 127 | + type(flag = logical(1)) |
| 128 | + ) |
| 129 | + backsolve(U, b, upper.tri = flag) |
| 130 | + } |
| 131 | + |
| 132 | + back_bad_A <- function(A, b) { |
| 133 | + declare(type(A = double(2)), type(b = double(2))) |
| 134 | + backsolve(A, b) |
| 135 | + } |
| 136 | + |
| 137 | + back_bad_B <- function(U, b) { |
| 138 | + declare(type(U = double(2, 2)), type(b = double(2, 2, 2))) |
| 139 | + backsolve(U, b) |
| 140 | + } |
| 141 | + |
| 142 | + expect_error(quick(matmul_bad_rank), "%\\*% only supports vectors/matrices") |
| 143 | + expect_error(quick(transpose_bad_rank), "t\\(\\) only supports rank 0-2") |
| 144 | + expect_error(quick(outer_bad_rank), "outer\\(\\) only supports vectors") |
| 145 | + expect_error(quick(outer_missing), "outer\\(\\) expects X and Y") |
| 146 | + expect_error(quick(forward_k), "forwardsolve\\(\\) does not support k") |
| 147 | + expect_error(quick(back_bad_upper), "only supports literal upper\\.tri") |
| 148 | + expect_error(quick(back_bad_A), "triangular solve expects a matrix") |
| 149 | + expect_error(quick(back_bad_B), "triangular solve only supports vector") |
| 150 | +}) |
| 151 | + |
| 152 | +test_that("matrix conformability warnings are surfaced", { |
| 153 | + matmul_warn <- function(A, B, n, m, k) { |
| 154 | + declare( |
| 155 | + type(n = integer(1)), |
| 156 | + type(m = integer(1)), |
| 157 | + type(k = integer(1)), |
| 158 | + type(A = double(n, m)), |
| 159 | + type(B = double(k, n)) |
| 160 | + ) |
| 161 | + A %*% B |
| 162 | + } |
| 163 | + |
| 164 | + expect_warning( |
| 165 | + quick(matmul_warn), |
| 166 | + "cannot verify conformability in %\\*%" |
| 167 | + ) |
| 168 | +}) |
0 commit comments