Solve a difference (or recurrence) equation by iterating it a number of times.
difeq(y, steps, target, parms, ..., n_out = 0L, n_history = 0L,
grow_history = FALSE, return_history = n_history > 0, dllname = "",
parms_are_real = TRUE, ynames = names(y), outnames = NULL,
return_by_column = TRUE, return_initial = TRUE, return_step = TRUE,
return_output_with_y = TRUE, restartable = FALSE,
return_minimal = FALSE)
difeq_continue(obj, steps, y = NULL, ..., copy = FALSE, parms = NULL,
return_history = NULL, return_by_column = NULL, return_initial = NULL,
return_step = NULL, return_output_with_y = NULL, restartable = NULL)
yprev(step, i = NULL)
The initial state of the system. Must be a numeric
vector (and will be passed through as.numeric
by this
function).
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).
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
.
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.
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.
The number of iterations of history to save during the simulation. By default, no history is saved.
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.
Logical indicating if history is to be
returned. By default, history is returned if n_history
is nonzero.
Name of the shared library (without extension) to
find the function func
in the case where func
refers to compiled function.
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 <Rinternals.h>
,
cast to SEXP
and then pull it apart using the R API (or
Rcpp).
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.
An optional character vector, used when
n_out
is greater than 0, to name the model output matrix.
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
.
Logical, indicating if the output should
include the initial conditions. Specifying FALSE
avoids
binding this onto the output.
Logical, indicating if a row (or column if
return_by_column
is TRUE
) representing step is included.
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
.
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.
Shorthand option - if set to TRUE
then it sets all of return_by_column
,
return_initial
, return_time
,
return_output_with_y
to FALSE
An object to continue from; this must be the results of
running a simulation with the option restartable =
TRUE
. Note that continuing a problem moves the pointer along
in time (unless copy = TRUE
, and that the incoming time
(times[[1]]
) must equal the previous time exactly.
Logical, indicating if the pointer should be copied
before continuing. If TRUE
, this is non-destructive with
respect to the data in the original pointer so the problem can
be restarted multiple times. By default this is FALSE
because there is a (potentially very small) cost to this
operation.
The step to access (not that this is not an offset,
but the actual step; within your target function you'd write
things like yprev(step - 1)
to get the previous step.
index within the state vector y
to return. The
index here is R-style base-1 indexing, so pass 1
in to
access the first element. This can be left NULL
to
return all the elements or a vector longer than one.
# Here is a really simple equation that just increases by 'p' each
# time (p is the parameter vector and could be any R structure).
rhs <- function(i, y, p) y + p
y0 <- 1
t <- 0:10
p <- 5
dde::difeq(y0, t, rhs, p)
#> [,1] [,2]
#> [1,] 0 1
#> [2,] 1 6
#> [3,] 2 11
#> [4,] 3 16
#> [5,] 4 21
#> [6,] 5 26
#> [7,] 6 31
#> [8,] 7 36
#> [9,] 8 41
#> [10,] 9 46
#> [11,] 10 51