[Rcpp-commits] r1678 - in pkg/Rcpp/inst: doc/Rcpp-sugar include/Rcpp/sugar/functions unitTests

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Jun 23 19:36:38 CEST 2010


Author: romain
Date: 2010-06-23 19:36:37 +0200 (Wed, 23 Jun 2010)
New Revision: 1678

Added:
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/abs.h
   pkg/Rcpp/inst/unitTests/runit.sugar.abs.R
Modified:
   pkg/Rcpp/inst/doc/Rcpp-sugar/Rcpp-sugar.Rnw
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h
Log:
sugar/abs

Modified: pkg/Rcpp/inst/doc/Rcpp-sugar/Rcpp-sugar.Rnw
===================================================================
--- pkg/Rcpp/inst/doc/Rcpp-sugar/Rcpp-sugar.Rnw	2010-06-23 17:35:38 UTC (rev 1677)
+++ pkg/Rcpp/inst/doc/Rcpp-sugar/Rcpp-sugar.Rnw	2010-06-23 17:36:37 UTC (rev 1678)
@@ -424,6 +424,32 @@
 sign( xx * xx )
 @
 
+\subsubsection{diff}
+
+The i\textsuperscript{th} element of the result of \texttt{diff} is 
+the difference between the $(i+1)$\textsuperscript{th} and the 
+i\textsuperscript{th} element of the input expression. Supported types are
+integer and numeric.
+
+<<lang=cpp>>=
+IntegerVector xx;
+
+diff( xx )
+@
+
+\subsubsection{abs}
+
+The i\textsuperscript{th} element of the result of \texttt{abs} is 
+the absolute value of the i\textsuperscript{th} element of the input expression. 
+Supported types are integer and numeric.
+
+<<lang=cpp>>=
+IntegerVector xx;
+
+abs( xx )
+@
+
+
 \section{Performance}
 \label{sec:performance}
 

Added: pkg/Rcpp/inst/include/Rcpp/sugar/functions/abs.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/abs.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/abs.h	2010-06-23 17:36:37 UTC (rev 1678)
@@ -0,0 +1,79 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// abs.h: Rcpp R/C++ interface class library -- abs
+//
+// 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/>.
+
+#ifndef Rcpp__sugar__abs_h
+#define Rcpp__sugar__abs_h
+
+namespace Rcpp{
+namespace sugar{
+
+template <bool NA, int RTYPE>
+class abs__impl{
+public:
+	typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
+	static inline STORAGE get( STORAGE x){
+		return Rcpp::traits::is_na<RTYPE>(x) ? NA_INTEGER : ( x > 0 ? x : -x ) ;
+	}
+} ;
+
+template <int RTYPE>
+class abs__impl<false,RTYPE>{
+public:
+	typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
+	static inline STORAGE get( STORAGE x){
+		return ( x > 0 ? x : -x ) ;
+	}
+} ;
+
+	
+template <int RTYPE, bool NA, typename T>
+class Abs : public Rcpp::VectorBase< RTYPE,NA, Abs<RTYPE,NA,T> > {
+public:
+	typedef typename Rcpp::VectorBase<RTYPE,NA,T> VEC_TYPE ;
+	typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
+	
+	Abs( const VEC_TYPE& object_ ) : object(object_){}
+	
+	inline STORAGE operator[]( int i ) const {
+		return abs__impl<NA,RTYPE>::get( object[i] );
+	}
+	inline int size() const { return object.size() ; }
+	         
+private:
+	const VEC_TYPE& object ;
+} ;
+	
+} // sugar
+
+template <bool NA, typename T>
+inline sugar::Abs<INTSXP,NA,T> abs( const VectorBase<INTSXP,NA,T>& t){
+	return sugar::Abs<INTSXP,NA,T>( t ) ;
+}
+
+template <bool NA, typename T>
+inline sugar::Abs<REALSXP,NA,T> abs( const VectorBase<REALSXP,NA,T>& t){
+	return sugar::Abs<REALSXP,NA,T>( t ) ;
+}
+
+
+} // Rcpp
+#endif
+

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h	2010-06-23 17:35:38 UTC (rev 1677)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h	2010-06-23 17:36:37 UTC (rev 1678)
@@ -33,5 +33,6 @@
 #include <Rcpp/sugar/functions/pmax.h>
 #include <Rcpp/sugar/functions/sign.h>
 #include <Rcpp/sugar/functions/diff.h>
+#include <Rcpp/sugar/functions/abs.h>
 
 #endif

Added: pkg/Rcpp/inst/unitTests/runit.sugar.abs.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.sugar.abs.R	                        (rev 0)
+++ pkg/Rcpp/inst/unitTests/runit.sugar.abs.R	2010-06-23 17:36:37 UTC (rev 1678)
@@ -0,0 +1,34 @@
+#!/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.sugar.abs <- function( ){
+
+	fx <- cxxfunction( signature( x = "numeric", y = "integer" ), '
+	
+		NumericVector xx(x) ;
+		IntegerVector yy(y) ;
+		
+		return List::create( abs(xx), abs(yy) ) ;
+	', plugin = "Rcpp" )
+	
+	x <- rnorm(10)
+	y <- -10:10
+	checkEquals( fx(x,y) , list( abs(x), abs(y) ) )
+}
+



More information about the Rcpp-commits mailing list