[Rcpp-commits] r911 - in pkg/RcppArmadillo: . inst src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Mar 17 13:49:17 CET 2010


Author: romain
Date: 2010-03-17 13:49:17 +0100 (Wed, 17 Mar 2010)
New Revision: 911

Modified:
   pkg/RcppArmadillo/DESCRIPTION
   pkg/RcppArmadillo/inst/ChangeLog
   pkg/RcppArmadillo/src/RcppArmadillo.cpp
   pkg/RcppArmadillo/src/fastLm.cpp
Log:
update RcppArmadillo to use make_list

Modified: pkg/RcppArmadillo/DESCRIPTION
===================================================================
--- pkg/RcppArmadillo/DESCRIPTION	2010-03-16 22:19:30 UTC (rev 910)
+++ pkg/RcppArmadillo/DESCRIPTION	2010-03-17 12:49:17 UTC (rev 911)
@@ -1,7 +1,7 @@
 Package: RcppArmadillo
 Type: Package
 Title: Rcpp/Armadillo translation layer
-Version: 0.1.0
+Version: 0.1.1
 Date: $Date$
 Author: Romain Francois and Dirk Eddelbuettel
 Maintainer: Dirk Eddelbuettel and Romain Francois <RomainAndDirk at r-enthusiasts.com>
@@ -24,7 +24,7 @@
  capabilities of the Rcpp package for seamless R and C++ integration/
 License: GPL (>= 2)
 LazyLoad: yes
-Depends: Rcpp (>= 0.7.8)
+Depends: Rcpp (>= 0.7.10.1)
 SystemRequirements: Armadillo (>= 0.6.10), Boost
 URL: http://arma.sourceforge.net/, http://dirk.eddelbuettel.com/code/rcpp.html
 OS_type: unix

Modified: pkg/RcppArmadillo/inst/ChangeLog
===================================================================
--- pkg/RcppArmadillo/inst/ChangeLog	2010-03-16 22:19:30 UTC (rev 910)
+++ pkg/RcppArmadillo/inst/ChangeLog	2010-03-17 12:49:17 UTC (rev 911)
@@ -1,3 +1,10 @@
+2010-03-17   Romain Francois <romain at r-enthusiasts.com>
+
+	* src/fastLm.cpp: added some arma:: prefix
+	
+	* src/RcppArmadillo.cpp: update examples to use make_list and showcase
+	various ways to use them : Named(.)=., _[.] = , Named( ., . )
+
 2010-03-11  Dirk Eddelbuettel  <edd at debian.org>
 
 	* DESCRIPTION: Release 0.1.0 and initial CRAN upload

