100{
103 int nobs = 0, nans = 0;
104 SEXP
Snames, Pnames, Cnames, Onames = R_NilValue;
106 SEXP pompfun;
108 int *dim;
110 SEXP cvec;
112
113 PROTECT(times = AS_NUMERIC(times));
114 ntimes = length(times);
115 if (ntimes < 1)
116 err(
"length('times') = 0, no work to do.");
117
119 dim = INTEGER(GET_DIM(x));
120 nvars = dim[0]; nrepsx = dim[1];
121
122 if (ntimes != dim[2])
123 err(
"length of 'times' and 3rd dimension of 'x' do not agree.");
124
126 dim = INTEGER(GET_DIM(
params));
127 npars = dim[0]; nrepsp = dim[1];
128
129 nreps = (nrepsp > nrepsx) ? nrepsp : nrepsx;
130
131 if ((
nreps % nrepsp != 0) || (
nreps % nrepsx != 0))
132 err(
"larger number of replicates is not a multiple of smaller.");
133
134 PROTECT(pompfun = GET_SLOT(object,install("vmeasure")));
135
136 PROTECT(
Snames = GET_ROWNAMES(GET_DIMNAMES(x)));
137 PROTECT(Pnames = GET_ROWNAMES(GET_DIMNAMES(
params)));
139 PROTECT(Onames = GET_SLOT(pompfun,install("obsnames")));
140
141
143 PROTECT(cvec = NEW_NUMERIC(
ncovars));
145
146
148
149
150 PROTECT(
args = GET_SLOT(
object,install(
"userdata")));
151
152 int nprotect = 11;
153 int first = 1;
154
155
157
159 double *ys, *yt = 0;
160 double *time = REAL(times), *xs = REAL(x), *ps = REAL(
params);
161 SEXP ans;
162 int j, k;
163
165
166 for (k = 0; k < ntimes; k++, time++) {
167
168 R_CheckUserInterrupt();
169
171
172 for (j = 0; j <
nreps; j++) {
173
174 if (first) {
175
176 PROTECT(
179 time,
183 )
184 );
185
186 nans = LENGTH(ans);
187
188 nobs = (int) sqrt(nans);
189 if (nans != nobs*nobs)
190 err(
"'vmeasure' must return a symmetric square matrix.");
191
192 PROTECT(Onames = GET_ROWNAMES(GET_DIMNAMES(ans)));
194 err(
"'vmeasure' must return a matrix with row-names.");
195
197
198 nprotect += 3;
199
201 ys = REAL(AS_NUMERIC(ans));
202
203 memcpy(yt,ys,nans*sizeof(double));
204 yt += nans;
205
206 first = 0;
207
208 } else {
209
210 PROTECT(
213 time,
217 )
218 );
219
220 if (LENGTH(ans) != nans)
221 err(
"'vmeasure' returns variable-length results.");
222
223 ys = REAL(AS_NUMERIC(ans));
224
225 memcpy(yt,ys,nans*sizeof(double));
226 yt += nans;
227
228 UNPROTECT(1);
229
230 }
231
232 }
233 }
234
235 }
236
237 break;
238
240 double *yt = 0, *xp, *pp;
241 double *time = REAL(times), *xs = REAL(x), *ps = REAL(
params);
242 SEXP vmatindex;
243 int *oidx, *sidx, *pidx, *cidx, *vmidx;
245 int j, k;
246
247 nobs = LENGTH(Onames);
248 nans = nobs*nobs;
249
250 sidx = INTEGER(GET_SLOT(pompfun,install("stateindex")));
251 pidx = INTEGER(GET_SLOT(pompfun,install("paramindex")));
252 oidx = INTEGER(GET_SLOT(pompfun,install("obsindex")));
253 cidx = INTEGER(GET_SLOT(pompfun,install("covarindex")));
254
255 PROTECT(vmatindex = NEW_INTEGER(nans)); nprotect++;
256 vmidx = INTEGER(vmatindex);
257 for (k = 0; k < nobs; k++) {
258 for (j = 0; j < nobs; j++) {
259 vmidx[j+nobs*k] = oidx[j]+nobs*oidx[k];
260 }
261 }
262
263
264 *((
void **) (&ff)) = R_ExternalPtrAddr(
fn);
265
268
269 for (k = 0; k < ntimes; k++, time++) {
270
271 R_CheckUserInterrupt();
272
273
275
276 for (j = 0; j <
nreps; j++, yt += nans) {
277
278 xp = &xs[
nvars*((j%nrepsx)+nrepsx*k)];
279 pp = &ps[
npars*(j%nrepsp)];
280
281 (*ff)(yt,xp,pp,vmidx,sidx,pidx,cidx,
cov,*time);
282
283 }
284 }
285
286 }
287
288 break;
289
290 default: {
291 nobs = LENGTH(Onames);
292 int dim[4] = {nobs, nobs,
nreps, ntimes};
293 const char *dimnm[4] = {"var1","var2",".id","time"};
294 double *yt = 0;
295 int i, n = nobs*nobs*
nreps*ntimes;
296
301
302 for (i = 0, yt = REAL(
Y); i < n; i++, yt++) *yt = R_NaReal;
303
304 warn(
"'vmeasure' unspecified: NAs generated.");
305 }
306
307 }
308
309 UNPROTECT(nprotect);
311}
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_vmeasure(double *f, const double *x, const double *p, const int *vmatindex, 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 void setcolnames(SEXP x, SEXP names)
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)