Rcpp Version 1.0.14
Loading...
Searching...
No Matches
SubMatrix.h
Go to the documentation of this file.
1
2// SubMatrix.h: Rcpp R/C++ interface class library -- sub matrices
3//
4// Copyright (C) 2010 - 2013 Dirk Eddelbuettel and Romain Francois
5//
6// This file is part of Rcpp.
7//
8// Rcpp is free software: you can redistribute it and/or modify it
9// under the terms of the GNU General Public License as published by
10// the Free Software Foundation, either version 2 of the License, or
11// (at your option) any later version.
12//
13// Rcpp is distributed in the hope that it will be useful, but
14// WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU General Public License for more details.
17//
18// You should have received a copy of the GNU General Public License
19// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
20
21#ifndef Rcpp__vector__SubMatrix_h
22#define Rcpp__vector__SubMatrix_h
23
24namespace Rcpp{
25
26template <int RTYPE>
27class SubMatrix : public Rcpp::MatrixBase< RTYPE, true, SubMatrix<RTYPE> > {
28public:
31 typedef typename MATRIX::Proxy Proxy ;
32
34 m(m_),
35 iter( static_cast< Vector<RTYPE>& >(m_).begin() + row_range_.get_start() + col_range_.get_start() * m_.nrow() ),
36 m_nr( m.nrow() ),
37 nc( col_range_.size() ),
38 nr( row_range_.size() )
39 {}
40
41 inline R_xlen_t size() const { return ((R_xlen_t)ncol()) * nrow() ; }
42 inline R_xlen_t ncol() const { return nc ; }
43 inline R_xlen_t nrow() const { return nr ; }
44
45 inline Proxy operator()(int i, int j) const {
46 return iter[ i + j*m_nr ] ;
47 }
48
49 inline vec_iterator column_iterator( int j ) const { return iter + j*m_nr ; }
50
51private:
55} ;
56
57template <int RTYPE, template <class> class StoragePolicy >
58Matrix<RTYPE,StoragePolicy>::Matrix( const SubMatrix<RTYPE>& sub ) : VECTOR( Rf_allocMatrix( RTYPE, (int)sub.nrow(), (int)sub.ncol() )), nrows((int)sub.nrow()) {
59 R_xlen_t nc = sub.ncol() ;
60 iterator start = VECTOR::begin() ;
62 for( R_xlen_t j=0; j<nc; j++){
63 rhs_it = sub.column_iterator((int)j) ;
64 for( int i=0; i<nrows; i++, ++start){
65 *start = rhs_it[i] ;
66 }
67 }
68}
69
70template <int RTYPE, template <class> class StoragePolicy >
72 int nc = sub.ncol(), nr = sub.nrow() ;
73 if( nc != nrow() || nr != ncol() ){
74 nrows = nr ;
75 VECTOR::set__( Rf_allocMatrix( RTYPE, nr, nc ) ) ;
76 }
77 iterator start = VECTOR::begin() ;
79 for( int j=0; j<nc; j++){
80 rhs_it = sub.column_iterator(j) ;
81 for( int i=0; i<nrows; i++, ++start){
82 *start = rhs_it[i] ;
83 }
84 }
85 return *this ;
86}
87
88#undef RCPP_WRAP_SUBMATRIX
89#define RCPP_WRAP_SUBMATRIX(RTYPE) \
90template<> inline SEXP wrap< SubMatrix<RTYPE> >( \
91 const SubMatrix<RTYPE>& object \
92 ) { \
93 return Matrix<RTYPE>( object ) ; \
94 }
99// RCPP_WRAP_SUBMATRIX(STRSXP)
100// RCPP_WRAP_SUBMATRIX(VECSXP)
101// RCPP_WRAP_SUBMATRIX(EXPRSXP)
102#undef RCPP_WRAP_SUBMATRIX
103
104}
105
106#endif
#define RCPP_WRAP_SUBMATRIX(RTYPE)
Definition SubMatrix.h:89
int ncol() const
Definition Matrix.h:94
VECTOR::Proxy Proxy
Definition Matrix.h:48
Matrix & operator=(const Matrix &other)
Definition Matrix.h:83
int nrows
Definition Matrix.h:29
VECTOR::iterator iterator
Definition Matrix.h:44
R_xlen_t nc
Definition SubMatrix.h:54
Vector< RTYPE >::iterator vec_iterator
Definition SubMatrix.h:30
R_xlen_t m_nr
Definition SubMatrix.h:54
Matrix< RTYPE > MATRIX
Definition SubMatrix.h:29
R_xlen_t size() const
Definition SubMatrix.h:41
R_xlen_t nrow() const
Definition SubMatrix.h:43
Proxy operator()(int i, int j) const
Definition SubMatrix.h:45
vec_iterator iter
Definition SubMatrix.h:53
R_xlen_t nr
Definition SubMatrix.h:54
vec_iterator column_iterator(int j) const
Definition SubMatrix.h:49
MATRIX::Proxy Proxy
Definition SubMatrix.h:31
R_xlen_t ncol() const
Definition SubMatrix.h:42
SubMatrix(MATRIX &m_, const Range &row_range_, const Range &col_range_)
Definition SubMatrix.h:33
iterator begin()
Definition Vector.h:333
traits::r_vector_iterator< RTYPE, StoragePolicy >::type iterator
Definition Vector.h:45
Rcpp API.
Definition algo.h:28
T as(SEXP x)
Definition as.h:151