trilin: Fast 3D linear interpolation

Description Usage Arguments Value Examples

View source: R/RcppExports.R

Description

Takes values of a function evaluated on a grid and computes an approximate value on the interior of the grid using linear interpolation.

Usage

1
trilin(pt, x, y, z, m_f, n_x, n_y, n_z)

Arguments

pt

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

x

A vector of x-values

y

A vector of y-values

z

A vector of z-values

m_f

The matrix of function values on the grid x \times y \times z. Must be ordered vertically by x and then y, and horizontally by z. For example, if x=(x_1,x_2), y=(y_1,y_2), and z=(z_1,z_2), then m_f is:

R=≤ft[ \begin{array}{cc} f(x_1, y_1, z_1) & f(x_1, y_1, z_2) \\ f(x_1, y_2, z_1) & f(x_1, y_2, z_2) \\ f(x_2, y_1, z_1) & f(x_2, y_1, z_2) \\ f(x_2, y_2, z_1) & f(x_2, y_2, z_2) \end{array}\right]

n_x

An integer. The length of x

n_y

An integer. The length of y

n_z

An integer. The length of z

Value

Returns the trilinear interpolation of (x,y,z,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
f <- function(pt) pt[1] + pt[2] * pt[3] - pt[3] * pt[1]
    # An arbitrary function
nn <- 7
    # Number of points at which the interpolating grid is defined
v.x <- v.y <- v.z <- 1:nn
    # The vector of x, y, and z values (need not all be the same)
m.xy <- cbind( rep( v.x, each=nn), rep( v.y, nn ) )
    # The matrix of (x,y) values at which to evaluate f
v.f <- t( apply( m.xy, 1, function( xy ) sapply( v.z, function(z) f( c( xy, z ) ) ) ) )
    # The matrix of the z-values

test.pt <- 1:3
print(f(test.pt))
print(trilin( test.pt, v.x, v.y, v.z, v.f, 7, 7, 7 ) )
    # Is exact at grid points

test.pt.2 <- 1:3 + .2
print(f(test.pt.2))
print(trilin( test.pt.2, v.x, v.y, v.z, v.f, 7, 7, 7 ) )
    # Approximation is still exact with quadratic functions

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