[Rcpp-commits] r722 - in pkg/Rcpp: . inst inst/unitTests src/Rcpp

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Feb 17 13:41:38 CET 2010


Author: romain
Date: 2010-02-17 13:41:38 +0100 (Wed, 17 Feb 2010)
New Revision: 722

Added:
   pkg/Rcpp/inst/unitTests/runit.SimpleMatrix.R
Modified:
   pkg/Rcpp/DESCRIPTION
   pkg/Rcpp/NEWS
   pkg/Rcpp/inst/ChangeLog
   pkg/Rcpp/src/Rcpp/SimpleVector.h
Log:
first attempt at SimpleMatrix<int> and derived NumericMatrix, IntegerMatrix, etc ...

Modified: pkg/Rcpp/DESCRIPTION
===================================================================
--- pkg/Rcpp/DESCRIPTION	2010-02-17 12:08:26 UTC (rev 721)
+++ pkg/Rcpp/DESCRIPTION	2010-02-17 12:41:38 UTC (rev 722)
@@ -1,6 +1,6 @@
 Package: Rcpp
 Title: Rcpp R/C++ interface package
-Version: 0.7.7.2
+Version: 0.7.7.3
 Date: $Date$
 Author: Dirk Eddelbuettel and Romain Francois, with contributions 
  by Simon Urbanek and David Reiss; based on code written during 

Modified: pkg/Rcpp/NEWS
===================================================================
--- pkg/Rcpp/NEWS	2010-02-17 12:08:26 UTC (rev 721)
+++ pkg/Rcpp/NEWS	2010-02-17 12:41:38 UTC (rev 722)
@@ -1,5 +1,12 @@
 0.7.8   (under development)
 
+    o   new template class SimpleMatrix<int> and generated classes
+    	IntegerMatrix, RawMatrix, NumericMatrix, ComplexMatrix and 
+    	LogicalMatrix. These classes derived from their Vector 
+    	equivalent except that their constructors check that their 
+    	input is a matrix. The classes don't/can't however guarantee 
+    	that the enclosed SEXP will allways be a matrix
+
     o	RcppExample et al have been moved to a new package RcppExamples
 
     o	New examples for 'fast lm' using compiled code: 

Modified: pkg/Rcpp/inst/ChangeLog
===================================================================
--- pkg/Rcpp/inst/ChangeLog	2010-02-17 12:08:26 UTC (rev 721)
+++ pkg/Rcpp/inst/ChangeLog	2010-02-17 12:41:38 UTC (rev 722)
@@ -1,5 +1,12 @@
 2010-02-17  Romain Francois <romain at r-enthusiasts.com>
 
+	* src/Rcpp/SimpleVector.h : new template class SimpleMatrix<int>
+	that derives from SimpleVector<int>, with typedefs NumericMatrix
+	IntegerMatrix, LogicalMatrix, RawMatrix, ComplexMatrix. The actual 
+	functionality comes from SimpleVector but SimpleMatrix has 
+	constructors and assignment operators that behave slighlty
+	differently : they check that their input is a matrix
+
 	* src/Rcpp/traits/wrap_type_traits: added support for float
 
 	* src/Rcpp/Rcpp.h: now including RObject here instead of in 

Added: pkg/Rcpp/inst/unitTests/runit.SimpleMatrix.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.SimpleMatrix.R	                        (rev 0)
+++ pkg/Rcpp/inst/unitTests/runit.SimpleMatrix.R	2010-02-17 12:41:38 UTC (rev 722)
@@ -0,0 +1,36 @@
+#!/usr/bin/r -t
+#
+# 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/>.
+
+test.NumericMatrix <- function(){
+	funx <- cfunction(signature(x = "matrix" ), '
+		NumericMatrix m(x) ;
+		double trace = 0.0 ;
+		for( size_t i=0 ; i<4; i++){
+			trace += m(i,i) ;
+		}
+		return wrap( trace ) ;
+	', Rcpp = TRUE, includes = "using namespace Rcpp;"  )
+	x <- matrix( 1:16 + .5, ncol = 4 )
+	checkEquals( funx(x), sum(diag(x)), msg = "matrix indexing" )
+	
+	y <- as.vector( x )
+	checkException( funx(y) , msg = "not a matrix" )
+	
+}
+

Modified: pkg/Rcpp/src/Rcpp/SimpleVector.h
===================================================================
--- pkg/Rcpp/src/Rcpp/SimpleVector.h	2010-02-17 12:08:26 UTC (rev 721)
+++ pkg/Rcpp/src/Rcpp/SimpleVector.h	2010-02-17 12:41:38 UTC (rev 722)
@@ -105,25 +105,82 @@
 		UNPROTECT(1) ;
 	}
 