Modified: pkg/RcppArmadillo/src/RcppArmadillo.cpp
===================================================================
--- pkg/RcppArmadillo/src/RcppArmadillo.cpp	2010-03-16 22:19:30 UTC (rev 910)
+++ pkg/RcppArmadillo/src/RcppArmadillo.cpp	2010-03-17 12:49:17 UTC (rev 911)
@@ -26,55 +26,27 @@
 	
 extern "C" SEXP RcppArmadillo_wrap(){
 	
-    List output(4); 
-    List matrices(4) ;
-    List rows(2) ;
-    List cols(2) ;
+    // using the _ notation
+    List matrices = make_list( 
+    	_["Mat<int>"]          = eye<imat>( 3,3 ), 
+    	_["Mat<double>"]       = eye<mat>( 3,3 ),
+    	_["Mat<float>"]        = eye<fmat>( 3, 3 ), 
+    	_["Mat<unsigned int>"] = eye<umat>( 3, 3 )
+    	) ;
+    
+    // using the Named(.) = . notation
+    List cols = make_list( 
+    	Named( "Col<double>" ) = zeros<mat>(5,1), 
+    	Named( "Col<float>" )  = zeros<fmat>(5,1)
+    	) ; 
+    
+    // using the Named( . , . ) notation
+    List rows = make_list( 
+    	Named( "Row<double>", zeros<mat>(1,5) ),
+    	Named( "Row<float>" , zeros<fmat>(1,5) )
+    	) ;
 	
-    imat x1 = eye<imat>( 3,3 ) ;
-    // x1.swap_rows(0,1)  ;
-    matrices[0] = x1 ;
-	
-    mat x2 = eye<mat>( 3,3 ) ;
-    // x2.swap_rows(0,1)  ;
-    matrices[1] = x2 ;
-	
-    fmat x3 = eye<fmat>( 3, 3 );
-    // x3.swap_rows(0,1)  ;
-    matrices[2] = x3 ;
-	
-    umat x4 = eye<umat>( 3, 3 );
-    // x4.swap_rows(0,1)  ;
-    matrices[3] = x4 ;
-	
-    colvec r1 = zeros<mat>(5,1);
-    cols[0] = r1  ;
-	
-    fcolvec r2 = zeros<fmat>(5,1);
-    cols[1] = r2  ;
-	
-    rowvec c1 = zeros<mat>(1,5);
-    rows[0] = c1  ;
-	
-    frowvec c2 = zeros<fmat>(1,5);
-    rows[1] = c2  ;
-	
-    std::vector<std::string> names(4) ;
-    names[0] = "Mat<int>" ;
-    names[1] = "Mat<double>" ;
-    names[2] = "Mat<float>" ;
-    names[3] = "Mat<unsigned int>" ;
-    matrices.names() = names ;
-	
-    names.resize(2) ;
-    names[0] = "Row<double>" ;
-    names[1] = "Row<float>" ;
-    rows.names() = names ;
-	
-    names[0] = "Col<double>" ;
-    names[1] = "Col<float>" ;
-    cols.names() = names ;
-	
+    // creating an empty list and grow it on demand
     List fields ;
     field<int> f1( 2, 2 ) ;
     f1( 0, 0 ) = 0 ; 
@@ -95,17 +67,12 @@
     f3(1,1) = zeros<mat>(2,1) ;
     fields["field<colvec>"] = f3 ;
 	
-    output[0] = matrices;
-    output[1] = rows ;
-    output[2] = cols ;
-    output[3] = fields ;
-	
-    names[0] = "matrices : Mat<T>" ; 
-    names[1] = "rows : Row<T>" ;
-    names.push_back( "columns : Col<T>" );
-    names.push_back( "fields  : field<T>" );
-    output.names() = names ;
-	
+    List output = make_list( 
+    	_["matrices : Mat<T>"]  = matrices, 
+    	_["rows : Row<T>"]      = rows,
+    	_["columns : Col<T>"]   = cols, 
+    	_["fields  : field<T>"] = fields ) ;
+    
     return output ;
 	
 }
