| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#include <R.h> |
| 32 |
#include <Rinternals.h> |
| 33 |
#include "locale.h" |
| 34 |
#include "pkg.h" |
| 35 |
|
| 36 |
#define mod_iterate1(n1, n2, i1, i2) \ |
| 37 |
for (i = i1 = i2 = 0; i < n; \ |
| 38 |
i1 = (++i1 == n1) ? 0 : i1, \ |
| 39 |
i2 = (++i2 == n2) ? 0 : i2, \ |
| 40 |
++i) |
| 41 |
|
| 42 |
SEXP pkg_do_bar(SEXP args) |
| 43 |
{ |
| 44 |
SEXP sx, sa, sy; |
| 45 |
int i, ix, ia, n, nx, na; |
| 46 |
double ai, *a, xi, *x, *y; |
| 47 |
Rboolean naflag = FALSE; |
| 48 |
|
| 49 |
args = CDR(args); |
| 50 |
|
| 51 |
if (!isNumeric(CAR(args)) || !isNumeric(CADR(args))) |
| 52 |
error(_("invalid arguments")); |
| 53 |
|
| 54 |
na = LENGTH(CAR(args)); |
| 55 |
nx = LENGTH(CADR(args)); |
| 56 |
if ((na == 0) || (nx == 0)) |
| 57 |
return(allocVector(REALSXP, 0)); |
| 58 |
|
| 59 |
n = (nx < na) ? na : nx; |
| 60 |
|
| 61 |
PROTECT(sa = coerceVector(CAR(args), REALSXP)); |
| 62 |
PROTECT(sx = coerceVector(CADR(args), REALSXP)); |
| 63 |
PROTECT(sy = allocVector(REALSXP, n)); |
| 64 |
a = REAL(sa); |
| 65 |
x = REAL(sx); |
| 66 |
y = REAL(sy); |
| 67 |
|
| 68 |
mod_iterate1(na, nx, ia, ix) |
| 69 |
{ |
| 70 |
ai = a[ia]; |
| 71 |
xi = x[ix]; |
| 72 |
if (ISNA(ai) || ISNA(xi)) |
| 73 |
y[i] = NA_REAL; |
| 74 |
else if (ISNAN(ai) || ISNAN(xi)) |
| 75 |
y[i] = R_NaN; |
| 76 |
else |
| 77 |
{ |
| 78 |
|
| 79 |
y[i] = pkg_gamma_inc(ai, xi); |
| 80 |
if (ISNAN(y[i])) naflag = TRUE; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
if (naflag) |
| 85 |
warning(R_MSG_NA); |
| 86 |
|
| 87 |
if (n == na) |
| 88 |
{ |
| 89 |
SET_ATTRIB(sy, duplicate(ATTRIB(sa))); |
| 90 |
SET_OBJECT(sy, OBJECT(sa)); |
| 91 |
} |
| 92 |
else if (n == nx) |
| 93 |
{ |
| 94 |
SET_ATTRIB(sy, duplicate(ATTRIB(sx))); |
| 95 |
SET_OBJECT(sy, OBJECT(sx)); |
| 96 |
} |
| 97 |
|
| 98 |
UNPROTECT(3); |
| 99 |
|
| 100 |
return sy; |
| 101 |
} |