lin: Fast 1D Linear interpolation

Description Usage Arguments Value Examples

View source: R/RcppExports.R

Description

Provides very fast 1D linear interpolation. Typically about 10 times faste than R's built-in approx function.

Usage

1
lin( x, y, pt, n_x)

Arguments

x

A vector of x-values

y

A vector of y-values

pt

An x-value

n_x

An integer. The length of x (and y).

Value

Returns the linear interpolation of (x,x) evaluated at x=pt. That is, if x= (x_1, x_2, …, x_n), y= (y_1, y_2, …, y_n), and pt= x, x_i < x ≤ x_{i+1}, then the output is:

y = ≤ft( \frac{x - x_i}{x_{i+1} - x_i} \right) y_i + ≤ft( 1 - \frac{x - x_i}{x_{i+1} - x_i} \right) y_{i+1}

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
## 1D Interpolation
f.1 <- function(x) x^2
n <- 5
x.grid <- seq( 0, 10, length.out = n )
f.1.grid <- sapply( x.grid, f.1 )
lin( x.grid, f.1.grid, 3, n ) == 3
    # Check interpolation
x.grid.fine <- seq( 0, 10, length.out = n^2 )
plot( x.grid.fine, sapply( x.grid.fine, f.1 ),
      main='Exact and linear interpoliation of x^2', type='l' )
lines( x.grid.fine, sapply( x.grid.fine, lin, x=x.grid, y=f.1.grid, n_x=n ), 
        col='red' )
    # Compare the exact and approximate solution
library(microbenchmark)
microbenchmark(sapply( x.grid.fine, lin, x=x.grid, y=f.1.grid, n_x=n ))
microbenchmark(sapply( x.grid.fine, approx, x=x.grid, y=f.1.grid ))
    # lin is about 10x faster than R's built-in approx function

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