D1tr <- function(y, x = 1)1{2## Purpose: discrete trivial estimate of 1st derivative.3## ----------------------------------------------------------------------4## Arguments: x is optional; let's say we don't like "'"5## ----------------------------------------------------------------------6## Author: Martin Maechler, ~ 19907n <- length(y)89if(length(x) == 1)10c(y[2] - y[1], 0.5 * (y[-(1:2)] - y[-((n-1):n)]), y[n] - y[n-1])/x11else {12## Here, already using non-matching apostrophes, but developer mode13## still seems to work ..14if(n != length(x)) stop("lengths' of x & 'y' must equal")15if(is.unsorted(x)) stop("'x' must be sorted !")16c(y[2] - y[1], 0.5 * (y[-(1:2)] - y[-((n-1):n)]), y[n] - y[n-1]) /17c(x[2] - x[1], 0.5 * (x[-(1:2)] - x[-((n-1):n)]), x[n] - x[n-1])18}19}202122