[Rcpp-commits] r2357 - in pkg/Rcpp: . inst/include/Rcpp/vector inst/unitTests

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun Oct 24 02:50:02 CEST 2010


Author: romain
Date: 2010-10-24 02:50:01 +0200 (Sun, 24 Oct 2010)
New Revision: 2357

Modified:
   pkg/Rcpp/ChangeLog
   pkg/Rcpp/inst/include/Rcpp/vector/Matrix.h
   pkg/Rcpp/inst/unitTests/runit.Matrix.R
Log:
new syntactic shortcut for indexing rows and columns of matrices

Modified: pkg/Rcpp/ChangeLog
===================================================================
--- pkg/Rcpp/ChangeLog	2010-10-23 15:22:44 UTC (rev 2356)
+++ pkg/Rcpp/ChangeLog	2010-10-24 00:50:01 UTC (rev 2357)
@@ -2,6 +2,12 @@
 
     * inst/include/Rcpp/vector/MatrixRow.h: Faster row indexing
     
+    * inst/include/Rcpp/vector/Matrix.h: added use of _ to get columns and rows
+    of a matrix. For example if x is a NumericMatrix, x(_,i) returns the same
+    as x.column(i) and x(i,_) returns the same as x.row(i)
+    
+    * inst/unitTests/runit.Matrix.R: unit tests for the above<
+    
 2010-10-21  Romain Francois <romain at r-enthusiasts.com>
 
     * inst/include/Rcpp/vector/MatrixColumn.h: Fixed indexing bug (mismatch 

Modified: pkg/Rcpp/inst/include/Rcpp/vector/Matrix.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/vector/Matrix.h	2010-10-23 15:22:44 UTC (rev 2356)
+++ pkg/Rcpp/inst/include/Rcpp/vector/Matrix.h	2010-10-24 00:50:01 UTC (rev 2357)
@@ -138,6 +138,17 @@
 		    ) ;
 	}
 	
+	typedef MatrixRow<RTYPE> Row ;
+	typedef MatrixColumn<RTYPE> Column ;
+
+	inline Row operator()( int i, internal::NamedPlaceHolder ){
+	    return Row( *this, i ) ;
+	}
+	
+	inline Column operator()( internal::NamedPlaceHolder, int i ){
+	    return Column( *this, i ) ;
+	}
+	
 private:
     
     inline int offset( int i, int j) const {
@@ -187,10 +198,7 @@
     inline int rows() const throw(not_a_matrix){ 
     	return nrows ; 
     }
-	    
-	typedef MatrixRow<RTYPE> Row ;
-	typedef MatrixColumn<RTYPE> Column ;
-
+	
 	inline Row row( int i ){ return Row( *this, i ) ; }
 	inline Column column( int i ){ return Column(*this, i ) ; }
 	

Modified: pkg/Rcpp/inst/unitTests/runit.Matrix.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.Matrix.R	2010-10-23 15:22:44 UTC (rev 2356)
+++ pkg/Rcpp/inst/unitTests/runit.Matrix.R	2010-10-24 00:50:01 UTC (rev 2357)
@@ -185,9 +185,32 @@
 			        x.row(1) + x.column(1)
 			        ) ;
 			    '
+			), 
+			"runit_NumericMatrix_colsum" = list( 
+			    signature( x = "matrix" ), 
+			    '
+                 NumericMatrix input( x ) ;
+                 int nr = input.nrow(), nc = input.ncol() ;
+                 NumericMatrix output = clone<NumericMatrix>( input ) ;
+                 for( int i=1; i<nc; i++){
+                    output(_,i) = output(_,i-1) + input(_,i) ; 
+                 }
+                 return output ;
+			    '
+			), 
+			"runit_NumericMatrix_rowsum" = list( 
+			    signature( x = "matrix" ), 
+			    '
+                 NumericMatrix input( x ) ;
+                 int nr = input.nrow(), nc = input.ncol() ;
+                 NumericMatrix output = clone<NumericMatrix>( input ) ;
+                 for( int i=1; i<nr; i++){
+                    output(i,_) = output(i-1,_) + input(i,_) ; 
+                 }
+                 return output ;
+			    '
 			)
-
-        )
+		)
         
         signatures <- lapply(f, "[[", 1L)
         bodies <- lapply(f, "[[", 2L)
@@ -211,10 +234,6 @@
 	    x[,2], 
 	    x[2,] + x[,2]
 	    )
-	cat( "\n" )
-	print( x )
-	print( target[[4]] )
-	print( res[[4]] )       
 	checkEquals( res, target, msg = "column and row as sugar" )
 	
 }
@@ -325,6 +344,17 @@
 	m <- lapply( 1:16, function(i) seq(from=1, to = i ) )
 	dim( m ) <- c( 4, 4 )
 	checkEquals( funx( m ), 1:4, msg = "List::Column" )
-	
 }
 
+test.NumericMatrix.colsum <- function( ){
+    funx <- .rcpp.Matrix$runit_NumericMatrix_colsum
+    probs <- matrix(1:12,nrow=3)
+    checkEquals( funx( probs ), t(apply(probs,1,cumsum)) )
+}
+
+test.NumericMatrix.rowsum <- function( ){
+    funx <- .rcpp.Matrix$runit_NumericMatrix_rowsum
+    probs <- matrix(1:12,nrow=3)
+    checkEquals( funx( probs ), apply(probs,2,cumsum) )
+}
+



More information about the Rcpp-commits mailing list