[Rcpp-commits] r303 - in pkg: inst inst/unitTests src src/Rcpp
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Thu Jan 7 17:38:02 CET 2010
Author: romain
Date: 2010-01-07 17:38:02 +0100 (Thu, 07 Jan 2010)
New Revision: 303
Added:
pkg/inst/unitTests/runit.ComplexVector.R
pkg/src/ComplexVector.cpp
pkg/src/Rcpp/ComplexVector.h
Modified:
pkg/inst/ChangeLog
pkg/src/Rcpp.h
pkg/src/RcppCommon.h
Log:
+Rcpp::ComplexVector
Modified: pkg/inst/ChangeLog
===================================================================
--- pkg/inst/ChangeLog 2010-01-07 15:27:21 UTC (rev 302)
+++ pkg/inst/ChangeLog 2010-01-07 16:38:02 UTC (rev 303)
@@ -1,5 +1,10 @@
2010-01-07 Romain Francois <francoisromain at free.fr>
+ * src/Rcpp/ComplexVector.h: new class Rcpp::ComplexVector
+ to manage ... complex vectors (CPLXSXP)
+ * src/ComplexVector.cpp: implementation
+ * inst/unitTests/runit.ComplexVector.R: unit tests
+
* src/Rcpp/Promise.h: new class Rcpp::Promise to manage
promises (PROMSXP). only read access so far (no way to force
the promise)
Added: pkg/inst/unitTests/runit.ComplexVector.R
===================================================================
--- pkg/inst/unitTests/runit.ComplexVector.R (rev 0)
+++ pkg/inst/unitTests/runit.ComplexVector.R 2010-01-07 16:38:02 UTC (rev 303)
@@ -0,0 +1,61 @@
+#!/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/>.
+
+.setUp <- function(){
+ suppressMessages( require( inline ) )
+}
+
+test.ComplexVector <- function(){
+ funx <- cfunction(signature(), '
+ ComplexVector x(10) ;
+ Rcomplex rc ;
+ for( int i=0; i<10; i++) {
+ rc.r = rc.i = i + 0.0 ;
+ x[i] = rc ;
+ }
+ return x ;',
+ Rcpp=TRUE, verbose=FALSE, includes = "using namespace Rcpp;" )
+ checkEquals( funx(), 0:9*(1+1i), msg = "ComplexVector" )
+}
+
+test.ComplexVector.INTSXP <- function(){
+ funx <- cfunction(signature(vec = "complex" ), '
+ ComplexVector x(vec) ;
+ for( int i=0; i<x.size(); i++) {
+ x[i].r = x[i].r*2 ;
+ x[i].i = x[i].i*2 ;
+ }
+ return x ;',
+ Rcpp=TRUE, verbose=FALSE, includes = "using namespace Rcpp;" )
+ checkEquals( funx(0:9*(1+1i)), 2*0:9*(1+1i), msg = "ComplexVector( CPLXSXP) " )
+}
+
+test.ComplexVector.initializer.list <- function(){
+ if( Rcpp:::capabilities()[["initializer lists"]] ){
+ funx <- cfunction(signature(), '
+ Rcomplex c1 ; c1.r = c1.i = 0.0 ;
+ Rcomplex c2 ; c2.r = c2.i = 1.0 ;
+ ComplexVector x = { c1, c2 } ;
+ return x ;',
+ Rcpp=TRUE, verbose=FALSE, includes = "using namespace Rcpp;" )
+ checkEquals( funx(), c( 0:1*(1+1i)), msg = "ComplexVector( initializer list) " )
+ }
+}
+
+
Added: pkg/src/ComplexVector.cpp
===================================================================
--- pkg/src/ComplexVector.cpp (rev 0)
+++ pkg/src/ComplexVector.cpp 2010-01-07 16:38:02 UTC (rev 303)
@@ -0,0 +1,68 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// ComplexVector.h: Rcpp R/C++ interface class library -- complex vectors
+//
+// 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/>.
+
+#include <RcppCommon.h>
+#include <Rcpp/RObject.h>
+#include <Rcpp/ComplexVector.h>
+#include <algorithm>
+
+namespace Rcpp{
+
+ ComplexVector::ComplexVector(SEXP x) throw(not_compatible) : RObject() {
+ switch( TYPEOF( x ) ){
+ case CPLXSXP:
+ setSEXP( x ) ;
+ break ;
+ case REALSXP:
+ case LGLSXP:
+ case RAWSXP:
+ case INTSXP:
+ setSEXP( Rf_coerceVector( x, CPLXSXP) ) ;
+ break ;
+ default:
+ throw not_compatible( "cannot convert to complex vector" ) ;
+ }
+ }
+
+ ComplexVector::ComplexVector(int size) : RObject() {
+ setSEXP( Rf_allocVector(CPLXSXP, size) ) ;
+ }
+
+#ifdef HAS_INIT_LISTS
+ ComplexVector::ComplexVector( std::initializer_list<Rcomplex> list ) {
+ SEXP x = PROTECT( Rf_allocVector( CPLXSXP, list.size() ) ) ;
+ std::copy( list.begin(), list.end(), COMPLEX(x) );
+ setSEXP(x) ;
+ UNPROTECT( 1 ); /* x */
+ }
+#endif
+
+Rcomplex& ComplexVector::operator[]( int i ) const {
+ return COMPLEX(m_sexp)[i] ;
+}
+Rcomplex* ComplexVector::begin() const {
+ return COMPLEX(m_sexp) ;
+}
+Rcomplex* ComplexVector::end() const {
+ return COMPLEX(m_sexp) + LENGTH(m_sexp);
+}
+
+} // namespace
Added: pkg/src/Rcpp/ComplexVector.h
===================================================================
--- pkg/src/Rcpp/ComplexVector.h (rev 0)
+++ pkg/src/Rcpp/ComplexVector.h 2010-01-07 16:38:02 UTC (rev 303)
@@ -0,0 +1,65 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// ComplexVector.h: Rcpp R/C++ interface class library -- complex vectors
+//
+// 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_ComplexVector_h
+#define Rcpp_ComplexVector_h
+
+#include <RcppCommon.h>
+#include <Rcpp/RObject.h>
+
+#ifdef HAS_INIT_LISTS
+#include <initializer_list>
+#include <algorithm>
+#endif
+
+namespace Rcpp{
+
+class ComplexVector : public RObject {
+public:
+
+ ComplexVector(SEXP x) throw(not_compatible);
+ ComplexVector(int size) ;
+
+#ifdef HAS_INIT_LISTS
+ ComplexVector( std::initializer_list<Rcomplex> list ) ;
+#endif
+
+ /**
+ * the length of the vector, uses Rf_length
+ */
+ inline int length() const { return Rf_length( m_sexp ) ; }
+
+ /**
+ * alias of length
+ */
+ inline int size() const { return Rf_length( m_sexp ) ; }
+
+ Rcomplex& operator[]( int i ) const ;
+ Rcomplex* begin() const ;
+ Rcomplex* end() const ;
+
+ typedef Rcomplex* iterator ;
+
+} ;
+
+} // namespace
+
+#endif
Modified: pkg/src/Rcpp.h
===================================================================
--- pkg/src/Rcpp.h 2010-01-07 15:27:21 UTC (rev 302)
+++ pkg/src/Rcpp.h 2010-01-07 16:38:02 UTC (rev 303)
@@ -64,6 +64,6 @@
#include <Rcpp/WeakReference.h>
#include <Rcpp/CharacterVector.h>
#include <Rcpp/ExpressionVector.h>
+#include <Rcpp/ComplexVector.h>
-
#endif
Modified: pkg/src/RcppCommon.h
===================================================================
--- pkg/src/RcppCommon.h 2010-01-07 15:27:21 UTC (rev 302)
+++ pkg/src/RcppCommon.h 2010-01-07 16:38:02 UTC (rev 303)
@@ -48,6 +48,7 @@
#include <R.h>
#include <Rinternals.h>
#include <R_ext/Callbacks.h>
+#include <R_ext/Complex.h>
#include <Rversion.h>
#define GET_NAMES(x) Rf_getAttrib(x, R_NamesSymbol)
#include <Rcpp/Rcpp_Rinternals.h>
More information about the Rcpp-commits
mailing list