# sps_prep: Prepare list of objects for shape preserving interpolation. In spsi: Shape-Preserving Uni-Variate and Bi-Variate Spline Interpolation

## Description

This is primitive function which output is used by `sps_eval`. Function works with both uni- and bi-variate functions and returns list consisting original data provided, values of the derivatives and degrees of polynomial needed to satisfy the shape constraints required

## Usage

 ```1 2 3 4 5 6``` ```sps_prep(x, y, z=NULL, fx = NA, fy = NA, fxy = NA, shape = c("monotonicity", "curvature"), shape.x = shape, shape.y = shape, max.deg = 50, smoothness = 1, tol = 0.0001) ```

## Arguments

 `x` Vector of x-coordinates of points to be interpolated. `y` Vector of y-coordinates of points to be interpolated. For uni-variate case length should be equal to length of `x` and y[i] = f(x[i]). For bi-variate case length may differ. `z` Bi-variate case only. Matrix of values of function on grid spanned by x and y; z[i,j] = f(x[i],y[j]) OR Vector of values of functions on points (x, y); z[i] = f(x[i],y[i]). In this case length of all three vectors should be equal and it should be possible to transform the point to a gridded form. `fx` Matrix of values of the derivative with respect to x of the function; fx[i,j] = fx(x[i],y[j]); By default it is estimated internally. `fy` Bi-variate case only. Matrix of values of the derivative with respect to y of the function; fy[i,j] = fy(x[i],y[j]); By default it is estimated internally. `fxy` Bi-variate case only. Matrix of values of mixed partial derivative of the function; fxy[i,j] = fxy(x[i],y[j]); By default it is estimated internally. `shape` Specifies which attributes should be preserved. Vector should contain 'monotonicity' and/or 'curvature' only. In bi-variate case this can be set separately for both dimensions using `shape.x`, `shape.y`. `shape.x` Specifies which attributes should be preserved for x dimension. `shape.y` Specifies which attributes should be preserved for y dimension. `max.deg` Specifies maximum degree of polynomial allowed. In some cases in order to preserve shape, very high degrees are necessary. If maximum degree is reached, it is not guaranteed that resulting spline will preserve all the attributes required. `smoothness` How many times does the spline needs to be differentiable. `tol` Tolerance used within program. Default value is suitable for graphical purposes.

## Details

If `z` is not provided function will prepare list needed for uni-variate interpolation. If values of the derivatives are provided, resulting spline will preserve them. For bi-variate case it is possible to set some derivatives and let program estimate the rest.

## Value

Uni-variate: list with 6 components:

 `x,y` Coordinates of the data points `k` Smoothness (or continuity class) required `fx` Estimated or given values of derivative `deg` Degree of polynomial needed on each of the line segments `dim` Number of variables; numeric equal to 1

Bi-variate: list with 10 components:

 `x,y` Coordinates of the data points `z` Matrix of vales of the function `k` Smoothness (or continuity class) required `fx, fy, fxy` Estimated or given values of respective derivatives `deg.x, deg.y` Degree of polynomial needed on each of the line segments in each dimension `dim` Number of variables; numeric equal to 2

## Author(s)

Szymon Sacher <[email protected]> & Andrew Clausen <[email protected]>

## References

Costantini, P; Fontanella, F; 'Shape Preserving Bi-variate Interpolation' SIAM J NUMER. ANAL. Vol. 27, No.2, pp. 488-506, April 1990

`sps_eval` `sps_fun`
 ``` 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``` ```## Univariate example x <- c( 1, 2, 3, 4, 5, 6) y <- c(16 ,18, 21, 17, 15, 12) spline <- sps_prep(x, y, shape = 'monotonicity', smoothness = 2) plot(seq(1, 6, 0.1), sps_eval(spline, seq(1, 6, 0.1))) ## Bivariate example tower <- function(x, y) { X <- abs(x) Y <- abs(y) ifelse((X + Y) <= 1, floor(3*(1 - X - Y)), ifelse(pmax(X, Y) >= 1, pmax(X, Y)/2 - 0.5, 0)) } X <- Y <- seq(-1.25, 1.25, 2.5/13) grid <- mesh(X, Y) Z <- tower(grid\$x, grid\$y) spline <- sps_prep(X, Y, Z) X_ <- Y_ <- seq(-1.25, 1.25, 2.5/60) persp3D(X_, Y_, sps_eval(spline, x = X_, y = Y_, grid = TRUE)) ```