Skip to content

Commit

Permalink
remove duplicated/overloaded r_iterate_dynamic_function
Browse files Browse the repository at this point in the history
  • Loading branch information
njtierney committed Dec 9, 2024
1 parent 17ee6d3 commit 29b0973
Showing 1 changed file with 29 additions and 25 deletions.
54 changes: 29 additions & 25 deletions tests/testthat/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -117,31 +117,35 @@ r_iterate_dynamic_function <- function(transition_function,
converged = as.integer(diff < tol),
max_iter = i)
}

r_iterate_dynamic_function <- function(transition_function, initial_state, niter = 100, tol = 1e-6, ...) {

states <- list(initial_state)

i <- 0L
diff <- Inf

while(i < niter & diff > tol) {
i <- i + 1L
states[[i + 1]] <- transition_function(states[[i]], i, ...)
growth <- states[[i + 1]] / states[[i]]
diffs <- growth - 1
diff <- max(abs(diffs))
}

all_states <- matrix(0, length(states[[1]]), niter)
states_keep <- states[-1]
all_states[, seq_along(states_keep)] <- t(do.call(rbind, states_keep))

list(stable_state = states[[i]],
all_states = all_states,
converged = as.integer(diff < tol),
max_iter = i)
}
#
# r_iterate_dynamic_function <- function(transition_function,
# initial_state,
# niter = 100,
# tol = 1e-6,
# ...) {
#
# states <- list(initial_state)
#
# i <- 0L
# diff <- Inf
#
# while(i < niter & diff > tol) {
# i <- i + 1L
# states[[i + 1]] <- transition_function(states[[i]], i, ...)
# growth <- states[[i + 1]] / states[[i]]
# diffs <- growth - 1
# diff <- max(abs(diffs))
# }
#
# all_states <- matrix(0, length(states[[1]]), niter)
# states_keep <- states[-1]
# all_states[, seq_along(states_keep)] <- t(do.call(rbind, states_keep))
#
# list(stable_state = states[[i]],
# all_states = all_states,
# converged = as.integer(diff < tol),
# max_iter = i)
# }

# a midpoint solver for use in deSolve, from the vignette p8
rk_midpoint <- deSolve::rkMethod(
Expand Down

0 comments on commit 29b0973

Please sign in to comment.