-
Notifications
You must be signed in to change notification settings - Fork 0
/
parallelMVNDensity.cpp
38 lines (33 loc) · 1.28 KB
/
parallelMVNDensity.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*************************************************************************************
* The original code can be found in: http://gallery.rcpp.org/articles/dmvnorm_arma/ *
*************************************************************************************/
/* PS: recommended for Ubuntu users */
#include "RcppArmadillo.h"
#include "Rcpp.h"
#include <omp.h>
const double log2pi = std::log(2.0 * M_PI);
/*** Calculate Multivariate Normal Density ***/
// [[Rcpp::depends("RcppArmadillo")]]
// [[Rcpp::export]]
arma::vec parallelMVNDensity(arma::mat x,
arma::rowvec mean,
arma::mat sigma,
bool logd = false,
int cores = 2) {
omp_set_num_threads(cores);
int n = x.n_rows;
int xdim = x.n_cols;
arma::vec out(n);
arma::mat rooti = arma::trans(arma::inv(trimatu(arma::chol(sigma))));
double rootisum = arma::sum(log(rooti.diag()));
double constants = -(xdim/2) * log2pi;
#pragma omp parallel for schedule(static)
for (int i=0; i < n; i++) {
arma::vec z = rooti * arma::trans( x.row(i) - mean) ;
out(i) = constants - 0.5 * arma::sum(z%z) + rootisum;
}
if (logd==false) {
out=exp(out);
}
return(out);
}