swh:1:snp:81eadaa089e8253d8469bcef66aa332632c6c669
Tip revision: c1688b374d201c13fb40b4dda2d2a89e34b94ec6 authored by Hans W. Borchers on 23 January 2021, 09:10:02 UTC
version 2.3.3
version 2.3.3
Tip revision: c1688b3
piecewise.R
##
## p i e c e w i s e . R Piecewise Linear Function
##
piecewise <- function(x, y, abs = FALSE)
{
n <- length(x)
areas <- 0.0
zeros <- if (y[1] == 0) c(x[1]) else c()
for (i in 2:n) {
if (y[i]*y[i-1] >= 0) {
if (y[i] == 0) zeros <- c(zeros, x[i])
areas <- c(areas, (y[i]+y[i-1]) * (x[i]-x[i-1]) / 2.0)
} else {
x0 <- (x[i-1]*y[i] - x[i]*y[i-1])/(y[i] - y[i-1])
zeros <- c(zeros, x0)
areas <- c(areas, y[i-1]*(x0-x[i-1])/2.0, y[i]*(x[i]-x0)/2.0)
}
}
area <- if (abs) sum(abs(areas)) else sum(areas)
return(list(area=area, zeros=zeros))
}