131 {
132
134 int give_log;
136 SEXP
Snames, Pnames, Cnames;
138 SEXP F, cvec;
140 int *dim;
141
142 ntimes = LENGTH(times);
143 dim = INTEGER(GET_DIM(x));
nvars = dim[0]; nrepsx = dim[1];
144 if (ntimes < 2)
145 err(
"length(times) < 2: with no transitions, there is no work to do.");
146 if (ntimes != dim[2])
147 err(
"the length of 'times' and 3rd dimension of 'x' do not agree.");
148 dim = INTEGER(GET_DIM(
params));
npars = dim[0]; nrepsp = dim[1];
149
150 give_log = *(INTEGER(AS_INTEGER(log)));
151
152
153 if (nrepsx != nrepsp && nrepsx % nrepsp != 0 && nrepsp % nrepsx != 0) {
154 err(
"the larger number of replicates is not a multiple of smaller.");
155 } else {
156 nreps = (nrepsx > nrepsp) ? nrepsx : nrepsp;
157 }
158
159 PROTECT(
Snames = GET_ROWNAMES(GET_DIMNAMES(x)));
160 PROTECT(Pnames = GET_ROWNAMES(GET_DIMNAMES(
params)));
162
164
165
167 PROTECT(cvec = NEW_NUMERIC(
ncovars));
169
171
172 int nprotect = 6;
173 double *t1 = REAL(times), *t2 = REAL(times)+1;
174 double *x1p = REAL(x);
175 double *x2p = REAL(x)+nrepsx*
nvars;
176 double *ft = REAL(F);
177
179
181
183
184 for (int k = 0; k < ntimes-1; k++, t1++, t2++) {
185
186 R_CheckUserInterrupt();
187
188
190
191 for (
int j = 0; j <
nreps; j++, ft++) {
192
194 double *x1 = x1p+
nvars*(j%nrepsx);
195 double *x2 = x2p+
nvars*(j%nrepsx);
196
197 PROTECT(ans =
eval_call(
fn,
args,t1,t2,x1,x2,
nvars,p,
npars,
cov,
ncovars));
198 *ft = *REAL(AS_NUMERIC(ans));
199 UNPROTECT(1);
200
201 if (!give_log) *ft = exp(*ft);
202
203 }
204
205 x1p = x2p;
207
208 }
209
210 }
211
212 break;
213
215
216 int *sidx, *pidx, *cidx;
218
219 sidx = INTEGER(GET_SLOT(func,install("stateindex")));
220 pidx = INTEGER(GET_SLOT(func,install("paramindex")));
221 cidx = INTEGER(GET_SLOT(func,install("covarindex")));
222
223 *((
void **) (&ff)) = R_ExternalPtrAddr(
fn);
224
225 for (int k = 0; k < ntimes-1; k++, t1++, t2++) {
226
227 R_CheckUserInterrupt();
228
229
231
232 for (
int j = 0; j <
nreps; j++, ft++) {
233
235 double *x1 = x1p+
nvars*(j%nrepsx);
236 double *x2 = x2p+
nvars*(j%nrepsx);
237
238 (*ff)(ft,x1,x2,*t1,*t2,p,sidx,pidx,cidx,
cov);
239
240 if (!give_log) *ft = exp(*ft);
241
242 }
243
244 x1p = x2p;
246
247 }
248
249 }
250
251 break;
252
253 default: {
254 double *ft = REAL(F);
255 int j, k;
256
257 for (k = 0; k < ntimes-1; k++) {
258 for (j = 0; j <
nreps; j++, ft++) {
259 *ft = R_NaReal;
260 }
261 }
262
263 warn(
"'dprocess' unspecified: likelihood undefined.");
264
265 }
266
267 }
268
269 UNPROTECT(nprotect);
270 return F;
271}
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)
static R_INLINE SEXP eval_call(SEXP fn, SEXP args, double *t1, double *t2, double *x1, double *x2, 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 nreps, int ntimes)
void pomp_dprocess(double *loglik, const double *x1, const double *x2, double t1, double t2, const double *p, const int *stateindex, const int *parindex, const int *covindex, const double *covars)