R/grid_functions.R

Defines functions grid_function

Documented in grid_function

#' Discretize function over space-time grid
#'
#' @param f function to evaluate on grid
#' @param x space variable
#' @param t time variable, can be null to discretize a function over just space.
#'
#' @description {Discretizes a function of space-time over a grid.}
#' @details {Simple double for-loop for space-time functions and one for-loop for functions of a single-variable}
#' @return matrix
#' @export grid_function
grid_function <- function(f, x, t = NULL)
{
  # If both x and t variable are present
  if(!is.null(t))
  {
    N <- length(t)
    M <- length(x)
    m <- matrix(data = 0, nrow = N, ncol = M)
    for(i in 1:N)
    {
      for(j in 1:M)
      {
        m[i, j] <- f(x[j], t[i])
      }
    }
  } else if(is.null(t)){
    n <- length(x)
    m <- matrix(data = 0, nrow = n)
    for(j in 1:n)
    {
      m[j] <- f(x[j])
    }
  }
  return(m)
}
shill1729/FeynmanKacSolver documentation built on May 19, 2020, 8:23 p.m.