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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Nov 26 16:03:58 CET 2010


Author: romain
Date: 2010-11-26 16:03:58 +0100 (Fri, 26 Nov 2010)
New Revision: 2539

Modified:
   pkg/Rcpp/ChangeLog
   pkg/Rcpp/inst/include/Rcpp/vector/Matrix.h
Log:
more support for SubMatrix

Modified: pkg/Rcpp/ChangeLog
===================================================================
--- pkg/Rcpp/ChangeLog	2010-11-26 14:45:17 UTC (rev 2538)
+++ pkg/Rcpp/ChangeLog	2010-11-26 15:03:58 UTC (rev 2539)
@@ -11,6 +11,9 @@
     
     * inst/include/Rcpp/vector/MatrixRow.h: same for MatrixRow
     
+    * inst/include/Rcpp/vector/Matrix.h: added Matrix( SubMatrix ) and
+    Matrix::operator=( SubMatrix )
+    
 2010-11-25  Romain Francois <romain at r-enthusiasts.com>
 
     * inst/include/Rcpp/module/Module_generated_function.h: new .function with

Modified: pkg/Rcpp/inst/include/Rcpp/vector/Matrix.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/vector/Matrix.h	2010-11-26 14:45:17 UTC (rev 2538)
+++ pkg/Rcpp/inst/include/Rcpp/vector/Matrix.h	2010-11-26 15:03:58 UTC (rev 2539)
@@ -93,6 +93,10 @@
     	UNPROTECT( 2 ) ;
     	import_matrix_expression<NA,MAT>( other, nrows, nc ) ;
 	}
+	
+	// defined later
+	Matrix( const SubMatrix<RTYPE>& ) ;
+	Matrix& operator=( const SubMatrix<RTYPE>& ) ;
    
 private:
 	
@@ -248,10 +252,45 @@
         return iter[ i + j*m_nc ] ;
     }
     
+    inline vec_iterator column_iterator( int j ) const { return iter + j*m_nc ; } 
+    
 private:
     MATRIX& m ;
     vec_iterator iter ;
     int m_nc, nc, nr ;
 } ;
 
+template <int RTYPE>
+Matrix<RTYPE>::Matrix( const SubMatrix<RTYPE>& sub ) : nrows(sub.nrow()) {
+    int nc = sub.ncol() ;
+    VECTOR::setSEXP( Rf_allocMatrix( RTYPE, nrows, nc ) ) ;
+	iterator start = VECTOR::begin() ;
+	iterator rhs_it ;
+	for( int j=0; j<nc; j++){
+	    rhs_it = sub.column_iterator(j) ;
+	    for( int i=0; i<nrows; i++, ++start){
+	        *start = rhs_it[i] ;
+	    }
+	}
+}
+
+template <int RTYPE>
+Matrix<RTYPE>& Matrix<RTYPE>::operator=( const SubMatrix<RTYPE>& sub ){
+    int nc = sub.ncol(), nr = sub.nrow() ;
+    if( nc != nrow() || nr != ncol() ){
+        nrows = nr ;
+        VECTOR::setSEXP( Rf_allocMatrix( RTYPE, nr, nc ) ) ;
+	}
+	iterator start = VECTOR::begin() ;
+	iterator rhs_it ;
+	for( int j=0; j<nc; j++){
+	    rhs_it = sub.column_iterator(j) ;
+	    for( int i=0; i<nrows; i++, ++start){
+	        *start = rhs_it[i] ;
+	    }
+	}
+	return *this ;
+}
+
+
 #endif



More information about the Rcpp-commits mailing list