splinefun <- function(x, y, method="fmm")
{
	if (!is.numeric(x) || !is.numeric(y))
		stop("splinefun: x and y must be numeric")
	n <- length(x)
	if (n != length(y))
		stop("x and y must have equal lengths")
	method <- match(method, c("periodic", "natural", "fmm"))
	if(is.na(method))
		stop("splinefun: invalid interpolation method")
##<TSL>	if(any(diff(x) <= 0))
##		stop("invalid x array in spline")
	o<-order(x)
	x<-x[o]
	y<-y[o]
	if(method == 1 && y[1] != y[n]) {
		warning("first and last y values differ in spline - using y[1] for both")
		y[n] <- y[1]
	}
	z <- .C("spline_coef",
		method=as.integer(method),
		n=n,
		x=as.double(x),
		y=as.double(y),
		b=double(n),
		c=double(n),
		d=double(n),
		e=double(if(method == 1) n else 0))
	rm(x,y,n,method,o)
	function(x) {
		.C("spline_eval",
			z$method,
			length(x),
			x=as.double(x),
			y=double(length(x)),
			z$n,
			z$x,
			z$y,
			z$b,
			z$c,
			z$d)$y
	}
}
