Rcpp Version 1.1.2
Loading...
Searching...
No Matches
rowSums.h
Go to the documentation of this file.
1// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
2//
3// rowSums.h: Rcpp R/C++ interface class library -- rowSums, colSums, rowMeans, colMeans
4//
5// Copyright (C) 2016 - 2025 Nathan Russell
6// Copyright (C) 2026 Nathan Russell and IƱaki Ucar
7//
8// This file is part of Rcpp.
9//
10// Rcpp is free software: you can redistribute it and/or modify it
11// under the terms of the GNU General Public License as published by
12// the Free Software Foundation, either version 2 of the License, or
13// (at your option) any later version.
14//
15// Rcpp is distributed in the hope that it will be useful, but
16// WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18// GNU General Public License for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
22
23#ifndef Rcpp__sugar__rowSums_h
24#define Rcpp__sugar__rowSums_h
25
26namespace Rcpp {
27namespace sugar {
28namespace detail {
29
30
31inline void incr(double* lhs, double rhs) {
32 *lhs += rhs;
33}
34
35inline void incr(int* lhs, int rhs) {
36 *lhs = RCPP_SAFE_ADD(*lhs, rhs);
37}
38
39inline void incr(Rcomplex* lhs, const Rcomplex& rhs) {
40 lhs->r += rhs.r;
41 lhs->i += rhs.i;
42}
43
44
45inline void div(double* lhs, R_xlen_t rhs) {
46 *lhs /= static_cast<double>(rhs);
47}
48
49inline void div(Rcomplex* lhs, R_xlen_t rhs) {
50 lhs->r /= static_cast<double>(rhs);
51 lhs->i /= static_cast<double>(rhs);
52}
53
54
55inline void set_nan(double* x) {
56 *x = R_NaN;
57}
58
59inline void set_nan(Rcomplex* x) {
60 x->r = R_NaN;
61 x->i = R_NaN;
62}
63
64
65template <int RTYPE>
68 enum { rtype = RTYPE };
69};
70
71template <>
72struct RowSumsReturn<LGLSXP> {
74 enum { rtype = INTSXP };
75};
76
77template <int RTYPE>
79 : public RowSumsReturn<RTYPE> {};
80
81
82template <int RTYPE>
85 enum { rtype = REALSXP };
86};
87
88template <>
89struct RowMeansReturn<CPLXSXP> {
91 enum { rtype = CPLXSXP };
92};
93
94template <int RTYPE>
96 : public RowMeansReturn<RTYPE> {};
97
98
99} // detail
100
101
102// RowSums
103// na.rm = FALSE
104// default input
105// default output
106//
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> > {
110private:
112
116
117public:
119 : ref(ref_)
120 {}
121
123 R_xlen_t i, j, nr = ref.nrow(), nc = ref.ncol();
124 return_vector res(nr);
125
126 for (j = 0; j < nc; j++) {
127 for (i = 0; i < nr; i++) {
128 detail::incr(&res[i], ref(i, j));
129 }
130 }
131
132 return res;
133 }
134};
135
136// RowSums
137// na.rm = FALSE
138// LGLSXP / INTSXP input
139// INTSXP output
140//
141// int + NA_LOGICAL (NA_INTEGER) != NA_INTEGER, as is the
142// case with NA_REAL, so we specialize for these two SEXPTYPES
143// and do explicit accounting of NAs.
144//
145// The two specializations, while necessary, are redundant, hence
146// the macro. The same applies to the 'na.rm = TRUE' variant, and
147// likewise for colSums, rowMeans, and colMeans.
148//
149#define ROW_SUMS_IMPL_KEEPNA(__RTYPE__) \
150 \
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> > { \
154private: \
155 const MatrixBase<__RTYPE__, NA, T>& ref; \
156 \
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; \
160 \
161public: \
162 RowSumsImpl(const MatrixBase<__RTYPE__, NA, T>& ref_) \
163 : ref(ref_) \
164 {} \
165 \
166 return_vector get() const { \
167 R_xlen_t i, j, nr = ref.nrow(), nc = ref.ncol(); \
168 return_vector res(nr); \
169 \
170 for (j = 0; j < nc; j++) { \
171 for (i = 0; i < nr; i++) { \
172 if (traits::is_na<__RTYPE__>(res[i])) \
173 continue; \
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)); \
177 } \
178 } \
179 \
180 return res; \
181 } \
182};
183
186
187#undef ROW_SUMS_IMPL_KEEPNA
188
189// RowSums
190// na.rm = TRUE
191// default input
192// default output
193//
194template <int RTYPE, bool NA, typename T>
195class RowSumsImpl<RTYPE, NA, T, true> :
196 public Lazy<typename detail::RowSumsReturn<RTYPE>::type, RowSumsImpl<RTYPE, NA, T, true> > {
197private:
199
203
204public:
206 : ref(ref_)
207 {}
208
210 R_xlen_t i, j, nr = ref.nrow(), nc = ref.ncol();
211 return_vector res(nr);
212
213 stored_type current = stored_type();
214
215 for (j = 0; j < nc; j++) {
216 for (i = 0; i < nr; i++) {
217 current = ref(i, j);
218 if (!traits::is_na<RTYPE>(current)) {
219 detail::incr(&res[i], current);
220 }
221 }
222 }
223
224 return res;
225 }
226};
227
228// RowSums
229// na.rm = TRUE
230// LGLSXP / INTSXP input
231// INTSXP output
232//
233#define ROW_SUMS_IMPL_RMNA(__RTYPE__) \
234 \
235template <bool NA, typename T> \
236class RowSumsImpl<__RTYPE__, NA, T, true> : \
237 public Lazy<typename detail::RowSumsReturn<__RTYPE__>::type, RowSumsImpl<__RTYPE__, NA, T, true> > { \
238private: \
239 const MatrixBase<__RTYPE__, NA, T>& ref; \
240 \
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; \
244 \
245public: \
246 RowSumsImpl(const MatrixBase<__RTYPE__, NA, T>& ref_) \
247 : ref(ref_) \
248 {} \
249 \
250 return_vector get() const { \
251 R_xlen_t i, j, nr = ref.nrow(), nc = ref.ncol(); \
252 return_vector res(nr); \
253 \
254 stored_type current = stored_type(); \
255 \
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); \
261 } \
262 } \
263 } \
264 \
265 return res; \
266 } \
267};
268
269ROW_SUMS_IMPL_RMNA(LGLSXP)
270ROW_SUMS_IMPL_RMNA(INTSXP)
271
272#undef ROW_SUMS_IMPL_RMNA
273
274// RowSums
275// Input with template parameter NA = false
276// RowSumsImpl<..., NA_RM = false>
277//
278template <int RTYPE, typename T, bool NA_RM>
279class RowSumsImpl<RTYPE, false, T, NA_RM>
280 : public RowSumsImpl<RTYPE, false, T, false> {};
281
282
283// ColSums
284// na.rm = FALSE
285// default input
286// default output
287//
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> > {
291private:
293
297
298public:
300 : ref(ref_)
301 {}
302
304 R_xlen_t i, j, nr = ref.nrow(), nc = ref.ncol();
305 return_vector res(nc);
306
307 for (j = 0; j < nc; j++) {
308 for (i = 0; i < nr; i++) {
309 detail::incr(&res[j], ref(i, j));
310 }
311 }
312
313 return res;
314 }
315};
316
317// ColSums
318// na.rm = FALSE
319// LGLSXP / INTSXP input
320// INTSXP output
321//
322#define COL_SUMS_IMPL_KEEPNA(__RTYPE__) \
323 \
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> > { \
327private: \
328 const MatrixBase<__RTYPE__, NA, T>& ref; \
329 \
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; \
333 \
334public: \
335 ColSumsImpl(const MatrixBase<__RTYPE__, NA, T>& ref_) \
336 : ref(ref_) \
337 {} \
338 \
339 return_vector get() const { \
340 R_xlen_t i, j, nr = ref.nrow(), nc = ref.ncol(); \
341 return_vector res(nc); \
342 \
343 for (j = 0; j < nc; j++) { \
344 for (i = 0; i < nr; i++) { \
345 if (traits::is_na<__RTYPE__>(res[j])) \
346 continue; \
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)); \
350 } \
351 } \
352 \
353 return res; \
354 } \
355};
356
359
360#undef COL_SUMS_IMPL_KEEPNA
361
362// ColSums
363// na.rm = TRUE
364// default input
365// default output
366//
367template <int RTYPE, bool NA, typename T>
368class ColSumsImpl<RTYPE, NA, T, true> :
369 public Lazy<typename detail::ColSumsReturn<RTYPE>::type, ColSumsImpl<RTYPE, NA, T, true> > {
370private:
372
376
377public:
379 : ref(ref_)
380 {}
381
383 R_xlen_t i, j, nr = ref.nrow(), nc = ref.ncol();
384 return_vector res(nc);
385
386 stored_type current = stored_type();
387
388 for (j = 0; j < nc; j++) {
389 for (i = 0; i < nr; i++) {
390 current = ref(i, j);
391 if (!traits::is_na<RTYPE>(current)) {
392 detail::incr(&res[j], current);
393 }
394 }
395 }
396
397 return res;
398 }
399};
400
401// ColSums
402// na.rm = TRUE
403// LGLSXP / INTSXP input
404// INTSXP output
405//
406#define COL_SUMS_IMPL_RMNA(__RTYPE__) \
407 \
408template <bool NA, typename T> \
409class ColSumsImpl<__RTYPE__, NA, T, true> : \
410 public Lazy<typename detail::ColSumsReturn<__RTYPE__>::type, ColSumsImpl<__RTYPE__, NA, T, true> > { \
411private: \
412 const MatrixBase<__RTYPE__, NA, T>& ref; \
413 \
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; \
417 \
418public: \
419 ColSumsImpl(const MatrixBase<__RTYPE__, NA, T>& ref_) \
420 : ref(ref_) \
421 {} \
422 \
423 return_vector get() const { \
424 R_xlen_t i, j, nr = ref.nrow(), nc = ref.ncol(); \
425 return_vector res(nc); \
426 \
427 stored_type current = stored_type(); \
428 \
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); \
434 } \
435 } \
436 } \
437 \
438 return res; \
439 } \
440};
441
442COL_SUMS_IMPL_RMNA(LGLSXP)
443COL_SUMS_IMPL_RMNA(INTSXP)
444
445#undef COL_SUMS_IMPL_RMNA
446
447// ColSums
448// Input with template parameter NA = false
449// ColSumsImpl<..., NA_RM = false>
450//
451template <int RTYPE, typename T, bool NA_RM>
452class ColSumsImpl<RTYPE, false, T, NA_RM>
453 : public ColSumsImpl<RTYPE, false, T, false> {};
454
455
456// RowMeans
457// na.rm = FALSE
458// default input
459// default output
460//
461// All RowMeans and ColMeans variants use a single-pass
462// mean calculation as in array.c
463//
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> > {
467private:
469
473
474public:
476 : ref(ref_)
477 {}
478
480 R_xlen_t i, j, nr = ref.nrow(), nc = ref.ncol();
481 return_vector res(nr);
482
483 for (j = 0; j < nc; j++) {
484 for (i = 0; i < nr; i++) {
485 detail::incr(&res[i], ref(i, j));
486 }
487 }
488
489 for (i = 0; i < nr; i++) {
490 detail::div(&res[i], nc);
491 }
492
493 return res;
494 }
495};
496
497// RowMeans
498// na.rm = FALSE
499// LGLSXP / INTSXP input
500// REALSXP output
501//
502#define ROW_MEANS_IMPL_KEEPNA(__RTYPE__) \
503 \
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> > { \
507private: \
508 const MatrixBase<__RTYPE__, NA, T>& ref; \
509 \
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; \
513 \
514public: \
515 RowMeansImpl(const MatrixBase<__RTYPE__, NA, T>& ref_) \
516 : ref(ref_) \
517 {} \
518 \
519 return_vector get() const { \
520 R_xlen_t i, j, nr = ref.nrow(), nc = ref.ncol(); \
521 return_vector res(nr); \
522 \
523 for (j = 0; j < nc; j++) { \
524 for (i = 0; i < nr; i++) { \
525 if (traits::is_na<REALSXP>(res[i])) \
526 continue; \
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)); \
530 } \
531 } \
532 \
533 for (i = 0; i < nr; i++) \
534 if (!traits::is_na<REALSXP>(res[i])) \
535 detail::div(&res[i], nc); \
536 \
537 return res; \
538 } \
539};
540
543
544#undef ROW_MEANS_IMPL_KEEPNA
545
546// RowMeans
547// na.rm = TRUE
548// default input
549// default output
550//
551template <int RTYPE, bool NA, typename T>
552class RowMeansImpl<RTYPE, NA, T, true> :
553 public Lazy<typename detail::RowMeansReturn<RTYPE>::type, RowMeansImpl<RTYPE, NA, T, true> > {
554private:
556
560
561public:
563 : ref(ref_)
564 {}
565
567 R_xlen_t i, j, nr = ref.nrow(), nc = ref.ncol();
568 return_vector res(nr);
569
570 std::vector<R_xlen_t> n_ok(nr, 0);
571 stored_type current = stored_type();
572
573
574 for (j = 0; j < nc; j++) {
575 for (i = 0; i < nr; i++) {
576 current = ref(i, j);
577 if (!traits::is_na<RTYPE>(current)) {
578 detail::incr(&res[i], ref(i, j));
579 ++n_ok[i];
580 }
581 }
582 }
583
584 for (i = 0; i < nr; i++) {
585 if (n_ok[i]) {
586 detail::div(&res[i], n_ok[i]);
587 } else {
588 detail::set_nan(&res[i]);
589 }
590 }
591
592 return res;
593 }
594};
595
596// RowMeans
597// na.rm = TRUE
598// LGLSXP / INTSXP input
599// REALSXP output
600//
601#define ROW_MEANS_IMPL_RMNA(__RTYPE__) \
602 \
603template <bool NA, typename T> \
604class RowMeansImpl<__RTYPE__, NA, T, true> : \
605 public Lazy<typename detail::RowMeansReturn<__RTYPE__>::type, RowMeansImpl<__RTYPE__, NA, T, true> > { \
606private: \
607 const MatrixBase<__RTYPE__, NA, T>& ref; \
608 \
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; \
612 \
613public: \
614 RowMeansImpl(const MatrixBase<__RTYPE__, NA, T>& ref_) \
615 : ref(ref_) \
616 {} \
617 \
618 return_vector get() const { \
619 R_xlen_t i, j, nr = ref.nrow(), nc = ref.ncol(); \
620 return_vector res(nr); \
621 \
622 std::vector<R_xlen_t> n_ok(nr, 0); \
623 \
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)); \
628 ++n_ok[i]; \
629 } \
630 } \
631 } \
632 \
633 for (i = 0; i < nr; i++) { \
634 if (n_ok[i]) { \
635 detail::div(&res[i], n_ok[i]); \
636 } else { \
637 detail::set_nan(&res[i]); \
638 } \
639 } \
640 \
641 return res; \
642 } \
643};
644
647
648#undef ROW_MEANS_IMPL_RMNA
649
650// RowMeans
651// Input with template parameter NA = false
652// RowMeansImpl<..., NA_RM = false>
653//
654template <int RTYPE, typename T, bool NA_RM>
655class RowMeansImpl<RTYPE, false, T, NA_RM>
656 : public RowMeansImpl<RTYPE, false, T, false> {};
657
658
659// ColMeans
660// na.rm = FALSE
661// default input
662// default output
663//
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> > {
667private:
669
673
674public:
676 : ref(ref_)
677 {}
678
680 R_xlen_t i, j, nr = ref.nrow(), nc = ref.ncol();
681 return_vector res(nc);
682
683 for (j = 0; j < nc; j++) {
684 for (i = 0; i < nr; i++) {
685 detail::incr(&res[j], ref(i, j));
686 }
687 }
688
689 for (j = 0; j < nc; j++) {
690 detail::div(&res[j], nr);
691 }
692
693 return res;
694 }
695};
696
697// ColMeans
698// na.rm = FALSE
699// LGLSXP / INTSXP input
700// REALSXP output
701//
702#define COL_MEANS_IMPL_KEEPNA(__RTYPE__) \
703 \
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> > { \
707private: \
708 const MatrixBase<__RTYPE__, NA, T>& ref; \
709 \
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; \
713 \
714public: \
715 ColMeansImpl(const MatrixBase<__RTYPE__, NA, T>& ref_) \
716 : ref(ref_) \
717 {} \
718 \
719 return_vector get() const { \
720 R_xlen_t i, j, nr = ref.nrow(), nc = ref.ncol(); \
721 return_vector res(nc); \
722 \
723 for (j = 0; j < nc; j++) { \
724 for (i = 0; i < nr; i++) { \
725 if (traits::is_na<REALSXP>(res[j])) \
726 continue; \
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)); \
730 } \
731 } \
732 \
733 for (j = 0; j < nc; j++) \
734 if (!traits::is_na<REALSXP>(res[j])) \
735 detail::div(&res[j], nr); \
736 \
737 return res; \
738 } \
739};
740
743
744#undef COL_MEANS_IMPL_KEEPNA
745
746// ColMeans
747// na.rm = TRUE
748// default input
749// default output
750//
751template <int RTYPE, bool NA, typename T>
752class ColMeansImpl<RTYPE, NA, T, true> :
753 public Lazy<typename detail::ColMeansReturn<RTYPE>::type, ColMeansImpl<RTYPE, NA, T, true> > {
754private:
756
760
761public:
763 : ref(ref_)
764 {}
765
767 R_xlen_t i, j, nr = ref.nrow(), nc = ref.ncol();
768 return_vector res(nc);
769
770 std::vector<R_xlen_t> n_ok(nc, 0);
771 stored_type current = stored_type();
772
773
774 for (j = 0; j < nc; j++) {
775 for (i = 0; i < nr; i++) {
776 current = ref(i, j);
777 if (!traits::is_na<RTYPE>(current)) {
778 detail::incr(&res[j], ref(i, j));
779 ++n_ok[j];
780 }
781 }
782 }
783
784 for (j = 0; j < nc; j++) {
785 if (n_ok[j]) {
786 detail::div(&res[j], n_ok[j]);
787 } else {
788 detail::set_nan(&res[j]);
789 }
790 }
791
792 return res;
793 }
794};
795
796// ColMeans
797// na.rm = TRUE
798// LGLSXP / INTSXP input
799// REALSXP output
800//
801#define COL_MEANS_IMPL_RMNA(__RTYPE__) \
802 \
803template <bool NA, typename T> \
804class ColMeansImpl<__RTYPE__, NA, T, true> : \
805 public Lazy<typename detail::ColMeansReturn<__RTYPE__>::type, ColMeansImpl<__RTYPE__, NA, T, true> > { \
806private: \
807 const MatrixBase<__RTYPE__, NA, T>& ref; \
808 \
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; \
812 \
813public: \
814 ColMeansImpl(const MatrixBase<__RTYPE__, NA, T>& ref_) \
815 : ref(ref_) \
816 {} \
817 \
818 return_vector get() const { \
819 R_xlen_t i, j, nr = ref.nrow(), nc = ref.ncol(); \
820 return_vector res(nc); \
821 \
822 std::vector<R_xlen_t> n_ok(nc, 0); \
823 \
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)); \
828 ++n_ok[j]; \
829 } \
830 } \
831 } \
832 \
833 for (j = 0; j < nc; j++) { \
834 if (n_ok[j]) { \
835 detail::div(&res[j], n_ok[j]); \
836 } else { \
837 detail::set_nan(&res[j]); \
838 } \
839 } \
840 \
841 return res; \
842 } \
843};
844
847
848#undef COL_MEANS_IMPL_RMNA
849
850// ColMeans
851// Input with template parameter NA = false
852// ColMeansImpl<..., NA_RM = false>
853//
854template <int RTYPE, typename T, bool NA_RM>
855class ColMeansImpl<RTYPE, false, T, NA_RM>
856 : public ColMeansImpl<RTYPE, false, T, false> {};
857
858
859} // sugar
860
861
862template <int RTYPE, bool NA, typename T>
864rowSums(const MatrixBase<RTYPE, NA, T>& x, bool na_rm = false) {
865 if (!na_rm) {
867 }
869}
870
871template <int RTYPE, bool NA, typename T>
873colSums(const MatrixBase<RTYPE, NA, T>& x, bool na_rm = false) {
874 if (!na_rm) {
876 }
878}
879
880template <int RTYPE, bool NA, typename T>
882rowMeans(const MatrixBase<RTYPE, NA, T>& x, bool na_rm = false) {
883 if (!na_rm) {
885 }
887}
888
889template <int RTYPE, bool NA, typename T>
891colMeans(const MatrixBase<RTYPE, NA, T>& x, bool na_rm = false) {
892 if (!na_rm) {
894 }
896}
897
898
899} // Rcpp
900
901#endif // Rcpp__sugar__rowSums_h
const MatrixBase< RTYPE, NA, T > & ref
Definition rowSums.h:755
ColMeansImpl(const MatrixBase< RTYPE, NA, T > &ref_)
Definition rowSums.h:762
traits::storage_type< return_traits::rtype >::type stored_type
Definition rowSums.h:759
detail::ColMeansReturn< RTYPE > return_traits
Definition rowSums.h:757
return_vector get() const
Definition rowSums.h:679
ColMeansImpl(const MatrixBase< RTYPE, NA, T > &ref_)
Definition rowSums.h:675
const MatrixBase< RTYPE, NA, T > & ref
Definition rowSums.h:668
return_traits::type return_vector
Definition rowSums.h:671
detail::ColMeansReturn< RTYPE > return_traits
Definition rowSums.h:670
traits::storage_type< return_traits::rtype >::type stored_type
Definition rowSums.h:672
ColSumsImpl(const MatrixBase< RTYPE, NA, T > &ref_)
Definition rowSums.h:378
traits::storage_type< return_traits::rtype >::type stored_type
Definition rowSums.h:375
detail::ColSumsReturn< RTYPE > return_traits
Definition rowSums.h:373
const MatrixBase< RTYPE, NA, T > & ref
Definition rowSums.h:371
const MatrixBase< RTYPE, NA, T > & ref
Definition rowSums.h:292
ColSumsImpl(const MatrixBase< RTYPE, NA, T > &ref_)
Definition rowSums.h:299
return_vector get() const
Definition rowSums.h:303
return_traits::type return_vector
Definition rowSums.h:295
traits::storage_type< return_traits::rtype >::type stored_type
Definition rowSums.h:296
detail::ColSumsReturn< RTYPE > return_traits
Definition rowSums.h:294
const MatrixBase< RTYPE, NA, T > & ref
Definition rowSums.h:555
traits::storage_type< return_traits::rtype >::type stored_type
Definition rowSums.h:559
detail::RowMeansReturn< RTYPE > return_traits
Definition rowSums.h:557
RowMeansImpl(const MatrixBase< RTYPE, NA, T > &ref_)
Definition rowSums.h:562
return_traits::type return_vector
Definition rowSums.h:471
const MatrixBase< RTYPE, NA, T > & ref
Definition rowSums.h:468
traits::storage_type< return_traits::rtype >::type stored_type
Definition rowSums.h:472
return_vector get() const
Definition rowSums.h:479
detail::RowMeansReturn< RTYPE > return_traits
Definition rowSums.h:470
RowMeansImpl(const MatrixBase< RTYPE, NA, T > &ref_)
Definition rowSums.h:475
RowSumsImpl(const MatrixBase< RTYPE, NA, T > &ref_)
Definition rowSums.h:205
traits::storage_type< return_traits::rtype >::type stored_type
Definition rowSums.h:202
const MatrixBase< RTYPE, NA, T > & ref
Definition rowSums.h:198
detail::RowSumsReturn< RTYPE > return_traits
Definition rowSums.h:200
const MatrixBase< RTYPE, NA, T > & ref
Definition rowSums.h:111
return_vector get() const
Definition rowSums.h:122
traits::storage_type< return_traits::rtype >::type stored_type
Definition rowSums.h:115
detail::RowSumsReturn< RTYPE > return_traits
Definition rowSums.h:113
RowSumsImpl(const MatrixBase< RTYPE, NA, T > &ref_)
Definition rowSums.h:118
return_traits::type return_vector
Definition rowSums.h:114
void incr(double *lhs, double rhs)
Definition rowSums.h:31
void div(double *lhs, R_xlen_t rhs)
Definition rowSums.h:45
void set_nan(double *x)
Definition rowSums.h:55
bool is_na(typename storage_type< RTYPE >::type)
Definition is_na.h:32
Rcpp API.
Definition algo.h:28
sugar::detail::ColSumsReturn< RTYPE >::type colSums(const MatrixBase< RTYPE, NA, T > &x, bool na_rm=false)
Definition rowSums.h:873
sugar::detail::ColMeansReturn< RTYPE >::type colMeans(const MatrixBase< RTYPE, NA, T > &x, bool na_rm=false)
Definition rowSums.h:891
sugar::detail::RowSumsReturn< RTYPE >::type rowSums(const MatrixBase< RTYPE, NA, T > &x, bool na_rm=false)
Definition rowSums.h:864
static Na_Proxy NA
Definition Na_Proxy.h:52
sugar::detail::RowMeansReturn< RTYPE >::type rowMeans(const MatrixBase< RTYPE, NA, T > &x, bool na_rm=false)
Definition rowSums.h:882
#define ROW_SUMS_IMPL_RMNA(__RTYPE__)
Definition rowSums.h:233
#define ROW_MEANS_IMPL_KEEPNA(__RTYPE__)
Definition rowSums.h:502
#define ROW_SUMS_IMPL_KEEPNA(__RTYPE__)
Definition rowSums.h:149
#define COL_SUMS_IMPL_RMNA(__RTYPE__)
Definition rowSums.h:406
#define ROW_MEANS_IMPL_RMNA(__RTYPE__)
Definition rowSums.h:601
#define COL_MEANS_IMPL_KEEPNA(__RTYPE__)
Definition rowSums.h:702
#define COL_SUMS_IMPL_KEEPNA(__RTYPE__)
Definition rowSums.h:322
#define COL_MEANS_IMPL_RMNA(__RTYPE__)
Definition rowSums.h:801
#define RCPP_SAFE_ADD(a, b)
Definition safe_math.h:33