22#ifndef Rcpp__sugar__rowSums_h
23#define Rcpp__sugar__rowSums_h
51inline void incr(
double* lhs,
double rhs) {
55inline void incr(
int* lhs,
int rhs) {
66 *lhs /=
static_cast<double>(rhs);
70 lhs->r /=
static_cast<double>(rhs);
71 lhs->i /=
static_cast<double>(rhs);
127template <
int RTYPE,
bool NA,
typename T,
bool NA_RM = false>
129 public Lazy<typename detail::RowSumsReturn<RTYPE>::type, RowSumsImpl<RTYPE, NA, T, NA_RM> > {
146 for (j = 0; j < nc; j++) {
147 for (i = 0; i < nr; i++) {
169#define ROW_SUMS_IMPL_KEEPNA(__RTYPE__) \
171template <bool NA, typename T, bool NA_RM> \
172class RowSumsImpl<__RTYPE__, NA, T, NA_RM> : \
173 public Lazy<typename detail::RowSumsReturn<__RTYPE__>::type, RowSumsImpl<__RTYPE__, NA, T, NA_RM> > { \
175 const MatrixBase<__RTYPE__, NA, T>& ref; \
177 typedef detail::RowSumsReturn<__RTYPE__> return_traits; \
178 typedef typename return_traits::type return_vector; \
179 typedef typename traits::storage_type<return_traits::rtype>::type stored_type; \
182 unsigned char x : 1; \
186 RowSumsImpl(const MatrixBase<__RTYPE__, NA, T>& ref_) \
190 return_vector get() const { \
191 R_xlen_t i, j, nr = ref.nrow(), nc = ref.ncol(); \
192 return_vector res(nr); \
194 std::vector<bit> na_flags(nr); \
196 for (j = 0; j < nc; j++) { \
197 for (i = 0; i < nr; i++) { \
198 if (detail::check_na(ref(i, j))) { \
199 na_flags[i].x |= 0x1; \
201 detail::incr(&res[i], ref(i, j)); \
205 for (i = 0; i < nr; i++) { \
206 if (na_flags[i].x) { \
207 res[i] = NA_INTEGER; \
218#undef ROW_SUMS_IMPL_KEEPNA
225template <
int RTYPE,
bool NA,
typename T>
227 public Lazy<typename detail::RowSumsReturn<RTYPE>::type, RowSumsImpl<RTYPE, NA, T, true> > {
246 for (j = 0; j < nc; j++) {
247 for (i = 0; i < nr; i++) {
264#define ROW_SUMS_IMPL_RMNA(__RTYPE__) \
266template <bool NA, typename T> \
267class RowSumsImpl<__RTYPE__, NA, T, true> : \
268 public Lazy<typename detail::RowSumsReturn<__RTYPE__>::type, RowSumsImpl<__RTYPE__, NA, T, true> > { \
270 const MatrixBase<__RTYPE__, NA, T>& ref; \
272 typedef detail::RowSumsReturn<__RTYPE__> return_traits; \
273 typedef typename return_traits::type return_vector; \
274 typedef typename traits::storage_type<return_traits::rtype>::type stored_type; \
277 RowSumsImpl(const MatrixBase<__RTYPE__, NA, T>& ref_) \
281 return_vector get() const { \
282 R_xlen_t i, j, nr = ref.nrow(), nc = ref.ncol(); \
283 return_vector res(nr); \
285 stored_type current = stored_type(); \
287 for (j = 0; j < nc; j++) { \
288 for (i = 0; i < nr; i++) { \
289 current = ref(i, j); \
290 if (!detail::check_na(current)) { \
291 detail::incr(&res[i], current); \
303#undef ROW_SUMS_IMPL_RMNA
309template <
int RTYPE,
typename T,
bool NA_RM>
319template <
int RTYPE,
bool NA,
typename T,
bool NA_RM = false>
321 public Lazy<typename detail::ColSumsReturn<RTYPE>::type, ColSumsImpl<RTYPE, NA, T, NA_RM> > {
338 for (j = 0; j < nc; j++) {
339 for (i = 0; i < nr; i++) {
353#define COL_SUMS_IMPL_KEEPNA(__RTYPE__) \
355template <bool NA, typename T, bool NA_RM> \
356class ColSumsImpl<__RTYPE__, NA, T, NA_RM> : \
357 public Lazy<typename detail::ColSumsReturn<__RTYPE__>::type, ColSumsImpl<__RTYPE__, NA, T, NA_RM> > { \
359 const MatrixBase<__RTYPE__, NA, T>& ref; \
361 typedef detail::ColSumsReturn<__RTYPE__> return_traits; \
362 typedef typename return_traits::type return_vector; \
363 typedef typename traits::storage_type<return_traits::rtype>::type stored_type; \
366 unsigned char x : 1; \
370 ColSumsImpl(const MatrixBase<__RTYPE__, NA, T>& ref_) \
374 return_vector get() const { \
375 R_xlen_t i, j, nr = ref.nrow(), nc = ref.ncol(); \
376 return_vector res(nc); \
378 std::vector<bit> na_flags(nc); \
380 for (j = 0; j < nc; j++) { \
381 for (i = 0; i < nr; i++) { \
382 if (detail::check_na(ref(i, j))) { \
383 na_flags[j].x |= 0x1; \
385 detail::incr(&res[j], ref(i, j)); \
389 for (j = 0; j < nc; j++) { \
390 if (na_flags[j].x) { \
391 res[j] = NA_INTEGER; \
402#undef COL_SUMS_IMPL_KEEPNA
409template <
int RTYPE,
bool NA,
typename T>
411 public Lazy<typename detail::ColSumsReturn<RTYPE>::type, ColSumsImpl<RTYPE, NA, T, true> > {
430 for (j = 0; j < nc; j++) {
431 for (i = 0; i < nr; i++) {
448#define COL_SUMS_IMPL_RMNA(__RTYPE__) \
450template <bool NA, typename T> \
451class ColSumsImpl<__RTYPE__, NA, T, true> : \
452 public Lazy<typename detail::ColSumsReturn<__RTYPE__>::type, ColSumsImpl<__RTYPE__, NA, T, true> > { \
454 const MatrixBase<__RTYPE__, NA, T>& ref; \
456 typedef detail::ColSumsReturn<__RTYPE__> return_traits; \
457 typedef typename return_traits::type return_vector; \
458 typedef typename traits::storage_type<return_traits::rtype>::type stored_type; \
461 ColSumsImpl(const MatrixBase<__RTYPE__, NA, T>& ref_) \
465 return_vector get() const { \
466 R_xlen_t i, j, nr = ref.nrow(), nc = ref.ncol(); \
467 return_vector res(nc); \
469 stored_type current = stored_type(); \
471 for (j = 0; j < nc; j++) { \
472 for (i = 0; i < nr; i++) { \
473 current = ref(i, j); \
474 if (!detail::check_na(current)) { \
475 detail::incr(&res[j], current); \
487#undef COL_SUMS_IMPL_RMNA
493template <
int RTYPE,
typename T,
bool NA_RM>
506template <
int RTYPE,
bool NA,
typename T,
bool NA_RM = false>
508 public Lazy<typename detail::RowMeansReturn<RTYPE>::type, RowMeansImpl<RTYPE, NA, T, NA_RM> > {
525 for (j = 0; j < nc; j++) {
526 for (i = 0; i < nr; i++) {
531 for (i = 0; i < nr; i++) {
544#define ROW_MEANS_IMPL_KEEPNA(__RTYPE__) \
546template <bool NA, typename T, bool NA_RM> \
547class RowMeansImpl<__RTYPE__, NA, T, NA_RM> : \
548 public Lazy<typename detail::RowMeansReturn<__RTYPE__>::type, RowMeansImpl<__RTYPE__, NA, T, NA_RM> > { \
550 const MatrixBase<__RTYPE__, NA, T>& ref; \
552 typedef detail::RowMeansReturn<__RTYPE__> return_traits; \
553 typedef typename return_traits::type return_vector; \
554 typedef typename traits::storage_type<return_traits::rtype>::type stored_type; \
557 unsigned char x : 1; \
561 RowMeansImpl(const MatrixBase<__RTYPE__, NA, T>& ref_) \
565 return_vector get() const { \
566 R_xlen_t i, j, nr = ref.nrow(), nc = ref.ncol(); \
567 return_vector res(nr); \
569 std::vector<bit> na_flags(nc); \
571 for (j = 0; j < nc; j++) { \
572 for (i = 0; i < nr; i++) { \
573 if (detail::check_na(ref(i, j))) { \
574 na_flags[i].x |= 0x1; \
576 detail::incr(&res[i], ref(i, j)); \
580 for (i = 0; i < nr; i++) { \
581 if (!na_flags[i].x) { \
582 detail::div(&res[i], nc); \
595#undef ROW_MEANS_IMPL_KEEPNA
602template <
int RTYPE,
bool NA,
typename T>
604 public Lazy<typename detail::RowMeansReturn<RTYPE>::type, RowMeansImpl<RTYPE, NA, T, true> > {
621 std::vector<R_xlen_t>
n_ok(nr, 0);
625 for (j = 0; j < nc; j++) {
626 for (i = 0; i < nr; i++) {
635 for (i = 0; i < nr; i++) {
652#define ROW_MEANS_IMPL_RMNA(__RTYPE__) \
654template <bool NA, typename T> \
655class RowMeansImpl<__RTYPE__, NA, T, true> : \
656 public Lazy<typename detail::RowMeansReturn<__RTYPE__>::type, RowMeansImpl<__RTYPE__, NA, T, true> > { \
658 const MatrixBase<__RTYPE__, NA, T>& ref; \
660 typedef detail::RowMeansReturn<__RTYPE__> return_traits; \
661 typedef typename return_traits::type return_vector; \
662 typedef typename traits::storage_type<return_traits::rtype>::type stored_type; \
665 RowMeansImpl(const MatrixBase<__RTYPE__, NA, T>& ref_) \
669 return_vector get() const { \
670 R_xlen_t i, j, nr = ref.nrow(), nc = ref.ncol(); \
671 return_vector res(nr); \
673 std::vector<R_xlen_t> n_ok(nr, 0); \
675 for (j = 0; j < nc; j++) { \
676 for (i = 0; i < nr; i++) { \
677 if (!detail::check_na(ref(i, j))) { \
678 detail::incr(&res[i], ref(i, j)); \
684 for (i = 0; i < nr; i++) { \
686 detail::div(&res[i], n_ok[i]); \
688 detail::set_nan(&res[i]); \
699#undef ROW_MEANS_IMPL_RMNA
705template <
int RTYPE,
typename T,
bool NA_RM>
715template <
int RTYPE,
bool NA,
typename T,
bool NA_RM = false>
717 public Lazy<typename detail::ColMeansReturn<RTYPE>::type, ColMeansImpl<RTYPE, NA, T, NA_RM> > {
734 for (j = 0; j < nc; j++) {
735 for (i = 0; i < nr; i++) {
740 for (j = 0; j < nc; j++) {
753#define COL_MEANS_IMPL_KEEPNA(__RTYPE__) \
755template <bool NA, typename T, bool NA_RM> \
756class ColMeansImpl<__RTYPE__, NA, T, NA_RM> : \
757 public Lazy<typename detail::ColMeansReturn<__RTYPE__>::type, ColMeansImpl<__RTYPE__, NA, T, NA_RM> > { \
759 const MatrixBase<__RTYPE__, NA, T>& ref; \
761 typedef detail::ColMeansReturn<__RTYPE__> return_traits; \
762 typedef typename return_traits::type return_vector; \
763 typedef typename traits::storage_type<return_traits::rtype>::type stored_type; \
766 unsigned char x : 1; \
770 ColMeansImpl(const MatrixBase<__RTYPE__, NA, T>& ref_) \
774 return_vector get() const { \
775 R_xlen_t i, j, nr = ref.nrow(), nc = ref.ncol(); \
776 return_vector res(nc); \
778 std::vector<bit> na_flags(nc); \
780 for (j = 0; j < nc; j++) { \
781 for (i = 0; i < nr; i++) { \
782 if (detail::check_na(ref(i, j))) { \
783 na_flags[j].x |= 0x1; \
785 detail::incr(&res[j], ref(i, j)); \
789 for (j = 0; j < nc; j++) { \
790 if (!na_flags[j].x) { \
791 detail::div(&res[j], nr); \
804#undef COL_MEANS_IMPL_KEEPNA
811template <
int RTYPE,
bool NA,
typename T>
813 public Lazy<typename detail::ColMeansReturn<RTYPE>::type, ColMeansImpl<RTYPE, NA, T, true> > {
830 std::vector<R_xlen_t>
n_ok(nc, 0);
834 for (j = 0; j < nc; j++) {
835 for (i = 0; i < nr; i++) {
844 for (j = 0; j < nc; j++) {
861#define COL_MEANS_IMPL_RMNA(__RTYPE__) \
863template <bool NA, typename T> \
864class ColMeansImpl<__RTYPE__, NA, T, true> : \
865 public Lazy<typename detail::ColMeansReturn<__RTYPE__>::type, ColMeansImpl<__RTYPE__, NA, T, true> > { \
867 const MatrixBase<__RTYPE__, NA, T>& ref; \
869 typedef detail::ColMeansReturn<__RTYPE__> return_traits; \
870 typedef typename return_traits::type return_vector; \
871 typedef typename traits::storage_type<return_traits::rtype>::type stored_type; \
874 ColMeansImpl(const MatrixBase<__RTYPE__, NA, T>& ref_) \
878 return_vector get() const { \
879 R_xlen_t i, j, nr = ref.nrow(), nc = ref.ncol(); \
880 return_vector res(nc); \
882 std::vector<R_xlen_t> n_ok(nc, 0); \
884 for (j = 0; j < nc; j++) { \
885 for (i = 0; i < nr; i++) { \
886 if (!detail::check_na(ref(i, j))) { \
887 detail::incr(&res[j], ref(i, j)); \
893 for (j = 0; j < nc; j++) { \
895 detail::div(&res[j], n_ok[j]); \
897 detail::set_nan(&res[j]); \
908#undef COL_MEANS_IMPL_RMNA
914template <
int RTYPE,
typename T,
bool NA_RM>
922template <
int RTYPE,
bool NA,
typename T>
931template <
int RTYPE,
bool NA,
typename T>
940template <
int RTYPE,
bool NA,
typename T>
949template <
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)
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__)