R/classes.R

#' LotkaVolterraParams
#'
#' Parameters used in the equations.
#'
#' @slot prey_percapita_growth Exponential growth rate of prey
#' @slot predation_rate Proportionality constant for probability of predation
#' @slot predation_efficiency Food conversion rate
#' @slot predator_percapita_degradation Exponential decay rate of predators
#'
#' @name LotkaVolterraParams-class
#' @rdname LotkaVolterraParams-class
#' @export
methods::setClass('LotkaVolterraParams', slots = list(
  prey_percapita_growth = 'numeric',
  predation_rate = 'numeric',
  predation_efficiency = 'numeric',
  predator_percapita_degradation = 'numeric'
  )
)


#' LotkaVolterraState
#'
#' State variables of predator-prey system.
#'
#' @slot prey_population double
#' @slot predator_population double
#'
#' @name LotkaVolterraState-class
#' @rdname LotkaVolterraState-class
#' @export
methods::setClass('LotkaVolterraState', slots = list(
  prey_population = 'numeric',
  predator_population = 'numeric'
))


#' Get object data in a compact representation
#'
#' Returns a named vector where each value corresponds to a slot in the class.
#' The names of the slots are also translated from long and descriptive names
#' to short and mathematical names.
#'
#' @param object object of class LotkaVolterraParams or LotkaVolterraState
#'
#' @return vector
#' @export
methods::setGeneric('write_compact', function (object) {
  standardGeneric('write_compact')
})


#' Get LotkaVolterraParams data in a compact representation
#'
#' Implementation of mmbr::write_compact() for an object of class
#' LotkaVolterraParams. In the returned vector, the names of the slots are
#' renamed to:
#' \itemize{
#'  \item{'prey_percapita_growth'}{alpha}
#'  \item{'predation_rate'}{beta}
#'  \item{'predation_efficiency'}{gamma}
#'  \item{'predator_percapita_degradation'}{delta}
#' }
#'
#' @param object object of class LotkaVolterraParams
#'
#' @return vector
#' @export
methods::setMethod('write_compact', 'LotkaVolterraParams',
  function(object) {
    return (c(
      alpha = object@prey_percapita_growth,
      beta  = object@predation_rate,
      gamma = object@predation_efficiency,
      delta = object@predator_percapita_degradation
    ))
})


#' Get LotkaVolterraState data in a compact representation
#'
#' Implementation of mmbr::write_compact() for an object of class
#' LotkaVolterraState. In the returned vector, the names of the slots are
#' renamed to:
#' \itemize{
#'  \item{'prey_population'}{x}
#'  \item{'predator_population'}{y}
#' }
#'
#' @param object object of class LotkaVolterraState
#'
#' @return vector
#' @export
methods::setMethod('write_compact', 'LotkaVolterraState',
  function (object) {
    return (c(
      x = object@prey_population,
      y = object@predator_population
    ))
  }
)
atmunr/mmbr documentation built on Oct. 6, 2020, 2:51 a.m.