22 #ifndef Rcpp__vector__Matrix_h
23 #define Rcpp__vector__Matrix_h
27 template <
int RTYPE,
template <
class>
class StoragePolicy = PreserveStorage >
56 if(
dims.size() != 2 )
throw not_a_matrix();
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) ),
76 template <
bool NA,
typename MAT>
78 import_matrix_expression<NA,MAT>( other,
nrows,
ncol() ) ;
84 SEXP x = other.
get__() ;
85 if( ! ::Rf_isMatrix(x) )
throw not_a_matrix();
119 template <
typename U>
131 return static_cast< Vector<RTYPE>*
>( this )->
operator[]( i ) ;
134 return static_cast< const Vector<RTYPE>*
>( this )->
operator[]( i ) ;
144 inline Proxy at(
const size_t& i,
const size_t& j) {
145 return static_cast< Vector<RTYPE>*
>( this )->
operator()( i, j ) ;
148 return static_cast< const Vector<RTYPE>*
>( this )->
operator()( i, j ) ;
152 return Row( *
this, i ) ;
158 return Column( *
this, i ) ;
164 return Sub(
const_cast<Matrix&
>(*
this), row_range, col_range ) ;
174 inline R_xlen_t
offset(
const int i,
const int j)
const {
return i +
static_cast<R_xlen_t
>(
nrows) * j ; }
176 template <
typename U>
181 for (R_xlen_t i = 0; i < bounds; ++i) {
182 (*this)(i, i) = elem;
186 template <
typename U>
192 for (R_xlen_t i = 0; i < bounds; ++i) {
193 (*this)(i, i) = elem;
197 template <
bool NA,
typename MAT>
200 for(
int j=0; j<nc; j++){
201 for(
int i=0; i<nr; i++, ++start){
202 *start = other(i,j) ;
217 template<
template <
class>
class StoragePolicy >
221 std::ios::fmtflags flags = s.flags();
222 s.unsetf(std::ios::floatfield);
223 std::streamsize precision = s.precision();
225 const int rows = rhs.
rows();
227 for (
int i = 0; i < rows; ++i) {
228 typename MATRIX::Row
row =
const_cast<MATRIX &
>(rhs).
row(i);
230 typename MATRIX::Row::iterator j =
row.begin();
231 typename MATRIX::Row::iterator jend =
row.end();
234 s << std::showpoint << std::setw(precision + 1) << (*j);
237 for ( ; j != jend; j++) {
238 s <<
" " << std::showpoint << std::setw(precision + 1) << (*j);
249 #ifndef RCPP_NO_SUGAR
250 #define RCPP_GENERATE_MATRIX_SCALAR_OPERATOR(__OPERATOR__) \
251 template <int RTYPE, template <class> class StoragePolicy, typename T > \
252 inline typename traits::enable_if< traits::is_convertible< typename traits::remove_const_and_reference< T >::type, \
253 typename Matrix<RTYPE, StoragePolicy>::stored_type >::value, Matrix<RTYPE, StoragePolicy> >::type \
254 operator __OPERATOR__ (const Matrix<RTYPE, StoragePolicy> &lhs, const T &rhs) { \
255 Vector<RTYPE, StoragePolicy> v = static_cast<const Vector<RTYPE, StoragePolicy> &>(lhs) __OPERATOR__ rhs; \
256 v.attr("dim") = Vector<INTSXP>::create(lhs.nrow(), lhs.ncol()); \
257 return as< Matrix<RTYPE, StoragePolicy> >(v); \
265 #undef RCPP_GENERATE_MATRIX_SCALAR_OPERATOR
267 #define RCPP_GENERATE_SCALAR_MATRIX_OPERATOR(__OPERATOR__) \
268 template <int RTYPE, template <class> class StoragePolicy, typename T > \
269 inline typename traits::enable_if< traits::is_convertible< typename traits::remove_const_and_reference< T >::type, \
270 typename Matrix<RTYPE, StoragePolicy>::stored_type >::value, Matrix<RTYPE, StoragePolicy> >::type \
271 operator __OPERATOR__ (const T &lhs, const Matrix<RTYPE, StoragePolicy> &rhs) { \
272 Vector<RTYPE, StoragePolicy> v = lhs __OPERATOR__ static_cast<const Vector<RTYPE, StoragePolicy> &>(rhs); \
273 v.attr("dim") = Vector<INTSXP>::create(rhs.nrow(), rhs.ncol()); \
274 return as< Matrix<RTYPE, StoragePolicy> >(v); \
282 #undef RCPP_GENERATE_SCALAR_MATRIX_OPERATOR
285 template<
template <
class>
class StoragePolicy >
290 std::ios::fmtflags flags = s.flags();
297 typename VECTOR::iterator j =
static_cast<VECTOR &
>(
const_cast<MATRIX &
>(rhs)).begin();
298 typename VECTOR::iterator jend =
static_cast<VECTOR &
>(
const_cast<MATRIX &
>(rhs)).end();
300 for ( ; j != jend; ++j) {
310 int digitsMax = (
max >= 0) ? 0 : 1;
311 int digitsMin = (
min >= 0) ? 0 : 1;
325 int digits =
std::max(digitsMin, digitsMax);
327 const int rows = rhs.
rows();
329 for (
int i = 0; i < rows; ++i) {
330 typename MATRIX::Row
row =
const_cast<MATRIX &
>(rhs).
row(i);
332 typename MATRIX::Row::iterator j =
row.begin();
333 typename MATRIX::Row::iterator jend =
row.end();
336 s << std::setw(digits) << (*j);
339 for ( ; j != jend; ++j) {
340 s <<
" " << std::setw(digits) << (*j);
351 template<
template <
class>
class StoragePolicy >
355 const int rows = rhs.
rows();
357 for (
int i = 0; i < rows; ++i) {
358 typename MATRIX::Row
row =
const_cast<MATRIX &
>(rhs).
row(i);
360 typename MATRIX::Row::iterator j =
row.begin();
361 typename MATRIX::Row::iterator jend =
row.end();
364 s <<
"\"" << (*j) <<
"\"";
367 for ( ; j != jend; j++) {
368 s <<
" \"" << (*j) <<
"\"";
378 template<
int RTYPE,
template <
class>
class StoragePolicy >
382 const int rows = rhs.
rows();
384 for (
int i = 0; i < rows; ++i) {
385 typename MATRIX::Row
row =
const_cast<MATRIX &
>(rhs).
row(i);
387 typename MATRIX::Row::iterator j =
row.begin();
388 typename MATRIX::Row::iterator jend =
row.end();
394 for ( ; j != jend; j++) {
405 template<
int RTYPE,
template <
class>
class StoragePolicy >
411 int nrow = dims[0], ncol = dims[1];
413 R_xlen_t len = XLENGTH(x), len2 = XLENGTH(x)-1;
416 VECTOR s = VECTOR(r.get__());
417 for (R_xlen_t i = 0, j = 0; i < len; i++, j += nrow) {
418 if (j > len2) j -= len2;
423 SEXP dimNames = Rf_getAttrib(x, R_DimNamesSymbol);
424 if (!Rf_isNull(dimNames)) {
427 SET_VECTOR_ELT(newDimNames, 0, VECTOR_ELT(dimNames, 1));
428 SET_VECTOR_ELT(newDimNames, 1, VECTOR_ELT(dimNames, 0));
429 Rf_setAttrib(r, R_DimNamesSymbol, newDimNames);
434 template<
template <
class>
class StoragePolicy>
436 return tranpose_impl<REALSXP, StoragePolicy>(x);
439 template<
template <
class>
class StoragePolicy>
441 return tranpose_impl<INTSXP, StoragePolicy>(x);
444 template<
template <
class>
class StoragePolicy>
446 return tranpose_impl<STRSXP, StoragePolicy>(x);
#define RCPP_GENERATE_MATRIX_SCALAR_OPERATOR(__OPERATOR__)
#define RCPP_GENERATE_SCALAR_MATRIX_OPERATOR(__OPERATOR__)
AttributeProxy attr(const std::string &name)
Sub operator()(const Range &row_range, internal::NamedPlaceHolder)
Proxy operator()(const size_t &i, const size_t &j)
ConstRow operator()(int i, internal::NamedPlaceHolder) const
ConstMatrixRow< RTYPE > ConstRow
ConstColumn operator()(internal::NamedPlaceHolder, int i) const
static Matrix diag(int size, const U &diag_value)
VECTOR::const_iterator const_iterator
ConstColumn column(int i) const
void import_matrix_expression(const MatrixBase< RTYPE, NA, MAT > &other, int nr, int nc)
Column operator()(internal::NamedPlaceHolder, int i)
ConstMatrixColumn< RTYPE > ConstColumn
Vector< RTYPE, StoragePolicy > VECTOR
const_Proxy at(const size_t &i, const size_t &j) const
MatrixColumn< RTYPE > Column
const_iterator begin() const
Row operator()(int i, internal::NamedPlaceHolder)
const_iterator cend() const
Proxy at(const size_t &i, const size_t &j)
void fill_diag__dispatch(traits::false_type, const U &u)
const_Proxy operator()(const size_t &i, const size_t &j) const
Matrix(const Dimension &dims)
const_Proxy operator[](R_xlen_t i) const
Matrix(const int &nrows_, const int &ncols, Iterator start)
void fill_diag__dispatch(traits::true_type, const U &u)
Sub operator()(internal::NamedPlaceHolder, const Range &col_range)
Sub operator()(const Range &row_range, const Range &col_range)
Matrix(const no_init_matrix &obj)
StoragePolicy< Matrix > Storage
const_iterator cbegin() const
Matrix & operator=(const Matrix &other)
VECTOR::converter_type converter_type
Matrix(const Matrix &other)
VECTOR::stored_type stored_type
Matrix(const MatrixBase< RTYPE, NA, MAT > &other)
ConstRow row(int i) const
VECTOR::const_Proxy const_Proxy
Matrix(const int &nrows_, const int &ncols)
void fill_diag(const U &u)
const_iterator end() const
R_xlen_t offset(const int i, const int j) const
VECTOR::iterator iterator
Proxy operator[](R_xlen_t i)
traits::storage_type< RTYPE >::type stored_type
traits::r_vector_proxy< RTYPE, StoragePolicy >::type Proxy
traits::r_vector_const_proxy< RTYPE, StoragePolicy >::type const_Proxy
traits::r_vector_iterator< RTYPE, StoragePolicy >::type iterator
traits::r_vector_const_iterator< RTYPE, StoragePolicy >::type const_iterator
static target get(const T &input)
traits::enable_if< helpers::decays_to_ctype< typename std::iterator_traits< InputIterator >::value_type >::value, typename helpers::ctype< typename std::iterator_traits< InputIterator >::value_type >::type >::type min(InputIterator begin, InputIterator end)
traits::enable_if< helpers::decays_to_ctype< typename std::iterator_traits< InputIterator >::value_type >::value, typename helpers::ctype< typename std::iterator_traits< InputIterator >::value_type >::type >::type max(InputIterator begin, InputIterator end)
Matrix< RTYPE, StoragePolicy > tranpose_impl(const Matrix< RTYPE, StoragePolicy > &x)
sugar::Row< RTYPE, LHS_NA, LHS_T > row(const Rcpp::MatrixBase< RTYPE, LHS_NA, LHS_T > &lhs)
internal::DimNameProxy rownames(SEXP x)
sugar::Max< RTYPE, NA, T > max(const VectorBase< RTYPE, NA, T > &x)
internal::DimNameProxy colnames(SEXP x)
std::ostream & operator<<(std::ostream &os, const Date d)
Matrix< REALSXP, StoragePolicy > transpose(const Matrix< REALSXP, StoragePolicy > &x)
sugar::Min< RTYPE, NA, T > min(const VectorBase< RTYPE, NA, T > &x)