pomp
Inference for partially observed Markov processes
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
pomp_defines.h
Go to the documentation of this file.
1// -*- C++ -*-
2
3#ifndef _POMP_DEFINES_H_
4#define _POMP_DEFINES_H_
5
6#include <R.h>
7#include <Rmath.h>
8#include <Rdefines.h>
9
10
11#include "pomp.h"
12
13# define MATCHROWNAMES(X,N,W) (matchnames(GET_ROWNAMES(GET_DIMNAMES(X)),(N),(W)))
14# define MATCHCOLNAMES(X,N,W) (matchnames(GET_COLNAMES(GET_DIMNAMES(X)),(N),(W)))
15
16typedef enum {undef=0,Rfun=1,native=2,regNative=3} pompfunmode;
17typedef enum {dflt=0,onestep=1,discrete=2,euler=3,gill=4} rprocmode;
18
19// lookup-table structure, as used internally
20typedef struct {
21 int length, width;
22 int index;
23 int order;
24 double *x;
25 double *y;
27
28typedef SEXP pomp_fun_handler_t (SEXP pfun, SEXP gnsi, pompfunmode *mode, SEXP S, SEXP P, SEXP O, SEXP C);
29typedef SEXP load_stack_incr_t (SEXP pack);
30typedef SEXP load_stack_decr_t (SEXP pack);
31typedef lookup_table_t make_covariate_table_t (SEXP object, int *ncovar);
32typedef void table_lookup_t (lookup_table_t *tab, double x, double *y);
33typedef SEXP apply_probe_data_t (SEXP object, SEXP probes);
34typedef SEXP apply_probe_sim_t (SEXP object, SEXP nsim, SEXP params, SEXP probes, SEXP datval, SEXP gnsi);
35typedef SEXP systematic_resampling_t (SEXP weights);
36typedef void set_pomp_userdata_t (SEXP userdata);
37typedef void unset_pomp_userdata_t (void);
38typedef SEXP get_covariate_names_t (SEXP object);
39
40static R_INLINE SEXP makearray (int rank, const int *dim) {
41 int *dimp, k;
42 double *xp;
43 SEXP dimx, x;
44 PROTECT(dimx = NEW_INTEGER(rank));
45 dimp = INTEGER(dimx);
46 for (k = 0; k < rank; k++) dimp[k] = dim[k];
47 PROTECT(x = allocArray(REALSXP,dimx));
48 xp = REAL(x);
49 for (k = 0; k < length(x); k++) xp[k] = NA_REAL;
50 UNPROTECT(2);
51 return x;
52}
53
54// check if names exist and are nonempty
55static R_INLINE int invalid_names (SEXP names) {
56 return isNull(names);
57}
58
59static R_INLINE SEXP matchnames
60(
61 SEXP provided, SEXP needed, const char *where
62 ) {
63 int m = LENGTH(provided);
64 int n = length(needed);
65 SEXP index;
66 int *idx, i, j;
67 PROTECT(provided = AS_CHARACTER(provided));
68 PROTECT(needed = AS_CHARACTER(needed));
69 if (invalid_names(provided))
70 err("invalid variable names among the %s.",where); // #nocov
71 PROTECT(index = NEW_INTEGER(n));
72 idx = INTEGER(index);
73 for (i = 0; i < n; i++) {
74 for (j = 0; j < m; j++) {
75 if (!strcmp(CHAR(STRING_ELT(provided,j)),CHAR(STRING_ELT(needed,i)))) {
76 idx[i] = j;
77 break;
78 }
79 }
80 if (j==m)
81 err("variable '%s' not found among the %s.",CHAR(STRING_ELT(needed,i)),where);
82 }
83 UNPROTECT(3);
84 return index;
85}
86
87static R_INLINE void fillrownames (SEXP x, SEXP names) {
88 SEXP dim, dimnms;
89 int nr;
90 PROTECT(names = AS_CHARACTER(names));
91 PROTECT(dim = GET_DIM(x));
92 PROTECT(dimnms = allocVector(VECSXP,length(dim)));
93 nr = INTEGER(dim)[0];
94 if (nr > length(names)) {
95 SEXP nm;
96 int k;
97 PROTECT(nm = NEW_CHARACTER(nr));
98 for (k = 0; k < length(names); k++) {
99 SET_STRING_ELT(nm,k,STRING_ELT(names,k));
100 }
101 SET_ELEMENT(dimnms,0,nm);
102 UNPROTECT(1);
103 } else {
104 SET_ELEMENT(dimnms,0,names);
105 }
106 SET_DIMNAMES(x,dimnms);
107 UNPROTECT(3);
108}
109
110static R_INLINE void setrownames (SEXP x, SEXP names, int rank) {
111 SEXP dimnms, nm;
112 PROTECT(nm = AS_CHARACTER(names));
113 PROTECT(dimnms = allocVector(VECSXP,rank));
114 SET_ELEMENT(dimnms,0,nm); // set row names
115 SET_DIMNAMES(x,dimnms);
116 UNPROTECT(2);
117}
118
119// This only works if the dimnames have already been created and set
120// e.g., with 'setrownames'
121static R_INLINE void setcolnames (SEXP x, SEXP names) {
122 SEXP dn;
123 PROTECT(dn = GET_DIMNAMES(x));
124 SET_ELEMENT(dn,1,names);
125 SET_DIMNAMES(x,dn);
126 UNPROTECT(1);
127}
128
129static R_INLINE void fixdimnames (SEXP x, const char **names, int n) {
130 int nprotect = 2;
131 int i;
132 SEXP dimnames, nm;
133 PROTECT(dimnames = GET_DIMNAMES(x));
134 if (isNull(dimnames)) {
135 PROTECT(dimnames = allocVector(VECSXP,n)); nprotect++;
136 }
137 PROTECT(nm = allocVector(VECSXP,n));
138 for (i = 0; i < n; i++)
139 SET_ELEMENT(nm,i,mkChar(names[i]));
140 SET_NAMES(dimnames,nm);
141 SET_DIMNAMES(x,dimnames);
142 UNPROTECT(nprotect);
143}
144
145static R_INLINE SEXP as_matrix (SEXP x) {
146 int nprotect = 1;
147 SEXP dim, names;
148 int *xdim, nrow, ncol;
149 PROTECT(dim = GET_DIM(x));
150 if (isNull(dim)) {
151 PROTECT(x = duplicate(x));
152 PROTECT(names = GET_NAMES(x));
153 nprotect += 2;
154 dim = NEW_INTEGER(2);
155 xdim = INTEGER(dim); xdim[0] = LENGTH(x); xdim[1] = 1;
156 SET_DIM(x,dim);
157 SET_NAMES(x,R_NilValue);
158 setrownames(x,names,2);
159 } else if (LENGTH(dim) == 1) {
160 PROTECT(x = duplicate(x));
161 PROTECT(names = GET_ROWNAMES(GET_DIMNAMES(x)));
162 nprotect += 2;
163 dim = NEW_INTEGER(2);
164 xdim = INTEGER(dim); xdim[0] = LENGTH(x); xdim[1] = 1;
165 SET_DIM(x,dim);
166 SET_NAMES(x,R_NilValue);
167 setrownames(x,names,2);
168 } else if (LENGTH(dim) > 2) {
169 PROTECT(x = duplicate(x));
170 PROTECT(names = GET_ROWNAMES(GET_DIMNAMES(x)));
171 nprotect += 2;
172 nrow = INTEGER(dim)[0]; ncol = LENGTH(x)/nrow;
173 dim = NEW_INTEGER(2);
174 xdim = INTEGER(dim); xdim[0] = nrow; xdim[1] = ncol;
175 SET_DIM(x,dim);
176 SET_NAMES(x,R_NilValue);
177 setrownames(x,names,2);
178 }
179 UNPROTECT(nprotect);
180 return x;
181}
182
183static R_INLINE SEXP as_state_array (SEXP x) {
184 int nprotect = 1;
185 SEXP dim, names;
186 int *xdim, nrow, ncol;
187 PROTECT(dim = GET_DIM(x));
188 if (isNull(dim)) {
189 PROTECT(x = duplicate(x));
190 PROTECT(names = GET_NAMES(x));
191 nprotect += 2;
192 dim = NEW_INTEGER(3);
193 xdim = INTEGER(dim); xdim[0] = LENGTH(x); xdim[1] = 1; xdim[2] = 1;
194 SET_DIM(x,dim);
195 SET_NAMES(x,R_NilValue);
196 setrownames(x,names,3);
197 } else if (LENGTH(dim) == 1) {
198 PROTECT(x = duplicate(x));
199 PROTECT(names = GET_ROWNAMES(GET_DIMNAMES(x)));
200 nprotect += 2;
201 dim = NEW_INTEGER(3);
202 xdim = INTEGER(dim); xdim[0] = LENGTH(x); xdim[1] = 1; xdim[2] = 1;
203 SET_DIM(x,dim);
204 SET_NAMES(x,R_NilValue);
205 setrownames(x,names,3);
206 } else if (LENGTH(dim) == 2) {
207 PROTECT(x = duplicate(x));
208 PROTECT(names = GET_ROWNAMES(GET_DIMNAMES(x)));
209 nprotect += 2;
210 xdim = INTEGER(dim); nrow = xdim[0]; ncol = xdim[1];
211 dim = NEW_INTEGER(3);
212 xdim = INTEGER(dim); xdim[0] = nrow; xdim[1] = 1; xdim[2] = ncol;
213 SET_DIM(x,dim);
214 SET_NAMES(x,R_NilValue);
215 setrownames(x,names,3);
216 } else if (LENGTH(dim) > 3) {
217 PROTECT(x = duplicate(x));
218 PROTECT(names = GET_ROWNAMES(GET_DIMNAMES(x)));
219 nprotect += 2;
220 xdim = INTEGER(dim); nrow = xdim[0]; ncol = xdim[1];
221 dim = NEW_INTEGER(3);
222 xdim = INTEGER(dim);
223 xdim[0] = nrow; xdim[1] = ncol; xdim[2] = LENGTH(x)/nrow/ncol;
224 SET_DIM(x,dim);
225 SET_NAMES(x,R_NilValue);
226 setrownames(x,names,3);
227 }
228 UNPROTECT(nprotect);
229 return x;
230}
231
232static R_INLINE SEXP getListElement (SEXP list, const char *str)
233{
234 SEXP elmt = R_NilValue;
235 SEXP names = getAttrib(list,R_NamesSymbol);
236 for (R_len_t i = 0; i < length(list); i++)
237 if (strcmp(CHAR(STRING_ELT(names,i)),str) == 0) {
238 elmt = VECTOR_ELT(list,i);
239 break;
240 }
241 return elmt;
242}
243
244static R_INLINE SEXP getPairListElement (SEXP list, const char *name)
245{
246 const char *tag;
247 while (list != R_NilValue) {
248 tag = CHAR(PRINTNAME(TAG(list)));
249 if (strcmp(tag,name)==0) break;
250 list = CDR(list);
251 }
252 return CAR(list);
253}
254
255#ifdef __cplusplus
256
257template <class Scalar>
258class view {
259private:
260 Scalar *data;
261 int dim[2];
262public:
263 view (Scalar *x) {
264 data = x;
265 dim[0] = 0;
266 dim[1] = 0;
267 };
268 view (Scalar *x, int d1) {
269 data = x;
270 dim[0] = d1;
271 dim[1] = 0;
272 };
273 view (Scalar *x, int d1, int d2) {
274 data = x;
275 dim[0] = d1;
276 dim[1] = d2;
277 };
278 ~view (void) {};
279 inline Scalar& operator () (int d1) {
280 return(data[d1]);
281 };
282 inline Scalar& operator () (int d1, int d2) {
283 return(data[d1 + dim[0] * d2]);
284 };
285 inline Scalar& operator () (int d1, int d2, int d3) {
286 return(data[d1 + dim[0] * (d2 + dim[1] * d3)]);
287 };
288};
289
290#endif
291
292#endif
#define err(...)
Definition pomp.h:21
rprocmode
@ gill
@ discrete
@ dflt
@ onestep
@ euler
lookup_table_t make_covariate_table_t(SEXP object, int *ncovar)
static R_INLINE void fixdimnames(SEXP x, const char **names, int n)
static R_INLINE void setrownames(SEXP x, SEXP names, int rank)
SEXP pomp_fun_handler_t(SEXP pfun, SEXP gnsi, pompfunmode *mode, SEXP S, SEXP P, SEXP O, SEXP C)
void set_pomp_userdata_t(SEXP userdata)
SEXP load_stack_decr_t(SEXP pack)
void unset_pomp_userdata_t(void)
static R_INLINE void fillrownames(SEXP x, SEXP names)
static R_INLINE SEXP matchnames(SEXP provided, SEXP needed, const char *where)
static R_INLINE void setcolnames(SEXP x, SEXP names)
void table_lookup_t(lookup_table_t *tab, double x, double *y)
SEXP load_stack_incr_t(SEXP pack)
SEXP apply_probe_sim_t(SEXP object, SEXP nsim, SEXP params, SEXP probes, SEXP datval, SEXP gnsi)
static R_INLINE SEXP makearray(int rank, const int *dim)
static R_INLINE SEXP as_state_array(SEXP x)
SEXP systematic_resampling_t(SEXP weights)
pompfunmode
@ Rfun
@ native
@ undef
@ regNative
SEXP apply_probe_data_t(SEXP object, SEXP probes)
static R_INLINE SEXP getListElement(SEXP list, const char *str)
SEXP get_covariate_names_t(SEXP object)
static R_INLINE int invalid_names(SEXP names)
static R_INLINE SEXP as_matrix(SEXP x)
static R_INLINE SEXP getPairListElement(SEXP list, const char *name)
SEXP params
Definition trajectory.c:128
pompfunmode mode
Definition trajectory.c:126