[Rcpp-commits] r4436 - in pkg/RcppArmadillo: . inst/include inst/include/RcppArmadilloExtensions

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Aug 20 17:23:17 CEST 2013


Author: romain
Date: 2013-08-20 17:23:17 +0200 (Tue, 20 Aug 2013)
New Revision: 4436

Added:
   pkg/RcppArmadillo/inst/include/RcppArmadilloAs.h
Removed:
   pkg/RcppArmadillo/inst/include/RcppArmadilloExtensions/spmat.h
Modified:
   pkg/RcppArmadillo/ChangeLog
   pkg/RcppArmadillo/inst/include/RcppArmadillo.h
   pkg/RcppArmadillo/inst/include/RcppArmadilloForward.h
   pkg/RcppArmadillo/inst/include/RcppArmadilloWrap.h
Log:
cleaner implementations of wrap and as for SpMat

Modified: pkg/RcppArmadillo/ChangeLog
===================================================================
--- pkg/RcppArmadillo/ChangeLog	2013-08-20 09:30:16 UTC (rev 4435)
+++ pkg/RcppArmadillo/ChangeLog	2013-08-20 15:23:17 UTC (rev 4436)
@@ -1,6 +1,8 @@
 2013-08-20 Romain Francois <romain at r-enthusiasts.com>
 
         * include/RcppArmadilloWrap.h: Handle wrap<subview>
+        * include/RcppArmadilloWrap.h: cleaner implementation for wrap< SpMat<T> >
+        * include/RcppArmadilloAs.h: cleaner implementation for as< SpMat<T> >
         
 2013-08-17  Dirk Eddelbuettel  <edd at debian.org>
 

Modified: pkg/RcppArmadillo/inst/include/RcppArmadillo.h
===================================================================
--- pkg/RcppArmadillo/inst/include/RcppArmadillo.h	2013-08-20 09:30:16 UTC (rev 4435)
+++ pkg/RcppArmadillo/inst/include/RcppArmadillo.h	2013-08-20 15:23:17 UTC (rev 4436)
@@ -30,6 +30,7 @@
 #include <RcppArmadilloForward.h>
 #include <Rcpp.h>
 #include <RcppArmadilloWrap.h>
+#include <RcppArmadilloAs.h>
 #include <RcppArmadilloSugar.h>
 
 #endif

Added: pkg/RcppArmadillo/inst/include/RcppArmadilloAs.h
===================================================================
--- pkg/RcppArmadillo/inst/include/RcppArmadilloAs.h	                        (rev 0)
+++ pkg/RcppArmadillo/inst/include/RcppArmadilloAs.h	2013-08-20 15:23:17 UTC (rev 4436)
@@ -0,0 +1,91 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
+/* :tabSize=4:indentSize=4:noTabs=false:folding=explicit:collapseFolds=1: */
+//
+// RcppArmadilloAs.h: Rcpp/Armadillo glue, support for as
+//
+// Copyright (C)  2013  Dirk Eddelbuettel, Romain Francois
+//
+// This file is part of RcppArmadillo.
+//
+// RcppArmadillo is free software: you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 2 of the License, or
+// (at your option) any later version.
+//
+// RcppArmadillo is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with RcppArmadillo.  If not, see <http://www.gnu.org/licenses/>.
+
+#ifndef RcppArmadillo__RcppArmadilloAs__h
+#define RcppArmadillo__RcppArmadilloAs__h
+
+namespace Rcpp{
+namespace traits {
+
+	template <typename T> 
+	class Exporter< arma::Col<T> > : public IndexingExporter< arma::Col<T>, T > {
+	public: 
+	    Exporter(SEXP x) : IndexingExporter< arma::Col<T>, T >(x){}
+	}; 
+
+	template <typename T> 
+	class Exporter< arma::Row<T> > : public IndexingExporter< arma::Row<T>, T > {
+	public:
+	    Exporter(SEXP x) : IndexingExporter< arma::Row<T>, T >(x){}
+	}; 
+
+	template <typename T> 
+	class Exporter< arma::Mat<T> > : public MatrixExporter< arma::Mat<T>, T > {
+	public:
+	    Exporter(SEXP x) : MatrixExporter< arma::Mat<T>, T >(x){}
+	}; 
+		
+	 
+	template <typename T>
+	class Exporter< arma::SpMat<T> > {
+	public:
+		Exporter( SEXP x ) : mat(x){}
+		
+		arma::SpMat<T> get(){
+			IntegerVector dims = mat.slot("Dim");
+			arma::urowvec i = Rcpp::as<arma::urowvec>(mat.slot("i"));
+			arma::urowvec p = Rcpp::as<arma::urowvec>(mat.slot("p"));     
+			arma::Col<T> x     = Rcpp::as< arma::Col<T> >(mat.slot("x"));
+			
+			int nrow = dims[0], ncol = dims[1];
+			arma::SpMat<T> res(nrow, ncol);
+			
+			// create space for values, and copy
+			arma::access::rw(res.values) = arma::memory::acquire_chunked<T>(x.size() + 1);
+			arma::arrayops::copy(arma::access::rwp(res.values), x.begin(), x.size() + 1);
+			
+			// create space for row_indices, and copy 
+			arma::access::rw(res.row_indices) = 
+			    arma::memory::acquire_chunked<arma::uword>(i.size() + 1);
+			arma::arrayops::copy(arma::access::rwp(res.row_indices), i.begin(), i.size() + 1);
+			
+			// create space for col_ptrs, and copy 
+			arma::access::rw(res.col_ptrs) = arma::memory::acquire<arma::uword>(p.size() + 2);
+			arma::arrayops::copy(arma::access::rwp(res.col_ptrs), p.begin(), p.size() + 1);
+			
+			// important: set the sentinel as well
+			arma::access::rwp(res.col_ptrs)[p.size()+1] = std::numeric_limits<arma::uword>::max();
+			
+			// set the number of non-zero elements
+			arma::access::rw(res.n_nonzero) = x.size();
+			
+			return res;
+		}
+		
+	private:
+		S4 mat ;
+	} ;
+}	
+}
+
+#endif
+

