2
2
//
3
3
// Matrix.h: Rcpp R/C++ interface class library -- matrices
4
4
//
5
- // Copyright (C) 2010 - 2013 Dirk Eddelbuettel and Romain Francois
5
+ // Copyright (C) 2010 - 2014 Dirk Eddelbuettel and Romain Francois
6
6
//
7
7
// This file is part of Rcpp.
8
8
//
@@ -45,23 +45,43 @@ class Matrix : public Vector<RTYPE, StoragePolicy>, public MatrixBase<RTYPE, tru
45
45
46
46
Matrix () : VECTOR() {}
47
47
48
- Matrix (SEXP x) ;
48
+ Matrix (SEXP x) : VECTOR( r_cast<RTYPE>( x ) ), nrows( VECTOR::dims()[ 0 ] ) {}
49
49
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
+ {}
53
57
54
58
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
+
56
68
57
- Matrix ( const Matrix& other) ;
69
+ Matrix ( const Matrix& other) : VECTOR( other.get__() ), nrows(other.nrows) {}
58
70
59
71
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
+ }
61
75
62
76
Matrix ( const SubMatrix<RTYPE>& ) ;
63
77
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
+ }
65
85
Matrix& operator =( const SubMatrix<RTYPE>& ) ;
66
86
67
87
inline int ncol () const {
@@ -85,36 +105,88 @@ class Matrix : public Vector<RTYPE, StoragePolicy>, public MatrixBase<RTYPE, tru
85
105
inline iterator begin () { return VECTOR::begin () ; }
86
106
inline iterator end () { return VECTOR::end () ; }
87
107
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
+ }
89
112
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
+ }
91
118
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
+ }
94
125
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
+ }
97
132
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
+ }
104
151
105
152
106
153
private:
107
154
108
155
inline int offset ( int i, int j) const { return i + nrows * j ; }
109
156
110
157
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
+ }
112
168
113
169
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
+ }
115
180
116
181
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
+ }
118
190
119
191
} ;
120
192
0 commit comments