+protected:
+	void init(){
+		internal::r_init_vector<RTYPE>(m_sexp) ;
+	}
+	
+	void update_vector(){
+		start = internal::r_vector_start<RTYPE,value_type>(m_sexp) ;
+	}
+	
 private:
 	value_type* start ;
 	
 	virtual void update(){ 
-		start = internal::r_vector_start<RTYPE,value_type>(m_sexp) ;
+		update_vector() ;
 	}
 	
-	void init(){
-		internal::r_init_vector<RTYPE>(m_sexp) ;
+} ;
+
+template <int RTYPE> 
+class SimpleMatrix : public SimpleVector<RTYPE> {
+public:
+	SimpleMatrix() : SimpleVector<RTYPE>() {}
+	
+	SimpleMatrix(SEXP x) throw(RObject::not_compatible) : SimpleVector<RTYPE>(){
+		if( ! ::Rf_isMatrix(x) ) throw RObject::not_compatible("not a matrix") ;
+		SEXP y = r_cast<RTYPE>( x ) ;
+		SimpleVector<RTYPE>::setSEXP( y );
 	}
 	
+	SimpleMatrix( const Dimension& dims) : SimpleVector<RTYPE>() {
+		if( dims.size() != 2 ) throw RObject::not_compatible("not a matrix") ;
+		SimpleVector<RTYPE>::setSEXP( Rf_allocVector( RTYPE, dims.prod() ) ) ;
+		SimpleVector<RTYPE>::init() ;
+		SimpleVector<RTYPE>::attr( "dim" ) = dims ;
+	}
+	
+	SimpleMatrix( const int& nrows, const int& ncols) : SimpleVector<RTYPE>() {
+		SimpleVector<RTYPE>::setSEXP( Rf_allocVector( RTYPE, nrows*ncols ) ) ;
+		SimpleVector<RTYPE>::init() ;
+		SimpleVector<RTYPE>::attr( "dim" ) = Dimension( nrows, ncols ) ;
+	}
+	
+	
+	SimpleMatrix( const SimpleMatrix& other) : SimpleVector<RTYPE>() {
+		SEXP x = other.asSexp() ;
+		if( ! ::Rf_isMatrix(x) ) throw RObject::not_compatible("not a matrix") ;
+		SimpleVector<RTYPE>::setSEXP( x ) ;
+	}
+	
+	SimpleMatrix& operator=(const SimpleMatrix& other) {
+		SEXP x = other.asSexp() ;
+		if( ! ::Rf_isMatrix(x) ) throw RObject::not_compatible("not a matrix") ;
+		SimpleVector<RTYPE>::setSEXP( x ) ;
+		return *this ;
+	}
+	
+private:
+	virtual void update(){
+		SimpleVector<RTYPE>::update_vector() ;
+	}
+	
 } ;
 
+
 typedef SimpleVector<CPLXSXP> ComplexVector ;
 typedef SimpleVector<INTSXP> IntegerVector ;
 typedef SimpleVector<LGLSXP> LogicalVector ;
 typedef SimpleVector<REALSXP> NumericVector ;
 typedef SimpleVector<RAWSXP> RawVector ;
 
+typedef SimpleMatrix<CPLXSXP> ComplexMatrix ;
+typedef SimpleMatrix<INTSXP> IntegerMatrix ;
+typedef SimpleMatrix<LGLSXP> LogicalMatrix ;
+typedef SimpleMatrix<REALSXP> NumericMatrix ;
+typedef SimpleMatrix<RAWSXP> RawMatrix ;
+
 }// namespace Rcpp
 
 #endif



More information about the Rcpp-commits mailing list