23#ifndef Rcpp__sugar__rowSums_h
24#define Rcpp__sugar__rowSums_h
31inline void incr(
double* lhs,
double rhs) {
35inline void incr(
int* lhs,
int rhs) {
39inline void incr(Rcomplex* lhs,
const Rcomplex& rhs) {
45inline void div(
double* lhs, R_xlen_t rhs) {
46 *lhs /=
static_cast<double>(rhs);
49inline void div(Rcomplex* lhs, R_xlen_t rhs) {
50 lhs->r /=
static_cast<double>(rhs);
51 lhs->i /=
static_cast<double>(rhs);
107template <
int RTYPE,
bool NA,
typename T,
bool NA_RM = false>
109 public Lazy<typename detail::RowSumsReturn<RTYPE>::type, RowSumsImpl<RTYPE, NA, T, NA_RM> > {
123 R_xlen_t i, j, nr =
ref.nrow(), nc =
ref.ncol();
126 for (j = 0; j < nc; j++) {
127 for (i = 0; i < nr; i++) {
149#define ROW_SUMS_IMPL_KEEPNA(__RTYPE__) \
151template <bool NA, typename T, bool NA_RM> \
152class RowSumsImpl<__RTYPE__, NA, T, NA_RM> : \
153 public Lazy<typename detail::RowSumsReturn<__RTYPE__>::type, RowSumsImpl<__RTYPE__, NA, T, NA_RM> > { \
155 const MatrixBase<__RTYPE__, NA, T>& ref; \
157 typedef detail::RowSumsReturn<__RTYPE__> return_traits; \
158 typedef typename return_traits::type return_vector; \
159 typedef typename traits::storage_type<return_traits::rtype>::type stored_type; \
162 RowSumsImpl(const MatrixBase<__RTYPE__, NA, T>& ref_) \
166 return_vector get() const { \
167 R_xlen_t i, j, nr = ref.nrow(), nc = ref.ncol(); \
168 return_vector res(nr); \
170 for (j = 0; j < nc; j++) { \
171 for (i = 0; i < nr; i++) { \
172 if (traits::is_na<__RTYPE__>(res[i])) \
174 if (traits::is_na<__RTYPE__>(ref(i, j))) \
175 res[i] = traits::get_na<__RTYPE__>(); \
176 else detail::incr(&res[i], ref(i, j)); \
187#undef ROW_SUMS_IMPL_KEEPNA
194template <
int RTYPE,
bool NA,
typename T>
196 public Lazy<typename detail::RowSumsReturn<RTYPE>::type, RowSumsImpl<RTYPE, NA, T, true> > {
210 R_xlen_t i, j, nr =
ref.nrow(), nc =
ref.ncol();
215 for (j = 0; j < nc; j++) {
216 for (i = 0; i < nr; i++) {
233#define ROW_SUMS_IMPL_RMNA(__RTYPE__) \
235template <bool NA, typename T> \
236class RowSumsImpl<__RTYPE__, NA, T, true> : \
237 public Lazy<typename detail::RowSumsReturn<__RTYPE__>::type, RowSumsImpl<__RTYPE__, NA, T, true> > { \
239 const MatrixBase<__RTYPE__, NA, T>& ref; \
241 typedef detail::RowSumsReturn<__RTYPE__> return_traits; \
242 typedef typename return_traits::type return_vector; \
243 typedef typename traits::storage_type<return_traits::rtype>::type stored_type; \
246 RowSumsImpl(const MatrixBase<__RTYPE__, NA, T>& ref_) \
250 return_vector get() const { \
251 R_xlen_t i, j, nr = ref.nrow(), nc = ref.ncol(); \
252 return_vector res(nr); \
254 stored_type current = stored_type(); \
256 for (j = 0; j < nc; j++) { \
257 for (i = 0; i < nr; i++) { \
258 current = ref(i, j); \
259 if (!traits::is_na<__RTYPE__>(current)) { \
260 detail::incr(&res[i], current); \
272#undef ROW_SUMS_IMPL_RMNA
278template <
int RTYPE,
typename T,
bool NA_RM>
288template <
int RTYPE,
bool NA,
typename T,
bool NA_RM = false>
290 public Lazy<typename detail::ColSumsReturn<RTYPE>::type, ColSumsImpl<RTYPE, NA, T, NA_RM> > {
304 R_xlen_t i, j, nr =
ref.nrow(), nc =
ref.ncol();
307 for (j = 0; j < nc; j++) {
308 for (i = 0; i < nr; i++) {
322#define COL_SUMS_IMPL_KEEPNA(__RTYPE__) \
324template <bool NA, typename T, bool NA_RM> \
325class ColSumsImpl<__RTYPE__, NA, T, NA_RM> : \
326 public Lazy<typename detail::ColSumsReturn<__RTYPE__>::type, ColSumsImpl<__RTYPE__, NA, T, NA_RM> > { \
328 const MatrixBase<__RTYPE__, NA, T>& ref; \
330 typedef detail::ColSumsReturn<__RTYPE__> return_traits; \
331 typedef typename return_traits::type return_vector; \
332 typedef typename traits::storage_type<return_traits::rtype>::type stored_type; \
335 ColSumsImpl(const MatrixBase<__RTYPE__, NA, T>& ref_) \
339 return_vector get() const { \
340 R_xlen_t i, j, nr = ref.nrow(), nc = ref.ncol(); \
341 return_vector res(nc); \
343 for (j = 0; j < nc; j++) { \
344 for (i = 0; i < nr; i++) { \
345 if (traits::is_na<__RTYPE__>(res[j])) \
347 if (traits::is_na<__RTYPE__>(ref(i, j))) \
348 res[j] = traits::get_na<__RTYPE__>(); \
349 else detail::incr(&res[j], ref(i, j)); \
360#undef COL_SUMS_IMPL_KEEPNA
367template <
int RTYPE,
bool NA,
typename T>
369 public Lazy<typename detail::ColSumsReturn<RTYPE>::type, ColSumsImpl<RTYPE, NA, T, true> > {
383 R_xlen_t i, j, nr =
ref.nrow(), nc =
ref.ncol();
388 for (j = 0; j < nc; j++) {
389 for (i = 0; i < nr; i++) {
406#define COL_SUMS_IMPL_RMNA(__RTYPE__) \
408template <bool NA, typename T> \
409class ColSumsImpl<__RTYPE__, NA, T, true> : \
410 public Lazy<typename detail::ColSumsReturn<__RTYPE__>::type, ColSumsImpl<__RTYPE__, NA, T, true> > { \
412 const MatrixBase<__RTYPE__, NA, T>& ref; \
414 typedef detail::ColSumsReturn<__RTYPE__> return_traits; \
415 typedef typename return_traits::type return_vector; \
416 typedef typename traits::storage_type<return_traits::rtype>::type stored_type; \
419 ColSumsImpl(const MatrixBase<__RTYPE__, NA, T>& ref_) \
423 return_vector get() const { \
424 R_xlen_t i, j, nr = ref.nrow(), nc = ref.ncol(); \
425 return_vector res(nc); \
427 stored_type current = stored_type(); \
429 for (j = 0; j < nc; j++) { \
430 for (i = 0; i < nr; i++) { \
431 current = ref(i, j); \
432 if (!traits::is_na<__RTYPE__>(current)) { \
433 detail::incr(&res[j], current); \
445#undef COL_SUMS_IMPL_RMNA
451template <
int RTYPE,
typename T,
bool NA_RM>
464template <
int RTYPE,
bool NA,
typename T,
bool NA_RM = false>
466 public Lazy<typename detail::RowMeansReturn<RTYPE>::type, RowMeansImpl<RTYPE, NA, T, NA_RM> > {
480 R_xlen_t i, j, nr =
ref.nrow(), nc =
ref.ncol();
483 for (j = 0; j < nc; j++) {
484 for (i = 0; i < nr; i++) {
489 for (i = 0; i < nr; i++) {
502#define ROW_MEANS_IMPL_KEEPNA(__RTYPE__) \
504template <bool NA, typename T, bool NA_RM> \
505class RowMeansImpl<__RTYPE__, NA, T, NA_RM> : \
506 public Lazy<typename detail::RowMeansReturn<__RTYPE__>::type, RowMeansImpl<__RTYPE__, NA, T, NA_RM> > { \
508 const MatrixBase<__RTYPE__, NA, T>& ref; \
510 typedef detail::RowMeansReturn<__RTYPE__> return_traits; \
511 typedef typename return_traits::type return_vector; \
512 typedef typename traits::storage_type<return_traits::rtype>::type stored_type; \
515 RowMeansImpl(const MatrixBase<__RTYPE__, NA, T>& ref_) \
519 return_vector get() const { \
520 R_xlen_t i, j, nr = ref.nrow(), nc = ref.ncol(); \
521 return_vector res(nr); \
523 for (j = 0; j < nc; j++) { \
524 for (i = 0; i < nr; i++) { \
525 if (traits::is_na<REALSXP>(res[i])) \
527 if (traits::is_na<__RTYPE__>(ref(i, j))) \
528 res[i] = traits::get_na<REALSXP>(); \
529 else detail::incr(&res[i], ref(i, j)); \
533 for (i = 0; i < nr; i++) \
534 if (!traits::is_na<REALSXP>(res[i])) \
535 detail::div(&res[i], nc); \
544#undef ROW_MEANS_IMPL_KEEPNA
551template <
int RTYPE,
bool NA,
typename T>
553 public Lazy<typename detail::RowMeansReturn<RTYPE>::type, RowMeansImpl<RTYPE, NA, T, true> > {
567 R_xlen_t i, j, nr =
ref.nrow(), nc =
ref.ncol();
570 std::vector<R_xlen_t> n_ok(nr, 0);
574 for (j = 0; j < nc; j++) {
575 for (i = 0; i < nr; i++) {
584 for (i = 0; i < nr; i++) {
601#define ROW_MEANS_IMPL_RMNA(__RTYPE__) \
603template <bool NA, typename T> \
604class RowMeansImpl<__RTYPE__, NA, T, true> : \
605 public Lazy<typename detail::RowMeansReturn<__RTYPE__>::type, RowMeansImpl<__RTYPE__, NA, T, true> > { \
607 const MatrixBase<__RTYPE__, NA, T>& ref; \
609 typedef detail::RowMeansReturn<__RTYPE__> return_traits; \
610 typedef typename return_traits::type return_vector; \
611 typedef typename traits::storage_type<return_traits::rtype>::type stored_type; \
614 RowMeansImpl(const MatrixBase<__RTYPE__, NA, T>& ref_) \
618 return_vector get() const { \
619 R_xlen_t i, j, nr = ref.nrow(), nc = ref.ncol(); \
620 return_vector res(nr); \
622 std::vector<R_xlen_t> n_ok(nr, 0); \
624 for (j = 0; j < nc; j++) { \
625 for (i = 0; i < nr; i++) { \
626 if (!traits::is_na<__RTYPE__>(ref(i, j))) { \
627 detail::incr(&res[i], ref(i, j)); \
633 for (i = 0; i < nr; i++) { \
635 detail::div(&res[i], n_ok[i]); \
637 detail::set_nan(&res[i]); \
648#undef ROW_MEANS_IMPL_RMNA
654template <
int RTYPE,
typename T,
bool NA_RM>
664template <
int RTYPE,
bool NA,
typename T,
bool NA_RM = false>
666 public Lazy<typename detail::ColMeansReturn<RTYPE>::type, ColMeansImpl<RTYPE, NA, T, NA_RM> > {
680 R_xlen_t i, j, nr =
ref.nrow(), nc =
ref.ncol();
683 for (j = 0; j < nc; j++) {
684 for (i = 0; i < nr; i++) {
689 for (j = 0; j < nc; j++) {
702#define COL_MEANS_IMPL_KEEPNA(__RTYPE__) \
704template <bool NA, typename T, bool NA_RM> \
705class ColMeansImpl<__RTYPE__, NA, T, NA_RM> : \
706 public Lazy<typename detail::ColMeansReturn<__RTYPE__>::type, ColMeansImpl<__RTYPE__, NA, T, NA_RM> > { \
708 const MatrixBase<__RTYPE__, NA, T>& ref; \
710 typedef detail::ColMeansReturn<__RTYPE__> return_traits; \
711 typedef typename return_traits::type return_vector; \
712 typedef typename traits::storage_type<return_traits::rtype>::type stored_type; \
715 ColMeansImpl(const MatrixBase<__RTYPE__, NA, T>& ref_) \
719 return_vector get() const { \
720 R_xlen_t i, j, nr = ref.nrow(), nc = ref.ncol(); \
721 return_vector res(nc); \
723 for (j = 0; j < nc; j++) { \
724 for (i = 0; i < nr; i++) { \
725 if (traits::is_na<REALSXP>(res[j])) \
727 if (traits::is_na<__RTYPE__>(ref(i, j))) \
728 res[j] = traits::get_na<REALSXP>(); \
729 else detail::incr(&res[j], ref(i, j)); \
733 for (j = 0; j < nc; j++) \
734 if (!traits::is_na<REALSXP>(res[j])) \
735 detail::div(&res[j], nr); \
744#undef COL_MEANS_IMPL_KEEPNA
751template <
int RTYPE,
bool NA,
typename T>
753 public Lazy<typename detail::ColMeansReturn<RTYPE>::type, ColMeansImpl<RTYPE, NA, T, true> > {
767 R_xlen_t i, j, nr =
ref.nrow(), nc =
ref.ncol();
770 std::vector<R_xlen_t> n_ok(nc, 0);
774 for (j = 0; j < nc; j++) {
775 for (i = 0; i < nr; i++) {
784 for (j = 0; j < nc; j++) {
801#define COL_MEANS_IMPL_RMNA(__RTYPE__) \
803template <bool NA, typename T> \
804class ColMeansImpl<__RTYPE__, NA, T, true> : \
805 public Lazy<typename detail::ColMeansReturn<__RTYPE__>::type, ColMeansImpl<__RTYPE__, NA, T, true> > { \
807 const MatrixBase<__RTYPE__, NA, T>& ref; \
809 typedef detail::ColMeansReturn<__RTYPE__> return_traits; \
810 typedef typename return_traits::type return_vector; \
811 typedef typename traits::storage_type<return_traits::rtype>::type stored_type; \
814 ColMeansImpl(const MatrixBase<__RTYPE__, NA, T>& ref_) \
818 return_vector get() const { \
819 R_xlen_t i, j, nr = ref.nrow(), nc = ref.ncol(); \
820 return_vector res(nc); \
822 std::vector<R_xlen_t> n_ok(nc, 0); \
824 for (j = 0; j < nc; j++) { \
825 for (i = 0; i < nr; i++) { \
826 if (!traits::is_na<__RTYPE__>(ref(i, j))) { \
827 detail::incr(&res[j], ref(i, j)); \
833 for (j = 0; j < nc; j++) { \
835 detail::div(&res[j], n_ok[j]); \
837 detail::set_nan(&res[j]); \
848#undef COL_MEANS_IMPL_RMNA
854template <
int RTYPE,
typename T,
bool NA_RM>
862template <
int RTYPE,
bool NA,
typename T>
871template <
int RTYPE,
bool NA,
typename T>
880template <
int RTYPE,
bool NA,
typename T>
889template <
int RTYPE,
bool NA,
typename T>
const MatrixBase< RTYPE, NA, T > & ref
ColMeansImpl(const MatrixBase< RTYPE, NA, T > &ref_)
traits::storage_type< return_traits::rtype >::type stored_type
return_traits::type return_vector
return_vector get() const
detail::ColMeansReturn< RTYPE > return_traits
return_vector get() const
ColMeansImpl(const MatrixBase< RTYPE, NA, T > &ref_)
const MatrixBase< RTYPE, NA, T > & ref
return_traits::type return_vector
detail::ColMeansReturn< RTYPE > return_traits
traits::storage_type< return_traits::rtype >::type stored_type
ColSumsImpl(const MatrixBase< RTYPE, NA, T > &ref_)
return_traits::type return_vector
traits::storage_type< return_traits::rtype >::type stored_type
detail::ColSumsReturn< RTYPE > return_traits
const MatrixBase< RTYPE, NA, T > & ref
return_vector get() const
const MatrixBase< RTYPE, NA, T > & ref
ColSumsImpl(const MatrixBase< RTYPE, NA, T > &ref_)
return_vector get() const
return_traits::type return_vector
traits::storage_type< return_traits::rtype >::type stored_type
detail::ColSumsReturn< RTYPE > return_traits
return_vector get() const
const MatrixBase< RTYPE, NA, T > & ref
return_traits::type return_vector
traits::storage_type< return_traits::rtype >::type stored_type
detail::RowMeansReturn< RTYPE > return_traits
RowMeansImpl(const MatrixBase< RTYPE, NA, T > &ref_)
return_traits::type return_vector
const MatrixBase< RTYPE, NA, T > & ref
traits::storage_type< return_traits::rtype >::type stored_type
return_vector get() const
detail::RowMeansReturn< RTYPE > return_traits
RowMeansImpl(const MatrixBase< RTYPE, NA, T > &ref_)
RowSumsImpl(const MatrixBase< RTYPE, NA, T > &ref_)
return_vector get() const
traits::storage_type< return_traits::rtype >::type stored_type
return_traits::type return_vector
const MatrixBase< RTYPE, NA, T > & ref
detail::RowSumsReturn< RTYPE > return_traits
const MatrixBase< RTYPE, NA, T > & ref
return_vector get() const
traits::storage_type< return_traits::rtype >::type stored_type
detail::RowSumsReturn< RTYPE > return_traits
RowSumsImpl(const MatrixBase< RTYPE, NA, T > &ref_)
return_traits::type return_vector
void incr(double *lhs, double rhs)
void div(double *lhs, R_xlen_t rhs)
bool is_na(typename storage_type< RTYPE >::type)
sugar::detail::ColSumsReturn< RTYPE >::type colSums(const MatrixBase< RTYPE, NA, T > &x, bool na_rm=false)
sugar::detail::ColMeansReturn< RTYPE >::type colMeans(const MatrixBase< RTYPE, NA, T > &x, bool na_rm=false)
sugar::detail::RowSumsReturn< RTYPE >::type rowSums(const MatrixBase< RTYPE, NA, T > &x, bool na_rm=false)
sugar::detail::RowMeansReturn< RTYPE >::type rowMeans(const MatrixBase< RTYPE, NA, T > &x, bool na_rm=false)
#define ROW_SUMS_IMPL_RMNA(__RTYPE__)
#define ROW_MEANS_IMPL_KEEPNA(__RTYPE__)
#define ROW_SUMS_IMPL_KEEPNA(__RTYPE__)
#define COL_SUMS_IMPL_RMNA(__RTYPE__)
#define ROW_MEANS_IMPL_RMNA(__RTYPE__)
#define COL_MEANS_IMPL_KEEPNA(__RTYPE__)
#define COL_SUMS_IMPL_KEEPNA(__RTYPE__)
#define COL_MEANS_IMPL_RMNA(__RTYPE__)
#define RCPP_SAFE_ADD(a, b)