https://github.com/cran/rstpm2
Raw File
Tip revision: c12a9847539968aa375d4df8349a3a524e7c1bb5 authored by Mark Clements on 17 January 2019, 14:50:04 UTC
version 1.4.5
Tip revision: c12a984
Rcpp-tests.R
require(Rcpp)
require(RcppArmadillo)
require(inline)

src <- '
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
using namespace Rcpp;
using namespace arma;
// [[Rcpp::export]]
SEXP testRcpp(NumericVector x_) {
 vec x(x_);
 uvec index = find(x);
 return wrap(x);
}
'
sourceCpp(code=src)

testRcpp(map0)

time0 <- c(0,10,0,20)
ind0 <- time0>0
map0 <- vector("integer",)
map0[ind0] <- as.integer(1:sum(ind0))
map0[!ind0] <- NaN
## which0 <- which(ind0)
which0 <- 1:length(time0)
which0[!ind0] <- NaN


##    uvec find0() { return map0(find(ind0)); }
##    uvec find0(uvec index) { return (map0(index))(find(ind0(index))); }

src <- '
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
using namespace Rcpp;
using namespace arma;
    uvec removeNaN(vec x) {
      vec newx = x;
      uvec index = find(newx == newx); // NaN != NaN
      newx = newx(index);
      return conv_to<uvec>::from(newx);
    }
// [[Rcpp::export]]
uvec testRcpp(NumericVector x_) {
  vec x = as<vec>(x_);
  uvec index = find(x==x);
  return index;
}
'
sourceCpp(code=src)

testRcpp(map0)
testRcpp(which0)



testRcpp(1:10)

src <- '
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
using namespace Rcpp;
using namespace arma;
// [[Rcpp::export]]
double testRcpp(double x) {
 return R::lgammafn(x);
}
'
sourceCpp(code=src)
testRcpp(3.0) - log(gamma(3.0))

back to top