Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

row/col-Sums/Means and unit tests - for #549#551

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
eddelbuettel merged 2 commits intoRcppCore:masterfromnathan-russell:master
Sep 5, 2016
Merged
Changes from1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
PrevPrevious commit
Cleanup for#551
  • Loading branch information
@nathan-russell
nathan-russell committedSep 5, 2016
commit43ac7c3f06ed2e24157f8b86d59bbb25379faa5e
114 changes: 56 additions & 58 deletionsinst/include/Rcpp/sugar/functions/rowSums.h
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -48,37 +48,37 @@ inline bool check_na(Rcomplex x) {
}


inline void incr(double& lhs, double rhs) {
lhs += rhs;
inline void incr(double* lhs, double rhs) {
*lhs += rhs;
}

inline void incr(int& lhs, int rhs) {
lhs += rhs;
inline void incr(int* lhs, int rhs) {
*lhs += rhs;
}

inline void incr(Rcomplex& lhs, const Rcomplex& rhs) {
lhs.r += rhs.r;
lhs.i += rhs.i;
inline void incr(Rcomplex* lhs, const Rcomplex& rhs) {
lhs->r += rhs.r;
lhs->i += rhs.i;
}


inline void div(double& lhs, R_xlen_t rhs) {
lhs /= rhs;
inline void div(double* lhs, R_xlen_t rhs) {
*lhs /= rhs;
}

inline void div(Rcomplex& lhs, R_xlen_t rhs) {
lhs.r /= rhs;
lhs.i /= rhs;
inline void div(Rcomplex* lhs, R_xlen_t rhs) {
lhs->r /= rhs;
lhs->i /= rhs;
}


inline void set_nan(double& x) {
x = R_NaN;
inline void set_nan(double* x) {
*x = R_NaN;
}

inline void set_nan(Rcomplex& x) {
x.r = R_NaN;
x.i = R_NaN;
inline void set_nan(Rcomplex* x) {
x->r = R_NaN;
x->i = R_NaN;
}


Expand DownExpand Up@@ -145,7 +145,7 @@ class RowSumsImpl :

for (j = 0; j < nc; j++) {
for (i = 0; i < nr; i++) {
detail::incr(res[i], ref(i, j));
detail::incr(&res[i], ref(i, j));
}
}

Expand DownExpand Up@@ -198,7 +198,7 @@ public:
if (detail::check_na(ref(i, j))) { \
na_flags[i].x |= 0x1; \
} \
detail::incr(res[i], ref(i, j)); \
detail::incr(&res[i], ref(i, j)); \
} \
} \
\
Expand All@@ -215,6 +215,7 @@ public:
ROW_SUMS_IMPL_KEEPNA(LGLSXP)
ROW_SUMS_IMPL_KEEPNA(INTSXP)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Same#undef here?


#undef ROW_SUMS_IMPL_KEEPNA

// RowSums
// na.rm = TRUE
Expand DownExpand Up@@ -246,7 +247,7 @@ class RowSumsImpl<RTYPE, NA, T, true> :
for (i = 0; i < nr; i++) {
current = ref(i, j);
if (!detail::check_na(current)) {
detail::incr(res[i], current);
detail::incr(&res[i], current);
}
}
}
Expand DownExpand Up@@ -287,7 +288,7 @@ public:
for (i = 0; i < nr; i++) { \
current = ref(i, j); \
if (!detail::check_na(current)) { \
detail::incr(res[i], current); \
detail::incr(&res[i], current); \
} \
} \
} \
Expand All@@ -299,6 +300,7 @@ public:
ROW_SUMS_IMPL_RMNA(LGLSXP)
ROW_SUMS_IMPL_RMNA(INTSXP)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Let's#undef the macro after we're done with it.

eddelbuettel reacted with thumbs up emoji

#undef ROW_SUMS_IMPL_RMNA

// RowSums
// Input with template parameter NA = false
Expand DownExpand Up@@ -335,7 +337,7 @@ class ColSumsImpl :

for (j = 0; j < nc; j++) {
for (i = 0; i < nr; i++) {
detail::incr(res[j], ref(i, j));
detail::incr(&res[j], ref(i, j));
}
}

Expand DownExpand Up@@ -380,7 +382,7 @@ public:
if (detail::check_na(ref(i, j))) { \
na_flags[j].x |= 0x1; \
} \
detail::incr(res[j], ref(i, j)); \
detail::incr(&res[j], ref(i, j)); \
} \
} \
\
Expand All@@ -397,7 +399,8 @@ public:
COL_SUMS_IMPL_KEEPNA(LGLSXP)
COL_SUMS_IMPL_KEEPNA(INTSXP)


#undef COL_SUMS_IMPL_KEEPNA

// ColSums
// na.rm = TRUE
// default input
Expand DownExpand Up@@ -428,7 +431,7 @@ class ColSumsImpl<RTYPE, NA, T, true> :
for (i = 0; i < nr; i++) {
current = ref(i, j);
if (!detail::check_na(current)) {
detail::incr(res[j], current);
detail::incr(&res[j], current);
}
}
}
Expand DownExpand Up@@ -469,7 +472,7 @@ public:
for (i = 0; i < nr; i++) { \
current = ref(i, j); \
if (!detail::check_na(current)) { \
detail::incr(res[j], current); \
detail::incr(&res[j], current); \
} \
} \
} \
Expand All@@ -481,6 +484,7 @@ public:
COL_SUMS_IMPL_RMNA(LGLSXP)
COL_SUMS_IMPL_RMNA(INTSXP)

