99{
102 int nobs = 0;
103 SEXP
Snames, Pnames, Cnames, Onames = R_NilValue;
105 SEXP pompfun;
107 int *dim;
109 SEXP cvec;
111
112 PROTECT(times = AS_NUMERIC(times));
113 ntimes = length(times);
114 if (ntimes < 1)
115 err(
"length('times') = 0, no work to do.");
116
118 dim = INTEGER(GET_DIM(x));
119 nvars = dim[0]; nrepsx = dim[1];
120
121 if (ntimes != dim[2])
122 err(
"length of 'times' and 3rd dimension of 'x' do not agree.");
123
125 dim = INTEGER(GET_DIM(
params));
126 npars = dim[0]; nrepsp = dim[1];
127
128 nreps = (nrepsp > nrepsx) ? nrepsp : nrepsx;
129
130 if ((
nreps % nrepsp != 0) || (
nreps % nrepsx != 0))
131 err(
"larger number of replicates is not a multiple of smaller.");
132
133 PROTECT(pompfun = GET_SLOT(object,install("rmeasure")));
134
135 PROTECT(
Snames = GET_ROWNAMES(GET_DIMNAMES(x)));
136 PROTECT(Pnames = GET_ROWNAMES(GET_DIMNAMES(
params)));
138 PROTECT(Onames = GET_SLOT(pompfun,install("obsnames")));
139
140
142 PROTECT(cvec = NEW_NUMERIC(
ncovars));
144
145
147
148
149 PROTECT(
args = GET_SLOT(
object,install(
"userdata")));
150
151 int nprotect = 11;
152 int first = 1;
153
154
156
158 double *ys, *yt = 0;
159 double *time = REAL(times), *xs = REAL(x), *ps = REAL(
params);
160 SEXP ans;
161 int j, k;
162
164
165 for (k = 0; k < ntimes; k++, time++) {
166
167 R_CheckUserInterrupt();
168
170
171 for (j = 0; j <
nreps; j++) {
172
173 if (first) {
174
175 PROTECT(
178 time,
182 )
183 );
184
185 nobs = LENGTH(ans);
186
187 PROTECT(Onames = GET_NAMES(ans));
189 err(
"'rmeasure' must return a named numeric vector.");
190
192
193 nprotect += 3;
194
196 ys = REAL(AS_NUMERIC(ans));
197
198 memcpy(yt,ys,nobs*sizeof(double));
199 yt += nobs;
200
201 first = 0;
202
203 } else {
204
205 PROTECT(
208 time,
212 )
213 );
214
215 if (LENGTH(ans) != nobs)
216 err(
"'rmeasure' returns variable-length results.");
217
218 ys = REAL(AS_NUMERIC(ans));
219
220 memcpy(yt,ys,nobs*sizeof(double));
221 yt += nobs;
222
223 UNPROTECT(1);
224
225 }
226
227 }
228 }
229
230 }
231
232 break;
233
235 double *yt = 0, *xp, *pp;
236 double *time = REAL(times), *xs = REAL(x), *ps = REAL(
params);
237 int *oidx, *sidx, *pidx, *cidx;
239 int j, k;
240
241 nobs = LENGTH(Onames);
242
243 sidx = INTEGER(GET_SLOT(pompfun,install("stateindex")));
244 pidx = INTEGER(GET_SLOT(pompfun,install("paramindex")));
245 oidx = INTEGER(GET_SLOT(pompfun,install("obsindex")));
246 cidx = INTEGER(GET_SLOT(pompfun,install("covarindex")));
247
248
249 *((
void **) (&ff)) = R_ExternalPtrAddr(
fn);
250
253
254 GetRNGstate();
255
256 for (k = 0; k < ntimes; k++, time++) {
257
258 R_CheckUserInterrupt();
259
260
262
263 for (j = 0; j <
nreps; j++, yt += nobs) {
264
265 xp = &xs[
nvars*((j%nrepsx)+nrepsx*k)];
266 pp = &ps[
npars*(j%nrepsp)];
267
268 (*ff)(yt,xp,pp,oidx,sidx,pidx,cidx,
cov,*time);
269
270 }
271 }
272
273 PutRNGstate();
274
275 }
276
277 break;
278
279 default: {
280 nobs = LENGTH(Onames);
281 int dim[3] = {nobs,
nreps, ntimes};
282 const char *dimnm[3] = {"name",".id","time"};
283 double *yt = 0;
284 int i, n = nobs*
nreps*ntimes;
285
289
290 for (i = 0, yt = REAL(
Y); i < n; i++, yt++) *yt = R_NaReal;
291
292 warn(
"'rmeasure' unspecified: NAs generated.");
293 }
294
295 }
296
297 UNPROTECT(nprotect);
299}
lookup_table_t make_covariate_table(SEXP, int *)
SEXP pomp_fun_handler(SEXP, SEXP, pompfunmode *, SEXP, SEXP, SEXP, SEXP)
void table_lookup(lookup_table_t *, double, double *)
SEXP get_covariate_names(SEXP)
void pomp_rmeasure(double *y, const double *x, const double *p, const int *obsindex, const int *stateindex, const int *parindex, const int *covindex, const double *covars, double t)
static R_INLINE void fixdimnames(SEXP x, const char **names, int n)
static R_INLINE void setrownames(SEXP x, SEXP names, int rank)
static R_INLINE SEXP makearray(int rank, const int *dim)
static R_INLINE SEXP as_state_array(SEXP x)
static R_INLINE int invalid_names(SEXP names)
static R_INLINE SEXP as_matrix(SEXP x)
static R_INLINE SEXP eval_call(SEXP fn, SEXP args, double *t, double *x, int nvar, double *p, int npar, double *c, int ncov)
static R_INLINE SEXP add_args(SEXP args, SEXP Snames, SEXP Pnames, SEXP Cnames)
static R_INLINE SEXP ret_array(int n, int nreps, int ntimes, SEXP names)