1dmnorm=function (x, mean = rep(0, d), varcov, log = FALSE)
2{
3    d <- if (is.matrix(varcov))
4        ncol(varcov)
5    else 1
6    if (d > 1 & is.vector(x))
7        x <- matrix(x, 1, d)
8    n <- if (d == 1)
9        length(x)
10    else nrow(x)
11    X <- t(matrix(x, nrow = n, ncol = d)) - mean
12    Q <- apply((solve(varcov) %*% X) * X, 2, sum)
13    logDet <- sum(logb(abs(diag(qr(varcov)[[1]]))))
14    logPDF <- as.vector(Q + d * logb(2 * pi) + logDet)/(-2)
15    if (log)
16        logPDF
17    else exp(logPDF)
18}
19