#undef COL_SUMS_IMPL_RMNA

// ColSums
// Input with template parameter NA = false
Expand DownExpand Up@@ -520,12 +524,12 @@ class RowMeansImpl :

for (j = 0; j < nc; j++) {
for (i = 0; i < nr; i++) {
detail::incr(res[i], ref(i, j));
detail::incr(&res[i], ref(i, j));
}
}

for (i = 0; i < nr; i++) {
detail::div(res[i], nc);
detail::div(&res[i], nc);
}

return res;
Expand DownExpand Up@@ -569,13 +573,13 @@ public:
if (detail::check_na(ref(i, j))) { \
na_flags[i].x |= 0x1; \
} \
detail::incr(res[i], ref(i, j)); \
detail::incr(&res[i], ref(i, j)); \
} \
} \
\
for (i = 0; i < nr; i++) { \
if (!na_flags[i].x) { \
detail::div(res[i], nc); \
detail::div(&res[i], nc); \
} else { \
res[i] = NA_REAL; \
} \
Expand All@@ -588,7 +592,8 @@ public:
ROW_MEANS_IMPL_KEEPNA(LGLSXP)
ROW_MEANS_IMPL_KEEPNA(INTSXP)


#undef ROW_MEANS_IMPL_KEEPNA

// RowMeans
// na.rm = TRUE
// default input
Expand DownExpand Up@@ -621,17 +626,17 @@ class RowMeansImpl<RTYPE, NA, T, true> :
for (i = 0; i < nr; i++) {
current = ref(i, j);
if (!detail::check_na(current)) {
detail::incr(res[i], ref(i, j));
detail::incr(&res[i], ref(i, j));
++n_ok[i];
}
}
}

for (i = 0; i < nr; i++) {
if (n_ok[i]) {
detail::div(res[i], n_ok[i]);
detail::div(&res[i], n_ok[i]);
} else {
detail::set_nan(res[i]);
detail::set_nan(&res[i]);
}
}

Expand DownExpand Up@@ -670,17 +675,17 @@ public:
for (j = 0; j < nc; j++) { \
for (i = 0; i < nr; i++) { \
if (!detail::check_na(ref(i, j))) { \
detail::incr(res[i], ref(i, j)); \
detail::incr(&res[i], ref(i, j)); \
++n_ok[i]; \
} \
} \
} \
\
for (i = 0; i < nr; i++) { \
if (n_ok[i]) { \
detail::div(res[i], n_ok[i]); \
detail::div(&res[i], n_ok[i]); \
} else { \
detail::set_nan(res[i]); \
detail::set_nan(&res[i]); \
} \
} \
\
Expand All@@ -691,6 +696,7 @@ public:
ROW_MEANS_IMPL_RMNA(LGLSXP)
ROW_MEANS_IMPL_RMNA(INTSXP)

