R/Integrator.R

#' The base class for numerical integration.
#'
#' @slot result numeric. The result of the integration.
#' @slot x numeric. A vector of x-values for the integration.
#' @slot y numeric. A vector of y-values for the function being integrated.
#' @slot start numeric. The starting value for the integral.
#' @slot end numeric. The ending value for the integral.
#'
#' @export
Integrator = setClass("Integrator", slots = c(result = "numeric", x = "numeric", y = "numeric", start = "numeric", end = "numeric"))

#' Performs numerical integration
#'
#' @param type The type of integration to perform - either "Simp" or "Trap".
#' @param x A vector of x-values.
#' @param y A vector of y-values.
#' @param start The starting value for the integral.
#' @param end The ending value for the integral.
#'
#' @return An object of the proper type, integrated.
#' @export
#'
#' @examples
#' integrateIt("Trap", 1:10, 1:10, 1, 10)
integrateIt = function(type, x, y, start, end, ...) {
	switch(type,
		   "Trap" = return(Trapezoid(x, y, start, end)),
		   "Simp" = return(Simpson(x, y, start, end)),
		   stop("Only 'Trap' and 'Simp' are valid values."))
}

#' Creates a generic for \link{integrateIt},
#' using that as the default value.
#'
#' @return An object of the proper type, integrated.
#' @export
#' @examples
#' integrateIt("Trap", 1:10, 1:10, 1, 10)
setGeneric("integrateIt")
alexjweil/integrateIt documentation built on May 10, 2019, 8:54 a.m.