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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Mar 19 19:28:48 CET 2010


Author: romain
Date: 2010-03-19 19:28:48 +0100 (Fri, 19 Mar 2010)
New Revision: 924

Modified:
   pkg/Rcpp/inst/ChangeLog
   pkg/Rcpp/inst/unitTests/runit.Matrix.R
   pkg/Rcpp/src/Rcpp/Vector.h
Log:
Matrix gains a diag method to create diagnoal matrices with specification of the diagnoal value

Modified: pkg/Rcpp/inst/ChangeLog
===================================================================
--- pkg/Rcpp/inst/ChangeLog	2010-03-19 17:15:17 UTC (rev 923)
+++ pkg/Rcpp/inst/ChangeLog	2010-03-19 18:28:48 UTC (rev 924)
@@ -1,5 +1,8 @@
 2010-03-19  Romain Francois <romain at r-enthusiasts.com>
 
+	* src/Rcpp/Vector.h : Matrix gains a diag method to create diagonal 
+	matrices
+
 	* src/Rcpp/Named.h: Named is no longer a class, but a templated function.
 	This preserves all the existing interface, but also allows Named to be used
 	in making simple vectors, e.g through the Vector::create factory functions

Modified: pkg/Rcpp/inst/unitTests/runit.Matrix.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.Matrix.R	2010-03-19 17:15:17 UTC (rev 923)
+++ pkg/Rcpp/inst/unitTests/runit.Matrix.R	2010-03-19 18:28:48 UTC (rev 924)
@@ -63,3 +63,25 @@
 	checkEquals( funx(x), g(diag(matrix(1:16,ncol=4))), msg = "GenericMatrix" )
 }
 
+test.IntegerMatrix.diag <- function(){
+	fx <- cfunction( signature(), '
+		return IntegerMatrix::diag( 5, 1 ) ;
+	', 
+	Rcpp  = TRUE, includes = "using namespace Rcpp;" )
+	expected <- matrix( 0L, nrow = 5, ncol = 5 )
+	diag( expected ) <- 1L
+	checkEquals( fx(), expected, msg = "IntegerMatrix::diag" )
+}
+
+test.CharacterMatrix.diag <- function(){
+	fx <- cfunction( signature(), '
+		return CharacterMatrix::diag( 5, "foo" ) ;
+	', 
+	Rcpp  = TRUE, includes = "using namespace Rcpp;" )
+	expected <- matrix( "", nrow = 5, ncol = 5 )
+	diag( expected ) <- "foo"
+	checkEquals( fx(), expected, msg = "CharacterMatrix::diag" )
+	
+}
+
+

Modified: pkg/Rcpp/src/Rcpp/Vector.h
===================================================================
--- pkg/Rcpp/src/Rcpp/Vector.h	2010-03-19 17:15:17 UTC (rev 923)
+++ pkg/Rcpp/src/Rcpp/Vector.h	2010-03-19 18:28:48 UTC (rev 924)
@@ -2172,6 +2172,9 @@
 class Matrix : public Vector<RTYPE> {
 public:
 	typedef Vector<RTYPE> VECTOR ;
+	typedef typename VECTOR::iterator iterator ;
+    typedef typename VECTOR::converter_type converter_type ;
+    	
 	Matrix() : VECTOR() {}
 	
 	Matrix(SEXP x) throw(RObject::not_compatible) : VECTOR(){
@@ -2203,6 +2206,17 @@
 		return *this ;
 	}
 	
+	template <typename U>
+    static Matrix diag( int size, const U& diag_value ){
+    	Matrix res(size,size) ;
+    	iterator it( res.begin() ) ;
+    	for( int i=0; i<size; i++){
+    		*it = converter_type::get( diag_value );
+    		it += ( size + 1 ); 
+    	}
+    	return res ;
+    }
+    
 private:
 	virtual void update(){
 		VECTOR::update_vector() ;



More information about the Rcpp-commits mailing list