Deleted: pkg/RcppArmadillo/inst/include/RcppArmadilloExtensions/spmat.h
===================================================================
--- pkg/RcppArmadillo/inst/include/RcppArmadilloExtensions/spmat.h	2013-08-20 09:30:16 UTC (rev 4435)
+++ pkg/RcppArmadillo/inst/include/RcppArmadilloExtensions/spmat.h	2013-08-20 15:23:17 UTC (rev 4436)
@@ -1,101 +0,0 @@
-// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
-/* :tabSize=4:indentSize=4:noTabs=false:folding=explicit:collapseFolds=1: */
-//
-// spmat.h: Conversion between Armadillo sp_mat and the dgCMatrix from Matrix
-//
-// Copyright (C)  2013  Dirk Eddelbuettel and Romain Francois
-//
-// This file is part of RcppArmadillo.
-//
-// RcppArmadillo is free software: you can redistribute it and/or modify it
-// under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 2 of the License, or
-// (at your option) any later version.
-//
-// RcppArmadillo is distributed in the hope that it will be useful, but
-// WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with RcppArmadillo.  If not, see <http://www.gnu.org/licenses/>.
-
-#ifndef RcppArmadillo__SpMat__h
-#define RcppArmadillo__SpMat__h
-
-#include <RcppArmadillo.h>
-
-namespace Rcpp {
-
-    
-    // converts an SEXP object from R which was created as a sparse
-    // matrix via the Matrix package) into an Armadillo sp_mat matrix
-    //
-    // TODO: template'ize to allow for types other than double, though
-    //       realistically this is all we need
-    template <> arma::sp_mat as(SEXP sx) {
-        S4 mat(sx);  
-        IntegerVector dims = mat.slot("Dim");
-        arma::urowvec i = Rcpp::as<arma::urowvec>(mat.slot("i"));
-        arma::urowvec p = Rcpp::as<arma::urowvec>(mat.slot("p"));     
-        arma::vec x     = Rcpp::as<arma::vec>(mat.slot("x"));
-        
-        int nrow = dims[0], ncol = dims[1];
-        arma::sp_mat res(nrow, ncol);
-
-        // create space for values, and copy
-        arma::access::rw(res.values) = arma::memory::acquire_chunked<double>(x.size() + 1);
-        arma::arrayops::copy(arma::access::rwp(res.values), x.begin(), x.size() + 1);
-
-        // create space for row_indices, and copy 
-        arma::access::rw(res.row_indices) = 
-            arma::memory::acquire_chunked<arma::uword>(i.size() + 1);
-        arma::arrayops::copy(arma::access::rwp(res.row_indices), i.begin(), i.size() + 1);
-    
-        // create space for col_ptrs, and copy 
-        arma::access::rw(res.col_ptrs) = arma::memory::acquire<arma::uword>(p.size() + 2);
-        arma::arrayops::copy(arma::access::rwp(res.col_ptrs), p.begin(), p.size() + 1);
-
-        // important: set the sentinel as well
-        arma::access::rwp(res.col_ptrs)[p.size()+1] = std::numeric_limits<arma::uword>::max();
-    
-        // set the number of non-zero elements
-        arma::access::rw(res.n_nonzero) = x.size();
-
-        return res;
-    }
-
-
-    // convert an Armadillo sp_mat into a corresponding R sparse matrix
-    // we copy to STL vectors as the Matrix package expects vectors whereas the
-    // default wrap in Armadillo returns matrix with one row (or col) 
-    SEXP wrap(arma::sp_mat sm) {
-
-        IntegerVector dim(2);
-        dim[0] = sm.n_rows; 
-        dim[1] = sm.n_cols;
-
-        arma::vec  x(sm.n_nonzero);        // create space for values, and copy
-        arma::arrayops::copy(x.begin(), sm.values, sm.n_nonzero);
-        std::vector<double> vx = arma::conv_to< std::vector< double > >::from(x);
-
-        arma::urowvec i(sm.n_nonzero);	// create space for row_indices, and copy & cast
-        arma::arrayops::copy(i.begin(), sm.row_indices, sm.n_nonzero);
-        std::vector<int> vi = arma::conv_to< std::vector< int > >::from(i);
- 
-        arma::urowvec p(sm.n_cols+1);	// create space for col_ptrs, and copy 
-        arma::arrayops::copy(p.begin(), sm.col_ptrs, sm.n_cols+1);
-        // do not copy sentinel for returning R
-        std::vector<int> vp = arma::conv_to< std::vector< int > >::from(p);
-
-        S4 s("dgCMatrix");
-        s.slot("i")   = vi;
-        s.slot("p")   = vp;
-        s.slot("x")   = vx;
-        s.slot("Dim") = dim;
-        return s;
-    }
-
-}
-
-#endif

