[Rcpp-commits] r2438 - in pkg/Rcpp: . inst/include/Rcpp/sugar/matrix inst/unitTests

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Nov 17 11:45:58 CET 2010


Author: romain
Date: 2010-11-17 11:45:58 +0100 (Wed, 17 Nov 2010)
New Revision: 2438

Added:
   pkg/Rcpp/inst/include/Rcpp/sugar/matrix/as_vector.h
Modified:
   pkg/Rcpp/ChangeLog
   pkg/Rcpp/inst/include/Rcpp/sugar/matrix/matrix_functions.h
   pkg/Rcpp/inst/unitTests/runit.sugar.R
Log:
added as_vector

Modified: pkg/Rcpp/ChangeLog
===================================================================
--- pkg/Rcpp/ChangeLog	2010-11-17 09:45:33 UTC (rev 2437)
+++ pkg/Rcpp/ChangeLog	2010-11-17 10:45:58 UTC (rev 2438)
@@ -1,3 +1,8 @@
+2010-11-17  Romain Francois <romain at r-enthusiasts.com>
+
+    * inst/include/Rcpp/sugar/as_vector.h: added the as_vector function that turns
+    a sugar matrix expression into a vector of the appropriate type
+
 2010-11-13  Romain Francois <romain at r-enthusiasts.com>
 
     * inst/include/Rcpp/Module.h: expose constructors as the "cpp_constructor"

Added: pkg/Rcpp/inst/include/Rcpp/sugar/matrix/as_vector.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/matrix/as_vector.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/matrix/as_vector.h	2010-11-17 10:45:58 UTC (rev 2438)
@@ -0,0 +1,61 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// as_vector.h: Rcpp R/C++ interface class library -- as_vector( sugar matrix expression )
+//
+// Copyright (C) 2010 Dirk Eddelbuettel and Romain Francois
+//
+// This file is part of Rcpp.
+//
+// Rcpp 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.
+//
+// Rcpp 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 Rcpp.  If not, see <http://www.gnu.org/licenses/>.
+
+#ifndef Rcpp__sugar__matrix_as_vector_h
+#define Rcpp__sugar__matrix_as_vector_h
+
+namespace Rcpp{	
+namespace internal{
+
+template <int RTYPE, bool NA, typename T>
+inline Rcpp::Vector<RTYPE> 
+as_vector__impl( MatrixBase<RTYPE,NA,T>& t, Rcpp::traits::false_type ){
+    T& ref = t.get_ref() ;
+    int nc = ref.ncol(), nr = ref.nrow() ;
+    Vector<RTYPE> out (nr*nc) ;
+    int k =0; 
+    for( int col_index=0; col_index<nc; col_index++)
+        for( int row_index=0; row_index<nr; row_index++, k++)
+            out[k] = ref( row_index, col_index ) ;
+    
+    return out ;
+}
+
+template <int RTYPE, bool NA, typename T>
+inline Rcpp::Vector<RTYPE> 
+as_vector__impl( MatrixBase<RTYPE,NA,T>& t, Rcpp::traits::true_type ){
+    Matrix<RTYPE>& ref = t.get_ref() ;
+    int size = ref.ncol()*ref.nrow() ;
+    typename Rcpp::Vector<RTYPE>::iterator first(static_cast<const Rcpp::Vector<RTYPE>&>(ref).begin())  ;
+    return Vector<RTYPE>(first, first+size );
+} 
+
+} // internal
+ 
+template <int RTYPE, bool NA, typename T>
+inline Rcpp::Vector<RTYPE> 
+as_vector( const MatrixBase<RTYPE,NA,T>& t ){
+    return internal::as_vector__impl( const_cast< MatrixBase<RTYPE,NA,T>& >(t), typename Rcpp::traits::same_type< T , Matrix<RTYPE> >() ) ;
+}
+
+} // Rcpp
+#endif
+

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/matrix/matrix_functions.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/matrix/matrix_functions.h	2010-11-17 09:45:33 UTC (rev 2437)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/matrix/matrix_functions.h	2010-11-17 10:45:58 UTC (rev 2438)
@@ -28,5 +28,6 @@
 #include <Rcpp/sugar/matrix/lower_tri.h>
 #include <Rcpp/sugar/matrix/upper_tri.h>
 #include <Rcpp/sugar/matrix/diag.h>
+#include <Rcpp/sugar/matrix/as_vector.h>
 
 #endif

Modified: pkg/Rcpp/inst/unitTests/runit.sugar.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.sugar.R	2010-11-17 09:45:33 UTC (rev 2437)
+++ pkg/Rcpp/inst/unitTests/runit.sugar.R	2010-11-17 10:45:58 UTC (rev 2438)
@@ -662,6 +662,15 @@
 			   NumericVector res = cumsum( xx ) ;
 			   return res ;
 			   '
+			), 
+			"runit_asvector" = list( 
+			    signature( x = "numeric", y = "numeric", z = "matrix" ), 
+			    '
+			    return List::create( 
+			        as_vector( NumericMatrix(z) ), 
+			        as_vector( outer( NumericVector(x) , NumericVector(y) , std::plus<double>() ) )
+			    ) ;
+			    '
 			)
 		)
 		
@@ -1294,3 +1303,10 @@
     checkEquals( fx(x), cumsum(x) )
 }
 
+test.sugar.asvector <- function(){
+    fx <- .rcpp.sugar$runit_asvector
+    res <- fx( 1:4, 1:5, diag( 1:5 ) )
+    checkEquals( res[[1]], as.vector( diag(1:5) ) )
+    checkEquals( res[[2]], as.vector( outer( 1:4, 1:5, "+" ) ) )
+}
+



More information about the Rcpp-commits mailing list