# [Rcpp-devel] RcppArmadillo and 'trunc' problem

alexios ghalanos alexios at 4dscape.com
Wed May 30 13:59:14 CEST 2012

```Thanks Dirk.

When I run it inline I have not problem:

library(inline)

# The function draws a vector of random deviates from the multivariate
# Laplace of Kotz (zero mean) given a correlation matrix R.
fx <- cxxfunction( signature(R = "matrix", Z = "numeric" ) , '
Rcpp::NumericMatrix RR(R);
Rcpp::NumericVector RZ(Z);
int m = RR.nrow();
arma::mat AR(RR.begin(), m, m, true);
arma::rowvec AZ = Rcpp::as<arma::rowvec>(RZ);
Rcpp::RNGScope();
double e  = Rf_rexp(1);
arma::vec eigval(m);
arma::mat eigvec(m, m);
arma::mat temp(m, m);
arma::eig_sym(eigval, eigvec, AR);
arma::rowvec ans(m);
temp = ( eigvec * arma::diagmat( arma::sqrt( eigval ) ) * arma::inv(
eigvec ) );
ans = sqrt(e) * (AZ * temp);
return Rcpp::wrap(ans);

R = diag(5)
Z = rnorm(5)

# This works.
fx(R, Z)

But when I do a package check it fails (unless I comment out trunc and
ftrunc in sugar folder). The function in the package (which is meant to
be called internally by another routine) is:

arma::rowvec rmvlx(arma::mat R, arma::rowvec Z){
Rcpp::RNGScope();
double e  = Rf_rexp(1);
int m = R.n_rows;
arma::vec eigval(m);
arma::mat eigvec(m, m);
arma::mat temp(m, m);
arma::eig_sym(eigval, eigvec, R);
arma::rowvec ans(m);
temp = ( eigvec * arma::diagmat( arma::sqrt( eigval ) ) * arma::inv(
eigvec ) );
ans = sqrt(e) * (Z * temp);
return ans;
}

On 30/05/2012 12:38, Dirk Eddelbuettel wrote:
>
> Hi Alexios,
>
> On 30 May 2012 at 12:00, alexios ghalanos wrote:
> | Hi,
> |
> | While writing some extra functions into the rmgarch package (on r-forge)
> | which uses RcppArmadillo, I encountered the error relating to 'trunc'
> | and 'ftrunc' already discussed here:
> |
> | http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2010-September/001123.html
>
> That's a long time ago, a few things changed.  That old email was regarding
> OS X, your sessionInfo() shows Windows.
>
> | This occurred for the first time once I made use of Rf_exp and Rf_rchisq
> | in code mixed with RcppArmadillo.
>
> Can you provide a self-contained example, maybe via inline as we often do here?
>
> | While the workaround described in the previous discussion works (i.e.
> | undefine the two macros in sugar) for me, it will not work for my
> | package once I release the update on r-forge.
> |
> | Could you please suggest some solution or some mistake I am making which
> | gives rise to this problem?
>
> We will certainly try to help, but so far we have nothing reproducible -- the
> following works, and combines RcppArmadillo with rexp and rchisq:
>
> R>  library(inline)
> R>
> R>  f<- cxxfunction(signature(), plugin="RcppArmadillo", body='
> +   arma::colvec X(2);
> +   RNGScope scope;
> +   X(0) = Rf_rexp(1);
> +   X(1) = Rf_rchisq(1);
> +   return Rcpp::wrap(X);
> + ')
> R>  set.seed(42); f()
>           [,1]
> [1,] 0.198337
> [2,] 0.755413
> R>  set.seed(42); f()
>           [,1]
> [1,] 0.198337
> [2,] 0.755413
> R>  set.seed(42); f()
>           [,1]
> [1,] 0.198337
> [2,] 0.755413
> R>
>
> Dirk
>
> | sessionInfo ()
> | R version 2.15.0 (2012-03-30)
> | Platform: x86_64-pc-mingw32/x64 (64-bit)
> |
> | attached base packages:
> |  stats     graphics  grDevices utils     datasets  methods   base
> |
> | other attached packages:
> |
> | loaded via a namespace (and not attached):
> |  tools_2.15.0
> |
> |
> | Thanks and Regards,
> |
> | Alexios
> | _______________________________________________
> | Rcpp-devel mailing list
> | Rcpp-devel at lists.r-forge.r-project.org
> | https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>

```