| 26 | |
| 27 | template <int RTYPE, template <class> class StoragePolicy = PreserveStorage > |
| 28 | class Matrix : public Vector<RTYPE, StoragePolicy>, public MatrixBase<RTYPE, true, Matrix<RTYPE,StoragePolicy> > { |
| 29 | int nrows ; |
| 30 | |
| 31 | public: |
| 32 | using Vector<RTYPE, StoragePolicy>::size; // disambiguate diamond pattern for g++-6 and later |
| 33 | |
| 34 | struct r_type : traits::integral_constant<int,RTYPE>{} ; |
| 35 | struct can_have_na : traits::true_type{} ; |
| 36 | typedef MatrixRow<RTYPE> Row ; |
| 37 | typedef ConstMatrixRow<RTYPE> ConstRow ; |
| 38 | typedef MatrixColumn<RTYPE> Column ; |
| 39 | typedef ConstMatrixColumn<RTYPE> ConstColumn ; |
| 40 | typedef SubMatrix<RTYPE> Sub ; |
| 41 | |
| 42 | typedef StoragePolicy<Matrix> Storage ; |
| 43 | typedef Vector<RTYPE, StoragePolicy> VECTOR ; |
| 44 | typedef typename VECTOR::iterator iterator ; |
| 45 | typedef typename VECTOR::const_iterator const_iterator ; |
| 46 | typedef typename VECTOR::converter_type converter_type ; |
| 47 | typedef typename VECTOR::stored_type stored_type ; |
| 48 | typedef typename VECTOR::Proxy Proxy ; |
| 49 | typedef typename VECTOR::const_Proxy const_Proxy ; |
| 50 | |
| 51 | Matrix() : VECTOR(Dimension(0, 0)), nrows(0) {} |
| 52 | |
| 53 | Matrix(SEXP x) : VECTOR(x), nrows( VECTOR::dims()[0] ) {} |
| 54 | |
| 55 | Matrix( const Dimension& dims) : VECTOR( Rf_allocMatrix( RTYPE, dims[0], dims[1] ) ), nrows(dims[0]) { |
| 56 | if( dims.size() != 2 ) throw not_a_matrix(); |
| 57 | VECTOR::init() ; |
| 58 | } |
| 59 | Matrix( const int& nrows_, const int& ncols) : VECTOR( Dimension( nrows_, ncols ) ), |
| 60 | nrows(nrows_) |
| 61 | {} |
| 62 | |
| 63 | template <typename Iterator> |
| 64 | Matrix( const int& nrows_, const int& ncols, Iterator start ) : |
| 65 | VECTOR( start, start + (static_cast<R_xlen_t>(nrows_)*ncols) ), |
| 66 | nrows(nrows_) |
| 67 | { |
| 68 | VECTOR::attr( "dim" ) = Dimension( nrows, ncols ) ; |
| 69 | } |
| 70 | |
| 71 | Matrix( const int& n) : VECTOR( Dimension( n, n ) ), nrows(n) {} |
| 72 | |
| 73 | |
| 74 | Matrix( const Matrix& other) : VECTOR( other.get__() ), nrows(other.nrows) {} |
| 75 | |
| 76 | template <bool NA, typename MAT> |
| 77 | Matrix( const MatrixBase<RTYPE,NA,MAT>& other ) : VECTOR( Rf_allocMatrix( RTYPE, other.nrow(), other.ncol() ) ), nrows(other.nrow()) { |
| 78 | import_matrix_expression<NA,MAT>( other, nrows, ncol() ) ; |
| 79 | } |
| 80 | |
| 81 | Matrix( const SubMatrix<RTYPE>& ) ; |
| 82 | |
| 83 | Matrix& operator=(const Matrix& other) { |
| 84 | SEXP x = other.get__() ; |
| 85 | if( ! ::Rf_isMatrix(x) ) throw not_a_matrix(); |