[Rcpp-commits] r719 - pkg/RcppArmadillo/src

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


Author: romain
Date: 2010-02-17 12:17:32 +0100 (Wed, 17 Feb 2010)
New Revision: 719

Modified:
   pkg/RcppArmadillo/src/RcppArmadillo.cpp
   pkg/RcppArmadillo/src/RcppArmadillo.h
Log:
handle casting and make example nicer

Modified: pkg/RcppArmadillo/src/RcppArmadillo.cpp
===================================================================
--- pkg/RcppArmadillo/src/RcppArmadillo.cpp	2010-02-17 10:42:54 UTC (rev 718)
+++ pkg/RcppArmadillo/src/RcppArmadillo.cpp	2010-02-17 11:17:32 UTC (rev 719)
@@ -4,22 +4,64 @@
 	using namespace Rcpp ;
 	using namespace arma ;
 	
-	List output(4); 
+	List output(3); 
+	List matrices(4) ;
+	List rows(2) ;
+	List cols(2) ;
 	
 	imat x1 = eye<imat>( 3,3 ) ;
 	x1.swap_rows(0,1)  ;
-	output[0] = x1 ;
+	matrices[0] = x1 ;
 	
 	mat x2 = eye<mat>( 3,3 ) ;
 	x2.swap_rows(0,1)  ;
-	output[1] = x2 ;
+	matrices[1] = x2 ;
 	
-	colvec y1 = zeros<colvec>(5,1);
-	output[2] = y1  ;
+	fmat x3 = eye<fmat>( 3, 3 );
+	x3.swap_rows(0,1)  ;
+	matrices[2] = x3 ;
 	
-	rowvec y2 = zeros<mat>(1,5);
-	output[3] = y2  ;
+	umat x4 = eye<umat>( 3, 3 );
+	x4.swap_rows(0,1)  ;
+	matrices[3] = x4 ;
 	
+	colvec r1 = zeros<mat>(5,1);
+	cols[0] = r1  ;
+	
+	fcolvec r2 = zeros<fmat>(5,1);
+	cols[1] = r2  ;
+	
+	rowvec c1 = zeros<mat>(1,5);
+	rows[0] = c1  ;
+	
+	frowvec c2 = zeros<fmat>(1,5);
+	rows[1] = c2  ;
+	
+	std::vector<std::string> names(4) ;
+	names[0] = "Mat<int>" ;
+	names[1] = "Mat<double>" ;
+	names[2] = "Mat<float>" ;
+	names[3] = "Mat<unsigned int>" ;
+	matrices.names() = names ;
+	
+	names.resize(2) ;
+	names[0] = "Row<double>" ;
+	names[1] = "Row<float>" ;
+	rows.names() = names ;
+	
+	names[0] = "Col<double>" ;
+	names[1] = "Col<float>" ;
+	cols.names() = names ;
+	
+	output[0] = matrices;
+	output[1] = rows ;
+	output[2] = cols ;
+	
+	names[0] = "matrices : Mat<T>" ; 
+	names[1] = "rows : Row<T>" ;
+	names.push_back( "columns : Col<T>" );
+	output.names() = names ;
+	
 	return output ;
 }
 

