difeq: Solve difference equation

View source: R/difeq.R

difeqR Documentation

Solve difference equation

Description

Solve a difference (or recurrence) equation by iterating it a number of times.

Usage

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)

Arguments

y

The initial state of the system. Must be a numeric vector (and will be passed through as.numeric by this function).

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 <Rinternals.h>, 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

obj

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.

copy

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.

step

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.

i

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.

Examples


# 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)

dde documentation built on Sept. 23, 2024, 5:09 p.m.