Modified: pkg/RcppArmadillo/inst/include/RcppArmadilloForward.h
===================================================================
--- pkg/RcppArmadillo/inst/include/RcppArmadilloForward.h	2013-08-20 09:30:16 UTC (rev 4435)
+++ pkg/RcppArmadillo/inst/include/RcppArmadilloForward.h	2013-08-20 15:23:17 UTC (rev 4436)
@@ -45,6 +45,7 @@
     template <typename T> SEXP wrap ( const arma::field<T>& ) ;
     template <typename T> SEXP wrap ( const arma::Cube<T>& ) ;
     template <typename T> SEXP wrap ( const arma::subview<T>& ) ;
+    template <typename T> SEXP wrap ( const arma::SpMat<T>& ) ;
     
     template <typename T1, typename T2, typename glue_type> 
     SEXP wrap(const arma::Glue<T1, T2, glue_type>& X ) ;
@@ -88,6 +89,7 @@
 	template <typename T> class Exporter< arma::Mat<T> > ;
 	template <typename T> class Exporter< arma::Row<T> > ;
 	template <typename T> class Exporter< arma::Col<T> > ;
+	template <typename T> class Exporter< arma::SpMat<T> > ;
         // template <typename T> class Exporter< arma::field<T> > ;
         // template <typename T> class Exporter< arma::Cube<T> > ;
 

Modified: pkg/RcppArmadillo/inst/include/RcppArmadilloWrap.h
===================================================================
--- pkg/RcppArmadillo/inst/include/RcppArmadilloWrap.h	2013-08-20 09:30:16 UTC (rev 4435)
+++ pkg/RcppArmadillo/inst/include/RcppArmadilloWrap.h	2013-08-20 15:23:17 UTC (rev 4436)
@@ -43,42 +43,67 @@
 	    return ::Rcpp::wrap(object.memptr() , object.memptr() + object.n_elem);
 	}
 