#undef ROW_MEANS_IMPL_RMNA

// RowMeans
// Input with template parameter NA = false
Expand DownExpand Up@@ -727,12 +733,12 @@ class ColMeansImpl :

for (j = 0; j < nc; j++) {
for (i = 0; i < nr; i++) {
detail::incr(res[j], ref(i, j));
detail::incr(&res[j], ref(i, j));
}
}

for (j = 0; j < nc; j++) {
detail::div(res[j], nr);
detail::div(&res[j], nr);
}

return res;
Expand DownExpand Up@@ -776,13 +782,13 @@ public:
if (detail::check_na(ref(i, j))) { \
na_flags[j].x |= 0x1; \
} \
detail::incr(res[j], ref(i, j)); \
detail::incr(&res[j], ref(i, j)); \
} \
} \
\
for (j = 0; j < nc; j++) { \
if (!na_flags[j].x) { \
detail::div(res[j], nr); \
detail::div(&res[j], nr); \
} else { \
res[j] = NA_REAL; \
} \
Expand All@@ -795,6 +801,7 @@ public:
COL_MEANS_IMPL_KEEPNA(LGLSXP)
COL_MEANS_IMPL_KEEPNA(INTSXP)

#undef COL_MEANS_IMPL_KEEPNA

// ColMeans
// na.rm = TRUE
Expand DownExpand Up@@ -828,17 +835,17 @@ class ColMeansImpl<RTYPE, NA, T, true> :
for (i = 0; i < nr; i++) {
current = ref(i, j);
if (!detail::check_na(current)) {
detail::incr(res[j], ref(i, j));
detail::incr(&res[j], ref(i, j));
++n_ok[j];
}
}
}

for (j = 0; j < nc; j++) {
if (n_ok[j]) {
detail::div(res[j], n_ok[j]);
detail::div(&res[j], n_ok[j]);
} else {
detail::set_nan(res[j]);
detail::set_nan(&res[j]);
}
}

Expand DownExpand Up@@ -877,17 +884,17 @@ public:
for (j = 0; j < nc; j++) { \
for (i = 0; i < nr; i++) { \
if (!detail::check_na(ref(i, j))) { \
detail::incr(res[j], ref(i, j)); \
detail::incr(&res[j], ref(i, j)); \
++n_ok[j]; \
} \
} \
} \
\
for (j = 0; j < nc; j++) { \
if (n_ok[j]) { \
detail::div(res[j], n_ok[j]); \
detail::div(&res[j], n_ok[j]); \
} else { \
detail::set_nan(res[j]); \
detail::set_nan(&res[j]); \
} \
} \
\
Expand All@@ -898,7 +905,8 @@ public:
COL_MEANS_IMPL_RMNA(LGLSXP)
COL_MEANS_IMPL_RMNA(INTSXP)


#undef COL_MEANS_IMPL_RMNA

// ColMeans
// Input with template parameter NA = false
// ColMeansImpl<..., NA_RM = false>
Expand DownExpand Up@@ -948,16 +956,6 @@ colMeans(const MatrixBase<RTYPE, NA, T>& x, bool na_rm = false) {
}


#undef ROW_SUMS_IMPL_KEEPNA
#undef ROW_SUMS_IMPL_RMNA
#undef COL_SUMS_IMPL_KEEPNA
#undef COL_SUMS_IMPL_RMNA
#undef ROW_MEANS_IMPL_KEEPNA
#undef ROW_MEANS_IMPL_RMNA
#undef COL_MEANS_IMPL_KEEPNA
#undef COL_MEANS_IMPL_RMNA


} // Rcpp

#endif // Rcpp__sugar__rowSums_h

[8]ページ先頭

©2009-2025 Movatter.jp