Debian Codegraph @

inst/example_API/src/bar.c

raw symbols SCIP
1 /*
2 * Example of a routine making use of the interface defined in
3 * init.c. The routine will iterate on both arguments passed from R,
4 * thereby making the R function vectorized.
5 *
6 * The code is derived from package actuar and base R.
7 *
8 * Copyright (C) 2016 Vincent Goulet
9 * Copyright (C) 1995--1997 Robert Gentleman and Ross Ihaka
10 * Copyright (C) 1998--2016 The R Core Team.
11 * Copyright (C) 2003--2016 The R Foundation
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License as
15 * published by the Free Software Foundation; either version 3 of the
16 * License, or (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 * 02110-1301, USA.
27 *
28 * AUTHOR: Vincent Goulet <[email protected]>
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); /* drop function name from arguments */
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 /* this is where the expint routine is used */
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 }