Title: | Some Interpolation Methods |
---|---|
Description: | Some interpolation methods taken from 'Boost': barycentric rational interpolation, modified Akima interpolation, PCHIP (piecewise cubic Hermite interpolating polynomial) interpolation, and Catmull-Rom splines. |
Authors: | Stéphane Laurent [aut, cre] |
Maintainer: | Stéphane Laurent <[email protected]> |
License: | GPL-3 |
Version: | 1.0.1 |
Built: | 2024-11-04 03:02:45 UTC |
Source: | https://github.com/stla/interpolators |
Evaluation of an interpolator at some given values.
evalInterpolator(ipr, x, derivative = 0)
evalInterpolator(ipr, x, derivative = 0)
ipr |
an interpolator |
x |
numeric vector giving the values to be interpolated; missing values are not allowed; for Catmull-Rom splines, the values must be between 0 and 1 |
derivative |
order of differentiation, 0 or 1 |
Numeric vector of interpolated values, or numeric matrix of interpolated points for the Catmull-Rom interpolator.
Barycentric rational interpolator.
iprBarycentricRational(x, y, ao = 3)
iprBarycentricRational(x, y, ao = 3)
x , y
|
numeric vectors giving the coordinates of the known points, without missing value |
ao |
approximation order, an integer greater than or equal to 3 |
See Barycentric rational interpolation.
An interpolator, for usage in evalInterpolator
.
library(interpolators) x <- c(1, 2, 4, 5) y <- x^2 ipr <- iprBarycentricRational(x, y) evalInterpolator(ipr, c(2, 3)) evalInterpolator(ipr, c(2, 3), derivative = 1)
library(interpolators) x <- c(1, 2, 4, 5) y <- x^2 ipr <- iprBarycentricRational(x, y) evalInterpolator(ipr, c(2, 3)) evalInterpolator(ipr, c(2, 3), derivative = 1)
Catmull-Rom interpolator for 2-dimensional or 3-dimensional points.
iprCatmullRom(points, closed = FALSE, alpha = 0.5)
iprCatmullRom(points, closed = FALSE, alpha = 0.5)
points |
numeric matrix of 2D or 3D points, one point per row |
closed |
Boolean, whether the curve is closed |
alpha |
parameter between 0 and 1; the default value 0.5 is recommended |
See Catmull-Rom splines.
An interpolator, for usage in evalInterpolator
.
library(interpolators) points <- rbind( c(0, 2.5), c(2, 4), c(3, 2), c(4, 1.5), c(5, 6), c(6, 5), c(7, 3), c(9, 1), c(10, 2.5), c(11, 7), c(9, 5), c(8, 6), c(7, 5.5) ) ipr <- iprCatmullRom(points) s <- seq(0, 1, length.out = 400) Curve <- evalInterpolator(ipr, s) head(Curve) plot(Curve, type = "l", lwd = 2) points(points, pch = 19) # a closed example (pentagram) #### rho <- sqrt((5 - sqrt(5))/10) R <- sqrt((25 - 11*sqrt(5))/10) points <- matrix(NA_real_, nrow = 10L, ncol = 2L) points[c(1, 3, 5, 7, 9), ] <- t(vapply(0:4, function(i){ c(rho*cospi(2*i/5), rho*sinpi(2*i/5)) }, numeric(2L))) points[c(2, 4, 6, 8, 10), ] <- t(vapply(0:4, function(i){ c(R*cospi(2*i/5 + 1/5), R*sinpi(2*i/5 + 1/5)) }, numeric(2L))) ipr <- iprCatmullRom(points, closed = TRUE) s <- seq(0, 1, length.out = 400L) Curve <- evalInterpolator(ipr, s) plot(Curve, type = "l", lwd = 2, asp = 1) points(points, pch = 19)
library(interpolators) points <- rbind( c(0, 2.5), c(2, 4), c(3, 2), c(4, 1.5), c(5, 6), c(6, 5), c(7, 3), c(9, 1), c(10, 2.5), c(11, 7), c(9, 5), c(8, 6), c(7, 5.5) ) ipr <- iprCatmullRom(points) s <- seq(0, 1, length.out = 400) Curve <- evalInterpolator(ipr, s) head(Curve) plot(Curve, type = "l", lwd = 2) points(points, pch = 19) # a closed example (pentagram) #### rho <- sqrt((5 - sqrt(5))/10) R <- sqrt((25 - 11*sqrt(5))/10) points <- matrix(NA_real_, nrow = 10L, ncol = 2L) points[c(1, 3, 5, 7, 9), ] <- t(vapply(0:4, function(i){ c(rho*cospi(2*i/5), rho*sinpi(2*i/5)) }, numeric(2L))) points[c(2, 4, 6, 8, 10), ] <- t(vapply(0:4, function(i){ c(R*cospi(2*i/5 + 1/5), R*sinpi(2*i/5 + 1/5)) }, numeric(2L))) ipr <- iprCatmullRom(points, closed = TRUE) s <- seq(0, 1, length.out = 400L) Curve <- evalInterpolator(ipr, s) plot(Curve, type = "l", lwd = 2, asp = 1) points(points, pch = 19)
Modified Akima interpolator.
iprMakima(x, y)
iprMakima(x, y)
x , y
|
numeric vectors giving the coordinates of the known points, without missing value |
See Modified Akima interpolation.
An interpolator, for usage in evalInterpolator
.
library(interpolators) x <- seq(0, 4*pi, length.out = 9L) y <- x - sin(x) ipr <- iprMakima(x, y) curve(x - sin(x), from = 0, to = 4*pi, lwd = 2) curve( evalInterpolator(ipr, x), add = TRUE, col = "blue", lwd = 3, lty = "dashed" ) points(x, y, pch = 19)
library(interpolators) x <- seq(0, 4*pi, length.out = 9L) y <- x - sin(x) ipr <- iprMakima(x, y) curve(x - sin(x), from = 0, to = 4*pi, lwd = 2) curve( evalInterpolator(ipr, x), add = TRUE, col = "blue", lwd = 3, lty = "dashed" ) points(x, y, pch = 19)
PCHIP interpolator. It is monotonic.
iprPCHIP(x, y)
iprPCHIP(x, y)
x , y
|
numeric vectors giving the coordinates of the known points, without missing value |
See PCHIP interpolation.
An interpolator, for usage in evalInterpolator
.
library(interpolators) x <- seq(0, 4*pi, length.out = 9L) y <- x - sin(x) ipr <- iprPCHIP(x, y) curve(x - sin(x), from = 0, to = 4*pi, lwd = 2) curve( evalInterpolator(ipr, x), add = TRUE, col = "blue", lwd = 3, lty = "dashed" ) points(x, y, pch = 19)
library(interpolators) x <- seq(0, 4*pi, length.out = 9L) y <- x - sin(x) ipr <- iprPCHIP(x, y) curve(x - sin(x), from = 0, to = 4*pi, lwd = 2) curve( evalInterpolator(ipr, x), add = TRUE, col = "blue", lwd = 3, lty = "dashed" ) points(x, y, pch = 19)