evalongrid: Evaluate a function on a grid

Description Usage Arguments Details Value Examples

View source: R/tools.R

Description

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

Usage

1
2
3
evalongrid(fun, dims, intervals = NULL, ..., grid = NULL)

evalongridV(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).

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.

The function evalongridV is for vectorized functions, i.e. those that can take a matrix of column vectors as argument. It's equivalent to fun(t(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.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
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(sort(rnorm(3)),sort(rnorm(5)))
evalongrid(f,grid=grid)
g <- ipol(f,grid=grid,method='fh')
evalongridV(g, grid=grid, threads=2)
## vector valued function
f <- function(x) c(prod(x),sum(x^2))
evalongrid(f,grid=grid)

chebpol documentation built on Dec. 9, 2019, 5:08 p.m.