Learn R Programming

chebpol (version 1.3-952)

evalongrid: Evaluate a function on a grid

Description

Evaluate a function on a Chebyshev grid, or on a user-specified grid.

Usage

evalongrid(fun, dims, intervals=NULL, ..., grid=NULL)

Arguments

fun
Multivariate real-valued function to be evaluated. Must be defined on the hypercube described by intervals.
dims
A vector of integers. The number of grid-points in each dimension.
intervals
A list. Each entry is a vector of length 2 with the lower and upper end of the interval in each dimension.
...
Further arguments to fun.
grid
Rather than specifying dims and intervals to get a Chebyshev grid, you may specify your own grid as a list of vectors whose Cartesian product will be the grid, as in expand.grid(grid).

Value

  • An array with the value of fun on each grid point. The dim attribute has been appropriately set for the grid. If fun returns a vector, this will be the first dimension of the returned array.

concept

Chebyshev polynomial

Details

The function fun should be a function(x,...), where length(x) equals length(dims) (or length(grid)).

If grid is provided, fun is evaluated on each point in the Cartesian product of the vectors in grid.

If intervals is not provided, it is assumed that the domain of the function is the hypercube [-1,1] x [-1,1] x ... x [-1,1]. Thus, the function is evaluated on a standard Chebyshev grid.

If intervals is provided, it should be a list with elements of length 2, providing minimum and maximum for each dimension.

The grid itself may be produced by expand.grid(chebknots(dims,intervals)), or expand.grid(grid).

This function does the same as apply(expand.grid(grid),1,fun), but it's faster and more memory-efficient for large grids because it does not actually expand the grid.

Examples

Run this code
f <- function(x) {a <- sum(x^2); ifelse(a == 0,0,exp(-1/a))}
## Standard Chebyshev grid
evalongrid(f,dims=c(3,5))
## Then Chebyshev on [0,1] x [2,3]
evalongrid(f,dims=c(3,5),intervals=list(c(0,1),c(2,3)))
## And on my own grid
grid <- list(rnorm(3),rnorm(5))
evalongrid(f,grid=grid)
## vector valued function
f <- function(x) c(prod(x),sum(x^2))
evalongrid(f,grid=grid)

Run the code above in your browser using DataLab