-		template <typename T>
-		SEXP arma_subview_wrap( const arma::subview<T>& data, int nrows, int ncols ){
-			const int RTYPE = Rcpp::traits::r_sexptype_traits<T>::rtype ;
-			Rcpp::Matrix<RTYPE> mat( nrows, ncols ) ;
-			for( int j=0, k=0; j<ncols; j++)
-				for( int i=0; i<nrows; i++, k++) 
-					mat[k] = data(i,j) ;
-			return mat ;
-		}
+	template <typename T>
+	SEXP arma_subview_wrap( const arma::subview<T>& data, int nrows, int ncols ){
+		const int RTYPE = Rcpp::traits::r_sexptype_traits<T>::rtype ;
+		Rcpp::Matrix<RTYPE> mat( nrows, ncols ) ;
+		for( int j=0, k=0; j<ncols; j++)
+			for( int i=0; i<nrows; i++, k++) 
+				mat[k] = data(i,j) ;
+		return mat ;
+	}
 	
     } /* namespace RcppArmadillo */
 	
     /* wrap */
 
     template <typename T> SEXP wrap ( const arma::Mat<T>& data ){
-	return RcppArmadillo::arma_wrap( data, Dimension( data.n_rows, data.n_cols ) ) ;
+    		return RcppArmadillo::arma_wrap( data, Dimension( data.n_rows, data.n_cols ) ) ;
     }
 
     template <typename T> SEXP wrap( const arma::Col<T>& data ){
-	return RcppArmadillo::arma_wrap( data, Dimension( data.n_elem, 1) ) ;
+    		return RcppArmadillo::arma_wrap( data, Dimension( data.n_elem, 1) ) ;
     }
 
     template <typename T> SEXP wrap( const arma::Row<T>& data ){
-	return RcppArmadillo::arma_wrap(data, Dimension( 1, data.n_elem ) ) ;
+    		return RcppArmadillo::arma_wrap(data, Dimension( 1, data.n_elem ) ) ;
     }
 
     template <typename T> SEXP wrap( const arma::Cube<T>& data ){
-	return RcppArmadillo::arma_wrap(data, Dimension(  data.n_rows, data.n_cols, data.n_slices ) ) ;
+    		return RcppArmadillo::arma_wrap(data, Dimension(  data.n_rows, data.n_cols, data.n_slices ) ) ;
     }
     
     template <typename T> SEXP wrap( const arma::subview<T>& data ){
     		return RcppArmadillo::arma_subview_wrap<T>( data, data.n_rows, data.n_cols ) ;
     }
     
+    template <typename T> SEXP wrap ( const arma::SpMat<T>& sm ){
+		IntegerVector dim(2);
+		dim[0] = sm.n_rows; 
+		dim[1] = sm.n_cols;
+
+		arma::Col<T>  x(sm.n_nonzero);        // create space for values, and copy
+		arma::arrayops::copy(x.begin(), sm.values, sm.n_nonzero);
+		std::vector<T> vx = arma::conv_to< typename std::vector<T> >::from(x);
+		
+		arma::urowvec i(sm.n_nonzero);	// create space for row_indices, and copy & cast
+		arma::arrayops::copy(i.begin(), sm.row_indices, sm.n_nonzero);
+		std::vector<int> vi = arma::conv_to< std::vector< int > >::from(i);
+		
+		arma::urowvec p(sm.n_cols+1);	// create space for col_ptrs, and copy 
+		arma::arrayops::copy(p.begin(), sm.col_ptrs, sm.n_cols+1);
+		// do not copy sentinel for returning R
+		std::vector<int> vp = arma::conv_to< std::vector< int > >::from(p);
+		
+		S4 s("dgCMatrix");
+		s.slot("i")   = vi;
+		s.slot("p")   = vp;
+		s.slot("x")   = vx;
+		s.slot("Dim") = dim;
+		return s;
+    }
+
     
-    
     namespace RcppArmadillo {
 	
 	/* Importer class for field<T> */
@@ -97,9 +122,9 @@
 
     template <typename T> 
     SEXP wrap( const arma::field<T>& data){
-	RObject x = wrap( RcppArmadillo::FieldImporter<T>( data ) ) ;
-	x.attr("dim" ) = Dimension( data.n_rows, data.n_cols ) ;
-	return x ;
+		RObject x = wrap( RcppArmadillo::FieldImporter<T>( data ) ) ;
+		x.attr("dim" ) = Dimension( data.n_rows, data.n_cols ) ;
+		return x ;
     }
     
     /* TODO: maybe we could use the advanced constructor to avoid creating the 
@@ -240,31 +265,6 @@
         return wrap( eT(X) ) ;
     }
     
-    
-    /* support for Rcpp::as */
-
-    namespace traits {
-	
-	template <typename T> 
-	class Exporter< arma::Col<T> > : public IndexingExporter< arma::Col<T>, T > {
-	public: 
-	    Exporter(SEXP x) : IndexingExporter< arma::Col<T>, T >(x){}
-	}; 
-
-	template <typename T> 
-	class Exporter< arma::Row<T> > : public IndexingExporter< arma::Row<T>, T > {
-	public:
-	    Exporter(SEXP x) : IndexingExporter< arma::Row<T>, T >(x){}
-	}; 
-
-	template <typename T> 
-	class Exporter< arma::Mat<T> > : public MatrixExporter< arma::Mat<T>, T > {
-	public:
-	    Exporter(SEXP x) : MatrixExporter< arma::Mat<T>, T >(x){}
-	}; 
-	
-    } // namespace traits
-
 }
 
 #endif



More information about the Rcpp-commits mailing list