# difeq_replicate: Solve difference equations repeatedly In dde: Solve Delay Differential Equations

 difeq_replicate R Documentation

## Solve difference equations repeatedly

### Description

Solve a replicate set of difference (or recurrence) equation by iterating it a number of times. This is a wrapper around `difeq` that does not (yet) do anything clever to avoid many allocations.

### Usage

``````difeq_replicate(n, y, steps, target, parms, ..., n_out = 0L,
n_history = 0L, grow_history = FALSE, return_history = n_history > 0,
dllname = "", parms_are_real = TRUE, ynames = NULL, outnames = NULL,
return_by_column = TRUE, return_initial = TRUE, return_step = TRUE,
return_output_with_y = TRUE, restartable = FALSE,
return_minimal = FALSE, as_array = TRUE)
``````

### Arguments

 `n` Number of replicates. It is an error to request zero replicates. `y` The initial state of the system. Must be either a numeric vector or a `list` of numeric vectors. If the latter, it must have length `n`. `steps` A vector of steps to return the system at. The first step is taken as step zero, and the solution will be recorded at every other step in the vector. So to step a system from time zero to times 1, 2, 3, ..., n use 0:n. Must be integer values and will be passed through `as.integer` (which may truncate or otherwise butcher non-integer values). `target` The target function to advance. This can either be an R function taking arguments `n, i, t, y, parms` or be a scalar character with the name of a compiled function with arguments ```size_t n, size_t step, double time, const double *y, double *dydt, size_t n_out, double *output void *data```. `parms` Parameters to pass through to the difference function `...` Dummy arguments - nothing is allowed here, but this means that all further arguments must be specified by name (not order) so I can easily reorder them later on. `n_out` The number of output variables (in addition to the difference equation variables). If given, then an R function must return an attribute `output` with the output variables. `n_history` The number of iterations of history to save during the simulation. By default, no history is saved. `grow_history` Logical indicating if history should be grown during the simulation. If `FALSE` (the default) then when history is used it is overwritten as needed (so only the most recent `n_history` elements are saved. This may require some tuning so that you have enough history to run your simulation (i.e. to the longest delay) or an error will be thrown when it underflows. The required history length will vary with your delay sizes and with the timestep for dopri. If `TRUE`, then history will grow as the buffer is exhausted. The growth is geometric, so every time it reaches the end of the buffer it will increase by a factor of about 1.6 (see the `ring` documentation). This may consume more memory than necessary, but may be useful where you don't want to care about picking the history length carefully. `return_history` Logical indicating if history is to be returned. By default, history is returned if `n_history` is nonzero. `dllname` Name of the shared library (without extension) to find the function `func` in the case where `func` refers to compiled function. `parms_are_real` Logical, indicating if `parms` should be treated as vector of doubles by `func` (when it is a compiled function). If `TRUE` (the default), then `REAL(parms)`, which is `double*` is passed through. If `FALSE` then if `params` is an externalptr type (`EXTPTRSXP`) we pass through the result of `R_ExternalPtrAddr`, otherwise we pass `params` through unmodified as a `SEXP`. In the last case, in your target function you will need to include ``, cast to `SEXP` and then pull it apart using the R API (or Rcpp). `ynames` Logical, indicating if the output should be named following the names of the input vector `y`. Alternatively, if `ynames` is a character vector of the same length as `y`, these will be used as the output names. `outnames` An optional character vector, used when `n_out` is greater than 0, to name the model output matrix. `return_by_column` Logical, indicating if the output should be returned organised by column (rather than row). This incurs a slight cost for transposing the matrices. If you can work with matrices that are transposed relative to `deSolve`, then set this to `FALSE`. `return_initial` Logical, indicating if the output should include the initial conditions. Specifying `FALSE` avoids binding this onto the output. `return_step` Logical, indicating if a row (or column if `return_by_column` is `TRUE`) representing step is included. `return_output_with_y` Logical, indicating if the output should be bound together with the returned matrix `y` (as it is with `deSolve`). If `FALSE`, then output will be returned as the attribute `output`. `restartable` Logical, indicating if the problem should be restartable. If `TRUE`, then the return value of a simulation can be passed to `difeq_restart` to continue the simulation after arbitrary changes to the state or the parameters. Note that this is really only useful for delay difference equations where you want to keep the history but make changes to the parameters or to the state vector while keeping the history of the problem so far. `return_minimal` Shorthand option - if set to `TRUE` then it sets all of `return_by_column`, `return_initial`, `return_time`, `return_output_with_y` to `FALSE` `as_array` (Defunct) Logical, indicating if the output should be converted into an array. If `TRUE` then ```res[, , i]``` will contain the `i`'th replicate, if `FALSE` then `res[[i]]` does instead. If both `as_array` and `restartable` are `TRUE`, then the attributes `ptr` and `restart_data` will be present as a `list` of restarting information for `difeq_continue`, though using these is not yet supported.

### Details

It is not currently possible to replicate over a set of parameters at once yet; the same parameter set will be used for all replications.

The details of how replication is done here are all considered implementation details and are up for change in the future - in particular if the models are run in turn or simultaneously (and the effect that has on the random number stream). Logic around naming output may change in future too; note that varying names in the `y` here will have some unexpected behaviours.

### Examples

``````
# Here is a really simple equation that does a random walk with
# steps that are normally distributed:
rhs <- function(i, y, p) y + runif(1)
y0 <- 1
t <- 0:10
p <- 5
dde::difeq_replicate(10, y0, t, rhs, p)
``````

dde documentation built on May 29, 2024, 8:15 a.m.