[Rcpp-devel] Best way to create random normal matrix

Julian Smith hilbertspaces at gmail.com
Wed Dec 5 00:40:11 CET 2012


I've been toying around with RcppEigen and I was curious what was the best
way to create a random matrix of standard normals of size n by n. I have
tried in Armadillo but ran into an error. Would prefer to do in RcppEigen.

(Note: I'm not 100% sure why I get the error with Armadillo either)

src <- '

Rcpp::NumericMatrix Xr(Xs);

int q = Rcpp::as<int>(ys);

int n = Xr.nrow(), k = Xr.ncol();

arma::mat X(Xr.begin(), n, k, false);

arma::mat G, Y, B;

G = arma::randn(n,q);

Y = X*G;

arma::mat Q, R;

arma::qr(Q,R,Y);

Q = Q(arma::span::all,arma::span(0,q-1));

B = arma::trans(Q)*X;

arma::vec d;

arma::mat u, v;

arma::svd(u, d, v, B );

arma::mat U = Q*u;

return
Rcpp::List::create(Rcpp::Named("d")=d,Rcpp::Named("U")=U,Rcpp::Named("V")=v);'

rsvd <- cxxfunction(signature(Xs="numeric", ys="integer"), body=src,
plugin="RcppArmadillo")


appsvd <- function(X, q) {

    .Call("rsvd", X, q, package = "RcppArmadillo")

}


[1] Error in .Call("rsvd", X, q, package = "RcppArmadillo") : C symbol name
"rsvd" not in load table
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20121204/e15953f0/attachment.html>


More information about the Rcpp-devel mailing list