@@ -28,6 +28,7 @@ namespace Rcpp{
2828template <int RTYPE , template <class > class StoragePolicy = PreserveStorage >
2929class Matrix : public Vector <RTYPE , StoragePolicy>, public MatrixBase<RTYPE , true , Matrix<RTYPE ,StoragePolicy> > {
3030 int nrows ;
31+ int ncols ;
3132
3233public:
3334 using Vector<RTYPE , StoragePolicy>::size; // disambiguate diamond pattern for g++-6 and later
@@ -49,33 +50,33 @@ class Matrix : public Vector<RTYPE, StoragePolicy>, public MatrixBase<RTYPE, tru
4950 typedef typename VECTOR ::Proxy Proxy ;
5051 typedef typename VECTOR ::const_Proxy const_Proxy ;
5152
52- Matrix () : VECTOR (Dimension(0 , 0 )), nrows(0 ) {}
53+ Matrix () : VECTOR (Dimension(0 , 0 )), nrows(0 ), ncols( 0 ) {}
5354
54- Matrix (SEXP x) : VECTOR (x), nrows( VECTOR ::dims()[0 ] ) {}
55+ Matrix (SEXP x) : VECTOR (x), nrows( VECTOR ::dims()[0 ] ), ncols( VECTOR ::dims()[ 1 ] ) {}
5556
56- Matrix ( const Dimension& dims) : VECTOR ( Rf_allocMatrix( RTYPE , dims[0 ], dims[1 ] ) ), nrows(dims[0 ]) {
57+ Matrix ( const Dimension& dims) : VECTOR ( Rf_allocMatrix( RTYPE , dims[0 ], dims[1 ] ) ), nrows(dims[0 ]), ncols(dims[ 1 ]) {
5758 if ( dims.size () != 2 ) throw not_a_matrix ();
5859 VECTOR::init () ;
5960 }
60- Matrix ( const int & nrows_, const int & ncols ) : VECTOR ( Dimension( nrows_, ncols ) ),
61- nrows (nrows_)
61+ Matrix ( const int & nrows_, const int & ncols_ ) : VECTOR ( Dimension( nrows_, ncols_ ) ),
62+ nrows (nrows_), ncols(ncols_)
6263 {}
6364
6465 template <typename Iterator>
65- Matrix ( const int & nrows_, const int & ncols , Iterator start ) :
66- VECTOR( start, start + (static_cast <R_xlen_t>(nrows_)*ncols ) ),
67- nrows(nrows_)
66+ Matrix ( const int & nrows_, const int & ncols_ , Iterator start ) :
67+ VECTOR( start, start + (static_cast <R_xlen_t>(nrows_)*ncols_ ) ),
68+ nrows(nrows_), ncols(ncols_)
6869 {
6970 VECTOR::attr ( " dim" ) = Dimension ( nrows, ncols ) ;
7071 }
7172
72- Matrix ( const int & n) : VECTOR( Dimension( n, n ) ), nrows(n) {}
73+ Matrix ( const int & n) : VECTOR( Dimension( n, n ) ), nrows(n), ncols(n) {}
7374
7475
75- Matrix ( const Matrix& other) : VECTOR( other.get__() ), nrows(other.nrows) {}
76+ Matrix ( const Matrix& other) : VECTOR( other.get__() ), nrows(other.nrows), ncols(other.ncols) {}
7677
7778 template <bool NA , typename MAT >
78- Matrix ( const MatrixBase<RTYPE ,NA ,MAT >& other ) : VECTOR( Rf_allocMatrix( RTYPE , static_cast <int >(other.nrow()), static_cast<int>(other.ncol()) ) ), nrows(static_cast <int >(other.nrow())) {
79+ Matrix ( const MatrixBase<RTYPE ,NA ,MAT >& other ) : VECTOR( Rf_allocMatrix( RTYPE , static_cast <int >(other.nrow()), static_cast<int>(other.ncol()) ) ), nrows(static_cast <int >(other.nrow())), ncols( static_cast < int >(other.ncol())) {
7980 import_matrix_expression<NA ,MAT >( other, nrows, ncol () ) ;
8081 }
8182
@@ -86,20 +87,21 @@ class Matrix : public Vector<RTYPE, StoragePolicy>, public MatrixBase<RTYPE, tru
8687 if ( ! ::Rf_isMatrix (x) ) throw not_a_matrix ();
8788 VECTOR::set__ ( x ) ;
8889 nrows = other.nrows ;
90+ ncols = other.ncols ;
8991 return *this ;
9092 }
9193 Matrix& operator =( const SubMatrix<RTYPE >& ) ;
9294
93- explicit Matrix ( const no_init_matrix& obj) : VECTOR(Rf_allocMatrix(RTYPE , obj.nrow(), obj.ncol())), nrows(obj.nrow()) {}
95+ explicit Matrix ( const no_init_matrix& obj) : VECTOR(Rf_allocMatrix(RTYPE , obj.nrow(), obj.ncol())), nrows(obj.nrow()), ncols(obj.ncol()) {}
9496
9597 inline int ncol () const {
96- return VECTOR::dims ()[ 1 ] ;
98+ return ncols ;
9799 }
98100 inline int nrow () const {
99101 return nrows ;
100102 }
101103 inline int cols () const {
102- return VECTOR::dims ()[ 1 ] ;
104+ return ncols ;
103105 }
104106 inline int rows () const {
105107 return nrows ;
0 commit comments