Modified: pkg/RcppArmadillo/src/RcppArmadillo.h
===================================================================
--- pkg/RcppArmadillo/src/RcppArmadillo.h	2010-02-17 10:42:54 UTC (rev 718)
+++ pkg/RcppArmadillo/src/RcppArmadillo.h	2010-02-17 11:17:32 UTC (rev 719)
@@ -34,39 +34,84 @@
 #define RCPPARMA_COPY_CAST(in,out,n,TYPE) for( int i=0; i<n; i++) { out[i] = static_cast<TYPE>(in[i]) ; } 
 
 namespace Rcpp{
+namespace RcppArmadillo{
 
-/* wrap */
+/* needs cast : float, unsigned int */
+template <typename T> SEXP wrap_Mat__dispatch( const arma::Mat<T>& mat, ::Rcpp::traits::true_type ){
+	::Rcpp::SimpleVector< ::Rcpp::traits::r_sexptype_traits<T>::rtype > vec( 
+		::Rcpp::Dimension( mat.n_rows, mat.n_cols ) );
+	typedef typename ::Rcpp::traits::storage_type< ::Rcpp::traits::r_sexptype_traits<T>::rtype >::type STORAGE_TYPE ;  
+	int n = mat.n_elem ;
+	RCPPARMA_COPY_CAST(mat,vec,n,STORAGE_TYPE);
+	return vec ;
+}
 
-template <typename T> SEXP wrap ( const arma::Mat<T>& mat ){
-	SimpleVector< traits::r_sexptype_traits<T>::rtype > vec( 
-		Dimension( mat.n_rows, mat.n_cols ) );
+/* no cast : double, int */
+template <typename T> SEXP wrap_Mat__dispatch( const arma::Mat<T>& mat, ::Rcpp::traits::false_type ){
+	::Rcpp::SimpleVector< ::Rcpp::traits::r_sexptype_traits<T>::rtype > vec( 
+		::Rcpp::Dimension( mat.n_rows, mat.n_cols ) );
 	int n = mat.n_elem ;
-	RCPPARMA_COPY(mat,vec,n)
-	return vec ; 
-} ;
+	RCPPARMA_COPY(mat,vec,n) ;
+	return vec ;
+}
 
-template <typename T> SEXP wrap( const arma::Col<T>& column ){
+
+template <typename T> SEXP wrap_Col__dispatch( const arma::Col<T>& column, ::Rcpp::traits::true_type ){
 	int n = column.n_rows ;
-	SimpleVector< traits::r_sexptype_traits<T>::rtype > vec( 
-		Dimension( n, 1 ) );
-	RCPPARMA_COPY(column,vec,n)
+	::Rcpp::SimpleVector< ::Rcpp::traits::r_sexptype_traits<T>::rtype > vec( 
+		::Rcpp::Dimension( n, 1 ) );
+	typedef typename ::Rcpp::traits::storage_type< ::Rcpp::traits::r_sexptype_traits<T>::rtype >::type STORAGE_TYPE ;  
+	RCPPARMA_COPY_CAST(column,vec,n,STORAGE_TYPE) ;
 	return vec ;
 }
 
-template <typename T> SEXP wrap( const arma::Row<T>& row ){
+template <typename T> SEXP wrap_Col__dispatch( const arma::Col<T>& column, ::Rcpp::traits::false_type ){
+	int n = column.n_rows ;
+	::Rcpp::SimpleVector< ::Rcpp::traits::r_sexptype_traits<T>::rtype > vec( 
+		::Rcpp::Dimension( n, 1 ) );
+	RCPPARMA_COPY(column,vec,n) ;
+	return vec ;
+}
+
+template <typename T> SEXP wrap_Row__dispatch( const arma::Row<T>& row, ::Rcpp::traits::true_type){
 	int n = row.n_cols ;
-	SimpleVector< traits::r_sexptype_traits<T>::rtype > vec( 
-		Dimension( 1, n ) );
-	RCPPARMA_COPY(row,vec,n)
+	::Rcpp::SimpleVector< ::Rcpp::traits::r_sexptype_traits<T>::rtype > vec( 
+		::Rcpp::Dimension( 1, n ) );
+	typedef typename ::Rcpp::traits::storage_type< ::Rcpp::traits::r_sexptype_traits<T>::rtype >::type STORAGE_TYPE ;  
+	RCPPARMA_COPY_CAST(row,vec,n,STORAGE_TYPE) ;
 	return vec ;
 }
 
+template <typename T> SEXP wrap_Row__dispatch( const arma::Row<T>& row, ::Rcpp::traits::false_type){
+	int n = row.n_cols ;
+	::Rcpp::SimpleVector< ::Rcpp::traits::r_sexptype_traits<T>::rtype > vec( 
+		::Rcpp::Dimension( 1, n ) );
+	RCPPARMA_COPY(row,vec,n) ;
+	return vec ;
+}
+
+} /* namespace RcppArmadillo */
+	
+/* wrap */
+
+template <typename T> SEXP wrap ( const arma::Mat<T>& mat ){
+	return RcppArmadillo::wrap_Mat__dispatch( mat, typename ::Rcpp::traits::r_sexptype_needscast<T>() ) ;
+} ;
+
+template <typename T> SEXP wrap( const arma::Col<T>& column ){
+	return RcppArmadillo::wrap_Col__dispatch( column, typename ::Rcpp::traits::r_sexptype_needscast<T>() ) ;
+}
+
+template <typename T> SEXP wrap( const arma::Row<T>& row ){
+	return RcppArmadillo::wrap_Row__dispatch( row, typename ::Rcpp::traits::r_sexptype_needscast<T>() ) ;
+}
+
 #ifdef HAS_CUBE
 template <typename T> SEXP wrap( const arma::Cube<T>& cube ){
 	int n = cube.n_elem;
 	SimpleVector< traits::r_sexptype_traits<T>::rtype > vec( 
 		Dimension( cube.n_rows, cube.n_cols, cube.n_slices ) );
-	RCPPARMA_COPY(cube,vec,n)
+	RCPPARMA_COPY(cube,vec,n);
 	return vec ;
 }
 #endif



More information about the Rcpp-commits mailing list