[Rcpp-commits] r1834 - in pkg/Rcpp/inst: include include/Rcpp/sugar/matrix include/Rcpp/traits include/Rcpp/vector unitTests
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Thu Jul 8 13:46:47 CEST 2010
Author: romain
Date: 2010-07-08 13:46:47 +0200 (Thu, 08 Jul 2010)
New Revision: 1834
Added:
pkg/Rcpp/inst/include/Rcpp/traits/if_.h
Modified:
pkg/Rcpp/inst/include/Rcpp/sugar/matrix/diag.h
pkg/Rcpp/inst/include/Rcpp/vector/Matrix.h
pkg/Rcpp/inst/include/RcppCommon.h
pkg/Rcpp/inst/unitTests/runit.sugar.R
Log:
dispatch on diag so that it does different things on vectors or matrices (just like in R)
Modified: pkg/Rcpp/inst/include/Rcpp/sugar/matrix/diag.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/matrix/diag.h 2010-07-08 10:09:53 UTC (rev 1833)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/matrix/diag.h 2010-07-08 11:46:47 UTC (rev 1834)
@@ -22,16 +22,16 @@
#ifndef Rcpp__sugar__diag_h
#define Rcpp__sugar__diag_h
-namespace Rcpp{
+namespace Rcpp{
namespace sugar{
template <int RTYPE, bool NA, typename T>
-class Matrix_Diag : public Rcpp::VectorBase< RTYPE ,NA, Matrix_Diag<RTYPE,NA,T> > {
+class Diag_Extractor : public Rcpp::VectorBase< RTYPE ,NA, Diag_Extractor<RTYPE,NA,T> > {
public:
typedef typename Rcpp::MatrixBase<RTYPE,NA,T> MAT_TYPE ;
typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
- Matrix_Diag( const MAT_TYPE& object_ ) : object(object_), n(0) {
+ Diag_Extractor( const MAT_TYPE& object_ ) : object(object_), n(0) {
int nr = object.nrow() ;
int nc = object.ncol() ;
n = (nc < nr ) ? nc : nr ;
@@ -47,15 +47,49 @@
int n ;
} ;
+
+template <int RTYPE, bool NA, typename T>
+class Diag_Maker : public Rcpp::MatrixBase< RTYPE ,NA, Diag_Maker<RTYPE,NA,T> > {
+public:
+ typedef typename Rcpp::VectorBase<RTYPE,NA,T> VEC_TYPE ;
+ typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
+
+ Diag_Maker( const VEC_TYPE& object_ ) : object(object_), n(object_.size()) {}
+
+ inline STORAGE operator[]( int index ) const {
+ int i = Rcpp::internal::get_line( index, n ) ;
+ int j = Rcpp::internal::get_column( index, n, i ) ;
+ return (i==j) ? object[i] : 0 ;
+ }
+ inline STORAGE operator()( int i, int j ) const {
+ return (i==j) ? object[i] : 0 ;
+ }
+ inline int size() const { return n * n; }
+ inline int ncol() const { return n; }
+ inline int nrow() const { return n; }
+
+private:
+ const VEC_TYPE& object ;
+ int n ;
+} ;
+
+template <typename T> struct diag_result_type_trait{
+ typedef typename Rcpp::traits::if_<
+ Rcpp::traits::matrix_interface<T>::value,
+ Diag_Extractor< T::r_type::value , T::can_have_na::value , T >,
+ Diag_Maker< T::r_type::value , T::can_have_na::value , T >
+ >::type type ;
+} ;
+
} // sugar
-template <int RTYPE,bool NA, typename T>
-inline sugar::Matrix_Diag<RTYPE,NA,T> diag(
- const MatrixBase<RTYPE,NA,T>& t
- ){
- return sugar::Matrix_Diag<RTYPE,NA,T>( t ) ;
+template <typename T>
+inline typename sugar::diag_result_type_trait<T>::type
+diag( const T& t ){
+ return typename sugar::diag_result_type_trait<T>::type( t ) ;
}
+
} // Rcpp
#endif
Added: pkg/Rcpp/inst/include/Rcpp/traits/if_.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/traits/if_.h (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/traits/if_.h 2010-07-08 11:46:47 UTC (rev 1834)
@@ -0,0 +1,41 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// if.h: Rcpp R/C++ interface class library -- dispatch
+//
+// 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__traits_if_h
+#define Rcpp__traits_if_h
+
+namespace Rcpp{
+namespace traits{
+
+template <bool COND, typename LHS, typename RHS>
+struct if_ {
+ typedef LHS type ;
+} ;
+
+template <typename LHS, typename RHS>
+struct if_<false,LHS,RHS> {
+ typedef RHS type ;
+} ;
+
+}
+}
+
+#endif
Modified: pkg/Rcpp/inst/include/Rcpp/vector/Matrix.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/vector/Matrix.h 2010-07-08 10:09:53 UTC (rev 1833)
+++ pkg/Rcpp/inst/include/Rcpp/vector/Matrix.h 2010-07-08 11:46:47 UTC (rev 1834)
@@ -25,6 +25,9 @@
template <int RTYPE>
class Matrix : public Vector<RTYPE>, public MatrixBase<RTYPE,true, Matrix<RTYPE> > {
public:
+ struct r_type : traits::integral_constant<int,RTYPE>{} ;
+ struct can_have_na : traits::true_type{} ;
+
typedef Vector<RTYPE> VECTOR ;
typedef typename VECTOR::iterator iterator ;
typedef typename VECTOR::converter_type converter_type ;
Modified: pkg/Rcpp/inst/include/RcppCommon.h
===================================================================
--- pkg/Rcpp/inst/include/RcppCommon.h 2010-07-08 10:09:53 UTC (rev 1833)
+++ pkg/Rcpp/inst/include/RcppCommon.h 2010-07-08 11:46:47 UTC (rev 1834)
@@ -235,6 +235,7 @@
#include <Rcpp/traits/r_type_traits.h>
#include <Rcpp/traits/wrap_type_traits.h>
#include <Rcpp/traits/is_na.h>
+#include <Rcpp/traits/if_.h>
#include <Rcpp/traits/get_na.h>
#include <Rcpp/traits/is_trivial.h>
#include <Rcpp/traits/init_type.h>
Modified: pkg/Rcpp/inst/unitTests/runit.sugar.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.sugar.R 2010-07-08 10:09:53 UTC (rev 1833)
+++ pkg/Rcpp/inst/unitTests/runit.sugar.R 2010-07-08 11:46:47 UTC (rev 1834)
@@ -539,14 +539,17 @@
) ;
'
),
- "runit_matrix_diag" = list(
- signature( x = "matrix" ),
+ "runit_diag" = list(
+ signature( x = "numeric", m = "matrix" ), '
+ NumericVector xx(x) ;
+ NumericMatrix mm(m) ;
+ return List::create(
+ diag( xx ) ,
+ diag( mm ),
+ diag( outer( xx, xx, std::plus<double>() ) )
+ ) ;
'
- NumericMatrix xx(x) ;
- return wrap( diag( xx ) ) ;
- '
)
-
)
signatures <- lapply( sugar.functions, "[[", 1L )
@@ -1047,9 +1050,12 @@
checkEquals( fx(m), list( row = row(m), col = col(m) ) )
}
-test.sugar.matrix.diag <- function( ){
- fx <- .rcpp.sugar$runit_matrix_diag
+test.sugar.diag <- function( ){
+ fx <- .rcpp.sugar$runit_diag
+
+ x <- 1:4
m <- matrix( 1:16, nc = 4 )
- checkEquals( fx(m), diag(m) )
+ checkEquals( fx(x, m),
+ list( diag(x), diag(m), diag( outer( x, x, "+" ) ) ) )
}
More information about the Rcpp-commits
mailing list