24SEXP
dense_sum(SEXP obj,
const char *
class,
int narm)
26 int *pdim =
DIM(obj), m = pdim[0], n = pdim[1];
28 char ul =
'\0', ct =
'\0', nu =
'\0';
31 if (
class[1] ==
's' &&
class[0] ==
'z')
38 int i, j, packed =
class[2] ==
'p',
39 sy =
class[1] ==
's', he = sy && ct ==
'C',
40 un =
class[1] ==
't' && nu !=
'N';
44 if (class[1] == 'g') { \
45 for (j = 0; j < n; ++j) \
46 SUM_KERNEL(for (i = 0; i < m; ++i)); \
47 } else if (class[1] == 's' || nu == 'N') { \
49 for (j = 0; j < n; ++j) { \
50 SUM_KERNEL(for (i = 0; i <= j; ++i)); \
55 for (j = 0; j < n; ++j) { \
58 SUM_KERNEL(for (i = j; i < n; ++i)); \
62 for (j = 0; j < n; ++j) { \
63 SUM_KERNEL(for (i = 0; i < j; ++i)); \
69 for (j = 0; j < n; ++j) { \
73 SUM_KERNEL(for (i = j + 1; i < n; ++i)); \
82 int_fast64_t s = (un) ? n : 0;
84#define SUM_KERNEL(__for__) \
88 s += (sy && i != j) ? 2 : 1; \
98 ans = Rf_ScalarInteger((
int) s);
100 ans = Rf_ScalarReal((
double) s);
106 int *px = (
class[0] ==
'l') ? LOGICAL(x) : INTEGER(x);
107 int_fast64_t s = (un) ? n : 0, t = 0;
108 unsigned int count = 0;
110#define SUM_KERNEL(__for__) \
113 if (*px != NA_INTEGER) { \
114 unsigned int d = (sy && i != j) ? 2 : 1; \
115 if (count > UINT_MAX - d) \
116 TRY_INCREMENT(s, t, over); \
117 t += (sy && i != j) ? 2LL * *px : *px; \
121 return Rf_ScalarInteger(NA_INTEGER); \
132 if (s > INT_MIN && s <= INT_MAX)
133 ans = Rf_ScalarInteger((
int) s);
135 ans = Rf_ScalarReal((
double) s);
138 px = (
class[0] ==
'l') ? LOGICAL(x) : INTEGER(x);
139 long double lr = (un) ? (
long double) n : 0.0;
141#define SUM_KERNEL(__for__) \
144 if (*px != NA_INTEGER) \
145 lr += (sy && i != j) ? 2.0L * *px : *px; \
147 return Rf_ScalarInteger(NA_INTEGER); \
161 double *px = REAL(x);
162 long double lr = (un) ? (
long double) n : 0.0;
164#define SUM_KERNEL(__for__) \
167 if (!(narm && ISNAN(*px))) \
168 lr += (sy && i != j) ? 2.0L * *px : *px; \
182 Rcomplex *px = COMPLEX(x), tmp;
183 long double lr = (un) ? (
long double) n : 0.0;
184 long double li = 0.0;
186#define SUM_KERNEL(__for__) \
189 if (!(narm && (ISNAN((*px).r) || (!he && ISNAN((*px).i))))) { \
190 lr += (sy && i != j) ? 2.0L * (*px).r : (*px).r; \
192 li += (sy && i != j) ? 2.0L * (*px).i : (*px).i; \
204 ans = Rf_ScalarComplex(tmp);
220 int *pdim =
DIM(obj), m = pdim[0], n = pdim[1];
222 char ul =
'\0', ct =
'\0', nu =
'\0';
225 if (
class[1] ==
's' &&
class[0] ==
'z')
232 int sy =
class[1] ==
's', he = sy && ct ==
'C',
233 un =
class[1] ==
't' && nu !=
'N';
235 if (
class[2] !=
'T') {
243 int *pp = INTEGER(p), *pi = INTEGER(i), j, k, kend;
251 int_fast64_t s = (int_fast64_t) pp[n - 1] + ((un) ? n : 0);
253 int up = (
class[2] ==
'C') == (ul ==
'U');
255 for (j = 0, k = 0; j < n; ++j) {
257 if (k < kend && pi[(up) ? kend - 1 : k] == j)
263 ans = Rf_ScalarInteger((
int) s);
265 ans = Rf_ScalarReal((
double) s);
271 int *px = (
class[0] ==
'l') ? LOGICAL(x) : INTEGER(x);
272 int_fast64_t s = (un) ? n : 0, t = 0;
273 unsigned int count = 0;
274 for (j = 0, k = 0; j < n; ++j) {
277 if (px[k] != NA_INTEGER) {
278 unsigned int d = (sy && pi[k] != j) ? 2 : 1;
279 if (count > UINT_MAX - d)
281 t += (sy && pi[k] != j) ? 2LL * px[k] : px[k];
285 return Rf_ScalarInteger(NA_INTEGER);
290 if (s > INT_MIN && s <= INT_MAX)
291 ans = Rf_ScalarInteger((
int) s);
293 ans = Rf_ScalarReal((
double) s);
297 long double lr = (
long double) s + (
long double) t;
301 if (px[k] != NA_INTEGER)
302 lr += (sy && pi[k] != j) ? 2.0L * px[k] : px[k];
304 return Rf_ScalarInteger(NA_INTEGER);
313 double *px = REAL(x);
314 long double lr = (un) ? (
long double) n : 0.0;
315 for (j = 0, k = 0; j < n; ++j) {
318 if (!(narm && ISNAN(px[k])))
319 lr += (sy && pi[k] != j) ? 2.0L * px[k] : px[k];
328 Rcomplex *px = COMPLEX(x), tmp;
329 long double lr = (un) ? (
long double) n : 0.0;
330 long double li = 0.0;
331 for (j = 0, k = 0; j < n; ++j) {
334 if (!(narm && (ISNAN(px[k].r) || (!he && ISNAN(px[k].i))))) {
335 lr += (sy && pi[k] != j) ? 2.0L * px[k].r : px[k].r;
337 li += (sy && pi[k] != j) ? 2.0L * px[k].i : px[k].i;
344 ans = Rf_ScalarComplex(tmp);
355 int *pi = INTEGER(i), *pj = INTEGER(j);
356 R_xlen_t k, kend = XLENGTH(i);
363 int_fast64_t s = (int_fast64_t) XLENGTH(i) + ((un) ? n : 0);
366 for (k = 0; k < kend; ++k)
371 ans = Rf_ScalarInteger((
int) s);
373 ans = Rf_ScalarReal((
double) s);
379 int *px = (
class[0] ==
'l') ? LOGICAL(x) : INTEGER(x);
380 int_fast64_t s = (un) ? n : 0, t = 0;
381 unsigned int count = 0;
382 for (k = 0; k < kend; ++k)
383 if (px[k] != NA_INTEGER) {
384 unsigned int d = (sy && pi[k] != pj[k]) ? 2 : 1;
385 if (count > UINT_MAX - d)
387 t += (sy && pi[k] != pj[k]) ? 2LL * px[k] : px[k];
391 return Rf_ScalarInteger(NA_INTEGER);
393 if (s > INT_MIN && s <= INT_MAX)
394 ans = Rf_ScalarInteger((
int) s);
396 ans = Rf_ScalarReal((
double) s);
400 long double lr = (
long double) s + (
long double) t;
401 for (; k < kend; ++k) {
402 if (px[k] != NA_INTEGER)
403 lr += (sy && pi[k] != pj[k]) ? 2.0L * px[k] : px[k];
405 return Rf_ScalarInteger(NA_INTEGER);
412 double *px = REAL(x);
413 long double lr = (un) ? (
long double) n : 0.0;
414 for (k = 0; k < kend; ++k)
415 if (!(narm && ISNAN(px[k])))
416 lr += (sy && pi[k] != pj[k]) ? 2.0L * px[k] : px[k];
422 Rcomplex *px = COMPLEX(x), tmp;
423 long double lr = (un) ? (
long double) n : 0.0;
424 long double li = 0.0;
425 for (k = 0; k < kend; ++k)
426 if (!(narm && (ISNAN(px[k].r) || (!he && ISNAN(px[k].i))))) {
427 lr += (sy && pi[k] != pj[k]) ? 2.0L * px[k].r : px[k].r;
429 li += (sy && pi[k] != pj[k]) ? 2.0L * px[k].i : px[k].i;
433 ans = Rf_ScalarComplex(tmp);
663 int *pdim =
DIM(obj), m = pdim[0], n = pdim[1];
665 char ul =
'\0', ct =
'\0', nu =
'\0';
668 if (
class[1] ==
's' &&
class[0] ==
'z')
674 int sy =
class[1] ==
's', he = sy && ct ==
'C',
675 un =
class[1] ==
't' && nu !=
'N';
676 long double lr = 1.0, li = 0.0;
678 int_fast64_t mn = (int_fast64_t) m * n,
679 q = (sy) ? (mn + n) / 2 : ((un) ? mn - n : mn);
681 if (
class[2] !=
'T') {
689 int *pp = INTEGER(p), *pi = INTEGER(i), j, k, kend;
694 int seen0 = 0, i0, i1,
695 up = !sy || (
class[2] ==
'C') == (ul ==
'U'),
696 lo = !sy || (
class[2] ==
'C') == (ul !=
'U');
706 int *px = (
class[0] ==
'l') ? LOGICAL(x) : INTEGER(x);
707 for (j = 0, k = 0; j < n; ++j) {
711 i1 = (lo) ? m : j + 1;
715 if (pi[k] == i0 || (un && i0 == j && pi[k] == ++i0))
722 if (px[k] != NA_INTEGER)
723 lr *= (sy && pi[k] != j)
724 ? (
long double) px[k] * px[k] : px[k];
729 if (!(seen0 || i1 == i0 || (un && i0 == j && i1 == ++i0))) {
738 double *px = REAL(x);
739 for (j = 0, k = 0; j < n; ++j) {
743 i1 = (lo) ? m : j + 1;
747 if (pi[k] == i0 || (un && i0 == j && pi[k] == ++i0))
754 if (!(narm && ISNAN(px[k])))
755 lr *= (sy && pi[k] != j)
756 ? (
long double) px[k] * px[k] : px[k];
759 if (!(seen0 || i1 == i0 || (un && i0 == j && i1 == ++i0))) {
768 Rcomplex *px = COMPLEX(x);
769 long double lr0, li0;
770 for (j = 0, k = 0; j < n; ++j) {
774 i1 = (lo) ? m : j + 1;
778 if (pi[k] == i0 || (un && i0 == j && pi[k] == ++i0))
786 if (!(narm && (ISNAN(px[k].r) || (!(he && pi[k] == j) && ISNAN(px[k].i))))) {
791 lr0 = lr0 * lr0 + li0 * li0;
797 lr = lr0 * px[k].r - li0 * px[k].i;
798 li = li0 * px[k].r + lr0 * px[k].i;
801 lr = lr0 * px[k].r - li0 * px[k].i;
802 li = li0 * px[k].r + lr0 * px[k].i;
808 if (!(seen0 || i1 == i0 || (un && i0 == j && i1 == ++i0))) {
824 int *pi = INTEGER(i), *pj = INTEGER(j);
825 R_xlen_t k, kend = XLENGTH(i);
835 int *px = (
class[0] ==
'l') ? LOGICAL(x) : INTEGER(x);
836 for (k = 0; k < kend; ++k)
837 if (px[k] != NA_INTEGER)
838 lr *= (sy && pi[k] != pj[k])
839 ? (
long double) px[k] * px[k] : px[k];
846 double *px = REAL(x);
847 for (k = 0; k < kend; ++k)
848 if (!(narm && ISNAN(px[k])))
849 lr *= (sy && pi[k] != pj[k])
850 ? (
long double) px[k] * px[k] : px[k];
855 Rcomplex *px = COMPLEX(x);
856 long double lr0, li0;
857 for (k = 0; k < kend; ++k)
858 if (!(narm && (ISNAN(px[k].r) || (!(he && pi[k] == pj[k]) && ISNAN(px[k].i))))) {
861 if (pi[k] != pj[k]) {
863 lr0 = lr0 * lr0 + li0 * li0;
869 lr = lr0 * px[k].r - li0 * px[k].i;
870 li = li0 * px[k].r + lr0 * px[k].i;
871 if (pi[k] != pj[k]) {
873 lr = lr0 * px[k].r - li0 * px[k].i;
874 li = li0 * px[k].r + lr0 * px[k].i;
893 ans = Rf_ScalarComplex(tmp);