bilin: Fast 2D Linear interpolation

Description Usage Arguments Value Examples

View source: R/RcppExports.R

Description

Provides very fast 2D linear interpolation.

Usage

1
bilin( pt, x, y, f, n_x, n_y)

Arguments

pt

An (x,y) vector at which to approximately evaluate a function

x

A vector of x-values

y

A vector of y-values

f

A vector of z-values ordered first by x and then by y. If x=(x_1,x_2) and y=(y_1,y_2) then f = (f(x_1,y_1),f(x_1,y_2),f(x_2,y_1),f(x_2,y_2))

n_x

An integer. The length of x

n_y

An integer. The length of y

Value

Returns the bilinear interpolation of (x,y,f) evaluated at pt

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
## 2D Interpolation
f <- function(pt) pt[1] * cos(pt[2]) + pt[2] ^ 2 * sin(pt[1])
    # An arbitrary function
n.coarse <- 7
    # Number of points at which the interpolating grid is defined
n.fine <- 201
    # Number of poiints to evaluate the interpolation
xy.coarse <- cbind( rep( 1:n.coarse, each=n.coarse), rep( 1:n.coarse, n.coarse ) )
    # The matrix of coarse (x,y) values at which to evaluate f
v.f.w <- apply( xy.coarse, 1, f )
m.f.w <- matrix( v.f.w, n.coarse, n.coarse )
    # The vector & matrix form of the z-values

xy.fine <- seq( 1, n.coarse, length.out=n.fine )
    # A fine grid on which to evaluate the interpolation
m.xy.fine <- cbind( rep( xy.fine, each=n.fine), rep( xy.fine, n.fine ) )
    # The matrix form
v.f.v <- apply( m.xy.fine, 1, bilin, x=1:n.coarse, y=1:n.coarse, f=v.f.w, n_x=n.coarse, n_y=n.coarse )
    # The linear interpolation
v.f.true <- apply( m.xy.fine, 1, f )
    # The true value
m.f.v <- matrix( v.f.v, n.fine, n.fine )
m.f.true <- matrix( v.f.true, n.fine, n.fine )
    # Convert to matrices for plotting

image( 1:n.coarse, 1:n.coarse, m.f.w )
    # f evaluated on the approximating grid
image( xy.fine, xy.fine, m.f.v )
contour( xy.fine, xy.fine, m.f.v, add=TRUE )
    # The linear interpolation based on the coarse grid
image( xy.fine, xy.fine, m.f.true )
contour( xy.fine, xy.fine, m.f.true, add=TRUE )
    # The true function evaluated on the fine grid

squipbar/linBilin documentation built on May 30, 2019, 8:41 a.m.