extern "C" SEXP c_rthgini(SEXP x, SEXP mu, SEXP unbiased_, SEXP nthreads) { const int unbiased = INTEGER(unbiased_)[0]; const int n = LENGTH(x); SEXP gini; PROTECT(gini = allocVector(REALSXP, 1)); RTH_GEN_NTHREADS(nthreads); thrust::device_vector<double> dx(REAL(x), REAL(x)+n); thrust::sort(dx.begin(), dx.end()); thrust::counting_iterator<int> begin(0); thrust::counting_iterator<int> end = begin + n; thrust::plus<flouble> binop; DBL(gini) = (double) thrust::transform_reduce(begin, end, compute_gini(n, dx.begin()), (flouble) 0., binop); if (unbiased) DBL(gini) = DBL(gini) / (n*(n-1)*DBL(mu)); else DBL(gini) = DBL(gini) / (n*n*DBL(mu)); return gini; }
RcppExport SEXP rthgini(SEXP x_, SEXP mu_, SEXP unbiased_, SEXP nthreads) { Rcpp::NumericVector x(x_); Rcpp::NumericVector mu(mu_); const int unbiased = INTEGER(unbiased_)[0]; const int n = LENGTH(x); Rcpp::NumericVector gini(1); #if RTH_OMP omp_set_num_threads(INT(nthreads)); #elif RTH_TBB tbb::task_scheduler_init init(INT(nthreads)); #endif thrust::device_vector<double> dx(x.begin(), x.end()); thrust::sort(dx.begin(), dx.end()); thrust::counting_iterator<int> begin(0); thrust::counting_iterator<int> end = begin + n; thrust::plus<flouble> binop; gini[0] = (double) thrust::transform_reduce(begin, end, compute_gini(n, dx.begin()), (flouble) 0., binop); if (unbiased) gini[0] = gini[0]/n/(n-1)/mu[0]; else gini[0] = gini[0]/n/n/mu[0]; return gini; }