[Rcpp-commits] r713 - in pkg/RcppArmadillo: . inst src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Feb 17 10:32:58 CET 2010


Author: romain
Date: 2010-02-17 10:32:58 +0100 (Wed, 17 Feb 2010)
New Revision: 713

Added:
   pkg/RcppArmadillo/inst/
   pkg/RcppArmadillo/inst/ChangeLog
Modified:
   pkg/RcppArmadillo/DESCRIPTION
   pkg/RcppArmadillo/src/RcppArmadillo.cpp
   pkg/RcppArmadillo/src/RcppArmadillo.h
Log:
added support for as<Mat>

Modified: pkg/RcppArmadillo/DESCRIPTION
===================================================================
--- pkg/RcppArmadillo/DESCRIPTION	2010-02-17 09:30:05 UTC (rev 712)
+++ pkg/RcppArmadillo/DESCRIPTION	2010-02-17 09:32:58 UTC (rev 713)
@@ -1,13 +1,13 @@
 Package: RcppArmadillo
 Type: Package
 Title: Rcpp/Armadillo translation layer
-Version: 0.0.0
+Version: 0.0.1
 Date: 2010-02-16
 Author: Romain Francois and Dirk Eddelbuettel
 Maintainer: Dirk and Romain <RomainAndDirk at r-enthusiasts.com>
 Description: The package eases integration of armadillo types with Rcpp
 License: GPL-2
 LazyLoad: yes
-Depends: Rcpp (>= 0.7.7)
+Depends: Rcpp (>= 0.7.7.2)
 SystemRequirements: armadillo
 Packaged: 2010-02-16 14:17:09 UTC; romain

Added: pkg/RcppArmadillo/inst/ChangeLog
===================================================================
--- pkg/RcppArmadillo/inst/ChangeLog	                        (rev 0)
+++ pkg/RcppArmadillo/inst/ChangeLog	2010-02-17 09:32:58 UTC (rev 713)
@@ -0,0 +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
+	
+2010-02-16  Romain Francois <romain at r-enthusiasts.com>
+
+	* initial version, covering wrap(Mat), wrap(Col), wrap(Row)

Modified: pkg/RcppArmadillo/src/RcppArmadillo.cpp
===================================================================
--- pkg/RcppArmadillo/src/RcppArmadillo.cpp	2010-02-17 09:30:05 UTC (rev 712)
+++ pkg/RcppArmadillo/src/RcppArmadillo.cpp	2010-02-17 09:32:58 UTC (rev 713)
@@ -25,15 +25,18 @@
 
 SEXP RcppArmadilloExample_as( SEXP input_ ){
 	using namespace Rcpp ;
-	using namespace arma ;
 	
 	List input(input_) ;
-	imat m1 = input[0] ; /* implicit as */
-	mat  m2 = input[1] ; /* implicit as */
+	arma::imat m1 = input[0] ; /* implicit as */
+	arma::mat  m2 = input[1] ; /* implicit as */
+	arma::umat m3 = input[0] ; /* implicit as */
+	arma::fmat m4 = input[1] ; /* implicit as */
 	
-	List res(2) ;
-	res[0] = accu( m1 ) ;
-	res[1] = accu( m2 ) ;
+	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:30:05 UTC (rev 712)
+++ pkg/RcppArmadillo/src/RcppArmadillo.h	2010-02-17 09:32:58 UTC (rev 713)
@@ -58,37 +58,57 @@
 
 namespace Rcpp{
 	
-/* as */
-template<> arma::Mat<int> as< arma::Mat<int> >(SEXP x){
+namespace RcppArmadillo{
+
+/* when a cast is needed */
+template <typename T> ::arma::Mat<T> convert__dispatch( SEXP x, T, ::Rcpp::traits::true_type ){
 	if( !Rf_isMatrix(x) ){
 		throw std::range_error( "not a matrix" ) ;
 	}
-	SimpleVector< traits::r_sexptype_traits<int>::rtype > input(x);
-	IntegerVector dim = input.attr("dim") ;
-	arma::Mat<int> out( dim[0], dim[1] ) ;
+	::Rcpp::SimpleVector< ::Rcpp::traits::r_sexptype_traits<T>::rtype > input(x);
+	::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] ;
+		out[i] = static_cast<T>( input[i] ) ;
 	}
-	return out ;
+	return out;
 }
 
-/* as */
-template<> arma::Mat<double> as< arma::Mat<double> >(SEXP x){
+/* when no cast is needed */
+template <typename T> ::arma::Mat<T> convert__dispatch( SEXP x, T, ::Rcpp::traits::false_type ){
 	if( !Rf_isMatrix(x) ){
 		throw std::range_error( "not a matrix" ) ;
 	}
-	SimpleVector< traits::r_sexptype_traits<double>::rtype > input(x);
-	IntegerVector dim = input.attr("dim") ;
-	arma::Mat<double> out( dim[0], dim[1] ) ;
+	::Rcpp::SimpleVector< ::Rcpp::traits::r_sexptype_traits<T>::rtype > input(x);
+	::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] ;
 	}
-	return out ;
+	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>() ) ;
 }
+	
+}
 
+/* as */
+
+#define GENERATE_CONVERTERS(TYPE) template<> arma::Mat<TYPE> as< arma::Mat<TYPE> >(SEXP x){ return RcppArmadillo::convert<TYPE>(x, TYPE()) ; } ;
+
+GENERATE_CONVERTERS(int)
+GENERATE_CONVERTERS(arma::u32)
+GENERATE_CONVERTERS(double)
+GENERATE_CONVERTERS(float)
+
+#undef GENERATE_CONVERTER
+
+}
+
 #endif
 



More information about the Rcpp-commits mailing list