这感觉真的很笨拙:
library(Deriv)
sz <- 100
f.vec <- function(x) 1:sz%*%x
xs <- paste0("x", 1:sz, collapse = ",")
h <- eval(parse(text = paste0("h <- function(", xs, ") f.vec(c(", xs, "))")))
dh <- Deriv(h)
dh(1:sz)
#> x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 x14 x15 x16
#> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#> x17 x18 x19 x20 x21 x22 x23 x24 x25 x26 x27 x28 x29 x30 x31 x32
#> 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#> x33 x34 x35 x36 x37 x38 x39 x40 x41 x42 x43 x44 x45 x46 x47 x48
#> 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#> x49 x50 x51 x52 x53 x54 x55 x56 x57 x58 x59 x60 x61 x62 x63 x64
#> 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
#> x65 x66 x67 x68 x69 x70 x71 x72 x73 x74 x75 x76 x77 x78 x79 x80
#> 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
#> x81 x82 x83 x84 x85 x86 x87 x88 x89 x90 x91 x92 x93 x94 x95 x96
#> 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
#> x97 x98 x99 x100
#> 97 98 99 100
的评估时间Deriv
似乎随着参数的数量大致呈二次方增加:
varDeriv <- function(sz) {
system.time({
f.vec <- function(x) 1:sz%*%x
xs <- paste0("x", 1:sz, collapse = ",")
h <- eval(parse(text = paste0("h <- function(", xs, ") f.vec(c(", xs, "))")))
dh <- Deriv(h)
dh(1:sz)
})
}
Vectorize(varDeriv)(seq(100, 500, by = 100))
#> [,1] [,2] [,3] [,4] [,5]
#> user.self 0.712 2.700 6.498 11.782 20.854
#> sys.self 0.015 0.011 0.020 0.055 0.068
#> elapsed 0.727 2.710 6.518 11.839 20.922
#> user.child 0.000 0.000 0.000 0.000 0.000
#> sys.child 0.000 0.000 0.000 0.000 0.000