I am helping a student build some R code in Rcpp for his first time. He is working with 3-dimensional arrays, so I am hopeful to be working with arma::cube objects. When I simply try to bring the array in as an argument I can't seem to use the same as<[TEMPLATE]> construction as other objects. arma::cube X=Rcpp::as<arma::cube>(arg1); This line fails to compile. I can build this cube from an input vector using explicit nested loops. I also notice that wrap() does a fine job of returning the array back to R. Is there anything else I need to know to more simply bring the array in as an SEXP and land it in the arma::cube. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20130306/b0165532/attachment.html>
[Rcpp-devel] Working with arma::cube
4 messages · Silkworth,David J., Dirk Eddelbuettel, Alexios Ghalanos
On 6 March 2013 at 00:50, Silkworth,David J. wrote:
| I am helping a student build some R code in Rcpp for his first time. He is | working with 3-dimensional arrays, so I am hopeful to be working with | arma::cube objects. | | When I simply try to bring the array in as an argument I can?t seem to use the | same as<[TEMPLATE]> construction as other objects. | | arma::cube X=Rcpp::as<arma::cube>(arg1); | | This line fails to compile. Yes. Nobody wrote such a converter. Maybe you and your student want to take a stab? | I can build this cube from an input vector using explicit nested loops. I also | notice that wrap() does a fine job of returning the array back to R. | | Is there anything else I need to know to more simply bring the array in as an | SEXP and land it in the arma::cube. Array plus dim attribute may work. I used arma::cube quite extensively in one project, but only at the C++ level as a container. Results back to R were always of lesser dimension. Dirk
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
Try this:
library(RcppArmadillo)
library(inline)
if( require( RcppArmadillo ) ){
fx <- cxxfunction( signature(x = "vector", n = "integer") , '
Rcpp::NumericVector XX(x);
Rcpp::IntegerVector dim(n);
arma::cube AY(XX.begin(), dim[0], dim[1], dim[2]);
return(wrap(AY));
', plugin = "RcppArmadillo" )
}
set.seed(10)
x = array(rnorm(25*25*10), dim=c(25,25,10))
y = fx(x, c(25,25,10))
all.equal(x,y)
Regards,
Alexios
On 06/03/2013 01:15, Dirk Eddelbuettel wrote:
On 6 March 2013 at 00:50, Silkworth,David J. wrote: | I am helping a student build some R code in Rcpp for his first time. He is | working with 3-dimensional arrays, so I am hopeful to be working with | arma::cube objects. | | When I simply try to bring the array in as an argument I can?t seem to use the | same as<[TEMPLATE]> construction as other objects. | | arma::cube X=Rcpp::as<arma::cube>(arg1); | | This line fails to compile. Yes. Nobody wrote such a converter. Maybe you and your student want to take a stab? | I can build this cube from an input vector using explicit nested loops. I also | notice that wrap() does a fine job of returning the array back to R. | | Is there anything else I need to know to more simply bring the array in as an | SEXP and land it in the arma::cube. Array plus dim attribute may work. I used arma::cube quite extensively in one project, but only at the C++ level as a container. Results back to R were always of lesser dimension. Dirk
On 6 March 2013 at 03:22, alexios ghalanos wrote:
| Try this:
|
| library(RcppArmadillo)
| library(inline)
| if( require( RcppArmadillo ) ){
| fx <- cxxfunction( signature(x = "vector", n = "integer") , '
| Rcpp::NumericVector XX(x);
| Rcpp::IntegerVector dim(n);
| arma::cube AY(XX.begin(), dim[0], dim[1], dim[2]);
| return(wrap(AY));
| ', plugin = "RcppArmadillo" )
| }
|
| set.seed(10)
| x = array(rnorm(25*25*10), dim=c(25,25,10))
| y = fx(x, c(25,25,10))
| all.equal(x,y)
Very nice.
And it provides a blueprint for an as<>() converter, especially as the dim
vector could be picked off as attribute rather than a second argument.
Dirk
| Regards,
| Alexios
|
| On 06/03/2013 01:15, Dirk Eddelbuettel wrote:
| >
| > On 6 March 2013 at 00:50, Silkworth,David J. wrote:
| > | I am helping a student build some R code in Rcpp for his first time. He is | > | working with 3-dimensional arrays, so I am hopeful to be working with | > | arma::cube objects. | > | | > | When I simply try to bring the array in as an argument I can?t seem to use the | > | same as<[TEMPLATE]> construction as other objects. | > | | > | arma::cube X=Rcpp::as<arma::cube>(arg1); | > | | > | This line fails to compile. | > | > Yes. Nobody wrote such a converter. Maybe you and your student want to take a stab? | > | > | I can build this cube from an input vector using explicit nested loops. I also | > | notice that wrap() does a fine job of returning the array back to R. | > | | > | Is there anything else I need to know to more simply bring the array in as an | > | SEXP and land it in the arma::cube. | > | > Array plus dim attribute may work. | > | > I used arma::cube quite extensively in one project, but only at the C++ level | > as a container. Results back to R were always of lesser dimension. | > | > Dirk | > | | _______________________________________________ | 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
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com