@@ -113,57 +80,55 @@
 extern "C" SEXP RcppArmadillo_as_Mat(SEXP input_){
 
     List input(input_) ;
-    arma::imat m1 = input[0] ; /* implicit as */
-    arma::mat  m2 = input[1] ; /* implicit as */
-    arma::umat m3 = input[0] ; /* implicit as */
-    arma::fmat m4 = input[1] ; /* implicit as */
+    imat m1 = input[0] ; /* implicit as */
+    mat  m2 = input[1] ; /* implicit as */
+    umat m3 = input[0] ; /* implicit as */
+    fmat m4 = input[1] ; /* implicit as */
 	
-    List res(4) ;
-    res[0] = arma::accu( m1 ) ;
-    res[1] = arma::accu( m2 ) ;
-    res[2] = arma::accu( m3 ) ;
-    res[3] = arma::accu( m4 ) ;
-	
+    List res = make_list( 
+    	accu( m1 ),
+    	accu( m2 ),
+    	accu( m3 ),
+    	accu( m4 ) ) ;
+    
     return res ;
 }
 
 extern "C" SEXP RcppArmadillo_as_Col( SEXP input_){
     List input(input_) ;
-    arma::icolvec m1 = input[0] ; /* implicit as */
-    arma::colvec  m2 = input[1] ; /* implicit as */
-    arma::ucolvec m3 = input[0] ; /* implicit as */
-    arma::fcolvec m4 = input[1] ; /* implicit as */
+    icolvec m1 = input[0] ; /* implicit as */
+    colvec  m2 = input[1] ; /* implicit as */
+    ucolvec m3 = input[0] ; /* implicit as */
+    fcolvec m4 = input[1] ; /* implicit as */
 	
-    List res(4) ;
-    res[0] = arma::accu( m1 ) ;
-    res[1] = arma::accu( m2 ) ;
-    res[2] = arma::accu( m3 ) ;
-    res[3] = arma::accu( m4 ) ;
-	
+    List res = make_list( 
+    	accu( m1 ),
+    	accu( m2 ),
+    	accu( m3 ),
+    	accu( m4 ) ) ;
+    
     return res ;
 }
 
 extern "C" SEXP RcppArmadillo_as_Row(SEXP input_){
     List input(input_) ;
-    arma::irowvec m1 = input[0] ; /* implicit as */
-    arma::rowvec  m2 = input[1] ; /* implicit as */
-    arma::urowvec m3 = input[0] ; /* implicit as */
-    arma::frowvec m4 = input[1] ; /* implicit as */
+    irowvec m1 = input[0] ; /* implicit as */
+    rowvec  m2 = input[1] ; /* implicit as */
+    urowvec m3 = input[0] ; /* implicit as */
+    frowvec m4 = input[1] ; /* implicit as */
 	
-    List res(4) ;
-    res[0] = arma::accu( m1 ) ;
-    res[1] = arma::accu( m2 ) ;
-    res[2] = arma::accu( m3 ) ;
-    res[3] = arma::accu( m4 ) ;
-	
+    List res = make_list( 
+    	accu( m1 ),
+    	accu( m2 ),
+    	accu( m3 ),
+    	accu( m4 ) ) ;
     return res ;
-	
 }
 
 extern "C" SEXP RcppArmadillo_wrap_Glue(){
 	
-    arma::mat m1 = eye<mat>( 3, 3 ) ;
-    arma::mat m2 = eye<mat>( 3, 3 ) ;
+    mat m1 = eye<mat>( 3, 3 ) ;
+    mat m2 = eye<mat>( 3, 3 ) ;
 	
     List res ;
     res["mat+mat"] = m1 + m2 ;
@@ -172,10 +137,10 @@
 
 extern "C" SEXP RcppArmadillo_wrap_Op(){
 
-    arma::mat m1 = eye<mat>( 3, 3 ) ;
+    mat m1 = eye<mat>( 3, 3 ) ;
 	
     List res ;
-    res["mat+mat"] = - m1 ;
+    res["- mat"] = - m1 ;
     return res ;
 }
 

Modified: pkg/RcppArmadillo/src/fastLm.cpp
===================================================================
--- pkg/RcppArmadillo/src/fastLm.cpp	2010-03-16 22:19:30 UTC (rev 910)
+++ pkg/RcppArmadillo/src/fastLm.cpp	2010-03-17 12:49:17 UTC (rev 911)
@@ -21,6 +21,8 @@
 
 #include <RcppArmadillo.h>
 
+// [romain]: what about "using namespace arma; " in the function
+
 extern "C" SEXP fastLm(SEXP ys, SEXP Xs) {
 
     Rcpp::NumericVector yr(ys);			// creates Rcpp vector from SEXP
@@ -35,20 +37,21 @@
     arma::colvec y(yr.begin(), yr.size());
 #endif
 
-    arma::colvec coef = solve(X, y);            // fit model y ~ X
+	arma::colvec coef = arma::solve(X, y);            // fit model y ~ X
+	arma::colvec resid = y - X*coef; 		// residuals
 
-    arma::colvec resid = y - X*coef; 		// residuals
-
 #if ARMA_VERSION_GE_090
     double sig2 = arma::as_scalar( trans(resid)*resid/(n-k) );
 #else
-    double sig2 = ( trans(resid)*resid/(n-k) );
+	double sig2 = ( arma::trans(resid)*resid/(n-k) );
 #endif
     						// std.error of estimate 
-    arma::colvec stderrest = sqrt( sig2 * diagvec( arma::inv(arma::trans(X)*X)) );
+    arma::colvec stderrest = arma::sqrt( sig2 * arma::diagvec( arma::inv(arma::trans(X)*X)) );
 
-    Rcpp::Pairlist res(Rcpp::Named( "coefficients", coef),
-                       Rcpp::Named( "stderr", stderrest));
-    return res;
+    return Rcpp::make_list( 
+    	Rcpp::Named("coefficients") = coef,
+    	Rcpp::Named("stderr")       = stderrest
+    ) ;
+    
 }
 



More information about the Rcpp-commits mailing list