[Rcpp-commits] r716 - in pkg/RcppArmadillo: R inst src
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Wed Feb 17 11:14:47 CET 2010
Author: romain
Date: 2010-02-17 11:14:47 +0100 (Wed, 17 Feb 2010)
New Revision: 716
Modified:
pkg/RcppArmadillo/R/zzz.R
pkg/RcppArmadillo/inst/ChangeLog
pkg/RcppArmadillo/src/RcppArmadillo.cpp
pkg/RcppArmadillo/src/RcppArmadillo.h
Log:
as<Row>, as<Col>
Modified: pkg/RcppArmadillo/R/zzz.R
===================================================================
--- pkg/RcppArmadillo/R/zzz.R 2010-02-17 09:41:49 UTC (rev 715)
+++ pkg/RcppArmadillo/R/zzz.R 2010-02-17 10:14:47 UTC (rev 716)
@@ -3,10 +3,23 @@
.Call( "RcppArmadilloExample", PACKAGE = "RcppArmadillo" )
}
-RcppArmadilloExample_as <- function(){
+RcppArmadilloExample_as_Mat <- function(){
integer_mat <- matrix( as.integer(diag(4)), ncol = 4, nrow = 4 )
numeric_mat <- diag(5)
- .Call( "RcppArmadilloExample_as",
+ .Call( "RcppArmadilloExample_as_Mat",
list( integer_mat, numeric_mat ),
PACKAGE = "RcppArmadillo" )
}
+
+RcppArmadilloExample_as_Col <- function(){
+ .Call( "RcppArmadilloExample_as_Col",
+ list( 1:10, as.numeric(1:10) ),
+ PACKAGE = "RcppArmadillo" )
+}
+
+RcppArmadilloExample_as_Row <- function(){
+ .Call( "RcppArmadilloExample_as_Row",
+ list( 1:10, as.numeric(1:10) ),
+ PACKAGE = "RcppArmadillo" )
+}
+
Modified: pkg/RcppArmadillo/inst/ChangeLog
===================================================================
--- pkg/RcppArmadillo/inst/ChangeLog 2010-02-17 09:41:49 UTC (rev 715)
+++ pkg/RcppArmadillo/inst/ChangeLog 2010-02-17 10:14:47 UTC (rev 716)
@@ -1,6 +1,7 @@
2010-02-17 Romain Francois <romain at r-enthusiasts.com>
- * added support for as<Mat<T>> with T %in% int, double, float, unsigned int
+ * added support for as<Mat<T>> as<Row<T>> and as<Col<T>>
+ with T in : int, double, float, unsigned int
2010-02-16 Romain Francois <romain at r-enthusiasts.com>
Modified: pkg/RcppArmadillo/src/RcppArmadillo.cpp
===================================================================
--- pkg/RcppArmadillo/src/RcppArmadillo.cpp 2010-02-17 09:41:49 UTC (rev 715)
+++ pkg/RcppArmadillo/src/RcppArmadillo.cpp 2010-02-17 10:14:47 UTC (rev 716)
@@ -23,7 +23,7 @@
return output ;
}
-SEXP RcppArmadilloExample_as( SEXP input_ ){
+SEXP RcppArmadilloExample_as_Mat( SEXP input_ ){
using namespace Rcpp ;
List input(input_) ;
@@ -40,3 +40,39 @@
return res ;
}
+
+SEXP RcppArmadilloExample_as_Col( SEXP input_ ){
+ using namespace Rcpp ;
+
+ List input(input_) ;
+ arma::icolvec m1 = input[0] ; /* implicit as */
+ arma::colvec m2 = input[1] ; /* implicit as */
+ arma::ucolvec m3 = input[0] ; /* implicit as */
+ arma::fcolvec m4 = input[1] ; /* implicit as */
+
+ List res(4) ;
+ res[0] = arma::accu( m1 ) ;
+ res[1] = arma::accu( m2 ) ;
+ res[2] = arma::accu( m3 ) ;
+ res[3] = arma::accu( m4 ) ;
+
+ return res ;
+}
+
+SEXP RcppArmadilloExample_as_Row( SEXP input_ ){
+ using namespace Rcpp ;
+
+ List input(input_) ;
+ arma::irowvec m1 = input[0] ; /* implicit as */
+ arma::rowvec m2 = input[1] ; /* implicit as */
+ arma::urowvec m3 = input[0] ; /* implicit as */
+ arma::frowvec m4 = input[1] ; /* implicit as */
+
+ List res(4) ;
+ res[0] = arma::accu( m1 ) ;
+ res[1] = arma::accu( m2 ) ;
+ res[2] = arma::accu( m3 ) ;
+ res[3] = arma::accu( m4 ) ;
+
+ return res ;
+}
Modified: pkg/RcppArmadillo/src/RcppArmadillo.h
===================================================================
--- pkg/RcppArmadillo/src/RcppArmadillo.h 2010-02-17 09:41:49 UTC (rev 715)
+++ pkg/RcppArmadillo/src/RcppArmadillo.h 2010-02-17 10:14:47 UTC (rev 716)
@@ -4,21 +4,33 @@
#include <RcppCommon.h>
#include <armadillo>
+#define RCPPARMA_FORWARD(TYPE) \
+ template <> arma::Mat<TYPE> as< arma::Mat<TYPE> >( SEXP ) ; \
+ template <> arma::Col<TYPE> as< arma::Col<TYPE> >( SEXP ) ; \
+ template <> arma::Row<TYPE> as< arma::Row<TYPE> >( SEXP ) ;
+
/* forward declarations */
namespace Rcpp{
template <typename T> SEXP wrap ( const arma::Mat<T>& ) ;
template <typename T> SEXP wrap ( const arma::Row<T>& ) ;
template <typename T> SEXP wrap ( const arma::Col<T>& ) ;
-
- template <> arma::Mat<int> as< arma::Mat<int> >( SEXP ) ;
- template <> arma::Mat<double> as< arma::Mat<double> >( SEXP ) ;
+
+ RCPPARMA_FORWARD(int)
+ RCPPARMA_FORWARD(double)
+ RCPPARMA_FORWARD(float)
+ RCPPARMA_FORWARD(arma::s32)
}
#include <Rcpp.h>
RcppExport SEXP RcppArmadilloExample() ;
-RcppExport SEXP RcppArmadilloExample_as( SEXP );
+RcppExport SEXP RcppArmadilloExample_as_Mat( SEXP );
+RcppExport SEXP RcppArmadilloExample_as_Col( SEXP );
+RcppExport SEXP RcppArmadilloExample_as_Row( SEXP );
+#define RCPPARMA_COPY(in,out,n) for( int i=0; i<n; i++) { out[i] = in[i] ; }
+#define RCPPARMA_COPY_CAST(in,out,n,TYPE) for( int i=0; i<n; i++) { out[i] = static_cast<TYPE>(in[i]) ; }
+
namespace Rcpp{
/* wrap */
@@ -61,7 +73,7 @@
namespace RcppArmadillo{
/* when a cast is needed */
-template <typename T> ::arma::Mat<T> convert__dispatch( SEXP x, T, ::Rcpp::traits::true_type ){
+template <typename T> ::arma::Mat<T> convert_Mat__dispatch( SEXP x, T, ::Rcpp::traits::true_type ){
if( !Rf_isMatrix(x) ){
throw std::range_error( "not a matrix" ) ;
}
@@ -69,14 +81,12 @@
::Rcpp::IntegerVector dim = input.attr("dim") ;
::arma::Mat<T> out( dim[0], dim[1] ) ;
int n = dim[0] * dim[1] ;
- for( int i=0; i<n; i++){
- out[i] = static_cast<T>( input[i] ) ;
- }
+ RCPPARMA_COPY_CAST(input, out, n, T) ;
return out;
}
/* when no cast is needed */
-template <typename T> ::arma::Mat<T> convert__dispatch( SEXP x, T, ::Rcpp::traits::false_type ){
+template <typename T> ::arma::Mat<T> convert_Mat__dispatch( SEXP x, T, ::Rcpp::traits::false_type ){
if( !Rf_isMatrix(x) ){
throw std::range_error( "not a matrix" ) ;
}
@@ -84,22 +94,66 @@
::Rcpp::IntegerVector dim = input.attr("dim") ;
::arma::Mat<T> out( dim[0], dim[1] ) ;
int n = dim[0] * dim[1] ;
- for( int i=0; i<n; i++){
- out[i] = input[i] ;
- }
+ RCPPARMA_COPY(input, out, n) ;
return out;
}
+/* when a cast is needed */
+template <typename T> ::arma::Col<T> convert_Col__dispatch( SEXP x, T, ::Rcpp::traits::true_type ){
+ ::Rcpp::SimpleVector< ::Rcpp::traits::r_sexptype_traits<T>::rtype > input(x);
+ int n = input.size() ;
+ ::arma::Col<T> out( n ) ;
+ RCPPARMA_COPY_CAST(input, out, n, T) ;
+ return out;
+}
+
+/* when no cast is needed */
+template <typename T> ::arma::Col<T> convert_Col__dispatch( SEXP x, T, ::Rcpp::traits::false_type ){
+ ::Rcpp::SimpleVector< ::Rcpp::traits::r_sexptype_traits<T>::rtype > input(x);
+ int n = input.size() ;
+ ::arma::Col<T> out( n ) ;
+ RCPPARMA_COPY(input, out, n) ;
+ return out;
+}
+
+/* when a cast is needed */
+template <typename T> ::arma::Row<T> convert_Row__dispatch( SEXP x, T, ::Rcpp::traits::true_type ){
+ ::Rcpp::SimpleVector< ::Rcpp::traits::r_sexptype_traits<T>::rtype > input(x);
+ int n = input.size() ;
+ ::arma::Row<T> out( n ) ;
+ RCPPARMA_COPY_CAST(input, out, n, T) ;
+ return out;
+}
+
+/* when no cast is needed */
+template <typename T> ::arma::Row<T> convert_Row__dispatch( SEXP x, T, ::Rcpp::traits::false_type ){
+ ::Rcpp::SimpleVector< ::Rcpp::traits::r_sexptype_traits<T>::rtype > input(x);
+ int n = input.size() ;
+ ::arma::Row<T> out( n ) ;
+ RCPPARMA_COPY(input, out, n) ;
+ return out;
+}
+
/* dispatch depending on whether the type of data in the R vector is the same as T */
-template <typename T> ::arma::Mat<T> convert( SEXP x, T t){
- return convert__dispatch( x, t, typename ::Rcpp::traits::r_sexptype_needscast<T>() ) ;
+template <typename T> ::arma::Mat<T> convert_Mat( SEXP x, T t){
+ return convert_Mat__dispatch( x, t, typename ::Rcpp::traits::r_sexptype_needscast<T>() ) ;
}
-
+template <typename T> ::arma::Col<T> convert_Col( SEXP x, T t){
+ return convert_Col__dispatch( x, t, typename ::Rcpp::traits::r_sexptype_needscast<T>() ) ;
}
+template <typename T> ::arma::Mat<T> convert_Row( SEXP x, T t){
+ return convert_Row__dispatch( x, t, typename ::Rcpp::traits::r_sexptype_needscast<T>() ) ;
+}
+
+}
+
/* as */
-#define GENERATE_CONVERTERS(TYPE) template<> arma::Mat<TYPE> as< arma::Mat<TYPE> >(SEXP x){ return RcppArmadillo::convert<TYPE>(x, TYPE()) ; } ;
+#define GENERATE_CONVERTERS(TYPE) \
+ template<> arma::Mat<TYPE> as< arma::Mat<TYPE> >(SEXP x){ return RcppArmadillo::convert_Mat<TYPE>(x, TYPE()) ; } ; \
+ template<> arma::Col<TYPE> as< arma::Col<TYPE> >(SEXP x){ return RcppArmadillo::convert_Col<TYPE>(x, TYPE()) ; } ; \
+ template<> arma::Row<TYPE> as< arma::Row<TYPE> >(SEXP x){ return RcppArmadillo::convert_Row<TYPE>(x, TYPE()) ; } ;
GENERATE_CONVERTERS(int)
GENERATE_CONVERTERS(arma::u32)
@@ -107,6 +161,9 @@
GENERATE_CONVERTERS(float)
#undef GENERATE_CONVERTER
+#undef RCPPARMA_COPY
+#undef RCPPARMA_COPY_CAST
+#undef RCPPARMA_FORWARD
}
More information about the Rcpp-commits
mailing list