Last chance! 50% off unlimited learning
Sale ends in
evalongrid(fun, dims, intervals=NULL, ..., grid=NULL)
intervals
.grid
as a list of vectors whose Cartesian
product will be the grid, as in expand.grid(grid)
.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.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.
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