/src/r-source/src/main/summary.c
Line | Count | Source |
1 | | /* |
2 | | * R : A Computer Language for Statistical Data Analysis |
3 | | * Copyright (C) 1997--2025 The R Core Team |
4 | | * Copyright (C) 1995, 1996 Robert Gentleman and Ross Ihaka |
5 | | * |
6 | | * This program is free software; you can redistribute it and/or modify |
7 | | * it under the terms of the GNU General Public License as published by |
8 | | * the Free Software Foundation; either version 2 of the License, or |
9 | | * (at your option) any later version. |
10 | | * |
11 | | * This program is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | * GNU General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU General Public License |
17 | | * along with this program; if not, a copy is available at |
18 | | * https://www.R-project.org/Licenses/ |
19 | | */ |
20 | | |
21 | | #ifdef HAVE_CONFIG_H |
22 | | #include <config.h> |
23 | | #endif |
24 | | |
25 | | #include <Defn.h> |
26 | | #include <Internal.h> |
27 | | #include <R_ext/Itermacros.h> |
28 | | |
29 | | #include <float.h> // for DBL_MAX |
30 | | |
31 | | #include "duplicate.h" |
32 | | |
33 | 3 | #define R_MSG_type _("invalid 'type' (%s) of argument") |
34 | 5.24k | #define imax2(x, y) ((x < y) ? y : x) |
35 | | |
36 | 1.21M | #define R_INT_MIN (1+INT_MIN) |
37 | | /* since INT_MIN is the NA_INTEGER value ! */ |
38 | 391k | #define Int2Real(i) ((i == NA_INTEGER) ? NA_REAL : (double)i) |
39 | | |
40 | | #ifdef DEBUG_sum |
41 | | #define DbgP1(s) REprintf(s) |
42 | | #define DbgP2(s,a) REprintf(s,a) |
43 | | #define DbgP3(s,a,b) REprintf(s,a,b) |
44 | | #else |
45 | | #define DbgP1(s) |
46 | | #define DbgP2(s,a) |
47 | | #define DbgP3(s,a,b) |
48 | | #endif |
49 | | |
50 | | #ifdef LONG_INT // defined in Defn.h |
51 | 4.99M | # define isum_INT LONG_INT |
52 | | static int isum(SEXP sx, isum_INT *value, bool narm, SEXP call) |
53 | 572k | { |
54 | 572k | LONG_INT s = 0; // at least 64-bit |
55 | 572k | int updated = 0; |
56 | 572k | #ifdef LONG_VECTOR_SUPPORT |
57 | 572k | int ii = R_INT_MIN; // need > 2^32 entries to overflow; checking earlier is a waste |
58 | | /* NOTE: cannot use 64-bit *value to pass NA_INTEGER: that is "regular" 64bit int |
59 | | * -> pass the NA information via return value ('updated'). |
60 | | * After the first 2^32 entries, only check every 1000th time (related to GET_REGION_BUFSIZE=512 ?) |
61 | | * Assume LONG_INT_MAX >= 2^63-1 >=~ 9.223e18 > (1000 * 9000..0L = 9 * 10^18) |
62 | | */ |
63 | 572k | # define ISUM_OVERFLOW_CHECK do { \ |
64 | 572k | if (ii++ > 1000) { \ |
65 | 572k | if (s > 9000000000000000L || s < -9000000000000000L) { \ |
66 | 572k | DbgP2("|OVERFLOW triggered: s=%ld|", s); \ |
67 | | /* *value = s; no use, TODO continue from 'k' */ \ |
68 | 572k | return 42; /* was overflow, NA; now switch to irsum()*/ \ |
69 | 572k | } \ |
70 | 572k | ii = 0; \ |
71 | 572k | } \ |
72 | 572k | } while (0) |
73 | | #else |
74 | | # define ISUM_OVERFLOW_CHECK do { } while(0) |
75 | | #endif |
76 | | |
77 | | /**** assumes INTEGER(sx) and LOGICAL(sx) are identical!! */ |
78 | 572k | ITERATE_BY_REGION(sx, x, i, nbatch, int, INTEGER, { |
79 | 572k | for (R_xlen_t k = 0; k < nbatch; k++) { |
80 | 572k | if (x[k] != NA_INTEGER) { |
81 | 572k | if(!updated) updated = 1; |
82 | 572k | s += x[k]; |
83 | 572k | ISUM_OVERFLOW_CHECK; |
84 | 572k | } else if (!narm) { |
85 | | // updated = NA_INTEGER; |
86 | 572k | return NA_INTEGER; |
87 | 572k | } |
88 | 572k | } |
89 | 572k | }); |
90 | 568k | *value = s; |
91 | 568k | return updated; |
92 | 572k | #undef ISUM_OVERFLOW_CHECK |
93 | 572k | } |
94 | | #else // no LONG_INT : should never be used with a C99/C11 compiler |
95 | | # define isum_INT int |
96 | | static bool isum(SEXP sx, isum_INT *value, bool narm, SEXP call) |
97 | | /* Version from R 3.0.0 */ |
98 | | { |
99 | | double s = 0.0; |
100 | | bool updated = false; |
101 | | |
102 | | /**** assumes INTEGER(sx) and LOGICAL(sx) are identical!! */ |
103 | | ITERATE_BY_REGION(sx, x, i, nbatch, int, INTEGER, { |
104 | | for (R_xlen_t k = 0; k < nbatch; k++) { |
105 | | if (x[k] != NA_INTEGER) { |
106 | | if(!updated) updated = true; |
107 | | s += x[k]; |
108 | | } else if (!narm) { |
109 | | if(!updated) updated = true; |
110 | | *value = NA_INTEGER; |
111 | | return updated; |
112 | | } |
113 | | } |
114 | | }); |
115 | | if(s > INT_MAX || s < R_INT_MIN){ |
116 | | warningcall(call, _("integer overflow - use sum(as.numeric(.))")); |
117 | | *value = NA_INTEGER; |
118 | | } |
119 | | else *value = (int) s; |
120 | | |
121 | | return updated; |
122 | | } |
123 | | #endif |
124 | | |
125 | | // Used instead of isum() for large vectors when overflow would occur: |
126 | | static bool risum(SEXP sx, double *value, bool narm) |
127 | 0 | { |
128 | 0 | LDOUBLE s = 0.0; |
129 | 0 | bool updated = false; |
130 | | |
131 | | /**** assumes INTEGER(sx) and LOGICAL(sx) are identical!! */ |
132 | 0 | ITERATE_BY_REGION(sx, x, i, nbatch, int, INTEGER, { |
133 | 0 | for (R_xlen_t k = 0; k < nbatch; k++) { |
134 | 0 | if (x[k] != NA_INTEGER) { |
135 | 0 | if(!updated) updated = true; |
136 | 0 | s += (double) x[k]; |
137 | 0 | } else if (!narm) { |
138 | 0 | if(!updated) updated = true; |
139 | 0 | *value = NA_REAL; |
140 | 0 | return updated; |
141 | 0 | } |
142 | 0 | } |
143 | 0 | }); |
144 | 0 | if(s > DBL_MAX) *value = R_PosInf; |
145 | 0 | else if (s < -DBL_MAX) *value = R_NegInf; |
146 | 0 | else *value = (double) s; |
147 | |
|
148 | 0 | return updated; |
149 | 0 | } |
150 | | |
151 | | |
152 | | static bool rsum(SEXP sx, double *value, bool narm) |
153 | 1.59M | { |
154 | 1.59M | LDOUBLE s = 0.0; |
155 | 1.59M | bool updated = false; |
156 | | |
157 | 1.59M | ITERATE_BY_REGION(sx, x, i, nbatch, double, REAL, { |
158 | 1.59M | for (R_xlen_t k = 0; k < nbatch; k++) { |
159 | 1.59M | if (!narm || !ISNAN(x[k])) { |
160 | 1.59M | if(!updated) updated = true; |
161 | 1.59M | s += x[k]; |
162 | 1.59M | } |
163 | 1.59M | } |
164 | 1.59M | }); |
165 | 1.59M | if(s > DBL_MAX) *value = R_PosInf; |
166 | 1.59M | else if (s < -DBL_MAX) *value = R_NegInf; |
167 | 1.59M | else *value = (double) s; |
168 | | |
169 | 1.59M | return updated; |
170 | 1.59M | } |
171 | | |
172 | | static bool csum(SEXP sx, Rcomplex *value, bool narm) |
173 | 847k | { |
174 | 847k | Rcomplex *x = COMPLEX(sx); |
175 | 847k | R_xlen_t n = XLENGTH(sx); |
176 | 847k | LDOUBLE sr = 0.0, si = 0.0; |
177 | 847k | bool updated = false; |
178 | | |
179 | 1.91M | for (R_xlen_t k = 0; k < n; k++) { |
180 | 1.07M | if (!narm || (!ISNAN(x[k].r) && !ISNAN(x[k].i))) { |
181 | 1.07M | if(!updated) updated = true; |
182 | 1.07M | sr += x[k].r; |
183 | 1.07M | si += x[k].i; |
184 | 1.07M | } |
185 | 1.07M | } |
186 | 847k | value->r = (double) sr; |
187 | 847k | value->i = (double) si; |
188 | | |
189 | 847k | return updated; |
190 | 847k | } |
191 | | |
192 | | static bool imin(SEXP sx, int *value, bool narm) |
193 | 170k | { |
194 | 170k | bool updated = false; |
195 | 170k | int s = 0; |
196 | | |
197 | 170k | ITERATE_BY_REGION(sx, x, i, nbatch, int, INTEGER, { |
198 | 170k | for (R_xlen_t k = 0; k < nbatch; k++) { |
199 | 170k | if (x[k] != NA_INTEGER) { |
200 | 170k | if (!updated || s > x[k]) { |
201 | 170k | s = x[k]; |
202 | 170k | if(!updated) updated = true; |
203 | 170k | } |
204 | 170k | } |
205 | 170k | else if (!narm) { |
206 | 170k | *value = NA_INTEGER; |
207 | 170k | return(true); |
208 | 170k | } |
209 | 170k | } |
210 | 170k | }); |
211 | 170k | *value = s; |
212 | 170k | return updated; |
213 | 170k | } |
214 | | |
215 | | static bool rmin(SEXP sx, double *value, bool narm) |
216 | 1.81k | { |
217 | 1.81k | double s = 0.0; /* -Wall */ |
218 | 1.81k | bool updated = false; |
219 | | |
220 | | /* s = R_PosInf; */ |
221 | 1.81k | ITERATE_BY_REGION(sx, x, i, nbatch, double, REAL, { |
222 | 1.81k | for (R_xlen_t k = 0; k < nbatch; k++) { |
223 | 1.81k | if (ISNAN(x[k])) {/* Na(N) */ |
224 | 1.81k | if (!narm) { |
225 | 1.81k | if(!ISNA(s)) s = x[k]; /* so any NA trumps all NaNs */ |
226 | 1.81k | if(!updated) updated = true; |
227 | 1.81k | } |
228 | 1.81k | } |
229 | 1.81k | else if (!updated || x[k] < s) { /* Never true if s is NA/NaN */ |
230 | 1.81k | s = x[k]; |
231 | 1.81k | if(!updated) updated = true; |
232 | 1.81k | } |
233 | 1.81k | } |
234 | 1.81k | }); |
235 | 1.81k | *value = s; |
236 | 1.81k | return updated; |
237 | 1.81k | } |
238 | | |
239 | | static bool smin(SEXP x, SEXP *value, bool narm) |
240 | 0 | { |
241 | 0 | SEXP s = NA_STRING; /* -Wall */ |
242 | 0 | bool updated = false; |
243 | 0 | const void *vmax = vmaxget(); // precautionary for Scollate |
244 | |
|
245 | 0 | for (R_xlen_t i = 0; i < XLENGTH(x); i++) { |
246 | 0 | if (STRING_ELT(x, i) != NA_STRING) { |
247 | 0 | if (!updated || |
248 | 0 | (s != STRING_ELT(x, i) && Scollate(s, STRING_ELT(x, i)) > 0)) { |
249 | 0 | s = STRING_ELT(x, i); |
250 | 0 | if(!updated) updated = true; |
251 | 0 | } |
252 | 0 | } |
253 | 0 | else if (!narm) { |
254 | 0 | *value = NA_STRING; |
255 | 0 | return(true); |
256 | 0 | } |
257 | 0 | } |
258 | 0 | *value = s; |
259 | |
|
260 | 0 | vmaxset(vmax); |
261 | 0 | return updated; |
262 | 0 | } |
263 | | |
264 | | static bool imax(SEXP sx, int *value, bool narm) |
265 | 1.07M | { |
266 | 1.07M | int s = 0 /* -Wall */; |
267 | 1.07M | bool updated = false; |
268 | | |
269 | 1.07M | ITERATE_BY_REGION(sx, x, i, nbatch, int, INTEGER, { |
270 | 1.07M | for (R_xlen_t k = 0; k < nbatch; k++) { |
271 | 1.07M | if (x[k] != NA_INTEGER) { |
272 | 1.07M | if (!updated || s < x[k]) { |
273 | 1.07M | s = x[k]; |
274 | 1.07M | if(!updated) updated = true; |
275 | 1.07M | } |
276 | 1.07M | } else if (!narm) { |
277 | 1.07M | *value = NA_INTEGER; |
278 | 1.07M | return(true); |
279 | 1.07M | } |
280 | 1.07M | } |
281 | 1.07M | }); |
282 | 1.07M | *value = s; |
283 | 1.07M | return updated; |
284 | 1.07M | } |
285 | | |
286 | | static bool rmax(SEXP sx, double *value, bool narm) |
287 | 391k | { |
288 | 391k | double s = 0.0 /* -Wall */; |
289 | 391k | bool updated = false; |
290 | | |
291 | 391k | ITERATE_BY_REGION(sx, x, iii, nbatch, double, REAL, { |
292 | 391k | for (R_xlen_t k = 0; k < nbatch; k++) { |
293 | 391k | if (ISNAN(x[k])) {/* Na(N) */ |
294 | 391k | if (!narm) { |
295 | 391k | if(!ISNA(s)) s = x[k]; /* so any NA trumps all NaNs */ |
296 | 391k | if(!updated) updated = true; |
297 | 391k | } |
298 | 391k | } |
299 | 391k | else if (!updated || x[k] > s) { /* Never true if s is NA/NaN */ |
300 | 391k | s = x[k]; |
301 | 391k | if(!updated) updated = true; |
302 | 391k | } |
303 | 391k | } |
304 | 391k | }); |
305 | 391k | *value = s; |
306 | 391k | return updated; |
307 | 391k | } |
308 | | |
309 | | static bool smax(SEXP x, SEXP *value, bool narm) |
310 | 3 | { |
311 | 3 | SEXP s = NA_STRING; /* -Wall */ |
312 | 3 | bool updated = false; |
313 | 3 | const void *vmax = vmaxget(); // precautionary for Scollate |
314 | | |
315 | 6 | for (R_xlen_t i = 0; i < XLENGTH(x); i++) { |
316 | 3 | if (STRING_ELT(x, i) != NA_STRING) { |
317 | 3 | if (!updated || |
318 | 3 | (s != STRING_ELT(x, i) && Scollate(s, STRING_ELT(x, i)) < 0)) { |
319 | 3 | s = STRING_ELT(x, i); |
320 | 3 | if(!updated) updated = true; |
321 | 3 | } |
322 | 3 | } |
323 | 0 | else if (!narm) { |
324 | 0 | *value = NA_STRING; |
325 | 0 | return(true); |
326 | 0 | } |
327 | 3 | } |
328 | 3 | *value = s; |
329 | | |
330 | 3 | vmaxset(vmax); |
331 | 3 | return updated; |
332 | 3 | } |
333 | | |
334 | | static bool iprod(SEXP sx, double *value, bool narm) |
335 | 102k | { |
336 | 102k | LDOUBLE s = 1.0; |
337 | 102k | bool updated = false; |
338 | | |
339 | | /**** assumes INTEGER(sx) and LOGICAL(sx) are identical!! */ |
340 | 102k | ITERATE_BY_REGION(sx, x, i, nbatch, int, INTEGER, { |
341 | 102k | for (R_xlen_t k = 0; k < nbatch; k++) { |
342 | 102k | if (x[k] != NA_INTEGER) { |
343 | 102k | s *= x[k]; |
344 | 102k | if(!updated) updated = true; |
345 | 102k | } |
346 | 102k | else if (!narm) { |
347 | 102k | if(!updated) updated = true; |
348 | 102k | *value = NA_REAL; |
349 | 102k | return updated; |
350 | 102k | } |
351 | | |
352 | 102k | if(ISNAN(s)) { /* how can this happen? */ |
353 | 102k | *value = NA_REAL; |
354 | 102k | return updated; |
355 | 102k | } |
356 | 102k | } |
357 | 102k | }); |
358 | | |
359 | | // This could over/underflow (does in package POT) |
360 | 101k | if(s > DBL_MAX) *value = R_PosInf; |
361 | 101k | else if (s < -DBL_MAX) *value = R_NegInf; |
362 | 101k | else *value = (double) s; |
363 | | |
364 | 101k | return updated; |
365 | 102k | } |
366 | | |
367 | | static bool rprod(SEXP sx, double *value, bool narm) |
368 | 0 | { |
369 | 0 | LDOUBLE s = 1.0; |
370 | 0 | bool updated = false; |
371 | |
|
372 | 0 | ITERATE_BY_REGION(sx, x, i, nbatch, double, REAL, { |
373 | 0 | for (R_xlen_t k = 0; k < nbatch; k++) { |
374 | 0 | if (!narm || !ISNAN(x[k])) { |
375 | 0 | if(!updated) updated = true; |
376 | 0 | s *= x[k]; |
377 | 0 | } |
378 | 0 | } |
379 | 0 | }); |
380 | |
|
381 | 0 | if(s > DBL_MAX) *value = R_PosInf; |
382 | 0 | else if (s < -DBL_MAX) *value = R_NegInf; |
383 | 0 | else *value = (double) s; |
384 | |
|
385 | 0 | return updated; |
386 | 0 | } |
387 | | |
388 | | static bool cprod(SEXP sx, Rcomplex *value, bool narm) |
389 | 0 | { |
390 | 0 | Rcomplex *x = COMPLEX(sx); |
391 | 0 | R_xlen_t n = XLENGTH(sx); |
392 | 0 | LDOUBLE sr = 1.0, si = 0.0; |
393 | 0 | bool updated = false; |
394 | 0 | for (R_xlen_t k = 0; k < n; k++) { |
395 | 0 | if (!narm || (!ISNAN(x[k].r) && !ISNAN(x[k].i))) { |
396 | 0 | if(!updated) updated = true; |
397 | 0 | LDOUBLE tr = sr, ti = si; |
398 | 0 | sr = tr * x[k].r - ti * x[k].i; |
399 | 0 | si = tr * x[k].i + ti * x[k].r; |
400 | 0 | } |
401 | 0 | } |
402 | 0 | value->r = (double) sr; |
403 | 0 | value->i = (double) si; |
404 | |
|
405 | 0 | return updated; |
406 | 0 | } |
407 | | |
408 | | |
409 | | attribute_hidden |
410 | | SEXP fixup_NaRm(SEXP args) |
411 | 15.8M | { |
412 | | /* Need to make sure na.rm is last and exists */ |
413 | 15.8M | SEXP na_value = ScalarLogical(FALSE); |
414 | 15.8M | bool seen_NaRm = false; |
415 | 33.2M | for(SEXP a = args, prev = R_NilValue; a != R_NilValue; a = CDR(a)) { |
416 | 17.3M | if(TAG(a) == R_NaRmSymbol) { |
417 | 177 | if(seen_NaRm) |
418 | 0 | error(_("formal argument \"%s\" matched by multiple actual arguments"), |
419 | 0 | "na.rm"); |
420 | 177 | seen_NaRm = true; |
421 | 177 | if(CDR(a) == R_NilValue) return args; |
422 | 0 | na_value = CAR(a); |
423 | 0 | if(prev == R_NilValue) args = CDR(a); |
424 | 0 | else SETCDR(prev, CDR(a)); |
425 | 0 | } |
426 | 17.3M | prev = a; |
427 | 17.3M | } |
428 | | |
429 | 15.8M | PROTECT(na_value); |
430 | 15.8M | SEXP t = CONS(na_value, R_NilValue); |
431 | 15.8M | UNPROTECT(1); |
432 | 15.8M | PROTECT(t); |
433 | 15.8M | SET_TAG(t, R_NaRmSymbol); |
434 | 15.8M | if (args == R_NilValue) |
435 | 759k | args = t; |
436 | 15.1M | else { |
437 | 15.1M | SEXP r = args; |
438 | 17.3M | while (CDR(r) != R_NilValue) r = CDR(r); |
439 | 15.1M | SETCDR(r, t); |
440 | 15.1M | } |
441 | 15.8M | UNPROTECT(1); |
442 | 15.8M | return args; |
443 | 15.8M | } |
444 | | |
445 | | /* do_summary provides a variety of data summaries |
446 | | op : 0 = sum, 1 = mean, 2 = min, 3 = max, 4 = prod |
447 | | */ |
448 | | /* NOTE: mean() is rather different as only one arg and no na.rm, and |
449 | | * dispatch is from an R-level generic, this being a special case of |
450 | | * mean.default. |
451 | | */ |
452 | | |
453 | | static R_INLINE SEXP logical_mean(SEXP x) |
454 | 0 | { |
455 | 0 | R_xlen_t n = XLENGTH(x); |
456 | 0 | LDOUBLE s = 0.0; |
457 | 0 | for (R_xlen_t i = 0; i < n; i++) { |
458 | 0 | int xi = LOGICAL_ELT(x, i); |
459 | 0 | if(xi == NA_LOGICAL) |
460 | 0 | return ScalarReal(R_NaReal); |
461 | 0 | s += xi; |
462 | 0 | } |
463 | 0 | return ScalarReal((double) (s/n)); |
464 | 0 | } |
465 | | |
466 | | static R_INLINE SEXP integer_mean(SEXP x) |
467 | 14 | { |
468 | 14 | R_xlen_t n = XLENGTH(x); |
469 | 14 | LDOUBLE s = 0.0; |
470 | 200 | for (R_xlen_t i = 0; i < n; i++) { |
471 | 186 | int xi = INTEGER_ELT(x, i); |
472 | 186 | if(xi == NA_INTEGER) |
473 | 0 | return ScalarReal(R_NaReal); |
474 | 186 | s += xi; |
475 | 186 | } |
476 | 14 | return ScalarReal((double) (s/n)); |
477 | 14 | } |
478 | | |
479 | | static R_INLINE SEXP real_mean(SEXP x) |
480 | 386 | { |
481 | 386 | R_xlen_t n = XLENGTH(x); |
482 | 386 | LDOUBLE s = 0.0; |
483 | 386 | ITERATE_BY_REGION(x, dx, i, nbatch, double, REAL, { |
484 | 386 | for (R_xlen_t k = 0; k < nbatch; k++) |
485 | 386 | s += dx[k]; |
486 | 386 | }); |
487 | 386 | bool finite_s = R_FINITE((double) s) != 0; //isfinite returns non-zero |
488 | 386 | if (finite_s) { |
489 | 344 | s /= n; |
490 | 344 | DbgP3("real_mean(): n=%g, s=%g\n", (double)n, s); |
491 | 344 | } else { // infinite s, maybe just overflowed; try to use smaller terms: |
492 | 42 | DbgP3("real_mean(): n=%g, infinite s=%g -- try again: ", (double)n, s); |
493 | 42 | s = 0.; |
494 | 42 | ITERATE_BY_REGION(x, dx, i, nbatch, double, REAL, { |
495 | 42 | for (R_xlen_t k = 0; k < nbatch; k++) |
496 | 42 | s += dx[k]/n; |
497 | 42 | }); |
498 | 42 | DbgP2(" --> new s=%g\n", s); |
499 | 42 | } |
500 | 386 | if (finite_s && R_FINITE((double) s)) { |
501 | 171 | LDOUBLE t = 0.0; |
502 | 171 | ITERATE_BY_REGION(x, dx, i, nbatch, double, REAL, { |
503 | 171 | for (R_xlen_t k = 0; k < nbatch; k++) |
504 | 171 | t += (dx[k] - s); |
505 | 171 | }); |
506 | 171 | s += t/n; |
507 | 171 | } |
508 | 215 | else if (R_FINITE((double) s)) { // was infinite: more careful |
509 | 8 | LDOUBLE t = 0.0; |
510 | 8 | ITERATE_BY_REGION(x, dx, i, nbatch, double, REAL, { |
511 | 8 | for (R_xlen_t k = 0; k < nbatch; k++) |
512 | 8 | t += (dx[k] - s)/n; |
513 | 8 | }); |
514 | 8 | DbgP2(" s = s + t, t=%g\n", t); |
515 | 8 | s += t; |
516 | 8 | } |
517 | 386 | return ScalarReal((double) s); |
518 | 386 | } |
519 | | |
520 | | static R_INLINE SEXP complex_mean(SEXP x) |
521 | 0 | { |
522 | 0 | R_xlen_t n = XLENGTH(x); |
523 | 0 | LDOUBLE s = 0.0, si = 0.0; |
524 | 0 | Rcomplex *px = COMPLEX(x); |
525 | 0 | for (R_xlen_t i = 0; i < n; i++) { |
526 | 0 | Rcomplex xi = px[i]; |
527 | 0 | s += xi.r; |
528 | 0 | si += xi.i; |
529 | 0 | } |
530 | 0 | s /= n; si /= n; |
531 | 0 | if( R_FINITE((double)s) && R_FINITE((double)si) ) { |
532 | 0 | LDOUBLE t = 0.0, ti = 0.0; |
533 | 0 | for (R_xlen_t i = 0; i < n; i++) { |
534 | 0 | Rcomplex xi = px[i]; |
535 | 0 | t += xi.r - s; |
536 | 0 | ti += xi.i - si; |
537 | 0 | } |
538 | 0 | s += t/n; si += ti/n; |
539 | 0 | } |
540 | 0 | Rcomplex val = { .r = (double)s, .i = (double)si }; |
541 | 0 | return ScalarComplex(val); |
542 | 0 | } |
543 | | |
544 | | attribute_hidden SEXP do_summary(SEXP call, SEXP op, SEXP args, SEXP env) |
545 | 5.00M | { |
546 | 5.00M | checkArity(op, args); |
547 | 5.00M | if(PRIMVAL(op) == 1) { /* mean */ |
548 | 400 | SEXP x = CAR(args); |
549 | 400 | switch(TYPEOF(x)) { |
550 | 0 | case LGLSXP: return logical_mean(x); |
551 | 14 | case INTSXP: return integer_mean(x); |
552 | 386 | case REALSXP: return real_mean(x); |
553 | 0 | case CPLXSXP: return complex_mean(x); |
554 | 0 | default: |
555 | 0 | error(R_MSG_type, R_typeToChar(x)); |
556 | 0 | return R_NilValue; // -Wall on clang 4.2 |
557 | 400 | } |
558 | 400 | } |
559 | | |
560 | 5.00M | SEXP ans, call2; |
561 | | /* match to foo(..., na.rm=FALSE) */ |
562 | 5.00M | PROTECT(args = fixup_NaRm(args)); |
563 | 5.00M | PROTECT(call2 = shallow_duplicate(call)); |
564 | 5.00M | R_args_enable_refcnt(args); |
565 | 5.00M | SETCDR(call2, args); |
566 | | |
567 | 5.00M | if (DispatchGroup("Summary", call2, op, args, env, &ans)) { |
568 | 0 | UNPROTECT(2); /* call2, args */ |
569 | 0 | SETCDR(call2, R_NilValue); /* clear refcnt on args */ |
570 | 0 | R_try_clear_args_refcnt(args); |
571 | 0 | return(ans); |
572 | 0 | } |
573 | 5.00M | UNPROTECT(1); /* call2 */ |
574 | 5.00M | SETCDR(call2, R_NilValue); /* clear refcnt on args */ |
575 | 5.00M | R_try_clear_args_refcnt(args); |
576 | | |
577 | | #ifdef DEBUG_Summary |
578 | | REprintf("C do_summary(op%s, *): did NOT dispatch\n", PRIMNAME(op)); |
579 | | #endif |
580 | | |
581 | 5.00M | ans = matchArgExact(R_NaRmSymbol, &args); |
582 | 5.00M | bool narm = asBool2(ans, call); |
583 | | |
584 | 5.00M | if (ALTREP(CAR(args)) && CDDR(args) == R_NilValue && |
585 | 13.5k | (CDR(args) == R_NilValue || TAG(CDR(args)) == R_NaRmSymbol)) { |
586 | 6.08k | SEXP toret = NULL; |
587 | 6.08k | SEXP vec = CAR(args); |
588 | 6.08k | switch(PRIMVAL(op)) { |
589 | 6.07k | case 0: |
590 | 6.07k | if(TYPEOF(vec) == INTSXP) |
591 | 6.07k | toret = ALTINTEGER_SUM(vec, narm); |
592 | 0 | else if (TYPEOF(vec) == REALSXP) |
593 | 0 | toret = ALTREAL_SUM(vec, narm); |
594 | 6.07k | break; |
595 | 0 | case 2: |
596 | 0 | if(TYPEOF(vec) == INTSXP) |
597 | 0 | toret = ALTINTEGER_MIN(vec, narm); |
598 | 0 | else if (TYPEOF(vec) == REALSXP) |
599 | 0 | toret = ALTREAL_MIN(vec, narm); |
600 | 0 | break; |
601 | 0 | case 3: |
602 | 0 | if(TYPEOF(vec) == INTSXP) |
603 | 0 | toret = ALTINTEGER_MAX(vec, narm); |
604 | 0 | else if (TYPEOF(vec) == REALSXP) |
605 | 0 | toret = ALTREAL_MAX(vec, narm); |
606 | 0 | break; |
607 | 11 | default: |
608 | 11 | break; |
609 | 6.08k | } |
610 | 6.08k | if(toret != NULL) { |
611 | 6.07k | UNPROTECT(1); /* args */ |
612 | 6.07k | return toret; |
613 | 6.07k | } |
614 | 6.08k | } |
615 | | |
616 | 4.99M | bool int_a, real_a, complex_a, |
617 | 4.99M | empty = true;// <==> only zero-length arguments, or NA with na.rm=T |
618 | 4.99M | int updated = 0; // |
619 | | /* updated = NA_INTEGER if encountered NA, |
620 | | updated != 0 , as soon as (i)tmp (do_summary), |
621 | | or *value ([ir]min / max) is assigned; */ |
622 | 4.99M | SEXP a; |
623 | 4.99M | double tmp = 0.0, s; |
624 | 4.99M | Rcomplex ztmp, zcum={.r = 0.0, .i = 0.0} /* -Wall */; |
625 | 4.99M | int itmp = 0, icum = 0, warn = 0 /* dummy */; |
626 | 4.99M | bool use_isum = true; // indicating if isum() should used; otherwise irsum() |
627 | 4.99M | isum_INT iLtmp = (isum_INT)0, iLcum = iLtmp; // for isum() only |
628 | 4.99M | SEXPTYPE ans_type;/* only INTEGER, REAL, COMPLEX or STRSXP here */ |
629 | | |
630 | 4.99M | int iop = PRIMVAL(op); |
631 | 4.99M | switch(iop) { |
632 | 4.27M | case 0:/* sum */ |
633 | | /* we need to find out if _all_ the arguments are integer or logical |
634 | | in advance, as we might overflow before we find out. NULL is |
635 | | documented to be the same as integer(0). |
636 | | */ |
637 | 4.27M | a = args; |
638 | 4.27M | complex_a = real_a = false; |
639 | 8.95M | while (a != R_NilValue) { |
640 | 4.67M | switch(TYPEOF(CAR(a))) { |
641 | 121k | case INTSXP: |
642 | 606k | case LGLSXP: |
643 | 1.95M | case NILSXP: |
644 | 1.95M | break; |
645 | 1.87M | case REALSXP: |
646 | 1.87M | real_a = true; |
647 | 1.87M | break; |
648 | 849k | case CPLXSXP: |
649 | 849k | complex_a = true; |
650 | 849k | break; |
651 | 1 | default: |
652 | 1 | a = CAR(a); goto invalid_type; |
653 | 4.67M | } |
654 | 4.67M | a = CDR(a); |
655 | 4.67M | } |
656 | 4.27M | if(complex_a) { |
657 | 849k | ans_type = CPLXSXP; |
658 | 3.42M | } else if(real_a) { |
659 | 1.22M | ans_type = REALSXP; |
660 | 2.20M | } else { |
661 | 2.20M | ans_type = INTSXP; iLcum = (isum_INT)0; |
662 | 2.20M | } |
663 | 4.27M | DbgP3("do_summary: sum(.. na.rm=%d): ans_type = %s\n", |
664 | 4.27M | narm, type2char(ans_type)); |
665 | 4.27M | zcum.r = zcum.i = 0.; icum = 0; |
666 | 4.27M | break; |
667 | | |
668 | 86.5k | case 2:/* min */ |
669 | 86.5k | DbgP2("do_summary: min(.. na.rm=%d) ", narm); |
670 | 86.5k | ans_type = INTSXP; |
671 | 86.5k | zcum.r = R_PosInf; |
672 | 86.5k | icum = INT_MAX; |
673 | 86.5k | break; |
674 | | |
675 | 515k | case 3:/* max */ |
676 | 515k | DbgP2("do_summary: max(.. na.rm=%d) ", narm); |
677 | 515k | ans_type = INTSXP; |
678 | 515k | zcum.r = R_NegInf;; |
679 | 515k | icum = R_INT_MIN; |
680 | 515k | break; |
681 | | |
682 | 119k | case 4:/* prod */ |
683 | 119k | ans_type = REALSXP; |
684 | 119k | zcum.r = 1.; |
685 | 119k | zcum.i = 0.; |
686 | 119k | break; |
687 | | |
688 | 0 | default: |
689 | 0 | errorcall(call, |
690 | 0 | _("internal error ('op = %d' in do_summary).\t Call a Guru"), |
691 | 0 | iop); |
692 | 0 | return R_NilValue;/*-Wall */ |
693 | 4.99M | } |
694 | | |
695 | 4.99M | SEXP stmp = NA_STRING, |
696 | 4.99M | scum = PROTECT(NA_STRING); |
697 | | /*-- now loop over all arguments. Do the 'op' switch INSIDE : */ |
698 | 11.4M | while (args != R_NilValue) { |
699 | 6.45M | a = CAR(args); |
700 | 6.45M | int_a = false;// int_a = true <--> a is INTEGER |
701 | 6.45M | real_a = false; |
702 | | |
703 | 6.45M | if(xlength(a) > 0) { |
704 | 4.76M | updated = 0;/*- GLOBAL -*/ |
705 | | |
706 | 4.76M | switch(iop) { |
707 | 172k | case 2:/* min */ |
708 | 1.63M | case 3:/* max */ |
709 | | |
710 | 1.63M | switch(TYPEOF(a)) { |
711 | 766k | case LGLSXP: |
712 | 1.24M | case INTSXP: |
713 | 1.24M | int_a = true; |
714 | 1.24M | if (iop == 2) updated = imin(a, &itmp, narm); |
715 | 1.07M | else updated = imax(a, &itmp, narm); |
716 | 1.24M | break; |
717 | 393k | case REALSXP: |
718 | 393k | real_a = true; |
719 | 393k | if(ans_type == INTSXP) {/* change to REAL */ |
720 | 391k | ans_type = REALSXP; |
721 | 391k | if(!empty) zcum.r = Int2Real(icum); |
722 | 391k | } |
723 | 393k | if (iop == 2) updated = rmin(a, &tmp, narm); |
724 | 391k | else updated = rmax(a, &tmp, narm); |
725 | 393k | break; |
726 | 3 | case STRSXP: |
727 | 3 | if(!empty && ans_type == INTSXP) { |
728 | 0 | scum = StringFromInteger(icum, &warn); |
729 | 0 | UNPROTECT(1); /* scum */ |
730 | 0 | PROTECT(scum); |
731 | 3 | } else if(!empty && ans_type == REALSXP) { |
732 | 1 | scum = StringFromReal(zcum.r, &warn); |
733 | 1 | UNPROTECT(1); /* scum */ |
734 | 1 | PROTECT(scum); |
735 | 1 | } |
736 | 3 | ans_type = STRSXP; |
737 | 3 | if (iop == 2) updated = smin(a, &stmp, narm); |
738 | 3 | else updated = smax(a, &stmp, narm); |
739 | 3 | break; |
740 | 1 | default: |
741 | 1 | goto invalid_type; |
742 | 1.63M | } |
743 | | |
744 | 1.63M | if(updated) {/* 'a' had non-NA elements; --> "add" tmp or itmp*/ |
745 | 1.63M | DbgP1(" updated:"); |
746 | 1.63M | if(ans_type == INTSXP) { |
747 | 1.24M | DbgP3(" INT: (old)icum= %ld, itmp=%ld\n", icum,itmp); |
748 | 1.24M | if (icum == NA_INTEGER); /* NA trumps anything */ |
749 | 1.24M | else if (itmp == NA_INTEGER || |
750 | 1.24M | (iop == 2 && itmp < icum) || /* min */ |
751 | 1.14M | (iop == 3 && itmp > icum)) /* max */ |
752 | 995k | icum = itmp; |
753 | 1.24M | } else if(ans_type == REALSXP) { |
754 | 396k | if (int_a) tmp = Int2Real(itmp); |
755 | 396k | DbgP3(" REAL: (old)cum= %g, tmp=%g\n", zcum.r,tmp); |
756 | 396k | if (ISNA(zcum.r)); /* NA trumps anything */ |
757 | 396k | else if (ISNAN(tmp)) { |
758 | 3 | if (ISNA(tmp)) zcum.r = tmp; |
759 | 1 | else zcum.r += tmp;/* NA or NaN */ |
760 | 396k | } else if( |
761 | 396k | (iop == 2 && tmp < zcum.r) || |
762 | 395k | (iop == 3 && tmp > zcum.r)) zcum.r = tmp; |
763 | 396k | } else if(ans_type == STRSXP) { |
764 | 3 | if(int_a) |
765 | 0 | stmp = StringFromInteger(itmp, &warn); |
766 | 3 | else if(real_a) |
767 | 0 | stmp = StringFromReal(tmp, &warn); |
768 | | |
769 | 3 | if(empty) |
770 | 1 | scum = stmp; |
771 | 2 | else if (scum != NA_STRING) { |
772 | 2 | PROTECT(stmp); |
773 | 2 | if(stmp == NA_STRING || |
774 | 2 | (iop == 2 && stmp != scum && Scollate(stmp, scum) < 0) || |
775 | 2 | (iop == 3 && stmp != scum && Scollate(stmp, scum) > 0) ) |
776 | 2 | scum = stmp; |
777 | 2 | UNPROTECT(1); /* stmp */ |
778 | 2 | } |
779 | 3 | UNPROTECT(1); /* scum */ |
780 | 3 | PROTECT(scum); |
781 | 3 | } |
782 | 1.63M | }/*updated*/ else { |
783 | | /*-- in what cases does this happen here at all? |
784 | | -- if there are no non-missing elements. |
785 | | */ |
786 | 30 | DbgP2(" NOT updated [!! RARE !!]: int_a=%s\n", int_a ? "TRUE" : "FALSE"); |
787 | 30 | } |
788 | | |
789 | 1.63M | break;/*--- end of min() / max() ---*/ |
790 | | |
791 | 3.01M | case 0:/* sum */ |
792 | 3.01M | switch(TYPEOF(a)) { |
793 | 483k | case LGLSXP: |
794 | 572k | case INTSXP: |
795 | 572k | #ifdef LONG_INT |
796 | 572k | updated = (use_isum ? |
797 | 572k | isum(a, &iLtmp, narm, call) : |
798 | 572k | risum(a, &tmp, narm)); |
799 | 572k | DbgP2(" int|lgl: updated=%d ", updated); |
800 | 572k | if(updated == NA_INTEGER) |
801 | 3.69k | goto na_answer; |
802 | 568k | else if(use_isum && updated == 42) { |
803 | | // impending integer overflow --> switch to irsum() |
804 | 0 | use_isum = false; |
805 | 0 | if(ans_type == INTSXP) ans_type = REALSXP; |
806 | | // re-sum() 'a' (a waste, rare; FIXME ?) : |
807 | 0 | risum(a, &tmp, narm); |
808 | 0 | zcum.r = (double) iLcum + tmp; |
809 | 0 | DbgP3(" .. switching type to REAL, tmp=%g, zcum.r=%g", |
810 | 0 | tmp, zcum.r); |
811 | 0 | } |
812 | 568k | else if(updated) { |
813 | | // iLtmp is LONG_INT i.e. at least 64bit |
814 | 568k | if(ans_type == INTSXP) { |
815 | 64.3k | s = (double) iLcum + (double) iLtmp; |
816 | 64.3k | if(s > INT_MAX || s < R_INT_MIN || |
817 | 64.3k | iLtmp < -LONG_INT_MAX || LONG_INT_MAX < iLtmp) { |
818 | 2 | ans_type = REALSXP; |
819 | 2 | zcum.r = s; |
820 | 2 | DbgP2(" int_1 switch: zcum.r = s = %g\n", s); |
821 | 64.3k | } else if(s < -(double)LONG_INT_MAX || (double)LONG_INT_MAX < s) { |
822 | 0 | use_isum = false; |
823 | 0 | ans_type = REALSXP; |
824 | 0 | zcum.r = s; |
825 | 0 | DbgP2(" int_2 switch: zcum.r = s = %g\n", s); |
826 | 0 | } |
827 | 64.3k | else { |
828 | 64.3k | iLcum += iLtmp; |
829 | 64.3k | DbgP3(" int_3: (iLtmp,iLcum) = (%ld,%ld)\n", |
830 | 64.3k | iLtmp, iLcum); |
831 | 64.3k | } |
832 | 504k | } else { // dealt with NA_INTEGER already above |
833 | 504k | zcum.r += use_isum ? (double)iLtmp : tmp; |
834 | 504k | DbgP3(" dbl: (*tmp, zcum.r) = (%g,%g)\n", |
835 | 504k | use_isum ? (double)iLtmp : tmp, zcum.r); |
836 | 504k | } |
837 | 568k | } |
838 | | #else |
839 | | updated = isum(a, &iLtmp, narm, call); |
840 | | if(updated) { |
841 | | if(iLtmp == NA_INTEGER) goto na_answer; |
842 | | if(ans_type == INTSXP) { |
843 | | s = (double) icum + (double) iLtmp; |
844 | | if(s > INT_MAX || s < R_INT_MIN){ |
845 | | warningcall(call,_( |
846 | | "Integer overflow - use sum(as.numeric(.))")); |
847 | | goto na_answer; |
848 | | } |
849 | | else icum += iLtmp; |
850 | | } else |
851 | | zcum.r += Int2Real(iLtmp); |
852 | | } |
853 | | #endif |
854 | 568k | break; |
855 | 1.59M | case REALSXP: |
856 | 1.59M | if(ans_type == INTSXP) { |
857 | 0 | ans_type = REALSXP; |
858 | 0 | if(!empty) zcum.r = Int2Real(iLcum); |
859 | 0 | } |
860 | 1.59M | updated = rsum(a, &tmp, narm); |
861 | 1.59M | if(updated) { |
862 | 1.59M | zcum.r += tmp; |
863 | 1.59M | } |
864 | 1.59M | break; |
865 | 847k | case CPLXSXP: |
866 | 847k | if(ans_type == INTSXP) { |
867 | 0 | ans_type = CPLXSXP; |
868 | 0 | if(!empty) zcum.r = Int2Real(iLcum); |
869 | 847k | } else if (ans_type == REALSXP) |
870 | 0 | ans_type = CPLXSXP; |
871 | 847k | updated = csum(a, &ztmp, narm); |
872 | 847k | if(updated) { |
873 | 847k | zcum.r += ztmp.r; |
874 | 847k | zcum.i += ztmp.i; |
875 | 847k | } |
876 | 847k | break; |
877 | 0 | default: |
878 | 0 | goto invalid_type; |
879 | 3.01M | } |
880 | | |
881 | 3.01M | break;/* sum() part */ |
882 | | |
883 | 3.01M | case 4:/* prod */ |
884 | | |
885 | 102k | switch(TYPEOF(a)) { |
886 | 0 | case LGLSXP: |
887 | 102k | case INTSXP: |
888 | 102k | case REALSXP: |
889 | 102k | if(TYPEOF(a) == REALSXP) |
890 | 0 | updated = rprod(a, &tmp, narm); |
891 | 102k | else |
892 | 102k | updated = iprod(a, &tmp, narm); |
893 | 102k | if(updated) { |
894 | 102k | zcum.r *= tmp; |
895 | 102k | zcum.i *= tmp; |
896 | 102k | } |
897 | 102k | break; |
898 | 0 | case CPLXSXP: |
899 | 0 | ans_type = CPLXSXP; |
900 | 0 | updated = cprod(a, &ztmp, narm); |
901 | 0 | if(updated) { |
902 | 0 | Rcomplex z; |
903 | 0 | z.r = zcum.r; |
904 | 0 | z.i = zcum.i; |
905 | 0 | zcum.r = z.r * ztmp.r - z.i * ztmp.i; |
906 | 0 | zcum.i = z.r * ztmp.i + z.i * ztmp.r; |
907 | 0 | } |
908 | 0 | break; |
909 | 0 | default: |
910 | 0 | goto invalid_type; |
911 | 102k | } |
912 | | |
913 | 102k | break;/* prod() part */ |
914 | | |
915 | 4.76M | } /* switch(iop) */ |
916 | | |
917 | 4.76M | } else { /* len(a)=0 */ |
918 | | /* Even though this has length zero it can still be invalid, |
919 | | e.g. list() or raw() */ |
920 | 1.69M | switch(TYPEOF(a)) { |
921 | 2.06k | case LGLSXP: |
922 | 30.4k | case INTSXP: |
923 | 306k | case REALSXP: |
924 | 1.68M | case NILSXP: /* OK historically, e.g. PR#1283 */ |
925 | 1.68M | break; |
926 | 1.54k | case CPLXSXP: |
927 | 1.54k | if (iop == 2 || iop == 3) goto invalid_type; |
928 | 1.54k | break; |
929 | 1.54k | case STRSXP: |
930 | 0 | if (iop == 2 || iop == 3) { |
931 | 0 | if(!empty && ans_type == INTSXP) { |
932 | 0 | scum = StringFromInteger(icum, &warn); |
933 | 0 | UNPROTECT(1); /* scum */ |
934 | 0 | PROTECT(scum); |
935 | 0 | } else if(!empty && ans_type == REALSXP) { |
936 | 0 | scum = StringFromReal(zcum.r, &warn); |
937 | 0 | UNPROTECT(1); /* scum */ |
938 | 0 | PROTECT(scum); |
939 | 0 | } |
940 | 0 | ans_type = STRSXP; |
941 | 0 | break; |
942 | 0 | } |
943 | 0 | default: |
944 | 0 | goto invalid_type; |
945 | 1.69M | } |
946 | 1.69M | if(ans_type < TYPEOF(a) && ans_type != CPLXSXP) { |
947 | 2.42k | if(!empty && ans_type == INTSXP) |
948 | 2.42k | zcum.r = Int2Real(icum); |
949 | 2.42k | ans_type = TYPEOF(a); |
950 | 2.42k | } |
951 | 1.69M | } |
952 | 6.44M | DbgP3(" .. upd.=%d, empty=%d", updated, (int)empty); |
953 | 6.44M | if(empty && updated) empty=false; |
954 | 6.44M | DbgP2(", new empty=%d\n", (int)empty); |
955 | 6.44M | args = CDR(args); |
956 | 6.44M | } /*-- while(..) loop over args */ |
957 | | |
958 | | /*-------------------------------------------------------*/ |
959 | 4.99M | if(empty && (iop == 2 || iop == 3)) { |
960 | 20 | if(ans_type == STRSXP) { |
961 | 0 | warningcall(call, _("no non-missing arguments, returning NA")); |
962 | 20 | } else { |
963 | 20 | if(iop == 2) |
964 | 18 | warningcall(call, _("no non-missing arguments to min; returning Inf")); |
965 | 2 | else |
966 | 2 | warningcall(call, _("no non-missing arguments to max; returning -Inf")); |
967 | 20 | ans_type = REALSXP; |
968 | 20 | } |
969 | 20 | } |
970 | | |
971 | 4.99M | ans = allocVector(ans_type, 1); |
972 | 4.99M | switch(ans_type) { |
973 | 2.40M | case INTSXP: INTEGER(ans)[0] = (iop == 0) ? (int)iLcum : icum; break; |
974 | 1.73M | case REALSXP: REAL(ans)[0] = zcum.r; break; |
975 | 849k | case CPLXSXP: COMPLEX(ans)[0].r = zcum.r; COMPLEX(ans)[0].i = zcum.i;break; |
976 | 2 | case STRSXP: SET_STRING_ELT(ans, 0, scum); break; |
977 | 4.99M | } |
978 | 4.99M | UNPROTECT(2); /* scum, args */ |
979 | 4.99M | return ans; |
980 | | |
981 | 3.69k | na_answer: /* only sum(INTSXP, ...) case currently used */ |
982 | 3.69k | ans = allocVector(ans_type, 1); |
983 | 3.69k | switch(ans_type) { |
984 | 3.59k | case INTSXP: INTEGER(ans)[0] = NA_INTEGER; break; |
985 | 96 | case REALSXP: REAL(ans)[0] = NA_REAL; break; |
986 | 1 | case CPLXSXP: COMPLEX(ans)[0].r = COMPLEX(ans)[0].i = NA_REAL; break; |
987 | 0 | case STRSXP: SET_STRING_ELT(ans, 0, NA_STRING); break; |
988 | 3.69k | } |
989 | 3.69k | UNPROTECT(2); /* scum, args */ |
990 | 3.69k | return ans; |
991 | | |
992 | 3 | invalid_type: |
993 | 3 | errorcall(call, R_MSG_type, R_typeToChar(a)); |
994 | 3 | return R_NilValue; |
995 | 3.69k | }/* do_summary */ |
996 | | |
997 | | |
998 | | attribute_hidden SEXP do_range(SEXP call, SEXP op, SEXP args, SEXP env) |
999 | 0 | { |
1000 | 0 | SEXP ans, a, b, prargs, call2; |
1001 | |
|
1002 | 0 | PROTECT(args = fixup_NaRm(args)); |
1003 | 0 | PROTECT(call2 = shallow_duplicate(call)); |
1004 | 0 | R_args_enable_refcnt(args); |
1005 | 0 | SETCDR(call2, args); |
1006 | |
|
1007 | 0 | if (DispatchGroup("Summary", call2, op, args, env, &ans)) { |
1008 | 0 | SETCDR(call2, R_NilValue); /* clear refcnt on args */ |
1009 | 0 | R_try_clear_args_refcnt(args); |
1010 | 0 | UNPROTECT(2); |
1011 | 0 | return(ans); |
1012 | 0 | } |
1013 | 0 | UNPROTECT(1); |
1014 | 0 | SETCDR(call2, R_NilValue); /* clear refcnt on args */ |
1015 | 0 | R_try_clear_args_refcnt(args); |
1016 | |
|
1017 | 0 | PROTECT(op = findFun(install("range.default"), env)); |
1018 | 0 | PROTECT(prargs = promiseArgs(args, R_GlobalEnv)); |
1019 | 0 | for (a = args, b = prargs; a != R_NilValue; a = CDR(a), b = CDR(b)) |
1020 | 0 | IF_PROMSXP_SET_PRVALUE(CAR(b), CAR(a)); |
1021 | 0 | ans = applyClosure(call, op, prargs, env, R_NilValue, TRUE); |
1022 | 0 | UNPROTECT(3); |
1023 | 0 | return(ans); |
1024 | 0 | } |
1025 | | |
1026 | | // which.min(x) : The index (starting at 1), of the first min(x) in x |
1027 | | // which.max(x) : The index (starting at 1), of the first max(x) in x |
1028 | | attribute_hidden SEXP do_first_min(SEXP call, SEXP op, SEXP args, SEXP rho) |
1029 | 4.76k | { |
1030 | 4.76k | SEXP sx = CAR(args), ans; |
1031 | 4.76k | int nprot = 1; |
1032 | 4.76k | R_xlen_t i, n, indx = -1; |
1033 | | |
1034 | 4.76k | checkArity(op, args); |
1035 | 4.76k | if (OBJECT(sx) && !isDataFrame(sx)) { |
1036 | 0 | SEXP call = PROTECT(lang2(install("xtfrm"), sx)); nprot++; |
1037 | 0 | PROTECT(sx = eval(call, rho)); nprot++; |
1038 | 0 | } else |
1039 | 4.76k | if (!isNumeric(sx)) { |
1040 | 0 | PROTECT(sx = coerceVector(CAR(args), REALSXP)); nprot++; |
1041 | 0 | } |
1042 | 4.76k | n = XLENGTH(sx); |
1043 | 4.76k | switch(TYPEOF(sx)) { |
1044 | 4.76k | case LGLSXP: // with only (TRUE, FALSE, NA) -- may be fast |
1045 | 4.76k | { |
1046 | 4.76k | int *r = LOGICAL(sx); |
1047 | 4.76k | if(PRIMVAL(op) == 0) { /* which.min */ |
1048 | 0 | for (i = 0; i < n; i++) |
1049 | 0 | if (r[i] == FALSE) { |
1050 | 0 | indx = i; break; // found FALSE: done |
1051 | 0 | } else if (indx == -1 && r[i] != NA_LOGICAL) { |
1052 | 0 | indx = i; // first TRUE |
1053 | 0 | } |
1054 | 4.76k | } else { /* which.max */ |
1055 | 4.76k | for (i = 0; i < n; i++) |
1056 | 4.76k | if (r[i] == TRUE) { |
1057 | 4.76k | indx = i; break; // found TRUE: done |
1058 | 4.76k | } else if (indx == -1 && r[i] != NA_LOGICAL) { |
1059 | 0 | indx = i; // first FALSE |
1060 | 0 | } |
1061 | 4.76k | } |
1062 | 4.76k | } |
1063 | 4.76k | break; |
1064 | | |
1065 | 0 | case INTSXP: |
1066 | 0 | { |
1067 | 0 | int s, *r = INTEGER(sx); |
1068 | 0 | if(PRIMVAL(op) == 0) { /* which.min */ |
1069 | 0 | s = INT_MAX; |
1070 | 0 | for (i = 0; i < n; i++) |
1071 | 0 | if (r[i] != NA_INTEGER && (r[i] < s || indx == -1)) { |
1072 | 0 | s = r[i]; indx = i; |
1073 | 0 | } |
1074 | 0 | } else { /* which.max */ |
1075 | 0 | s = INT_MIN; |
1076 | 0 | for (i = 0; i < n; i++) |
1077 | 0 | if (r[i] != NA_INTEGER && (r[i] > s || indx == -1)) { |
1078 | 0 | s = r[i]; indx = i; |
1079 | 0 | } |
1080 | 0 | } |
1081 | 0 | } |
1082 | 0 | break; |
1083 | | |
1084 | 0 | case REALSXP: |
1085 | 0 | { |
1086 | 0 | double s, *r = REAL(sx); |
1087 | 0 | if(PRIMVAL(op) == 0) { /* which.min */ |
1088 | 0 | s = R_PosInf; |
1089 | 0 | for (i = 0; i < n; i++) |
1090 | 0 | if ( !ISNAN(r[i]) && (r[i] < s || indx == -1) ) { |
1091 | 0 | s = r[i]; indx = i; |
1092 | 0 | } |
1093 | 0 | } else { /* which.max */ |
1094 | 0 | s = R_NegInf; |
1095 | 0 | for (i = 0; i < n; i++) |
1096 | 0 | if ( !ISNAN(r[i]) && (r[i] > s || indx == -1) ) { |
1097 | 0 | s = r[i]; indx = i; |
1098 | 0 | } |
1099 | 0 | } |
1100 | 0 | } |
1101 | 4.76k | } // switch() |
1102 | | |
1103 | | |
1104 | 4.76k | i = (indx != -1); |
1105 | 4.76k | bool large = (indx + 1) > INT_MAX; |
1106 | 4.76k | PROTECT(ans = allocVector(large ? REALSXP : INTSXP, i ? 1 : 0)); |
1107 | 4.76k | if (i) { |
1108 | 4.76k | if(large) |
1109 | 0 | REAL(ans)[0] = (double)indx + 1; |
1110 | 4.76k | else |
1111 | 4.76k | INTEGER(ans)[0] = (int)indx + 1; |
1112 | 4.76k | if (getAttrib(sx, R_NamesSymbol) != R_NilValue) { /* preserve names */ |
1113 | 4.76k | SEXP ansnam; |
1114 | 4.76k | PROTECT(ansnam = |
1115 | 4.76k | ScalarString(STRING_ELT(getAttrib(sx, R_NamesSymbol), indx))); |
1116 | 4.76k | setAttrib(ans, R_NamesSymbol, ansnam); |
1117 | 4.76k | UNPROTECT(1); |
1118 | 4.76k | } |
1119 | 4.76k | } |
1120 | 4.76k | UNPROTECT(nprot); |
1121 | 4.76k | return ans; |
1122 | 4.76k | } |
1123 | | |
1124 | | |
1125 | | /* which(x) : indices of non-NA TRUE values in x */ |
1126 | | attribute_hidden SEXP do_which(SEXP call, SEXP op, SEXP args, SEXP rho) |
1127 | 221k | { |
1128 | 221k | checkArity(op, args); |
1129 | 221k | SEXP v = CAR(args); |
1130 | 221k | if (!isLogical(v)) |
1131 | 0 | error(_("argument to 'which' is not logical")); |
1132 | 221k | R_xlen_t len = xlength(v), i, j = 0; |
1133 | 221k | SEXP ans; |
1134 | 221k | #ifdef LONG_VECTOR_SUPPORT |
1135 | 221k | if (len > R_SHORT_LEN_MAX) { |
1136 | 0 | R_xlen_t xoffset = 1; // 1 for 1-based indexing of response |
1137 | 0 | double *buf = (double *) R_alloc(len, sizeof(double)); |
1138 | 0 | ITERATE_BY_REGION(v, ptr, idx, nb, int, LOGICAL, { |
1139 | 0 | for(R_xlen_t i = 0; i < nb; i++) { |
1140 | 0 | if(ptr[i] == TRUE) { |
1141 | 0 | buf[j] = (double)(xoffset + i); // offset has +1 built in |
1142 | 0 | j++; |
1143 | 0 | } |
1144 | 0 | } |
1145 | 0 | xoffset += nb; // move to beginning of next buffer (+1 since R-based) |
1146 | 0 | }); |
1147 | |
|
1148 | 0 | len = j; |
1149 | 0 | PROTECT(ans = allocVector(REALSXP, len)); |
1150 | | // buf has doubles in it, memcopy if we found any indices. |
1151 | 0 | if(len) memcpy(REAL(ans), buf, sizeof(double) * len); |
1152 | 0 | } else |
1153 | 221k | #endif |
1154 | 221k | { |
1155 | 221k | int ioffset = 1; |
1156 | 221k | int *buf = (int *) R_alloc(len, sizeof(int)); |
1157 | | /* use iteration macros to be ALTREP safe and pull ptr retrieval out of tight loop */ |
1158 | 221k | ITERATE_BY_REGION(v, ptr, idx, nb, int, LOGICAL, { |
1159 | 221k | for(int i = 0; i < nb; i++) { |
1160 | 221k | if(ptr[i] == TRUE) { |
1161 | 221k | buf[j] = ioffset + i; // offset has +1 built in |
1162 | 221k | j++; |
1163 | 221k | } |
1164 | 221k | } |
1165 | 221k | ioffset += nb; // move to beginning of next buffer |
1166 | 221k | }); |
1167 | | |
1168 | 221k | len = j; |
1169 | | // buf has ints in it and we're returning ints, memcopy if we found any indices; |
1170 | 221k | PROTECT(ans = allocVector(INTSXP, len)); |
1171 | 221k | if(len) memcpy(INTEGER(ans), buf, sizeof(int) * len); |
1172 | 221k | } |
1173 | | |
1174 | 221k | SEXP v_nms = getAttrib(v, R_NamesSymbol); |
1175 | 221k | if (v_nms != R_NilValue) { |
1176 | 1.56k | SEXP ans_nms = PROTECT(allocVector(STRSXP, len)); |
1177 | 1.56k | #ifdef LONG_VECTOR_SUPPORT |
1178 | 1.56k | if (TYPEOF(ans) == REALSXP) |
1179 | 0 | for (i = 0; i < len; i++) { |
1180 | 0 | SET_STRING_ELT(ans_nms, i, |
1181 | 0 | STRING_ELT(v_nms, (R_xlen_t)REAL(ans)[i] - 1)); |
1182 | 0 | } |
1183 | 1.56k | else |
1184 | 1.56k | #endif |
1185 | 194k | for (i = 0; i < len; i++) { |
1186 | 193k | SET_STRING_ELT(ans_nms, i, |
1187 | 193k | STRING_ELT(v_nms, (R_xlen_t)INTEGER(ans)[i] - 1)); |
1188 | 193k | } |
1189 | 1.56k | setAttrib(ans, R_NamesSymbol, ans_nms); |
1190 | 1.56k | UNPROTECT(1); |
1191 | 1.56k | } |
1192 | 221k | UNPROTECT(1); |
1193 | 221k | return ans; |
1194 | 221k | } |
1195 | | |
1196 | | |
1197 | | /* op = 0 is pmin, op = 1 is pmax |
1198 | | NULL and logicals are handled as if they had been coerced to integer. |
1199 | | */ |
1200 | | attribute_hidden SEXP do_pmin(SEXP call, SEXP op, SEXP args, SEXP rho) |
1201 | 4.64k | { |
1202 | 4.64k | int narm = asLogical(CAR(args)); |
1203 | 4.64k | if(narm == NA_LOGICAL) |
1204 | 0 | error(_("invalid '%s' value"), "na.rm"); |
1205 | 4.64k | args = CDR(args); |
1206 | 4.64k | if(args == R_NilValue) error(_("no arguments")); |
1207 | 4.64k | SEXP x = CAR(args); |
1208 | | |
1209 | 4.64k | SEXPTYPE anstype = TYPEOF(x); |
1210 | 4.64k | switch(anstype) { |
1211 | 0 | case NILSXP: |
1212 | 0 | case LGLSXP: |
1213 | 2.63k | case INTSXP: |
1214 | 4.64k | case REALSXP: |
1215 | 4.64k | case STRSXP: |
1216 | 4.64k | break; |
1217 | 0 | default: |
1218 | 0 | error(_("invalid input type")); |
1219 | 4.64k | } |
1220 | 4.64k | SEXP a = CDR(args); |
1221 | 4.64k | if(a == R_NilValue) return x; /* one input */ |
1222 | | |
1223 | 4.64k | R_xlen_t n, len = xlength(x), /* not LENGTH, as NULL is allowed */ |
1224 | 4.64k | i, i1; |
1225 | 9.89k | for(; a != R_NilValue; a = CDR(a)) { |
1226 | 5.24k | x = CAR(a); |
1227 | 5.24k | SEXPTYPE type = TYPEOF(x); |
1228 | 5.24k | switch(type) { |
1229 | 0 | case NILSXP: |
1230 | 0 | case LGLSXP: |
1231 | 3.23k | case INTSXP: |
1232 | 5.24k | case REALSXP: |
1233 | 5.24k | case STRSXP: |
1234 | 5.24k | break; |
1235 | 0 | default: |
1236 | 0 | error(_("invalid input type")); |
1237 | 5.24k | } |
1238 | 5.24k | if(type > anstype) anstype = type; |
1239 | 5.24k | n = xlength(x); |
1240 | 5.24k | if ((len > 0) ^ (n > 0)) { |
1241 | | // till 2.15.0: error(_("cannot mix 0-length vectors with others")); |
1242 | 0 | len = 0; |
1243 | 0 | break; |
1244 | 0 | } |
1245 | 5.24k | len = imax2(len, n); |
1246 | 5.24k | } |
1247 | 4.64k | if(anstype < INTSXP) anstype = INTSXP; |
1248 | 4.64k | if(len == 0) return allocVector(anstype, 0); |
1249 | | /* Check for fractional recycling (added in 2.14.0) */ |
1250 | 14.5k | for(a = args; a != R_NilValue; a = CDR(a)) { |
1251 | 9.89k | n = xlength(CAR(a)); |
1252 | 9.89k | if (len % n) { |
1253 | 0 | warning(_("an argument will be fractionally recycled")); |
1254 | 0 | break; |
1255 | 0 | } |
1256 | 9.89k | } |
1257 | | |
1258 | 4.64k | SEXP ans = PROTECT(allocVector(anstype, len)); |
1259 | 4.64k | switch(anstype) { |
1260 | 2.63k | case INTSXP: |
1261 | 2.63k | { |
1262 | 2.63k | int *r, *ra = INTEGER(ans), tmp; |
1263 | 2.63k | PROTECT(x = coerceVector(CAR(args), anstype)); |
1264 | 2.63k | r = INTEGER(x); |
1265 | 2.63k | n = XLENGTH(x); |
1266 | 2.63k | xcopyIntegerWithRecycle(ra, r, 0, len, n); |
1267 | 2.63k | UNPROTECT(1); |
1268 | 5.86k | for(a = CDR(args); a != R_NilValue; a = CDR(a)) { |
1269 | 3.23k | x = CAR(a); |
1270 | 3.23k | PROTECT(x = coerceVector(CAR(a), anstype)); |
1271 | 3.23k | n = XLENGTH(x); |
1272 | 3.23k | r = INTEGER(x); |
1273 | 3.23k | MOD_ITERATE1(len, n, i, i1, { |
1274 | 3.23k | tmp = r[i1]; |
1275 | 3.23k | if(PRIMVAL(op) == 1) { |
1276 | 3.23k | if( (narm && ra[i] == NA_INTEGER) || |
1277 | 3.23k | (ra[i] != NA_INTEGER && tmp != NA_INTEGER |
1278 | 3.23k | && tmp > ra[i]) || |
1279 | 3.23k | (!narm && tmp == NA_INTEGER) ) |
1280 | 3.23k | ra[i] = tmp; |
1281 | 3.23k | } else { |
1282 | 3.23k | if( (narm && ra[i] == NA_INTEGER) || |
1283 | 3.23k | (ra[i] != NA_INTEGER && tmp != NA_INTEGER |
1284 | 3.23k | && tmp < ra[i]) || |
1285 | 3.23k | (!narm && tmp == NA_INTEGER) ) |
1286 | 3.23k | ra[i] = tmp; |
1287 | 3.23k | } |
1288 | 3.23k | }); |
1289 | 3.23k | UNPROTECT(1); |
1290 | 3.23k | } |
1291 | 2.63k | } |
1292 | 2.63k | break; |
1293 | 2.01k | case REALSXP: |
1294 | 2.01k | { |
1295 | 2.01k | double *r, *ra = REAL(ans), tmp; |
1296 | 2.01k | PROTECT(x = coerceVector(CAR(args), anstype)); |
1297 | 2.01k | r = REAL(x); |
1298 | 2.01k | n = XLENGTH(x); |
1299 | 2.01k | xcopyRealWithRecycle(ra, r, 0, len, n); |
1300 | 2.01k | UNPROTECT(1); |
1301 | 4.02k | for(a = CDR(args); a != R_NilValue; a = CDR(a)) { |
1302 | 2.01k | PROTECT(x = coerceVector(CAR(a), anstype)); |
1303 | 2.01k | n = XLENGTH(x); |
1304 | 2.01k | r = REAL(x); |
1305 | 2.01k | MOD_ITERATE1(len, n, i, i1, { |
1306 | 2.01k | tmp = r[i1]; |
1307 | 2.01k | if(PRIMVAL(op) == 1) { |
1308 | 2.01k | if( (narm && ISNAN(ra[i])) || |
1309 | 2.01k | (!ISNAN(ra[i]) && !ISNAN(tmp) && tmp > ra[i]) || |
1310 | 2.01k | (!narm && ISNAN(tmp)) ) |
1311 | 2.01k | ra[i] = tmp; |
1312 | 2.01k | } else { |
1313 | 2.01k | if( (narm && ISNAN(ra[i])) || |
1314 | 2.01k | (!ISNAN(ra[i]) && !ISNAN(tmp) && tmp < ra[i]) || |
1315 | 2.01k | (!narm && ISNAN(tmp)) ) |
1316 | 2.01k | ra[i] = tmp; |
1317 | 2.01k | } |
1318 | 2.01k | }); |
1319 | 2.01k | UNPROTECT(1); |
1320 | 2.01k | } |
1321 | 2.01k | } |
1322 | 2.01k | break; |
1323 | 0 | case STRSXP: |
1324 | 0 | { |
1325 | 0 | PROTECT(x = coerceVector(CAR(args), anstype)); |
1326 | 0 | n = XLENGTH(x); |
1327 | 0 | xcopyStringWithRecycle(ans, x, 0, len, n); |
1328 | 0 | UNPROTECT(1); |
1329 | 0 | for(a = CDR(args); a != R_NilValue; a = CDR(a)) { |
1330 | 0 | SEXP tmp, t2; |
1331 | 0 | PROTECT(x = coerceVector(CAR(a), anstype)); |
1332 | 0 | n = XLENGTH(x); |
1333 | 0 | MOD_ITERATE1(len, n, i, i1, { |
1334 | 0 | tmp = STRING_ELT(x, i1); |
1335 | 0 | t2 = STRING_ELT(ans, i); |
1336 | 0 | if(PRIMVAL(op) == 1) { |
1337 | 0 | if( (narm && t2 == NA_STRING) || |
1338 | 0 | (t2 != NA_STRING && tmp != NA_STRING && tmp != t2 && Scollate(tmp, t2) > 0) || |
1339 | 0 | (!narm && tmp == NA_STRING) ) |
1340 | 0 | SET_STRING_ELT(ans, i, tmp); |
1341 | 0 | } else { |
1342 | 0 | if( (narm && t2 == NA_STRING) || |
1343 | 0 | (t2 != NA_STRING && tmp != NA_STRING && tmp != t2 && Scollate(tmp, t2) < 0) || |
1344 | 0 | (!narm && tmp == NA_STRING) ) |
1345 | 0 | SET_STRING_ELT(ans, i, tmp); |
1346 | 0 | } |
1347 | 0 | }); |
1348 | 0 | UNPROTECT(1); |
1349 | 0 | } |
1350 | 0 | } |
1351 | 0 | break; |
1352 | 0 | default: |
1353 | 0 | break; |
1354 | 4.64k | } |
1355 | 4.64k | UNPROTECT(1); |
1356 | 4.64k | return ans; |
1357 | 4.64k | } |