22//
33// Matrix.h: Rcpp R/C++ interface class library -- matrices
44//
5- // Copyright (C) 2010 - 2013 Dirk Eddelbuettel and Romain Francois
5+ // Copyright (C) 2010 - 2014 Dirk Eddelbuettel and Romain Francois
66//
77// This file is part of Rcpp.
88//
@@ -45,23 +45,43 @@ class Matrix : public Vector<RTYPE, StoragePolicy>, public MatrixBase<RTYPE, tru
4545
4646 Matrix () : VECTOR() {}
4747
48- Matrix (SEXP x) ;
48+ Matrix (SEXP x) : VECTOR( r_cast<RTYPE>( x ) ), nrows( VECTOR::dims()[ 0 ] ) {}
4949
50- Matrix ( const int & n) ;
51- Matrix ( const Dimension& dims) ;
52- Matrix ( const int & nrows_, const int & ncols) ;
50+ Matrix ( const Dimension& dims) : VECTOR( Rf_allocMatrix( RTYPE, dims[0 ], dims[1 ] ) ), nrows(dims[0 ]) {
51+ if ( dims.size () != 2 ) throw not_compatible (" not a matrix" ) ;
52+ VECTOR::init () ;
53+ }
54+ Matrix ( const int & nrows_, const int & ncols) : VECTOR( Dimension( nrows_, ncols ) ),
55+ nrows (nrows_)
56+ {}
5357
5458 template <typename Iterator>
55- Matrix ( const int & nrows_, const int & ncols, Iterator start ) ;
59+ Matrix ( const int & nrows_, const int & ncols, Iterator start ) :
60+ VECTOR( start, start + (nrows_*ncols) ),
61+ nrows(nrows_)
62+ {
63+ VECTOR::attr ( " dim" ) = Dimension ( nrows, ncols ) ;
64+ }
65+
66+ Matrix ( const int & n) : VECTOR( Dimension( n, n ) ), nrows(n) {}
67+
5668
57- Matrix ( const Matrix& other) ;
69+ Matrix ( const Matrix& other) : VECTOR( other.get__() ), nrows(other.nrows) {}
5870
5971 template <bool NA, typename MAT>
60- Matrix ( const MatrixBase<RTYPE,NA,MAT>& other ) ;
72+ Matrix ( const MatrixBase<RTYPE,NA,MAT>& other ) : VECTOR( Rf_allocMatrix( RTYPE, other.nrow(), other.ncol() ) ), nrows(other.nrow()) {
73+ import_matrix_expression<NA,MAT>( other, nrows, ncol () ) ;
74+ }
6175
6276 Matrix ( const SubMatrix<RTYPE>& ) ;
6377
64- Matrix& operator =(const Matrix& other) ;
78+ Matrix& operator =(const Matrix& other) {
79+ SEXP x = other.get__ () ;
80+ if ( ! ::Rf_isMatrix (x) ) not_compatible (" not a matrix" ) ;
81+ VECTOR::set__ ( x ) ;
82+ nrows = other.nrows ;
83+ return *this ;
84+ }
6585 Matrix& operator =( const SubMatrix<RTYPE>& ) ;
6686
6787 inline int ncol () const {
@@ -85,36 +105,88 @@ class Matrix : public Vector<RTYPE, StoragePolicy>, public MatrixBase<RTYPE, tru
85105 inline iterator begin () { return VECTOR::begin () ; }
86106 inline iterator end () { return VECTOR::end () ; }
87107
88- template <typename U> void fill_diag ( const U& u) ;
108+ template <typename U>
109+ void fill_diag ( const U& u) {
110+ fill_diag__dispatch ( typename traits::is_trivial<RTYPE>::type (), u ) ;
111+ }
89112
90- template <typename U> static Matrix diag ( int size, const U& diag_value ) ;
113+ template <typename U> static Matrix diag ( int size, const U& diag_value ) {
114+ Matrix res (size,size) ;
115+ res.fill_diag ( diag_value ) ;
116+ return res ;
117+ }
91118
92- inline Proxy operator []( int i ) ;
93- inline const_Proxy operator []( int i ) const ;
119+ inline Proxy operator []( int i ) {
120+ return static_cast < Vector<RTYPE>* >( this )->operator []( i ) ;
121+ }
122+ inline const_Proxy operator []( int i ) const {
123+ return static_cast < const Vector<RTYPE>* >( this )->operator []( i ) ;
124+ }
94125
95- inline Proxy operator ()( const size_t & i, const size_t & j) ;
96- inline const_Proxy operator ()( const size_t & i, const size_t & j) const ;
126+ inline Proxy operator ()( const size_t & i, const size_t & j) {
127+ return static_cast < Vector<RTYPE>* >( this )->operator []( offset ( i, j ) ) ;
128+ }
129+ inline const_Proxy operator ()( const size_t & i, const size_t & j) const {
130+ return static_cast < const Vector<RTYPE>* >( this )->operator []( offset ( i, j ) ) ;
131+ }
97132
98- inline Row operator ()( int i, internal::NamedPlaceHolder ) ;
99- inline Column operator ()( internal::NamedPlaceHolder, int i ) ;
100- inline Column operator ()( internal::NamedPlaceHolder, int i ) const ;
101- inline Sub operator ()( const Range& row_range, const Range& col_range) ;
102- inline Sub operator ()( internal::NamedPlaceHolder, const Range& col_range) ;
103- inline Sub operator ()( const Range& row_range, internal::NamedPlaceHolder ) ;
133+ inline Row operator ()( int i, internal::NamedPlaceHolder ) {
134+ return Row ( *this , i ) ;
135+ }
136+ inline Column operator ()( internal::NamedPlaceHolder, int i ) {
137+ return Column ( *this , i ) ;
138+ }
139+ inline Column operator ()( internal::NamedPlaceHolder, int i ) const {
140+ return Column ( *this , i ) ;
141+ }
142+ inline Sub operator ()( const Range& row_range, const Range& col_range) {
143+ return Sub ( const_cast <Matrix&>(*this ), row_range, col_range ) ;
144+ }
145+ inline Sub operator ()( internal::NamedPlaceHolder, const Range& col_range) {
146+ return Sub ( const_cast <Matrix&>(*this ), Range (0 ,nrow ()-1 ) , col_range ) ;
147+ }
148+ inline Sub operator ()( const Range& row_range, internal::NamedPlaceHolder ) {
149+ return Sub ( const_cast <Matrix&>(*this ), row_range, Range (0 ,ncol ()-1 ) ) ;
150+ }
104151
105152
106153private:
107154
108155 inline int offset ( int i, int j) const { return i + nrows * j ; }
109156
110157 template <typename U>
111- void fill_diag__dispatch ( traits::false_type, const U& u) ;
158+ void fill_diag__dispatch ( traits::false_type, const U& u) {
159+ Shield<SEXP> elem ( converter_type::get ( u ) ) ;
160+ int n = Matrix::ncol () ;
161+ int offset = n +1 ;
162+ iterator it ( VECTOR::begin ()) ;
163+ for ( int i=0 ; i<n; i++){
164+ *it = ::Rf_duplicate ( elem );
165+ it += offset;
166+ }
167+ }
112168
113169 template <typename U>
114- void fill_diag__dispatch ( traits::true_type, const U& u) ;
170+ void fill_diag__dispatch ( traits::true_type, const U& u) {
171+ stored_type elem = converter_type::get ( u ) ;
172+ int n = Matrix::ncol () ;
173+ int offset = n + 1 ;
174+ iterator it ( VECTOR::begin ()) ;
175+ for ( int i=0 ; i<n; i++){
176+ *it = elem ;
177+ it += offset;
178+ }
179+ }
115180
116181 template <bool NA, typename MAT>
117- void import_matrix_expression ( const MatrixBase<RTYPE,NA,MAT>& other, int nr, int nc ) ;
182+ void import_matrix_expression ( const MatrixBase<RTYPE,NA,MAT>& other, int nr, int nc ) {
183+ iterator start = VECTOR::begin () ;
184+ for ( int j=0 ; j<nc; j++){
185+ for ( int i=0 ; i<nr; i++, ++start){
186+ *start = other (i,j) ;
187+ }
188+ }
189+ }
118190
119191} ;
120192
0 commit comments